1 /* This NetX test concentrates on the utility functions. */
2
3 #include "nx_md5.h"
4 #include "tx_api.h"
5 #include "nx_api.h"
6
7 #define DEMO_STACK_SIZE 2048
8
9 /* Define the ThreadX and NetX object control blocks... */
10
11 static TX_THREAD ntest_0;
12
13 /* Define thread prototypes. */
14
15 static void ntest_0_entry(ULONG thread_input);
16 extern void test_control_return(UINT status);
17
18 /* Define what the initial system looks like. */
19
20 #ifdef CTEST
test_application_define(void * first_unused_memory)21 VOID test_application_define(void *first_unused_memory)
22 #else
23 void netx_utility_test_application_define(void *first_unused_memory)
24 #endif
25 {
26
27 CHAR *pointer;
28
29
30 /* Setup the working pointer. */
31 pointer = (CHAR *) first_unused_memory;
32
33 /* Create the main thread. */
34 tx_thread_create(&ntest_0, "thread 0", ntest_0_entry, 0,
35 pointer, DEMO_STACK_SIZE,
36 4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
37 pointer = pointer + DEMO_STACK_SIZE;
38
39 /* Initialize the NetX system. */
40 nx_system_initialize();
41 }
42
43
44 /* Define the test threads. */
45
ntest_0_entry(ULONG thread_input)46 static void ntest_0_entry(ULONG thread_input)
47 {
48
49 UINT status;
50 UINT string_length;
51 UINT number;
52 UCHAR input_buffer[256] = {0};
53 UCHAR out_buffer[256] = {0};
54 UINT bytes_copied;
55 UINT i;
56 UINT size;
57 NX_MD5 context;
58
59 /* Print out test information banner. */
60 printf("NetX Test: Utility Test..............................................");
61
62 /* Null string pointer. */
63 status = _nx_utility_string_length_check(NX_NULL, &string_length, 10);
64
65 /* Check status. */
66 if (status == NX_SUCCESS)
67 {
68
69 printf("ERROR!\n");
70 test_control_return(1);
71 }
72
73 /* The length of string is less than max length. */
74 status = _nx_utility_string_length_check("test string", &string_length, sizeof("test string"));
75
76 /* Check status. */
77 if ((status != NX_SUCCESS) || (string_length != sizeof("test string") - 1))
78 {
79
80 printf("ERROR!\n");
81 test_control_return(1);
82 }
83
84 /* The length of string is equal to max length. */
85 status = _nx_utility_string_length_check("test string", &string_length, sizeof("test string") - 1);
86
87 /* Check status. */
88 if ((status != NX_SUCCESS) || (string_length != sizeof("test string") - 1))
89 {
90
91 printf("ERROR!\n");
92 test_control_return(1);
93 }
94
95 /* The length of string is equal to max length. */
96 status = _nx_utility_string_length_check("test string", &string_length, sizeof("test string") - 2);
97
98 /* Check status. */
99 if (status == NX_SUCCESS)
100 {
101
102 printf("ERROR!\n");
103 test_control_return(1);
104 }
105
106 /* Verify _nx_utility_string_to_uint(). */
107
108 /* Null string pointer. */
109 status = _nx_utility_string_to_uint(NX_NULL, sizeof("4294967295") -1, &number);
110
111 /* Check status. */
112 if (status != NX_PTR_ERROR)
113 {
114
115 printf("ERROR!\n");
116 test_control_return(1);
117 }
118
119 /* Null string pointer. */
120 status = _nx_utility_string_to_uint("4294967295", sizeof("4294967295") -1, NX_NULL);
121
122 /* Check status. */
123 if (status != NX_PTR_ERROR)
124 {
125
126 printf("ERROR!\n");
127 test_control_return(1);
128 }
129
130 /* Invalid string length. */
131 status = _nx_utility_string_to_uint("4294967295", 0, &number);
132
133 /* Check status. */
134 if (status != NX_SIZE_ERROR)
135 {
136
137 printf("ERROR!\n");
138 test_control_return(1);
139 }
140
141 /* Verify string "1234". */
142 status = _nx_utility_string_to_uint("1234", sizeof("1234") -1, &number);
143
144 /* Check status. */
145 if ((status) || (number != 1234))
146 {
147
148 printf("ERROR!\n");
149 test_control_return(1);
150 }
151
152 /* Verify max value number(Hex:FFFFFFFF, Decimal:4294967295). */
153 status = _nx_utility_string_to_uint("4294967295", sizeof("4294967295") -1, &number);
154
155 /* Check status. */
156 if ((status) || (number != 4294967295))
157 {
158
159 printf("ERROR!\n");
160 test_control_return(1);
161 }
162
163 /* Test invalid string "4294967296". */
164 status = _nx_utility_string_to_uint("4294967296", sizeof("4294967296") -1, &number);
165
166 /* Check status. */
167 if (status != NX_OVERFLOW)
168 {
169
170 printf("ERROR!\n");
171 test_control_return(1);
172 }
173
174 /* Test invalid string "4294967300". */
175 status = _nx_utility_string_to_uint("4294967300", sizeof("4294967300") -1, &number);
176
177 /* Check status. */
178 if (status != NX_OVERFLOW)
179 {
180
181 printf("ERROR!\n");
182 test_control_return(1);
183 }
184
185 /* Test invalid string. */
186 status = _nx_utility_string_to_uint("123+", sizeof("123+") -1, &number);
187
188 /* Check status. */
189 if (status != NX_INVALID_PARAMETERS)
190 {
191
192 printf("ERROR!\n");
193 test_control_return(1);
194 }
195
196 /* Test invalid string. */
197 status = _nx_utility_string_to_uint("123A", sizeof("123A") -1, &number);
198
199 /* Check status. */
200 if (status != NX_INVALID_PARAMETERS)
201 {
202
203 printf("ERROR!\n");
204 test_control_return(1);
205 }
206
207 /* Base64 encode test. */
208
209 /* Null name pointer. */
210 status = _nx_utility_base64_encode(NX_NULL, sizeof("name:password") -1, out_buffer, sizeof(out_buffer), &bytes_copied);
211
212 /* Check status. */
213 if (status != NX_PTR_ERROR)
214 {
215
216 printf("ERROR!\n");
217 test_control_return(1);
218 }
219
220 /* 0 name size. */
221 status = _nx_utility_base64_encode("name:password", 0, out_buffer, sizeof(out_buffer), &bytes_copied);
222
223 /* Check status. */
224 if (status != NX_SIZE_ERROR)
225 {
226
227 printf("ERROR!\n");
228 test_control_return(1);
229 }
230
231 /* NULL buffer pointer. */
232 status = _nx_utility_base64_encode("name:password", sizeof("name:password") -1, NX_NULL, sizeof(out_buffer), &bytes_copied);
233
234 /* Check status. */
235 if (status != NX_PTR_ERROR)
236 {
237
238 printf("ERROR!\n");
239 test_control_return(1);
240 }
241
242 /* 0 buffer size. */
243 status = _nx_utility_base64_encode("name:password", sizeof("name:password") -1, out_buffer, 0, &bytes_copied);
244
245 /* Check status. */
246 if (status != NX_SIZE_ERROR)
247 {
248
249 printf("ERROR!\n");
250 test_control_return(1);
251 }
252
253 /* NULL bytes copied pointer. */
254 status = _nx_utility_base64_encode("name:password", sizeof("name:password") -1, out_buffer, sizeof(out_buffer), NX_NULL);
255
256 /* Check status. */
257 if (status != NX_PTR_ERROR)
258 {
259
260 printf("ERROR!\n");
261 test_control_return(1);
262 }
263
264 /* Encode name with small buffer. */
265 status = _nx_utility_base64_encode("name:password", sizeof("name:password") -1, out_buffer, 20, &bytes_copied);
266
267 /* Check status. */
268 if (status != NX_SIZE_ERROR)
269 {
270
271 printf("ERROR!\n");
272 test_control_return(1);
273 }
274
275 /* Encode name successfully. */
276 status = _nx_utility_base64_encode("name:password", sizeof("name:password") -1, out_buffer, sizeof(out_buffer), &bytes_copied);
277
278 /* Check status. */
279 if ((status != NX_SUCCESS) ||
280 (bytes_copied != 20) ||
281 (memcmp(out_buffer, "bmFtZTpwYXNzd29yZA==", 20) != 0))
282 {
283
284 printf("ERROR!\n");
285 test_control_return(1);
286 }
287
288 /* Encode special character. */
289 input_buffer[0] = 0x80;
290 input_buffer[1] = 0;
291 status = _nx_utility_base64_encode(input_buffer, 1, out_buffer, sizeof(out_buffer), &bytes_copied);
292
293 /* Check status. */
294 if ((status != NX_SUCCESS) ||
295 (bytes_copied != 4) ||
296 (memcmp(out_buffer, "gA==", 4) != 0))
297 {
298
299 printf("ERROR!\n");
300 test_control_return(1);
301 }
302
303 /* Encode 1 character. */
304 input_buffer[0] = 'a';
305 input_buffer[1] = 0;
306 status = _nx_utility_base64_encode(input_buffer, 1, out_buffer, sizeof(out_buffer), &bytes_copied);
307
308 /* Check status. */
309 if ((status != NX_SUCCESS) ||
310 (bytes_copied != 4) ||
311 (memcmp(out_buffer, "YQ==", 4) != 0))
312 {
313
314 printf("ERROR!\n");
315 test_control_return(1);
316 }
317
318 /* Test array without null terminator. */
319 input_buffer[0] = 'a';
320 input_buffer[1] = 0xff;
321 status = _nx_utility_base64_encode(input_buffer, 1, out_buffer, sizeof(out_buffer), &bytes_copied);
322
323 /* Check status. */
324 if ((status != NX_SUCCESS) ||
325 (bytes_copied != 4) ||
326 (memcmp(out_buffer, "YQ==", 4) != 0))
327 {
328
329 printf("ERROR!\n");
330 test_control_return(1);
331 }
332
333 /* Encode 2 characters. */
334 input_buffer[0] = 'a';
335 input_buffer[1] = 'b';
336 input_buffer[2] = 0;
337 status = _nx_utility_base64_encode(input_buffer, 2, out_buffer, sizeof(out_buffer), &bytes_copied);
338
339 /* Check status. */
340 if ((status != NX_SUCCESS) ||
341 (bytes_copied != 4) ||
342 (memcmp(out_buffer, "YWI=", 4) != 0))
343 {
344
345 printf("ERROR!\n");
346 test_control_return(1);
347 }
348
349 /* Test array without null terminator. */
350 input_buffer[0] = 'a';
351 input_buffer[1] = 'b';
352 input_buffer[2] = 0xff;
353 status = _nx_utility_base64_encode(input_buffer, 2, out_buffer, sizeof(out_buffer), &bytes_copied);
354
355 /* Check status. */
356 if ((status != NX_SUCCESS) ||
357 (bytes_copied != 4) ||
358 (memcmp(out_buffer, "YWI=", 4) != 0))
359 {
360
361 printf("ERROR!\n");
362 test_control_return(1);
363 }
364
365 /* Crash test. */
366 for (i = 1; i <= 0xFF; i++)
367 {
368 input_buffer[0] = i;
369 input_buffer[1] = i;
370 input_buffer[2] = i;
371 input_buffer[3] = 0;
372 _nx_utility_base64_encode(input_buffer, 3, out_buffer, sizeof(out_buffer), &bytes_copied);
373 }
374
375 /* Base64 decode test. */
376
377 /* Null name pointer. */
378 status = _nx_utility_base64_decode(NX_NULL, sizeof("bmFtZTpwYXNzd29yZA==") -1, out_buffer, sizeof(out_buffer), &bytes_copied);
379
380 /* Check status. */
381 if (status != NX_PTR_ERROR)
382 {
383
384 printf("ERROR!\n");
385 test_control_return(1);
386 }
387
388 /* 0 name size. */
389 status = _nx_utility_base64_decode("bmFtZTpwYXNzd29yZA==", 0, out_buffer, sizeof(out_buffer), &bytes_copied);
390
391 /* Check status. */
392 if (status != NX_SIZE_ERROR)
393 {
394
395 printf("ERROR!\n");
396 test_control_return(1);
397 }
398
399 /* NULL buffer pointer. */
400 status = _nx_utility_base64_decode("bmFtZTpwYXNzd29yZA==", sizeof("bmFtZTpwYXNzd29yZA==") -1, NX_NULL, sizeof(out_buffer), &bytes_copied);
401
402 /* Check status. */
403 if (status != NX_PTR_ERROR)
404 {
405
406 printf("ERROR!\n");
407 test_control_return(1);
408 }
409
410 /* 0 buffer size. */
411 status = _nx_utility_base64_decode("bmFtZTpwYXNzd29yZA==", sizeof("bmFtZTpwYXNzd29yZA==") -1, out_buffer, 0, &bytes_copied);
412
413 /* Check status. */
414 if (status != NX_SIZE_ERROR)
415 {
416
417 printf("ERROR!\n");
418 test_control_return(1);
419 }
420
421 /* NULL bytes copied pointer. */
422 status = _nx_utility_base64_decode("bmFtZTpwYXNzd29yZA==", sizeof("bmFtZTpwYXNzd29yZA==") -1, out_buffer, sizeof(out_buffer), NX_NULL);
423
424 /* Check status. */
425 if (status != NX_PTR_ERROR)
426 {
427
428 printf("ERROR!\n");
429 test_control_return(1);
430 }
431
432 /* Encode name with small buffer. */
433 status = _nx_utility_base64_decode("bmFtZTpwYXNzd29yZA==", sizeof("bmFtZTpwYXNzd29yZA==") -1, out_buffer, 13, &bytes_copied);
434
435 /* Check status. */
436 if (status != NX_SIZE_ERROR)
437 {
438
439 printf("ERROR!\n");
440 test_control_return(1);
441 }
442
443 /* Encode name successfully. */
444 status = _nx_utility_base64_decode("bmFtZTpwYXNzd29yZA==", sizeof("bmFtZTpwYXNzd29yZA==") -1, out_buffer, sizeof(out_buffer), &bytes_copied);
445
446 /* Check status. */
447 if ((status != NX_SUCCESS) ||
448 (bytes_copied != 13) ||
449 (memcmp(out_buffer, "name:password", 13) != 0))
450 {
451
452 printf("ERROR!\n");
453 test_control_return(1);
454 }
455
456 /* Encode special character. */
457 status = _nx_utility_base64_decode("gA==", sizeof("gA==") - 1, out_buffer, sizeof(out_buffer), &bytes_copied);
458
459 /* Check status. */
460 if ((status != NX_SUCCESS) ||
461 (bytes_copied != 1) ||
462 (out_buffer[0] != 0x80))
463 {
464
465 printf("ERROR!\n");
466 test_control_return(1);
467 }
468
469 /* Decode cut string. */
470 status = _nx_utility_base64_decode("bmFt=TpwYXNzd29yZA==", sizeof("bmFt=TpwYXNzd29yZA==") -1, out_buffer, sizeof(out_buffer), &bytes_copied);
471
472 /* Check status. */
473 if ((status != NX_SUCCESS) ||
474 (bytes_copied != 3) ||
475 (memcmp(out_buffer, "nam", 3) != 0))
476 {
477
478 printf("ERROR!\n");
479 test_control_return(1);
480 }
481
482 /* Decode cut string. */
483 status = _nx_utility_base64_decode("bmFt\0TpwYXNzd29yZA==", sizeof("bmFt\0TpwYXNzd29yZA==") -1, out_buffer, sizeof(out_buffer), &bytes_copied);
484
485 /* Check status. */
486 if ((status != NX_SUCCESS) ||
487 (bytes_copied != 3) ||
488 (memcmp(out_buffer, "nam", 3) != 0))
489 {
490
491 printf("ERROR!\n");
492 test_control_return(1);
493 }
494
495 /* Crash test. */
496 for (i = 1; i <= 0xFF; i++)
497 {
498 input_buffer[0] = i;
499 input_buffer[1] = i;
500 input_buffer[2] = i;
501 input_buffer[3] = 0;
502 _nx_utility_base64_decode(input_buffer, 3, out_buffer, sizeof(out_buffer), &bytes_copied);
503 }
504
505 /* Read overflow test. */
506 input_buffer[0] = '=';
507 input_buffer[1] = '=';
508 input_buffer[2] = 0;
509 status = _nx_utility_base64_decode(&input_buffer[1], 1, out_buffer, 0xffffffff, &bytes_copied);
510
511 /* Check status. */
512 if (status || bytes_copied != 0)
513 {
514
515 printf("ERROR!\n");
516 test_control_return(1);
517 }
518
519 status = _nx_utility_base64_decode("==", sizeof("==") - 1, out_buffer, sizeof(out_buffer), &bytes_copied);
520
521 /* Check status. */
522 if (status || bytes_copied != 0)
523 {
524
525 printf("ERROR!\n");
526 test_control_return(1);
527 }
528
529 status = _nx_utility_base64_decode("T", sizeof("T") - 1, out_buffer, sizeof(out_buffer), &bytes_copied);
530
531 /* Check status. */
532 if (status || bytes_copied != 0)
533 {
534
535 printf("ERROR!\n");
536 test_control_return(1);
537 }
538
539 status = _nx_utility_base64_decode("TQ", sizeof("TQ") - 1, out_buffer, sizeof(out_buffer), &bytes_copied);
540
541 /* Check status. */
542 if (status || bytes_copied != 1 || out_buffer[0] != 'M')
543 {
544
545 printf("ERROR!\n");
546 test_control_return(1);
547 }
548
549 status = _nx_utility_base64_decode("TQ=", sizeof("TQ=") - 1, out_buffer, sizeof(out_buffer), &bytes_copied);
550
551 /* Check status. */
552 if (status || bytes_copied != 1 || out_buffer[0] != 'M')
553 {
554
555 printf("ERROR!\n");
556 test_control_return(1);
557 }
558
559 number = 0x1234;
560 size = _nx_utility_uint_to_string(number, 10, out_buffer, sizeof(out_buffer));
561 if ((size != 4) || (memcmp(out_buffer, "4660", size) != 0))
562 {
563 printf("ERROR!\n");
564 test_control_return(1);
565 }
566
567 size = _nx_utility_uint_to_string(number, 16, out_buffer, sizeof(out_buffer));
568 if ((size != 4) || (memcmp(out_buffer, "1234", size) != 0))
569 {
570 printf("ERROR!\n");
571 test_control_return(1);
572 }
573
574 size = _nx_utility_uint_to_string(number, 8, out_buffer, sizeof(out_buffer));
575 if ((size != 5) || (memcmp(out_buffer, "11064", size) != 0))
576 {
577 printf("ERROR!\n");
578 test_control_return(1);
579 }
580
581 number = 0xffffffff;
582 size = _nx_utility_uint_to_string(number, 10, out_buffer, sizeof(out_buffer));
583 if ((size != 10) || (memcmp(out_buffer, "4294967295", size) != 0))
584 {
585 printf("ERROR!\n");
586 test_control_return(1);
587 }
588
589 size = _nx_utility_uint_to_string(number, 16, out_buffer, sizeof(out_buffer));
590 if ((size != 8) || (memcmp(out_buffer, "ffffffff", size) != 0))
591 {
592 printf("ERROR!\n");
593 test_control_return(1);
594 }
595
596 size = _nx_utility_uint_to_string(number, 8, out_buffer, sizeof(out_buffer));
597 if ((size != 11) || (memcmp(out_buffer, "37777777777", size) != 0))
598 {
599 printf("ERROR!\n");
600 test_control_return(1);
601 }
602
603 number = 0;
604 size = _nx_utility_uint_to_string(number, 10, out_buffer, sizeof(out_buffer));
605 if ((size != 1) || (memcmp(out_buffer, "0", size) != 0))
606 {
607 printf("ERROR!\n");
608 test_control_return(1);
609 }
610
611 size = _nx_utility_uint_to_string(number, 16, out_buffer, sizeof(out_buffer));
612 if ((size != 1) || (memcmp(out_buffer, "0", size) != 0))
613 {
614 printf("ERROR!\n");
615 test_control_return(1);
616 }
617
618 size = _nx_utility_uint_to_string(number, 8, out_buffer, sizeof(out_buffer));
619 if ((size != 1) || (memcmp(out_buffer, "0", size) != 0))
620 {
621 printf("ERROR!\n");
622 test_control_return(1);
623 }
624
625 number = 0xffffffff;
626 size = _nx_utility_uint_to_string(number, 16, out_buffer, 8);
627 if (size != 0)
628 {
629 printf("ERROR!\n");
630 test_control_return(1);
631 }
632
633 size = _nx_utility_uint_to_string(number, 10, NX_NULL, 8);
634 if (size != 0)
635 {
636 printf("ERROR!\n");
637 test_control_return(1);
638 }
639
640 size = _nx_utility_uint_to_string(number, 10, out_buffer, 0);
641 if (size != 0)
642 {
643 printf("ERROR!\n");
644 test_control_return(1);
645 }
646
647 size = _nx_utility_uint_to_string(number, 0, out_buffer, sizeof(out_buffer));
648 if (size != 0)
649 {
650 printf("ERROR!\n");
651 test_control_return(1);
652 }
653
654 status = _nx_md5_initialize(NX_NULL);
655 if (status != NX_PTR_ERROR)
656 {
657 printf("ERROR!\n");
658 test_control_return(1);
659 }
660
661 status = _nx_md5_update(NX_NULL, input_buffer, 1);
662 if (status != NX_PTR_ERROR)
663 {
664 printf("ERROR!\n");
665 test_control_return(1);
666 }
667
668 status = _nx_md5_update(&context, input_buffer, 0);
669 if (status)
670 {
671 printf("ERROR!\n");
672 test_control_return(1);
673 }
674
675 _nx_md5_initialize(&context);
676 context.nx_md5_bit_count[0] = 0xffffffff;
677
678 status = _nx_md5_update(&context, input_buffer, 1);
679 if (status || context.nx_md5_bit_count[1] != 1)
680 {
681 printf("ERROR!\n");
682 test_control_return(1);
683 }
684
685 printf("SUCCESS!\n");
686 test_control_return(0);
687 }
688
689