Debian Codegraph @

crt/aws-c-http/tests/test_strutil.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 #include <aws/http/private/strutil.h>
6
7 #include <aws/testing/aws_test_harness.h>
8
9 #define TEST_CASE(NAME) \
10 AWS_TEST_CASE(NAME, s_test_##NAME); \
11 static int s_test_##NAME(struct aws_allocator *allocator, void *ctx)
12
13 AWS_TEST_CASE(strutil_trim_http_whitespace, s_strutil_trim_http_whitespace);
14 static int s_strutil_trim_http_whitespace(struct aws_allocator *allocator, void *ctx) {
15 (void)allocator;
16 (void)ctx;
17
18 struct test {
19 const char *input;
20 const char *expected;
21 };
22
23 struct test tests[] = {
24 {"a", "a"},
25 {" a", "a"},
26 {"a ", "a"},
27 {" a ", "a"},
28 {"", ""},
29 {" ", ""},
30 {" ", ""},
31 {"a", "a"},
32 {"\t", ""},
33 {"\ta", "a"},
34 {"a\t", "a"},
35 {"\t a \t", "a"},
36 };
37
38 for (size_t i = 0; i < AWS_ARRAY_SIZE(tests); ++i) {
39 struct aws_byte_cursor input = aws_byte_cursor_from_c_str(tests[i].input);
40 struct aws_byte_cursor expected = aws_byte_cursor_from_c_str(tests[i].expected);
41 struct aws_byte_cursor trimmed = aws_strutil_trim_http_whitespace(input);
42 ASSERT_TRUE(aws_byte_cursor_eq(&expected, &trimmed));
43 }
44
45 return 0;
46 }
47
48 AWS_TEST_CASE(strutil_is_http_token, s_strutil_is_http_token);
49 static int s_strutil_is_http_token(struct aws_allocator *allocator, void *ctx) {
50 (void)allocator;
51 (void)ctx;
52
53 /* sanity check */
54 ASSERT_TRUE(aws_strutil_is_http_token(aws_byte_cursor_from_c_str("A")));
55 ASSERT_TRUE(aws_strutil_is_http_token(aws_byte_cursor_from_c_str("Host")));
56
57 /* must be at least 1 character long*/
58 ASSERT_FALSE(aws_strutil_is_http_token(aws_byte_cursor_from_c_str("")));
59
60 /* all acceptable characters (RFC-7230 3.2.6 - tchar)*/
61 const char *all_acceptable = "!#$%&'*+-.^_`|~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
62 const size_t all_acceptable_strlen = strlen(all_acceptable);
63 ASSERT_TRUE(aws_strutil_is_http_token(aws_byte_cursor_from_c_str(all_acceptable)));
64
65 /* brute force over every character, and be sure it fails if it's not in the acceptable list */
66 for (size_t i = 0; i < 256; ++i) {
67 uint8_t c = (uint8_t)i;
68 bool is_acceptable = memchr(all_acceptable, c, all_acceptable_strlen) != NULL;
69 ASSERT_UINT_EQUALS(is_acceptable, aws_strutil_is_http_token(aws_byte_cursor_from_array(&c, 1)));
70 }
71
72 return 0;
73 }
74
75 AWS_TEST_CASE(strutil_is_lowercase_http_token, s_strutil_is_lowercase_http_token);
76 static int s_strutil_is_lowercase_http_token(struct aws_allocator *allocator, void *ctx) {
77 (void)allocator;
78 (void)ctx;
79
80 /* sanity check */
81 ASSERT_TRUE(aws_strutil_is_lowercase_http_token(aws_byte_cursor_from_c_str("a")));
82 ASSERT_TRUE(aws_strutil_is_lowercase_http_token(aws_byte_cursor_from_c_str("host")));
83
84 /* must be at least 1 character long*/
85 ASSERT_FALSE(aws_strutil_is_lowercase_http_token(aws_byte_cursor_from_c_str("")));
86
87 /* forbidden characters */
88 ASSERT_FALSE(aws_strutil_is_lowercase_http_token(aws_byte_cursor_from_c_str("Host")));
89 ASSERT_FALSE(aws_strutil_is_lowercase_http_token(aws_byte_cursor_from_c_str(":\"")));
90
91 /* all acceptable characters (RFC-7230 3.2.6 - tchar, but with uppercase removed) */
92 const char *all_acceptable = "!#$%&'*+-.^_`|~0123456789abcdefghijklmnopqrstuvwxyz";
93 const size_t all_acceptable_strlen = strlen(all_acceptable);
94 ASSERT_TRUE(aws_strutil_is_lowercase_http_token(aws_byte_cursor_from_c_str(all_acceptable)));
95
96 /* brute force over every character, and be sure it fails if it's not in the acceptable list */
97 for (size_t i = 0; i < 256; ++i) {
98 uint8_t c = (uint8_t)i;
99 bool is_acceptable = memchr(all_acceptable, c, all_acceptable_strlen) != NULL;
100 ASSERT_UINT_EQUALS(is_acceptable, aws_strutil_is_lowercase_http_token(aws_byte_cursor_from_array(&c, 1)));
101 }
102
103 return 0;
104 }
105
106 AWS_TEST_CASE(strutil_is_http_field_value, s_strutil_is_http_field_value);
107 static int s_strutil_is_http_field_value(struct aws_allocator *allocator, void *ctx) {
108 (void)allocator;
109 (void)ctx;
110
111 /* sanity check */
112 ASSERT_TRUE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str("0")));
113 ASSERT_TRUE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str("en")));
114
115 /* OK to have empty value */
116 ASSERT_TRUE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str("")));
117
118 /* OK to have whitespace in the middle */
119 ASSERT_TRUE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str("a b")));
120 ASSERT_TRUE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str("a\tb")));
121 ASSERT_TRUE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str("a\t\t \t\t b")));
122
123 /* Bad to have whitespace at the start or the end */
124 ASSERT_FALSE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str(" 999")));
125 ASSERT_FALSE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str("999 ")));
126 ASSERT_FALSE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str("\t999")));
127 ASSERT_FALSE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str("999\t")));
128
129 /* OK to use UTF-8 */
130 ASSERT_TRUE(aws_strutil_is_http_field_value(
131 aws_byte_cursor_from_c_str("\xF0\x9F\x91\x81\xF0\x9F\x91\x84\xF0\x9F\x91\x81")));
132
133 /* Bad to have line-folds */
134 ASSERT_FALSE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str("item1\r\n item2")));
135 ASSERT_FALSE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str("item1\r item2")));
136 ASSERT_FALSE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str("item1\n item2")));
137
138 /* The implementation uses a table of valid characters (for speed reasons).
139 * Lets test every possible byte value and make sure it lines up with what we expect.
140 * We'll put the test byte at index [1] of an otherwise valid string */
141 char mutable_str[] = {'a', 'b', 'c'};
142 for (size_t i = 0; i < 256; ++i) {
143 /* Grammar looks like:
144 * field-value = *( field-content / obs-fold ) ; we're forbidding obs-fold so ignore it
145 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
146 * field-vchar = VCHAR / obs-text
147 * VCHAR = %x21-7E ; visible (printing) characters
148 * obs-text = %x80-FF
149 */
150 bool allowed_in_grammar = (/*SP*/ i == ' ') || (/*HTAB*/ i == '\t') || (/*VCHAR*/ i >= 0x21 && i <= 0x7E) ||
151 (/*obs-text*/ i >= 0x80 && i <= 0xFF);
152
153 mutable_str[1] = (char)i;
154 struct aws_byte_cursor cursor = aws_byte_cursor_from_array(mutable_str, AWS_ARRAY_SIZE(mutable_str));
155 bool passes = aws_strutil_is_http_field_value(cursor);
156 ASSERT_INT_EQUALS(allowed_in_grammar, passes, "failed at character 0x%02X", i);
157 }
158
159 return 0;
160 }
161
162 AWS_TEST_CASE(strutil_is_http_reason_phrase, s_strutil_is_http_reason_phrase);
163 static int s_strutil_is_http_reason_phrase(struct aws_allocator *allocator, void *ctx) {
164 (void)allocator;
165 (void)ctx;
166
167 /* sanity check */
168 ASSERT_TRUE(aws_strutil_is_http_reason_phrase(aws_byte_cursor_from_c_str("OK")));
169
170 /* OK to have empty value */
171 ASSERT_TRUE(aws_strutil_is_http_reason_phrase(aws_byte_cursor_from_c_str("")));
172
173 /* OK to have whitespace in the middle, beginning, or end */
174 ASSERT_TRUE(aws_strutil_is_http_reason_phrase(aws_byte_cursor_from_c_str("Not Found")));
175 ASSERT_TRUE(aws_strutil_is_http_reason_phrase(aws_byte_cursor_from_c_str("Not\tFound")));
176 ASSERT_TRUE(aws_strutil_is_http_reason_phrase(aws_byte_cursor_from_c_str(" Not Found")));
177 ASSERT_TRUE(aws_strutil_is_http_reason_phrase(aws_byte_cursor_from_c_str("Not Found ")));
178 ASSERT_TRUE(aws_strutil_is_http_reason_phrase(aws_byte_cursor_from_c_str("\t Not\t\t Found \t")));
179 ASSERT_TRUE(aws_strutil_is_http_reason_phrase(aws_byte_cursor_from_c_str(" ")));
180 ASSERT_TRUE(aws_strutil_is_http_reason_phrase(aws_byte_cursor_from_c_str("\t")));
181
182 /* OK to use UTF-8 */
183 ASSERT_TRUE(aws_strutil_is_http_reason_phrase(
184 aws_byte_cursor_from_c_str("\xF0\x9F\x91\x81\xF0\x9F\x91\x84\xF0\x9F\x91\x81")));
185
186 /* Bad to have line-folds or other anything like it*/
187 ASSERT_FALSE(aws_strutil_is_http_reason_phrase(aws_byte_cursor_from_c_str("Line\r\nFolds")));
188 ASSERT_FALSE(aws_strutil_is_http_reason_phrase(aws_byte_cursor_from_c_str("Line\rFeed")));
189 ASSERT_FALSE(aws_strutil_is_http_reason_phrase(aws_byte_cursor_from_c_str("New\nLine")));
190
191 /* The implementation uses a table of valid characters (for speed reasons).
192 * Lets test every possible byte value and make sure it lines up with what we expect.
193 * We'll put the test byte at index [1] of an otherwise valid string */
194 char mutable_str[] = {'a', 'b', 'c'};
195 for (size_t i = 0; i < 256; ++i) {
196 /* Grammar looks like:
197 * reason-phrase = *( HTAB / SP / VCHAR / obs-text )
198 * VCHAR = %x21-7E ; visible (printing) characters
199 * obs-text = %x80-FF
200 */
201 bool allowed_in_grammar = (/*SP*/ i == ' ') || (/*HTAB*/ i == '\t') || (/*VCHAR*/ i >= 0x21 && i <= 0x7E) ||
202 (/*obs-text*/ i >= 0x80 && i <= 0xFF);
203
204 mutable_str[1] = (char)i;
205 struct aws_byte_cursor cursor = aws_byte_cursor_from_array(mutable_str, AWS_ARRAY_SIZE(mutable_str));
206 bool passes = aws_strutil_is_http_reason_phrase(cursor);
207 ASSERT_INT_EQUALS(allowed_in_grammar, passes, "failed at character 0x%02X", i);
208 }
209
210 return 0;
211 }
212
213 AWS_TEST_CASE(strutil_is_http_request_target, s_strutil_is_http_request_target);
214 static int s_strutil_is_http_request_target(struct aws_allocator *allocator, void *ctx) {
215 (void)allocator;
216 (void)ctx;
217
218 /* sanity check */
219 ASSERT_TRUE(aws_strutil_is_http_request_target(aws_byte_cursor_from_c_str("/")));
220
221 /* Bad to have empty value */
222 ASSERT_FALSE(aws_strutil_is_http_request_target(aws_byte_cursor_from_c_str("")));
223
224 /* Bad to have non-visible ascii */
225 ASSERT_FALSE(aws_strutil_is_http_request_target(aws_byte_cursor_from_c_str(" ")));
226 ASSERT_FALSE(aws_strutil_is_http_request_target(aws_byte_cursor_from_c_str("/spaces-are-bad .html")));
227 ASSERT_FALSE(aws_strutil_is_http_request_target(aws_byte_cursor_from_c_str("/tabs-are-bad\t.html")));
228 ASSERT_FALSE(aws_strutil_is_http_request_target(aws_byte_cursor_from_c_str("/crlf-is-really-bad\r\n.html")));
229 ASSERT_FALSE(aws_strutil_is_http_request_target(aws_byte_cursor_from_c_str("/newline-is-bad\n.html")));
230 ASSERT_FALSE(aws_strutil_is_http_request_target(aws_byte_cursor_from_c_str("/linefeed-is-bad\r.html")));
231
232 /* OK origin-form */
233 ASSERT_TRUE(aws_strutil_is_http_request_target(aws_byte_cursor_from_c_str("/where?q=now")));
234
235 /* OK absolute-form */
236 ASSERT_TRUE(aws_strutil_is_http_request_target(aws_byte_cursor_from_c_str("http://www.amazon.com/index.html")));
237
238 /* OK authority-form */
239 ASSERT_TRUE(aws_strutil_is_http_request_target(aws_byte_cursor_from_c_str("www.example.com:80")));
240
241 /* OK asterisk-form */
242 ASSERT_TRUE(aws_strutil_is_http_request_target(aws_byte_cursor_from_c_str("*")));
243
244 /* TODO: Actually check the complete grammar as defined in RFC7230 5.3 and
245 * RFC3986. Currently this just checks whether the sequence is blatantly illegal
246 * (ex: contains CR or LF) */
247
248 return 0;
249 }
250
251 AWS_TEST_CASE(strutil_is_http_pseudo_header_name, s_strutil_is_http_pseudo_header_name);
252 static int s_strutil_is_http_pseudo_header_name(struct aws_allocator *allocator, void *ctx) {
253 (void)allocator;
254 (void)ctx;
255
256 /* sanity check */
257 ASSERT_TRUE(aws_strutil_is_http_pseudo_header_name(aws_byte_cursor_from_c_str(":method")));
258 ASSERT_TRUE(aws_strutil_is_http_pseudo_header_name(aws_byte_cursor_from_c_str(":scheme")));
259 ASSERT_TRUE(aws_strutil_is_http_pseudo_header_name(aws_byte_cursor_from_c_str(":authority")));
260 ASSERT_TRUE(aws_strutil_is_http_pseudo_header_name(aws_byte_cursor_from_c_str(":path")));
261 ASSERT_TRUE(aws_strutil_is_http_pseudo_header_name(aws_byte_cursor_from_c_str(":status")));
262
263 /* Bad to have empty value */
264 ASSERT_FALSE(aws_strutil_is_http_pseudo_header_name(aws_byte_cursor_from_c_str("")));
265
266 /* Bad to have other values */
267 ASSERT_FALSE(aws_strutil_is_http_pseudo_header_name(aws_byte_cursor_from_c_str("connect")));
268 ASSERT_FALSE(aws_strutil_is_http_pseudo_header_name(aws_byte_cursor_from_c_str("Method")));
269 ASSERT_FALSE(aws_strutil_is_http_pseudo_header_name(aws_byte_cursor_from_c_str("httpCRT")));
270 return 0;
271 }