Debian Codegraph @

crt/s2n/tests/unit/s2n_array_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 #include "utils/s2n_array.h"
16
17 #include "s2n_test.h"
18 #include "utils/s2n_blob.h"
19 #include "utils/s2n_mem.h"
20 #include "utils/s2n_safety.h"
21
22 struct array_element {
23 int first;
24 char second;
25 };
26
27 #define NUM_OF_ELEMENTS 17
28
29 int main(int argc, char **argv)
30 {
31 BEGIN_TEST();
32 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
33
34 size_t element_size = sizeof(struct array_element);
35
36 struct array_element elements[NUM_OF_ELEMENTS] = { 0 };
37 for (size_t i = 0; i < NUM_OF_ELEMENTS; i++) {
38 elements[i].first = i;
39 elements[i].second = 'a' + i;
40 }
41
42 {
43 struct s2n_array *array = { 0 };
44 uint32_t len = 0;
45 uint32_t capacity = 0;
46
47 /* Verify add and get elements with null array */
48 EXPECT_ERROR(s2n_array_pushback(NULL, NULL));
49 EXPECT_ERROR(s2n_array_get(NULL, 0, NULL));
50
51 /* Verify freeing null array */
52 EXPECT_ERROR(s2n_array_free(NULL));
53
54 EXPECT_NOT_NULL(array = s2n_array_new(element_size));
55
56 /* Validate array parameters */
57 EXPECT_OK(s2n_array_capacity(array, &capacity));
58 EXPECT_EQUAL(capacity, 16);
59 EXPECT_OK(s2n_array_num_elements(array, &len));
60 EXPECT_EQUAL(len, 0);
61 EXPECT_EQUAL(array->element_size, element_size);
62
63 /* Add an element */
64 struct array_element *element = NULL;
65 EXPECT_OK(s2n_array_pushback(array, (void **) &element));
66 EXPECT_NOT_NULL(element);
67 element->first = elements[0].first;
68 element->second = elements[0].second;
69
70 /* Validate array parameters */
71 EXPECT_OK(s2n_array_capacity(array, &capacity));
72 EXPECT_EQUAL(capacity, 16);
73 EXPECT_OK(s2n_array_num_elements(array, &len));
74 EXPECT_EQUAL(len, 1);
75
76 /* Get first element */
77 struct array_element *first_element = NULL;
78 EXPECT_OK(s2n_array_get(array, 0, (void **) &first_element));
79 EXPECT_NOT_NULL(first_element);
80 EXPECT_EQUAL(first_element->first, elements[0].first);
81 EXPECT_EQUAL(first_element->second, elements[0].second);
82
83 /* Get second element */
84 struct array_element *second_element = NULL;
85 EXPECT_ERROR(s2n_array_get(array, 1, (void **) &second_element));
86 EXPECT_NULL(second_element);
87
88 /* Add more than 16 elements */
89 for (int i = 1; i < NUM_OF_ELEMENTS; i++) {
90 struct array_element *elem = NULL;
91 EXPECT_OK(s2n_array_pushback(array, (void **) &elem));
92 EXPECT_NOT_NULL(elem);
93 elem->first = elements[i].first;
94 elem->second = elements[i].second;
95 }
96
97 /* Validate array parameters again */
98 EXPECT_OK(s2n_array_capacity(array, &capacity));
99 EXPECT_EQUAL(capacity, 32);
100 EXPECT_OK(s2n_array_num_elements(array, &len));
101 EXPECT_EQUAL(len, 17);
102 EXPECT_EQUAL(array->element_size, element_size);
103 EXPECT_SUCCESS(memcmp(array->mem.data, elements, NUM_OF_ELEMENTS * element_size));
104
105 /* Insert element at given index */
106 struct array_element *insert_element = NULL;
107 EXPECT_OK(s2n_array_insert(array, 16, (void **) &insert_element));
108 EXPECT_NOT_NULL(insert_element);
109 insert_element->first = 20;
110 insert_element->second = 'a' + 20;
111
112 /* Validate array parameters */
113 EXPECT_OK(s2n_array_capacity(array, &capacity));
114 EXPECT_EQUAL(capacity, 32);
115 EXPECT_OK(s2n_array_num_elements(array, &len));
116 EXPECT_EQUAL(len, 18);
117
118 /* Get the inserted element */
119 struct array_element *inserted_element = NULL;
120 EXPECT_OK(s2n_array_get(array, 16, (void **) &inserted_element));
121 EXPECT_NOT_NULL(inserted_element);
122 EXPECT_EQUAL(inserted_element->first, insert_element->first);
123 EXPECT_EQUAL(inserted_element->second, insert_element->second);
124
125 /* Get the element after the inserted element */
126 struct array_element *after_inserted_element = NULL;
127 EXPECT_OK(s2n_array_get(array, 17, (void **) &after_inserted_element));
128 EXPECT_NOT_NULL(after_inserted_element);
129 EXPECT_EQUAL(after_inserted_element->first, elements[16].first);
130 EXPECT_EQUAL(after_inserted_element->second, elements[16].second);
131
132 /* Delete element from given index */
133 EXPECT_OK(s2n_array_remove(array, 0));
134
135 /* Validate array parameters */
136 EXPECT_OK(s2n_array_capacity(array, &capacity));
137 EXPECT_EQUAL(capacity, 32);
138 EXPECT_OK(s2n_array_num_elements(array, &len));
139 EXPECT_EQUAL(len, 17);
140
141 /* Get the current element at the deleted index */
142 struct array_element *after_removed_element = NULL;
143 EXPECT_OK(s2n_array_get(array, 0, (void **) &after_removed_element));
144 EXPECT_EQUAL(after_removed_element->first, elements[1].first);
145 EXPECT_EQUAL(after_removed_element->second, elements[1].second);
146
147 /* Done with the array, make sure it can be freed */
148 EXPECT_OK(s2n_array_free(array));
149
150 /* Check what happens if there is an integer overflow */
151 /* 0xF00000F0 * 16 = 3840 (in 32 bit arithmatic) */
152 EXPECT_NULL(array = s2n_array_new(0xF00000F0));
153 EXPECT_NOT_NULL(array = s2n_array_new(240));
154 EXPECT_OK(s2n_array_free(array));
155 }
156
157 /* Arrays initialize with default capacity */
158 {
159 DEFER_CLEANUP(struct s2n_array *default_array = s2n_array_new(element_size), s2n_array_free_p);
160
161 uint32_t capacity = 0;
162 EXPECT_OK(s2n_array_capacity(default_array, &capacity));
163 EXPECT_EQUAL(capacity, S2N_INITIAL_ARRAY_SIZE);
164 }
165
166 /* Test creating arrays with different initial capacities */
167 for (int i = 0; i < 10; i++) {
168 uint32_t capacity_set = i * i;
169 DEFER_CLEANUP(struct s2n_array *array = s2n_array_new_with_capacity(element_size, capacity_set),
170 s2n_array_free_p);
171
172 uint32_t actual_capacity = 0;
173 EXPECT_OK(s2n_array_capacity(array, &actual_capacity));
174 EXPECT_EQUAL(capacity_set, actual_capacity);
175
176 /* Array doesn't grow before capacity is reached */
177 for (int j = 0; j < capacity_set; j++) {
178 struct array_element *element = NULL;
179 EXPECT_OK(s2n_array_pushback(array, (void **) &element));
180 EXPECT_NOT_NULL(element);
181
182 EXPECT_OK(s2n_array_capacity(array, &actual_capacity));
183 EXPECT_EQUAL(capacity_set, actual_capacity);
184
185 uint32_t len = 0;
186 EXPECT_OK(s2n_array_num_elements(array, &len));
187 EXPECT_EQUAL(len, j + 1);
188 }
189
190 /* Array grows only after capacity is reached */
191 struct array_element *element = NULL;
192 EXPECT_OK(s2n_array_pushback(array, (void **) &element));
193 EXPECT_NOT_NULL(element);
194
195 EXPECT_OK(s2n_array_capacity(array, &actual_capacity));
196 EXPECT_NOT_EQUAL(capacity_set, actual_capacity);
197
198 uint32_t len = 0;
199 EXPECT_OK(s2n_array_num_elements(array, &len));
200 EXPECT_EQUAL(len, capacity_set + 1);
201 }
202
203 END_TEST();
204 }