quic_srt_gen_test.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <openssl/opensslconf.h>
  12. #include "internal/quic_srt_gen.h"
  13. #include "testutil.h"
  14. #include "testutil/output.h"
  15. struct test_case {
  16. const unsigned char *key;
  17. size_t key_len;
  18. QUIC_CONN_ID dcid;
  19. QUIC_STATELESS_RESET_TOKEN expected;
  20. };
  21. static const unsigned char key_1[] = { 0x01, 0x02, 0x03 };
  22. static const unsigned char key_2[] = {
  23. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  24. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
  25. };
  26. static const struct test_case tests[] = {
  27. {
  28. key_1, sizeof(key_1), { 2, { 0x55, 0x66 } },
  29. {{ 0x02,0x9e,0x8f,0x3d,0x1e,0xa9,0x06,0x23,0xb2,0x43,0xd2,0x19,0x59,0x8a,0xa1,0x66 }}
  30. },
  31. {
  32. key_2, sizeof(key_2), { 0, { 0 } },
  33. {{ 0x93,0x10,0x2f,0xc7,0xaf,0x9d,0x9b,0x28,0x3f,0x84,0x95,0x6b,0xa3,0xdc,0x07,0x6b }}
  34. },
  35. {
  36. key_2, sizeof(key_2),
  37. { 20, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } },
  38. {{ 0x9a,0x98,0x98,0x61,0xbe,0xfd,0xe3,0x05,0x45,0xac,0x66,0xcf,0x3b,0x58,0xfb,0xab }}
  39. }
  40. };
  41. static int test_srt_gen(int idx)
  42. {
  43. int testresult = 0;
  44. const struct test_case *t = &tests[idx];
  45. QUIC_SRT_GEN *srt_gen = NULL;
  46. QUIC_STATELESS_RESET_TOKEN token;
  47. size_t i;
  48. if (!TEST_ptr(srt_gen = ossl_quic_srt_gen_new(NULL, NULL,
  49. t->key, t->key_len)))
  50. goto err;
  51. for (i = 0; i < 2; ++i) {
  52. memset(&token, 0xff, sizeof(token));
  53. if (!TEST_true(ossl_quic_srt_gen_calculate_token(srt_gen, &t->dcid,
  54. &token)))
  55. goto err;
  56. if (!TEST_mem_eq(token.token, sizeof(token.token),
  57. &t->expected, sizeof(t->expected)))
  58. goto err;
  59. }
  60. testresult = 1;
  61. err:
  62. ossl_quic_srt_gen_free(srt_gen);
  63. return testresult;
  64. }
  65. int setup_tests(void)
  66. {
  67. ADD_ALL_TESTS(test_srt_gen, OSSL_NELEM(tests));
  68. return 1;
  69. }