Debian Codegraph @

crt/aws-c-common/tests/uri_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 #include <aws/common/uri.h>
6 #include <aws/testing/aws_test_harness.h>
7
8 static int s_test_uri_full_parse(struct aws_allocator *allocator, void *ctx) {
9 (void)ctx;
10 const char *str_uri = "https://some_user:[email protected]:8443/path/to/"
11 "resource?test1=value1&test%20space=value%20space&test2=value2&test2=value3";
12
13 struct aws_byte_cursor uri_csr = aws_byte_cursor_from_c_str(str_uri);
14 struct aws_uri uri;
15 ASSERT_SUCCESS(aws_uri_init_parse(&uri, allocator, &uri_csr));
16
17 struct aws_byte_cursor expected_scheme = aws_byte_cursor_from_c_str("https");
18 ASSERT_BIN_ARRAYS_EQUALS(expected_scheme.ptr, expected_scheme.len, uri.scheme.ptr, uri.scheme.len);
19
20 struct aws_byte_cursor expected_authority = aws_byte_cursor_from_c_str("some_user:[email protected]:8443");
21 ASSERT_BIN_ARRAYS_EQUALS(expected_authority.ptr, expected_authority.len, uri.authority.ptr, uri.authority.len);
22
23 struct aws_byte_cursor expected_userinfo = aws_byte_cursor_from_c_str("some_user:some_password");
24 ASSERT_BIN_ARRAYS_EQUALS(expected_userinfo.ptr, expected_userinfo.len, uri.userinfo.ptr, uri.userinfo.len);
25
26 struct aws_byte_cursor expected_user = aws_byte_cursor_from_c_str("some_user");
27 ASSERT_BIN_ARRAYS_EQUALS(expected_user.ptr, expected_user.len, uri.user.ptr, uri.user.len);
28
29 struct aws_byte_cursor expected_password = aws_byte_cursor_from_c_str("some_password");
30 ASSERT_BIN_ARRAYS_EQUALS(expected_password.ptr, expected_password.len, uri.password.ptr, uri.password.len);
31
32 struct aws_byte_cursor expected_host = aws_byte_cursor_from_c_str("www.test.com");
33 ASSERT_BIN_ARRAYS_EQUALS(expected_host.ptr, expected_host.len, uri.host_name.ptr, uri.host_name.len);
34
35 ASSERT_UINT_EQUALS(8443, uri.port);
36
37 struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str("/path/to/resource");
38 ASSERT_BIN_ARRAYS_EQUALS(expected_path.ptr, expected_path.len, uri.path.ptr, uri.path.len);
39
40 struct aws_byte_cursor expected_query_str =
41 aws_byte_cursor_from_c_str("test1=value1&test%20space=value%20space&test2=value2&test2=value3");
42 ASSERT_BIN_ARRAYS_EQUALS(
43 expected_query_str.ptr, expected_query_str.len, uri.query_string.ptr, uri.query_string.len);
44
45 struct aws_byte_cursor expected_request_uri = aws_byte_cursor_from_c_str(
46 "/path/to/resource?test1=value1&test%20space=value%20space&test2=value2&test2=value3");
47 ASSERT_BIN_ARRAYS_EQUALS(
48 expected_request_uri.ptr, expected_request_uri.len, uri.path_and_query.ptr, uri.path_and_query.len);
49
50 aws_uri_clean_up(&uri);
51 return AWS_OP_SUCCESS;
52 }
53
54 AWS_TEST_CASE(uri_full_parse, s_test_uri_full_parse);
55
56 static int s_test_uri_no_scheme_parse(struct aws_allocator *allocator, void *ctx) {
57 (void)ctx;
58 const char *str_uri =
59 "www.test.com:8443/path/to/resource?test1=value1&test%20space=value%20space&test2=value2&test2=value3";
60
61 struct aws_byte_cursor uri_csr = aws_byte_cursor_from_c_str(str_uri);
62 struct aws_uri uri;
63 ASSERT_SUCCESS(aws_uri_init_parse(&uri, allocator, &uri_csr));
64
65 ASSERT_UINT_EQUALS(0U, uri.scheme.len);
66
67 struct aws_byte_cursor expected_authority = aws_byte_cursor_from_c_str("www.test.com:8443");
68 ASSERT_BIN_ARRAYS_EQUALS(expected_authority.ptr, expected_authority.len, uri.authority.ptr, uri.authority.len);
69
70 struct aws_byte_cursor expected_host = aws_byte_cursor_from_c_str("www.test.com");
71 ASSERT_BIN_ARRAYS_EQUALS(expected_host.ptr, expected_host.len, uri.host_name.ptr, uri.host_name.len);
72
73 ASSERT_UINT_EQUALS(8443, uri.port);
74
75 struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str("/path/to/resource");
76 ASSERT_BIN_ARRAYS_EQUALS(expected_path.ptr, expected_path.len, uri.path.ptr, uri.path.len);
77
78 struct aws_byte_cursor expected_query_str =
79 aws_byte_cursor_from_c_str("test1=value1&test%20space=value%20space&test2=value2&test2=value3");
80 ASSERT_BIN_ARRAYS_EQUALS(
81 expected_query_str.ptr, expected_query_str.len, uri.query_string.ptr, uri.query_string.len);
82
83 struct aws_byte_cursor expected_request_uri = aws_byte_cursor_from_c_str(
84 "/path/to/resource?test1=value1&test%20space=value%20space&test2=value2&test2=value3");
85 ASSERT_BIN_ARRAYS_EQUALS(
86 expected_request_uri.ptr, expected_request_uri.len, uri.path_and_query.ptr, uri.path_and_query.len);
87
88 aws_uri_clean_up(&uri);
89 return AWS_OP_SUCCESS;
90 }
91
92 AWS_TEST_CASE(uri_no_scheme_parse, s_test_uri_no_scheme_parse);
93
94 static int s_test_uri_no_port_parse(struct aws_allocator *allocator, void *ctx) {
95 (void)ctx;
96 const char *str_uri =
97 "https://www.test.com/path/to/resource?test1=value1&test%20space=value%20space&test2=value2&test2=value3";
98
99 struct aws_byte_cursor uri_csr = aws_byte_cursor_from_c_str(str_uri);
100 struct aws_uri uri;
101 ASSERT_SUCCESS(aws_uri_init_parse(&uri, allocator, &uri_csr));
102
103 struct aws_byte_cursor expected_scheme = aws_byte_cursor_from_c_str("https");
104 ASSERT_BIN_ARRAYS_EQUALS(expected_scheme.ptr, expected_scheme.len, uri.scheme.ptr, uri.scheme.len);
105
106 struct aws_byte_cursor expected_authority = aws_byte_cursor_from_c_str("www.test.com");
107 ASSERT_BIN_ARRAYS_EQUALS(expected_authority.ptr, expected_authority.len, uri.authority.ptr, uri.authority.len);
108
109 struct aws_byte_cursor expected_host = aws_byte_cursor_from_c_str("www.test.com");
110 ASSERT_BIN_ARRAYS_EQUALS(expected_host.ptr, expected_host.len, uri.host_name.ptr, uri.host_name.len);
111
112 ASSERT_UINT_EQUALS(0, uri.port);
113
114 struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str("/path/to/resource");
115 ASSERT_BIN_ARRAYS_EQUALS(expected_path.ptr, expected_path.len, uri.path.ptr, uri.path.len);
116
117 struct aws_byte_cursor expected_query_str =
118 aws_byte_cursor_from_c_str("test1=value1&test%20space=value%20space&test2=value2&test2=value3");
119 ASSERT_BIN_ARRAYS_EQUALS(
120 expected_query_str.ptr, expected_query_str.len, uri.query_string.ptr, uri.query_string.len);
121
122 struct aws_byte_cursor expected_request_uri = aws_byte_cursor_from_c_str(
123 "/path/to/resource?test1=value1&test%20space=value%20space&test2=value2&test2=value3");
124 ASSERT_BIN_ARRAYS_EQUALS(
125 expected_request_uri.ptr, expected_request_uri.len, uri.path_and_query.ptr, uri.path_and_query.len);
126
127 aws_uri_clean_up(&uri);
128 return AWS_OP_SUCCESS;
129 }
130
131 AWS_TEST_CASE(uri_no_port_parse, s_test_uri_no_port_parse);
132
133 static int s_test_uri_no_path_parse(struct aws_allocator *allocator, void *ctx) {
134 (void)ctx;
135 const char *str_uri =
136 "https://www.test.com:8443/?test1=value1&test%20space=value%20space&test2=value2&test2=value3";
137
138 struct aws_byte_cursor uri_csr = aws_byte_cursor_from_c_str(str_uri);
139 struct aws_uri uri;
140 ASSERT_SUCCESS(aws_uri_init_parse(&uri, allocator, &uri_csr));
141
142 struct aws_byte_cursor expected_scheme = aws_byte_cursor_from_c_str("https");
143 ASSERT_BIN_ARRAYS_EQUALS(expected_scheme.ptr, expected_scheme.len, uri.scheme.ptr, uri.scheme.len);
144
145 struct aws_byte_cursor expected_authority = aws_byte_cursor_from_c_str("www.test.com:8443");
146 ASSERT_BIN_ARRAYS_EQUALS(expected_authority.ptr, expected_authority.len, uri.authority.ptr, uri.authority.len);
147
148 struct aws_byte_cursor expected_host = aws_byte_cursor_from_c_str("www.test.com");
149 ASSERT_BIN_ARRAYS_EQUALS(expected_host.ptr, expected_host.len, uri.host_name.ptr, uri.host_name.len);
150
151 ASSERT_UINT_EQUALS(8443, uri.port);
152
153 struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str("/");
154 ASSERT_BIN_ARRAYS_EQUALS(expected_path.ptr, expected_path.len, uri.path.ptr, uri.path.len);
155
156 struct aws_byte_cursor expected_query_str =
157 aws_byte_cursor_from_c_str("test1=value1&test%20space=value%20space&test2=value2&test2=value3");
158 ASSERT_BIN_ARRAYS_EQUALS(
159 expected_query_str.ptr, expected_query_str.len, uri.query_string.ptr, uri.query_string.len);
160
161 struct aws_byte_cursor expected_request_uri =
162 aws_byte_cursor_from_c_str("/?test1=value1&test%20space=value%20space&test2=value2&test2=value3");
163 ASSERT_BIN_ARRAYS_EQUALS(
164 expected_request_uri.ptr, expected_request_uri.len, uri.path_and_query.ptr, uri.path_and_query.len);
165 aws_uri_clean_up(&uri);
166 return AWS_OP_SUCCESS;
167 }
168
169 AWS_TEST_CASE(uri_no_path_parse, s_test_uri_no_path_parse);
170
171 static int s_test_uri_no_query_parse(struct aws_allocator *allocator, void *ctx) {
172 (void)ctx;
173 const char *str_uri = "https://www.test.com:8443/path/to/resource";
174
175 struct aws_byte_cursor uri_csr = aws_byte_cursor_from_c_str(str_uri);
176 struct aws_uri uri;
177 ASSERT_SUCCESS(aws_uri_init_parse(&uri, allocator, &uri_csr));
178
179 struct aws_byte_cursor expected_scheme = aws_byte_cursor_from_c_str("https");
180 ASSERT_BIN_ARRAYS_EQUALS(expected_scheme.ptr, expected_scheme.len, uri.scheme.ptr, uri.scheme.len);
181
182 struct aws_byte_cursor expected_authority = aws_byte_cursor_from_c_str("www.test.com:8443");
183 ASSERT_BIN_ARRAYS_EQUALS(expected_authority.ptr, expected_authority.len, uri.authority.ptr, uri.authority.len);
184
185 struct aws_byte_cursor expected_host = aws_byte_cursor_from_c_str("www.test.com");
186 ASSERT_BIN_ARRAYS_EQUALS(expected_host.ptr, expected_host.len, uri.host_name.ptr, uri.host_name.len);
187
188 ASSERT_UINT_EQUALS(8443, uri.port);
189
190 struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str("/path/to/resource");
191 ASSERT_BIN_ARRAYS_EQUALS(expected_path.ptr, expected_path.len, uri.path.ptr, uri.path.len);
192
193 struct aws_byte_cursor expected_request_uri = aws_byte_cursor_from_c_str("/path/to/resource");
194 ASSERT_BIN_ARRAYS_EQUALS(
195 expected_request_uri.ptr, expected_request_uri.len, uri.path_and_query.ptr, uri.path_and_query.len);
196
197 ASSERT_UINT_EQUALS(0U, uri.query_string.len);
198
199 aws_uri_clean_up(&uri);
200 return AWS_OP_SUCCESS;
201 }
202
203 AWS_TEST_CASE(uri_no_query_parse, s_test_uri_no_query_parse);
204
205 static int s_test_uri_minimal_parse(struct aws_allocator *allocator, void *ctx) {
206 (void)ctx;
207 const char *str_uri = "www.test.com/path/to/resource";
208
209 struct aws_byte_cursor uri_csr = aws_byte_cursor_from_c_str(str_uri);
210 struct aws_uri uri;
211 ASSERT_SUCCESS(aws_uri_init_parse(&uri, allocator, &uri_csr));
212
213 ASSERT_UINT_EQUALS(0U, uri.scheme.len);
214
215 struct aws_byte_cursor expected_authority = aws_byte_cursor_from_c_str("www.test.com");
216 ASSERT_BIN_ARRAYS_EQUALS(expected_authority.ptr, expected_authority.len, uri.authority.ptr, uri.authority.len);
217
218 struct aws_byte_cursor expected_host = aws_byte_cursor_from_c_str("www.test.com");
219 ASSERT_BIN_ARRAYS_EQUALS(expected_host.ptr, expected_host.len, uri.host_name.ptr, uri.host_name.len);
220
221 ASSERT_UINT_EQUALS(0, uri.port);
222
223 struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str("/path/to/resource");
224 ASSERT_BIN_ARRAYS_EQUALS(expected_path.ptr, expected_path.len, uri.path.ptr, uri.path.len);
225
226 struct aws_byte_cursor expected_request_uri = aws_byte_cursor_from_c_str("/path/to/resource");
227 ASSERT_BIN_ARRAYS_EQUALS(
228 expected_request_uri.ptr, expected_request_uri.len, uri.path_and_query.ptr, uri.path_and_query.len);
229
230 ASSERT_UINT_EQUALS(0U, uri.query_string.len);
231
232 aws_uri_clean_up(&uri);
233 return AWS_OP_SUCCESS;
234 }
235
236 AWS_TEST_CASE(uri_minimal_parse, s_test_uri_minimal_parse);
237
238 static int s_test_uri_path_and_query_only_parse(struct aws_allocator *allocator, void *ctx) {
239 (void)ctx;
240 const char *str_uri = "/path/to/resource?test1=value1&test%20space=value%20space&test2=value2&test2=value3";
241
242 struct aws_byte_cursor uri_csr = aws_byte_cursor_from_c_str(str_uri);
243 struct aws_uri uri;
244 ASSERT_SUCCESS(aws_uri_init_parse(&uri, allocator, &uri_csr));
245
246 ASSERT_UINT_EQUALS(0U, uri.scheme.len);
247 ASSERT_UINT_EQUALS(0U, uri.authority.len);
248
249 struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str("/path/to/resource");
250 ASSERT_BIN_ARRAYS_EQUALS(expected_path.ptr, expected_path.len, uri.path.ptr, uri.path.len);
251
252 struct aws_byte_cursor expected_query_str =
253 aws_byte_cursor_from_c_str("test1=value1&test%20space=value%20space&test2=value2&test2=value3");
254 ASSERT_BIN_ARRAYS_EQUALS(
255 expected_query_str.ptr, expected_query_str.len, uri.query_string.ptr, uri.query_string.len);
256
257 struct aws_byte_cursor expected_path_and_query = aws_byte_cursor_from_c_str(
258 "/path/to/resource?test1=value1&test%20space=value%20space&test2=value2&test2=value3");
259 ASSERT_BIN_ARRAYS_EQUALS(
260 expected_path_and_query.ptr, expected_path_and_query.len, uri.path_and_query.ptr, uri.path_and_query.len);
261
262 aws_uri_clean_up(&uri);
263 return AWS_OP_SUCCESS;
264 }
265
266 AWS_TEST_CASE(uri_path_and_query_only_parse, s_test_uri_path_and_query_only_parse);
267
268 static int s_test_uri_root_only_parse(struct aws_allocator *allocator, void *ctx) {
269 (void)ctx;
270 const char *str_uri = "https://www.test.com";
271
272 struct aws_byte_cursor uri_csr = aws_byte_cursor_from_c_str(str_uri);
273 struct aws_uri uri;
274 ASSERT_SUCCESS(aws_uri_init_parse(&uri, allocator, &uri_csr));
275
276 struct aws_byte_cursor expected_scheme = aws_byte_cursor_from_c_str("https");
277 ASSERT_BIN_ARRAYS_EQUALS(expected_scheme.ptr, expected_scheme.len, uri.scheme.ptr, uri.scheme.len);
278
279 struct aws_byte_cursor expected_authority = aws_byte_cursor_from_c_str("www.test.com");
280 ASSERT_BIN_ARRAYS_EQUALS(expected_authority.ptr, expected_authority.len, uri.authority.ptr, uri.authority.len);
281
282 struct aws_byte_cursor expected_host = aws_byte_cursor_from_c_str("www.test.com");
283 ASSERT_BIN_ARRAYS_EQUALS(expected_host.ptr, expected_host.len, uri.host_name.ptr, uri.host_name.len);
284
285 ASSERT_UINT_EQUALS(0, uri.port);
286
287 struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str("");
288 ASSERT_BIN_ARRAYS_EQUALS(expected_path.ptr, expected_path.len, uri.path.ptr, uri.path.len);
289
290 ASSERT_UINT_EQUALS(0U, uri.query_string.len);
291
292 aws_uri_clean_up(&uri);
293 return AWS_OP_SUCCESS;
294 }
295
296 AWS_TEST_CASE(uri_root_only_parse, s_test_uri_root_only_parse);
297
298 static int s_test_uri_root_slash_only_path_parse(struct aws_allocator *allocator, void *ctx) {
299 (void)ctx;
300 const char *str_uri = "https://www.test.com/";
301
302 struct aws_byte_cursor uri_csr = aws_byte_cursor_from_c_str(str_uri);
303 struct aws_uri uri;
304 ASSERT_SUCCESS(aws_uri_init_parse(&uri, allocator, &uri_csr));
305
306 struct aws_byte_cursor expected_scheme = aws_byte_cursor_from_c_str("https");
307 ASSERT_BIN_ARRAYS_EQUALS(expected_scheme.ptr, expected_scheme.len, uri.scheme.ptr, uri.scheme.len);
308
309 struct aws_byte_cursor expected_authority = aws_byte_cursor_from_c_str("www.test.com");
310 ASSERT_BIN_ARRAYS_EQUALS(expected_authority.ptr, expected_authority.len, uri.authority.ptr, uri.authority.len);
311
312 struct aws_byte_cursor expected_host = aws_byte_cursor_from_c_str("www.test.com");
313 ASSERT_BIN_ARRAYS_EQUALS(expected_host.ptr, expected_host.len, uri.host_name.ptr, uri.host_name.len);
314
315 ASSERT_UINT_EQUALS(0, uri.port);
316
317 struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str("/");
318 ASSERT_BIN_ARRAYS_EQUALS(expected_path.ptr, expected_path.len, uri.path.ptr, uri.path.len);
319
320 ASSERT_UINT_EQUALS(0U, uri.query_string.len);
321
322 aws_uri_clean_up(&uri);
323 return AWS_OP_SUCCESS;
324 }
325
326 AWS_TEST_CASE(uri_root_slash_only_path_parse, s_test_uri_root_slash_only_path_parse);
327
328 static int s_test_uri_userinfo_no_password_parse(struct aws_allocator *allocator, void *ctx) {
329 (void)ctx;
330 /* RFC-3986 section 3.2.1: Use of the format "user:password" in the userinfo field is deprecated.
331 * We will try to parse the userinfo with the format still, but if not happening, it will not be treated as an
332 * error. The whole userinfo will still be available to access */
333 const char *str_uri = "https://[email protected]";
334
335 struct aws_byte_cursor uri_csr = aws_byte_cursor_from_c_str(str_uri);
336 struct aws_uri uri;
337 ASSERT_SUCCESS(aws_uri_init_parse(&uri, allocator, &uri_csr));
338
339 struct aws_byte_cursor expected_scheme = aws_byte_cursor_from_c_str("https");
340 ASSERT_BIN_ARRAYS_EQUALS(expected_scheme.ptr, expected_scheme.len, uri.scheme.ptr, uri.scheme.len);
341
342 struct aws_byte_cursor expected_authority = aws_byte_cursor_from_c_str("[email protected]");
343 ASSERT_BIN_ARRAYS_EQUALS(expected_authority.ptr, expected_authority.len, uri.authority.ptr, uri.authority.len);
344
345 struct aws_byte_cursor expected_userinfo = aws_byte_cursor_from_c_str("some_name");
346 ASSERT_BIN_ARRAYS_EQUALS(expected_userinfo.ptr, expected_userinfo.len, uri.userinfo.ptr, uri.userinfo.len);
347
348 struct aws_byte_cursor expected_user = aws_byte_cursor_from_c_str("some_name");
349 ASSERT_BIN_ARRAYS_EQUALS(expected_user.ptr, expected_user.len, uri.user.ptr, uri.user.len);
350 ASSERT_UINT_EQUALS(0U, uri.password.len);
351
352 struct aws_byte_cursor expected_host = aws_byte_cursor_from_c_str("www.test.com");
353 ASSERT_BIN_ARRAYS_EQUALS(expected_host.ptr, expected_host.len, uri.host_name.ptr, uri.host_name.len);
354
355 ASSERT_UINT_EQUALS(0, uri.port);
356
357 struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str("");
358 ASSERT_BIN_ARRAYS_EQUALS(expected_path.ptr, expected_path.len, uri.path.ptr, uri.path.len);
359
360 ASSERT_UINT_EQUALS(0U, uri.query_string.len);
361
362 aws_uri_clean_up(&uri);
363 return AWS_OP_SUCCESS;
364 }
365
366 AWS_TEST_CASE(uri_userinfo_no_password_parse, s_test_uri_userinfo_no_password_parse);
367
368 static int s_test_uri_empty_user_parse(struct aws_allocator *allocator, void *ctx) {
369 (void)ctx;
370 /* RFC-3986 section 3.2.1: Use of the format "user:password" in the userinfo field is deprecated.
371 * We will try to parse the userinfo with the format still, but if not happening, it will not be treated as an
372 * error. The whole userinfo will still be available to access */
373 const char *str_uri = "https://@www.test.com";
374
375 struct aws_byte_cursor uri_csr = aws_byte_cursor_from_c_str(str_uri);
376 struct aws_uri uri;
377 ASSERT_SUCCESS(aws_uri_init_parse(&uri, allocator, &uri_csr));
378
379 struct aws_byte_cursor expected_scheme = aws_byte_cursor_from_c_str("https");
380 ASSERT_BIN_ARRAYS_EQUALS(expected_scheme.ptr, expected_scheme.len, uri.scheme.ptr, uri.scheme.len);
381
382 struct aws_byte_cursor expected_authority = aws_byte_cursor_from_c_str("@www.test.com");
383 ASSERT_BIN_ARRAYS_EQUALS(expected_authority.ptr, expected_authority.len, uri.authority.ptr, uri.authority.len);
384
385 ASSERT_UINT_EQUALS(0U, uri.userinfo.len);
386 ASSERT_UINT_EQUALS(0U, uri.user.len);
387 ASSERT_UINT_EQUALS(0U, uri.password.len);
388
389 struct aws_byte_cursor expected_host = aws_byte_cursor_from_c_str("www.test.com");
390 ASSERT_BIN_ARRAYS_EQUALS(expected_host.ptr, expected_host.len, uri.host_name.ptr, uri.host_name.len);
391
392 ASSERT_UINT_EQUALS(0, uri.port);
393
394 struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str("");
395 ASSERT_BIN_ARRAYS_EQUALS(expected_path.ptr, expected_path.len, uri.path.ptr, uri.path.len);
396
397 ASSERT_UINT_EQUALS(0U, uri.query_string.len);
398
399 aws_uri_clean_up(&uri);
400 return AWS_OP_SUCCESS;
401 }
402
403 AWS_TEST_CASE(uri_empty_user_parse, s_test_uri_empty_user_parse);
404
405 static int s_test_uri_query_params(struct aws_allocator *allocator, void *ctx) {
406 (void)ctx;
407 const char *str_uri = "https://www.test.com:8443/path/to/"
408 "resource?test1=value1&testkeyonly&&test%20space=value%20space&test2=value2&test2=value3";
409
410 struct aws_byte_cursor uri_csr = aws_byte_cursor_from_c_str(str_uri);
411 struct aws_uri uri;
412 ASSERT_SUCCESS(aws_uri_init_parse(&uri, allocator, &uri_csr));
413
414 struct aws_uri_param params[5];
415 AWS_ZERO_ARRAY(params);
416 struct aws_array_list params_list;
417 aws_array_list_init_static(&params_list, &params, 5, sizeof(struct aws_uri_param));
418
419 ASSERT_SUCCESS(aws_uri_query_string_params(&uri, &params_list));
420 ASSERT_UINT_EQUALS(5u, aws_array_list_length(&params_list));
421
422 struct aws_byte_cursor expected_key = aws_byte_cursor_from_c_str("test1");
423 struct aws_byte_cursor expected_value = aws_byte_cursor_from_c_str("value1");
424
425 ASSERT_BIN_ARRAYS_EQUALS(expected_key.ptr, expected_key.len, params[0].key.ptr, params[0].key.len);
426 ASSERT_BIN_ARRAYS_EQUALS(expected_value.ptr, expected_value.len, params[0].value.ptr, params[0].value.len);
427
428 expected_key = aws_byte_cursor_from_c_str("testkeyonly");
429
430 ASSERT_BIN_ARRAYS_EQUALS(expected_key.ptr, expected_key.len, params[1].key.ptr, params[1].key.len);
431 ASSERT_UINT_EQUALS(0U, params[1].value.len);
432
433 expected_key = aws_byte_cursor_from_c_str("test%20space");
434 expected_value = aws_byte_cursor_from_c_str("value%20space");
435
436 ASSERT_BIN_ARRAYS_EQUALS(expected_key.ptr, expected_key.len, params[2].key.ptr, params[2].key.len);
437 ASSERT_BIN_ARRAYS_EQUALS(expected_value.ptr, expected_value.len, params[2].value.ptr, params[2].value.len);
438
439 expected_key = aws_byte_cursor_from_c_str("test2");
440 expected_value = aws_byte_cursor_from_c_str("value2");
441
442 ASSERT_BIN_ARRAYS_EQUALS(expected_key.ptr, expected_key.len, params[3].key.ptr, params[3].key.len);
443 ASSERT_BIN_ARRAYS_EQUALS(expected_value.ptr, expected_value.len, params[3].value.ptr, params[3].value.len);
444
445 expected_key = aws_byte_cursor_from_c_str("test2");
446 expected_value = aws_byte_cursor_from_c_str("value3");
447
448 ASSERT_BIN_ARRAYS_EQUALS(expected_key.ptr, expected_key.len, params[4].key.ptr, params[4].key.len);
449 ASSERT_BIN_ARRAYS_EQUALS(expected_value.ptr, expected_value.len, params[4].value.ptr, params[4].value.len);
450
451 aws_uri_clean_up(&uri);
452
453 /* Test empty query string */
454 str_uri = "https://www.test.com:8443/path/to/resource";
455 uri_csr = aws_byte_cursor_from_c_str(str_uri);
456 ASSERT_SUCCESS(aws_uri_init_parse(&uri, allocator, &uri_csr));
457 aws_array_list_clear(&params_list);
458 ASSERT_SUCCESS(aws_uri_query_string_params(&uri, &params_list));
459 ASSERT_UINT_EQUALS(0, aws_array_list_length(&params_list));
460 aws_uri_clean_up(&uri);
461
462 return AWS_OP_SUCCESS;
463 }
464
465 AWS_TEST_CASE(uri_query_params, s_test_uri_query_params);
466
467 static int s_test_uri_ipv4_parse(struct aws_allocator *allocator, void *ctx) {
468 (void)ctx;
469 const char *str_uri = "https://127.0.0.1:8443";
470
471 struct aws_byte_cursor uri_csr = aws_byte_cursor_from_c_str(str_uri);
472 struct aws_uri uri;
473 ASSERT_SUCCESS(aws_uri_init_parse(&uri, allocator, &uri_csr));
474
475 struct aws_byte_cursor expected_scheme = aws_byte_cursor_from_c_str("https");
476 ASSERT_BIN_ARRAYS_EQUALS(expected_scheme.ptr, expected_scheme.len, uri.scheme.ptr, uri.scheme.len);
477
478 struct aws_byte_cursor expected_authority = aws_byte_cursor_from_c_str("127.0.0.1:8443");
479 ASSERT_BIN_ARRAYS_EQUALS(expected_authority.ptr, expected_authority.len, uri.authority.ptr, uri.authority.len);
480
481 struct aws_byte_cursor expected_host = aws_byte_cursor_from_c_str("127.0.0.1");
482 ASSERT_BIN_ARRAYS_EQUALS(expected_host.ptr, expected_host.len, uri.host_name.ptr, uri.host_name.len);
483
484 ASSERT_UINT_EQUALS(8443, uri.port);
485
486 struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str("");
487 ASSERT_BIN_ARRAYS_EQUALS(expected_path.ptr, expected_path.len, uri.path.ptr, uri.path.len);
488
489 ASSERT_UINT_EQUALS(0U, uri.query_string.len);
490
491 aws_uri_clean_up(&uri);
492 return AWS_OP_SUCCESS;
493 }
494
495 AWS_TEST_CASE(uri_ipv4_parse, s_test_uri_ipv4_parse);
496
497 static int s_test_uri_ipv6_parse(struct aws_allocator *allocator, void *ctx) {
498 (void)ctx;
499 const char *str_uri = "https://[2001:db8:85a3:8d3:1319:8a2e:370:7348%25en0]:443";
500
501 struct aws_byte_cursor uri_csr = aws_byte_cursor_from_c_str(str_uri);
502 struct aws_uri uri;
503 ASSERT_SUCCESS(aws_uri_init_parse(&uri, allocator, &uri_csr));
504
505 struct aws_byte_cursor expected_scheme = aws_byte_cursor_from_c_str("https");
506 ASSERT_BIN_ARRAYS_EQUALS(expected_scheme.ptr, expected_scheme.len, uri.scheme.ptr, uri.scheme.len);
507
508 struct aws_byte_cursor expected_authority =
509 aws_byte_cursor_from_c_str("[2001:db8:85a3:8d3:1319:8a2e:370:7348%25en0]:443");
510 ASSERT_BIN_ARRAYS_EQUALS(expected_authority.ptr, expected_authority.len, uri.authority.ptr, uri.authority.len);
511
512 struct aws_byte_cursor expected_host = aws_byte_cursor_from_c_str("2001:db8:85a3:8d3:1319:8a2e:370:7348%25en0");
513 ASSERT_BIN_ARRAYS_EQUALS(expected_host.ptr, expected_host.len, uri.host_name.ptr, uri.host_name.len);
514
515 ASSERT_UINT_EQUALS(443, uri.port);
516
517 struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str("");
518 ASSERT_BIN_ARRAYS_EQUALS(expected_path.ptr, expected_path.len, uri.path.ptr, uri.path.len);
519
520 ASSERT_UINT_EQUALS(0U, uri.query_string.len);
521
522 aws_uri_clean_up(&uri);
523 return AWS_OP_SUCCESS;
524 }
525
526 AWS_TEST_CASE(uri_ipv6_parse, s_test_uri_ipv6_parse);
527
528 static int s_test_uri_ipv6_no_port_parse(struct aws_allocator *allocator, void *ctx) {
529 (void)ctx;
530 const char *str_uri = "https://[2001:db8:85a3:8d3:1319:8a2e:370:7348%25en0]";
531
532 struct aws_byte_cursor uri_csr = aws_byte_cursor_from_c_str(str_uri);
533 struct aws_uri uri;
534 ASSERT_SUCCESS(aws_uri_init_parse(&uri, allocator, &uri_csr));
535
536 struct aws_byte_cursor expected_scheme = aws_byte_cursor_from_c_str("https");
537 ASSERT_BIN_ARRAYS_EQUALS(expected_scheme.ptr, expected_scheme.len, uri.scheme.ptr, uri.scheme.len);
538
539 struct aws_byte_cursor expected_authority =
540 aws_byte_cursor_from_c_str("[2001:db8:85a3:8d3:1319:8a2e:370:7348%25en0]");
541 ASSERT_BIN_ARRAYS_EQUALS(expected_authority.ptr, expected_authority.len, uri.authority.ptr, uri.authority.len);
542
543 struct aws_byte_cursor expected_host = aws_byte_cursor_from_c_str("2001:db8:85a3:8d3:1319:8a2e:370:7348%25en0");
544 ASSERT_BIN_ARRAYS_EQUALS(expected_host.ptr, expected_host.len, uri.host_name.ptr, uri.host_name.len);
545
546 struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str("");
547 ASSERT_BIN_ARRAYS_EQUALS(expected_path.ptr, expected_path.len, uri.path.ptr, uri.path.len);
548
549 ASSERT_UINT_EQUALS(0U, uri.query_string.len);
550
551 aws_uri_clean_up(&uri);
552 return AWS_OP_SUCCESS;
553 }
554
555 AWS_TEST_CASE(uri_ipv6_no_port_parse, s_test_uri_ipv6_no_port_parse);
556
557 static int s_test_uri_invalid_scheme_parse(struct aws_allocator *allocator, void *ctx) {
558 (void)ctx;
559 const char *str_uri =
560 "https:/www.test.com:8443/path/to/resource?test1=value1&test%20space=value%20space&test2=value2&test2=value3";
561
562 struct aws_byte_cursor uri_csr = aws_byte_cursor_from_c_str(str_uri);
563 struct aws_uri uri;
564 ASSERT_ERROR(AWS_ERROR_MALFORMED_INPUT_STRING, aws_uri_init_parse(&uri, allocator, &uri_csr));
565 return AWS_OP_SUCCESS;
566 }
567
568 AWS_TEST_CASE(uri_invalid_scheme_parse, s_test_uri_invalid_scheme_parse);
569
570 static int s_test_uri_invalid_port_parse(struct aws_allocator *allocator, void *ctx) {
571 (void)ctx;
572 const char *str_uri =
573 "https://www.test.com:s8443/path/to/resource?test1=value1&test%20space=value%20space&test2=value2&test2=value3";
574
575 struct aws_byte_cursor uri_csr = aws_byte_cursor_from_c_str(str_uri);
576 struct aws_uri uri;
577 ASSERT_ERROR(AWS_ERROR_MALFORMED_INPUT_STRING, aws_uri_init_parse(&uri, allocator, &uri_csr));
578 return AWS_OP_SUCCESS;
579 }
580
581 AWS_TEST_CASE(uri_invalid_port_parse, s_test_uri_invalid_port_parse);
582
583 static int s_test_uri_port_too_large_parse(struct aws_allocator *allocator, void *ctx) {
584 (void)ctx;
585 const char *str_uri = "https://www.test.com:4294967296/path/to/"
586 "resource?test1=value1&test%20space=value%20space&test2=value2&test2=value3";
587
588 struct aws_byte_cursor uri_csr = aws_byte_cursor_from_c_str(str_uri);
589 struct aws_uri uri;
590 ASSERT_ERROR(AWS_ERROR_MALFORMED_INPUT_STRING, aws_uri_init_parse(&uri, allocator, &uri_csr));
591 return AWS_OP_SUCCESS;
592 }
593
594 AWS_TEST_CASE(uri_port_too_large_parse, s_test_uri_port_too_large_parse);
595
596 static int s_test_uri_single_colon_parse(struct aws_allocator *allocator, void *ctx) {
597 (void)ctx;
598 const char *str_uri = ":";
599 struct aws_byte_cursor uri_csr = aws_byte_cursor_from_array(str_uri, 1);
600 struct aws_uri uri;
601 ASSERT_SUCCESS(aws_uri_init_parse(&uri, allocator, &uri_csr));
602 aws_uri_clean_up(&uri);
603 return AWS_OP_SUCCESS;
604 }
605
606 AWS_TEST_CASE(uri_single_colon_parse, s_test_uri_single_colon_parse);
607
608 static int s_test_uri_builder(struct aws_allocator *allocator, void *ctx) {
609 (void)ctx;
610 const char *str_uri =
611 "https://www.test.com:8443/path/to/resource?test1=value1&test%20space=value%20space&test2=value2&test2=value3";
612
613 struct aws_byte_cursor uri_csr = aws_byte_cursor_from_c_str(str_uri);
614 struct aws_uri uri;
615 ASSERT_SUCCESS(aws_uri_init_parse(&uri, allocator, &uri_csr));
616
617 struct aws_uri_param params[4];
618 AWS_ZERO_ARRAY(params);
619
620 struct aws_array_list params_list;
621 aws_array_list_init_static(&params_list, &params, 4, sizeof(struct aws_uri_param));
622
623 ASSERT_SUCCESS(aws_uri_query_string_params(&uri, &params_list));
624
625 struct aws_uri_builder_options builder_args = {
626 .scheme = uri.scheme,
627 .path = uri.path,
628 .host_name = uri.host_name,
629 .port = uri.port,
630 .query_params = &params_list,
631 };
632
633 struct aws_uri built_uri;
634 ASSERT_SUCCESS(aws_uri_init_from_builder_options(&built_uri, allocator, &builder_args));
635
636 struct aws_byte_cursor expected_scheme = aws_byte_cursor_from_c_str("https");
637 ASSERT_BIN_ARRAYS_EQUALS(expected_scheme.ptr, expected_scheme.len, built_uri.scheme.ptr, built_uri.scheme.len);
638
639 struct aws_byte_cursor expected_authority = aws_byte_cursor_from_c_str("www.test.com:8443");
640 ASSERT_BIN_ARRAYS_EQUALS(
641 expected_authority.ptr, expected_authority.len, built_uri.authority.ptr, built_uri.authority.len);
642
643 struct aws_byte_cursor expected_host = aws_byte_cursor_from_c_str("www.test.com");
644 ASSERT_BIN_ARRAYS_EQUALS(expected_host.ptr, expected_host.len, built_uri.host_name.ptr, built_uri.host_name.len);
645
646 ASSERT_UINT_EQUALS(8443, built_uri.port);
647
648 struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str("/path/to/resource");
649 ASSERT_BIN_ARRAYS_EQUALS(expected_path.ptr, expected_path.len, built_uri.path.ptr, built_uri.path.len);
650
651 struct aws_byte_cursor expected_query_str =
652 aws_byte_cursor_from_c_str("test1=value1&test%20space=value%20space&test2=value2&test2=value3");
653 ASSERT_BIN_ARRAYS_EQUALS(
654 expected_query_str.ptr, expected_query_str.len, built_uri.query_string.ptr, built_uri.query_string.len);
655
656 struct aws_byte_cursor expected_request_uri = aws_byte_cursor_from_c_str(
657 "/path/to/resource?test1=value1&test%20space=value%20space&test2=value2&test2=value3");
658 ASSERT_BIN_ARRAYS_EQUALS(
659 expected_request_uri.ptr, expected_request_uri.len, built_uri.path_and_query.ptr, built_uri.path_and_query.len);
660
661 aws_uri_clean_up(&uri);
662 aws_uri_clean_up(&built_uri);
663 return AWS_OP_SUCCESS;
664 }
665
666 AWS_TEST_CASE(uri_builder, s_test_uri_builder);
667
668 static int s_test_uri_builder_from_string(struct aws_allocator *allocator, void *ctx) {
669 (void)ctx;
670 const char *str_uri =
671 "https://www.test.com:8443/path/to/resource?test1=value1&test%20space=value%20space&test2=value2&test2=value3";
672
673 struct aws_byte_cursor uri_csr = aws_byte_cursor_from_c_str(str_uri);
674 struct aws_uri uri;
675 ASSERT_SUCCESS(aws_uri_init_parse(&uri, allocator, &uri_csr));
676
677 struct aws_uri_param params[4];
678 AWS_ZERO_ARRAY(params);
679
680 struct aws_byte_cursor query_string =
681 aws_byte_cursor_from_c_str("test1=value1&test%20space=value%20space&test2=value2&test2=value3");
682
683 struct aws_uri_builder_options builder_args = {
684 .scheme = uri.scheme,
685 .path = uri.path,
686 .host_name = uri.host_name,
687 .port = uri.port,
688 .query_string = query_string,
689 };
690
691 struct aws_uri built_uri;
692 ASSERT_SUCCESS(aws_uri_init_from_builder_options(&built_uri, allocator, &builder_args));
693
694 struct aws_byte_cursor expected_scheme = aws_byte_cursor_from_c_str("https");
695 ASSERT_BIN_ARRAYS_EQUALS(expected_scheme.ptr, expected_scheme.len, built_uri.scheme.ptr, built_uri.scheme.len);
696
697 struct aws_byte_cursor expected_authority = aws_byte_cursor_from_c_str("www.test.com:8443");
698 ASSERT_BIN_ARRAYS_EQUALS(
699 expected_authority.ptr, expected_authority.len, built_uri.authority.ptr, built_uri.authority.len);
700
701 struct aws_byte_cursor expected_host = aws_byte_cursor_from_c_str("www.test.com");
702 ASSERT_BIN_ARRAYS_EQUALS(expected_host.ptr, expected_host.len, built_uri.host_name.ptr, built_uri.host_name.len);
703
704 ASSERT_UINT_EQUALS(8443, built_uri.port);
705
706 struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str("/path/to/resource");
707 ASSERT_BIN_ARRAYS_EQUALS(expected_path.ptr, expected_path.len, built_uri.path.ptr, built_uri.path.len);
708
709 ASSERT_BIN_ARRAYS_EQUALS(
710 query_string.ptr, query_string.len, built_uri.query_string.ptr, built_uri.query_string.len);
711
712 struct aws_byte_cursor expected_request_uri = aws_byte_cursor_from_c_str(
713 "/path/to/resource?test1=value1&test%20space=value%20space&test2=value2&test2=value3");
714 ASSERT_BIN_ARRAYS_EQUALS(
715 expected_request_uri.ptr, expected_request_uri.len, built_uri.path_and_query.ptr, built_uri.path_and_query.len);
716
717 aws_uri_clean_up(&uri);
718 aws_uri_clean_up(&built_uri);
719 return AWS_OP_SUCCESS;
720 }
721
722 AWS_TEST_CASE(uri_builder_from_string, s_test_uri_builder_from_string);
723
724 static int s_test_uri_encode_path_case(
725 struct aws_allocator *allocator,
726 const char *input,
727 const char *expected_output) {
728 struct aws_byte_buf encoding;
729 ASSERT_SUCCESS(aws_byte_buf_init(&encoding, allocator, 100));
730
731 struct aws_byte_cursor path_cursor = aws_byte_cursor_from_c_str(input);
732 ASSERT_SUCCESS(aws_byte_buf_append_encoding_uri_path(&encoding, &path_cursor));
733
734 struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str(expected_output);
735 ASSERT_BIN_ARRAYS_EQUALS(encoding.buffer, encoding.len, expected_path.ptr, expected_path.len);
736
737 aws_byte_buf_clean_up(&encoding);
738
739 return AWS_OP_SUCCESS;
740 }
741
742 static int s_test_uri_encode_path_rfc3986(struct aws_allocator *allocator, void *ctx) {
743 (void)ctx;
744
745 ASSERT_SUCCESS(s_test_uri_encode_path_case(allocator, "/path/1234/", "/path/1234/"));
746 ASSERT_SUCCESS(s_test_uri_encode_path_case(
747 allocator, "/abcdefghijklmnopqrstuvwxyz/1234567890/", "/abcdefghijklmnopqrstuvwxyz/1234567890/"));
748 ASSERT_SUCCESS(s_test_uri_encode_path_case(
749 allocator, "/ABCDEFGHIJKLMNOPQRSTUVWXYZ/1234567890/", "/ABCDEFGHIJKLMNOPQRSTUVWXYZ/1234567890/"));
750 ASSERT_SUCCESS(s_test_uri_encode_path_case(
751 allocator,
752 "/ABCDEFGHIJKLMNOPQRSTUVWXYZ/_-~./$@&,:;=/",
753 "/ABCDEFGHIJKLMNOPQRSTUVWXYZ/_-~./%24%40%26%2C%3A%3B%3D/"));
754 ASSERT_SUCCESS(s_test_uri_encode_path_case(allocator, "/path/%^#! /", "/path/%25%5E%23%21%20/"));
755 ASSERT_SUCCESS(s_test_uri_encode_path_case(allocator, "/path/ሴ", "/path/%E1%88%B4"));
756 ASSERT_SUCCESS(s_test_uri_encode_path_case(
757 allocator, "/path/\"'()*+<>[\\]`{|}/", "/path/%22%27%28%29%2A%2B%3C%3E%5B%5C%5D%60%7B%7C%7D/"));
758
759 return AWS_OP_SUCCESS;
760 }
761
762 AWS_TEST_CASE(test_uri_encode_path_rfc3986, s_test_uri_encode_path_rfc3986);
763
764 static int s_test_uri_encode_param_case(
765 struct aws_allocator *allocator,
766 const char *input,
767 const char *expected_output) {
768 struct aws_byte_buf encoding;
769 ASSERT_SUCCESS(aws_byte_buf_init(&encoding, allocator, 10));
770
771 struct aws_byte_cursor path_cursor = aws_byte_cursor_from_c_str(input);
772 ASSERT_SUCCESS(aws_byte_buf_append_encoding_uri_param(&encoding, &path_cursor));
773
774 struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str(expected_output);
775 ASSERT_BIN_ARRAYS_EQUALS(encoding.buffer, encoding.len, expected_path.ptr, expected_path.len);
776
777 aws_byte_buf_clean_up(&encoding);
778
779 return AWS_OP_SUCCESS;
780 }
781
782 static int s_test_uri_encode_query(struct aws_allocator *allocator, void *ctx) {
783 (void)ctx;
784 (void)allocator;
785
786 ASSERT_SUCCESS(s_test_uri_encode_param_case(
787 allocator,
788 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
789 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
790 ASSERT_SUCCESS(s_test_uri_encode_param_case(allocator, "1234567890", "1234567890"));
791 ASSERT_SUCCESS(s_test_uri_encode_param_case(allocator, "_~.-", "_~.-"));
792 ASSERT_SUCCESS(s_test_uri_encode_param_case(allocator, "%^#! ", "%25%5E%23%21%20"));
793 ASSERT_SUCCESS(s_test_uri_encode_param_case(allocator, "/$@&,:;=", "%2F%24%40%26%2C%3A%3B%3D"));
794 ASSERT_SUCCESS(s_test_uri_encode_param_case(allocator, "ሴ", "%E1%88%B4"));
795 ASSERT_SUCCESS(
796 s_test_uri_encode_param_case(allocator, "\"'()*+<>[\\]`{|}", "%22%27%28%29%2A%2B%3C%3E%5B%5C%5D%60%7B%7C%7D"));
797
798 return AWS_OP_SUCCESS;
799 }
800
801 AWS_TEST_CASE(test_uri_encode_query, s_test_uri_encode_query);
802
803 static int s_test_uri_decode_ok(struct aws_allocator *allocator, const char *input, const char *expected_output) {
804 struct aws_byte_buf decoding;
805 ASSERT_SUCCESS(aws_byte_buf_init(&decoding, allocator, 10));
806
807 struct aws_byte_cursor input_cursor = aws_byte_cursor_from_c_str(input);
808 ASSERT_SUCCESS(aws_byte_buf_append_decoding_uri(&decoding, &input_cursor));
809
810 ASSERT_BIN_ARRAYS_EQUALS(expected_output, strlen(expected_output), decoding.buffer, decoding.len);
811
812 aws_byte_buf_clean_up(&decoding);
813 return AWS_OP_SUCCESS;
814 }
815
816 static int s_test_uri_decode_err(struct aws_allocator *allocator, const char *input) {
817 struct aws_byte_buf decoding;
818 ASSERT_SUCCESS(aws_byte_buf_init(&decoding, allocator, 10));
819
820 struct aws_byte_cursor input_cursor = aws_byte_cursor_from_c_str(input);
821 ASSERT_ERROR(AWS_ERROR_MALFORMED_INPUT_STRING, aws_byte_buf_append_decoding_uri(&decoding, &input_cursor));
822
823 aws_byte_buf_clean_up(&decoding);
824 return AWS_OP_SUCCESS;
825 }
826
827 static int s_test_uri_roundtrip(struct aws_allocator *allocator, const char *input) {
828 struct aws_byte_cursor input_cursor = aws_byte_cursor_from_c_str(input);
829
830 struct aws_byte_buf encoding;
831 ASSERT_SUCCESS(aws_byte_buf_init(&encoding, allocator, 10));
832
833 struct aws_byte_buf decoding;
834 ASSERT_SUCCESS(aws_byte_buf_init(&decoding, allocator, 10));
835
836 /* test param roundtrip encode/decode */
837 ASSERT_SUCCESS(aws_byte_buf_append_encoding_uri_param(&encoding, &input_cursor));
838
839 struct aws_byte_cursor encoding_cursor = aws_byte_cursor_from_buf(&encoding);
840 ASSERT_SUCCESS(aws_byte_buf_append_decoding_uri(&decoding, &encoding_cursor));
841
842 ASSERT_BIN_ARRAYS_EQUALS(input, strlen(input), decoding.buffer, decoding.len);
843
844 /* test path roundtrip encode/decode */
845 aws_byte_buf_reset(&encoding, false);
846 aws_byte_buf_reset(&decoding, false);
847
848 ASSERT_SUCCESS(aws_byte_buf_append_encoding_uri_path(&encoding, &input_cursor));
849
850 encoding_cursor = aws_byte_cursor_from_buf(&encoding);
851 ASSERT_SUCCESS(aws_byte_buf_append_decoding_uri(&decoding, &encoding_cursor));
852
853 ASSERT_BIN_ARRAYS_EQUALS(input, strlen(input), decoding.buffer, decoding.len);
854
855 aws_byte_buf_clean_up(&encoding);
856 aws_byte_buf_clean_up(&decoding);
857 return AWS_OP_SUCCESS;
858 }
859
860 static int s_test_uri_decode(struct aws_allocator *allocator, void *ctx) {
861 (void)ctx;
862
863 /* test against expected */
864 ASSERT_SUCCESS(s_test_uri_decode_ok(allocator, "", ""));
865 ASSERT_SUCCESS(s_test_uri_decode_ok(allocator, "abc123", "abc123"));
866 ASSERT_SUCCESS(s_test_uri_decode_ok(allocator, "%20", " "));
867 ASSERT_SUCCESS(s_test_uri_decode_ok(allocator, "%E1%88%B4", "ሴ"));
868 ASSERT_SUCCESS(s_test_uri_decode_ok(allocator, "%e1%88%b4", "ሴ"));
869 ASSERT_SUCCESS(s_test_uri_decode_ok(allocator, "%2520", "%20"));
870 ASSERT_SUCCESS(s_test_uri_decode_ok(allocator, "ሴ", "ሴ")); /* odd input should just pass through */
871 ASSERT_SUCCESS(s_test_uri_decode_ok(
872 allocator,
873 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", /* long enough to resize output buffer */
874 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
875
876 /* these should fail */
877 ASSERT_SUCCESS(s_test_uri_decode_err(allocator, "%"));
878 ASSERT_SUCCESS(s_test_uri_decode_err(allocator, "%2"));
879 ASSERT_SUCCESS(s_test_uri_decode_err(allocator, "%%20"));
880 ASSERT_SUCCESS(s_test_uri_decode_err(allocator, "%fg"));
881 ASSERT_SUCCESS(s_test_uri_decode_err(allocator, "%gf"));
882
883 /* Test roundtrip encoding and decoding. Results should match original input */
884 ASSERT_SUCCESS(s_test_uri_roundtrip(allocator, ""));
885 ASSERT_SUCCESS(s_test_uri_roundtrip(allocator, "abc123"));
886 ASSERT_SUCCESS(s_test_uri_roundtrip(allocator, "a + b"));
887 ASSERT_SUCCESS(s_test_uri_roundtrip(allocator, "ሴ"));
888
889 /* do roundtrip test against every possible value (except 0 because helper functions use c-strings) */
890 uint8_t every_value[256];
891 for (size_t i = 0; i < 255; ++i) {
892 every_value[i] = (uint8_t)(i + 1);
893 }
894 every_value[255] = 0;
895 ASSERT_SUCCESS(s_test_uri_roundtrip(allocator, (const char *)every_value));
896
897 return AWS_OP_SUCCESS;
898 }
899 AWS_TEST_CASE(test_uri_decode, s_test_uri_decode);