Debian Codegraph @

crt/aws-c-common/tests/string_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/string.h>
7
8 #include <aws/common/hash_table.h>
9 #include <aws/testing/aws_test_harness.h>
10
11 AWS_TEST_CASE(string_tests, s_string_tests_fn);
12 static int s_string_tests_fn(struct aws_allocator *allocator, void *ctx) {
13 (void)ctx;
14
15 /* Test: static string creation from macro works. */
16 AWS_STATIC_STRING_FROM_LITERAL(test_string_1, "foofaraw");
17 ASSERT_NULL(test_string_1->allocator, "Static string should have no allocator.");
18 ASSERT_INT_EQUALS(test_string_1->len, 8, "Length should have been set correctly.");
19 ASSERT_BIN_ARRAYS_EQUALS(
20 aws_string_bytes(test_string_1),
21 test_string_1->len,
22 "foofaraw",
23 8,
24 "Data bytes should have been set correctly.");
25 ASSERT_INT_EQUALS(
26 aws_string_bytes(test_string_1)[test_string_1->len], '\0', "Static string should have null byte at end.");
27
28 /* Test: string creation works. */
29 struct aws_string *test_string_2 = aws_string_new_from_c_str(allocator, "foofaraw");
30 ASSERT_NOT_NULL(test_string_2, "Memory allocation of string should have succeeded.");
31 ASSERT_PTR_EQUALS(test_string_2->allocator, allocator, "Allocator should have been set correctly.");
32 ASSERT_INT_EQUALS(test_string_2->len, 8, "Length should have been set correctly.");
33 ASSERT_BIN_ARRAYS_EQUALS(
34 aws_string_bytes(test_string_2),
35 test_string_2->len,
36 "foofaraw",
37 8,
38 "Data bytes should have been set correctly.");
39 ASSERT_INT_EQUALS(
40 aws_string_bytes(test_string_2)[test_string_2->len],
41 '\0',
42 "String from C-string should have null byte at end.");
43
44 /* Test: strings from first two tests are equal and have same hashes. */
45 ASSERT_TRUE(aws_string_eq(test_string_1, test_string_2), "Buffers should be equal.");
46 ASSERT_INT_EQUALS(
47 aws_hash_string(test_string_1), aws_hash_string(test_string_2), "Hash values of byte buffers should be equal.");
48
49 /* Test: write from string to byte cursor works. */
50 uint8_t dest[8] = {0};
51 struct aws_byte_buf dest_cur = aws_byte_buf_from_empty_array(dest, sizeof(dest));
52
53 ASSERT_TRUE(
54 aws_byte_buf_write_from_whole_string(&dest_cur, test_string_2),
55 "Write from whole string should have succeeded.");
56 ASSERT_BIN_ARRAYS_EQUALS(dest, 8, "foofaraw", 8);
57
58 /* Test: write from string fails cleanly when byte cursor too short. */
59 int8_t short_dest[7] = {0};
60 struct aws_byte_buf short_dest_buf = aws_byte_buf_from_empty_array(short_dest, sizeof(short_dest));
61
62 ASSERT_FALSE(
63 aws_byte_buf_write_from_whole_string(&short_dest_buf, test_string_2),
64 "Write from whole buffer should have failed.");
65 ASSERT_INT_EQUALS(short_dest_buf.len, 0, "Destination cursor length should be unchanged.");
66 ASSERT_INT_EQUALS(0, short_dest_buf.buffer[0], "Destination cursor should not have received data.");
67
68 /* Test: can duplicate both a static string and an allocated one. */
69
70 struct aws_string *dup_string_1 = aws_string_new_from_string(allocator, test_string_1);
71 ASSERT_NOT_NULL(dup_string_1, "Memory allocation of string should have succeeded.");
72 ASSERT_TRUE(aws_string_eq(test_string_1, dup_string_1), "Strings should be equal.");
73
74 struct aws_string *dup_string_2 = aws_string_new_from_string(allocator, test_string_2);
75 ASSERT_NOT_NULL(dup_string_2, "Memory allocation of string should have succeeded.");
76 ASSERT_TRUE(aws_string_eq(test_string_2, dup_string_2), "Strings should be equal.");
77
78 /* Test: can clone_or_reuse both a static string and an allocated one. */
79 struct aws_string *clone_string_1 = aws_string_clone_or_reuse(allocator, test_string_1);
80 ASSERT_NOT_NULL(clone_string_1, "Memory allocation of string should have succeeded.");
81 ASSERT_TRUE(aws_string_eq(test_string_1, clone_string_1), "Strings should be equal.");
82 ASSERT_TRUE(test_string_1 == clone_string_1, "Static strings should be reused");
83
84 struct aws_string *clone_string_2 = aws_string_clone_or_reuse(allocator, test_string_2);
85 ASSERT_NOT_NULL(clone_string_2, "Memory allocation of string should have succeeded.");
86 ASSERT_TRUE(aws_string_eq(test_string_2, clone_string_2), "Strings should be equal.");
87 ASSERT_TRUE(test_string_2 != clone_string_2, "Dynamic strings should not be reused");
88
89 /* Test: all allocated memory is deallocated properly. */
90 aws_string_destroy(test_string_2);
91 aws_string_destroy(dup_string_1);
92 aws_string_destroy(dup_string_2);
93 aws_string_destroy(clone_string_1);
94 aws_string_destroy(clone_string_2);
95
96 return 0;
97 }
98
99 AWS_TEST_CASE(binary_string_test, s_binary_string_test_fn);
100 static int s_binary_string_test_fn(struct aws_allocator *allocator, void *ctx) {
101 (void)ctx;
102
103 uint8_t test_array[] = {0x86, 0x75, 0x30, 0x90, 0x00, 0xde, 0xad, 0xbe, 0xef};
104 size_t len = sizeof(test_array);
105 struct aws_string *binary_string = aws_string_new_from_array(allocator, test_array, len);
106
107 ASSERT_NOT_NULL(binary_string, "Memory allocation of string should have succeeded.");
108 ASSERT_PTR_EQUALS(allocator, binary_string->allocator, "Allocator should have been set correctly.");
109 ASSERT_BIN_ARRAYS_EQUALS(
110 test_array,
111 len,
112 aws_string_bytes(binary_string),
113 binary_string->len,
114 "Binary string bytes should be same as source array.");
115 ASSERT_INT_EQUALS(
116 aws_string_bytes(binary_string)[binary_string->len],
117 0x00,
118 "String from binary array should have null byte at end");
119 aws_string_destroy(binary_string);
120 return 0;
121 }
122
123 AWS_TEST_CASE(string_compare_test, s_string_compare_test_fn);
124 static int s_string_compare_test_fn(struct aws_allocator *allocator, void *ctx) {
125 (void)allocator;
126 (void)ctx;
127
128 AWS_STATIC_STRING_FROM_LITERAL(empty, "");
129 AWS_STATIC_STRING_FROM_LITERAL(foo, "foo");
130 AWS_STATIC_STRING_FROM_LITERAL(bar, "bar");
131 AWS_STATIC_STRING_FROM_LITERAL(foobar, "foobar");
132 AWS_STATIC_STRING_FROM_LITERAL(foo2, "foo");
133 AWS_STATIC_STRING_FROM_LITERAL(foobaz, "foobaz");
134 AWS_STATIC_STRING_FROM_LITERAL(bar_food, "bar food");
135 AWS_STATIC_STRING_FROM_LITERAL(bar_null_food, "bar\0food");
136 AWS_STATIC_STRING_FROM_LITERAL(bar_null_back, "bar\0back");
137
138 ASSERT_TRUE(aws_string_compare(empty, bar) < 0);
139 ASSERT_TRUE(aws_string_compare(foo, bar) > 0);
140 ASSERT_TRUE(aws_string_compare(bar, foo) < 0);
141 ASSERT_TRUE(aws_string_compare(foo, foobar) < 0);
142 ASSERT_TRUE(aws_string_compare(foo, foo2) == 0);
143 ASSERT_TRUE(aws_string_compare(foobar, foobaz) < 0);
144 ASSERT_TRUE(aws_string_compare(foobaz, empty) > 0);
145 ASSERT_TRUE(aws_string_compare(empty, empty) == 0);
146 ASSERT_TRUE(aws_string_compare(foo, bar_food) > 0);
147 ASSERT_TRUE(aws_string_compare(bar_food, bar) > 0);
148 ASSERT_TRUE(aws_string_compare(bar_null_food, bar) > 0);
149 ASSERT_TRUE(aws_string_compare(bar_null_food, bar_food) < 0);
150 ASSERT_TRUE(aws_string_compare(bar_null_food, bar_null_back) > 0);
151
152 /* Test that bytes are being compared as unsigned integers. */
153 AWS_STATIC_STRING_FROM_LITERAL(x80, "\x80");
154 AWS_STATIC_STRING_FROM_LITERAL(x7f, "\x79");
155 ASSERT_TRUE(aws_string_compare(x80, x7f) > 0);
156 ASSERT_TRUE(aws_string_compare(x7f, x80) < 0);
157 return 0;
158 }
159
160 AWS_TEST_CASE(string_destroy_secure_test, string_destroy_secure_test_fn);
161 static int string_destroy_secure_test_fn(struct aws_allocator *allocator, void *ctx) {
162 (void)ctx;
163
164 /* Just verifies all memory was freed. */
165 struct aws_string *empty = aws_string_new_from_c_str(allocator, "");
166 struct aws_string *logorrhea = aws_string_new_from_c_str(allocator, "logorrhea");
167 const uint8_t bytes[] = {0xde, 0xad, 0xbe, 0xef, 0x00, 0x86, 0x75, 0x30, 0x90};
168 struct aws_string *deadbeef = aws_string_new_from_array(allocator, bytes, sizeof(bytes));
169 ASSERT_NOT_NULL(empty, "Memory allocation of string should have succeeded.");
170 ASSERT_NOT_NULL(logorrhea, "Memory allocation of string should have succeeded.");
171 ASSERT_NOT_NULL(deadbeef, "Memory allocation of string should have succeeded.");
172 aws_string_destroy_secure(empty);
173 aws_string_destroy_secure(logorrhea);
174 aws_string_destroy_secure(deadbeef);
175 return 0;
176 }
177
178 static int secure_strlen_test_fn(struct aws_allocator *allocator, void *ctx) {
179 (void)allocator;
180 (void)ctx;
181
182 size_t str_len = 0;
183 const char test_string[] = "HelloWorld!";
184 ASSERT_SUCCESS(aws_secure_strlen(test_string, sizeof(test_string), &str_len));
185 ASSERT_UINT_EQUALS(sizeof(test_string) - 1, str_len);
186
187 ASSERT_ERROR(AWS_ERROR_INVALID_ARGUMENT, aws_secure_strlen(NULL, sizeof(test_string), &str_len));
188 ASSERT_ERROR(AWS_ERROR_INVALID_ARGUMENT, aws_secure_strlen(test_string, sizeof(test_string), NULL));
189 ASSERT_ERROR(
190 AWS_ERROR_C_STRING_BUFFER_NOT_NULL_TERMINATED,
191 aws_secure_strlen(test_string, sizeof(test_string) - 1, &str_len));
192 return AWS_OP_SUCCESS;
193 }
194 AWS_TEST_CASE(secure_strlen_test, secure_strlen_test_fn)