Debian Codegraph @

crt/aws-c-common/tests/byte_buf_test.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/common/byte_buf.h>
7 #include <aws/common/private/byte_buf.h>
8 #include <aws/common/string.h>
9 #include <aws/testing/aws_test_harness.h>
10
11 #include <locale.h>
12
13 AWS_TEST_CASE(test_buffer_cat, s_test_buffer_cat_fn)
14 static int s_test_buffer_cat_fn(struct aws_allocator *allocator, void *ctx) {
15 (void)ctx;
16
17 struct aws_byte_buf str1 = aws_byte_buf_from_c_str("testa");
18 struct aws_byte_buf str2 = aws_byte_buf_from_c_str(";testb");
19 struct aws_byte_buf str3 = aws_byte_buf_from_c_str(";testc");
20
21 const char expected[] = "testa;testb;testc";
22
23 struct aws_byte_buf destination;
24 ASSERT_SUCCESS(aws_byte_buf_init(&destination, allocator, str1.len + str2.len + str3.len + 10));
25 ASSERT_SUCCESS(aws_byte_buf_cat(&destination, 3, &str1, &str2, &str3));
26
27 ASSERT_INT_EQUALS(strlen(expected), destination.len);
28 ASSERT_INT_EQUALS(strlen(expected) + 10, destination.capacity);
29 ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), destination.buffer, destination.len);
30
31 destination.len = 0;
32 ASSERT_SUCCESS(aws_byte_buf_cat(&destination, 1, &str1));
33 ASSERT_INT_EQUALS(str1.len, destination.len);
34 ASSERT_BIN_ARRAYS_EQUALS(str1.buffer, str1.len, destination.buffer, destination.len);
35
36 destination.len = 0;
37 ASSERT_SUCCESS(aws_byte_buf_cat(&destination, 0));
38 ASSERT_INT_EQUALS(0, destination.len);
39
40 aws_byte_buf_clean_up(&destination);
41
42 return 0;
43 }
44
45 AWS_TEST_CASE(test_buffer_cat_dest_too_small, s_test_buffer_cat_dest_too_small_fn)
46 static int s_test_buffer_cat_dest_too_small_fn(struct aws_allocator *allocator, void *ctx) {
47 (void)ctx;
48
49 struct aws_byte_buf str1 = aws_byte_buf_from_c_str("testa");
50 struct aws_byte_buf str2 = aws_byte_buf_from_c_str(";testb");
51 struct aws_byte_buf str3 = aws_byte_buf_from_c_str(";testc");
52
53 struct aws_byte_buf destination;
54 ASSERT_SUCCESS(aws_byte_buf_init(&destination, allocator, str1.len + str2.len));
55 ASSERT_INT_EQUALS(0, destination.len);
56 ASSERT_INT_EQUALS(str1.len + str2.len, destination.capacity);
57
58 ASSERT_ERROR(AWS_ERROR_DEST_COPY_TOO_SMALL, aws_byte_buf_cat(&destination, 3, &str1, &str2, &str3));
59
60 aws_byte_buf_clean_up(&destination);
61
62 return 0;
63 }
64
65 AWS_TEST_CASE(test_buffer_cpy, s_test_buffer_cpy_fn)
66 static int s_test_buffer_cpy_fn(struct aws_allocator *allocator, void *ctx) {
67 (void)ctx;
68
69 struct aws_byte_buf from_buf = aws_byte_buf_from_c_str("testa");
70 struct aws_byte_cursor from = aws_byte_cursor_from_buf(&from_buf);
71 struct aws_byte_buf destination;
72
73 ASSERT_SUCCESS(aws_byte_buf_init(&destination, allocator, from.len + 10));
74 ASSERT_SUCCESS(aws_byte_buf_append(&destination, &from));
75
76 ASSERT_INT_EQUALS(from.len, destination.len);
77 ASSERT_INT_EQUALS(from.len + 10, destination.capacity);
78
79 ASSERT_BIN_ARRAYS_EQUALS(from.ptr, from.len, destination.buffer, destination.len);
80
81 aws_byte_buf_clean_up(&destination);
82
83 return 0;
84 }
85
86 AWS_TEST_CASE(test_buffer_cpy_offsets, s_test_buffer_cpy_offsets_fn)
87 static int s_test_buffer_cpy_offsets_fn(struct aws_allocator *allocator, void *ctx) {
88 (void)ctx;
89
90 struct aws_byte_buf from_buf = aws_byte_buf_from_c_str("testa");
91 struct aws_byte_cursor from = aws_byte_cursor_from_buf(&from_buf);
92 aws_byte_cursor_advance(&from, 2);
93 struct aws_byte_buf destination;
94
95 ASSERT_SUCCESS(aws_byte_buf_init(&destination, allocator, from_buf.len + 10));
96 ASSERT_SUCCESS(aws_byte_buf_append(&destination, &from));
97
98 ASSERT_INT_EQUALS(from_buf.len - 2, destination.len);
99 ASSERT_INT_EQUALS(from_buf.len + 10, destination.capacity);
100
101 char expected[] = "sta";
102
103 ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), destination.buffer, destination.len);
104
105 aws_byte_buf_clean_up(&destination);
106
107 return 0;
108 }
109
110 AWS_TEST_CASE(test_buffer_cpy_dest_too_small, s_test_buffer_cpy_dest_too_small_fn)
111 static int s_test_buffer_cpy_dest_too_small_fn(struct aws_allocator *allocator, void *ctx) {
112 (void)ctx;
113
114 struct aws_byte_buf from_buf = aws_byte_buf_from_c_str("testa");
115 struct aws_byte_cursor from = aws_byte_cursor_from_buf(&from_buf);
116
117 struct aws_byte_buf destination;
118
119 ASSERT_SUCCESS(aws_byte_buf_init(&destination, allocator, from.len - 1));
120 ASSERT_ERROR(AWS_ERROR_DEST_COPY_TOO_SMALL, aws_byte_buf_append(&destination, &from));
121 ASSERT_INT_EQUALS(0, destination.len);
122
123 aws_byte_buf_clean_up(&destination);
124
125 return 0;
126 }
127
128 AWS_TEST_CASE(test_buffer_cpy_offsets_dest_too_small, s_test_buffer_cpy_offsets_dest_too_small_fn)
129 static int s_test_buffer_cpy_offsets_dest_too_small_fn(struct aws_allocator *allocator, void *ctx) {
130 (void)ctx;
131
132 struct aws_byte_buf from_buf = aws_byte_buf_from_c_str("testa");
133 struct aws_byte_cursor from = aws_byte_cursor_from_buf(&from_buf);
134 struct aws_byte_buf destination;
135
136 ASSERT_SUCCESS(aws_byte_buf_init(&destination, allocator, from.len));
137 destination.len = 1;
138 ASSERT_ERROR(AWS_ERROR_DEST_COPY_TOO_SMALL, aws_byte_buf_append(&destination, &from));
139 ASSERT_INT_EQUALS(1, destination.len);
140
141 aws_byte_buf_clean_up(&destination);
142
143 return 0;
144 }
145
146 AWS_TEST_CASE(test_buffer_eq, s_test_buffer_eq_fn)
147 static int s_test_buffer_eq_fn(struct aws_allocator *allocator, void *ctx) {
148 (void)ctx;
149
150 struct aws_byte_buf b1 = aws_byte_buf_from_c_str("testa");
151 struct aws_byte_buf b1_equal = aws_byte_buf_from_c_str("testa");
152
153 struct aws_byte_buf b2 = aws_byte_buf_from_c_str("testb");
154
155 b1.capacity = 5;
156 b1_equal.allocator = allocator;
157
158 ASSERT_TRUE(aws_byte_buf_eq(&b1, &b1_equal));
159 ASSERT_TRUE(aws_byte_buf_eq(&b1, &b1));
160
161 ASSERT_FALSE(aws_byte_buf_eq(&b1, &b2));
162 return 0;
163 }
164
165 AWS_TEST_CASE(test_buffer_eq_same_content_different_len, s_test_buffer_eq_same_content_different_len_fn)
166 static int s_test_buffer_eq_same_content_different_len_fn(struct aws_allocator *allocator, void *ctx) {
167 (void)allocator;
168 (void)ctx;
169
170 struct aws_byte_buf b1 = aws_byte_buf_from_c_str("testa");
171 struct aws_byte_buf b2 = aws_byte_buf_from_c_str("testa");
172 b2.len--;
173
174 ASSERT_FALSE(aws_byte_buf_eq(&b1, &b2));
175
176 return 0;
177 }
178
179 AWS_TEST_CASE(test_buffer_eq_null_internal_byte_buffer, s_test_buffer_eq_null_internal_byte_buffer_fn)
180 static int s_test_buffer_eq_null_internal_byte_buffer_fn(struct aws_allocator *allocator, void *ctx) {
181 (void)allocator;
182 (void)ctx;
183
184 struct aws_byte_buf b1 = aws_byte_buf_from_array(NULL, 0);
185 struct aws_byte_buf b2 = aws_byte_buf_from_array(NULL, 0);
186
187 ASSERT_TRUE(aws_byte_buf_eq(&b1, &b2));
188 ASSERT_TRUE(aws_byte_buf_eq(&b2, &b1));
189
190 struct aws_byte_buf b3 = aws_byte_buf_from_c_str("abc");
191 ASSERT_FALSE(aws_byte_buf_eq(&b1, &b3));
192 return 0;
193 }
194
195 AWS_TEST_CASE(test_buffer_init_copy, s_test_buffer_init_copy_fn)
196 static int s_test_buffer_init_copy_fn(struct aws_allocator *allocator, void *ctx) {
197 (void)ctx;
198
199 struct aws_byte_buf src = aws_byte_buf_from_c_str("test_string");
200 struct aws_byte_buf dest;
201
202 ASSERT_SUCCESS(aws_byte_buf_init_copy(&dest, allocator, &src));
203 ASSERT_TRUE(aws_byte_buf_eq(&src, &dest));
204 ASSERT_INT_EQUALS(src.len, dest.capacity);
205 ASSERT_PTR_EQUALS(allocator, dest.allocator);
206 aws_byte_buf_clean_up(&dest);
207 return 0;
208 }
209
210 AWS_TEST_CASE(test_buffer_init_copy_null_buffer, s_test_buffer_init_copy_null_buffer_fn)
211 static int s_test_buffer_init_copy_null_buffer_fn(struct aws_allocator *allocator, void *ctx) {
212 (void)ctx;
213
214 struct aws_byte_buf src;
215 AWS_ZERO_STRUCT(src);
216 struct aws_byte_buf dest;
217 ASSERT_SUCCESS(aws_byte_buf_init_copy(&dest, allocator, &src));
218
219 return 0;
220 }
221
222 AWS_TEST_CASE(test_buffer_advance, s_test_buffer_advance)
223 static int s_test_buffer_advance(struct aws_allocator *allocator, void *ctx) {
224 (void)ctx;
225 (void)allocator;
226
227 uint8_t arr[16];
228 AWS_ZERO_ARRAY(arr);
229 struct aws_byte_buf src_buf = aws_byte_buf_from_empty_array(arr, sizeof(arr));
230
231 struct aws_byte_buf dst_buf = {0};
232
233 ASSERT_TRUE(aws_byte_buf_advance(&src_buf, &dst_buf, 4));
234 ASSERT_NULL(dst_buf.allocator);
235 ASSERT_INT_EQUALS(src_buf.len, 4);
236 ASSERT_INT_EQUALS(dst_buf.len, 0);
237 ASSERT_INT_EQUALS(dst_buf.capacity, 4);
238 ASSERT_PTR_EQUALS(src_buf.buffer, arr);
239 ASSERT_PTR_EQUALS(dst_buf.buffer, arr);
240
241 ASSERT_TRUE(aws_byte_buf_advance(&src_buf, &dst_buf, 12));
242 ASSERT_PTR_EQUALS(dst_buf.buffer, arr + 4);
243 ASSERT_INT_EQUALS(src_buf.len, 16);
244
245 src_buf.len = 12;
246 ASSERT_FALSE(aws_byte_buf_advance(&src_buf, &dst_buf, 5));
247 ASSERT_PTR_EQUALS(dst_buf.buffer, NULL);
248 ASSERT_NULL(dst_buf.allocator);
249 ASSERT_INT_EQUALS(dst_buf.len, 0);
250 ASSERT_INT_EQUALS(dst_buf.capacity, 0);
251 ASSERT_INT_EQUALS(src_buf.len, 12);
252
253 return 0;
254 }
255
256 AWS_TEST_CASE(test_buffer_printf, s_test_buffer_printf)
257 static int s_test_buffer_printf(struct aws_allocator *allocator, void *ctx) {
258 (void)allocator;
259 (void)ctx;
260
261 const char src[] = "abcdefg";
262
263 char dst[100] = {0};
264
265 /* check PRInSTR */
266 memset(dst, 0, sizeof(dst));
267 snprintf(dst, sizeof(dst), PRInSTR, 2, src);
268 ASSERT_UINT_EQUALS('a', dst[0]);
269 ASSERT_UINT_EQUALS('b', dst[1]);
270 ASSERT_UINT_EQUALS(0, dst[2]);
271
272 /* check AWS_BYTE_CURSOR_PRI() */
273 struct aws_byte_cursor cursor = aws_byte_cursor_from_array(src, 2);
274 memset(dst, 0, sizeof(dst));
275 snprintf(dst, sizeof(dst), PRInSTR, AWS_BYTE_CURSOR_PRI(cursor));
276 ASSERT_UINT_EQUALS('a', dst[0]);
277 ASSERT_UINT_EQUALS('b', dst[1]);
278 ASSERT_UINT_EQUALS(0, dst[2]);
279
280 /* check AWS_BYTE_BUF_PRI() */
281 struct aws_byte_buf buf = aws_byte_buf_from_array(src, 2);
282 memset(dst, 0, sizeof(dst));
283 snprintf(dst, sizeof(dst), PRInSTR, AWS_BYTE_BUF_PRI(buf));
284 ASSERT_UINT_EQUALS('a', dst[0]);
285 ASSERT_UINT_EQUALS('b', dst[1]);
286 ASSERT_UINT_EQUALS(0, dst[2]);
287
288 return AWS_OP_SUCCESS;
289 }
290
291 AWS_TEST_CASE(test_array_eq, s_test_array_eq)
292 static int s_test_array_eq(struct aws_allocator *allocator, void *ctx) {
293 (void)ctx;
294 (void)allocator;
295
296 uint8_t a[] = {1, 2, 3};
297 uint8_t b[] = {1, 2, 3};
298 uint8_t c[] = {7, 8, 9};
299 uint8_t d[] = {1, 2, 3, 4};
300
301 /* Simple */
302 ASSERT_TRUE(aws_array_eq(a, 3, b, 3));
303 ASSERT_FALSE(aws_array_eq(a, 3, c, 3));
304 ASSERT_FALSE(aws_array_eq(a, 3, d, 4));
305
306 /* Comparisons agains self */
307 ASSERT_TRUE(aws_array_eq(a, 3, a, 3));
308 ASSERT_FALSE(aws_array_eq(a, 3, a, 2));
309
310 /* Different data but size is 0 */
311 ASSERT_TRUE(aws_array_eq(a, 0, c, 0));
312
313 /* NULL inputs are OK if length is 0 */
314 ASSERT_TRUE(aws_array_eq(NULL, 0, NULL, 0));
315 ASSERT_TRUE(aws_array_eq(a, 0, NULL, 0));
316 ASSERT_TRUE(aws_array_eq(NULL, 0, b, 0));
317
318 return 0;
319 }
320
321 AWS_TEST_CASE(test_array_eq_ignore_case, s_test_array_eq_ignore_case)
322 static int s_test_array_eq_ignore_case(struct aws_allocator *allocator, void *ctx) {
323 (void)ctx;
324 (void)allocator;
325 {
326 uint8_t a[] = {'a', 'B', 'c', 'D', '1'};
327 uint8_t b[] = {'a', 'b', 'C', 'D', '1'}; /* same as a */
328 uint8_t c[] = {'a', 'b', 'c', 'd', '9'}; /* different */
329
330 /* Simple */
331 ASSERT_TRUE(aws_array_eq_ignore_case(a, 5, b, 5));
332 ASSERT_FALSE(aws_array_eq_ignore_case(a, 5, c, 5));
333 ASSERT_FALSE(aws_array_eq_ignore_case(a, 5, b, 3));
334
335 /* Comparisons against self */
336 ASSERT_TRUE(aws_array_eq_ignore_case(a, 5, a, 5));
337 ASSERT_FALSE(aws_array_eq_ignore_case(a, 5, a, 4));
338
339 /* Different data but size is 0 */
340 ASSERT_TRUE(aws_array_eq_ignore_case(a, 0, c, 0));
341
342 /* NULL inputs are OK if length is 0 */
343 ASSERT_TRUE(aws_array_eq_ignore_case(NULL, 0, NULL, 0));
344 ASSERT_TRUE(aws_array_eq_ignore_case(a, 0, NULL, 0));
345 ASSERT_TRUE(aws_array_eq_ignore_case(NULL, 0, b, 0));
346 }
347
348 {
349 /* Comparison should continue beyond null-terminator */
350 uint8_t a[] = {'a', 0, 'b'};
351 uint8_t b[] = {'a', 0, 'c'};
352 uint8_t c[] = {'a', 0, 'b'};
353 ASSERT_FALSE(aws_array_eq_ignore_case(&a, 3, &b, 3));
354 ASSERT_TRUE(aws_array_eq_ignore_case(&a, 3, &c, 3));
355 }
356
357 {
358 /* Compare every possible uint8_t value, then lower against upper, then upper against lower.
359 * Ex:
360 * a_src = {0 ... 255, 'a' ... 'z', 'A' ... 'Z'};
361 * b_src = {0 ... 255, 'A' ... 'Z', 'a' ... 'z'};
362 */
363 uint8_t a[256 + 26 + 26];
364 uint8_t b[256 + 26 + 26];
365 for (size_t i = 0; i < 256; ++i) {
366 a[i] = (uint8_t)i;
367 b[i] = (uint8_t)i;
368 }
369 for (size_t i = 0, c = 'a'; c <= 'z'; ++i, ++c) {
370 a[256 + i] = (uint8_t)c;
371 b[256 + 26 + i] = (uint8_t)c;
372 }
373 for (size_t i = 0, c = 'A'; c <= 'Z'; ++i, ++c) {
374 a[256 + 26 + i] = (uint8_t)c;
375 b[256 + i] = (uint8_t)c;
376 }
377
378 ASSERT_TRUE(aws_array_eq_ignore_case(&a, sizeof(a), &b, sizeof(b)));
379 }
380
381 return 0;
382 }
383
384 AWS_TEST_CASE(test_array_eq_c_str, s_test_array_eq_c_str)
385 static int s_test_array_eq_c_str(struct aws_allocator *allocator, void *ctx) {
386 (void)ctx;
387 (void)allocator;
388
389 {
390 uint8_t arr_a[] = {'a', 'b', 'c'};
391 const char *str_a = "abc";
392 const char *str_b = "xyz";
393 const char *str_c = "abcd";
394 const char *empty = "";
395
396 /* Simple */
397 ASSERT_TRUE(aws_array_eq_c_str(arr_a, 3, str_a));
398 ASSERT_FALSE(aws_array_eq_c_str(arr_a, 3, str_b));
399 ASSERT_FALSE(aws_array_eq_c_str(arr_a, 3, str_c));
400
401 /* Referencing self */
402 ASSERT_TRUE(aws_array_eq_c_str(str_a, 3, str_a));
403 ASSERT_FALSE(aws_array_eq_c_str(str_a, 2, str_a));
404
405 /* Check length 0 */
406 ASSERT_TRUE(aws_array_eq_c_str(arr_a, 0, empty));
407 ASSERT_FALSE(aws_array_eq_c_str(arr_a, 0, str_a));
408
409 /* NULL array is OK if length is 0 */
410 ASSERT_TRUE(aws_array_eq_c_str(NULL, 0, empty));
411 ASSERT_FALSE(aws_array_eq_c_str(NULL, 0, str_a));
412 }
413
414 {
415 /* Array is not expected to contain null-terminator */
416 uint8_t arr_a[] = {'a', 'b', 'c', 0};
417 const char *str_a = "abc";
418 ASSERT_FALSE(aws_array_eq_c_str(arr_a, 4, str_a));
419 }
420
421 return 0;
422 }
423
424 AWS_TEST_CASE(test_array_eq_c_str_ignore_case, s_test_array_eq_c_str_ignore_case)
425 static int s_test_array_eq_c_str_ignore_case(struct aws_allocator *allocator, void *ctx) {
426 (void)ctx;
427 (void)allocator;
428
429 {
430 uint8_t arr_a[] = {'a', 'B', 'c'};
431 const char *str_a = "Abc";
432 const char *str_b = "xyz";
433 const char *str_c = "aBcd";
434 const char *empty = "";
435
436 /* Simple */
437 ASSERT_TRUE(aws_array_eq_c_str_ignore_case(arr_a, 3, str_a));
438 ASSERT_FALSE(aws_array_eq_c_str_ignore_case(arr_a, 3, str_b));
439 ASSERT_FALSE(aws_array_eq_c_str_ignore_case(arr_a, 3, str_c));
440
441 /* Referencing self */
442 ASSERT_TRUE(aws_array_eq_c_str_ignore_case(str_a, 3, str_a));
443 ASSERT_FALSE(aws_array_eq_c_str_ignore_case(str_a, 2, str_a));
444
445 /* Check length 0 */
446 ASSERT_TRUE(aws_array_eq_c_str_ignore_case(arr_a, 0, empty));
447 ASSERT_FALSE(aws_array_eq_c_str_ignore_case(arr_a, 0, str_a));
448
449 /* NULL array is OK if length is 0 */
450 ASSERT_TRUE(aws_array_eq_c_str_ignore_case(NULL, 0, empty));
451 ASSERT_FALSE(aws_array_eq_c_str_ignore_case(NULL, 0, str_a));
452 }
453
454 {
455 /* Array is not expected to contain null-terminator */
456 uint8_t arr_a[] = {'a', 'b', 'c', 0};
457 const char *str_a = "abc";
458 ASSERT_FALSE(aws_array_eq_c_str_ignore_case(arr_a, 4, str_a));
459 }
460
461 return 0;
462 }
463
464 AWS_TEST_CASE(test_array_hash_ignore_case, s_test_array_hash_ignore_case)
465 static int s_test_array_hash_ignore_case(struct aws_allocator *allocator, void *ctx) {
466 (void)ctx;
467 (void)allocator;
468
469 {
470 /* Check against known FNV-1A values */
471 uint8_t a[] = {'a', 'b', 'c'};
472 ASSERT_UINT_EQUALS(0xe71fa2190541574bULL, aws_hash_array_ignore_case(a, 3));
473
474 uint8_t b[] = {'A', 'B', 'C'};
475 ASSERT_UINT_EQUALS(0xe71fa2190541574bULL, aws_hash_array_ignore_case(&b, 3));
476 }
477
478 {
479 uint8_t a[] = {'a', 'B', 'c', 1, 2, 3};
480 uint8_t b[] = {'A', 'b', 'c', 1, 2, 3};
481 ASSERT_TRUE(aws_hash_array_ignore_case(a, 6) == aws_hash_array_ignore_case(b, 6));
482 }
483
484 {
485 uint8_t a[] = {'a', 'b', 'c'};
486 uint8_t b[] = {'x', 'y', 'z'};
487 ASSERT_FALSE(aws_hash_array_ignore_case(a, 3) == aws_hash_array_ignore_case(b, 3));
488 }
489
490 return 0;
491 }
492
493 static int s_do_append_dynamic_test(
494 struct aws_allocator *allocator,
495 size_t starting_size,
496 size_t append_size,
497 size_t iterations,
498 int (*append_dynamic)(struct aws_byte_buf *, const struct aws_byte_cursor *)) {
499 struct aws_byte_buf accum_buf;
500 aws_byte_buf_init(&accum_buf, allocator, starting_size);
501 memset(accum_buf.buffer, 0, starting_size);
502 accum_buf.len = starting_size;
503
504 struct aws_byte_buf append_buf;
505 aws_byte_buf_init(&append_buf, allocator, append_size);
506 append_buf.len = append_size;
507
508 struct aws_byte_cursor append_cursor = aws_byte_cursor_from_buf(&append_buf);
509
510 for (size_t i = 0; i < iterations; ++i) {
511
512 /*
513 * Initialize the source and dest buffers to different, easily recognizable byte blocks
514 */
515 memset(append_buf.buffer, 255, append_buf.capacity);
516 memset(accum_buf.buffer, 0, accum_buf.capacity);
517
518 size_t before_size = accum_buf.len;
519 ASSERT_TRUE(append_dynamic(&accum_buf, &append_cursor) == AWS_OP_SUCCESS);
520 size_t after_size = accum_buf.len;
521
522 size_t expected_len = starting_size + (i + 1) * append_size;
523 ASSERT_TRUE(accum_buf.capacity >= expected_len);
524 ASSERT_TRUE(after_size == expected_len);
525
526 /*
527 * Verify post-append contents.
528 *
529 * Check that the result has the right number of 0s followed by the right number of
530 * 255s.
531 */
532 for (size_t bi = 0; bi < before_size; ++bi) {
533 ASSERT_TRUE(accum_buf.buffer[bi] == 0);
534 }
535
536 for (size_t ai = before_size; ai < after_size; ++ai) {
537 ASSERT_TRUE(accum_buf.buffer[ai] == 255);
538 }
539 }
540
541 aws_byte_buf_clean_up(&accum_buf);
542 aws_byte_buf_clean_up(&append_buf);
543
544 return AWS_OP_SUCCESS;
545 }
546
547 static int s_test_byte_buf_write_to_capacity(struct aws_allocator *allocator, void *ctx) {
548 (void)allocator;
549 (void)ctx;
550
551 uint8_t buf_storage[5];
552 AWS_ZERO_ARRAY(buf_storage);
553 struct aws_byte_buf buf = aws_byte_buf_from_empty_array(buf_storage, sizeof(buf_storage));
554
555 /* Test a write to a fresh buffer with plenty of space */
556 struct aws_byte_cursor original_abc = aws_byte_cursor_from_c_str("abc");
557 struct aws_byte_cursor advancing_abc = original_abc;
558 struct aws_byte_cursor written = aws_byte_buf_write_to_capacity(&buf, &advancing_abc);
559 ASSERT_BIN_ARRAYS_EQUALS("abc", 3, buf.buffer, buf.len);
560 ASSERT_UINT_EQUALS(0, advancing_abc.len);
561 ASSERT_BIN_ARRAYS_EQUALS("abc", 3, written.ptr, written.len);
562 ASSERT_PTR_EQUALS(original_abc.ptr, written.ptr);
563
564 /* Test writing again to same buffer, but we can't fit it all */
565 struct aws_byte_cursor advancing_def = aws_byte_cursor_from_c_str("def");
566 written = aws_byte_buf_write_to_capacity(&buf, &advancing_def);
567 ASSERT_UINT_EQUALS(buf.len, buf.capacity);
568 ASSERT_BIN_ARRAYS_EQUALS("abcde", 5, buf.buffer, buf.len);
569 ASSERT_BIN_ARRAYS_EQUALS("f", 1, advancing_def.ptr, advancing_def.len);
570 ASSERT_BIN_ARRAYS_EQUALS("de", 2, written.ptr, written.len);
571
572 /* Test writing a cursor that exactly matches capacity. */
573 aws_byte_buf_reset(&buf, false);
574 struct aws_byte_cursor advancing_filler = aws_byte_cursor_from_c_str("12345");
575 written = aws_byte_buf_write_to_capacity(&buf, &advancing_filler);
576 ASSERT_BIN_ARRAYS_EQUALS("12345", 5, buf.buffer, buf.len);
577 ASSERT_UINT_EQUALS(0, advancing_filler.len);
578 ASSERT_BIN_ARRAYS_EQUALS("12345", 5, written.ptr, written.len);
579
580 /* Test passing an empty cursor. Nothing should happen. */
581 aws_byte_buf_reset(&buf, false);
582 struct aws_byte_cursor advancing_zeroed;
583 AWS_ZERO_STRUCT(advancing_zeroed);
584 written = aws_byte_buf_write_to_capacity(&buf, &advancing_zeroed);
585 ASSERT_UINT_EQUALS(0, buf.len);
586 ASSERT_UINT_EQUALS(0, advancing_zeroed.len);
587 ASSERT_NULL(advancing_zeroed.ptr);
588 ASSERT_UINT_EQUALS(0, written.len);
589 ASSERT_NULL(written.ptr);
590
591 /* Test writing to a full buffer. Nothing should happen. */
592 buf.len = buf.capacity;
593 struct aws_byte_cursor original_nope = aws_byte_cursor_from_c_str("nope");
594 struct aws_byte_cursor advancing_nope = original_nope;
595 written = aws_byte_buf_write_to_capacity(&buf, &advancing_nope);
596 ASSERT_UINT_EQUALS(buf.capacity, buf.len);
597 ASSERT_UINT_EQUALS(original_nope.len, advancing_nope.len);
598 ASSERT_PTR_EQUALS(original_nope.ptr, advancing_nope.ptr);
599 ASSERT_PTR_EQUALS(original_nope.ptr, written.ptr);
600 ASSERT_UINT_EQUALS(0, written.len);
601
602 aws_byte_buf_clean_up(&buf);
603 return 0;
604 }
605 AWS_TEST_CASE(test_byte_buf_write_to_capacity, s_test_byte_buf_write_to_capacity);
606
607 static int s_test_byte_buf_append_dynamic(struct aws_allocator *allocator, void *ctx) {
608 (void)ctx;
609
610 /*
611 * Throw a small sample of different growth request profiles at the functions
612 */
613
614 /*
615 * regular append
616 */
617 ASSERT_TRUE(s_do_append_dynamic_test(allocator, 1, 10000, 1, aws_byte_buf_append_dynamic) == AWS_OP_SUCCESS);
618 ASSERT_TRUE(s_do_append_dynamic_test(allocator, 1, 1, 1000, aws_byte_buf_append_dynamic) == AWS_OP_SUCCESS);
619 ASSERT_TRUE(s_do_append_dynamic_test(allocator, 10000, 1, 2, aws_byte_buf_append_dynamic) == AWS_OP_SUCCESS);
620 ASSERT_TRUE(s_do_append_dynamic_test(allocator, 100, 10, 100, aws_byte_buf_append_dynamic) == AWS_OP_SUCCESS);
621
622 /*
623 * secure append - note we don't attempt to check if the memory was actually zeroed
624 */
625 ASSERT_TRUE(s_do_append_dynamic_test(allocator, 1, 10000, 1, aws_byte_buf_append_dynamic_secure) == AWS_OP_SUCCESS);
626 ASSERT_TRUE(s_do_append_dynamic_test(allocator, 1, 1, 1000, aws_byte_buf_append_dynamic_secure) == AWS_OP_SUCCESS);
627 ASSERT_TRUE(s_do_append_dynamic_test(allocator, 10000, 1, 2, aws_byte_buf_append_dynamic_secure) == AWS_OP_SUCCESS);
628 ASSERT_TRUE(
629 s_do_append_dynamic_test(allocator, 100, 10, 100, aws_byte_buf_append_dynamic_secure) == AWS_OP_SUCCESS);
630
631 return 0;
632 }
633 AWS_TEST_CASE(test_byte_buf_append_dynamic, s_test_byte_buf_append_dynamic)
634
635 static int s_test_byte_buf_append_auto(struct aws_allocator *allocator, void *ctx) {
636 (void)ctx;
637
638 uint8_t src_bytes[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
639 struct aws_byte_cursor src = aws_byte_cursor_from_array(src_bytes, sizeof(src_bytes));
640
641 /*
642 * Case 1: growable buffer (allocator != NULL) — append_auto must
643 * grow the buffer when needed, matching aws_byte_buf_append_dynamic.
644 */
645 {
646 struct aws_byte_buf growable;
647 ASSERT_SUCCESS(aws_byte_buf_init(&growable, allocator, 4));
648 ASSERT_INT_EQUALS(0, growable.len);
649 ASSERT_INT_EQUALS(4, growable.capacity);
650
651 /* Source is 8 bytes; buffer capacity is 4 — must grow. */
652 ASSERT_SUCCESS(aws_byte_buf_append_auto(&growable, &src));
653 ASSERT_INT_EQUALS(8, growable.len);
654 ASSERT_TRUE(growable.capacity >= 8);
655 ASSERT_BIN_ARRAYS_EQUALS(src_bytes, sizeof(src_bytes), growable.buffer, growable.len);
656
657 aws_byte_buf_clean_up(&growable);
658 }
659
660 /*
661 * Case 2: fixed-size buffer (allocator == NULL) where the source
662 * fits — append_auto must succeed via the static path.
663 */
664 {
665 uint8_t backing[16] = {0};
666 struct aws_byte_buf fixed = aws_byte_buf_from_empty_array(backing, sizeof(backing));
667 ASSERT_PTR_EQUALS(NULL, fixed.allocator);
668
669 ASSERT_SUCCESS(aws_byte_buf_append_auto(&fixed, &src));
670 ASSERT_INT_EQUALS(8, fixed.len);
671 ASSERT_INT_EQUALS(16, fixed.capacity);
672 ASSERT_BIN_ARRAYS_EQUALS(src_bytes, sizeof(src_bytes), fixed.buffer, fixed.len);
673
674 /* No clean_up — fixed buffer is stack-backed. */
675 }
676
677 /*
678 * Case 3: fixed-size buffer (allocator == NULL) where the source
679 * does NOT fit — append_auto must fail with
680 * AWS_ERROR_DEST_COPY_TOO_SMALL (loud failure, no realloc attempt).
681 */
682 {
683 uint8_t backing[4] = {0};
684 struct aws_byte_buf too_small = aws_byte_buf_from_empty_array(backing, sizeof(backing));
685 ASSERT_PTR_EQUALS(NULL, too_small.allocator);
686
687 ASSERT_FAILS(aws_byte_buf_append_auto(&too_small, &src));
688 ASSERT_INT_EQUALS(AWS_ERROR_DEST_COPY_TOO_SMALL, aws_last_error());
689 ASSERT_INT_EQUALS(0, too_small.len);
690 ASSERT_INT_EQUALS(4, too_small.capacity);
691 }
692
693 /*
694 * Case 4: empty source on a fixed-size buffer — should succeed
695 * without modifying state.
696 */
697 {
698 uint8_t backing[4] = {0};
699 struct aws_byte_buf fixed = aws_byte_buf_from_empty_array(backing, sizeof(backing));
700 struct aws_byte_cursor empty_src = {.len = 0, .ptr = NULL};
701
702 ASSERT_SUCCESS(aws_byte_buf_append_auto(&fixed, &empty_src));
703 ASSERT_INT_EQUALS(0, fixed.len);
704 }
705
706 return 0;
707 }
708 AWS_TEST_CASE(test_byte_buf_append_auto, s_test_byte_buf_append_auto)
709
710 static uint8_t s_append_byte_array[] = {0xFF, 0xFE, 0xAB, 0x00, 0x55, 0x62};
711
712 static int s_test_byte_buf_append_byte(struct aws_allocator *allocator, void *ctx) {
713 (void)ctx;
714
715 struct aws_byte_buf buffer;
716 aws_byte_buf_init(&buffer, allocator, 1);
717
718 for (size_t i = 0; i < AWS_ARRAY_SIZE(s_append_byte_array); ++i) {
719 ASSERT_SUCCESS(aws_byte_buf_append_byte_dynamic(&buffer, s_append_byte_array[i]));
720 ASSERT_BIN_ARRAYS_EQUALS(s_append_byte_array, i + 1, buffer.buffer, buffer.len);
721 }
722
723 aws_byte_buf_clean_up(&buffer);
724
725 return 0;
726 }
727 AWS_TEST_CASE(test_byte_buf_append_byte, s_test_byte_buf_append_byte)
728
729 AWS_STATIC_STRING_FROM_LITERAL(s_to_lower_test, "UPPerANdLowercASE");
730
731 static int s_test_byte_buf_append_lookup_success(struct aws_allocator *allocator, void *ctx) {
732 (void)ctx;
733
734 struct aws_byte_buf buffer;
735 aws_byte_buf_init(&buffer, allocator, s_to_lower_test->len);
736
737 struct aws_byte_cursor to_lower_cursor = aws_byte_cursor_from_c_str((char *)s_to_lower_test->bytes);
738
739 ASSERT_TRUE(
740 aws_byte_buf_append_with_lookup(&buffer, &to_lower_cursor, aws_lookup_table_to_lower_get()) == AWS_OP_SUCCESS);
741 ASSERT_TRUE(buffer.len == s_to_lower_test->len);
742 for (size_t i = 0; i < s_to_lower_test->len; ++i) {
743 uint8_t value = buffer.buffer[i];
744 ASSERT_TRUE(value > 'Z' || value < 'A');
745 }
746
747 aws_byte_buf_clean_up(&buffer);
748
749 return 0;
750 }
751 AWS_TEST_CASE(test_byte_buf_append_lookup_success, s_test_byte_buf_append_lookup_success)
752
753 static int s_test_reset_body(struct aws_byte_buf *buffer) {
754 struct aws_byte_cursor to_lower_cursor = aws_byte_cursor_from_c_str((char *)s_to_lower_test->bytes);
755
756 ASSERT_TRUE(
757 aws_byte_buf_append_with_lookup(buffer, &to_lower_cursor, aws_lookup_table_to_lower_get()) == AWS_OP_SUCCESS);
758 ASSERT_TRUE(buffer->len == s_to_lower_test->len);
759 for (size_t i = 0; i < s_to_lower_test->len; ++i) {
760 uint8_t value = buffer->buffer[i];
761 ASSERT_TRUE(value > 'Z' || value < 'A');
762 }
763 return 0;
764 }
765 static int s_test_byte_buf_reset(struct aws_allocator *allocator, void *ctx) {
766 (void)ctx;
767
768 struct aws_byte_buf buffer;
769 aws_byte_buf_init(&buffer, allocator, s_to_lower_test->len);
770 ASSERT_SUCCESS(s_test_reset_body(&buffer));
771
772 size_t old_cap = buffer.capacity;
773 aws_byte_buf_reset(&buffer, false);
774 ASSERT_TRUE(buffer.len == 0);
775 ASSERT_TRUE(buffer.capacity == old_cap);
776 ASSERT_SUCCESS(s_test_reset_body(&buffer));
777
778 old_cap = buffer.capacity;
779 aws_byte_buf_reset(&buffer, true);
780 ASSERT_TRUE(buffer.len == 0);
781 ASSERT_TRUE(buffer.capacity == old_cap);
782 for (size_t i = 0; i < buffer.capacity; i++) {
783 ASSERT_TRUE(buffer.buffer[i] == 0);
784 }
785 ASSERT_SUCCESS(s_test_reset_body(&buffer));
786
787 aws_byte_buf_clean_up(&buffer);
788 /* check that reset succeeds even on an empty buffer */
789 aws_byte_buf_reset(&buffer, true);
790 return 0;
791 }
792 AWS_TEST_CASE(test_byte_buf_reset, s_test_byte_buf_reset)
793
794 static int s_test_byte_buf_append_lookup_failure(struct aws_allocator *allocator, void *ctx) {
795 (void)ctx;
796
797 struct aws_byte_buf buffer;
798 aws_byte_buf_init(&buffer, allocator, 3);
799
800 struct aws_byte_cursor to_lower_cursor = aws_byte_cursor_from_c_str((char *)s_to_lower_test->bytes);
801
802 ASSERT_TRUE(
803 aws_byte_buf_append_with_lookup(&buffer, &to_lower_cursor, aws_lookup_table_to_lower_get()) == AWS_OP_ERR);
804
805 aws_byte_buf_clean_up(&buffer);
806
807 return 0;
808 }
809 AWS_TEST_CASE(test_byte_buf_append_lookup_failure, s_test_byte_buf_append_lookup_failure)
810
811 AWS_STATIC_STRING_FROM_LITERAL(s_reserve_test_suffix, "ReserveTest");
812 AWS_STATIC_STRING_FROM_LITERAL(s_reserve_test_prefix, "Successful");
813 AWS_STATIC_STRING_FROM_LITERAL(s_reserve_test_prefix_concatenated, "SuccessfulReserveTest");
814
815 static int s_test_byte_buf_reserve(struct aws_allocator *allocator, void *ctx) {
816 (void)ctx;
817 (void)allocator;
818
819 struct aws_byte_buf buffer;
820 aws_byte_buf_init(&buffer, allocator, s_reserve_test_prefix->len);
821
822 struct aws_byte_cursor prefix_cursor = aws_byte_cursor_from_string(s_reserve_test_prefix);
823 ASSERT_TRUE(aws_byte_buf_append(&buffer, &prefix_cursor) == AWS_OP_SUCCESS);
824
825 struct aws_byte_cursor suffix_cursor = aws_byte_cursor_from_string(s_reserve_test_suffix);
826 ASSERT_TRUE(aws_byte_buf_append(&buffer, &suffix_cursor) == AWS_OP_ERR);
827
828 aws_byte_buf_reserve(&buffer, s_reserve_test_prefix_concatenated->len);
829 ASSERT_TRUE(aws_byte_buf_append(&buffer, &suffix_cursor) == AWS_OP_SUCCESS);
830
831 ASSERT_TRUE(aws_byte_buf_eq_c_str(&buffer, (char *)s_reserve_test_prefix_concatenated->bytes));
832
833 aws_byte_buf_clean_up(&buffer);
834
835 return 0;
836 }
837 AWS_TEST_CASE(test_byte_buf_reserve, s_test_byte_buf_reserve)
838
839 static int s_test_byte_buf_reserve_initial_capacity_zero(struct aws_allocator *allocator, void *ctx) {
840 (void)ctx;
841 struct aws_byte_buf buffer;
842 AWS_ZERO_STRUCT(buffer);
843 size_t capacity = 1;
844 ASSERT_SUCCESS(aws_byte_buf_init(&buffer, allocator, 0));
845 ASSERT_SUCCESS(aws_byte_buf_reserve(&buffer, capacity));
846 ASSERT_TRUE(buffer.capacity == capacity);
847 aws_byte_buf_clean_up(&buffer);
848 return 0;
849 }
850 AWS_TEST_CASE(test_byte_buf_reserve_initial_capacity_zero, s_test_byte_buf_reserve_initial_capacity_zero)
851
852 static int s_test_byte_buf_reserve_relative(struct aws_allocator *allocator, void *ctx) {
853 (void)ctx;
854 (void)allocator;
855
856 struct aws_byte_buf buffer;
857 aws_byte_buf_init(&buffer, allocator, 1);
858
859 struct aws_byte_cursor prefix_cursor = aws_byte_cursor_from_string(s_reserve_test_prefix);
860
861 ASSERT_TRUE(aws_byte_buf_reserve_relative(&buffer, prefix_cursor.len) == AWS_OP_SUCCESS);
862 ASSERT_TRUE(aws_byte_buf_append(&buffer, &prefix_cursor) == AWS_OP_SUCCESS);
863
864 struct aws_byte_cursor suffix_cursor = aws_byte_cursor_from_string(s_reserve_test_suffix);
865 ASSERT_TRUE(aws_byte_buf_reserve_relative(&buffer, suffix_cursor.len) == AWS_OP_SUCCESS);
866 ASSERT_TRUE(aws_byte_buf_append(&buffer, &suffix_cursor) == AWS_OP_SUCCESS);
867
868 ASSERT_TRUE(aws_byte_buf_eq_c_str(&buffer, (char *)s_reserve_test_prefix_concatenated->bytes));
869
870 aws_byte_buf_clean_up(&buffer);
871
872 return 0;
873 }
874 AWS_TEST_CASE(test_byte_buf_reserve_relative, s_test_byte_buf_reserve_relative)
875
876 static int s_test_byte_buf_reserve_smart(struct aws_allocator *allocator, void *ctx) {
877 (void)ctx;
878 (void)allocator;
879
880 struct aws_byte_buf buffer;
881 size_t base = 10;
882 aws_byte_buf_init(&buffer, allocator, base);
883 ASSERT_TRUE(aws_byte_buf_reserve_smart(&buffer, 2 * base) == AWS_OP_SUCCESS);
884 ASSERT_UINT_EQUALS(2 * base, buffer.capacity);
885 ASSERT_TRUE(aws_byte_buf_reserve_smart(&buffer, 3 * base) == AWS_OP_SUCCESS);
886 /* double the previous capacity instead of just expand the capacity to meet the requirement to reduce the number of
887 * allocations */
888 ASSERT_UINT_EQUALS(4 * base, buffer.capacity);
889 ASSERT_TRUE(aws_byte_buf_reserve_smart(&buffer, 5 * base) == AWS_OP_SUCCESS);
890 /* double the previous capacity instead of just expand the capacity to meet the requirement to reduce the number of
891 * allocations */
892 ASSERT_UINT_EQUALS(8 * base, buffer.capacity);
893 ASSERT_TRUE(aws_byte_buf_reserve_smart(&buffer, 5 * base) == AWS_OP_SUCCESS);
894 ASSERT_UINT_EQUALS(8 * base, buffer.capacity);
895
896 aws_byte_buf_clean_up(&buffer);
897
898 return 0;
899 }
900 AWS_TEST_CASE(test_byte_buf_reserve_smart, s_test_byte_buf_reserve_smart)
901
902 static int s_test_byte_buf_reserve_smart_relative(struct aws_allocator *allocator, void *ctx) {
903 (void)ctx;
904 (void)allocator;
905
906 struct aws_byte_buf buffer;
907 aws_byte_buf_init(&buffer, allocator, 1);
908
909 struct aws_byte_cursor prefix_cursor = aws_byte_cursor_from_string(s_reserve_test_prefix);
910 size_t length = prefix_cursor.len;
911
912 ASSERT_TRUE(aws_byte_buf_reserve_smart_relative(&buffer, length) == AWS_OP_SUCCESS);
913 ASSERT_TRUE(aws_byte_buf_append(&buffer, &prefix_cursor) == AWS_OP_SUCCESS);
914 ASSERT_UINT_EQUALS(length, buffer.capacity);
915 ASSERT_TRUE(aws_byte_buf_reserve_smart_relative(&buffer, length) == AWS_OP_SUCCESS);
916 ASSERT_TRUE(aws_byte_buf_append(&buffer, &prefix_cursor) == AWS_OP_SUCCESS);
917 ASSERT_UINT_EQUALS(2 * length, buffer.capacity);
918 ASSERT_TRUE(aws_byte_buf_reserve_smart_relative(&buffer, length) == AWS_OP_SUCCESS);
919 ASSERT_TRUE(aws_byte_buf_append(&buffer, &prefix_cursor) == AWS_OP_SUCCESS);
920 /* 4 times base as it's expanded to twice the original capacity to prevent too many allocation */
921 ASSERT_UINT_EQUALS(4 * length, buffer.capacity);
922 ASSERT_TRUE(aws_byte_buf_reserve_smart_relative(&buffer, length) == AWS_OP_SUCCESS);
923 ASSERT_TRUE(aws_byte_buf_append(&buffer, &prefix_cursor) == AWS_OP_SUCCESS);
924 /* still 4 times base as it doesn't need to expand again */
925 ASSERT_UINT_EQUALS(4 * length, buffer.capacity);
926
927 aws_byte_buf_clean_up(&buffer);
928
929 return 0;
930 }
931 AWS_TEST_CASE(test_byte_buf_reserve_smart_relative, s_test_byte_buf_reserve_smart_relative)
932
933 static int s_test_byte_cursor_starts_with(struct aws_allocator *allocator, void *ctx) {
934 (void)ctx;
935 (void)allocator;
936
937 struct aws_byte_cursor a = aws_byte_cursor_from_c_str("a");
938 struct aws_byte_cursor abcd = aws_byte_cursor_from_c_str("abcd");
939 struct aws_byte_cursor ab = aws_byte_cursor_from_c_str("ab");
940 struct aws_byte_cursor abcde = aws_byte_cursor_from_c_str("abcde");
941 struct aws_byte_cursor ABCD = aws_byte_cursor_from_c_str("ABCD");
942 struct aws_byte_cursor AB = aws_byte_cursor_from_c_str("AB");
943 struct aws_byte_cursor empty_string = aws_byte_cursor_from_c_str("");
944 struct aws_byte_cursor null_char_string = aws_byte_cursor_from_array("\0", 1);
945
946 /* TRUE */
947 ASSERT_TRUE(aws_byte_cursor_starts_with(&a, &a));
948 ASSERT_TRUE(aws_byte_cursor_starts_with(&abcd, &ab));
949 ASSERT_TRUE(aws_byte_cursor_starts_with(&abcd, &abcd));
950 ASSERT_TRUE(aws_byte_cursor_starts_with(&abcd, &empty_string));
951 ASSERT_TRUE(aws_byte_cursor_starts_with(&empty_string, &empty_string));
952
953 /* FALSE */
954 ASSERT_FALSE(aws_byte_cursor_starts_with(&abcd, &abcde));
955 ASSERT_FALSE(aws_byte_cursor_starts_with(&abcd, &ABCD));
956 ASSERT_FALSE(aws_byte_cursor_starts_with(&abcd, &AB));
957 ASSERT_FALSE(aws_byte_cursor_starts_with(&empty_string, &a));
958 ASSERT_FALSE(aws_byte_cursor_starts_with(&empty_string, &null_char_string));
959 ASSERT_FALSE(aws_byte_cursor_starts_with(&abcd, &null_char_string));
960
961 return 0;
962 }
963 AWS_TEST_CASE(test_byte_cursor_starts_with, s_test_byte_cursor_starts_with)
964
965 static int s_test_byte_cursor_starts_with_ignore_case(struct aws_allocator *allocator, void *ctx) {
966 (void)ctx;
967 (void)allocator;
968
969 struct aws_byte_cursor a = aws_byte_cursor_from_c_str("a");
970 struct aws_byte_cursor A = aws_byte_cursor_from_c_str("A");
971 struct aws_byte_cursor abcd = aws_byte_cursor_from_c_str("abcd");
972 struct aws_byte_cursor ABCD = aws_byte_cursor_from_c_str("ABCD");
973 struct aws_byte_cursor abcde = aws_byte_cursor_from_c_str("abcde");
974 struct aws_byte_cursor azcd = aws_byte_cursor_from_c_str("azcd");
975 struct aws_byte_cursor empty_string = aws_byte_cursor_from_c_str("");
976 struct aws_byte_cursor null_char_string = aws_byte_cursor_from_array("\0", 1);
977
978 /* TRUE */
979 ASSERT_TRUE(aws_byte_cursor_starts_with_ignore_case(&abcd, &abcd));
980 ASSERT_TRUE(aws_byte_cursor_starts_with_ignore_case(&abcd, &ABCD));
981 ASSERT_TRUE(aws_byte_cursor_starts_with_ignore_case(&ABCD, &abcd));
982 ASSERT_TRUE(aws_byte_cursor_starts_with_ignore_case(&ABCD, &ABCD));
983
984 ASSERT_TRUE(aws_byte_cursor_starts_with_ignore_case(&abcd, &a));
985 ASSERT_TRUE(aws_byte_cursor_starts_with_ignore_case(&abcd, &A));
986 ASSERT_TRUE(aws_byte_cursor_starts_with_ignore_case(&ABCD, &a));
987 ASSERT_TRUE(aws_byte_cursor_starts_with_ignore_case(&ABCD, &A));
988
989 ASSERT_TRUE(aws_byte_cursor_starts_with_ignore_case(&abcd, &empty_string));
990 ASSERT_TRUE(aws_byte_cursor_starts_with_ignore_case(&empty_string, &empty_string));
991
992 /* FALSE */
993 ASSERT_FALSE(aws_byte_cursor_starts_with_ignore_case(&abcd, &abcde));
994 ASSERT_FALSE(aws_byte_cursor_starts_with_ignore_case(&abcd, &azcd));
995 ASSERT_FALSE(aws_byte_cursor_starts_with_ignore_case(&empty_string, &a));
996 ASSERT_FALSE(aws_byte_cursor_starts_with_ignore_case(&empty_string, &null_char_string));
997
998 return 0;
999 }
1000 AWS_TEST_CASE(test_byte_cursor_starts_with_ignore_case, s_test_byte_cursor_starts_with_ignore_case)
1001
1002 static int s_test_byte_cursor_compare_lexical(struct aws_allocator *allocator, void *ctx) {
1003 (void)ctx;
1004 (void)allocator;
1005
1006 struct aws_byte_cursor test_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("test");
1007 struct aws_byte_cursor test_cursor2 = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("test");
1008 struct aws_byte_cursor test1_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("test1");
1009 struct aws_byte_cursor test2_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("test2");
1010 struct aws_byte_cursor abc_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("abc");
1011
1012 ASSERT_TRUE(aws_byte_cursor_compare_lexical(&test_cursor, &test_cursor2) == 0);
1013
1014 ASSERT_TRUE(aws_byte_cursor_compare_lexical(&test_cursor, &abc_cursor) > 0);
1015 ASSERT_TRUE(aws_byte_cursor_compare_lexical(&abc_cursor, &test_cursor) < 0);
1016
1017 ASSERT_TRUE(aws_byte_cursor_compare_lexical(&test_cursor, &test2_cursor) < 0);
1018 ASSERT_TRUE(aws_byte_cursor_compare_lexical(&test2_cursor, &test_cursor) > 0);
1019
1020 ASSERT_TRUE(aws_byte_cursor_compare_lexical(&test1_cursor, &test2_cursor) < 0);
1021 ASSERT_TRUE(aws_byte_cursor_compare_lexical(&test2_cursor, &test1_cursor) > 0);
1022
1023 struct aws_byte_cursor ff_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xFF\xFF");
1024 struct aws_byte_cursor one_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\x01\x01");
1025
1026 ASSERT_TRUE(aws_byte_cursor_compare_lexical(&ff_cursor, &one_cursor) > 0);
1027 ASSERT_TRUE(aws_byte_cursor_compare_lexical(&one_cursor, &ff_cursor) < 0);
1028
1029 struct aws_byte_cursor Test_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Test");
1030 struct aws_byte_cursor tesT_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("tesT");
1031
1032 ASSERT_TRUE(aws_byte_cursor_compare_lexical(&Test_cursor, &tesT_cursor) < 0);
1033 ASSERT_TRUE(aws_byte_cursor_compare_lexical(&tesT_cursor, &Test_cursor) > 0);
1034
1035 return 0;
1036 }
1037 AWS_TEST_CASE(test_byte_cursor_compare_lexical, s_test_byte_cursor_compare_lexical)
1038
1039 static int s_test_byte_cursor_compare_lookup(struct aws_allocator *allocator, void *ctx) {
1040 (void)ctx;
1041 (void)allocator;
1042
1043 struct aws_byte_cursor Test_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Test");
1044 struct aws_byte_cursor tesT_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("tesT");
1045
1046 ASSERT_TRUE(aws_byte_cursor_compare_lookup(&Test_cursor, &tesT_cursor, aws_lookup_table_to_lower_get()) == 0);
1047 ASSERT_TRUE(aws_byte_cursor_compare_lookup(&tesT_cursor, &Test_cursor, aws_lookup_table_to_lower_get()) == 0);
1048
1049 struct aws_byte_cursor ABC_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("ABC");
1050 struct aws_byte_cursor abc_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("abc");
1051
1052 ASSERT_TRUE(aws_byte_cursor_compare_lexical(&ABC_cursor, &abc_cursor) < 0);
1053
1054 ASSERT_TRUE(aws_byte_cursor_compare_lookup(&ABC_cursor, &abc_cursor, aws_lookup_table_to_lower_get()) == 0);
1055 ASSERT_TRUE(aws_byte_cursor_compare_lookup(&abc_cursor, &ABC_cursor, aws_lookup_table_to_lower_get()) == 0);
1056
1057 ASSERT_TRUE(aws_byte_cursor_compare_lookup(&ABC_cursor, &tesT_cursor, aws_lookup_table_to_lower_get()) < 0);
1058 ASSERT_TRUE(aws_byte_cursor_compare_lookup(&tesT_cursor, &ABC_cursor, aws_lookup_table_to_lower_get()) > 0);
1059
1060 ASSERT_TRUE(aws_byte_cursor_compare_lookup(&abc_cursor, &tesT_cursor, aws_lookup_table_to_lower_get()) < 0);
1061 ASSERT_TRUE(aws_byte_cursor_compare_lookup(&tesT_cursor, &abc_cursor, aws_lookup_table_to_lower_get()) > 0);
1062
1063 ASSERT_TRUE(aws_byte_cursor_compare_lookup(&abc_cursor, &Test_cursor, aws_lookup_table_to_lower_get()) < 0);
1064 ASSERT_TRUE(aws_byte_cursor_compare_lookup(&Test_cursor, &abc_cursor, aws_lookup_table_to_lower_get()) > 0);
1065
1066 return 0;
1067 }
1068 AWS_TEST_CASE(test_byte_cursor_compare_lookup, s_test_byte_cursor_compare_lookup)
1069
1070 static int s_test_byte_buf_init_cache_and_update_cursors(struct aws_allocator *allocator, void *ctx) {
1071 (void)ctx;
1072
1073 { /* store one cursor */
1074 struct aws_byte_cursor cursor = aws_byte_cursor_from_c_str("asdf");
1075 struct aws_byte_buf buf;
1076 ASSERT_SUCCESS(aws_byte_buf_init_cache_and_update_cursors(&buf, allocator, &cursor, NULL));
1077 ASSERT_BIN_ARRAYS_EQUALS("asdf", 4, cursor.ptr, cursor.len);
1078 ASSERT_TRUE(cursor.ptr >= buf.buffer && cursor.ptr < buf.buffer + buf.len);
1079 ASSERT_BIN_ARRAYS_EQUALS("asdf", 4, buf.buffer, buf.len);
1080 ASSERT_UINT_EQUALS(buf.len, buf.capacity);
1081 aws_byte_buf_clean_up(&buf);
1082 }
1083
1084 { /* store no cursors */
1085 struct aws_byte_buf buf;
1086 ASSERT_SUCCESS(aws_byte_buf_init_cache_and_update_cursors(&buf, allocator, NULL));
1087 aws_byte_buf_clean_up(&buf);
1088 }
1089
1090 { /* store multiple cursors */
1091 struct aws_byte_cursor cursor1 = aws_byte_cursor_from_c_str("one");
1092 struct aws_byte_cursor cursor2 = aws_byte_cursor_from_c_str("two");
1093 struct aws_byte_buf buf;
1094 ASSERT_SUCCESS(aws_byte_buf_init_cache_and_update_cursors(&buf, allocator, &cursor1, &cursor2, NULL));
1095 ASSERT_BIN_ARRAYS_EQUALS("one", 3, cursor1.ptr, cursor1.len);
1096 ASSERT_TRUE(cursor1.ptr >= buf.buffer && cursor1.ptr < buf.buffer + buf.len);
1097 ASSERT_BIN_ARRAYS_EQUALS("two", 3, cursor2.ptr, cursor2.len);
1098 ASSERT_TRUE(cursor2.ptr >= buf.buffer && cursor2.ptr < buf.buffer + buf.len);
1099 ASSERT_BIN_ARRAYS_EQUALS("onetwo", 6, buf.buffer, buf.len);
1100 ASSERT_UINT_EQUALS(buf.len, buf.capacity);
1101 aws_byte_buf_clean_up(&buf);
1102 }
1103
1104 { /* store empty string cursor */
1105 struct aws_byte_cursor cursor = aws_byte_cursor_from_c_str("");
1106 struct aws_byte_buf buf;
1107 ASSERT_SUCCESS(aws_byte_buf_init_cache_and_update_cursors(&buf, allocator, &cursor, NULL));
1108 ASSERT_UINT_EQUALS(0, cursor.len);
1109 ASSERT_UINT_EQUALS(0, buf.len);
1110 aws_byte_buf_clean_up(&buf);
1111 }
1112
1113 { /* store zeroed out cursor */
1114 struct aws_byte_cursor cursor;
1115 AWS_ZERO_STRUCT(cursor);
1116 struct aws_byte_buf buf;
1117 ASSERT_SUCCESS(aws_byte_buf_init_cache_and_update_cursors(&buf, allocator, &cursor, NULL));
1118 ASSERT_UINT_EQUALS(0, cursor.len);
1119 ASSERT_UINT_EQUALS(0, buf.len);
1120 aws_byte_buf_clean_up(&buf);
1121 }
1122
1123 { /* store something valid after some empty cursors */
1124 struct aws_byte_cursor cursor_empty = aws_byte_cursor_from_c_str("");
1125 struct aws_byte_cursor cursor_zeroed;
1126 AWS_ZERO_STRUCT(cursor_zeroed);
1127 struct aws_byte_cursor cursor_normal = aws_byte_cursor_from_c_str("normal");
1128 struct aws_byte_buf buf;
1129 ASSERT_SUCCESS(aws_byte_buf_init_cache_and_update_cursors(
1130 &buf, allocator, &cursor_empty, &cursor_zeroed, &cursor_normal, NULL));
1131 ASSERT_UINT_EQUALS(0, cursor_empty.len);
1132 ASSERT_UINT_EQUALS(0, cursor_zeroed.len);
1133 ASSERT_BIN_ARRAYS_EQUALS("normal", 6, cursor_normal.ptr, cursor_normal.len);
1134 ASSERT_TRUE(cursor_normal.ptr >= buf.buffer && cursor_normal.ptr < buf.buffer + buf.len);
1135 ASSERT_BIN_ARRAYS_EQUALS("normal", 6, buf.buffer, buf.len);
1136 ASSERT_UINT_EQUALS(buf.len, buf.capacity);
1137 aws_byte_buf_clean_up(&buf);
1138 }
1139
1140 return 0;
1141 }
1142 AWS_TEST_CASE(test_byte_buf_init_cache_and_update_cursors, s_test_byte_buf_init_cache_and_update_cursors)
1143
1144 static int s_test_byte_buf_empty_appends(struct aws_allocator *allocator, void *ctx) {
1145 (void)ctx;
1146
1147 { /* append */
1148 struct aws_byte_buf buffer;
1149 AWS_ZERO_STRUCT(buffer);
1150
1151 struct aws_byte_cursor zeroed_out;
1152 AWS_ZERO_STRUCT(zeroed_out);
1153 ASSERT_SUCCESS(aws_byte_buf_append(&buffer, &zeroed_out));
1154 ASSERT_UINT_EQUALS(buffer.len, 0);
1155
1156 struct aws_byte_cursor empty = aws_byte_cursor_from_c_str("");
1157 ASSERT_SUCCESS(aws_byte_buf_append(&buffer, &empty));
1158 ASSERT_UINT_EQUALS(buffer.len, 0);
1159 }
1160
1161 { /* dynamic append */
1162 struct aws_byte_buf buffer;
1163 aws_byte_buf_init(&buffer, allocator, 0);
1164
1165 struct aws_byte_cursor zeroed_out;
1166 AWS_ZERO_STRUCT(zeroed_out);
1167 ASSERT_SUCCESS(aws_byte_buf_append_dynamic(&buffer, &zeroed_out));
1168 ASSERT_UINT_EQUALS(buffer.len, 0);
1169
1170 struct aws_byte_cursor empty = aws_byte_cursor_from_c_str("");
1171 ASSERT_SUCCESS(aws_byte_buf_append_dynamic(&buffer, &empty));
1172 ASSERT_UINT_EQUALS(buffer.len, 0);
1173 }
1174
1175 { /* append with lookup */
1176 struct aws_byte_buf buffer;
1177 AWS_ZERO_STRUCT(buffer);
1178
1179 struct aws_byte_cursor zeroed_out;
1180 AWS_ZERO_STRUCT(zeroed_out);
1181 ASSERT_SUCCESS(aws_byte_buf_append_with_lookup(&buffer, &zeroed_out, aws_lookup_table_to_lower_get()));
1182 ASSERT_UINT_EQUALS(buffer.len, 0);
1183
1184 struct aws_byte_cursor empty = aws_byte_cursor_from_c_str("");
1185 ASSERT_SUCCESS(aws_byte_buf_append_with_lookup(&buffer, &empty, aws_lookup_table_to_lower_get()));
1186 ASSERT_UINT_EQUALS(buffer.len, 0);
1187 }
1188
1189 { /* append and update */
1190 struct aws_byte_buf buffer;
1191 AWS_ZERO_STRUCT(buffer);
1192
1193 struct aws_byte_cursor zeroed_out;
1194 AWS_ZERO_STRUCT(zeroed_out);
1195 ASSERT_SUCCESS(aws_byte_buf_append_and_update(&buffer, &zeroed_out));
1196 ASSERT_UINT_EQUALS(buffer.len, 0);
1197 ASSERT_NULL(zeroed_out.ptr, 0);
1198 ASSERT_UINT_EQUALS(zeroed_out.len, 0);
1199
1200 struct aws_byte_cursor empty = aws_byte_cursor_from_c_str("");
1201 ASSERT_SUCCESS(aws_byte_buf_append_and_update(&buffer, &empty));
1202 ASSERT_NULL(empty.ptr, 0);
1203 ASSERT_UINT_EQUALS(empty.len, 0);
1204 }
1205
1206 return 0;
1207 }
1208 AWS_TEST_CASE(test_byte_buf_empty_appends, s_test_byte_buf_empty_appends)
1209
1210 static int s_test_byte_buf_append_and_update_fail(struct aws_allocator *allocator, void *ctx) {
1211 (void)ctx;
1212 (void)allocator;
1213
1214 struct aws_byte_buf buffer;
1215 aws_byte_buf_init(&buffer, allocator, 10);
1216
1217 struct aws_byte_cursor test_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("TOOOOOOOO LONG");
1218 struct aws_byte_cursor test_cursor_copy = test_cursor;
1219
1220 ASSERT_FAILS(aws_byte_buf_append_and_update(&buffer, &test_cursor));
1221 ASSERT_TRUE((test_cursor.ptr == test_cursor_copy.ptr) && (test_cursor.len == test_cursor_copy.len));
1222
1223 aws_byte_buf_clean_up(&buffer);
1224
1225 return 0;
1226 }
1227 AWS_TEST_CASE(test_byte_buf_append_and_update_fail, s_test_byte_buf_append_and_update_fail)
1228
1229 static int s_test_byte_buf_append_and_update_success(struct aws_allocator *allocator, void *ctx) {
1230 (void)ctx;
1231 (void)allocator;
1232
1233 struct aws_byte_buf buffer;
1234 aws_byte_buf_init(&buffer, allocator, 12);
1235
1236 struct aws_byte_cursor test_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("SHORT");
1237 struct aws_byte_cursor test_cursor_copy = test_cursor;
1238
1239 ASSERT_SUCCESS(aws_byte_buf_append_and_update(&buffer, &test_cursor));
1240 ASSERT_TRUE(test_cursor.ptr == buffer.buffer);
1241 ASSERT_TRUE(aws_byte_cursor_eq(&test_cursor, &test_cursor_copy));
1242
1243 struct aws_byte_cursor test_cursor2 = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("STOP");
1244 struct aws_byte_cursor test_cursor_copy2 = test_cursor2;
1245
1246 ASSERT_SUCCESS(aws_byte_buf_append_and_update(&buffer, &test_cursor2));
1247 ASSERT_TRUE(test_cursor2.ptr == buffer.buffer + test_cursor.len);
1248 ASSERT_TRUE(aws_byte_cursor_eq(&test_cursor2, &test_cursor_copy2));
1249
1250 ASSERT_TRUE(buffer.len == test_cursor.len + test_cursor2.len);
1251
1252 aws_byte_buf_clean_up(&buffer);
1253
1254 return 0;
1255 }
1256 AWS_TEST_CASE(test_byte_buf_append_and_update_success, s_test_byte_buf_append_and_update_success)
1257
1258 static int s_test_isalnum(struct aws_allocator *allocator, void *ctx) {
1259 (void)allocator;
1260 (void)ctx;
1261
1262 ASSERT_TRUE(aws_isalnum('0'));
1263 ASSERT_TRUE(aws_isalnum('a'));
1264 ASSERT_TRUE(aws_isalnum('A'));
1265
1266 ASSERT_FALSE(aws_isalnum(' '));
1267 ASSERT_FALSE(aws_isalnum('\0'));
1268
1269 size_t count = 0;
1270 for (size_t i = 0; i <= UINT8_MAX; ++i) {
1271 if (aws_isalnum((uint8_t)i)) {
1272 count++;
1273 }
1274 }
1275 ASSERT_UINT_EQUALS(62, count);
1276
1277 /* should not be affected by C locale */
1278 setlocale(LC_CTYPE, "de_DE.iso88591");
1279 ASSERT_FALSE(aws_isalnum((uint8_t)'\xdf')); /* German letter ß in ISO-8859-1 */
1280
1281 return 0;
1282 }
1283 AWS_TEST_CASE(test_isalnum, s_test_isalnum)
1284
1285 static int s_test_isalpha(struct aws_allocator *allocator, void *ctx) {
1286 (void)allocator;
1287 (void)ctx;
1288
1289 ASSERT_TRUE(aws_isalpha('a'));
1290 ASSERT_TRUE(aws_isalpha('A'));
1291
1292 ASSERT_FALSE(aws_isalpha('0'));
1293 ASSERT_FALSE(aws_isalpha('\0'));
1294 ASSERT_FALSE(aws_isalpha(' '));
1295
1296 size_t count = 0;
1297 for (size_t i = 0; i <= UINT8_MAX; ++i) {
1298 if (aws_isalpha((uint8_t)i)) {
1299 count++;
1300 }
1301 }
1302 ASSERT_UINT_EQUALS(52, count);
1303
1304 /* should not be affected by C locale */
1305 setlocale(LC_CTYPE, "de_DE.iso88591");
1306 ASSERT_FALSE(aws_isalpha((uint8_t)'\xdf')); /* German letter ß in ISO-8859-1 */
1307
1308 return 0;
1309 }
1310 AWS_TEST_CASE(test_isalpha, s_test_isalpha)
1311
1312 static int s_test_isdigit(struct aws_allocator *allocator, void *ctx) {
1313 (void)allocator;
1314 (void)ctx;
1315
1316 ASSERT_TRUE(aws_isdigit('0'));
1317 ASSERT_TRUE(aws_isdigit('9'));
1318
1319 ASSERT_FALSE(aws_isdigit('a'));
1320 ASSERT_FALSE(aws_isdigit('A'));
1321 ASSERT_FALSE(aws_isdigit('\0'));
1322 ASSERT_FALSE(aws_isdigit(' '));
1323
1324 size_t count = 0;
1325 for (size_t i = 0; i <= UINT8_MAX; ++i) {
1326 if (aws_isdigit((uint8_t)i)) {
1327 count++;
1328 }
1329 }
1330 ASSERT_UINT_EQUALS(10, count);
1331
1332 return 0;
1333 }
1334 AWS_TEST_CASE(test_isdigit, s_test_isdigit)
1335
1336 static int s_test_isxdigit(struct aws_allocator *allocator, void *ctx) {
1337 (void)allocator;
1338 (void)ctx;
1339
1340 ASSERT_TRUE(aws_isxdigit('0'));
1341 ASSERT_TRUE(aws_isxdigit('9'));
1342 ASSERT_TRUE(aws_isxdigit('a'));
1343 ASSERT_TRUE(aws_isxdigit('A'));
1344 ASSERT_TRUE(aws_isxdigit('f'));
1345 ASSERT_TRUE(aws_isxdigit('F'));
1346
1347 ASSERT_FALSE(aws_isxdigit('g'));
1348 ASSERT_FALSE(aws_isxdigit('G'));
1349 ASSERT_FALSE(aws_isxdigit('\0'));
1350 ASSERT_FALSE(aws_isxdigit(' '));
1351
1352 size_t count = 0;
1353 for (size_t i = 0; i <= UINT8_MAX; ++i) {
1354 if (aws_isxdigit((uint8_t)i)) {
1355 count++;
1356 }
1357 }
1358 ASSERT_UINT_EQUALS(22, count);
1359
1360 return 0;
1361 }
1362 AWS_TEST_CASE(test_isxdigit, s_test_isxdigit)
1363
1364 static int s_test_isspace(struct aws_allocator *allocator, void *ctx) {
1365 (void)allocator;
1366 (void)ctx;
1367
1368 ASSERT_TRUE(aws_isspace(' '));
1369 ASSERT_TRUE(aws_isspace('\t'));
1370 ASSERT_TRUE(aws_isspace('\n'));
1371 ASSERT_TRUE(aws_isspace('\v'));
1372 ASSERT_TRUE(aws_isspace('\f'));
1373 ASSERT_TRUE(aws_isspace('\r'));
1374
1375 ASSERT_FALSE(aws_isspace('\0'));
1376 ASSERT_FALSE(aws_isspace('a'));
1377 ASSERT_FALSE(aws_isspace(0xA0)); /* NBSP in some code-pages */
1378
1379 size_t count = 0;
1380 for (size_t i = 0; i <= UINT8_MAX; ++i) {
1381 if (aws_isspace((uint8_t)i)) {
1382 count++;
1383 }
1384 }
1385 ASSERT_UINT_EQUALS(6, count);
1386
1387 return 0;
1388 }
1389 AWS_TEST_CASE(test_isspace, s_test_isspace)
1390
1391 AWS_TEST_CASE(test_byte_cursor_utf8_parse_u64, s_byte_cursor_utf8_parse_u64);
1392 static int s_byte_cursor_utf8_parse_u64(struct aws_allocator *allocator, void *ctx) {
1393 (void)allocator;
1394 (void)ctx;
1395
1396 uint64_t val;
1397
1398 /* sanity check */
1399 ASSERT_SUCCESS(aws_byte_cursor_utf8_parse_u64(aws_byte_cursor_from_c_str("0"), &val));
1400 ASSERT_UINT_EQUALS(0, val);
1401
1402 /* every acceptable character */
1403 ASSERT_SUCCESS(aws_byte_cursor_utf8_parse_u64(aws_byte_cursor_from_c_str("9876543210"), &val));
1404 ASSERT_UINT_EQUALS(9876543210, val);
1405
1406 /* max value */
1407 ASSERT_SUCCESS(aws_byte_cursor_utf8_parse_u64(aws_byte_cursor_from_c_str("18446744073709551615"), &val));
1408 ASSERT_UINT_EQUALS(UINT64_MAX, val);
1409
1410 /* leading zeros should have no effect */
1411 ASSERT_SUCCESS(
1412 aws_byte_cursor_utf8_parse_u64(aws_byte_cursor_from_c_str("00000000000018446744073709551615"), &val));
1413 ASSERT_UINT_EQUALS(UINT64_MAX, val);
1414
1415 /* one bigger than max */
1416 ASSERT_ERROR(
1417 AWS_ERROR_OVERFLOW_DETECTED,
1418 aws_byte_cursor_utf8_parse_u64(aws_byte_cursor_from_c_str("18446744073709551616"), &val));
1419
1420 /* overflow on base multiply */
1421 ASSERT_ERROR(
1422 AWS_ERROR_OVERFLOW_DETECTED,
1423 aws_byte_cursor_utf8_parse_u64(aws_byte_cursor_from_c_str("184467440737095516150"), &val));
1424
1425 /* whitespace is not ok */
1426 ASSERT_ERROR(AWS_ERROR_INVALID_ARGUMENT, aws_byte_cursor_utf8_parse_u64(aws_byte_cursor_from_c_str(" 0"), &val));
1427
1428 ASSERT_ERROR(AWS_ERROR_INVALID_ARGUMENT, aws_byte_cursor_utf8_parse_u64(aws_byte_cursor_from_c_str("0 "), &val));
1429
1430 ASSERT_ERROR(AWS_ERROR_INVALID_ARGUMENT, aws_byte_cursor_utf8_parse_u64(aws_byte_cursor_from_c_str("0 0"), &val));
1431
1432 /* blank strings are not ok */
1433 ASSERT_ERROR(AWS_ERROR_INVALID_ARGUMENT, aws_byte_cursor_utf8_parse_u64(aws_byte_cursor_from_c_str(""), &val));
1434
1435 /* hex is not ok */
1436 ASSERT_ERROR(AWS_ERROR_INVALID_ARGUMENT, aws_byte_cursor_utf8_parse_u64(aws_byte_cursor_from_c_str("0x0"), &val));
1437
1438 ASSERT_ERROR(AWS_ERROR_INVALID_ARGUMENT, aws_byte_cursor_utf8_parse_u64(aws_byte_cursor_from_c_str("FF"), &val));
1439
1440 return 0;
1441 }
1442
1443 AWS_TEST_CASE(test_byte_cursor_utf8_parse_i64, s_byte_cursor_utf8_parse_i64);
1444 static int s_byte_cursor_utf8_parse_i64(struct aws_allocator *allocator, void *ctx) {
1445 (void)allocator;
1446 (void)ctx;
1447
1448 int64_t val;
1449
1450 /* sanity check */
1451 ASSERT_SUCCESS(aws_byte_cursor_utf8_parse_i64(aws_byte_cursor_from_c_str("0"), &val));
1452 ASSERT_INT_EQUALS(0, val);
1453
1454 /* every acceptable character */
1455 ASSERT_SUCCESS(aws_byte_cursor_utf8_parse_i64(aws_byte_cursor_from_c_str("9876543210"), &val));
1456 ASSERT_INT_EQUALS(9876543210, val);
1457
1458 /* max value */
1459 ASSERT_SUCCESS(aws_byte_cursor_utf8_parse_i64(aws_byte_cursor_from_c_str("9223372036854775807"), &val));
1460 ASSERT_INT_EQUALS(INT64_MAX, val);
1461
1462 /* min value */
1463 ASSERT_SUCCESS(aws_byte_cursor_utf8_parse_i64(aws_byte_cursor_from_c_str("-9223372036854775808"), &val));
1464 ASSERT_INT_EQUALS(INT64_MIN, val);
1465
1466 /* leading zeros should have no effect */
1467 ASSERT_SUCCESS(aws_byte_cursor_utf8_parse_i64(aws_byte_cursor_from_c_str("0000000000009223372036854775807"), &val));
1468 ASSERT_INT_EQUALS(INT64_MAX, val);
1469
1470 /* one bigger than max */
1471 ASSERT_ERROR(
1472 AWS_ERROR_OVERFLOW_DETECTED,
1473 aws_byte_cursor_utf8_parse_i64(aws_byte_cursor_from_c_str("9223372036854775808"), &val));
1474
1475 /* one less than max */
1476 ASSERT_ERROR(
1477 AWS_ERROR_OVERFLOW_DETECTED,
1478 aws_byte_cursor_utf8_parse_i64(aws_byte_cursor_from_c_str("-9223372036854775809"), &val));
1479
1480 /* whitespace is not ok */
1481 ASSERT_ERROR(AWS_ERROR_INVALID_ARGUMENT, aws_byte_cursor_utf8_parse_i64(aws_byte_cursor_from_c_str(" 0"), &val));
1482
1483 ASSERT_ERROR(AWS_ERROR_INVALID_ARGUMENT, aws_byte_cursor_utf8_parse_i64(aws_byte_cursor_from_c_str("- 0"), &val));
1484
1485 ASSERT_ERROR(AWS_ERROR_INVALID_ARGUMENT, aws_byte_cursor_utf8_parse_i64(aws_byte_cursor_from_c_str("0 "), &val));
1486
1487 ASSERT_ERROR(AWS_ERROR_INVALID_ARGUMENT, aws_byte_cursor_utf8_parse_i64(aws_byte_cursor_from_c_str("0 0"), &val));
1488
1489 /* blank strings are not ok */
1490 ASSERT_ERROR(AWS_ERROR_INVALID_ARGUMENT, aws_byte_cursor_utf8_parse_i64(aws_byte_cursor_from_c_str(""), &val));
1491
1492 /* hex is not ok */
1493 ASSERT_ERROR(AWS_ERROR_INVALID_ARGUMENT, aws_byte_cursor_utf8_parse_i64(aws_byte_cursor_from_c_str("0x0"), &val));
1494
1495 ASSERT_ERROR(AWS_ERROR_INVALID_ARGUMENT, aws_byte_cursor_utf8_parse_i64(aws_byte_cursor_from_c_str("FF"), &val));
1496
1497 return 0;
1498 }
1499
1500 AWS_TEST_CASE(test_byte_cursor_utf8_parse_u64_hex, s_byte_cursor_parse_uint64_hex);
1501 static int s_byte_cursor_parse_uint64_hex(struct aws_allocator *allocator, void *ctx) {
1502 (void)allocator;
1503 (void)ctx;
1504
1505 uint64_t val;
1506
1507 /* sanity check */
1508 ASSERT_SUCCESS(aws_byte_cursor_utf8_parse_u64_hex(aws_byte_cursor_from_c_str("0"), &val));
1509 ASSERT_UINT_EQUALS(0x0, val);
1510
1511 /* every possible character */
1512 ASSERT_SUCCESS(aws_byte_cursor_utf8_parse_u64_hex(aws_byte_cursor_from_c_str("9876543210"), &val));
1513 ASSERT_UINT_EQUALS(0x9876543210, val);
1514
1515 ASSERT_SUCCESS(aws_byte_cursor_utf8_parse_u64_hex(aws_byte_cursor_from_c_str("ABCDEFabcdef"), &val));
1516 ASSERT_UINT_EQUALS(0xABCDEFabcdefULL, val);
1517
1518 /* max value */
1519 ASSERT_SUCCESS(aws_byte_cursor_utf8_parse_u64_hex(aws_byte_cursor_from_c_str("ffffffffffffffff"), &val));
1520 ASSERT_UINT_EQUALS(UINT64_MAX, val);
1521
1522 /* ignore leading zeroes */
1523 ASSERT_SUCCESS(
1524 aws_byte_cursor_utf8_parse_u64_hex(aws_byte_cursor_from_c_str("0000000000000000ffffffffffffffff"), &val));
1525 ASSERT_UINT_EQUALS(UINT64_MAX, val);
1526
1527 /* overflow */
1528 ASSERT_ERROR(
1529 AWS_ERROR_OVERFLOW_DETECTED,
1530 aws_byte_cursor_utf8_parse_u64_hex(aws_byte_cursor_from_c_str("10000000000000000"), &val));
1531
1532 /* overflow - regression test */
1533 ASSERT_ERROR(
1534 AWS_ERROR_OVERFLOW_DETECTED,
1535 aws_byte_cursor_utf8_parse_u64_hex(aws_byte_cursor_from_c_str("fffffffffffffffff"), &val));
1536
1537 /* invalid character */
1538 ASSERT_ERROR(AWS_ERROR_INVALID_ARGUMENT, aws_byte_cursor_utf8_parse_u64_hex(aws_byte_cursor_from_c_str("g"), &val));
1539
1540 return 0;
1541 }