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