xref: /openssl/test/quic_lcidm_test.c (revision 935aa143)
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 "internal/quic_lcidm.h"
11 #include "testutil.h"
12 
13 static char ptrs[8];
14 
15 static const QUIC_CONN_ID cid8_1 = { 8, { 1 } };
16 static const QUIC_CONN_ID cid8_2 = { 8, { 2 } };
17 static const QUIC_CONN_ID cid8_3 = { 8, { 3 } };
18 static const QUIC_CONN_ID cid8_4 = { 8, { 4 } };
19 static const QUIC_CONN_ID cid8_5 = { 8, { 5 } };
20 
test_lcidm(void)21 static int test_lcidm(void)
22 {
23     int testresult = 0;
24     QUIC_LCIDM *lcidm;
25     size_t lcid_len = 10; /* != ODCID len */
26     QUIC_CONN_ID lcid_1, lcid_dummy, lcid_init = {0};
27     OSSL_QUIC_FRAME_NEW_CONN_ID ncid_frame_1, ncid_frame_2, ncid_frame_3;
28     void *opaque = NULL;
29     uint64_t seq_num = UINT64_MAX;
30     int did_retire = 0;
31 
32     if (!TEST_ptr(lcidm = ossl_quic_lcidm_new(NULL, lcid_len)))
33         goto err;
34 
35     if (!TEST_size_t_eq(ossl_quic_lcidm_get_lcid_len(lcidm), lcid_len))
36         goto err;
37 
38     if (!TEST_true(ossl_quic_lcidm_enrol_odcid(lcidm, ptrs + 0, &cid8_1))
39         || !TEST_false(ossl_quic_lcidm_enrol_odcid(lcidm, ptrs + 0, &cid8_2))
40         || !TEST_false(ossl_quic_lcidm_enrol_odcid(lcidm, ptrs + 1, &cid8_1))
41         || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 1), 0)
42         || !TEST_true(ossl_quic_lcidm_enrol_odcid(lcidm, ptrs + 1, &cid8_3))
43         || !TEST_false(ossl_quic_lcidm_enrol_odcid(lcidm, ptrs + 1, &cid8_4))
44         || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 0), 1)
45         || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 1), 1)
46         || !TEST_true(ossl_quic_lcidm_retire_odcid(lcidm, ptrs + 0))
47         || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 0), 0)
48         || !TEST_false(ossl_quic_lcidm_enrol_odcid(lcidm, ptrs + 0, &cid8_1))
49         || !TEST_false(ossl_quic_lcidm_enrol_odcid(lcidm, ptrs + 0, &cid8_5))
50         || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 0), 0)
51 
52         || !TEST_true(ossl_quic_lcidm_generate_initial(lcidm, ptrs + 2, &lcid_1))
53         || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 2), 1)
54         || !TEST_false(ossl_quic_lcidm_generate_initial(lcidm, ptrs + 2, &lcid_init))
55         || !TEST_true(ossl_quic_lcidm_generate(lcidm, ptrs + 2, &ncid_frame_1))
56         || !TEST_true(ossl_quic_lcidm_generate(lcidm, ptrs + 2, &ncid_frame_2))
57         || !TEST_true(ossl_quic_lcidm_generate(lcidm, ptrs + 2, &ncid_frame_3))
58         || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 2), 4)
59         || !TEST_true(ossl_quic_lcidm_lookup(lcidm, &lcid_1, &seq_num, &opaque))
60         || !TEST_ptr_eq(opaque, ptrs + 2)
61         || !TEST_uint64_t_eq(seq_num, 0)
62         || !TEST_true(ossl_quic_lcidm_lookup(lcidm, &ncid_frame_1.conn_id,
63                                              &seq_num, &opaque))
64         || !TEST_ptr_eq(opaque, ptrs + 2)
65         || !TEST_uint64_t_eq(seq_num, 1)
66         || !TEST_true(ossl_quic_lcidm_lookup(lcidm, &ncid_frame_2.conn_id,
67                                              &seq_num, &opaque))
68         || !TEST_ptr_eq(opaque, ptrs + 2)
69         || !TEST_uint64_t_eq(seq_num, 2)
70         || !TEST_true(ossl_quic_lcidm_lookup(lcidm, &ncid_frame_3.conn_id,
71                                              &seq_num, &opaque))
72         || !TEST_ptr_eq(opaque, ptrs + 2)
73         || !TEST_uint64_t_eq(seq_num, 3)
74         || !TEST_true(ossl_quic_lcidm_retire(lcidm, ptrs + 2, 2, NULL,
75                                              &lcid_dummy, &seq_num, &did_retire))
76         || !TEST_true(did_retire)
77         || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 2), 3)
78         || !TEST_true(ossl_quic_lcidm_retire(lcidm, ptrs + 2, 2, NULL,
79                                              &lcid_dummy, &seq_num, &did_retire))
80         || !TEST_true(did_retire)
81         || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 2), 2)
82         || !TEST_true(ossl_quic_lcidm_retire(lcidm, ptrs + 2, 2, NULL,
83                                              &lcid_dummy, &seq_num, &did_retire))
84         || !TEST_false(did_retire)
85         || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 2), 2)
86         || !TEST_false(ossl_quic_lcidm_lookup(lcidm, &lcid_init,
87                                               &seq_num, &opaque))
88         || !TEST_false(ossl_quic_lcidm_lookup(lcidm, &ncid_frame_1.conn_id,
89                                               &seq_num, &opaque))
90         || !TEST_true(ossl_quic_lcidm_lookup(lcidm, &ncid_frame_2.conn_id,
91                                              &seq_num, &opaque))
92         || !TEST_true(ossl_quic_lcidm_cull(lcidm, ptrs + 2))
93         || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 2), 0))
94         goto err;
95 
96     testresult = 1;
97 err:
98     ossl_quic_lcidm_free(lcidm);
99     return testresult;
100 }
101 
setup_tests(void)102 int setup_tests(void)
103 {
104     ADD_TEST(test_lcidm);
105     return 1;
106 }
107