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
10 #include <stdio.h>
11 #include <string.h>
12
13 #include <openssl/opensslconf.h>
14 #include "internal/quic_srt_gen.h"
15
16 #include "testutil.h"
17 #include "testutil/output.h"
18
19 struct test_case {
20 const unsigned char *key;
21 size_t key_len;
22 QUIC_CONN_ID dcid;
23 QUIC_STATELESS_RESET_TOKEN expected;
24 };
25
26 static const unsigned char key_1[] = { 0x01, 0x02, 0x03 };
27
28 static const unsigned char key_2[] = {
29 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
30 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
31 };
32
33 static const struct test_case tests[] = {
34 {
35 key_1, sizeof(key_1), { 2, { 0x55, 0x66 } },
36 {{ 0x02,0x9e,0x8f,0x3d,0x1e,0xa9,0x06,0x23,0xb2,0x43,0xd2,0x19,0x59,0x8a,0xa1,0x66 }}
37 },
38 {
39 key_2, sizeof(key_2), { 0, { 0 } },
40 {{ 0x93,0x10,0x2f,0xc7,0xaf,0x9d,0x9b,0x28,0x3f,0x84,0x95,0x6b,0xa3,0xdc,0x07,0x6b }}
41 },
42 {
43 key_2, sizeof(key_2),
44 { 20, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } },
45 {{ 0x9a,0x98,0x98,0x61,0xbe,0xfd,0xe3,0x05,0x45,0xac,0x66,0xcf,0x3b,0x58,0xfb,0xab }}
46 }
47 };
48
test_srt_gen(int idx)49 static int test_srt_gen(int idx)
50 {
51 int testresult = 0;
52 const struct test_case *t = &tests[idx];
53 QUIC_SRT_GEN *srt_gen = NULL;
54 QUIC_STATELESS_RESET_TOKEN token;
55 size_t i;
56
57 if (!TEST_ptr(srt_gen = ossl_quic_srt_gen_new(NULL, NULL,
58 t->key, t->key_len)))
59 goto err;
60
61 for (i = 0; i < 2; ++i) {
62 memset(&token, 0xff, sizeof(token));
63
64 if (!TEST_true(ossl_quic_srt_gen_calculate_token(srt_gen, &t->dcid,
65 &token)))
66 goto err;
67
68 if (!TEST_mem_eq(token.token, sizeof(token.token),
69 &t->expected, sizeof(t->expected)))
70 goto err;
71 }
72
73 testresult = 1;
74 err:
75 ossl_quic_srt_gen_free(srt_gen);
76 return testresult;
77 }
78
setup_tests(void)79 int setup_tests(void)
80 {
81 ADD_ALL_TESTS(test_srt_gen, OSSL_NELEM(tests));
82 return 1;
83 }
84