Debian Codegraph @

crt/s2n/tests/unit/s2n_map_test.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include "utils/s2n_map.h"
17
18 #include <string.h>
19
20 #include "api/s2n.h"
21 #include "s2n_test.h"
22
23 #define TEST_VALUE_COUNT 8192
24
25 int main(int argc, char **argv)
26 {
27 char keystr[sizeof("ffff")];
28 char valstr[sizeof("16384")];
29 uint32_t size = 0;
30 struct s2n_map *empty = NULL, *map = NULL;
31 struct s2n_blob key = { 0 };
32 struct s2n_blob val = { 0 };
33 bool key_found = false;
34
35 BEGIN_TEST();
36 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
37
38 EXPECT_NOT_NULL(empty = s2n_map_new());
39 EXPECT_OK(s2n_map_size(empty, &size));
40 EXPECT_EQUAL(size, 0);
41
42 /* Try a lookup on an empty map. Expect an error because the map is still mutable. */
43 EXPECT_SUCCESS(snprintf(keystr, sizeof(keystr), "%04x", 1234));
44 key.data = (void *) keystr;
45 key.size = strlen(keystr) + 1;
46 EXPECT_ERROR(s2n_map_lookup(empty, &key, &val, &key_found));
47
48 EXPECT_SUCCESS(snprintf(valstr, sizeof(valstr), "%05d", 1234));
49 val.data = (void *) valstr;
50 val.size = strlen(valstr) + 1;
51
52 /* Try to add/put key with zero-size data. Expect failures */
53 key.size = 0;
54 EXPECT_ERROR(s2n_map_add(empty, &key, &val));
55 EXPECT_ERROR(s2n_map_put(empty, &key, &val));
56 key.size = strlen(keystr) + 1;
57
58 /* Make the empty map complete */
59 EXPECT_OK(s2n_map_complete(empty));
60
61 /* Lookup and expect no result */
62 EXPECT_OK(s2n_map_lookup(empty, &key, &val, &key_found));
63 EXPECT_EQUAL(key_found, false);
64
65 /* Done with the empty map */
66 EXPECT_OK(s2n_map_free(empty));
67
68 /* Expect failure since initial map size is zero */
69 EXPECT_NULL(map = s2n_map_new_with_initial_capacity(0));
70
71 /* Create map with the smallest initial size */
72 EXPECT_NOT_NULL(map = s2n_map_new_with_initial_capacity(1));
73
74 /* Insert 8k key value pairs of the form hex(i) -> dec(i) */
75 for (int i = 0; i < TEST_VALUE_COUNT; i++) {
76 EXPECT_SUCCESS(snprintf(keystr, sizeof(keystr), "%04x", i));
77 EXPECT_SUCCESS(snprintf(valstr, sizeof(valstr), "%05d", i));
78
79 key.data = (void *) keystr;
80 key.size = strlen(keystr) + 1;
81 val.data = (void *) valstr;
82 val.size = strlen(valstr) + 1;
83
84 EXPECT_OK(s2n_map_add(map, &key, &val));
85 }
86 EXPECT_OK(s2n_map_size(map, &size));
87 EXPECT_EQUAL(size, TEST_VALUE_COUNT);
88
89 /* Try adding some duplicates */
90 for (int i = 0; i < 10; i++) {
91 EXPECT_SUCCESS(snprintf(keystr, sizeof(keystr), "%04x", i));
92 EXPECT_SUCCESS(snprintf(valstr, sizeof(valstr), "%05d", i));
93
94 key.data = (void *) keystr;
95 key.size = strlen(keystr) + 1;
96 val.data = (void *) valstr;
97 val.size = strlen(valstr) + 1;
98
99 EXPECT_ERROR(s2n_map_add(map, &key, &val));
100 }
101 EXPECT_OK(s2n_map_size(map, &size));
102 EXPECT_EQUAL(size, TEST_VALUE_COUNT);
103
104 /* Try replacing some entries */
105 for (int i = 0; i < 10; i++) {
106 EXPECT_SUCCESS(snprintf(keystr, sizeof(keystr), "%04x", i));
107 EXPECT_SUCCESS(snprintf(valstr, sizeof(valstr), "%05d", i + 1));
108
109 key.data = (void *) keystr;
110 key.size = strlen(keystr) + 1;
111 val.data = (void *) valstr;
112 val.size = strlen(valstr) + 1;
113
114 EXPECT_OK(s2n_map_put(map, &key, &val));
115 }
116
117 /* Try a lookup before the map is complete: should fail */
118 EXPECT_SUCCESS(snprintf(keystr, sizeof(keystr), "%04x", 1));
119 EXPECT_ERROR(s2n_map_lookup(map, &key, &val, &key_found));
120
121 /* Make the map complete */
122 EXPECT_OK(s2n_map_complete(map));
123
124 /* Make sure that add-after-complete fails */
125 EXPECT_SUCCESS(snprintf(keystr, sizeof(keystr), "%04x", TEST_VALUE_COUNT + 1));
126 EXPECT_SUCCESS(snprintf(valstr, sizeof(valstr), "%05d", TEST_VALUE_COUNT + 1));
127
128 key.data = (void *) keystr;
129 key.size = strlen(keystr) + 1;
130 val.data = (void *) valstr;
131 val.size = strlen(valstr) + 1;
132
133 EXPECT_ERROR(s2n_map_add(map, &key, &val));
134
135 /* Check for equivalence */
136 for (int i = 0; i < TEST_VALUE_COUNT; i++) {
137 if (i >= 10) {
138 EXPECT_SUCCESS(snprintf(keystr, sizeof(keystr), "%04x", i));
139 EXPECT_SUCCESS(snprintf(valstr, sizeof(valstr), "%05d", i));
140 } else {
141 /* The first 10 entries were overwritten with i+1 */
142 EXPECT_SUCCESS(snprintf(keystr, sizeof(keystr), "%04x", i));
143 EXPECT_SUCCESS(snprintf(valstr, sizeof(valstr), "%05d", i + 1));
144 }
145
146 key.data = (void *) keystr;
147 key.size = strlen(keystr) + 1;
148
149 EXPECT_OK(s2n_map_lookup(map, &key, &val, &key_found));
150 EXPECT_EQUAL(key_found, true);
151
152 EXPECT_SUCCESS(memcmp(val.data, valstr, strlen(valstr) + 1));
153 }
154
155 /* Check for a key that shouldn't be there */
156 EXPECT_SUCCESS(snprintf(keystr, sizeof(keystr), "%04x", TEST_VALUE_COUNT + 1));
157 key.data = (void *) keystr;
158 key.size = strlen(keystr) + 1;
159 EXPECT_OK(s2n_map_lookup(map, &key, &val, &key_found));
160 EXPECT_EQUAL(key_found, false);
161
162 /* Make the map mutable */
163 EXPECT_OK(s2n_map_unlock(map));
164 /* Make sure that add-after-unlock succeeds */
165 EXPECT_SUCCESS(snprintf(keystr, sizeof(keystr), "%04x", TEST_VALUE_COUNT + 1));
166 EXPECT_SUCCESS(snprintf(valstr, sizeof(valstr), "%05d", TEST_VALUE_COUNT + 1));
167
168 key.data = (void *) keystr;
169 key.size = strlen(keystr) + 1;
170 val.data = (void *) valstr;
171 val.size = strlen(valstr) + 1;
172
173 EXPECT_OK(s2n_map_add(map, &key, &val));
174
175 /* Complete the map again */
176 EXPECT_OK(s2n_map_complete(map));
177
178 /* Check the element added after map unlock */
179 EXPECT_OK(s2n_map_lookup(map, &key, &val, &key_found));
180 EXPECT_EQUAL(key_found, true);
181 EXPECT_SUCCESS(memcmp(val.data, valstr, strlen(valstr) + 1));
182
183 EXPECT_OK(s2n_map_free(map));
184
185 END_TEST();
186 }