mirror of
https://git.gnunet.org/libmicrohttpd.git
synced 2026-09-29 04:09:31 +03:00
test_https_time_out: additional fixes
Fixed possible abort on SIGPIPE. Avoid meaningless test results reports (-1 was mapped to unsigned value). Check GnuTLS functions results. Correctly initialize GnuTLS session.
This commit is contained in:
4 files changed
+59
-70
No files matched your search
@@ -1,6 +1,7 @@
|
||||
/*
|
||||
This file is part of libmicrohttpd
|
||||
Copyright (C) 2007 Christian Grothoff
|
||||
Copyright (C) 2014-2021 Karlson2k (Evgeny Grin)
|
||||
|
||||
libmicrohttpd is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published
|
||||
@@ -23,6 +24,7 @@
|
||||
* @brief: daemon TLS alert response test-case
|
||||
*
|
||||
* @author Sagie Amir
|
||||
* @author Karlson2k (Evgeny Grin)
|
||||
*/
|
||||
|
||||
#include "platform.h"
|
||||
@@ -31,6 +33,9 @@
|
||||
#ifdef MHD_HTTPS_REQUIRE_GRYPT
|
||||
#include <gcrypt.h>
|
||||
#endif /* MHD_HTTPS_REQUIRE_GRYPT */
|
||||
#ifdef HAVE_SIGNAL_H
|
||||
#include <signal.h>
|
||||
#endif /* HAVE_SIGNAL_H */
|
||||
#include "mhd_sockets.h" /* only macros used */
|
||||
|
||||
|
||||
@@ -83,7 +88,7 @@ test_tls_session_time_out (gnutls_session_t session, int port)
|
||||
if (sd == MHD_INVALID_SOCKET)
|
||||
{
|
||||
fprintf (stderr, "Failed to create socket: %s\n", strerror (errno));
|
||||
return -1;
|
||||
return 2;
|
||||
}
|
||||
|
||||
memset (&sa, '\0', sizeof (struct sockaddr_in));
|
||||
@@ -91,33 +96,37 @@ test_tls_session_time_out (gnutls_session_t session, int port)
|
||||
sa.sin_port = htons (port);
|
||||
sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
|
||||
|
||||
gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) (intptr_t) sd);
|
||||
|
||||
ret = connect (sd, (struct sockaddr *) &sa, sizeof (struct sockaddr_in));
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
fprintf (stderr, "Error: %s\n", MHD_E_FAILED_TO_CONNECT);
|
||||
MHD_socket_close_chk_ (sd);
|
||||
return -1;
|
||||
return 2;
|
||||
}
|
||||
|
||||
#if (GNUTLS_VERSION_NUMBER + 0 >= 0x030109) && ! defined(_WIN64)
|
||||
gnutls_transport_set_int (session, (int) (sd));
|
||||
#else /* GnuTLS before 3.1.9 or Win64 */
|
||||
gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) (intptr_t) (sd));
|
||||
#endif /* GnuTLS before 3.1.9 or Win64 */
|
||||
|
||||
ret = gnutls_handshake (session);
|
||||
if (ret < 0)
|
||||
{
|
||||
fprintf (stderr, "Handshake failed\n");
|
||||
MHD_socket_close_chk_ (sd);
|
||||
return -1;
|
||||
return 2;
|
||||
}
|
||||
|
||||
(void) sleep (TIME_OUT + 1);
|
||||
(void) sleep (TIME_OUT + 2);
|
||||
|
||||
/* check that server has closed the connection */
|
||||
if (1 == num_disconnects)
|
||||
{
|
||||
fprintf (stderr, "Connection failed to time-out\n");
|
||||
MHD_socket_close_chk_ (sd);
|
||||
return -1;
|
||||
return 1;
|
||||
}
|
||||
else if (0 != num_disconnects)
|
||||
abort ();
|
||||
@@ -133,8 +142,6 @@ main (int argc, char *const *argv)
|
||||
int errorCount = 0;
|
||||
struct MHD_Daemon *d;
|
||||
gnutls_session_t session;
|
||||
gnutls_datum_t key;
|
||||
gnutls_datum_t cert;
|
||||
gnutls_certificate_credentials_t xcred;
|
||||
int port;
|
||||
(void) argc; /* Unused. Silent compiler warning. */
|
||||
@@ -144,13 +151,30 @@ main (int argc, char *const *argv)
|
||||
else
|
||||
port = 3070;
|
||||
|
||||
#ifdef MHD_SEND_SPIPE_SUPPRESS_NEEDED
|
||||
#if defined(HAVE_SIGNAL_H) && defined(SIGPIPE)
|
||||
if (SIG_ERR == signal (SIGPIPE, SIG_IGN))
|
||||
{
|
||||
fprintf (stderr, "Error suppressing SIGPIPE signal.\n");
|
||||
exit (99);
|
||||
}
|
||||
#else /* ! HAVE_SIGNAL_H || ! SIGPIPE */
|
||||
fprintf (stderr, "Cannot suppress SIGPIPE signal.\n");
|
||||
/* exit (77); */
|
||||
#endif
|
||||
#endif /* MHD_SEND_SPIPE_SUPPRESS_NEEDED */
|
||||
|
||||
#ifdef MHD_HTTPS_REQUIRE_GRYPT
|
||||
gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
|
||||
#ifdef GCRYCTL_INITIALIZATION_FINISHED
|
||||
gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
|
||||
#endif
|
||||
#endif /* MHD_HTTPS_REQUIRE_GRYPT */
|
||||
gnutls_global_init ();
|
||||
if (GNUTLS_E_SUCCESS != gnutls_global_init ())
|
||||
{
|
||||
fprintf (stderr, "Cannot initialize GnuTLS.\n");
|
||||
exit (99);
|
||||
}
|
||||
gnutls_global_set_log_level (11);
|
||||
|
||||
d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
|
||||
@@ -178,13 +202,13 @@ main (int argc, char *const *argv)
|
||||
port = (int) dinfo->port;
|
||||
}
|
||||
|
||||
if (0 != setup_session (&session, &key, &cert, &xcred))
|
||||
if (0 != setup_session (&session, &xcred))
|
||||
{
|
||||
fprintf (stderr, "failed to setup session\n");
|
||||
return 1;
|
||||
}
|
||||
errorCount += test_tls_session_time_out (session, port);
|
||||
teardown_session (session, &key, &cert, xcred);
|
||||
teardown_session (session, xcred);
|
||||
|
||||
print_test_result (errorCount, argv[0]);
|
||||
|
||||
|
||||
Reference in new issue
Block a user