diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-01-13 22:16:19 +0300 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-01-13 22:16:19 +0300 |
commit | 376afc84c2522c0fdffd67dadf625af05dcd2c5a (patch) | |
tree | 3a448891b67dce8b7aecacac7c95f7a8fed94b4a | |
parent | b09dc373013822a7446515c41bd550cb5336e8ee (diff) | |
download | libmicrohttpd-376afc84c2522c0fdffd67dadf625af05dcd2c5a.tar.gz libmicrohttpd-376afc84c2522c0fdffd67dadf625af05dcd2c5a.zip |
test_upgrade: added detailed error reporting and add some check for return values
-rw-r--r-- | src/microhttpd/test_upgrade.c | 319 |
1 files changed, 262 insertions, 57 deletions
diff --git a/src/microhttpd/test_upgrade.c b/src/microhttpd/test_upgrade.c index 06e66a3c..152bc85b 100644 --- a/src/microhttpd/test_upgrade.c +++ b/src/microhttpd/test_upgrade.c | |||
@@ -59,6 +59,110 @@ | |||
59 | #endif /* HAVE_FORK && HAVE_WAITPID */ | 59 | #endif /* HAVE_FORK && HAVE_WAITPID */ |
60 | #endif /* HTTPS_SUPPORT */ | 60 | #endif /* HTTPS_SUPPORT */ |
61 | 61 | ||
62 | |||
63 | _MHD_NORETURN static void | ||
64 | _externalErrorExit_func (const char *errDesc, const char *funcName, int lineNum) | ||
65 | { | ||
66 | fflush (stdout); | ||
67 | if ((NULL != errDesc) && (0 != errDesc[0])) | ||
68 | fprintf (stderr, "%s", errDesc); | ||
69 | else | ||
70 | fprintf (stderr, "System or external library call failed"); | ||
71 | if ((NULL != funcName) && (0 != funcName[0])) | ||
72 | fprintf (stderr, " in %s", funcName); | ||
73 | if (0 < lineNum) | ||
74 | fprintf (stderr, " at line %d", lineNum); | ||
75 | |||
76 | fprintf (stderr, ".\nLast errno value: %d (%s)\n", (int) errno, | ||
77 | strerror (errno)); | ||
78 | #ifdef MHD_WINSOCK_SOCKETS | ||
79 | fprintf (stderr, "WSAGetLastError() value: %d\n", (int) WSAGetLastError ()); | ||
80 | #endif /* MHD_WINSOCK_SOCKETS */ | ||
81 | fflush (stderr); | ||
82 | exit (99); | ||
83 | } | ||
84 | |||
85 | |||
86 | _MHD_NORETURN static void | ||
87 | _mhdErrorExit_func (const char *errDesc, const char *funcName, int lineNum) | ||
88 | { | ||
89 | fflush (stdout); | ||
90 | if ((NULL != errDesc) && (0 != errDesc[0])) | ||
91 | fprintf (stderr, "%s", errDesc); | ||
92 | else | ||
93 | fprintf (stderr, "MHD unexpected error"); | ||
94 | if ((NULL != funcName) && (0 != funcName[0])) | ||
95 | fprintf (stderr, " in %s", funcName); | ||
96 | if (0 < lineNum) | ||
97 | fprintf (stderr, " at line %d", lineNum); | ||
98 | |||
99 | fprintf (stderr, ".\nLast errno value: %d (%s)\n", (int) errno, | ||
100 | strerror (errno)); | ||
101 | |||
102 | fflush (stderr); | ||
103 | exit (8); | ||
104 | } | ||
105 | |||
106 | |||
107 | static void | ||
108 | _testErrorLog_func (const char *errDesc, const char *funcName, int lineNum) | ||
109 | { | ||
110 | fflush (stdout); | ||
111 | if ((NULL != errDesc) && (0 != errDesc[0])) | ||
112 | fprintf (stderr, "%s", errDesc); | ||
113 | else | ||
114 | fprintf (stderr, "System or external library call resulted in error"); | ||
115 | if ((NULL != funcName) && (0 != funcName[0])) | ||
116 | fprintf (stderr, " in %s", funcName); | ||
117 | if (0 < lineNum) | ||
118 | fprintf (stderr, " at line %d", lineNum); | ||
119 | |||
120 | fprintf (stderr, ".\nLast errno value: %d (%s)\n", (int) errno, | ||
121 | strerror (errno)); | ||
122 | #ifdef MHD_WINSOCK_SOCKETS | ||
123 | fprintf (stderr, "WSAGetLastError() value: %d\n", (int) WSAGetLastError ()); | ||
124 | #endif /* MHD_WINSOCK_SOCKETS */ | ||
125 | fflush (stderr); | ||
126 | } | ||
127 | |||
128 | |||
129 | #if defined(HAVE___FUNC__) | ||
130 | #define externalErrorExit(ignore) \ | ||
131 | _externalErrorExit_func(NULL, __func__, __LINE__) | ||
132 | #define externalErrorExitDesc(errDesc) \ | ||
133 | _externalErrorExit_func(errDesc, __func__, __LINE__) | ||
134 | #define mhdErrorExit(ignore) \ | ||
135 | _mhdErrorExit_func(NULL, __func__, __LINE__) | ||
136 | #define mhdErrorExitDesc(errDesc) \ | ||
137 | _mhdErrorExit_func(errDesc, __func__, __LINE__) | ||
138 | #define testErrorLog(ignore) \ | ||
139 | _testErrorLog_func(NULL, __func__, __LINE__) | ||
140 | #define testErrorLogDesc(errDesc) \ | ||
141 | _testErrorLog_func(errDesc, __func__, __LINE__) | ||
142 | #elif defined(HAVE___FUNCTION__) | ||
143 | #define externalErrorExit(ignore) \ | ||
144 | _externalErrorExit_func(NULL, __FUNCTION__, __LINE__) | ||
145 | #define externalErrorExitDesc(errDesc) \ | ||
146 | _externalErrorExit_func(errDesc, __FUNCTION__, __LINE__) | ||
147 | #define mhdErrorExit(ignore) \ | ||
148 | _mhdErrorExit_func(NULL, __FUNCTION__, __LINE__) | ||
149 | #define mhdErrorExitDesc(errDesc) \ | ||
150 | _mhdErrorExit_func(errDesc, __FUNCTION__, __LINE__) | ||
151 | #define testErrorLog(ignore) \ | ||
152 | _testErrorLog_func(NULL, __FUNCTION__, __LINE__) | ||
153 | #define testErrorLogDesc(errDesc) \ | ||
154 | _testErrorLog_func(errDesc, __FUNCTION__, __LINE__) | ||
155 | #else | ||
156 | #define externalErrorExit(ignore) _externalErrorExit_func(NULL, NULL, __LINE__) | ||
157 | #define externalErrorExitDesc(errDesc) \ | ||
158 | _externalErrorExit_func(errDesc, NULL, __LINE__) | ||
159 | #define mhdErrorExit(ignore) _mhdErrorExit_func(NULL, NULL, __LINE__) | ||
160 | #define mhdErrorExitDesc(errDesc) _mhdErrorExit_func(errDesc, NULL, __LINE__) | ||
161 | #define testErrorLog(ignore) _testErrorLog_func(NULL, NULL, __LINE__) | ||
162 | #define testErrorLogDesc(errDesc) _testErrorLog_func(errDesc, NULL, __LINE__) | ||
163 | #endif | ||
164 | |||
165 | |||
62 | static int verbose = 0; | 166 | static int verbose = 0; |
63 | 167 | ||
64 | enum tls_tool | 168 | enum tls_tool |
@@ -198,11 +302,15 @@ wr_create_plain_sckt (void) | |||
198 | { | 302 | { |
199 | struct wr_socket *s = malloc (sizeof(struct wr_socket)); | 303 | struct wr_socket *s = malloc (sizeof(struct wr_socket)); |
200 | if (NULL == s) | 304 | if (NULL == s) |
305 | { | ||
306 | testErrorLogDesc ("malloc() failed"); | ||
201 | return NULL; | 307 | return NULL; |
308 | } | ||
202 | s->t = wr_plain; | 309 | s->t = wr_plain; |
203 | s->fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); | 310 | s->fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); |
204 | if (MHD_INVALID_SOCKET != s->fd) | 311 | if (MHD_INVALID_SOCKET != s->fd) |
205 | return s; | 312 | return s; /* Success */ |
313 | testErrorLogDesc ("socket() failed"); | ||
206 | free (s); | 314 | free (s); |
207 | return NULL; | 315 | return NULL; |
208 | } | 316 | } |
@@ -218,7 +326,10 @@ wr_create_tls_sckt (void) | |||
218 | #ifdef HTTPS_SUPPORT | 326 | #ifdef HTTPS_SUPPORT |
219 | struct wr_socket *s = malloc (sizeof(struct wr_socket)); | 327 | struct wr_socket *s = malloc (sizeof(struct wr_socket)); |
220 | if (NULL == s) | 328 | if (NULL == s) |
329 | { | ||
330 | testErrorLogDesc ("malloc() failed"); | ||
221 | return NULL; | 331 | return NULL; |
332 | } | ||
222 | s->t = wr_tls; | 333 | s->t = wr_tls; |
223 | s->tls_connected = 0; | 334 | s->tls_connected = 0; |
224 | s->fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); | 335 | s->fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); |
@@ -243,13 +354,23 @@ wr_create_tls_sckt (void) | |||
243 | #endif /* GnuTLS before 3.1.9 or Win x64 */ | 354 | #endif /* GnuTLS before 3.1.9 or Win x64 */ |
244 | return s; | 355 | return s; |
245 | } | 356 | } |
357 | else | ||
358 | testErrorLogDesc ("gnutls_credentials_set() failed"); | ||
246 | gnutls_certificate_free_credentials (s->tls_crd); | 359 | gnutls_certificate_free_credentials (s->tls_crd); |
247 | } | 360 | } |
361 | else | ||
362 | testErrorLogDesc ("gnutls_certificate_allocate_credentials() failed"); | ||
248 | } | 363 | } |
364 | else | ||
365 | testErrorLogDesc ("gnutls_set_default_priority() failed"); | ||
249 | gnutls_deinit (s->tls_s); | 366 | gnutls_deinit (s->tls_s); |
250 | } | 367 | } |
368 | else | ||
369 | testErrorLogDesc ("gnutls_init() failed"); | ||
251 | (void) MHD_socket_close_ (s->fd); | 370 | (void) MHD_socket_close_ (s->fd); |
252 | } | 371 | } |
372 | else | ||
373 | testErrorLogDesc ("socket() failed"); | ||
253 | free (s); | 374 | free (s); |
254 | #endif /* HTTPS_SUPPORT */ | 375 | #endif /* HTTPS_SUPPORT */ |
255 | return NULL; | 376 | return NULL; |
@@ -268,7 +389,10 @@ wr_create_from_plain_sckt (MHD_socket plain_sk) | |||
268 | struct wr_socket *s = malloc (sizeof(struct wr_socket)); | 389 | struct wr_socket *s = malloc (sizeof(struct wr_socket)); |
269 | 390 | ||
270 | if (NULL == s) | 391 | if (NULL == s) |
392 | { | ||
393 | testErrorLogDesc ("malloc() failed"); | ||
271 | return NULL; | 394 | return NULL; |
395 | } | ||
272 | s->t = wr_plain; | 396 | s->t = wr_plain; |
273 | s->fd = plain_sk; | 397 | s->fd = plain_sk; |
274 | return s; | 398 | return s; |
@@ -288,7 +412,10 @@ wr_connect (struct wr_socket *s, | |||
288 | int length) | 412 | int length) |
289 | { | 413 | { |
290 | if (0 != connect (s->fd, addr, length)) | 414 | if (0 != connect (s->fd, addr, length)) |
415 | { | ||
416 | testErrorLogDesc ("connect() failed"); | ||
291 | return -1; | 417 | return -1; |
418 | } | ||
292 | if (wr_plain == s->t) | 419 | if (wr_plain == s->t) |
293 | return 0; | 420 | return 0; |
294 | #ifdef HTTPS_SUPPORT | 421 | #ifdef HTTPS_SUPPORT |
@@ -303,6 +430,8 @@ wr_connect (struct wr_socket *s, | |||
303 | return 0; | 430 | return 0; |
304 | } | 431 | } |
305 | #endif /* HTTPS_SUPPORT */ | 432 | #endif /* HTTPS_SUPPORT */ |
433 | testErrorLogDesc ("HTTPS socket connect called, but code does not support" \ | ||
434 | " HTTPS sockets"); | ||
306 | return -1; | 435 | return -1; |
307 | } | 436 | } |
308 | 437 | ||
@@ -318,7 +447,10 @@ wr_handshake (struct wr_socket *s) | |||
318 | else if (GNUTLS_E_AGAIN == res) | 447 | else if (GNUTLS_E_AGAIN == res) |
319 | MHD_socket_set_error_ (MHD_SCKT_EAGAIN_); | 448 | MHD_socket_set_error_ (MHD_SCKT_EAGAIN_); |
320 | else | 449 | else |
450 | { | ||
451 | testErrorLogDesc ("gnutls_handshake() failed with hard error"); | ||
321 | MHD_socket_set_error_ (MHD_SCKT_ECONNABORTED_); /* hard error */ | 452 | MHD_socket_set_error_ (MHD_SCKT_ECONNABORTED_); /* hard error */ |
453 | } | ||
322 | return s->tls_connected; | 454 | return s->tls_connected; |
323 | } | 455 | } |
324 | 456 | ||
@@ -355,9 +487,15 @@ wr_send (struct wr_socket *s, | |||
355 | if (GNUTLS_E_AGAIN == ret) | 487 | if (GNUTLS_E_AGAIN == ret) |
356 | MHD_socket_set_error_ (MHD_SCKT_EAGAIN_); | 488 | MHD_socket_set_error_ (MHD_SCKT_EAGAIN_); |
357 | else | 489 | else |
490 | { | ||
491 | testErrorLogDesc ("gnutls_record_send() failed with hard error"); | ||
358 | MHD_socket_set_error_ (MHD_SCKT_ECONNABORTED_); /* hard error */ | 492 | MHD_socket_set_error_ (MHD_SCKT_ECONNABORTED_); /* hard error */ |
493 | return -1; | ||
494 | } | ||
359 | } | 495 | } |
360 | #endif /* HTTPS_SUPPORT */ | 496 | #endif /* HTTPS_SUPPORT */ |
497 | testErrorLogDesc ("HTTPS socket send called, but code does not support" \ | ||
498 | " HTTPS sockets"); | ||
361 | return -1; | 499 | return -1; |
362 | } | 500 | } |
363 | 501 | ||
@@ -391,7 +529,11 @@ wr_recv (struct wr_socket *s, | |||
391 | if (GNUTLS_E_AGAIN == ret) | 529 | if (GNUTLS_E_AGAIN == ret) |
392 | MHD_socket_set_error_ (MHD_SCKT_EAGAIN_); | 530 | MHD_socket_set_error_ (MHD_SCKT_EAGAIN_); |
393 | else | 531 | else |
532 | { | ||
533 | testErrorLogDesc ("gnutls_record_recv() failed with hard error"); | ||
394 | MHD_socket_set_error_ (MHD_SCKT_ECONNABORTED_); /* hard error */ | 534 | MHD_socket_set_error_ (MHD_SCKT_ECONNABORTED_); /* hard error */ |
535 | return -1; | ||
536 | } | ||
395 | } | 537 | } |
396 | #endif /* HTTPS_SUPPORT */ | 538 | #endif /* HTTPS_SUPPORT */ |
397 | return -1; | 539 | return -1; |
@@ -440,6 +582,32 @@ static pthread_t pt_client; | |||
440 | static volatile bool done; | 582 | static volatile bool done; |
441 | 583 | ||
442 | 584 | ||
585 | const char * | ||
586 | term_reason_str (enum MHD_RequestTerminationCode term_code) | ||
587 | { | ||
588 | switch ((int) term_code) | ||
589 | { | ||
590 | case MHD_REQUEST_TERMINATED_COMPLETED_OK: | ||
591 | return "COMPLETED_OK"; | ||
592 | case MHD_REQUEST_TERMINATED_WITH_ERROR: | ||
593 | return "TERMINATED_WITH_ERROR"; | ||
594 | case MHD_REQUEST_TERMINATED_TIMEOUT_REACHED: | ||
595 | return "TIMEOUT_REACHED"; | ||
596 | case MHD_REQUEST_TERMINATED_DAEMON_SHUTDOWN: | ||
597 | return "DAEMON_SHUTDOWN"; | ||
598 | case MHD_REQUEST_TERMINATED_READ_ERROR: | ||
599 | return "READ_ERROR"; | ||
600 | case MHD_REQUEST_TERMINATED_CLIENT_ABORT: | ||
601 | return "CLIENT_ABORT"; | ||
602 | case -1: | ||
603 | return "(not called)"; | ||
604 | default: | ||
605 | return "(unknown code)"; | ||
606 | } | ||
607 | return "(problem)"; /* unreachable */ | ||
608 | } | ||
609 | |||
610 | |||
443 | /** | 611 | /** |
444 | * Callback used by MHD to notify the application about completed | 612 | * Callback used by MHD to notify the application about completed |
445 | * requests. Frees memory. | 613 | * requests. Frees memory. |
@@ -456,19 +624,23 @@ notify_completed_cb (void *cls, | |||
456 | void **con_cls, | 624 | void **con_cls, |
457 | enum MHD_RequestTerminationCode toe) | 625 | enum MHD_RequestTerminationCode toe) |
458 | { | 626 | { |
459 | pthread_t *ppth = *con_cls; | ||
460 | |||
461 | (void) cls; | 627 | (void) cls; |
462 | (void) connection; /* Unused. Silent compiler warning. */ | 628 | (void) connection; /* Unused. Silent compiler warning. */ |
629 | if (verbose) | ||
630 | printf ("notify_completed_cb() has been called with '%s' code.\n", | ||
631 | term_reason_str (toe)); | ||
463 | if ( (toe != MHD_REQUEST_TERMINATED_COMPLETED_OK) && | 632 | if ( (toe != MHD_REQUEST_TERMINATED_COMPLETED_OK) && |
464 | (toe != MHD_REQUEST_TERMINATED_CLIENT_ABORT) && | 633 | (toe != MHD_REQUEST_TERMINATED_CLIENT_ABORT) && |
465 | (toe != MHD_REQUEST_TERMINATED_DAEMON_SHUTDOWN) ) | 634 | (toe != MHD_REQUEST_TERMINATED_DAEMON_SHUTDOWN) ) |
466 | abort (); | 635 | mhdErrorExitDesc ("notify_completed_cb() called with wrong code"); |
636 | if (NULL == con_cls) | ||
637 | mhdErrorExitDesc ("'con_cls' parameter is NULL"); | ||
638 | if (NULL == *con_cls) | ||
639 | mhdErrorExitDesc ("'*con_cls' pointer is NULL"); | ||
467 | if (! pthread_equal (**((pthread_t **) con_cls), | 640 | if (! pthread_equal (**((pthread_t **) con_cls), |
468 | pthread_self ())) | 641 | pthread_self ())) |
469 | abort (); | 642 | mhdErrorExitDesc ("notify_completed_cb() is called in wrong thread"); |
470 | if (NULL != ppth) | 643 | free (*con_cls); |
471 | free (*con_cls); | ||
472 | *con_cls = NULL; | 644 | *con_cls = NULL; |
473 | } | 645 | } |
474 | 646 | ||
@@ -490,12 +662,16 @@ log_cb (void *cls, | |||
490 | 662 | ||
491 | (void) cls; | 663 | (void) cls; |
492 | (void) connection; /* Unused. Silent compiler warning. */ | 664 | (void) connection; /* Unused. Silent compiler warning. */ |
493 | if (0 != strcmp (uri, | 665 | if (NULL == uri) |
494 | "/")) | 666 | mhdErrorExitDesc ("The 'uri' parameter is NULL"); |
495 | abort (); | 667 | if (0 != strcmp (uri, "/")) |
668 | { | ||
669 | fprintf (stderr, "Wrong 'uri' value: '%s'. ", uri); | ||
670 | mhdErrorExit (); | ||
671 | } | ||
496 | ppth = malloc (sizeof (pthread_t)); | 672 | ppth = malloc (sizeof (pthread_t)); |
497 | if (NULL == ppth) | 673 | if (NULL == ppth) |
498 | abort (); | 674 | externalErrorExitDesc ("malloc() failed"); |
499 | *ppth = pthread_self (); | 675 | *ppth = pthread_self (); |
500 | return (void *) ppth; | 676 | return (void *) ppth; |
501 | } | 677 | } |
@@ -534,15 +710,15 @@ notify_connection_cb (void *cls, | |||
534 | { | 710 | { |
535 | case MHD_CONNECTION_NOTIFY_STARTED: | 711 | case MHD_CONNECTION_NOTIFY_STARTED: |
536 | if (MHD_NO != started) | 712 | if (MHD_NO != started) |
537 | abort (); | 713 | mhdErrorExitDesc ("The connection has been already started"); |
538 | started = MHD_YES; | 714 | started = MHD_YES; |
539 | *socket_context = &started; | 715 | *socket_context = &started; |
540 | break; | 716 | break; |
541 | case MHD_CONNECTION_NOTIFY_CLOSED: | 717 | case MHD_CONNECTION_NOTIFY_CLOSED: |
542 | if (MHD_YES != started) | 718 | if (MHD_YES != started) |
543 | abort (); | 719 | mhdErrorExitDesc ("The connection has not been started before"); |
544 | if (&started != *socket_context) | 720 | if (&started != *socket_context) |
545 | abort (); | 721 | mhdErrorExitDesc ("Wrong '*socket_context' value"); |
546 | *socket_context = NULL; | 722 | *socket_context = NULL; |
547 | started = MHD_NO; | 723 | started = MHD_NO; |
548 | break; | 724 | break; |
@@ -563,15 +739,15 @@ make_blocking (MHD_socket fd) | |||
563 | 739 | ||
564 | flags = fcntl (fd, F_GETFL); | 740 | flags = fcntl (fd, F_GETFL); |
565 | if (-1 == flags) | 741 | if (-1 == flags) |
566 | abort (); | 742 | externalErrorExitDesc ("fcntl() failed"); |
567 | if ((flags & ~O_NONBLOCK) != flags) | 743 | if ((flags & ~O_NONBLOCK) != flags) |
568 | if (-1 == fcntl (fd, F_SETFL, flags & ~O_NONBLOCK)) | 744 | if (-1 == fcntl (fd, F_SETFL, flags & ~O_NONBLOCK)) |
569 | abort (); | 745 | externalErrorExitDesc ("fcntl() failed"); |
570 | #elif defined(MHD_WINSOCK_SOCKETS) | 746 | #elif defined(MHD_WINSOCK_SOCKETS) |
571 | unsigned long flags = 0; | 747 | unsigned long flags = 0; |
572 | 748 | ||
573 | if (0 != ioctlsocket (fd, (int) FIONBIO, &flags)) | 749 | if (0 != ioctlsocket (fd, (int) FIONBIO, &flags)) |
574 | abort (); | 750 | externalErrorExitDesc ("ioctlsocket() failed"); |
575 | #endif /* MHD_WINSOCK_SOCKETS */ | 751 | #endif /* MHD_WINSOCK_SOCKETS */ |
576 | 752 | ||
577 | } | 753 | } |
@@ -598,7 +774,7 @@ send_all (struct wr_socket *sock, | |||
598 | ret = 0; | 774 | ret = 0; |
599 | continue; | 775 | continue; |
600 | } | 776 | } |
601 | abort (); | 777 | externalErrorExitDesc ("send() failed"); |
602 | } | 778 | } |
603 | } | 779 | } |
604 | } | 780 | } |
@@ -628,10 +804,10 @@ recv_hdr (struct wr_socket *sock) | |||
628 | { | 804 | { |
629 | if (MHD_SCKT_ERR_IS_EAGAIN_ (MHD_socket_get_error_ ())) | 805 | if (MHD_SCKT_ERR_IS_EAGAIN_ (MHD_socket_get_error_ ())) |
630 | continue; | 806 | continue; |
631 | abort (); | 807 | externalErrorExitDesc ("recv() failed"); |
632 | } | 808 | } |
633 | if (0 == ret) | 809 | if (0 == ret) |
634 | continue; | 810 | continue; /* TODO: replace with error exit! */ |
635 | if (c == next) | 811 | if (c == next) |
636 | { | 812 | { |
637 | i++; | 813 | i++; |
@@ -675,11 +851,15 @@ recv_all (struct wr_socket *sock, | |||
675 | ret = 0; | 851 | ret = 0; |
676 | continue; | 852 | continue; |
677 | } | 853 | } |
678 | abort (); | 854 | externalErrorExitDesc ("recv() failed"); |
679 | } | 855 | } |
680 | } | 856 | } |
681 | if (0 != strncmp (text, buf, len)) | 857 | if (0 != strncmp (text, buf, len)) |
682 | abort (); | 858 | { |
859 | fprintf (stderr, "Wrong received text. Expected: '%s' ." | ||
860 | "Got: '%.*s'. ", text, (int) len, buf); | ||
861 | mhdErrorExit (); | ||
862 | } | ||
683 | } | 863 | } |
684 | 864 | ||
685 | 865 | ||
@@ -794,12 +974,12 @@ upgrade_cb (void *cls, | |||
794 | 974 | ||
795 | usock = wr_create_from_plain_sckt (sock); | 975 | usock = wr_create_from_plain_sckt (sock); |
796 | if (0 != extra_in_size) | 976 | if (0 != extra_in_size) |
797 | abort (); | 977 | mhdErrorExitDesc ("'extra_in_size' is not zero"); |
798 | if (0 != pthread_create (&pt, | 978 | if (0 != pthread_create (&pt, |
799 | NULL, | 979 | NULL, |
800 | &run_usock, | 980 | &run_usock, |
801 | urh)) | 981 | urh)) |
802 | abort (); | 982 | externalErrorExitDesc ("pthread_create() failed"); |
803 | } | 983 | } |
804 | 984 | ||
805 | 985 | ||
@@ -853,7 +1033,6 @@ ahc_upgrade (void *cls, | |||
853 | void **con_cls) | 1033 | void **con_cls) |
854 | { | 1034 | { |
855 | struct MHD_Response *resp; | 1035 | struct MHD_Response *resp; |
856 | enum MHD_Result ret; | ||
857 | (void) cls; | 1036 | (void) cls; |
858 | (void) url; | 1037 | (void) url; |
859 | (void) method; /* Unused. Silent compiler warning. */ | 1038 | (void) method; /* Unused. Silent compiler warning. */ |
@@ -861,20 +1040,26 @@ ahc_upgrade (void *cls, | |||
861 | (void) upload_data; | 1040 | (void) upload_data; |
862 | (void) upload_data_size; /* Unused. Silent compiler warning. */ | 1041 | (void) upload_data_size; /* Unused. Silent compiler warning. */ |
863 | 1042 | ||
1043 | if (NULL == con_cls) | ||
1044 | mhdErrorExitDesc ("'con_cls' is NULL"); | ||
864 | if (NULL == *con_cls) | 1045 | if (NULL == *con_cls) |
865 | abort (); | 1046 | mhdErrorExitDesc ("'*con_cls' value is NULL"); |
866 | if (! pthread_equal (**((pthread_t **) con_cls), pthread_self ())) | 1047 | if (! pthread_equal (**((pthread_t **) con_cls), pthread_self ())) |
867 | abort (); | 1048 | mhdErrorExitDesc ("ahc_upgrade() is called in wrong thread"); |
868 | resp = MHD_create_response_for_upgrade (&upgrade_cb, | 1049 | resp = MHD_create_response_for_upgrade (&upgrade_cb, |
869 | NULL); | 1050 | NULL); |
870 | MHD_add_response_header (resp, | 1051 | if (NULL == resp) |
871 | MHD_HTTP_HEADER_UPGRADE, | 1052 | mhdErrorExitDesc ("MHD_create_response_for_upgrade() failed"); |
872 | "Hello World Protocol"); | 1053 | if (MHD_YES != MHD_add_response_header (resp, |
873 | ret = MHD_queue_response (connection, | 1054 | MHD_HTTP_HEADER_UPGRADE, |
874 | MHD_HTTP_SWITCHING_PROTOCOLS, | 1055 | "Hello World Protocol")) |
875 | resp); | 1056 | mhdErrorExitDesc ("MHD_add_response_header() failed"); |
1057 | if (MHD_YES != MHD_queue_response (connection, | ||
1058 | MHD_HTTP_SWITCHING_PROTOCOLS, | ||
1059 | resp)) | ||
1060 | mhdErrorExitDesc ("MHD_queue_response() failed"); | ||
876 | MHD_destroy_response (resp); | 1061 | MHD_destroy_response (resp); |
877 | return ret; | 1062 | return MHD_YES; |
878 | } | 1063 | } |
879 | 1064 | ||
880 | 1065 | ||
@@ -907,7 +1092,7 @@ run_mhd_select_loop (struct MHD_Daemon *daemon) | |||
907 | &ws, | 1092 | &ws, |
908 | &es, | 1093 | &es, |
909 | &max_fd)) | 1094 | &max_fd)) |
910 | abort (); | 1095 | mhdErrorExitDesc ("MHD_get_fdset() failed"); |
911 | (void) MHD_get_timeout (daemon, | 1096 | (void) MHD_get_timeout (daemon, |
912 | &to); | 1097 | &to); |
913 | if (1000 < to) | 1098 | if (1000 < to) |
@@ -919,7 +1104,7 @@ run_mhd_select_loop (struct MHD_Daemon *daemon) | |||
919 | &ws, | 1104 | &ws, |
920 | &es, | 1105 | &es, |
921 | &tv)) | 1106 | &tv)) |
922 | abort (); | 1107 | mhdErrorExitDesc ("MHD_get_fdset() failed"); /* TODO: handle errors */ |
923 | MHD_run_from_select (daemon, | 1108 | MHD_run_from_select (daemon, |
924 | &rs, | 1109 | &rs, |
925 | &ws, | 1110 | &ws, |
@@ -939,7 +1124,7 @@ static void | |||
939 | run_mhd_poll_loop (struct MHD_Daemon *daemon) | 1124 | run_mhd_poll_loop (struct MHD_Daemon *daemon) |
940 | { | 1125 | { |
941 | (void) daemon; /* Unused. Silent compiler warning. */ | 1126 | (void) daemon; /* Unused. Silent compiler warning. */ |
942 | abort (); /* currently not implementable with existing MHD API */ | 1127 | externalErrorExitDesc ("Not implementable with MHD API"); |
943 | } | 1128 | } |
944 | 1129 | ||
945 | 1130 | ||
@@ -964,6 +1149,8 @@ run_mhd_epoll_loop (struct MHD_Daemon *daemon) | |||
964 | 1149 | ||
965 | di = MHD_get_daemon_info (daemon, | 1150 | di = MHD_get_daemon_info (daemon, |
966 | MHD_DAEMON_INFO_EPOLL_FD); | 1151 | MHD_DAEMON_INFO_EPOLL_FD); |
1152 | if (NULL == di) | ||
1153 | mhdErrorExitDesc ("MHD_get_daemon_info() failed"); | ||
967 | ep = di->listen_fd; | 1154 | ep = di->listen_fd; |
968 | while (! done) | 1155 | while (! done) |
969 | { | 1156 | { |
@@ -985,7 +1172,7 @@ run_mhd_epoll_loop (struct MHD_Daemon *daemon) | |||
985 | if ( (-1 == ret) && | 1172 | if ( (-1 == ret) && |
986 | (EAGAIN != errno) && | 1173 | (EAGAIN != errno) && |
987 | (EINTR != errno) ) | 1174 | (EINTR != errno) ) |
988 | abort (); | 1175 | mhdErrorExitDesc ("MHD_get_fdset() failed"); /* TODO: handle errors */ |
989 | MHD_run (daemon); | 1176 | MHD_run (daemon); |
990 | } | 1177 | } |
991 | } | 1178 | } |
@@ -1074,28 +1261,28 @@ test_upgrade (int flags, | |||
1074 | MHD_OPTION_END); | 1261 | MHD_OPTION_END); |
1075 | #endif /* HTTPS_SUPPORT */ | 1262 | #endif /* HTTPS_SUPPORT */ |
1076 | if (NULL == d) | 1263 | if (NULL == d) |
1077 | return 2; | 1264 | mhdErrorExitDesc ("MHD_start_daemon() failed"); |
1078 | real_flags = MHD_get_daemon_info (d, | 1265 | real_flags = MHD_get_daemon_info (d, |
1079 | MHD_DAEMON_INFO_FLAGS); | 1266 | MHD_DAEMON_INFO_FLAGS); |
1080 | if (NULL == real_flags) | 1267 | if (NULL == real_flags) |
1081 | abort (); | 1268 | mhdErrorExitDesc ("MHD_get_daemon_info() failed"); |
1082 | dinfo = MHD_get_daemon_info (d, | 1269 | dinfo = MHD_get_daemon_info (d, |
1083 | MHD_DAEMON_INFO_BIND_PORT); | 1270 | MHD_DAEMON_INFO_BIND_PORT); |
1084 | if ( (NULL == dinfo) || | 1271 | if ( (NULL == dinfo) || |
1085 | (0 == dinfo->port) ) | 1272 | (0 == dinfo->port) ) |
1086 | abort (); | 1273 | mhdErrorExitDesc ("MHD_get_daemon_info() failed"); |
1087 | if (! test_tls || (TLS_LIB_GNUTLS == use_tls_tool)) | 1274 | if (! test_tls || (TLS_LIB_GNUTLS == use_tls_tool)) |
1088 | { | 1275 | { |
1089 | sock = test_tls ? wr_create_tls_sckt () : wr_create_plain_sckt (); | 1276 | sock = test_tls ? wr_create_tls_sckt () : wr_create_plain_sckt (); |
1090 | if (NULL == sock) | 1277 | if (NULL == sock) |
1091 | abort (); | 1278 | externalErrorExitDesc ("Create socket failed"); |
1092 | sa.sin_family = AF_INET; | 1279 | sa.sin_family = AF_INET; |
1093 | sa.sin_port = htons (dinfo->port); | 1280 | sa.sin_port = htons (dinfo->port); |
1094 | sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK); | 1281 | sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
1095 | if (0 != wr_connect (sock, | 1282 | if (0 != wr_connect (sock, |
1096 | (struct sockaddr *) &sa, | 1283 | (struct sockaddr *) &sa, |
1097 | sizeof (sa))) | 1284 | sizeof (sa))) |
1098 | abort (); | 1285 | externalErrorExitDesc ("Connect socket failed"); |
1099 | } | 1286 | } |
1100 | else | 1287 | else |
1101 | { | 1288 | { |
@@ -1109,16 +1296,13 @@ test_upgrade (int flags, | |||
1109 | sizeof (port)); | 1296 | sizeof (port)); |
1110 | if (-1 == (pid = gnutlscli_connect (&tls_fork_sock, | 1297 | if (-1 == (pid = gnutlscli_connect (&tls_fork_sock, |
1111 | port))) | 1298 | port))) |
1112 | { | 1299 | externalErrorExitDesc ("gnutlscli_connect() failed"); |
1113 | MHD_stop_daemon (d); | ||
1114 | return 4; | ||
1115 | } | ||
1116 | 1300 | ||
1117 | sock = wr_create_from_plain_sckt (tls_fork_sock); | 1301 | sock = wr_create_from_plain_sckt (tls_fork_sock); |
1118 | if (NULL == sock) | 1302 | if (NULL == sock) |
1119 | abort (); | 1303 | externalErrorExitDesc ("wr_create_from_plain_sckt() failed"); |
1120 | #else /* !HTTPS_SUPPORT || !HAVE_FORK || !HAVE_WAITPID */ | 1304 | #else /* !HTTPS_SUPPORT || !HAVE_FORK || !HAVE_WAITPID */ |
1121 | abort (); | 1305 | externalErrorExitDesc ("Unsupported 'use_tls_tool' value"); |
1122 | #endif /* !HTTPS_SUPPORT || !HAVE_FORK || !HAVE_WAITPID */ | 1306 | #endif /* !HTTPS_SUPPORT || !HAVE_FORK || !HAVE_WAITPID */ |
1123 | } | 1307 | } |
1124 | 1308 | ||
@@ -1126,7 +1310,7 @@ test_upgrade (int flags, | |||
1126 | NULL, | 1310 | NULL, |
1127 | &run_usock_client, | 1311 | &run_usock_client, |
1128 | sock)) | 1312 | sock)) |
1129 | abort (); | 1313 | externalErrorExitDesc ("pthread_create() failed"); |
1130 | if (0 == (flags & MHD_USE_INTERNAL_POLLING_THREAD) ) | 1314 | if (0 == (flags & MHD_USE_INTERNAL_POLLING_THREAD) ) |
1131 | { | 1315 | { |
1132 | enum MHD_FLAG used_flags; | 1316 | enum MHD_FLAG used_flags; |
@@ -1137,13 +1321,18 @@ test_upgrade (int flags, | |||
1137 | sizeof (used_flags)); | 1321 | sizeof (used_flags)); |
1138 | run_mhd_loop (d, used_flags); | 1322 | run_mhd_loop (d, used_flags); |
1139 | } | 1323 | } |
1140 | pthread_join (pt_client, | 1324 | if (0 != pthread_join (pt_client, |
1141 | NULL); | 1325 | NULL)) |
1142 | pthread_join (pt, | 1326 | externalErrorExitDesc ("pthread_join() failed"); |
1143 | NULL); | 1327 | if (0 != pthread_join (pt, |
1328 | NULL)) | ||
1329 | externalErrorExitDesc ("pthread_join() failed"); | ||
1144 | #if defined(HTTPS_SUPPORT) && defined(HAVE_FORK) && defined(HAVE_WAITPID) | 1330 | #if defined(HTTPS_SUPPORT) && defined(HAVE_FORK) && defined(HAVE_WAITPID) |
1145 | if (test_tls && (TLS_LIB_GNUTLS != use_tls_tool)) | 1331 | if (test_tls && (TLS_LIB_GNUTLS != use_tls_tool)) |
1146 | waitpid (pid, NULL, 0); | 1332 | { |
1333 | if ((pid_t) -1 == waitpid (pid, NULL, 0)) | ||
1334 | externalErrorExitDesc ("waitpid() failed"); | ||
1335 | } | ||
1147 | #endif /* HTTPS_SUPPORT && HAVE_FORK && HAVE_WAITPID */ | 1336 | #endif /* HTTPS_SUPPORT && HAVE_FORK && HAVE_WAITPID */ |
1148 | MHD_stop_daemon (d); | 1337 | MHD_stop_daemon (d); |
1149 | return 0; | 1338 | return 0; |
@@ -1196,12 +1385,12 @@ main (int argc, | |||
1196 | printf ("GnuTLS library will be used for testing.\n"); | 1385 | printf ("GnuTLS library will be used for testing.\n"); |
1197 | break; | 1386 | break; |
1198 | default: | 1387 | default: |
1199 | abort (); | 1388 | externalErrorExitDesc ("Wrong 'use_tls_tool' value"); |
1200 | } | 1389 | } |
1201 | } | 1390 | } |
1202 | if ( (TLS_LIB_GNUTLS == use_tls_tool) && | 1391 | if ( (TLS_LIB_GNUTLS == use_tls_tool) && |
1203 | (GNUTLS_E_SUCCESS != gnutls_global_init ()) ) | 1392 | (GNUTLS_E_SUCCESS != gnutls_global_init ()) ) |
1204 | abort (); | 1393 | externalErrorExitDesc ("gnutls_global_init() failed"); |
1205 | 1394 | ||
1206 | #else /* ! HTTPS_SUPPORT */ | 1395 | #else /* ! HTTPS_SUPPORT */ |
1207 | fprintf (stderr, "HTTPS support was disabled by configure.\n"); | 1396 | fprintf (stderr, "HTTPS support was disabled by configure.\n"); |
@@ -1285,6 +1474,8 @@ main (int argc, | |||
1285 | /* Test different event loops, with and without thread pool */ | 1474 | /* Test different event loops, with and without thread pool */ |
1286 | res = test_upgrade (MHD_USE_INTERNAL_POLLING_THREAD, | 1475 | res = test_upgrade (MHD_USE_INTERNAL_POLLING_THREAD, |
1287 | 0); | 1476 | 0); |
1477 | fflush (stderr); | ||
1478 | fflush (stdout); | ||
1288 | error_count += res; | 1479 | error_count += res; |
1289 | if (res) | 1480 | if (res) |
1290 | fprintf (stderr, | 1481 | fprintf (stderr, |
@@ -1294,6 +1485,8 @@ main (int argc, | |||
1294 | printf ("PASSED: Upgrade with internal select.\n"); | 1485 | printf ("PASSED: Upgrade with internal select.\n"); |
1295 | res = test_upgrade (MHD_USE_INTERNAL_POLLING_THREAD, | 1486 | res = test_upgrade (MHD_USE_INTERNAL_POLLING_THREAD, |
1296 | 2); | 1487 | 2); |
1488 | fflush (stderr); | ||
1489 | fflush (stdout); | ||
1297 | error_count += res; | 1490 | error_count += res; |
1298 | if (res) | 1491 | if (res) |
1299 | fprintf (stderr, | 1492 | fprintf (stderr, |
@@ -1303,6 +1496,8 @@ main (int argc, | |||
1303 | printf ("PASSED: Upgrade with internal select with thread pool.\n"); | 1496 | printf ("PASSED: Upgrade with internal select with thread pool.\n"); |
1304 | res = test_upgrade (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD, | 1497 | res = test_upgrade (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD, |
1305 | 0); | 1498 | 0); |
1499 | fflush (stderr); | ||
1500 | fflush (stdout); | ||
1306 | error_count += res; | 1501 | error_count += res; |
1307 | if (res) | 1502 | if (res) |
1308 | fprintf (stderr, | 1503 | fprintf (stderr, |
@@ -1312,6 +1507,8 @@ main (int argc, | |||
1312 | printf ("PASSED: Upgrade with internal 'auto'.\n"); | 1507 | printf ("PASSED: Upgrade with internal 'auto'.\n"); |
1313 | res = test_upgrade (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD, | 1508 | res = test_upgrade (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD, |
1314 | 2); | 1509 | 2); |
1510 | fflush (stderr); | ||
1511 | fflush (stdout); | ||
1315 | error_count += res; | 1512 | error_count += res; |
1316 | if (res) | 1513 | if (res) |
1317 | fprintf (stderr, | 1514 | fprintf (stderr, |
@@ -1322,6 +1519,8 @@ main (int argc, | |||
1322 | #ifdef HAVE_POLL | 1519 | #ifdef HAVE_POLL |
1323 | res = test_upgrade (MHD_USE_POLL_INTERNAL_THREAD, | 1520 | res = test_upgrade (MHD_USE_POLL_INTERNAL_THREAD, |
1324 | 0); | 1521 | 0); |
1522 | fflush (stderr); | ||
1523 | fflush (stdout); | ||
1325 | error_count += res; | 1524 | error_count += res; |
1326 | if (res) | 1525 | if (res) |
1327 | fprintf (stderr, | 1526 | fprintf (stderr, |
@@ -1331,6 +1530,8 @@ main (int argc, | |||
1331 | printf ("PASSED: Upgrade with internal poll.\n"); | 1530 | printf ("PASSED: Upgrade with internal poll.\n"); |
1332 | res = test_upgrade (MHD_USE_POLL_INTERNAL_THREAD, | 1531 | res = test_upgrade (MHD_USE_POLL_INTERNAL_THREAD, |
1333 | 2); | 1532 | 2); |
1533 | fflush (stderr); | ||
1534 | fflush (stdout); | ||
1334 | if (res) | 1535 | if (res) |
1335 | fprintf (stderr, | 1536 | fprintf (stderr, |
1336 | "FAILED: Upgrade with internal poll with thread pool, return code %d.\n", | 1537 | "FAILED: Upgrade with internal poll with thread pool, return code %d.\n", |
@@ -1341,6 +1542,8 @@ main (int argc, | |||
1341 | #ifdef EPOLL_SUPPORT | 1542 | #ifdef EPOLL_SUPPORT |
1342 | res = test_upgrade (MHD_USE_EPOLL_INTERNAL_THREAD, | 1543 | res = test_upgrade (MHD_USE_EPOLL_INTERNAL_THREAD, |
1343 | 0); | 1544 | 0); |
1545 | fflush (stderr); | ||
1546 | fflush (stdout); | ||
1344 | if (res) | 1547 | if (res) |
1345 | fprintf (stderr, | 1548 | fprintf (stderr, |
1346 | "FAILED: Upgrade with internal epoll, return code %d.\n", | 1549 | "FAILED: Upgrade with internal epoll, return code %d.\n", |
@@ -1349,6 +1552,8 @@ main (int argc, | |||
1349 | printf ("PASSED: Upgrade with internal epoll.\n"); | 1552 | printf ("PASSED: Upgrade with internal epoll.\n"); |
1350 | res = test_upgrade (MHD_USE_EPOLL_INTERNAL_THREAD, | 1553 | res = test_upgrade (MHD_USE_EPOLL_INTERNAL_THREAD, |
1351 | 2); | 1554 | 2); |
1555 | fflush (stderr); | ||
1556 | fflush (stdout); | ||
1352 | if (res) | 1557 | if (res) |
1353 | fprintf (stderr, | 1558 | fprintf (stderr, |
1354 | "FAILED: Upgrade with internal epoll, return code %d.\n", | 1559 | "FAILED: Upgrade with internal epoll, return code %d.\n", |