Debian Codegraph @

crt/s2n/tests/unit/s2n_map_iterator_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 "api/s2n.h"
17 #include "s2n_test.h"
18 #include "utils/s2n_map.h"
19 #include "utils/s2n_map_internal.h"
20
21 #define TEST_VALUE_COUNT 10
22
23 int main(int argc, char **argv)
24 {
25 BEGIN_TEST();
26
27 /* s2n_map_iterator iteration test */
28 {
29 struct s2n_map *map = s2n_map_new();
30 EXPECT_NOT_NULL(map);
31 /* fail to initialize an iterator on a mutable map */
32 {
33 struct s2n_map_iterator iter = { 0 };
34 EXPECT_ERROR_WITH_ERRNO(s2n_map_iterator_init(&iter, map), S2N_ERR_MAP_MUTABLE);
35 };
36
37 EXPECT_OK(s2n_map_complete(map));
38
39 /* has next is false on an empty map, and next returns an error */
40 {
41 struct s2n_map_iterator iter = { 0 };
42 EXPECT_OK(s2n_map_iterator_init(&iter, map));
43
44 EXPECT_FALSE(s2n_map_iterator_has_next(&iter));
45
46 struct s2n_blob value = { 0 };
47 EXPECT_ERROR_WITH_ERRNO(s2n_map_iterator_next(&iter, &value), S2N_ERR_ARRAY_INDEX_OOB);
48 };
49
50 EXPECT_OK(s2n_map_unlock(map));
51 for (uint8_t i = 0; i < TEST_VALUE_COUNT; i++) {
52 struct s2n_blob key = { .size = 1, .data = &i };
53 struct s2n_blob val = { .size = 1, .data = &i };
54 EXPECT_OK(s2n_map_put(map, &key, &val));
55 }
56 EXPECT_OK(s2n_map_complete(map));
57
58 /* iterator goes over all elements */
59 {
60 bool seen[TEST_VALUE_COUNT] = { 0 };
61
62 struct s2n_map_iterator iter = { 0 };
63 EXPECT_OK(s2n_map_iterator_init(&iter, map));
64
65 struct s2n_blob value = { 0 };
66 for (size_t i = 0; i < TEST_VALUE_COUNT; i++) {
67 EXPECT_TRUE(s2n_map_iterator_has_next(&iter));
68
69 EXPECT_OK(s2n_map_iterator_next(&iter, &value));
70 seen[*value.data] = true;
71 }
72
73 /* all elements have been iterated over */
74 EXPECT_FALSE(s2n_map_iterator_has_next(&iter));
75 EXPECT_ERROR_WITH_ERRNO(s2n_map_iterator_next(&iter, &value), S2N_ERR_ARRAY_INDEX_OOB);
76
77 /* all elements were seen */
78 for (size_t i = 0; i < TEST_VALUE_COUNT; i++) {
79 EXPECT_TRUE(seen[i]);
80 }
81 };
82
83 /* next returns an error when the blob is null */
84 {
85 struct s2n_map_iterator iter = { 0 };
86 EXPECT_OK(s2n_map_iterator_init(&iter, map));
87
88 EXPECT_ERROR_WITH_ERRNO(s2n_map_iterator_next(&iter, NULL), S2N_ERR_NULL);
89 }
90
91 EXPECT_OK(s2n_map_free(map));
92 };
93
94 /* test first and last slots in table */
95 {
96 /* 2 (first and last slot) * 2 (key and value) */
97 struct s2n_blob blobs[2 * 2] = { 0 };
98 for (uint8_t i = 0; i < (2 * 2); i++) {
99 EXPECT_SUCCESS(s2n_alloc(&blobs[i], 1));
100 *blobs[i].data = i;
101 }
102
103 struct s2n_map *test_map = s2n_map_new();
104 EXPECT_NOT_NULL(test_map);
105
106 /* set values in map to 0 and 1 */
107 test_map->table[0].value = blobs[0];
108 test_map->table[0].key = blobs[2];
109 test_map->table[test_map->capacity - 1].value = blobs[1];
110 test_map->table[test_map->capacity - 1].key = blobs[3];
111
112 test_map->size = 2;
113 EXPECT_OK(s2n_map_complete(test_map));
114
115 struct s2n_map_iterator iter = { 0 };
116 EXPECT_OK(s2n_map_iterator_init(&iter, test_map));
117 bool seen[2] = { 0 };
118
119 struct s2n_blob value = { 0 };
120 for (size_t i = 0; i < 2; i++) {
121 EXPECT_TRUE(s2n_map_iterator_has_next(&iter));
122
123 EXPECT_OK(s2n_map_iterator_next(&iter, &value));
124 seen[*value.data] = true;
125 }
126
127 /* assert that 0 and 1 were both seen */
128 EXPECT_TRUE(seen[0] && seen[1]);
129
130 EXPECT_OK(s2n_map_free(test_map));
131 };
132
133 END_TEST();
134 }