libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

test_https_time_out.c (7199B)


      1 /*
      2  This file is part of libmicrohttpd
      3  Copyright (C) 2007 Christian Grothoff
      4  Copyright (C) 2014-2022 Evgeny Grin (Karlson2k)
      5 
      6  libmicrohttpd is free software; you can redistribute it and/or modify
      7  it under the terms of the GNU General Public License as published
      8  by the Free Software Foundation; either version 2, or (at your
      9  option) any later version.
     10 
     11  libmicrohttpd is distributed in the hope that it will be useful, but
     12  WITHOUT ANY WARRANTY; without even the implied warranty of
     13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14  General Public License for more details.
     15 
     16  You should have received a copy of the GNU General Public License
     17  along with libmicrohttpd; see the file COPYING.  If not, write to the
     18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     19  Boston, MA 02110-1301, USA.
     20  */
     21 
     22 /**
     23  * @file test_https_time_out.c
     24  * @brief: daemon TLS timeout test
     25  *
     26  * @author Sagie Amir
     27  * @author Karlson2k (Evgeny Grin)
     28  */
     29 
     30 #include "platform.h"
     31 #include "microhttpd.h"
     32 #include "tls_test_common.h"
     33 #ifdef MHD_HTTPS_REQUIRE_GCRYPT
     34 #include <gcrypt.h>
     35 #endif /* MHD_HTTPS_REQUIRE_GCRYPT */
     36 #ifdef HAVE_SIGNAL_H
     37 #include <signal.h>
     38 #endif /* HAVE_SIGNAL_H */
     39 #ifdef HAVE_UNISTD_H
     40 #include <unistd.h>
     41 #endif /* HAVE_UNISTD_H */
     42 #ifdef HAVE_TIME_H
     43 #include <time.h>
     44 #endif /* HAVE_TIME_H */
     45 
     46 #include "mhd_sockets.h" /* only macros used */
     47 
     48 
     49 #ifdef _WIN32
     50 #ifndef WIN32_LEAN_AND_MEAN
     51 #define WIN32_LEAN_AND_MEAN 1
     52 #endif /* !WIN32_LEAN_AND_MEAN */
     53 #include <windows.h>
     54 #endif
     55 #include "tls_test_keys.h"
     56 
     57 /* Turn any MHD_PANIC() or failing mhd_assert() reached from this
     58    test into a marked, classifiable test error (TESTING.md, P5). */
     59 #include "mhd_panic_tripwire.h"
     60 
     61 static const unsigned int timeout_val = 2;
     62 
     63 static volatile unsigned int num_connects = 0;
     64 static volatile unsigned int num_disconnects = 0;
     65 
     66 
     67 /**
     68  * Pause execution for specified number of milliseconds.
     69  * @param ms the number of milliseconds to sleep
     70  */
     71 static void
     72 _MHD_sleep (uint32_t ms)
     73 {
     74 #if defined(_WIN32)
     75   Sleep (ms);
     76 #elif defined(HAVE_NANOSLEEP)
     77   struct timespec slp = {ms / 1000, (ms % 1000) * 1000000};
     78   struct timespec rmn;
     79   int num_retries = 0;
     80   while (0 != nanosleep (&slp, &rmn))
     81   {
     82     if (num_retries++ > 8)
     83       break;
     84     slp = rmn;
     85   }
     86 #elif defined(HAVE_USLEEP)
     87   uint64_t us = ms * 1000;
     88   do
     89   {
     90     uint64_t this_sleep;
     91     if (999999 < us)
     92       this_sleep = 999999;
     93     else
     94       this_sleep = us;
     95     /* Ignore return value as it could be void */
     96     usleep (this_sleep);
     97     us -= this_sleep;
     98   } while (us > 0);
     99 #else
    100   sleep ((ms + 999) / 1000);
    101 #endif
    102 }
    103 
    104 
    105 static void
    106 socket_cb (void *cls,
    107            struct MHD_Connection *c,
    108            void **socket_context,
    109            enum MHD_ConnectionNotificationCode toe)
    110 {
    111   if (NULL == socket_context)
    112     abort ();
    113   if (NULL == c)
    114     abort ();
    115   if (NULL != cls)
    116     abort ();
    117 
    118   if (MHD_CONNECTION_NOTIFY_STARTED == toe)
    119   {
    120     num_connects++;
    121 #ifdef _DEBUG
    122     fprintf (stderr, "MHD: Connection has started.\n");
    123 #endif /* _DEBUG */
    124   }
    125   else if (MHD_CONNECTION_NOTIFY_CLOSED == toe)
    126   {
    127     num_disconnects++;
    128 #ifdef _DEBUG
    129     fprintf (stderr, "MHD: Connection has closed.\n");
    130 #endif /* _DEBUG */
    131   }
    132   else
    133     abort ();
    134 }
    135 
    136 
    137 static unsigned int
    138 test_tls_session_time_out (gnutls_session_t session, uint16_t port)
    139 {
    140   int ret;
    141   MHD_socket sd;
    142   struct sockaddr_in sa;
    143 
    144   sd = socket (AF_INET, SOCK_STREAM, 0);
    145   if (sd == MHD_INVALID_SOCKET)
    146   {
    147     fprintf (stderr, "Failed to create socket: %s\n", strerror (errno));
    148     return 2;
    149   }
    150 
    151   memset (&sa, '\0', sizeof (struct sockaddr_in));
    152   sa.sin_family = AF_INET;
    153   sa.sin_port = htons (port);
    154   sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
    155 
    156   ret = connect (sd, (struct sockaddr *) &sa, sizeof (struct sockaddr_in));
    157 
    158   if (ret < 0)
    159   {
    160     fprintf (stderr, "Error: %s\n", MHD_E_FAILED_TO_CONNECT);
    161     MHD_socket_close_chk_ (sd);
    162     return 2;
    163   }
    164 
    165 #if (GNUTLS_VERSION_NUMBER + 0 >= 0x030109) && ! defined(_WIN64)
    166   gnutls_transport_set_int (session, (int) (sd));
    167 #else  /* GnuTLS before 3.1.9 or Win64 */
    168   gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) (intptr_t) (sd));
    169 #endif /* GnuTLS before 3.1.9 or Win64 */
    170 
    171   ret = gnutls_handshake (session);
    172   if (ret < 0)
    173   {
    174     fprintf (stderr, "Handshake failed\n");
    175     MHD_socket_close_chk_ (sd);
    176     return 2;
    177   }
    178 
    179   _MHD_sleep (timeout_val * 1000 + 1700);
    180 
    181   if (0 == num_connects)
    182   {
    183     fprintf (stderr, "MHD has not detected any connection attempt.\n");
    184     MHD_socket_close_chk_ (sd);
    185     return 4;
    186   }
    187   /* check that server has closed the connection */
    188   if (0 == num_disconnects)
    189   {
    190     fprintf (stderr, "MHD has not detected any disconnections.\n");
    191     MHD_socket_close_chk_ (sd);
    192     return 1;
    193   }
    194 
    195   MHD_socket_close_chk_ (sd);
    196   return 0;
    197 }
    198 
    199 
    200 int
    201 main (int argc, char *const *argv)
    202 {
    203   unsigned int errorCount = 0;
    204   struct MHD_Daemon *d;
    205   gnutls_session_t session;
    206   gnutls_certificate_credentials_t xcred;
    207   uint16_t port;
    208   (void) argc;   /* Unused. Silent compiler warning. */
    209 
    210   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    211     port = 0;
    212   else
    213     port = 3070;
    214 
    215 #ifdef MHD_SEND_SPIPE_SUPPRESS_NEEDED
    216 #if defined(HAVE_SIGNAL_H) && defined(SIGPIPE)
    217   if (SIG_ERR == signal (SIGPIPE, SIG_IGN))
    218   {
    219     fprintf (stderr, "Error suppressing SIGPIPE signal.\n");
    220     exit (99);
    221   }
    222 #else /* ! HAVE_SIGNAL_H || ! SIGPIPE */
    223   fprintf (stderr, "Cannot suppress SIGPIPE signal.\n");
    224   /* exit (77); */
    225 #endif
    226 #endif /* MHD_SEND_SPIPE_SUPPRESS_NEEDED */
    227 
    228 #ifdef MHD_HTTPS_REQUIRE_GCRYPT
    229   gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
    230 #ifdef GCRYCTL_INITIALIZATION_FINISHED
    231   gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
    232 #endif
    233 #endif /* MHD_HTTPS_REQUIRE_GCRYPT */
    234   if (GNUTLS_E_SUCCESS != gnutls_global_init ())
    235   {
    236     fprintf (stderr, "Cannot initialize GnuTLS.\n");
    237     exit (99);
    238   }
    239   gnutls_global_set_log_level (11);
    240 
    241   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
    242                         | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_TLS
    243                         | MHD_USE_ERROR_LOG, port,
    244                         NULL, NULL, &http_dummy_ahc, NULL,
    245                         MHD_OPTION_CONNECTION_TIMEOUT,
    246                         (unsigned int) timeout_val,
    247                         MHD_OPTION_NOTIFY_CONNECTION, &socket_cb, NULL,
    248                         MHD_OPTION_HTTPS_MEM_KEY, srv_self_signed_key_pem,
    249                         MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem,
    250                         MHD_OPTION_END);
    251 
    252   if (NULL == d)
    253   {
    254     fprintf (stderr, MHD_E_SERVER_INIT);
    255     return -1;
    256   }
    257   if (0 == port)
    258   {
    259     const union MHD_DaemonInfo *dinfo;
    260     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    261     if ((NULL == dinfo) || (0 == dinfo->port) )
    262     {
    263       MHD_stop_daemon (d);
    264       return 99;
    265     }
    266     port = dinfo->port;
    267   }
    268 
    269   if (0 != setup_session (&session, &xcred))
    270   {
    271     fprintf (stderr, "failed to setup session\n");
    272     return 1;
    273   }
    274   errorCount += test_tls_session_time_out (session, port);
    275   teardown_session (session, xcred);
    276 
    277   print_test_result (errorCount, argv[0]);
    278 
    279   MHD_stop_daemon (d);
    280   gnutls_global_deinit ();
    281 
    282   return errorCount != 0 ? 1 : 0;
    283 }