Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/tests/urlencode_ut/urlencode_ut.c

raw
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 #ifdef __cplusplus
5 #include <cstdlib>
6 #include <cstdio>
7 #include <cstddef>
8 #else
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <stddef.h>
12 #endif
13
14 void* real_malloc(size_t size)
15 {
16 return malloc(size);
17 }
18
19 void* real_realloc(void* ptr, size_t size)
20 {
21 return realloc(ptr, size);
22 }
23
24 void real_free(void* ptr)
25 {
26 free(ptr);
27 }
28
29 #include "azure_macro_utils/macro_utils.h"
30 #include "testrunnerswitcher.h"
31 #include "umock_c/umock_c.h"
32
33 #define ENABLE_MOCKS
34 #include "azure_c_shared_utility/gballoc.h"
35 #undef ENABLE_MOCKS
36
37 #include "azure_c_shared_utility/urlencode.h"
38 #include "azure_c_shared_utility/strings.h"
39
40 static const struct
41 {
42 const char* inputData;
43 const char* expectedOutput;
44 } testVector[] =
45 {
46 { "\x01", "%01" },
47 { "\x02", "%02" },
48 { "\x03", "%03" },
49 { "\x04", "%04" },
50 { "\x05", "%05" },
51 { "\x06", "%06" },
52 { "\x07", "%07" },
53 { "\x08", "%08" },
54 { "\x09", "%09" },
55 { "\x0a", "%0a" },
56 { "\x0b", "%0b" },
57 { "\x0c", "%0c" },
58 { "\x0d", "%0d" },
59 { "\x0e", "%0e" },
60 { "\x0f", "%0f" },
61 { "\x10", "%10" },
62 { "\x11", "%11" },
63 { "\x12", "%12" },
64 { "\x13", "%13" },
65 { "\x14", "%14" },
66 { "\x15", "%15" },
67 { "\x16", "%16" },
68 { "\x17", "%17" },
69 { "\x18", "%18" },
70 { "\x19", "%19" },
71 { "\x1a", "%1a" },
72 { "\x1b", "%1b" },
73 { "\x1c", "%1c" },
74 { "\x1d", "%1d" },
75 { "\x1e", "%1e" },
76 { "\x1f", "%1f" },
77 { "\x20", "%20" },
78 { "\x21", "!" },
79 { "\x22", "%22" },
80 { "\x23", "%23" },
81 { "\x24", "%24" },
82 { "\x25", "%25" },
83 { "\x26", "%26" },
84 { "\x27", "%27" },
85 { "\x28", "(" },
86 { "\x29", ")" },
87 { "\x2a", "*" },
88 { "\x2b", "%2b" },
89 { "\x2c", "%2c" },
90 { "\x2d", "-" },
91 { "\x2e", "." },
92 { "\x2f", "%2f" },
93 { "\x30", "0" },
94 { "\x31", "1" },
95 { "\x32", "2" },
96 { "\x33", "3" },
97 { "\x34", "4" },
98 { "\x35", "5" },
99 { "\x36", "6" },
100 { "\x37", "7" },
101 { "\x38", "8" },
102 { "\x39", "9" },
103 { "\x3a", "%3a" },
104 { "\x3b", "%3b" },
105 { "\x3c", "%3c" },
106 { "\x3d", "%3d" },
107 { "\x3e", "%3e" },
108 { "\x3f", "%3f" },
109 { "\x40", "%40" },
110 { "\x41", "A" },
111 { "\x42", "B" },
112 { "\x43", "C" },
113 { "\x44", "D" },
114 { "\x45", "E" },
115 { "\x46", "F" },
116 { "\x47", "G" },
117 { "\x48", "H" },
118 { "\x49", "I" },
119 { "\x4a", "J" },
120 { "\x4b", "K" },
121 { "\x4c", "L" },
122 { "\x4d", "M" },
123 { "\x4e", "N" },
124 { "\x4f", "O" },
125 { "\x50", "P" },
126 { "\x51", "Q" },
127 { "\x52", "R" },
128 { "\x53", "S" },
129 { "\x54", "T" },
130 { "\x55", "U" },
131 { "\x56", "V" },
132 { "\x57", "W" },
133 { "\x58", "X" },
134 { "\x59", "Y" },
135 { "\x5a", "Z" },
136 { "\x5b", "%5b" },
137 { "\x5c", "%5c" },
138 { "\x5d", "%5d" },
139 { "\x5e", "%5e" },
140 { "\x5f", "_" },
141 { "\x60", "%60" },
142 { "\x61", "a" },
143 { "\x62", "b" },
144 { "\x63", "c" },
145 { "\x64", "d" },
146 { "\x65", "e" },
147 { "\x66", "f" },
148 { "\x67", "g" },
149 { "\x68", "h" },
150 { "\x69", "i" },
151 { "\x6a", "j" },
152 { "\x6b", "k" },
153 { "\x6c", "l" },
154 { "\x6d", "m" },
155 { "\x6e", "n" },
156 { "\x6f", "o" },
157 { "\x70", "p" },
158 { "\x71", "q" },
159 { "\x72", "r" },
160 { "\x73", "s" },
161 { "\x74", "t" },
162 { "\x75", "u" },
163 { "\x76", "v" },
164 { "\x77", "w" },
165 { "\x78", "x" },
166 { "\x79", "y" },
167 { "\x7a", "z" },
168 { "\x7b", "%7b" },
169 { "\x7c", "%7c" },
170 { "\x7d", "%7d" },
171 { "\x7e", "%7e" },
172 { "\x7f", "%7f" },
173 { "\x80", "%c2%80" },
174 { "\x81", "%c2%81" },
175 { "\x82", "%c2%82" },
176 { "\x83", "%c2%83" },
177 { "\x84", "%c2%84" },
178 { "\x85", "%c2%85" },
179 { "\x86", "%c2%86" },
180 { "\x87", "%c2%87" },
181 { "\x88", "%c2%88" },
182 { "\x89", "%c2%89" },
183 { "\x8a", "%c2%8a" },
184 { "\x8b", "%c2%8b" },
185 { "\x8c", "%c2%8c" },
186 { "\x8d", "%c2%8d" },
187 { "\x8e", "%c2%8e" },
188 { "\x8f", "%c2%8f" },
189 { "\x90", "%c2%90" },
190 { "\x91", "%c2%91" },
191 { "\x92", "%c2%92" },
192 { "\x93", "%c2%93" },
193 { "\x94", "%c2%94" },
194 { "\x95", "%c2%95" },
195 { "\x96", "%c2%96" },
196 { "\x97", "%c2%97" },
197 { "\x98", "%c2%98" },
198 { "\x99", "%c2%99" },
199 { "\x9a", "%c2%9a" },
200 { "\x9b", "%c2%9b" },
201 { "\x9c", "%c2%9c" },
202 { "\x9d", "%c2%9d" },
203 { "\x9e", "%c2%9e" },
204 { "\x9f", "%c2%9f" },
205 { "\xa0", "%c2%a0" },
206 { "\xa1", "%c2%a1" },
207 { "\xa2", "%c2%a2" },
208 { "\xa3", "%c2%a3" },
209 { "\xa4", "%c2%a4" },
210 { "\xa5", "%c2%a5" },
211 { "\xa6", "%c2%a6" },
212 { "\xa7", "%c2%a7" },
213 { "\xa8", "%c2%a8" },
214 { "\xa9", "%c2%a9" },
215 { "\xaa", "%c2%aa" },
216 { "\xab", "%c2%ab" },
217 { "\xac", "%c2%ac" },
218 { "\xad", "%c2%ad" },
219 { "\xae", "%c2%ae" },
220 { "\xaf", "%c2%af" },
221 { "\xb0", "%c2%b0" },
222 { "\xb1", "%c2%b1" },
223 { "\xb2", "%c2%b2" },
224 { "\xb3", "%c2%b3" },
225 { "\xb4", "%c2%b4" },
226 { "\xb5", "%c2%b5" },
227 { "\xb6", "%c2%b6" },
228 { "\xb7", "%c2%b7" },
229 { "\xb8", "%c2%b8" },
230 { "\xb9", "%c2%b9" },
231 { "\xba", "%c2%ba" },
232 { "\xbb", "%c2%bb" },
233 { "\xbc", "%c2%bc" },
234 { "\xbd", "%c2%bd" },
235 { "\xbe", "%c2%be" },
236 { "\xbf", "%c2%bf" },
237 { "\xc0", "%c3%80" },
238 { "\xc1", "%c3%81" },
239 { "\xc2", "%c3%82" },
240 { "\xc3", "%c3%83" },
241 { "\xc4", "%c3%84" },
242 { "\xc5", "%c3%85" },
243 { "\xc6", "%c3%86" },
244 { "\xc7", "%c3%87" },
245 { "\xc8", "%c3%88" },
246 { "\xc9", "%c3%89" },
247 { "\xca", "%c3%8a" },
248 { "\xcb", "%c3%8b" },
249 { "\xcc", "%c3%8c" },
250 { "\xcd", "%c3%8d" },
251 { "\xce", "%c3%8e" },
252 { "\xcf", "%c3%8f" },
253 { "\xd0", "%c3%90" },
254 { "\xd1", "%c3%91" },
255 { "\xd2", "%c3%92" },
256 { "\xd3", "%c3%93" },
257 { "\xd4", "%c3%94" },
258 { "\xd5", "%c3%95" },
259 { "\xd6", "%c3%96" },
260 { "\xd7", "%c3%97" },
261 { "\xd8", "%c3%98" },
262 { "\xd9", "%c3%99" },
263 { "\xda", "%c3%9a" },
264 { "\xdb", "%c3%9b" },
265 { "\xdc", "%c3%9c" },
266 { "\xdd", "%c3%9d" },
267 { "\xde", "%c3%9e" },
268 { "\xdf", "%c3%9f" },
269 { "\xe0", "%c3%a0" },
270 { "\xe1", "%c3%a1" },
271 { "\xe2", "%c3%a2" },
272 { "\xe3", "%c3%a3" },
273 { "\xe4", "%c3%a4" },
274 { "\xe5", "%c3%a5" },
275 { "\xe6", "%c3%a6" },
276 { "\xe7", "%c3%a7" },
277 { "\xe8", "%c3%a8" },
278 { "\xe9", "%c3%a9" },
279 { "\xea", "%c3%aa" },
280 { "\xeb", "%c3%ab" },
281 { "\xec", "%c3%ac" },
282 { "\xed", "%c3%ad" },
283 { "\xee", "%c3%ae" },
284 { "\xef", "%c3%af" },
285 { "\xf0", "%c3%b0" },
286 { "\xf1", "%c3%b1" },
287 { "\xf2", "%c3%b2" },
288 { "\xf3", "%c3%b3" },
289 { "\xf4", "%c3%b4" },
290 { "\xf5", "%c3%b5" },
291 { "\xf6", "%c3%b6" },
292 { "\xf7", "%c3%b7" },
293 { "\xf8", "%c3%b8" },
294 { "\xf9", "%c3%b9" },
295 { "\xfa", "%c3%ba" },
296 { "\xfb", "%c3%bb" },
297 { "\xfc", "%c3%bc" },
298 { "\xfd", "%c3%bd" },
299 { "\xfe", "%c3%be" },
300 { "\xff", "%c3%bf" }
301 };
302
303 static const struct
304 {
305 const char* charRep;
306 const char* endcodedRep;
307 } testVectorASCII[] =
308 {
309 { "\x01", "%01" },
310 { "\x02", "%02" },
311 { "\x03", "%03" },
312 { "\x04", "%04" },
313 { "\x05", "%05" },
314 { "\x06", "%06" },
315 { "\x07", "%07" },
316 { "\x08", "%08" },
317 { "\x09", "%09" },
318 { "\x0a", "%0a" },
319 { "\x0b", "%0b" },
320 { "\x0c", "%0c" },
321 { "\x0d", "%0d" },
322 { "\x0e", "%0e" },
323 { "\x0f", "%0f" },
324 { "\x10", "%10" },
325 { "\x11", "%11" },
326 { "\x12", "%12" },
327 { "\x13", "%13" },
328 { "\x14", "%14" },
329 { "\x15", "%15" },
330 { "\x16", "%16" },
331 { "\x17", "%17" },
332 { "\x18", "%18" },
333 { "\x19", "%19" },
334 { "\x1a", "%1a" },
335 { "\x1b", "%1b" },
336 { "\x1c", "%1c" },
337 { "\x1d", "%1d" },
338 { "\x1e", "%1e" },
339 { "\x1f", "%1f" },
340 { "\x20", "%20" },
341 { "\x21", "!" },
342 { "\x22", "%22" },
343 { "\x23", "%23" },
344 { "\x24", "%24" },
345 { "\x25", "%25" },
346 { "\x26", "%26" },
347 { "\x27", "%27" },
348 { "\x28", "(" },
349 { "\x29", ")" },
350 { "\x2a", "*" },
351 { "\x2b", "%2b" },
352 { "\x2c", "%2c" },
353 { "\x2d", "-" },
354 { "\x2e", "." },
355 { "\x2f", "%2f" },
356 { "\x30", "0" },
357 { "\x31", "1" },
358 { "\x32", "2" },
359 { "\x33", "3" },
360 { "\x34", "4" },
361 { "\x35", "5" },
362 { "\x36", "6" },
363 { "\x37", "7" },
364 { "\x38", "8" },
365 { "\x39", "9" },
366 { "\x3a", "%3a" },
367 { "\x3b", "%3b" },
368 { "\x3c", "%3c" },
369 { "\x3d", "%3d" },
370 { "\x3e", "%3e" },
371 { "\x3f", "%3f" },
372 { "\x40", "%40" },
373 { "\x41", "A" },
374 { "\x42", "B" },
375 { "\x43", "C" },
376 { "\x44", "D" },
377 { "\x45", "E" },
378 { "\x46", "F" },
379 { "\x47", "G" },
380 { "\x48", "H" },
381 { "\x49", "I" },
382 { "\x4a", "J" },
383 { "\x4b", "K" },
384 { "\x4c", "L" },
385 { "\x4d", "M" },
386 { "\x4e", "N" },
387 { "\x4f", "O" },
388 { "\x50", "P" },
389 { "\x51", "Q" },
390 { "\x52", "R" },
391 { "\x53", "S" },
392 { "\x54", "T" },
393 { "\x55", "U" },
394 { "\x56", "V" },
395 { "\x57", "W" },
396 { "\x58", "X" },
397 { "\x59", "Y" },
398 { "\x5a", "Z" },
399 { "\x5b", "%5b" },
400 { "\x5c", "%5c" },
401 { "\x5d", "%5d" },
402 { "\x5e", "%5e" },
403 { "\x5f", "_" },
404 { "\x60", "%60" },
405 { "\x61", "a" },
406 { "\x62", "b" },
407 { "\x63", "c" },
408 { "\x64", "d" },
409 { "\x65", "e" },
410 { "\x66", "f" },
411 { "\x67", "g" },
412 { "\x68", "h" },
413 { "\x69", "i" },
414 { "\x6a", "j" },
415 { "\x6b", "k" },
416 { "\x6c", "l" },
417 { "\x6d", "m" },
418 { "\x6e", "n" },
419 { "\x6f", "o" },
420 { "\x70", "p" },
421 { "\x71", "q" },
422 { "\x72", "r" },
423 { "\x73", "s" },
424 { "\x74", "t" },
425 { "\x75", "u" },
426 { "\x76", "v" },
427 { "\x77", "w" },
428 { "\x78", "x" },
429 { "\x79", "y" },
430 { "\x7a", "z" },
431 { "\x7b", "%7b" },
432 { "\x7c", "%7c" },
433 { "\x7d", "%7d" },
434 { "\x7e", "%7e" },
435 { "\x7f", "%7f" },
436 };
437
438 static const struct
439 {
440 const char* charRep;
441 const char* endcodedRep;
442 } testVectorExtendedASCII[] =
443 {
444 { "\x80", "%c2%80" },
445 { "\x81", "%c2%81" },
446 { "\x82", "%c2%82" },
447 { "\x83", "%c2%83" },
448 { "\x84", "%c2%84" },
449 { "\x85", "%c2%85" },
450 { "\x86", "%c2%86" },
451 { "\x87", "%c2%87" },
452 { "\x88", "%c2%88" },
453 { "\x89", "%c2%89" },
454 { "\x8a", "%c2%8a" },
455 { "\x8b", "%c2%8b" },
456 { "\x8c", "%c2%8c" },
457 { "\x8d", "%c2%8d" },
458 { "\x8e", "%c2%8e" },
459 { "\x8f", "%c2%8f" },
460 { "\x90", "%c2%90" },
461 { "\x91", "%c2%91" },
462 { "\x92", "%c2%92" },
463 { "\x93", "%c2%93" },
464 { "\x94", "%c2%94" },
465 { "\x95", "%c2%95" },
466 { "\x96", "%c2%96" },
467 { "\x97", "%c2%97" },
468 { "\x98", "%c2%98" },
469 { "\x99", "%c2%99" },
470 { "\x9a", "%c2%9a" },
471 { "\x9b", "%c2%9b" },
472 { "\x9c", "%c2%9c" },
473 { "\x9d", "%c2%9d" },
474 { "\x9e", "%c2%9e" },
475 { "\x9f", "%c2%9f" },
476 { "\xa0", "%c2%a0" },
477 { "\xa1", "%c2%a1" },
478 { "\xa2", "%c2%a2" },
479 { "\xa3", "%c2%a3" },
480 { "\xa4", "%c2%a4" },
481 { "\xa5", "%c2%a5" },
482 { "\xa6", "%c2%a6" },
483 { "\xa7", "%c2%a7" },
484 { "\xa8", "%c2%a8" },
485 { "\xa9", "%c2%a9" },
486 { "\xaa", "%c2%aa" },
487 { "\xab", "%c2%ab" },
488 { "\xac", "%c2%ac" },
489 { "\xad", "%c2%ad" },
490 { "\xae", "%c2%ae" },
491 { "\xaf", "%c2%af" },
492 { "\xb0", "%c2%b0" },
493 { "\xb1", "%c2%b1" },
494 { "\xb2", "%c2%b2" },
495 { "\xb3", "%c2%b3" },
496 { "\xb4", "%c2%b4" },
497 { "\xb5", "%c2%b5" },
498 { "\xb6", "%c2%b6" },
499 { "\xb7", "%c2%b7" },
500 { "\xb8", "%c2%b8" },
501 { "\xb9", "%c2%b9" },
502 { "\xba", "%c2%ba" },
503 { "\xbb", "%c2%bb" },
504 { "\xbc", "%c2%bc" },
505 { "\xbd", "%c2%bd" },
506 { "\xbe", "%c2%be" },
507 { "\xbf", "%c2%bf" },
508 { "\xc0", "%c3%80" },
509 { "\xc1", "%c3%81" },
510 { "\xc2", "%c3%82" },
511 { "\xc3", "%c3%83" },
512 { "\xc4", "%c3%84" },
513 { "\xc5", "%c3%85" },
514 { "\xc6", "%c3%86" },
515 { "\xc7", "%c3%87" },
516 { "\xc8", "%c3%88" },
517 { "\xc9", "%c3%89" },
518 { "\xca", "%c3%8a" },
519 { "\xcb", "%c3%8b" },
520 { "\xcc", "%c3%8c" },
521 { "\xcd", "%c3%8d" },
522 { "\xce", "%c3%8e" },
523 { "\xcf", "%c3%8f" },
524 { "\xd0", "%c3%90" },
525 { "\xd1", "%c3%91" },
526 { "\xd2", "%c3%92" },
527 { "\xd3", "%c3%93" },
528 { "\xd4", "%c3%94" },
529 { "\xd5", "%c3%95" },
530 { "\xd6", "%c3%96" },
531 { "\xd7", "%c3%97" },
532 { "\xd8", "%c3%98" },
533 { "\xd9", "%c3%99" },
534 { "\xda", "%c3%9a" },
535 { "\xdb", "%c3%9b" },
536 { "\xdc", "%c3%9c" },
537 { "\xdd", "%c3%9d" },
538 { "\xde", "%c3%9e" },
539 { "\xdf", "%c3%9f" },
540 { "\xe0", "%c3%a0" },
541 { "\xe1", "%c3%a1" },
542 { "\xe2", "%c3%a2" },
543 { "\xe3", "%c3%a3" },
544 { "\xe4", "%c3%a4" },
545 { "\xe5", "%c3%a5" },
546 { "\xe6", "%c3%a6" },
547 { "\xe7", "%c3%a7" },
548 { "\xe8", "%c3%a8" },
549 { "\xe9", "%c3%a9" },
550 { "\xea", "%c3%aa" },
551 { "\xeb", "%c3%ab" },
552 { "\xec", "%c3%ac" },
553 { "\xed", "%c3%ad" },
554 { "\xee", "%c3%ae" },
555 { "\xef", "%c3%af" },
556 { "\xf0", "%c3%b0" },
557 { "\xf1", "%c3%b1" },
558 { "\xf2", "%c3%b2" },
559 { "\xf3", "%c3%b3" },
560 { "\xf4", "%c3%b4" },
561 { "\xf5", "%c3%b5" },
562 { "\xf6", "%c3%b6" },
563 { "\xf7", "%c3%b7" },
564 { "\xf8", "%c3%b8" },
565 { "\xf9", "%c3%b9" },
566 { "\xfa", "%c3%ba" },
567 { "\xfb", "%c3%bb" },
568 { "\xfc", "%c3%bc" },
569 { "\xfd", "%c3%bd" },
570 { "\xfe", "%c3%be" },
571 { "\xff", "%c3%bf" }
572 };
573
574 const char* UNRESERVED_CHAR = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._";
575
576 MU_DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES)
577
578 static void on_umock_c_error(UMOCK_C_ERROR_CODE error_code)
579 {
580 ASSERT_FAIL("umock_c reported error :%" PRI_MU_ENUM "", MU_ENUM_VALUE(UMOCK_C_ERROR_CODE, error_code));
581 }
582
583 BEGIN_TEST_SUITE(URLEncode_UnitTests)
584
585 TEST_SUITE_INITIALIZE(TestClassInitialize)
586 {
587 umock_c_init(on_umock_c_error);
588
589 REGISTER_GLOBAL_MOCK_HOOK(gballoc_malloc, real_malloc);
590 REGISTER_GLOBAL_MOCK_HOOK(gballoc_free, real_free);
591 REGISTER_GLOBAL_MOCK_HOOK(gballoc_realloc, real_realloc);
592 }
593
594 TEST_SUITE_CLEANUP(TestClassCleanup)
595 {
596 umock_c_deinit();
597 }
598
599 /* Encode Tests */
600 TEST_FUNCTION(URL_EncodeString_is_null_should_yield_NULL)
601 {
602 // arrange
603 // act
604 STRING_HANDLE encodedURL = URL_EncodeString(NULL);
605
606 //assert
607 ASSERT_IS_NULL(encodedURL);
608 }
609
610 /*Tests_SRS_URL_ENCODE_06_003: [If input is a zero length string then URL_Encode will return a zero length string.]*/
611 TEST_FUNCTION(URL_EncodeString_new_should_yield_zeroLengthString)
612 {
613 // arrange
614 const char* newString = "";
615
616 // act
617 STRING_HANDLE encodedURL = URL_EncodeString(newString);
618
619 //assert
620 ASSERT_IS_NOT_NULL(encodedURL);
621 ASSERT_ARE_EQUAL(size_t, strlen(STRING_c_str(encodedURL)),0);
622 STRING_delete(encodedURL);
623 }
624
625 TEST_FUNCTION(URL_EncodeString_is_hello_world)
626 {
627 // arrange
628 const char* hello = "hello world";
629
630 // act
631 STRING_HANDLE encodedURL = URL_EncodeString(hello);
632
633 //assert
634 ASSERT_IS_NOT_NULL(encodedURL);
635 ASSERT_ARE_EQUAL(char_ptr,"hello%20world", STRING_c_str(encodedURL) );
636 STRING_delete(encodedURL);
637 }
638
639 TEST_FUNCTION(URL_EncodeString_unreserved_mapping)
640 {
641 // arrange
642
643 // act
644 STRING_HANDLE encodedUnreservedURL = URL_EncodeString(UNRESERVED_CHAR);
645
646 //assert
647 ASSERT_IS_NOT_NULL(encodedUnreservedURL);
648 ASSERT_ARE_EQUAL(char_ptr, UNRESERVED_CHAR, STRING_c_str(encodedUnreservedURL) );
649 STRING_delete(encodedUnreservedURL);
650 }
651
652 TEST_FUNCTION(URL_EncodeString_path_with_device)
653 {
654 // arrange
655 const char* pathWithDevice = "/getalarm('Le Pichet')";
656
657 // act
658 STRING_HANDLE encodedPathWithDevice = URL_EncodeString(pathWithDevice);
659
660 //assert
661 ASSERT_IS_NOT_NULL(encodedPathWithDevice);
662 ASSERT_ARE_EQUAL(char_ptr, "%2fgetalarm(%27Le%20Pichet%27)", STRING_c_str(encodedPathWithDevice));
663 STRING_delete(encodedPathWithDevice);
664 }
665
666 TEST_FUNCTION(URL_EncodeString_a_few_bogus_characters)
667 {
668 // arrange
669 const char* bogusCharacters = "{}%";
670
671 // act
672 STRING_HANDLE encodedBogusCharacters = URL_EncodeString(bogusCharacters);
673
674 //assert
675 ASSERT_IS_NOT_NULL(encodedBogusCharacters);
676 ASSERT_ARE_EQUAL(char_ptr, "%7b%7d%25", STRING_c_str(encodedBogusCharacters));
677 STRING_delete(encodedBogusCharacters);
678 }
679
680 TEST_FUNCTION(URL_EncodeString_full_url)
681 {
682 // arrange
683 const char* fullUrl = "https://one.two.three.four-five.com/six/Seven('EightNine1234567890.Ten_Eleven')?twelve-thirteen=2015-11-31 HTTP/1.1";
684
685 // act
686 STRING_HANDLE encodedFullUrl = URL_EncodeString(fullUrl);
687
688 //ASSERT
689 ASSERT_IS_NOT_NULL(encodedFullUrl);
690 ASSERT_ARE_EQUAL(char_ptr, "https%3a%2f%2fone.two.three.four-five.com%2fsix%2fSeven(%27EightNine1234567890.Ten_Eleven%27)%3ftwelve-thirteen%3d2015-11-31%20HTTP%2f1.1", STRING_c_str(encodedFullUrl));
691 STRING_delete(encodedFullUrl);
692 }
693
694 TEST_FUNCTION(URL_EncodeString_Exhaustive_chars)
695 {
696 size_t i;
697 size_t numberOfTests = sizeof(testVector) / sizeof(testVector[i]);
698 for (i = 0; i < numberOfTests; i++)
699 {
700 //arrange
701
702 const char* original = testVector[i].inputData;
703
704 //act
705 STRING_HANDLE encodedOriginal = URL_EncodeString(original);
706
707 //assert
708 ASSERT_IS_NOT_NULL(encodedOriginal);
709 ASSERT_ARE_EQUAL(char_ptr, testVector[i].expectedOutput, STRING_c_str(encodedOriginal));
710 STRING_delete(encodedOriginal);
711 }
712 }
713
714 /*Tests_SRS_URL_ENCODE_06_001: [If input is NULL then URL_Encode will return NULL.]*/
715 TEST_FUNCTION(URL_is_null_should_yield_NULL)
716 {
717 // arrange
718 // act
719 STRING_HANDLE encodedURL = URL_Encode(NULL);
720
721 //assert
722 ASSERT_IS_NULL(encodedURL);
723 }
724 /*Tests_SRS_URL_ENCODE_06_003: [If input is a zero length string then URL_Encode will return a zero length string.]*/
725 TEST_FUNCTION(URL_new_should_yield_zeroLengthString)
726 {
727 // arrange
728 STRING_HANDLE newString = STRING_new();
729 // act
730 STRING_HANDLE encodedURL = URL_Encode(newString);
731
732 //assert
733 ASSERT_IS_NOT_NULL(encodedURL);
734 ASSERT_ARE_EQUAL(size_t, strlen(STRING_c_str(encodedURL)),0);
735 STRING_delete(newString);
736 STRING_delete(encodedURL);
737 }
738
739 TEST_FUNCTION(URL_is_hello_world)
740 {
741 // arrange
742 STRING_HANDLE encodedURL;
743 STRING_HANDLE hello = STRING_new();
744
745 ASSERT_ARE_EQUAL(int, STRING_concat(hello, "hello world"),0);
746
747 // act
748 encodedURL = URL_Encode(hello);
749
750
751 //assert
752 ASSERT_IS_NOT_NULL(encodedURL);
753 ASSERT_ARE_EQUAL(char_ptr,"hello%20world",STRING_c_str(encodedURL));
754 STRING_delete(hello);
755 STRING_delete(encodedURL);
756 }
757
758 TEST_FUNCTION(URL_unreserved_mapping)
759 {
760 // arrange
761 STRING_HANDLE encodedUnreservedURL;
762 STRING_HANDLE unreserved = STRING_new();
763
764 ASSERT_ARE_EQUAL(int, STRING_concat(unreserved, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._"), 0);
765
766 // act
767 encodedUnreservedURL = URL_Encode(unreserved);
768
769
770 //assert
771 ASSERT_IS_NOT_NULL(encodedUnreservedURL);
772 ASSERT_ARE_EQUAL(char_ptr, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._", STRING_c_str(encodedUnreservedURL));
773 STRING_delete(unreserved);
774 STRING_delete(encodedUnreservedURL);
775 }
776
777 TEST_FUNCTION(URL_path_with_device)
778 {
779 // arrange
780 STRING_HANDLE encodedPathWithDevice;
781 STRING_HANDLE pathWithDevice = STRING_new();
782
783 ASSERT_ARE_EQUAL(int, STRING_concat(pathWithDevice, "/getalarm('Le Pichet')"), 0);
784
785 // act
786 encodedPathWithDevice = URL_Encode(pathWithDevice);
787
788
789 //assert
790 ASSERT_IS_NOT_NULL(encodedPathWithDevice);
791 ASSERT_ARE_EQUAL(char_ptr, "%2fgetalarm(%27Le%20Pichet%27)", STRING_c_str(encodedPathWithDevice));
792 STRING_delete(pathWithDevice);
793 STRING_delete(encodedPathWithDevice);
794 }
795
796 TEST_FUNCTION(URL_a_few_bogus_characters)
797 {
798 // arrange
799 STRING_HANDLE encodedBogusCharacters;
800 STRING_HANDLE bogusCharacters = STRING_new();
801
802 ASSERT_ARE_EQUAL(int, STRING_concat(bogusCharacters, "{}%"), 0);
803
804 // act
805 encodedBogusCharacters = URL_Encode(bogusCharacters);
806
807
808 //assert
809 ASSERT_IS_NOT_NULL(encodedBogusCharacters);
810 ASSERT_ARE_EQUAL(char_ptr, "%7b%7d%25", STRING_c_str(encodedBogusCharacters));
811 STRING_delete(bogusCharacters);
812 STRING_delete(encodedBogusCharacters);
813 }
814
815 TEST_FUNCTION(URL_full_IOT_url)
816 {
817 // arrange
818 STRING_HANDLE encodedFullUrl;
819 STRING_HANDLE fullUrl = STRING_new();
820
821 ASSERT_ARE_EQUAL(int, STRING_concat(fullUrl, "https://one.two.three.four-five.com/six/Seven('EightNine1234567890.Ten_Eleven')?twelve-thirteen=2015-11-31 HTTP/1.1"), 0);
822
823 // act
824 encodedFullUrl = URL_Encode(fullUrl);
825
826 //ASSERT
827 ASSERT_IS_NOT_NULL(encodedFullUrl);
828 ASSERT_ARE_EQUAL(char_ptr, "https%3a%2f%2fone.two.three.four-five.com%2fsix%2fSeven(%27EightNine1234567890.Ten_Eleven%27)%3ftwelve-thirteen%3d2015-11-31%20HTTP%2f1.1", STRING_c_str(encodedFullUrl));
829 STRING_delete(fullUrl);
830 STRING_delete(encodedFullUrl);
831 }
832
833 TEST_FUNCTION(URL_Exhaustive_chars)
834 {
835 size_t i;
836 size_t numberOfTests = sizeof(testVector) / sizeof(testVector[i]);
837 for (i = 0; i < numberOfTests; i++)
838 {
839 //arrange
840 STRING_HANDLE encodedOriginal;
841 STRING_HANDLE original = STRING_new();
842 ASSERT_ARE_EQUAL(int, STRING_concat(original, testVector[i].inputData), 0);
843
844 //act
845 encodedOriginal = URL_Encode(original);
846
847 //assert
848 ASSERT_IS_NOT_NULL(encodedOriginal);
849 ASSERT_ARE_EQUAL(char_ptr, testVector[i].expectedOutput, STRING_c_str(encodedOriginal));
850 STRING_delete(original);
851 STRING_delete(encodedOriginal);
852 }
853 }
854
855 /* Decode Tests */
856 TEST_FUNCTION(URL_DecodeString_null_input)
857 {
858 //arrange
859
860 //act
861 STRING_HANDLE decodedURL = URL_DecodeString(NULL);
862
863 //assert
864 ASSERT_IS_NULL(decodedURL);
865
866 //cleanup
867 }
868
869 TEST_FUNCTION(URL_DecodeString_zerolength_input)
870 {
871 //arrange
872 const char* newString = "";
873
874 //act
875 STRING_HANDLE decodedURL = URL_DecodeString(newString);
876
877 //assert
878 ASSERT_IS_NOT_NULL(decodedURL);
879 ASSERT_ARE_EQUAL(size_t, 0, strlen(STRING_c_str(decodedURL)));
880
881 //cleanup
882 STRING_delete(decodedURL);
883 }
884
885 TEST_FUNCTION(URL_DecodeString_unencoded_input)
886 {
887 //arrange
888 const char* newString = "hello world"; //space is not encoded
889
890 //act
891 STRING_HANDLE decodedURL = URL_DecodeString(newString);
892
893 //assert
894 ASSERT_IS_NULL(decodedURL);
895
896 //cleanup
897 }
898
899 TEST_FUNCTION(URL_DecodeString_partially_unencoded_input)
900 {
901 //arrange
902 const char* newString = "hello%20world&mistake"; //& is an unencoded char
903
904 //act
905 STRING_HANDLE decodedURL = URL_DecodeString(newString);
906
907 //assert
908 ASSERT_IS_NULL(decodedURL);
909
910 //cleanup
911 }
912
913 TEST_FUNCTION(URL_DecodeString_invalid_encoding_incomplete)
914 {
915 //arrange
916 const char* newString = "%7";
917
918 //act
919 STRING_HANDLE decodedURL = URL_DecodeString(newString);
920
921 //assert
922 ASSERT_IS_NULL(decodedURL);
923
924 //cleanup
925 }
926
927 TEST_FUNCTION(URL_DecodeString_invalid_encoding_non_hex)
928 {
929 //arrange
930 const char* newString = "%G5";
931
932 //act
933 STRING_HANDLE decodedURL = URL_DecodeString(newString);
934
935 //assert
936 ASSERT_IS_NULL(decodedURL);
937
938 //cleanup
939 }
940
941 TEST_FUNCTION(URL_DecodeString_invalid_has_multibyte_encoding)
942 {
943 //arrange
944 const char* newString = "%C2%B4";
945
946 //act
947 STRING_HANDLE decodedURL = URL_DecodeString(newString);
948
949 //assert
950 ASSERT_IS_NULL(decodedURL);
951
952 //cleanup
953 }
954
955 TEST_FUNCTION(URL_DecodeString_unreserved_mapping)
956 {
957 //arrange
958
959 //act
960 STRING_HANDLE decodedURL = URL_DecodeString(UNRESERVED_CHAR);
961
962 //assert
963 ASSERT_IS_NOT_NULL(decodedURL);
964 ASSERT_ARE_EQUAL(char_ptr, UNRESERVED_CHAR, STRING_c_str(decodedURL));
965
966 //cleanup
967 STRING_delete(decodedURL);
968 }
969
970 TEST_FUNCTION(URL_DecodeString_path_with_device)
971 {
972 //arrange
973 const char* input = "%2fgetalarm(%27Le%20Pichet%27)";
974
975 //act
976 STRING_HANDLE decodedURL = URL_DecodeString(input);
977
978 //assert
979 ASSERT_IS_NOT_NULL(decodedURL);
980 ASSERT_ARE_EQUAL(char_ptr, "/getalarm('Le Pichet')", STRING_c_str(decodedURL));
981
982 //cleanup
983 STRING_delete(decodedURL);
984 }
985
986 TEST_FUNCTION(URL_DecodeString_a_few_bogus_characters)
987 {
988 //arrange
989 const char* input = "%7b%7d%25";
990
991 //act
992 STRING_HANDLE decodedURL = URL_DecodeString(input);
993
994 //assert
995 ASSERT_IS_NOT_NULL(decodedURL);
996 ASSERT_ARE_EQUAL(char_ptr, "{}%", STRING_c_str(decodedURL));
997
998 //cleanup
999 STRING_delete(decodedURL);
1000 }
1001
1002 TEST_FUNCTION(URL_DecodeString_full_url)
1003 {
1004 //arrange
1005 const char* input = "https%3a%2f%2fone.two.three.four-five.com%2fsix%2fSeven(%27EightNine1234567890.Ten_Eleven%27)%3ftwelve-thirteen%3d2015-11-31%20HTTP%2f1.1";
1006
1007 //act
1008 STRING_HANDLE decodedURL = URL_DecodeString(input);
1009
1010 //assert
1011 ASSERT_IS_NOT_NULL(decodedURL);
1012 ASSERT_ARE_EQUAL(char_ptr, "https://one.two.three.four-five.com/six/Seven('EightNine1234567890.Ten_Eleven')?twelve-thirteen=2015-11-31 HTTP/1.1", STRING_c_str(decodedURL));
1013
1014 //cleanup
1015 STRING_delete(decodedURL);
1016 }
1017
1018 TEST_FUNCTION(URL_DecodeString_ASCII_chars)
1019 {
1020 size_t i;
1021 size_t numberOfTests = sizeof(testVectorASCII) / sizeof(testVectorASCII[i]);
1022
1023 for (i = 0; i < numberOfTests; i++)
1024 {
1025 //arrange
1026 STRING_HANDLE decoded;
1027
1028 //act
1029 decoded = URL_DecodeString(testVectorASCII[i].endcodedRep);
1030
1031 //assert
1032 ASSERT_IS_NOT_NULL(decoded);
1033 ASSERT_ARE_EQUAL(char_ptr, testVectorASCII[i].charRep, STRING_c_str(decoded));
1034 STRING_delete(decoded);
1035 }
1036 }
1037
1038 TEST_FUNCTION(URL_DecodeString_Extended_ASCII_chars)
1039 {
1040 size_t i;
1041 size_t numberOfTests = sizeof(testVectorExtendedASCII) / sizeof(testVectorExtendedASCII[i]);
1042
1043 for (i = 0; i < numberOfTests; i++)
1044 {
1045 //arrange
1046 STRING_HANDLE decoded;
1047
1048 //act
1049 decoded = URL_DecodeString(testVectorExtendedASCII[i].endcodedRep);
1050
1051 //assert
1052 ASSERT_IS_NULL(decoded); //these should be rejected by the function
1053 STRING_delete(decoded);
1054 }
1055 }
1056
1057 TEST_FUNCTION(URL_Decode_null_input)
1058 {
1059 //arrange
1060
1061 //act
1062 STRING_HANDLE decodedURL = URL_Decode(NULL);
1063
1064 //assert
1065 ASSERT_IS_NULL(decodedURL);
1066
1067 //cleanup
1068 }
1069
1070 TEST_FUNCTION(URL_Decode_zerolength_input)
1071 {
1072 //arrange
1073 STRING_HANDLE decodedURL;
1074 STRING_HANDLE newString = STRING_new();
1075 ASSERT_ARE_EQUAL(int, 0, STRING_concat(newString, ""));
1076
1077 //act
1078 decodedURL = URL_Decode(newString);
1079
1080 //assert
1081 ASSERT_IS_NOT_NULL(decodedURL);
1082 ASSERT_ARE_EQUAL(size_t, 0, strlen(STRING_c_str(decodedURL)));
1083
1084 //cleanup
1085 STRING_delete(decodedURL);
1086 STRING_delete(newString);
1087 }
1088
1089 TEST_FUNCTION(URL_Decode_unencoded_input)
1090 {
1091 //arrange
1092 STRING_HANDLE decodedURL;
1093 STRING_HANDLE newString = STRING_new();
1094 ASSERT_ARE_EQUAL(int, 0, STRING_concat(newString, "hello world")); //space is not encoded
1095
1096 //act
1097 decodedURL = URL_Decode(newString);
1098
1099 //assert
1100 ASSERT_IS_NULL(decodedURL);
1101
1102 //cleanup
1103 STRING_delete(newString);
1104 }
1105
1106 TEST_FUNCTION(URL_Decode_partially_unencoded_input)
1107 {
1108 //arrange
1109 STRING_HANDLE decodedURL;
1110 STRING_HANDLE newString = STRING_new();
1111 ASSERT_ARE_EQUAL(int, 0, STRING_concat(newString, "hello%20world&mistake")); //& is an unencoded char
1112
1113 //act
1114 decodedURL = URL_Decode(newString);
1115
1116 //assert
1117 ASSERT_IS_NULL(decodedURL);
1118
1119 //cleanup
1120 STRING_delete(newString);
1121 }
1122
1123 TEST_FUNCTION(URL_Decode_invalid_encoding_incomplete)
1124 {
1125 //arrange
1126 STRING_HANDLE decodedURL;
1127 STRING_HANDLE newString = STRING_new();
1128 ASSERT_ARE_EQUAL(int, 0, STRING_concat(newString, "%7"));
1129
1130 //act
1131 decodedURL = URL_Decode(newString);
1132
1133 //assert
1134 ASSERT_IS_NULL(decodedURL);
1135
1136 //cleanup
1137 STRING_delete(newString);
1138 }
1139
1140 TEST_FUNCTION(URL_Decode_invalid_encoding_non_hex)
1141 {
1142 //arrange
1143 STRING_HANDLE decodedURL;
1144 STRING_HANDLE newString = STRING_new();
1145 ASSERT_ARE_EQUAL(int, 0, STRING_concat(newString, "%G5"));
1146
1147 //act
1148 decodedURL = URL_Decode(newString);
1149
1150 //assert
1151 ASSERT_IS_NULL(decodedURL);
1152
1153 //cleanup
1154 STRING_delete(newString);
1155 }
1156
1157 TEST_FUNCTION(URL_Decode_invalid_has_multibyte_encoding)
1158 {
1159 //arrange
1160 STRING_HANDLE decodedURL;
1161 STRING_HANDLE newString = STRING_new();
1162 ASSERT_ARE_EQUAL(int, 0, STRING_concat(newString, "%C2%B4"));
1163
1164 //act
1165 decodedURL = URL_Decode(newString);
1166
1167 //assert
1168 ASSERT_IS_NULL(decodedURL);
1169
1170 //cleanup
1171 STRING_delete(newString);
1172 }
1173
1174 TEST_FUNCTION(URL_Decode_unreserved_mapping)
1175 {
1176 //arrange
1177 STRING_HANDLE decodedURL;
1178 STRING_HANDLE newString = STRING_new();
1179 ASSERT_ARE_EQUAL(int, 0, STRING_concat(newString, UNRESERVED_CHAR));
1180
1181 //act
1182 decodedURL = URL_Decode(newString);
1183
1184 //assert
1185 ASSERT_IS_NOT_NULL(decodedURL);
1186 ASSERT_ARE_EQUAL(char_ptr, UNRESERVED_CHAR, STRING_c_str(decodedURL));
1187
1188 //cleanup
1189 STRING_delete(decodedURL);
1190 STRING_delete(newString);
1191 }
1192
1193 TEST_FUNCTION(URL_Decode_path_with_device)
1194 {
1195 //arrange
1196 STRING_HANDLE decodedURL;
1197 STRING_HANDLE input = STRING_new();
1198 ASSERT_ARE_EQUAL(int, 0, STRING_concat(input, "%2fgetalarm(%27Le%20Pichet%27)"));
1199
1200 //act
1201 decodedURL = URL_Decode(input);
1202
1203 //assert
1204 ASSERT_IS_NOT_NULL(decodedURL);
1205 ASSERT_ARE_EQUAL(char_ptr, "/getalarm('Le Pichet')", STRING_c_str(decodedURL));
1206
1207 //cleanup
1208 STRING_delete(decodedURL);
1209 STRING_delete(input);
1210 }
1211
1212 TEST_FUNCTION(URL_Decode_a_few_bogus_characters)
1213 {
1214 //arrange
1215 STRING_HANDLE decodedURL;
1216 STRING_HANDLE input = STRING_new();
1217 ASSERT_ARE_EQUAL(int, 0, STRING_concat(input, "%7b%7d%25"));
1218
1219 //act
1220 decodedURL = URL_Decode(input);
1221
1222 //assert
1223 ASSERT_IS_NOT_NULL(decodedURL);
1224 ASSERT_ARE_EQUAL(char_ptr, "{}%", STRING_c_str(decodedURL));
1225
1226 //cleanup
1227 STRING_delete(decodedURL);
1228 STRING_delete(input);
1229 }
1230
1231 TEST_FUNCTION(URL_Decode_full_url)
1232 {
1233 //arrange
1234 STRING_HANDLE decodedURL;
1235 STRING_HANDLE input = STRING_new();
1236 ASSERT_ARE_EQUAL(int, 0, STRING_concat(input, "https%3a%2f%2fone.two.three.four-five.com%2fsix%2fSeven(%27EightNine1234567890.Ten_Eleven%27)%3ftwelve-thirteen%3d2015-11-31%20HTTP%2f1.1"));
1237
1238 //act
1239 decodedURL = URL_Decode(input);
1240
1241 //assert
1242 ASSERT_IS_NOT_NULL(decodedURL);
1243 ASSERT_ARE_EQUAL(char_ptr, "https://one.two.three.four-five.com/six/Seven('EightNine1234567890.Ten_Eleven')?twelve-thirteen=2015-11-31 HTTP/1.1", STRING_c_str(decodedURL));
1244
1245 //cleanup
1246 STRING_delete(decodedURL);
1247 STRING_delete(input);
1248 }
1249
1250 TEST_FUNCTION(URL_Decode_ASCII_chars)
1251 {
1252 size_t i;
1253 size_t numberOfTests = sizeof(testVectorASCII) / sizeof(testVectorASCII[i]);
1254
1255 for (i = 0; i < numberOfTests; i++)
1256 {
1257 //arrange
1258 STRING_HANDLE decoded;
1259 STRING_HANDLE encoded = STRING_new();
1260 ASSERT_ARE_EQUAL(int, STRING_concat(encoded, testVectorASCII[i].endcodedRep), 0);
1261
1262 //act
1263 decoded = URL_Decode(encoded);
1264
1265 //assert
1266 ASSERT_IS_NOT_NULL(decoded);
1267 ASSERT_ARE_EQUAL(char_ptr, testVectorASCII[i].charRep, STRING_c_str(decoded));
1268 STRING_delete(encoded);
1269 STRING_delete(decoded);
1270 }
1271 }
1272
1273 TEST_FUNCTION(URL_Decode_Extended_ASCII_chars)
1274 {
1275 size_t i;
1276 size_t numberOfTests = sizeof(testVectorExtendedASCII) / sizeof(testVectorExtendedASCII[i]);
1277
1278 for (i = 0; i < numberOfTests; i++)
1279 {
1280 //arrange
1281 STRING_HANDLE decoded;
1282 STRING_HANDLE encoded = STRING_new();
1283 ASSERT_ARE_EQUAL(int, STRING_concat(encoded, testVectorExtendedASCII[i].endcodedRep), 0);
1284
1285 //act
1286 decoded = URL_Decode(encoded);
1287
1288 //assert
1289 ASSERT_IS_NULL(decoded); //these should be rejected by the function
1290
1291 //cleanup
1292 STRING_delete(encoded);
1293 }
1294 }
1295
1296 END_TEST_SUITE(URLEncode_UnitTests)