xref: /openssl/test/quic_rcidm_test.c (revision b6461792)
1 /*
2  * Copyright 2023-2024 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_rcidm.h"
11 #include "testutil.h"
12 
13 static const QUIC_CONN_ID cid8_1 = { 8, { 1 } };
14 static const QUIC_CONN_ID cid8_2 = { 8, { 2 } };
15 static const QUIC_CONN_ID cid8_3 = { 8, { 3 } };
16 static const QUIC_CONN_ID cid8_4 = { 8, { 4 } };
17 static const QUIC_CONN_ID cid8_5 = { 8, { 5 } };
18 
19 /*
20  * 0: Client, Initial ODCID
21  * 1: Client, Initial ODCID + Retry ODCID
22  * 2: Server, doesn't start with Initial ODCID
23  */
test_rcidm(int idx)24 static int test_rcidm(int idx)
25 {
26     int testresult = 0;
27     QUIC_RCIDM *rcidm;
28     OSSL_QUIC_FRAME_NEW_CONN_ID ncid_frame_1 = {0}, ncid_frame_2 = {0};
29     QUIC_CONN_ID dcid_out;
30     const QUIC_CONN_ID *odcid = NULL;
31     uint64_t seq_num_out;
32 
33     ncid_frame_1.seq_num        = 2;
34     ncid_frame_1.conn_id.id_len = 8;
35     ncid_frame_1.conn_id.id[0]  = 3;
36 
37     ncid_frame_2.seq_num        = 3;
38     ncid_frame_2.conn_id.id_len = 8;
39     ncid_frame_2.conn_id.id[0]  = 4;
40 
41     odcid = ((idx == 2) ? NULL : &cid8_1);
42     if (!TEST_ptr(rcidm = ossl_quic_rcidm_new(odcid)))
43         goto err;
44 
45     if (idx != 2) {
46         if (/* ODCID not counted */
47             !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 1))
48             || !TEST_false(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 0))
49             || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid(rcidm, &dcid_out))
50 
51             || !TEST_true(ossl_quic_conn_id_eq(&dcid_out, &cid8_1))
52             || !TEST_size_t_eq(ossl_quic_rcidm_get_num_active(rcidm), 0))
53             goto err;
54     } else {
55         if (!TEST_false(ossl_quic_rcidm_get_preferred_tx_dcid(rcidm, &dcid_out))
56             || !TEST_size_t_eq(ossl_quic_rcidm_get_num_active(rcidm), 0))
57             goto err;
58     }
59 
60     if (idx == 1) {
61         if (!TEST_true(ossl_quic_rcidm_add_from_server_retry(rcidm, &cid8_5))
62             || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 1))
63             || !TEST_false(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 0))
64             || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid(rcidm, &dcid_out))
65 
66             || !TEST_true(ossl_quic_conn_id_eq(&dcid_out, &cid8_5))
67             || !TEST_size_t_eq(ossl_quic_rcidm_get_num_active(rcidm), 0))
68             goto err;
69     }
70 
71     if (!TEST_true(ossl_quic_rcidm_add_from_initial(rcidm, &cid8_2))
72         /* Initial SCID (seq=0) is counted */
73         || !TEST_size_t_eq(ossl_quic_rcidm_get_num_active(rcidm), 1)
74         || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 1))
75         || !TEST_false(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 0))
76         || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid(rcidm, &dcid_out))
77         || !TEST_true(ossl_quic_conn_id_eq(&dcid_out, &cid8_2))
78 
79         || !TEST_true(ossl_quic_rcidm_add_from_ncid(rcidm, &ncid_frame_1))
80         || !TEST_size_t_eq(ossl_quic_rcidm_get_num_active(rcidm), 2)
81         /* Not changed over yet - handshake not confirmed */
82         || !TEST_false(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 0))
83         || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid(rcidm, &dcid_out))
84         || !TEST_true(ossl_quic_conn_id_eq(&dcid_out, &cid8_2))
85 
86         || !TEST_true(ossl_quic_rcidm_add_from_ncid(rcidm, &ncid_frame_2))
87         || !TEST_size_t_eq(ossl_quic_rcidm_get_num_active(rcidm), 3)
88         || !TEST_size_t_eq(ossl_quic_rcidm_get_num_retiring(rcidm), 0)
89         || !TEST_false(ossl_quic_rcidm_pop_retire_seq_num(rcidm, &seq_num_out))
90         /* Not changed over yet - handshake not confirmed */
91         || !TEST_false(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 0))
92         || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid(rcidm, &dcid_out))
93         || !TEST_true(ossl_quic_conn_id_eq(&dcid_out, &cid8_2)))
94         goto err;
95 
96     ossl_quic_rcidm_on_handshake_complete(rcidm);
97 
98     if (!TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 1))
99         || !TEST_false(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 1))
100         || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid(rcidm, &dcid_out))
101         || !TEST_true(ossl_quic_conn_id_eq(&dcid_out, &cid8_3))
102         || !TEST_size_t_eq(ossl_quic_rcidm_get_num_retiring(rcidm), 1)
103         || !TEST_true(ossl_quic_rcidm_peek_retire_seq_num(rcidm, &seq_num_out))
104         || !TEST_uint64_t_eq(seq_num_out, 0))
105         goto err;
106 
107     ossl_quic_rcidm_request_roll(rcidm);
108 
109     if (!TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 1))
110         || !TEST_false(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 1))
111         || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid(rcidm, &dcid_out))
112         || !TEST_true(ossl_quic_conn_id_eq(&dcid_out, &cid8_4))
113         || !TEST_size_t_eq(ossl_quic_rcidm_get_num_retiring(rcidm), 2)
114         || !TEST_true(ossl_quic_rcidm_peek_retire_seq_num(rcidm, &seq_num_out))
115         || !TEST_uint64_t_eq(seq_num_out, 0)
116         || !TEST_true(ossl_quic_rcidm_pop_retire_seq_num(rcidm, &seq_num_out))
117         || !TEST_uint64_t_eq(seq_num_out, 0)
118         || !TEST_true(ossl_quic_rcidm_pop_retire_seq_num(rcidm, &seq_num_out))
119         || !TEST_uint64_t_eq(seq_num_out, 2))
120         goto err;
121 
122     testresult = 1;
123 err:
124     ossl_quic_rcidm_free(rcidm);
125     return testresult;
126 }
127 
setup_tests(void)128 int setup_tests(void)
129 {
130     ADD_ALL_TESTS(test_rcidm, 3);
131     return 1;
132 }
133