diff options
Diffstat (limited to 'src/microhttpd_ws/test_websocket.c')
-rw-r--r-- | src/microhttpd_ws/test_websocket.c | 1247 |
1 files changed, 1182 insertions, 65 deletions
diff --git a/src/microhttpd_ws/test_websocket.c b/src/microhttpd_ws/test_websocket.c index 0034eb4c..29a4661a 100644 --- a/src/microhttpd_ws/test_websocket.c +++ b/src/microhttpd_ws/test_websocket.c | |||
@@ -27,8 +27,13 @@ | |||
27 | #include <stdlib.h> | 27 | #include <stdlib.h> |
28 | #include <string.h> | 28 | #include <string.h> |
29 | #include <stdio.h> | 29 | #include <stdio.h> |
30 | #include <stdint.h> | ||
30 | #include <time.h> | 31 | #include <time.h> |
31 | 32 | ||
33 | #if SIZE_MAX >= 0x100000000 | ||
34 | #define ENABLE_64BIT_TESTS 1 | ||
35 | #endif | ||
36 | |||
32 | int disable_alloc = 0; | 37 | int disable_alloc = 0; |
33 | size_t open_allocs = 0; | 38 | size_t open_allocs = 0; |
34 | 39 | ||
@@ -73,6 +78,20 @@ test_free (void*buf) | |||
73 | free (buf); | 78 | free (buf); |
74 | } | 79 | } |
75 | 80 | ||
81 | /** | ||
82 | * Custom `rng()` function used for client mode tests | ||
83 | */ | ||
84 | static size_t | ||
85 | test_rng (void*cls, void*buf, size_t buf_len) | ||
86 | { | ||
87 | for (size_t i = 0; i < buf_len; ++i) | ||
88 | { | ||
89 | ((char*) buf) [i] = (char) (rand () % 0xFF); | ||
90 | } | ||
91 | |||
92 | return buf_len; | ||
93 | } | ||
94 | |||
76 | 95 | ||
77 | /** | 96 | /** |
78 | * Helper function which allocates a big amount of data | 97 | * Helper function which allocates a big amount of data |
@@ -126,7 +145,14 @@ test_decode_single (unsigned int test_line, | |||
126 | int ret = MHD_WEBSOCKET_STATUS_OK; | 145 | int ret = MHD_WEBSOCKET_STATUS_OK; |
127 | 146 | ||
128 | /* initialize stream */ | 147 | /* initialize stream */ |
129 | ret = MHD_websocket_stream_init (&ws, flags, max_payload_size); | 148 | ret = MHD_websocket_stream_init2 (&ws, |
149 | flags, | ||
150 | max_payload_size, | ||
151 | malloc, | ||
152 | realloc, | ||
153 | free, | ||
154 | NULL, | ||
155 | test_rng); | ||
130 | if (MHD_WEBSOCKET_STATUS_OK != ret) | 156 | if (MHD_WEBSOCKET_STATUS_OK != ret) |
131 | { | 157 | { |
132 | fprintf (stderr, | 158 | fprintf (stderr, |
@@ -267,7 +293,7 @@ test_decode_single (unsigned int test_line, | |||
267 | 293 | ||
268 | /** | 294 | /** |
269 | * Test procedure for `MHD_websocket_stream_init()` and | 295 | * Test procedure for `MHD_websocket_stream_init()` and |
270 | * `MHD_websocket_stream_init()2` | 296 | * `MHD_websocket_stream_init2()` |
271 | */ | 297 | */ |
272 | int | 298 | int |
273 | test_inits () | 299 | test_inits () |
@@ -281,15 +307,19 @@ test_inits () | |||
281 | All valid flags | 307 | All valid flags |
282 | ------------------------------------------------------------------------------ | 308 | ------------------------------------------------------------------------------ |
283 | */ | 309 | */ |
284 | /* Regular test: all valid flags for init */ | 310 | /* Regular test: all valid flags for init (only the even ones work) */ |
285 | for (int i = 0; i < 7; ++i) | 311 | for (int i = 0; i < 7; ++i) |
286 | { | 312 | { |
287 | ws = NULL; | 313 | ws = NULL; |
288 | ret = MHD_websocket_stream_init (&ws, | 314 | ret = MHD_websocket_stream_init (&ws, |
289 | i, | 315 | i, |
290 | 0); | 316 | 0); |
291 | if ((MHD_WEBSOCKET_STATUS_OK != ret) || | 317 | if (((0 == (i & MHD_WEBSOCKET_FLAG_CLIENT)) && |
292 | (NULL == ws) ) | 318 | ((MHD_WEBSOCKET_STATUS_OK != ret) || |
319 | (NULL == ws))) || | ||
320 | ((0 != (i & MHD_WEBSOCKET_FLAG_CLIENT)) && | ||
321 | ((MHD_WEBSOCKET_STATUS_OK == ret) || | ||
322 | (NULL != ws)))) | ||
293 | { | 323 | { |
294 | fprintf (stderr, | 324 | fprintf (stderr, |
295 | "Init test failed in line %u for flags %d.\n", | 325 | "Init test failed in line %u for flags %d.\n", |
@@ -312,7 +342,9 @@ test_inits () | |||
312 | 0, | 342 | 0, |
313 | test_malloc, | 343 | test_malloc, |
314 | test_realloc, | 344 | test_realloc, |
315 | test_free); | 345 | test_free, |
346 | NULL, | ||
347 | test_rng); | ||
316 | if ((MHD_WEBSOCKET_STATUS_OK != ret) || | 348 | if ((MHD_WEBSOCKET_STATUS_OK != ret) || |
317 | (NULL == ws) ) | 349 | (NULL == ws) ) |
318 | { | 350 | { |
@@ -361,7 +393,9 @@ test_inits () | |||
361 | 0, | 393 | 0, |
362 | test_malloc, | 394 | test_malloc, |
363 | test_realloc, | 395 | test_realloc, |
364 | test_free); | 396 | test_free, |
397 | NULL, | ||
398 | NULL); | ||
365 | if ((MHD_WEBSOCKET_STATUS_PARAMETER_ERROR != ret) || | 399 | if ((MHD_WEBSOCKET_STATUS_PARAMETER_ERROR != ret) || |
366 | (NULL != ws) ) | 400 | (NULL != ws) ) |
367 | { | 401 | { |
@@ -410,7 +444,9 @@ test_inits () | |||
410 | 0, | 444 | 0, |
411 | test_malloc, | 445 | test_malloc, |
412 | test_realloc, | 446 | test_realloc, |
413 | test_free); | 447 | test_free, |
448 | NULL, | ||
449 | NULL); | ||
414 | if ((MHD_WEBSOCKET_STATUS_OK != ret) || | 450 | if ((MHD_WEBSOCKET_STATUS_OK != ret) || |
415 | (NULL == ws) ) | 451 | (NULL == ws) ) |
416 | { | 452 | { |
@@ -451,7 +487,9 @@ test_inits () | |||
451 | 1, | 487 | 1, |
452 | test_malloc, | 488 | test_malloc, |
453 | test_realloc, | 489 | test_realloc, |
454 | test_free); | 490 | test_free, |
491 | NULL, | ||
492 | NULL); | ||
455 | if ((MHD_WEBSOCKET_STATUS_OK != ret) || | 493 | if ((MHD_WEBSOCKET_STATUS_OK != ret) || |
456 | (NULL == ws) ) | 494 | (NULL == ws) ) |
457 | { | 495 | { |
@@ -492,7 +530,9 @@ test_inits () | |||
492 | 1000, | 530 | 1000, |
493 | test_malloc, | 531 | test_malloc, |
494 | test_realloc, | 532 | test_realloc, |
495 | test_free); | 533 | test_free, |
534 | NULL, | ||
535 | NULL); | ||
496 | if ((MHD_WEBSOCKET_STATUS_OK != ret) || | 536 | if ((MHD_WEBSOCKET_STATUS_OK != ret) || |
497 | (NULL == ws) ) | 537 | (NULL == ws) ) |
498 | { | 538 | { |
@@ -506,6 +546,7 @@ test_inits () | |||
506 | MHD_websocket_stream_free (ws); | 546 | MHD_websocket_stream_free (ws); |
507 | ws = NULL; | 547 | ws = NULL; |
508 | } | 548 | } |
549 | #ifdef ENABLE_64BIT_TESTS | ||
509 | /* Edge test (success): max_payload_size = 0x7FFFFFFFFFFFFFFF for init */ | 550 | /* Edge test (success): max_payload_size = 0x7FFFFFFFFFFFFFFF for init */ |
510 | ws = NULL; | 551 | ws = NULL; |
511 | ret = MHD_websocket_stream_init (&ws, | 552 | ret = MHD_websocket_stream_init (&ws, |
@@ -533,7 +574,9 @@ test_inits () | |||
533 | (uint64_t) 0x7FFFFFFFFFFFFFFF, | 574 | (uint64_t) 0x7FFFFFFFFFFFFFFF, |
534 | test_malloc, | 575 | test_malloc, |
535 | test_realloc, | 576 | test_realloc, |
536 | test_free); | 577 | test_free, |
578 | NULL, | ||
579 | NULL); | ||
537 | if ((MHD_WEBSOCKET_STATUS_OK != ret) || | 580 | if ((MHD_WEBSOCKET_STATUS_OK != ret) || |
538 | (NULL == ws) ) | 581 | (NULL == ws) ) |
539 | { | 582 | { |
@@ -574,7 +617,9 @@ test_inits () | |||
574 | (uint64_t) 0x8000000000000000, | 617 | (uint64_t) 0x8000000000000000, |
575 | test_malloc, | 618 | test_malloc, |
576 | test_realloc, | 619 | test_realloc, |
577 | test_free); | 620 | test_free, |
621 | NULL, | ||
622 | NULL); | ||
578 | if ((MHD_WEBSOCKET_STATUS_PARAMETER_ERROR != ret) || | 623 | if ((MHD_WEBSOCKET_STATUS_PARAMETER_ERROR != ret) || |
579 | (NULL != ws) ) | 624 | (NULL != ws) ) |
580 | { | 625 | { |
@@ -588,6 +633,7 @@ test_inits () | |||
588 | MHD_websocket_stream_free (ws); | 633 | MHD_websocket_stream_free (ws); |
589 | ws = NULL; | 634 | ws = NULL; |
590 | } | 635 | } |
636 | #endif | ||
591 | 637 | ||
592 | /* | 638 | /* |
593 | ------------------------------------------------------------------------------ | 639 | ------------------------------------------------------------------------------ |
@@ -621,7 +667,9 @@ test_inits () | |||
621 | 0, | 667 | 0, |
622 | test_malloc, | 668 | test_malloc, |
623 | test_realloc, | 669 | test_realloc, |
624 | test_free); | 670 | test_free, |
671 | NULL, | ||
672 | NULL); | ||
625 | if ((MHD_WEBSOCKET_STATUS_PARAMETER_ERROR != ret) || | 673 | if ((MHD_WEBSOCKET_STATUS_PARAMETER_ERROR != ret) || |
626 | (NULL != ws) ) | 674 | (NULL != ws) ) |
627 | { | 675 | { |
@@ -643,7 +691,9 @@ test_inits () | |||
643 | 0, | 691 | 0, |
644 | NULL, | 692 | NULL, |
645 | test_realloc, | 693 | test_realloc, |
646 | test_free); | 694 | test_free, |
695 | NULL, | ||
696 | NULL); | ||
647 | if ((MHD_WEBSOCKET_STATUS_PARAMETER_ERROR != ret) || | 697 | if ((MHD_WEBSOCKET_STATUS_PARAMETER_ERROR != ret) || |
648 | (NULL != ws) ) | 698 | (NULL != ws) ) |
649 | { | 699 | { |
@@ -665,7 +715,9 @@ test_inits () | |||
665 | 0, | 715 | 0, |
666 | test_malloc, | 716 | test_malloc, |
667 | NULL, | 717 | NULL, |
668 | test_free); | 718 | test_free, |
719 | NULL, | ||
720 | NULL); | ||
669 | if ((MHD_WEBSOCKET_STATUS_PARAMETER_ERROR != ret) || | 721 | if ((MHD_WEBSOCKET_STATUS_PARAMETER_ERROR != ret) || |
670 | (NULL != ws) ) | 722 | (NULL != ws) ) |
671 | { | 723 | { |
@@ -687,6 +739,8 @@ test_inits () | |||
687 | 0, | 739 | 0, |
688 | test_malloc, | 740 | test_malloc, |
689 | test_realloc, | 741 | test_realloc, |
742 | NULL, | ||
743 | NULL, | ||
690 | NULL); | 744 | NULL); |
691 | if ((MHD_WEBSOCKET_STATUS_PARAMETER_ERROR != ret) || | 745 | if ((MHD_WEBSOCKET_STATUS_PARAMETER_ERROR != ret) || |
692 | (NULL != ws) ) | 746 | (NULL != ws) ) |
@@ -701,13 +755,109 @@ test_inits () | |||
701 | MHD_websocket_stream_free (ws); | 755 | MHD_websocket_stream_free (ws); |
702 | ws = NULL; | 756 | ws = NULL; |
703 | } | 757 | } |
758 | /* Regular test: rng given for server mode (will be ignored) */ | ||
759 | ws = NULL; | ||
760 | ret = MHD_websocket_stream_init2 (&ws, | ||
761 | MHD_WEBSOCKET_FLAG_SERVER | ||
762 | | MHD_WEBSOCKET_FLAG_NO_FRAGMENTS, | ||
763 | 0, | ||
764 | test_malloc, | ||
765 | test_realloc, | ||
766 | test_free, | ||
767 | NULL, | ||
768 | test_rng); | ||
769 | if ((MHD_WEBSOCKET_STATUS_OK != ret) || | ||
770 | (NULL == ws) ) | ||
771 | { | ||
772 | fprintf (stderr, | ||
773 | "Init test failed in line %u.\n", | ||
774 | (unsigned int) __LINE__); | ||
775 | ++failed; | ||
776 | } | ||
777 | if (NULL != ws) | ||
778 | { | ||
779 | MHD_websocket_stream_free (ws); | ||
780 | ws = NULL; | ||
781 | } | ||
782 | /* Regular test: cls_rng given for server mode (will be ignored) */ | ||
783 | ws = NULL; | ||
784 | ret = MHD_websocket_stream_init2 (&ws, | ||
785 | MHD_WEBSOCKET_FLAG_SERVER | ||
786 | | MHD_WEBSOCKET_FLAG_NO_FRAGMENTS, | ||
787 | 0, | ||
788 | test_malloc, | ||
789 | test_realloc, | ||
790 | test_free, | ||
791 | (void*) 12345, | ||
792 | test_rng); | ||
793 | if ((MHD_WEBSOCKET_STATUS_OK != ret) || | ||
794 | (NULL == ws) ) | ||
795 | { | ||
796 | fprintf (stderr, | ||
797 | "Init test failed in line %u.\n", | ||
798 | (unsigned int) __LINE__); | ||
799 | ++failed; | ||
800 | } | ||
801 | if (NULL != ws) | ||
802 | { | ||
803 | MHD_websocket_stream_free (ws); | ||
804 | ws = NULL; | ||
805 | } | ||
806 | /* Regular test: rng given for client mode */ | ||
807 | ws = NULL; | ||
808 | ret = MHD_websocket_stream_init2 (&ws, | ||
809 | MHD_WEBSOCKET_FLAG_CLIENT | ||
810 | | MHD_WEBSOCKET_FLAG_NO_FRAGMENTS, | ||
811 | 0, | ||
812 | test_malloc, | ||
813 | test_realloc, | ||
814 | test_free, | ||
815 | NULL, | ||
816 | test_rng); | ||
817 | if ((MHD_WEBSOCKET_STATUS_OK != ret) || | ||
818 | (NULL == ws) ) | ||
819 | { | ||
820 | fprintf (stderr, | ||
821 | "Init test failed in line %u.\n", | ||
822 | (unsigned int) __LINE__); | ||
823 | ++failed; | ||
824 | } | ||
825 | if (NULL != ws) | ||
826 | { | ||
827 | MHD_websocket_stream_free (ws); | ||
828 | ws = NULL; | ||
829 | } | ||
830 | /* Fail test: rng not given for client mode */ | ||
831 | ws = NULL; | ||
832 | ret = MHD_websocket_stream_init2 (&ws, | ||
833 | MHD_WEBSOCKET_FLAG_CLIENT | ||
834 | | MHD_WEBSOCKET_FLAG_NO_FRAGMENTS, | ||
835 | 0, | ||
836 | test_malloc, | ||
837 | test_realloc, | ||
838 | test_free, | ||
839 | NULL, | ||
840 | NULL); | ||
841 | if ((MHD_WEBSOCKET_STATUS_PARAMETER_ERROR != ret) || | ||
842 | (NULL != ws) ) | ||
843 | { | ||
844 | fprintf (stderr, | ||
845 | "Init test failed in line %u %u.\n", | ||
846 | (unsigned int) __LINE__, ret); | ||
847 | ++failed; | ||
848 | } | ||
849 | if (NULL != ws) | ||
850 | { | ||
851 | MHD_websocket_stream_free (ws); | ||
852 | ws = NULL; | ||
853 | } | ||
704 | 854 | ||
705 | return failed != 0 ? 0x01 : 0x00; | 855 | return failed != 0 ? 0x01 : 0x00; |
706 | } | 856 | } |
707 | 857 | ||
708 | 858 | ||
709 | /** | 859 | /** |
710 | * Test procedure for `MHD_websocket_create_accept()` | 860 | * Test procedure for `MHD_websocket_create_accept_header()` |
711 | */ | 861 | */ |
712 | int | 862 | int |
713 | test_accept () | 863 | test_accept () |
@@ -723,8 +873,8 @@ test_accept () | |||
723 | */ | 873 | */ |
724 | /* Regular test: Test case from RFC6455 4.2.2 */ | 874 | /* Regular test: Test case from RFC6455 4.2.2 */ |
725 | memset (accept_key, 0, 29); | 875 | memset (accept_key, 0, 29); |
726 | ret = MHD_websocket_create_accept ("dGhlIHNhbXBsZSBub25jZQ==", | 876 | ret = MHD_websocket_create_accept_header ("dGhlIHNhbXBsZSBub25jZQ==", |
727 | accept_key); | 877 | accept_key); |
728 | if ((MHD_WEBSOCKET_STATUS_OK != ret) || | 878 | if ((MHD_WEBSOCKET_STATUS_OK != ret) || |
729 | (0 != memcmp (accept_key, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", 29))) | 879 | (0 != memcmp (accept_key, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", 29))) |
730 | { | 880 | { |
@@ -741,9 +891,9 @@ test_accept () | |||
741 | */ | 891 | */ |
742 | /* Fail test: missing sec-key value */ | 892 | /* Fail test: missing sec-key value */ |
743 | memset (accept_key, 0, 29); | 893 | memset (accept_key, 0, 29); |
744 | ret = MHD_websocket_create_accept (NULL, | 894 | ret = MHD_websocket_create_accept_header (NULL, |
745 | accept_key); | 895 | accept_key); |
746 | if (MHD_WEBSOCKET_STATUS_PARAMETER_ERROR != ret) | 896 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) |
747 | { | 897 | { |
748 | fprintf (stderr, | 898 | fprintf (stderr, |
749 | "Accept test failed in line %u.\n", | 899 | "Accept test failed in line %u.\n", |
@@ -752,8 +902,8 @@ test_accept () | |||
752 | } | 902 | } |
753 | /* Fail test: missing accept variable */ | 903 | /* Fail test: missing accept variable */ |
754 | memset (accept_key, 0, 29); | 904 | memset (accept_key, 0, 29); |
755 | ret = MHD_websocket_create_accept ("dGhlIHNhbXBsZSBub25jZQ==", | 905 | ret = MHD_websocket_create_accept_header ("dGhlIHNhbXBsZSBub25jZQ==", |
756 | NULL); | 906 | NULL); |
757 | if (MHD_WEBSOCKET_STATUS_PARAMETER_ERROR != ret) | 907 | if (MHD_WEBSOCKET_STATUS_PARAMETER_ERROR != ret) |
758 | { | 908 | { |
759 | fprintf (stderr, | 909 | fprintf (stderr, |
@@ -1038,7 +1188,7 @@ test_decodes () | |||
1038 | 12, | 1188 | 12, |
1039 | NULL, | 1189 | NULL, |
1040 | 0, | 1190 | 0, |
1041 | MHD_WEBSOCKET_STATUS_BINARY_FRAGMENT, | 1191 | MHD_WEBSOCKET_STATUS_BINARY_FIRST_FRAGMENT, |
1042 | MHD_WEBSOCKET_VALIDITY_VALID, | 1192 | MHD_WEBSOCKET_VALIDITY_VALID, |
1043 | 6); | 1193 | 6); |
1044 | /* Regular test: Fragmented binary frame without payload, fragments to the caller, 2nd call */ | 1194 | /* Regular test: Fragmented binary frame without payload, fragments to the caller, 2nd call */ |
@@ -1080,7 +1230,7 @@ test_decodes () | |||
1080 | 18, | 1230 | 18, |
1081 | "\x01\x02\x03", | 1231 | "\x01\x02\x03", |
1082 | 3, | 1232 | 3, |
1083 | MHD_WEBSOCKET_STATUS_BINARY_FRAGMENT, | 1233 | MHD_WEBSOCKET_STATUS_BINARY_FIRST_FRAGMENT, |
1084 | MHD_WEBSOCKET_VALIDITY_VALID, | 1234 | MHD_WEBSOCKET_VALIDITY_VALID, |
1085 | 9); | 1235 | 9); |
1086 | /* Regular test: Fragmented binary frame without payload, fragments to the caller, 2nd call */ | 1236 | /* Regular test: Fragmented binary frame without payload, fragments to the caller, 2nd call */ |
@@ -1097,6 +1247,62 @@ test_decodes () | |||
1097 | MHD_WEBSOCKET_STATUS_BINARY_LAST_FRAGMENT, | 1247 | MHD_WEBSOCKET_STATUS_BINARY_LAST_FRAGMENT, |
1098 | MHD_WEBSOCKET_VALIDITY_VALID, | 1248 | MHD_WEBSOCKET_VALIDITY_VALID, |
1099 | 18); | 1249 | 18); |
1250 | /* Regular test: Fragmented binary frame with payload, fragments to the caller, 1st call */ | ||
1251 | failed += test_decode_single (__LINE__, | ||
1252 | MHD_WEBSOCKET_FLAG_SERVER | ||
1253 | | MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS, | ||
1254 | 0, | ||
1255 | 1, | ||
1256 | 0, | ||
1257 | "\x02\x83\x00\x00\x00\x00\x01\x02\x03\x00\x83\x00\x00\x00\x00\x04\x05\x06\x00\x83\x00\x00\x00\x00\x07\x08\x09\x80\x83\x00\x00\x00\x00\x0A\x0B\x0C", | ||
1258 | 36, | ||
1259 | "\x01\x02\x03", | ||
1260 | 3, | ||
1261 | MHD_WEBSOCKET_STATUS_BINARY_FIRST_FRAGMENT, | ||
1262 | MHD_WEBSOCKET_VALIDITY_VALID, | ||
1263 | 9); | ||
1264 | /* Regular test: Fragmented binary frame without payload, fragments to the caller, 2nd call */ | ||
1265 | failed += test_decode_single (__LINE__, | ||
1266 | MHD_WEBSOCKET_FLAG_SERVER | ||
1267 | | MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS, | ||
1268 | 0, | ||
1269 | 2, | ||
1270 | 0, | ||
1271 | "\x02\x83\x00\x00\x00\x00\x01\x02\x03\x00\x83\x00\x00\x00\x00\x04\x05\x06\x00\x83\x00\x00\x00\x00\x07\x08\x09\x80\x83\x00\x00\x00\x00\x0A\x0B\x0C", | ||
1272 | 36, | ||
1273 | "\x04\x05\x06", | ||
1274 | 3, | ||
1275 | MHD_WEBSOCKET_STATUS_BINARY_NEXT_FRAGMENT, | ||
1276 | MHD_WEBSOCKET_VALIDITY_VALID, | ||
1277 | 18); | ||
1278 | /* Regular test: Fragmented binary frame without payload, fragments to the caller, 3rd call */ | ||
1279 | failed += test_decode_single (__LINE__, | ||
1280 | MHD_WEBSOCKET_FLAG_SERVER | ||
1281 | | MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS, | ||
1282 | 0, | ||
1283 | 3, | ||
1284 | 0, | ||
1285 | "\x02\x83\x00\x00\x00\x00\x01\x02\x03\x00\x83\x00\x00\x00\x00\x04\x05\x06\x00\x83\x00\x00\x00\x00\x07\x08\x09\x80\x83\x00\x00\x00\x00\x0A\x0B\x0C", | ||
1286 | 36, | ||
1287 | "\x07\x08\x09", | ||
1288 | 3, | ||
1289 | MHD_WEBSOCKET_STATUS_BINARY_NEXT_FRAGMENT, | ||
1290 | MHD_WEBSOCKET_VALIDITY_VALID, | ||
1291 | 27); | ||
1292 | /* Regular test: Fragmented binary frame without payload, fragments to the caller, 4th call */ | ||
1293 | failed += test_decode_single (__LINE__, | ||
1294 | MHD_WEBSOCKET_FLAG_SERVER | ||
1295 | | MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS, | ||
1296 | 0, | ||
1297 | 4, | ||
1298 | 0, | ||
1299 | "\x02\x83\x00\x00\x00\x00\x01\x02\x03\x00\x83\x00\x00\x00\x00\x04\x05\x06\x00\x83\x00\x00\x00\x00\x07\x08\x09\x80\x83\x00\x00\x00\x00\x0A\x0B\x0C", | ||
1300 | 36, | ||
1301 | "\x0A\x0B\x0C", | ||
1302 | 3, | ||
1303 | MHD_WEBSOCKET_STATUS_BINARY_LAST_FRAGMENT, | ||
1304 | MHD_WEBSOCKET_VALIDITY_VALID, | ||
1305 | 36); | ||
1100 | /* Regular test: Binary frame with bytes which look like invalid UTF-8 character */ | 1306 | /* Regular test: Binary frame with bytes which look like invalid UTF-8 character */ |
1101 | failed += test_decode_single (__LINE__, | 1307 | failed += test_decode_single (__LINE__, |
1102 | MHD_WEBSOCKET_FLAG_SERVER | 1308 | MHD_WEBSOCKET_FLAG_SERVER |
@@ -1167,7 +1373,7 @@ test_decodes () | |||
1167 | 17, | 1373 | 17, |
1168 | "H\xC3", | 1374 | "H\xC3", |
1169 | 2, | 1375 | 2, |
1170 | MHD_WEBSOCKET_STATUS_BINARY_FRAGMENT, | 1376 | MHD_WEBSOCKET_STATUS_BINARY_FIRST_FRAGMENT, |
1171 | MHD_WEBSOCKET_VALIDITY_VALID, | 1377 | MHD_WEBSOCKET_VALIDITY_VALID, |
1172 | 8); | 1378 | 8); |
1173 | /* Regular test: Fragmented binary frame with bytes which look like valid UTF-8 sequence, | 1379 | /* Regular test: Fragmented binary frame with bytes which look like valid UTF-8 sequence, |
@@ -1802,7 +2008,7 @@ test_decodes () | |||
1802 | 17, | 2008 | 17, |
1803 | "Hel", | 2009 | "Hel", |
1804 | 3, | 2010 | 3, |
1805 | MHD_WEBSOCKET_STATUS_TEXT_FRAGMENT, | 2011 | MHD_WEBSOCKET_STATUS_TEXT_FIRST_FRAGMENT, |
1806 | MHD_WEBSOCKET_VALIDITY_VALID, | 2012 | MHD_WEBSOCKET_VALIDITY_VALID, |
1807 | 9); | 2013 | 9); |
1808 | /* Regular test: Fragmented, masked text frame, we are the server and want fragments, second call */ | 2014 | /* Regular test: Fragmented, masked text frame, we are the server and want fragments, second call */ |
@@ -1833,6 +2039,48 @@ test_decodes () | |||
1833 | MHD_WEBSOCKET_STATUS_OK, | 2039 | MHD_WEBSOCKET_STATUS_OK, |
1834 | MHD_WEBSOCKET_VALIDITY_VALID, | 2040 | MHD_WEBSOCKET_VALIDITY_VALID, |
1835 | 17); | 2041 | 17); |
2042 | /* Regular test: Fragmented, masked text frame, we are the server and want fragments, 1st call */ | ||
2043 | failed += test_decode_single (__LINE__, | ||
2044 | MHD_WEBSOCKET_FLAG_SERVER | ||
2045 | | MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS, | ||
2046 | 0, | ||
2047 | 1, | ||
2048 | 0, | ||
2049 | "\x01\x83\x37\xfa\x21\x3d\x7f\x9f\x4d\x00\x81\x3d\x37\xfa\x21\x51\x80\x81\x37\x37\xfa\x21\x58", | ||
2050 | 23, | ||
2051 | "Hel", | ||
2052 | 3, | ||
2053 | MHD_WEBSOCKET_STATUS_TEXT_FIRST_FRAGMENT, | ||
2054 | MHD_WEBSOCKET_VALIDITY_VALID, | ||
2055 | 9); | ||
2056 | /* Regular test: Fragmented, masked text frame, we are the server and want fragments, 2nd call */ | ||
2057 | failed += test_decode_single (__LINE__, | ||
2058 | MHD_WEBSOCKET_FLAG_SERVER | ||
2059 | | MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS, | ||
2060 | 0, | ||
2061 | 2, | ||
2062 | 0, | ||
2063 | "\x01\x83\x37\xfa\x21\x3d\x7f\x9f\x4d\x00\x81\x3d\x37\xfa\x21\x51\x80\x81\x37\x37\xfa\x21\x58", | ||
2064 | 23, | ||
2065 | "l", | ||
2066 | 1, | ||
2067 | MHD_WEBSOCKET_STATUS_TEXT_NEXT_FRAGMENT, | ||
2068 | MHD_WEBSOCKET_VALIDITY_VALID, | ||
2069 | 16); | ||
2070 | /* Regular test: Fragmented, masked text frame, we are the server and want fragments, 3rd call */ | ||
2071 | failed += test_decode_single (__LINE__, | ||
2072 | MHD_WEBSOCKET_FLAG_SERVER | ||
2073 | | MHD_WEBSOCKET_FLAG_WANT_FRAGMENTS, | ||
2074 | 0, | ||
2075 | 3, | ||
2076 | 0, | ||
2077 | "\x01\x83\x37\xfa\x21\x3d\x7f\x9f\x4d\x00\x81\x3d\x37\xfa\x21\x51\x80\x81\x37\x37\xfa\x21\x58", | ||
2078 | 23, | ||
2079 | "o", | ||
2080 | 1, | ||
2081 | MHD_WEBSOCKET_STATUS_TEXT_LAST_FRAGMENT, | ||
2082 | MHD_WEBSOCKET_VALIDITY_VALID, | ||
2083 | 23); | ||
1836 | 2084 | ||
1837 | 2085 | ||
1838 | /* | 2086 | /* |
@@ -2255,6 +2503,7 @@ test_decodes () | |||
2255 | free (buf2); | 2503 | free (buf2); |
2256 | buf2 = NULL; | 2504 | buf2 = NULL; |
2257 | } | 2505 | } |
2506 | #ifdef ENABLE_64BIT_TESTS | ||
2258 | /* Edge test (success): Maximum allowed length (here is only the header checked) */ | 2507 | /* Edge test (success): Maximum allowed length (here is only the header checked) */ |
2259 | failed += test_decode_single (__LINE__, | 2508 | failed += test_decode_single (__LINE__, |
2260 | MHD_WEBSOCKET_FLAG_SERVER | 2509 | MHD_WEBSOCKET_FLAG_SERVER |
@@ -2269,6 +2518,23 @@ test_decodes () | |||
2269 | MHD_WEBSOCKET_STATUS_OK, | 2518 | MHD_WEBSOCKET_STATUS_OK, |
2270 | MHD_WEBSOCKET_VALIDITY_VALID, | 2519 | MHD_WEBSOCKET_VALIDITY_VALID, |
2271 | 10); | 2520 | 10); |
2521 | #else | ||
2522 | /* Edge test (fail): Maximum allowed length | ||
2523 | (the size is allowed, but the system cannot handle this amount of memory) */ | ||
2524 | failed += test_decode_single (__LINE__, | ||
2525 | MHD_WEBSOCKET_FLAG_SERVER | ||
2526 | | MHD_WEBSOCKET_FLAG_NO_FRAGMENTS, | ||
2527 | 0, | ||
2528 | 1, | ||
2529 | 0, | ||
2530 | "\x81\xff\x7f\xff\xff\xff\xff\xff\xff\xff", | ||
2531 | 10, | ||
2532 | NULL, | ||
2533 | 0, | ||
2534 | MHD_WEBSOCKET_STATUS_MAXIMUM_SIZE_EXCEEDED, | ||
2535 | MHD_WEBSOCKET_VALIDITY_INVALID, | ||
2536 | 10); | ||
2537 | #endif | ||
2272 | /* Edge test (fail): Too big payload length */ | 2538 | /* Edge test (fail): Too big payload length */ |
2273 | failed += test_decode_single (__LINE__, | 2539 | failed += test_decode_single (__LINE__, |
2274 | MHD_WEBSOCKET_FLAG_SERVER | 2540 | MHD_WEBSOCKET_FLAG_SERVER |
@@ -2447,7 +2713,7 @@ test_decodes () | |||
2447 | 17, | 2713 | 17, |
2448 | "Hel", | 2714 | "Hel", |
2449 | 3, | 2715 | 3, |
2450 | MHD_WEBSOCKET_STATUS_TEXT_FRAGMENT, | 2716 | MHD_WEBSOCKET_STATUS_TEXT_FIRST_FRAGMENT, |
2451 | MHD_WEBSOCKET_VALIDITY_VALID, | 2717 | MHD_WEBSOCKET_VALIDITY_VALID, |
2452 | 9); | 2718 | 9); |
2453 | /* Edge test (success): Fragmented frames with the sum of payload greater than | 2719 | /* Edge test (success): Fragmented frames with the sum of payload greater than |
@@ -3247,7 +3513,7 @@ test_decodes () | |||
3247 | 28, | 3513 | 28, |
3248 | "This is my n", | 3514 | "This is my n", |
3249 | 12, | 3515 | 12, |
3250 | MHD_WEBSOCKET_STATUS_TEXT_FRAGMENT, | 3516 | MHD_WEBSOCKET_STATUS_TEXT_FIRST_FRAGMENT, |
3251 | MHD_WEBSOCKET_VALIDITY_VALID, | 3517 | MHD_WEBSOCKET_VALIDITY_VALID, |
3252 | 19); | 3518 | 19); |
3253 | /* Regular test: UTF-8 sequence between fragments, fragmentation for the caller, 2nd call */ | 3519 | /* Regular test: UTF-8 sequence between fragments, fragmentation for the caller, 2nd call */ |
@@ -3276,7 +3542,7 @@ test_decodes () | |||
3276 | 14, | 3542 | 14, |
3277 | NULL, | 3543 | NULL, |
3278 | 0, | 3544 | 0, |
3279 | MHD_WEBSOCKET_STATUS_TEXT_FRAGMENT, | 3545 | MHD_WEBSOCKET_STATUS_TEXT_FIRST_FRAGMENT, |
3280 | MHD_WEBSOCKET_VALIDITY_VALID, | 3546 | MHD_WEBSOCKET_VALIDITY_VALID, |
3281 | 7); | 3547 | 7); |
3282 | /* Edge test (success): UTF-8 sequence between fragments, but nothing before, fragmentation for the caller, 2nd call */ | 3548 | /* Edge test (success): UTF-8 sequence between fragments, but nothing before, fragmentation for the caller, 2nd call */ |
@@ -3664,7 +3930,7 @@ test_decodes () | |||
3664 | 35, | 3930 | 35, |
3665 | "This ", | 3931 | "This ", |
3666 | 5, | 3932 | 5, |
3667 | MHD_WEBSOCKET_STATUS_TEXT_FRAGMENT, | 3933 | MHD_WEBSOCKET_STATUS_TEXT_FIRST_FRAGMENT, |
3668 | MHD_WEBSOCKET_VALIDITY_VALID, | 3934 | MHD_WEBSOCKET_VALIDITY_VALID, |
3669 | 11); | 3935 | 11); |
3670 | /* Regular test: Fragmented text frame mixed with one ping frame, the caller wants fragments (2nd call) */ | 3936 | /* Regular test: Fragmented text frame mixed with one ping frame, the caller wants fragments (2nd call) */ |
@@ -3734,7 +4000,7 @@ test_decodes () | |||
3734 | 36, | 4000 | 36, |
3735 | "This ", | 4001 | "This ", |
3736 | 5, | 4002 | 5, |
3737 | MHD_WEBSOCKET_STATUS_TEXT_FRAGMENT, | 4003 | MHD_WEBSOCKET_STATUS_TEXT_FIRST_FRAGMENT, |
3738 | MHD_WEBSOCKET_VALIDITY_VALID, | 4004 | MHD_WEBSOCKET_VALIDITY_VALID, |
3739 | 11); | 4005 | 11); |
3740 | /* Fail test: Fragmented text frame mixed with one non-fragmented binary frame; the caller wants fragments; 2nd call */ | 4006 | /* Fail test: Fragmented text frame mixed with one non-fragmented binary frame; the caller wants fragments; 2nd call */ |
@@ -4106,7 +4372,7 @@ test_decodes () | |||
4106 | &streambuf_read_len, | 4372 | &streambuf_read_len, |
4107 | &payload, | 4373 | &payload, |
4108 | &payload_len); | 4374 | &payload_len); |
4109 | if ((MHD_WEBSOCKET_STATUS_TEXT_FRAGMENT != ret) || | 4375 | if ((MHD_WEBSOCKET_STATUS_TEXT_FIRST_FRAGMENT != ret) || |
4110 | (3 != payload_len) || | 4376 | (3 != payload_len) || |
4111 | (NULL == payload) || | 4377 | (NULL == payload) || |
4112 | (0 != memcmp ("Hel", payload, 3 + 1))) | 4378 | (0 != memcmp ("Hel", payload, 3 + 1))) |
@@ -4155,7 +4421,7 @@ test_decodes () | |||
4155 | &streambuf_read_len, | 4421 | &streambuf_read_len, |
4156 | &payload, | 4422 | &payload, |
4157 | &payload_len); | 4423 | &payload_len); |
4158 | if ((MHD_WEBSOCKET_STATUS_TEXT_FRAGMENT != ret) || | 4424 | if ((MHD_WEBSOCKET_STATUS_TEXT_FIRST_FRAGMENT != ret) || |
4159 | (2 != payload_len) || | 4425 | (2 != payload_len) || |
4160 | (NULL == payload) || | 4426 | (NULL == payload) || |
4161 | (0 != memcmp ("He", payload, 2 + 1))) | 4427 | (0 != memcmp ("He", payload, 2 + 1))) |
@@ -4195,6 +4461,8 @@ test_decodes () | |||
4195 | MHD_websocket_free (ws, payload); | 4461 | MHD_websocket_free (ws, payload); |
4196 | payload = NULL; | 4462 | payload = NULL; |
4197 | } | 4463 | } |
4464 | |||
4465 | MHD_websocket_stream_free (ws); | ||
4198 | } | 4466 | } |
4199 | else | 4467 | else |
4200 | { | 4468 | { |
@@ -4439,7 +4707,9 @@ test_decodes () | |||
4439 | 0, | 4707 | 0, |
4440 | test_malloc, | 4708 | test_malloc, |
4441 | test_realloc, | 4709 | test_realloc, |
4442 | test_free)) | 4710 | test_free, |
4711 | NULL, | ||
4712 | NULL)) | ||
4443 | { | 4713 | { |
4444 | size_t streambuf_read_len = 0; | 4714 | size_t streambuf_read_len = 0; |
4445 | char*payload = NULL; | 4715 | char*payload = NULL; |
@@ -4484,7 +4754,9 @@ test_decodes () | |||
4484 | 0, | 4754 | 0, |
4485 | test_malloc, | 4755 | test_malloc, |
4486 | test_realloc, | 4756 | test_realloc, |
4487 | test_free)) | 4757 | test_free, |
4758 | NULL, | ||
4759 | NULL)) | ||
4488 | { | 4760 | { |
4489 | /* Failure test: No memory allocation after fragmented frame */ | 4761 | /* Failure test: No memory allocation after fragmented frame */ |
4490 | disable_alloc = 0; | 4762 | disable_alloc = 0; |
@@ -4623,7 +4895,9 @@ test_decodes () | |||
4623 | 0, | 4895 | 0, |
4624 | test_malloc, | 4896 | test_malloc, |
4625 | test_realloc, | 4897 | test_realloc, |
4626 | test_free)) | 4898 | test_free, |
4899 | NULL, | ||
4900 | NULL)) | ||
4627 | { | 4901 | { |
4628 | ret = MHD_websocket_decode (ws, | 4902 | ret = MHD_websocket_decode (ws, |
4629 | "\x81\x85\x00\x00\x00\x00Hel", | 4903 | "\x81\x85\x00\x00\x00\x00Hel", |
@@ -4674,7 +4948,9 @@ test_decodes () | |||
4674 | 0, | 4948 | 0, |
4675 | test_malloc, | 4949 | test_malloc, |
4676 | test_realloc, | 4950 | test_realloc, |
4677 | test_free)) | 4951 | test_free, |
4952 | NULL, | ||
4953 | NULL)) | ||
4678 | { | 4954 | { |
4679 | ret = MHD_websocket_decode (ws, | 4955 | ret = MHD_websocket_decode (ws, |
4680 | "\x88\x85\x00\x00\x00\x00Hel", | 4956 | "\x88\x85\x00\x00\x00\x00Hel", |
@@ -4725,7 +5001,9 @@ test_decodes () | |||
4725 | 0, | 5001 | 0, |
4726 | test_malloc, | 5002 | test_malloc, |
4727 | test_realloc, | 5003 | test_realloc, |
4728 | test_free)) | 5004 | test_free, |
5005 | NULL, | ||
5006 | NULL)) | ||
4729 | { | 5007 | { |
4730 | ret = MHD_websocket_decode (ws, | 5008 | ret = MHD_websocket_decode (ws, |
4731 | "\x01\x85\x00\x00\x00\x00Hello", | 5009 | "\x01\x85\x00\x00\x00\x00Hello", |
@@ -4775,7 +5053,9 @@ test_decodes () | |||
4775 | 0, | 5053 | 0, |
4776 | test_malloc, | 5054 | test_malloc, |
4777 | test_realloc, | 5055 | test_realloc, |
4778 | test_free)) | 5056 | test_free, |
5057 | NULL, | ||
5058 | NULL)) | ||
4779 | { | 5059 | { |
4780 | ret = MHD_websocket_decode (ws, | 5060 | ret = MHD_websocket_decode (ws, |
4781 | "\x01\x85\x00\x00\x00\x00Hello", | 5061 | "\x01\x85\x00\x00\x00\x00Hello", |
@@ -4841,7 +5121,9 @@ test_decodes () | |||
4841 | 0, | 5121 | 0, |
4842 | test_malloc, | 5122 | test_malloc, |
4843 | test_realloc, | 5123 | test_realloc, |
4844 | test_free)) | 5124 | test_free, |
5125 | NULL, | ||
5126 | NULL)) | ||
4845 | { | 5127 | { |
4846 | ret = MHD_websocket_decode (ws, | 5128 | ret = MHD_websocket_decode (ws, |
4847 | "\x01\x85\x00\x00\x00\x00Hello", | 5129 | "\x01\x85\x00\x00\x00\x00Hello", |
@@ -4929,9 +5211,14 @@ test_encodes_text () | |||
4929 | size_t frame_len = 0; | 5211 | size_t frame_len = 0; |
4930 | int utf8_step = 0; | 5212 | int utf8_step = 0; |
4931 | 5213 | ||
4932 | if (MHD_WEBSOCKET_STATUS_OK != MHD_websocket_stream_init (&wsc, | 5214 | if (MHD_WEBSOCKET_STATUS_OK != MHD_websocket_stream_init2 (&wsc, |
4933 | MHD_WEBSOCKET_FLAG_CLIENT, | 5215 | MHD_WEBSOCKET_FLAG_CLIENT, |
4934 | 0)) | 5216 | 0, |
5217 | malloc, | ||
5218 | realloc, | ||
5219 | free, | ||
5220 | NULL, | ||
5221 | test_rng)) | ||
4935 | { | 5222 | { |
4936 | fprintf (stderr, | 5223 | fprintf (stderr, |
4937 | "No encode text tests possible due to failed stream init in line %u\n", | 5224 | "No encode text tests possible due to failed stream init in line %u\n", |
@@ -5742,6 +6029,7 @@ test_encodes_text () | |||
5742 | free (buf2); | 6029 | free (buf2); |
5743 | buf2 = NULL; | 6030 | buf2 = NULL; |
5744 | } | 6031 | } |
6032 | #ifdef ENABLE_64BIT_TESTS | ||
5745 | /* Fail test: frame_len is greater than 0x7FFFFFFFFFFFFFFF | 6033 | /* Fail test: frame_len is greater than 0x7FFFFFFFFFFFFFFF |
5746 | (this is the maximum allowed payload size) */ | 6034 | (this is the maximum allowed payload size) */ |
5747 | frame_len = 0; | 6035 | frame_len = 0; |
@@ -5766,6 +6054,7 @@ test_encodes_text () | |||
5766 | MHD_websocket_free (wss, frame); | 6054 | MHD_websocket_free (wss, frame); |
5767 | frame = NULL; | 6055 | frame = NULL; |
5768 | } | 6056 | } |
6057 | #endif | ||
5769 | 6058 | ||
5770 | /* | 6059 | /* |
5771 | ------------------------------------------------------------------------------ | 6060 | ------------------------------------------------------------------------------ |
@@ -6188,7 +6477,9 @@ test_encodes_text () | |||
6188 | 0, | 6477 | 0, |
6189 | test_malloc, | 6478 | test_malloc, |
6190 | test_realloc, | 6479 | test_realloc, |
6191 | test_free)) | 6480 | test_free, |
6481 | NULL, | ||
6482 | NULL)) | ||
6192 | { | 6483 | { |
6193 | /* Fail test: allocation while no memory available */ | 6484 | /* Fail test: allocation while no memory available */ |
6194 | disable_alloc = 1; | 6485 | disable_alloc = 1; |
@@ -6276,9 +6567,14 @@ test_encodes_binary () | |||
6276 | char*frame = NULL; | 6567 | char*frame = NULL; |
6277 | size_t frame_len = 0; | 6568 | size_t frame_len = 0; |
6278 | 6569 | ||
6279 | if (MHD_WEBSOCKET_STATUS_OK != MHD_websocket_stream_init (&wsc, | 6570 | if (MHD_WEBSOCKET_STATUS_OK != MHD_websocket_stream_init2 (&wsc, |
6280 | MHD_WEBSOCKET_FLAG_CLIENT, | 6571 | MHD_WEBSOCKET_FLAG_CLIENT, |
6281 | 0)) | 6572 | 0, |
6573 | malloc, | ||
6574 | realloc, | ||
6575 | free, | ||
6576 | NULL, | ||
6577 | test_rng)) | ||
6282 | { | 6578 | { |
6283 | fprintf (stderr, | 6579 | fprintf (stderr, |
6284 | "No encode binary tests possible due to failed stream init in line %u\n", | 6580 | "No encode binary tests possible due to failed stream init in line %u\n", |
@@ -6688,6 +6984,7 @@ test_encodes_binary () | |||
6688 | free (buf2); | 6984 | free (buf2); |
6689 | buf2 = NULL; | 6985 | buf2 = NULL; |
6690 | } | 6986 | } |
6987 | #ifdef ENABLE_64BIT_TESTS | ||
6691 | /* Fail test: `frame_len` is greater than 0x7FFFFFFFFFFFFFFF | 6988 | /* Fail test: `frame_len` is greater than 0x7FFFFFFFFFFFFFFF |
6692 | (this is the maximum allowed payload size) */ | 6989 | (this is the maximum allowed payload size) */ |
6693 | frame_len = 0; | 6990 | frame_len = 0; |
@@ -6711,6 +7008,7 @@ test_encodes_binary () | |||
6711 | MHD_websocket_free (wss, frame); | 7008 | MHD_websocket_free (wss, frame); |
6712 | frame = NULL; | 7009 | frame = NULL; |
6713 | } | 7010 | } |
7011 | #endif | ||
6714 | 7012 | ||
6715 | /* | 7013 | /* |
6716 | ------------------------------------------------------------------------------ | 7014 | ------------------------------------------------------------------------------ |
@@ -6882,7 +7180,9 @@ test_encodes_binary () | |||
6882 | 0, | 7180 | 0, |
6883 | test_malloc, | 7181 | test_malloc, |
6884 | test_realloc, | 7182 | test_realloc, |
6885 | test_free)) | 7183 | test_free, |
7184 | NULL, | ||
7185 | NULL)) | ||
6886 | { | 7186 | { |
6887 | /* Fail test: allocation while no memory available */ | 7187 | /* Fail test: allocation while no memory available */ |
6888 | disable_alloc = 1; | 7188 | disable_alloc = 1; |
@@ -6968,18 +7268,28 @@ test_encodes_close () | |||
6968 | char*frame = NULL; | 7268 | char*frame = NULL; |
6969 | size_t frame_len = 0; | 7269 | size_t frame_len = 0; |
6970 | 7270 | ||
6971 | if (MHD_WEBSOCKET_STATUS_OK != MHD_websocket_stream_init (&wsc, | 7271 | if (MHD_WEBSOCKET_STATUS_OK != MHD_websocket_stream_init2 (&wsc, |
6972 | MHD_WEBSOCKET_FLAG_CLIENT, | 7272 | MHD_WEBSOCKET_FLAG_CLIENT, |
6973 | 0)) | 7273 | 0, |
7274 | malloc, | ||
7275 | realloc, | ||
7276 | free, | ||
7277 | NULL, | ||
7278 | test_rng)) | ||
6974 | { | 7279 | { |
6975 | fprintf (stderr, | 7280 | fprintf (stderr, |
6976 | "No encode close tests possible due to failed stream init in line %u\n", | 7281 | "No encode close tests possible due to failed stream init in line %u\n", |
6977 | (unsigned int) __LINE__); | 7282 | (unsigned int) __LINE__); |
6978 | return 0x10; | 7283 | return 0x10; |
6979 | } | 7284 | } |
6980 | if (MHD_WEBSOCKET_STATUS_OK != MHD_websocket_stream_init (&wss, | 7285 | if (MHD_WEBSOCKET_STATUS_OK != MHD_websocket_stream_init2 (&wss, |
6981 | MHD_WEBSOCKET_FLAG_SERVER, | 7286 | MHD_WEBSOCKET_FLAG_SERVER, |
6982 | 0)) | 7287 | 0, |
7288 | malloc, | ||
7289 | realloc, | ||
7290 | free, | ||
7291 | NULL, | ||
7292 | test_rng)) | ||
6983 | { | 7293 | { |
6984 | fprintf (stderr, | 7294 | fprintf (stderr, |
6985 | "No encode close tests possible due to failed stream init in line %u\n", | 7295 | "No encode close tests possible due to failed stream init in line %u\n", |
@@ -7623,7 +7933,9 @@ test_encodes_close () | |||
7623 | 0, | 7933 | 0, |
7624 | test_malloc, | 7934 | test_malloc, |
7625 | test_realloc, | 7935 | test_realloc, |
7626 | test_free)) | 7936 | test_free, |
7937 | NULL, | ||
7938 | NULL)) | ||
7627 | { | 7939 | { |
7628 | /* Fail test: allocation while no memory available */ | 7940 | /* Fail test: allocation while no memory available */ |
7629 | disable_alloc = 1; | 7941 | disable_alloc = 1; |
@@ -7709,9 +8021,14 @@ test_encodes_ping () | |||
7709 | char*frame = NULL; | 8021 | char*frame = NULL; |
7710 | size_t frame_len = 0; | 8022 | size_t frame_len = 0; |
7711 | 8023 | ||
7712 | if (MHD_WEBSOCKET_STATUS_OK != MHD_websocket_stream_init (&wsc, | 8024 | if (MHD_WEBSOCKET_STATUS_OK != MHD_websocket_stream_init2 (&wsc, |
7713 | MHD_WEBSOCKET_FLAG_CLIENT, | 8025 | MHD_WEBSOCKET_FLAG_CLIENT, |
7714 | 0)) | 8026 | 0, |
8027 | malloc, | ||
8028 | realloc, | ||
8029 | free, | ||
8030 | NULL, | ||
8031 | test_rng)) | ||
7715 | { | 8032 | { |
7716 | fprintf (stderr, | 8033 | fprintf (stderr, |
7717 | "No encode ping tests possible due to failed stream init in line %u\n", | 8034 | "No encode ping tests possible due to failed stream init in line %u\n", |
@@ -8156,7 +8473,9 @@ test_encodes_ping () | |||
8156 | 0, | 8473 | 0, |
8157 | test_malloc, | 8474 | test_malloc, |
8158 | test_realloc, | 8475 | test_realloc, |
8159 | test_free)) | 8476 | test_free, |
8477 | NULL, | ||
8478 | NULL)) | ||
8160 | { | 8479 | { |
8161 | /* Fail test: allocation while no memory available */ | 8480 | /* Fail test: allocation while no memory available */ |
8162 | disable_alloc = 1; | 8481 | disable_alloc = 1; |
@@ -8240,9 +8559,14 @@ test_encodes_pong () | |||
8240 | char*frame = NULL; | 8559 | char*frame = NULL; |
8241 | size_t frame_len = 0; | 8560 | size_t frame_len = 0; |
8242 | 8561 | ||
8243 | if (MHD_WEBSOCKET_STATUS_OK != MHD_websocket_stream_init (&wsc, | 8562 | if (MHD_WEBSOCKET_STATUS_OK != MHD_websocket_stream_init2 (&wsc, |
8244 | MHD_WEBSOCKET_FLAG_CLIENT, | 8563 | MHD_WEBSOCKET_FLAG_CLIENT, |
8245 | 0)) | 8564 | 0, |
8565 | malloc, | ||
8566 | realloc, | ||
8567 | free, | ||
8568 | NULL, | ||
8569 | test_rng)) | ||
8246 | { | 8570 | { |
8247 | fprintf (stderr, | 8571 | fprintf (stderr, |
8248 | "No encode pong tests possible due to failed stream init in line %u\n", | 8572 | "No encode pong tests possible due to failed stream init in line %u\n", |
@@ -8687,7 +9011,9 @@ test_encodes_pong () | |||
8687 | 0, | 9011 | 0, |
8688 | test_malloc, | 9012 | test_malloc, |
8689 | test_realloc, | 9013 | test_realloc, |
8690 | test_free)) | 9014 | test_free, |
9015 | NULL, | ||
9016 | NULL)) | ||
8691 | { | 9017 | { |
8692 | /* Fail test: allocation while no memory available */ | 9018 | /* Fail test: allocation while no memory available */ |
8693 | disable_alloc = 1; | 9019 | disable_alloc = 1; |
@@ -8955,6 +9281,793 @@ test_split_close_reason () | |||
8955 | } | 9281 | } |
8956 | 9282 | ||
8957 | 9283 | ||
9284 | /** | ||
9285 | * Test procedure for `MHD_websocket_check_http_version()` | ||
9286 | */ | ||
9287 | int | ||
9288 | test_check_http_version () | ||
9289 | { | ||
9290 | int failed = 0; | ||
9291 | int ret; | ||
9292 | |||
9293 | /* | ||
9294 | ------------------------------------------------------------------------------ | ||
9295 | Version check with valid HTTP version syntax | ||
9296 | ------------------------------------------------------------------------------ | ||
9297 | */ | ||
9298 | /* Regular test: HTTP/1.1 */ | ||
9299 | ret = MHD_websocket_check_http_version ("HTTP/1.1"); | ||
9300 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9301 | { | ||
9302 | fprintf (stderr, | ||
9303 | "check_http_version test failed in line %u.\n", | ||
9304 | (unsigned int) __LINE__); | ||
9305 | ++failed; | ||
9306 | } | ||
9307 | /* Regular test: HTTP/1.2 */ | ||
9308 | ret = MHD_websocket_check_http_version ("HTTP/1.2"); | ||
9309 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9310 | { | ||
9311 | fprintf (stderr, | ||
9312 | "check_http_version test failed in line %u.\n", | ||
9313 | (unsigned int) __LINE__); | ||
9314 | ++failed; | ||
9315 | } | ||
9316 | /* Regular test: HTTP/1.10 */ | ||
9317 | ret = MHD_websocket_check_http_version ("HTTP/1.10"); | ||
9318 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9319 | { | ||
9320 | fprintf (stderr, | ||
9321 | "check_http_version test failed in line %u.\n", | ||
9322 | (unsigned int) __LINE__); | ||
9323 | ++failed; | ||
9324 | } | ||
9325 | /* Regular test: HTTP/2.0 */ | ||
9326 | ret = MHD_websocket_check_http_version ("HTTP/2.0"); | ||
9327 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9328 | { | ||
9329 | fprintf (stderr, | ||
9330 | "check_http_version test failed in line %u.\n", | ||
9331 | (unsigned int) __LINE__); | ||
9332 | ++failed; | ||
9333 | } | ||
9334 | /* Regular test: HTTP/3.0 */ | ||
9335 | ret = MHD_websocket_check_http_version ("HTTP/3.0"); | ||
9336 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9337 | { | ||
9338 | fprintf (stderr, | ||
9339 | "check_http_version test failed in line %u.\n", | ||
9340 | (unsigned int) __LINE__); | ||
9341 | ++failed; | ||
9342 | } | ||
9343 | /* Fail test: HTTP/1.0 */ | ||
9344 | ret = MHD_websocket_check_http_version ("HTTP/1.0"); | ||
9345 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9346 | { | ||
9347 | fprintf (stderr, | ||
9348 | "check_http_version test failed in line %u.\n", | ||
9349 | (unsigned int) __LINE__); | ||
9350 | ++failed; | ||
9351 | } | ||
9352 | /* Fail test: HTTP/0.9 */ | ||
9353 | ret = MHD_websocket_check_http_version ("HTTP/0.9"); | ||
9354 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9355 | { | ||
9356 | fprintf (stderr, | ||
9357 | "check_http_version test failed in line %u.\n", | ||
9358 | (unsigned int) __LINE__); | ||
9359 | ++failed; | ||
9360 | } | ||
9361 | |||
9362 | /* | ||
9363 | ------------------------------------------------------------------------------ | ||
9364 | Version check edge cases | ||
9365 | ------------------------------------------------------------------------------ | ||
9366 | */ | ||
9367 | /* Edge test (success): HTTP/123.45 */ | ||
9368 | ret = MHD_websocket_check_http_version ("HTTP/123.45"); | ||
9369 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9370 | { | ||
9371 | fprintf (stderr, | ||
9372 | "check_http_version test failed in line %u.\n", | ||
9373 | (unsigned int) __LINE__); | ||
9374 | ++failed; | ||
9375 | } | ||
9376 | /* Edge test (success): HTTP/1.45 */ | ||
9377 | ret = MHD_websocket_check_http_version ("HTTP/1.45"); | ||
9378 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9379 | { | ||
9380 | fprintf (stderr, | ||
9381 | "check_http_version test failed in line %u.\n", | ||
9382 | (unsigned int) __LINE__); | ||
9383 | ++failed; | ||
9384 | } | ||
9385 | /* Edge test (success): HTTP/01.1 */ | ||
9386 | ret = MHD_websocket_check_http_version ("HTTP/01.1"); | ||
9387 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9388 | { | ||
9389 | fprintf (stderr, | ||
9390 | "check_http_version test failed in line %u.\n", | ||
9391 | (unsigned int) __LINE__); | ||
9392 | ++failed; | ||
9393 | } | ||
9394 | /* Edge test (success): HTTP/0001.1 */ | ||
9395 | ret = MHD_websocket_check_http_version ("HTTP/0001.1"); | ||
9396 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9397 | { | ||
9398 | fprintf (stderr, | ||
9399 | "check_http_version test failed in line %u.\n", | ||
9400 | (unsigned int) __LINE__); | ||
9401 | ++failed; | ||
9402 | } | ||
9403 | /* Edge test (success): HTTP/1.01 */ | ||
9404 | ret = MHD_websocket_check_http_version ("HTTP/1.01"); | ||
9405 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9406 | { | ||
9407 | fprintf (stderr, | ||
9408 | "check_http_version test failed in line %u.\n", | ||
9409 | (unsigned int) __LINE__); | ||
9410 | ++failed; | ||
9411 | } | ||
9412 | /* Edge test (success): HTTP/1.0001 */ | ||
9413 | ret = MHD_websocket_check_http_version ("HTTP/1.0001"); | ||
9414 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9415 | { | ||
9416 | fprintf (stderr, | ||
9417 | "check_http_version test failed in line %u.\n", | ||
9418 | (unsigned int) __LINE__); | ||
9419 | ++failed; | ||
9420 | } | ||
9421 | /* Edge test (success): HTTP/0001.0001 */ | ||
9422 | ret = MHD_websocket_check_http_version ("HTTP/0001.0001"); | ||
9423 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9424 | { | ||
9425 | fprintf (stderr, | ||
9426 | "check_http_version test failed in line %u.\n", | ||
9427 | (unsigned int) __LINE__); | ||
9428 | ++failed; | ||
9429 | } | ||
9430 | /* Edge test (success): HTTP/2.000 */ | ||
9431 | ret = MHD_websocket_check_http_version ("HTTP/2.000"); | ||
9432 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9433 | { | ||
9434 | fprintf (stderr, | ||
9435 | "check_http_version test failed in line %u.\n", | ||
9436 | (unsigned int) __LINE__); | ||
9437 | ++failed; | ||
9438 | } | ||
9439 | /* Edge test (fail): HTTP/0.0 */ | ||
9440 | ret = MHD_websocket_check_http_version ("HTTP/0.0"); | ||
9441 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9442 | { | ||
9443 | fprintf (stderr, | ||
9444 | "check_http_version test failed in line %u.\n", | ||
9445 | (unsigned int) __LINE__); | ||
9446 | ++failed; | ||
9447 | } | ||
9448 | /* Edge test (fail): HTTP/00.0 */ | ||
9449 | ret = MHD_websocket_check_http_version ("HTTP/00.0"); | ||
9450 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9451 | { | ||
9452 | fprintf (stderr, | ||
9453 | "check_http_version test failed in line %u.\n", | ||
9454 | (unsigned int) __LINE__); | ||
9455 | ++failed; | ||
9456 | } | ||
9457 | /* Edge test (fail): HTTP/00.0 */ | ||
9458 | ret = MHD_websocket_check_http_version ("HTTP/0.00"); | ||
9459 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9460 | { | ||
9461 | fprintf (stderr, | ||
9462 | "check_http_version test failed in line %u.\n", | ||
9463 | (unsigned int) __LINE__); | ||
9464 | ++failed; | ||
9465 | } | ||
9466 | |||
9467 | /* | ||
9468 | ------------------------------------------------------------------------------ | ||
9469 | Invalid version syntax | ||
9470 | ------------------------------------------------------------------------------ | ||
9471 | */ | ||
9472 | /* Fail test: (empty string) */ | ||
9473 | ret = MHD_websocket_check_http_version (""); | ||
9474 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9475 | { | ||
9476 | fprintf (stderr, | ||
9477 | "check_http_version test failed in line %u.\n", | ||
9478 | (unsigned int) __LINE__); | ||
9479 | ++failed; | ||
9480 | } | ||
9481 | /* Fail test: http/1.1 */ | ||
9482 | ret = MHD_websocket_check_http_version ("http/1.1"); | ||
9483 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9484 | { | ||
9485 | fprintf (stderr, | ||
9486 | "check_http_version test failed in line %u.\n", | ||
9487 | (unsigned int) __LINE__); | ||
9488 | ++failed; | ||
9489 | } | ||
9490 | /* Fail test: "HTTP / 1.1" */ | ||
9491 | ret = MHD_websocket_check_http_version ("HTTP / 1.1"); | ||
9492 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9493 | { | ||
9494 | fprintf (stderr, | ||
9495 | "check_http_version test failed in line %u.\n", | ||
9496 | (unsigned int) __LINE__); | ||
9497 | ++failed; | ||
9498 | } | ||
9499 | |||
9500 | /* | ||
9501 | ------------------------------------------------------------------------------ | ||
9502 | Missing parameters | ||
9503 | ------------------------------------------------------------------------------ | ||
9504 | */ | ||
9505 | /* Fail test: NULL as version */ | ||
9506 | ret = MHD_websocket_check_http_version (NULL); | ||
9507 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9508 | { | ||
9509 | fprintf (stderr, | ||
9510 | "check_http_version test failed in line %u.\n", | ||
9511 | (unsigned int) __LINE__); | ||
9512 | ++failed; | ||
9513 | } | ||
9514 | |||
9515 | return failed != 0 ? 0x200 : 0x00; | ||
9516 | } | ||
9517 | |||
9518 | |||
9519 | /** | ||
9520 | * Test procedure for `MHD_websocket_check_connection_header()` | ||
9521 | */ | ||
9522 | int | ||
9523 | test_check_connection_header () | ||
9524 | { | ||
9525 | int failed = 0; | ||
9526 | int ret; | ||
9527 | |||
9528 | /* | ||
9529 | ------------------------------------------------------------------------------ | ||
9530 | Check with valid Connection header syntax | ||
9531 | ------------------------------------------------------------------------------ | ||
9532 | */ | ||
9533 | /* Regular test: Upgrade */ | ||
9534 | ret = MHD_websocket_check_connection_header ("Upgrade"); | ||
9535 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9536 | { | ||
9537 | fprintf (stderr, | ||
9538 | "check_connection_header test failed in line %u.\n", | ||
9539 | (unsigned int) __LINE__); | ||
9540 | ++failed; | ||
9541 | } | ||
9542 | /* Regular test: keep-alive, Upgrade */ | ||
9543 | ret = MHD_websocket_check_connection_header ("keep-alive, Upgrade"); | ||
9544 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9545 | { | ||
9546 | fprintf (stderr, | ||
9547 | "check_connection_header test failed in line %u.\n", | ||
9548 | (unsigned int) __LINE__); | ||
9549 | ++failed; | ||
9550 | } | ||
9551 | /* Fail test: keep-alive */ | ||
9552 | ret = MHD_websocket_check_connection_header ("keep-alive"); | ||
9553 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9554 | { | ||
9555 | fprintf (stderr, | ||
9556 | "check_connection_header test failed in line %u.\n", | ||
9557 | (unsigned int) __LINE__); | ||
9558 | ++failed; | ||
9559 | } | ||
9560 | /* Fail test: close */ | ||
9561 | ret = MHD_websocket_check_connection_header ("close"); | ||
9562 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9563 | { | ||
9564 | fprintf (stderr, | ||
9565 | "check_connection_header test failed in line %u.\n", | ||
9566 | (unsigned int) __LINE__); | ||
9567 | ++failed; | ||
9568 | } | ||
9569 | |||
9570 | /* | ||
9571 | ------------------------------------------------------------------------------ | ||
9572 | Connection check edge cases | ||
9573 | ------------------------------------------------------------------------------ | ||
9574 | */ | ||
9575 | /* Edge test (success): keep-alive,Upgrade */ | ||
9576 | ret = MHD_websocket_check_connection_header ("keep-alive,Upgrade"); | ||
9577 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9578 | { | ||
9579 | fprintf (stderr, | ||
9580 | "check_connection_header test failed in line %u.\n", | ||
9581 | (unsigned int) __LINE__); | ||
9582 | ++failed; | ||
9583 | } | ||
9584 | /* Edge test (success): Upgrade, keep-alive */ | ||
9585 | ret = MHD_websocket_check_connection_header ("Upgrade, keep-alive"); | ||
9586 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9587 | { | ||
9588 | fprintf (stderr, | ||
9589 | "check_connection_header test failed in line %u.\n", | ||
9590 | (unsigned int) __LINE__); | ||
9591 | ++failed; | ||
9592 | } | ||
9593 | /* Edge test (success): Upgrade,keep-alive */ | ||
9594 | ret = MHD_websocket_check_connection_header ("Upgrade,keep-alive"); | ||
9595 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9596 | { | ||
9597 | fprintf (stderr, | ||
9598 | "check_connection_header test failed in line %u.\n", | ||
9599 | (unsigned int) __LINE__); | ||
9600 | ++failed; | ||
9601 | } | ||
9602 | /* Edge test (success): Transfer-Encoding,Upgrade,keep-alive */ | ||
9603 | ret = MHD_websocket_check_connection_header ("Transfer-Encoding,Upgrade,keep-alive"); | ||
9604 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9605 | { | ||
9606 | fprintf (stderr, | ||
9607 | "check_connection_header test failed in line %u.\n", | ||
9608 | (unsigned int) __LINE__); | ||
9609 | ++failed; | ||
9610 | } | ||
9611 | /* Edge test (success): Transfer-Encoding , Upgrade , keep-alive */ | ||
9612 | ret = MHD_websocket_check_connection_header ("Transfer-Encoding , Upgrade , keep-alive"); | ||
9613 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9614 | { | ||
9615 | fprintf (stderr, | ||
9616 | "check_connection_header test failed in line %u.\n", | ||
9617 | (unsigned int) __LINE__); | ||
9618 | ++failed; | ||
9619 | } | ||
9620 | /* Edge test (success): upgrade */ | ||
9621 | ret = MHD_websocket_check_connection_header ("upgrade"); | ||
9622 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9623 | { | ||
9624 | fprintf (stderr, | ||
9625 | "check_connection_header test failed in line %u.\n", | ||
9626 | (unsigned int) __LINE__); | ||
9627 | ++failed; | ||
9628 | } | ||
9629 | /* Edge test (success): UPGRADE */ | ||
9630 | ret = MHD_websocket_check_connection_header ("UPGRADE"); | ||
9631 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9632 | { | ||
9633 | fprintf (stderr, | ||
9634 | "check_connection_header test failed in line %u.\n", | ||
9635 | (unsigned int) __LINE__); | ||
9636 | ++failed; | ||
9637 | } | ||
9638 | /* Edge test (success): All allowed token characters, then upgrade token */ | ||
9639 | ret = MHD_websocket_check_connection_header ("!#$%&'*+-.^_`|~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,Upgrade"); | ||
9640 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9641 | { | ||
9642 | fprintf (stderr, | ||
9643 | "check_connection_header test failed in line %u.\n", | ||
9644 | (unsigned int) __LINE__); | ||
9645 | ++failed; | ||
9646 | } | ||
9647 | /* Edge test (success): Different, allowed whitespaces */ | ||
9648 | ret = MHD_websocket_check_connection_header (" \tUpgrade \t"); | ||
9649 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9650 | { | ||
9651 | fprintf (stderr, | ||
9652 | "check_connection_header test failed in line %u.\n", | ||
9653 | (unsigned int) __LINE__); | ||
9654 | ++failed; | ||
9655 | } | ||
9656 | /* Edge test (fail): Different, disallowed whitespaces */ | ||
9657 | ret = MHD_websocket_check_connection_header ("\rUpgrade"); | ||
9658 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9659 | { | ||
9660 | fprintf (stderr, | ||
9661 | "check_connection_header test failed in line %u.\n", | ||
9662 | (unsigned int) __LINE__); | ||
9663 | ++failed; | ||
9664 | } | ||
9665 | /* Edge test (fail): Different, disallowed whitespaces */ | ||
9666 | ret = MHD_websocket_check_connection_header ("\nUpgrade"); | ||
9667 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9668 | { | ||
9669 | fprintf (stderr, | ||
9670 | "check_connection_header test failed in line %u.\n", | ||
9671 | (unsigned int) __LINE__); | ||
9672 | ++failed; | ||
9673 | } | ||
9674 | /* Edge test (fail): Different, disallowed whitespaces */ | ||
9675 | ret = MHD_websocket_check_connection_header ("\vUpgrade"); | ||
9676 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9677 | { | ||
9678 | fprintf (stderr, | ||
9679 | "check_connection_header test failed in line %u.\n", | ||
9680 | (unsigned int) __LINE__); | ||
9681 | ++failed; | ||
9682 | } | ||
9683 | /* Edge test (fail): Different, disallowed whitespaces */ | ||
9684 | ret = MHD_websocket_check_connection_header ("\fUpgrade"); | ||
9685 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9686 | { | ||
9687 | fprintf (stderr, | ||
9688 | "check_connection_header test failed in line %u.\n", | ||
9689 | (unsigned int) __LINE__); | ||
9690 | ++failed; | ||
9691 | } | ||
9692 | |||
9693 | /* | ||
9694 | ------------------------------------------------------------------------------ | ||
9695 | Invalid header syntax | ||
9696 | ------------------------------------------------------------------------------ | ||
9697 | */ | ||
9698 | /* Fail test: (empty string) */ | ||
9699 | ret = MHD_websocket_check_connection_header (""); | ||
9700 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9701 | { | ||
9702 | fprintf (stderr, | ||
9703 | "check_connection_header test failed in line %u.\n", | ||
9704 | (unsigned int) __LINE__); | ||
9705 | ++failed; | ||
9706 | } | ||
9707 | /* Fail test: (Disallowed) multiple word token with the term "Upgrade" in it */ | ||
9708 | ret = MHD_websocket_check_connection_header ("Upgrade or Downgrade"); | ||
9709 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9710 | { | ||
9711 | fprintf (stderr, | ||
9712 | "check_connection_header test failed in line %u.\n", | ||
9713 | (unsigned int) __LINE__); | ||
9714 | ++failed; | ||
9715 | } | ||
9716 | /* Fail test: Invalid characters */ | ||
9717 | ret = MHD_websocket_check_connection_header ("\"Upgrade\""); | ||
9718 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9719 | { | ||
9720 | fprintf (stderr, | ||
9721 | "check_connection_header test failed in line %u.\n", | ||
9722 | (unsigned int) __LINE__); | ||
9723 | ++failed; | ||
9724 | } | ||
9725 | |||
9726 | /* | ||
9727 | ------------------------------------------------------------------------------ | ||
9728 | Missing parameters | ||
9729 | ------------------------------------------------------------------------------ | ||
9730 | */ | ||
9731 | /* Fail test: NULL as connection */ | ||
9732 | ret = MHD_websocket_check_connection_header (NULL); | ||
9733 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9734 | { | ||
9735 | fprintf (stderr, | ||
9736 | "check_connection_header test failed in line %u.\n", | ||
9737 | (unsigned int) __LINE__); | ||
9738 | ++failed; | ||
9739 | } | ||
9740 | |||
9741 | return failed != 0 ? 0x400 : 0x00; | ||
9742 | } | ||
9743 | |||
9744 | |||
9745 | /** | ||
9746 | * Test procedure for `MHD_websocket_check_upgrade_header()` | ||
9747 | */ | ||
9748 | int | ||
9749 | test_check_upgrade_header () | ||
9750 | { | ||
9751 | int failed = 0; | ||
9752 | int ret; | ||
9753 | |||
9754 | /* | ||
9755 | ------------------------------------------------------------------------------ | ||
9756 | Check with valid Upgrade header syntax | ||
9757 | ------------------------------------------------------------------------------ | ||
9758 | */ | ||
9759 | /* Regular test: websocket */ | ||
9760 | ret = MHD_websocket_check_upgrade_header ("websocket"); | ||
9761 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9762 | { | ||
9763 | fprintf (stderr, | ||
9764 | "check_upgrade_header test failed in line %u.\n", | ||
9765 | (unsigned int) __LINE__); | ||
9766 | ++failed; | ||
9767 | } | ||
9768 | /* Fail test: HTTP/2.0 */ | ||
9769 | ret = MHD_websocket_check_upgrade_header ("HTTP/2.0"); | ||
9770 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9771 | { | ||
9772 | fprintf (stderr, | ||
9773 | "check_upgrade_header test failed in line %u.\n", | ||
9774 | (unsigned int) __LINE__); | ||
9775 | ++failed; | ||
9776 | } | ||
9777 | |||
9778 | /* | ||
9779 | ------------------------------------------------------------------------------ | ||
9780 | Upgrade check edge cases | ||
9781 | ------------------------------------------------------------------------------ | ||
9782 | */ | ||
9783 | /* Edge test (success): websocket,HTTP/2.0 */ | ||
9784 | ret = MHD_websocket_check_upgrade_header ("websocket,HTTP/2.0"); | ||
9785 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9786 | { | ||
9787 | fprintf (stderr, | ||
9788 | "check_upgrade_header test failed in line %u.\n", | ||
9789 | (unsigned int) __LINE__); | ||
9790 | ++failed; | ||
9791 | } | ||
9792 | /* Edge test (success): websocket ,HTTP/2.0 */ | ||
9793 | ret = MHD_websocket_check_upgrade_header (" websocket ,HTTP/2.0"); | ||
9794 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9795 | { | ||
9796 | fprintf (stderr, | ||
9797 | "check_upgrade_header test failed in line %u.\n", | ||
9798 | (unsigned int) __LINE__); | ||
9799 | ++failed; | ||
9800 | } | ||
9801 | /* Edge test (success): HTTP/2.0, websocket */ | ||
9802 | ret = MHD_websocket_check_upgrade_header ("HTTP/2.0, websocket "); | ||
9803 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9804 | { | ||
9805 | fprintf (stderr, | ||
9806 | "check_upgrade_header test failed in line %u.\n", | ||
9807 | (unsigned int) __LINE__); | ||
9808 | ++failed; | ||
9809 | } | ||
9810 | /* Edge test (fail): websocket/13 */ | ||
9811 | ret = MHD_websocket_check_upgrade_header ("websocket/13"); | ||
9812 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9813 | { | ||
9814 | fprintf (stderr, | ||
9815 | "check_upgrade_header test failed in line %u.\n", | ||
9816 | (unsigned int) __LINE__); | ||
9817 | ++failed; | ||
9818 | } | ||
9819 | /* Edge test (success): WeBsOcKeT */ | ||
9820 | ret = MHD_websocket_check_upgrade_header ("WeBsOcKeT"); | ||
9821 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9822 | { | ||
9823 | fprintf (stderr, | ||
9824 | "check_upgrade_header test failed in line %u.\n", | ||
9825 | (unsigned int) __LINE__); | ||
9826 | ++failed; | ||
9827 | } | ||
9828 | /* Edge test (success): WEBSOCKET */ | ||
9829 | ret = MHD_websocket_check_upgrade_header ("WEBSOCKET"); | ||
9830 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9831 | { | ||
9832 | fprintf (stderr, | ||
9833 | "check_upgrade_header test failed in line %u.\n", | ||
9834 | (unsigned int) __LINE__); | ||
9835 | ++failed; | ||
9836 | } | ||
9837 | /* Edge test (success): All allowed token characters plus /, then websocket keyowrd */ | ||
9838 | ret = MHD_websocket_check_upgrade_header ("!#$%&'*+-.^_`|~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/,websocket"); | ||
9839 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9840 | { | ||
9841 | fprintf (stderr, | ||
9842 | "check_upgrade_header test failed in line %u.\n", | ||
9843 | (unsigned int) __LINE__); | ||
9844 | ++failed; | ||
9845 | } | ||
9846 | /* Edge test (success): Different, allowed whitespaces */ | ||
9847 | ret = MHD_websocket_check_upgrade_header (" \twebsocket \t"); | ||
9848 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9849 | { | ||
9850 | fprintf (stderr, | ||
9851 | "check_upgrade_header test failed in line %u.\n", | ||
9852 | (unsigned int) __LINE__); | ||
9853 | ++failed; | ||
9854 | } | ||
9855 | /* Edge test (fail): Different, disallowed whitespaces */ | ||
9856 | ret = MHD_websocket_check_upgrade_header ("\rwebsocket"); | ||
9857 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9858 | { | ||
9859 | fprintf (stderr, | ||
9860 | "check_upgrade_header test failed in line %u.\n", | ||
9861 | (unsigned int) __LINE__); | ||
9862 | ++failed; | ||
9863 | } | ||
9864 | /* Edge test (fail): Different, disallowed whitespaces */ | ||
9865 | ret = MHD_websocket_check_upgrade_header ("\nwebsocket"); | ||
9866 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9867 | { | ||
9868 | fprintf (stderr, | ||
9869 | "check_upgrade_header test failed in line %u.\n", | ||
9870 | (unsigned int) __LINE__); | ||
9871 | ++failed; | ||
9872 | } | ||
9873 | /* Edge test (fail): Different, disallowed whitespaces */ | ||
9874 | ret = MHD_websocket_check_upgrade_header ("\vwebsocket"); | ||
9875 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9876 | { | ||
9877 | fprintf (stderr, | ||
9878 | "check_upgrade_header test failed in line %u.\n", | ||
9879 | (unsigned int) __LINE__); | ||
9880 | ++failed; | ||
9881 | } | ||
9882 | /* Edge test (fail): Different, disallowed whitespaces */ | ||
9883 | ret = MHD_websocket_check_upgrade_header ("\fwebsocket"); | ||
9884 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9885 | { | ||
9886 | fprintf (stderr, | ||
9887 | "check_upgrade_header test failed in line %u.\n", | ||
9888 | (unsigned int) __LINE__); | ||
9889 | ++failed; | ||
9890 | } | ||
9891 | |||
9892 | /* | ||
9893 | ------------------------------------------------------------------------------ | ||
9894 | Invalid header syntax | ||
9895 | ------------------------------------------------------------------------------ | ||
9896 | */ | ||
9897 | /* Fail test: (empty string) */ | ||
9898 | ret = MHD_websocket_check_upgrade_header (""); | ||
9899 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9900 | { | ||
9901 | fprintf (stderr, | ||
9902 | "check_upgrade_header test failed in line %u.\n", | ||
9903 | (unsigned int) __LINE__); | ||
9904 | ++failed; | ||
9905 | } | ||
9906 | /* Fail test: (Disallowed) multiple word token with the term "websocket" in it */ | ||
9907 | ret = MHD_websocket_check_upgrade_header ("websocket or something"); | ||
9908 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9909 | { | ||
9910 | fprintf (stderr, | ||
9911 | "check_upgrade_header test failed in line %u.\n", | ||
9912 | (unsigned int) __LINE__); | ||
9913 | ++failed; | ||
9914 | } | ||
9915 | /* Fail test: Invalid characters */ | ||
9916 | ret = MHD_websocket_check_upgrade_header ("\"websocket\""); | ||
9917 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9918 | { | ||
9919 | fprintf (stderr, | ||
9920 | "check_upgrade_header test failed in line %u.\n", | ||
9921 | (unsigned int) __LINE__); | ||
9922 | ++failed; | ||
9923 | } | ||
9924 | |||
9925 | /* | ||
9926 | ------------------------------------------------------------------------------ | ||
9927 | Missing parameters | ||
9928 | ------------------------------------------------------------------------------ | ||
9929 | */ | ||
9930 | /* Fail test: NULL as upgrade */ | ||
9931 | ret = MHD_websocket_check_upgrade_header (NULL); | ||
9932 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9933 | { | ||
9934 | fprintf (stderr, | ||
9935 | "check_upgrade_header test failed in line %u.\n", | ||
9936 | (unsigned int) __LINE__); | ||
9937 | ++failed; | ||
9938 | } | ||
9939 | |||
9940 | return failed != 0 ? 0x800 : 0x00; | ||
9941 | } | ||
9942 | |||
9943 | |||
9944 | /** | ||
9945 | * Test procedure for `MHD_websocket_check_version_header()` | ||
9946 | */ | ||
9947 | int | ||
9948 | test_check_version_header () | ||
9949 | { | ||
9950 | int failed = 0; | ||
9951 | int ret; | ||
9952 | |||
9953 | /* | ||
9954 | ------------------------------------------------------------------------------ | ||
9955 | Check with valid Upgrade header syntax | ||
9956 | ------------------------------------------------------------------------------ | ||
9957 | */ | ||
9958 | /* Regular test: 13 */ | ||
9959 | ret = MHD_websocket_check_version_header ("13"); | ||
9960 | if (MHD_WEBSOCKET_STATUS_OK != ret) | ||
9961 | { | ||
9962 | fprintf (stderr, | ||
9963 | "check_version_header test failed in line %u.\n", | ||
9964 | (unsigned int) __LINE__); | ||
9965 | ++failed; | ||
9966 | } | ||
9967 | |||
9968 | /* | ||
9969 | ------------------------------------------------------------------------------ | ||
9970 | Version check edge cases | ||
9971 | ------------------------------------------------------------------------------ | ||
9972 | */ | ||
9973 | /* Edge test (fail): 14 */ | ||
9974 | ret = MHD_websocket_check_version_header ("14"); | ||
9975 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9976 | { | ||
9977 | fprintf (stderr, | ||
9978 | "check_version_header test failed in line %u.\n", | ||
9979 | (unsigned int) __LINE__); | ||
9980 | ++failed; | ||
9981 | } | ||
9982 | /* Edge test (fail): 12 */ | ||
9983 | ret = MHD_websocket_check_version_header ("12"); | ||
9984 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9985 | { | ||
9986 | fprintf (stderr, | ||
9987 | "check_version_header test failed in line %u.\n", | ||
9988 | (unsigned int) __LINE__); | ||
9989 | ++failed; | ||
9990 | } | ||
9991 | /* Edge test (fail): 0 */ | ||
9992 | ret = MHD_websocket_check_version_header ("1"); | ||
9993 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
9994 | { | ||
9995 | fprintf (stderr, | ||
9996 | "check_version_header test failed in line %u.\n", | ||
9997 | (unsigned int) __LINE__); | ||
9998 | ++failed; | ||
9999 | } | ||
10000 | /* Edge test (fail): 1 */ | ||
10001 | ret = MHD_websocket_check_version_header ("1"); | ||
10002 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
10003 | { | ||
10004 | fprintf (stderr, | ||
10005 | "check_version_header test failed in line %u.\n", | ||
10006 | (unsigned int) __LINE__); | ||
10007 | ++failed; | ||
10008 | } | ||
10009 | /* Edge test (fail): 130 */ | ||
10010 | ret = MHD_websocket_check_version_header ("130"); | ||
10011 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
10012 | { | ||
10013 | fprintf (stderr, | ||
10014 | "check_version_header test failed in line %u.\n", | ||
10015 | (unsigned int) __LINE__); | ||
10016 | ++failed; | ||
10017 | } | ||
10018 | /* Edge test (fail): " 13" */ | ||
10019 | ret = MHD_websocket_check_version_header (" 13"); | ||
10020 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
10021 | { | ||
10022 | fprintf (stderr, | ||
10023 | "check_version_header test failed in line %u.\n", | ||
10024 | (unsigned int) __LINE__); | ||
10025 | ++failed; | ||
10026 | } | ||
10027 | |||
10028 | /* | ||
10029 | ------------------------------------------------------------------------------ | ||
10030 | Invalid header syntax | ||
10031 | ------------------------------------------------------------------------------ | ||
10032 | */ | ||
10033 | /* Fail test: (empty string) */ | ||
10034 | ret = MHD_websocket_check_version_header (""); | ||
10035 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
10036 | { | ||
10037 | fprintf (stderr, | ||
10038 | "check_version_header test failed in line %u.\n", | ||
10039 | (unsigned int) __LINE__); | ||
10040 | ++failed; | ||
10041 | } | ||
10042 | /* Fail test: Invalid characters */ | ||
10043 | ret = MHD_websocket_check_version_header ("abc"); | ||
10044 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
10045 | { | ||
10046 | fprintf (stderr, | ||
10047 | "check_version_header test failed in line %u.\n", | ||
10048 | (unsigned int) __LINE__); | ||
10049 | ++failed; | ||
10050 | } | ||
10051 | |||
10052 | /* | ||
10053 | ------------------------------------------------------------------------------ | ||
10054 | Missing parameters | ||
10055 | ------------------------------------------------------------------------------ | ||
10056 | */ | ||
10057 | /* Fail test: NULL as version */ | ||
10058 | ret = MHD_websocket_check_version_header (NULL); | ||
10059 | if (MHD_WEBSOCKET_STATUS_NO_WEBSOCKET_HANDSHAKE_HEADER != ret) | ||
10060 | { | ||
10061 | fprintf (stderr, | ||
10062 | "check_version_header test failed in line %u.\n", | ||
10063 | (unsigned int) __LINE__); | ||
10064 | ++failed; | ||
10065 | } | ||
10066 | |||
10067 | return failed != 0 ? 0x1000 : 0x00; | ||
10068 | } | ||
10069 | |||
10070 | |||
8958 | int | 10071 | int |
8959 | main (int argc, char *const *argv) | 10072 | main (int argc, char *const *argv) |
8960 | { | 10073 | { |
@@ -8962,7 +10075,7 @@ main (int argc, char *const *argv) | |||
8962 | (void) argc; (void) argv; /* Unused. Silent compiler warning. */ | 10075 | (void) argc; (void) argv; /* Unused. Silent compiler warning. */ |
8963 | 10076 | ||
8964 | /* seed random number generator */ | 10077 | /* seed random number generator */ |
8965 | MHD_websocket_srand ((unsigned long) time (NULL)); | 10078 | srand ((unsigned long) time (NULL)); |
8966 | 10079 | ||
8967 | /* perform tests */ | 10080 | /* perform tests */ |
8968 | errorCount += test_inits (); | 10081 | errorCount += test_inits (); |
@@ -8974,6 +10087,10 @@ main (int argc, char *const *argv) | |||
8974 | errorCount += test_encodes_ping (); | 10087 | errorCount += test_encodes_ping (); |
8975 | errorCount += test_encodes_pong (); | 10088 | errorCount += test_encodes_pong (); |
8976 | errorCount += test_split_close_reason (); | 10089 | errorCount += test_split_close_reason (); |
10090 | errorCount += test_check_http_version (); | ||
10091 | errorCount += test_check_connection_header (); | ||
10092 | errorCount += test_check_upgrade_header (); | ||
10093 | errorCount += test_check_version_header (); | ||
8977 | 10094 | ||
8978 | /* output result */ | 10095 | /* output result */ |
8979 | if (errorCount != 0) | 10096 | if (errorCount != 0) |