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_srtm.h"
11 #include "testutil.h"
12
13 static char ptrs[8];
14
15 static const QUIC_STATELESS_RESET_TOKEN token_1 = {{
16 0x01, 0x02, 0x03, 0x04
17 }};
18
19 static const QUIC_STATELESS_RESET_TOKEN token_2 = {{
20 0x01, 0x02, 0x03, 0x05
21 }};
22
test_srtm(void)23 static int test_srtm(void)
24 {
25 int testresult = 0;
26 QUIC_SRTM *srtm;
27 void *opaque = NULL;
28 uint64_t seq_num = 0;
29
30 if (!TEST_ptr(srtm = ossl_quic_srtm_new(NULL, NULL)))
31 goto err;
32
33 if (!TEST_true(ossl_quic_srtm_add(srtm, ptrs + 0, 0, &token_1))
34 || !TEST_false(ossl_quic_srtm_add(srtm, ptrs + 0, 0, &token_1))
35 || !TEST_false(ossl_quic_srtm_remove(srtm, ptrs + 0, 1))
36 || !TEST_false(ossl_quic_srtm_remove(srtm, ptrs + 3, 0))
37 || !TEST_true(ossl_quic_srtm_cull(srtm, ptrs + 3))
38 || !TEST_true(ossl_quic_srtm_cull(srtm, ptrs + 3))
39 || !TEST_true(ossl_quic_srtm_add(srtm, ptrs + 0, 1, &token_1))
40 || !TEST_true(ossl_quic_srtm_add(srtm, ptrs + 0, 2, &token_1))
41 || !TEST_true(ossl_quic_srtm_add(srtm, ptrs + 0, 3, &token_1))
42 || !TEST_true(ossl_quic_srtm_add(srtm, ptrs + 1, 0, &token_1))
43 || !TEST_true(ossl_quic_srtm_add(srtm, ptrs + 2, 0, &token_2))
44 || !TEST_true(ossl_quic_srtm_add(srtm, ptrs + 3, 3, &token_2))
45 || !TEST_true(ossl_quic_srtm_remove(srtm, ptrs + 3, 3))
46 || !TEST_true(ossl_quic_srtm_lookup(srtm, &token_1, 0, &opaque, &seq_num))
47 || !TEST_ptr_eq(opaque, ptrs + 1)
48 || !TEST_uint64_t_eq(seq_num, 0)
49 || !TEST_true(ossl_quic_srtm_lookup(srtm, &token_1, 1, &opaque, &seq_num))
50 || !TEST_ptr_eq(opaque, ptrs + 0)
51 || !TEST_uint64_t_eq(seq_num, 3)
52 || !TEST_true(ossl_quic_srtm_lookup(srtm, &token_1, 2, &opaque, &seq_num))
53 || !TEST_ptr_eq(opaque, ptrs + 0)
54 || !TEST_uint64_t_eq(seq_num, 2)
55 || !TEST_true(ossl_quic_srtm_lookup(srtm, &token_1, 3, &opaque, &seq_num))
56 || !TEST_ptr_eq(opaque, ptrs + 0)
57 || !TEST_uint64_t_eq(seq_num, 1)
58 || !TEST_true(ossl_quic_srtm_lookup(srtm, &token_1, 4, &opaque, &seq_num))
59 || !TEST_ptr_eq(opaque, ptrs + 0)
60 || !TEST_uint64_t_eq(seq_num, 0)
61 || !TEST_false(ossl_quic_srtm_lookup(srtm, &token_1, 5, &opaque, &seq_num))
62 || !TEST_true(ossl_quic_srtm_cull(srtm, ptrs + 0))
63 || !TEST_true(ossl_quic_srtm_lookup(srtm, &token_1, 0, &opaque, &seq_num))
64 || !TEST_ptr_eq(opaque, ptrs + 1)
65 || !TEST_uint64_t_eq(seq_num, 0)
66 || !TEST_true(ossl_quic_srtm_lookup(srtm, &token_2, 0, &opaque, &seq_num))
67 || !TEST_ptr_eq(opaque, ptrs + 2)
68 || !TEST_uint64_t_eq(seq_num, 0)
69 || !TEST_true(ossl_quic_srtm_remove(srtm, ptrs + 2, 0))
70 || !TEST_false(ossl_quic_srtm_lookup(srtm, &token_2, 0, &opaque, &seq_num))
71 )
72 goto err;
73
74 testresult = 1;
75 err:
76 ossl_quic_srtm_free(srtm);
77 return testresult;
78 }
79
setup_tests(void)80 int setup_tests(void)
81 {
82 ADD_TEST(test_srtm);
83 return 1;
84 }
85