Home | History | Annotate | Download | only in https
      1 /*
      2  This file is part of libmicrohttpd
      3  Copyright (C) 2007 Christian Grothoff
      4 
      5  libmicrohttpd is free software; you can redistribute it and/or modify
      6  it under the terms of the GNU General Public License as published
      7  by the Free Software Foundation; either version 2, or (at your
      8  option) any later version.
      9 
     10  libmicrohttpd is distributed in the hope that it will be useful, but
     11  WITHOUT ANY WARRANTY; without even the implied warranty of
     12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13  General Public License for more details.
     14 
     15  You should have received a copy of the GNU General Public License
     16  along with libmicrohttpd; see the file COPYING.  If not, write to the
     17  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     18  Boston, MA 02111-1307, USA.
     19  */
     20 
     21 /**
     22  * @file mhds_get_test.c
     23  * @brief: daemon TLS alert response test-case
     24  *
     25  * @author Sagie Amir
     26  */
     27 
     28 #include "platform.h"
     29 #include "microhttpd.h"
     30 #include "internal.h"
     31 #include "tls_test_common.h"
     32 #include <gcrypt.h>
     33 
     34 #ifdef _WIN32
     35 #ifndef WIN32_LEAN_AND_MEAN
     36 #define WIN32_LEAN_AND_MEAN 1
     37 #endif /* !WIN32_LEAN_AND_MEAN */
     38 #include <windows.h>
     39 #endif
     40 
     41 extern const char srv_key_pem[];
     42 extern const char srv_self_signed_cert_pem[];
     43 
     44 static const int TIME_OUT = 3;
     45 
     46 static int
     47 test_tls_session_time_out (gnutls_session_t session)
     48 {
     49   int ret;
     50   MHD_socket sd;
     51   struct sockaddr_in sa;
     52 
     53   sd = socket (AF_INET, SOCK_STREAM, 0);
     54   if (sd == -1)
     55     {
     56       fprintf (stderr, "Failed to create socket: %s\n", strerror (errno));
     57       return -1;
     58     }
     59 
     60   memset (&sa, '\0', sizeof (struct sockaddr_in));
     61   sa.sin_family = AF_INET;
     62   sa.sin_port = htons (DEAMON_TEST_PORT);
     63   sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
     64 
     65   gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) (intptr_t) sd);
     66 
     67   ret = connect (sd, &sa, sizeof (struct sockaddr_in));
     68 
     69   if (ret < 0)
     70     {
     71       fprintf (stderr, "Error: %s\n", MHD_E_FAILED_TO_CONNECT);
     72       close (sd);
     73       return -1;
     74     }
     75 
     76   ret = gnutls_handshake (session);
     77   if (ret < 0)
     78     {
     79       fprintf (stderr, "Handshake failed\n");
     80       close (sd);
     81       return -1;
     82     }
     83 
     84   sleep (TIME_OUT + 1);
     85 
     86   /* check that server has closed the connection */
     87   /* TODO better RST trigger */
     88   if (send (sd, "", 1, 0) == 0)
     89     {
     90       fprintf (stderr, "Connection failed to time-out\n");
     91       close (sd);
     92       return -1;
     93     }
     94 
     95   close (sd);
     96   return 0;
     97 }
     98 
     99 
    100 int
    101 main (int argc, char *const *argv)
    102 {
    103   int errorCount = 0;;
    104   struct MHD_Daemon *d;
    105   gnutls_session_t session;
    106   gnutls_datum_t key;
    107   gnutls_datum_t cert;
    108   gnutls_certificate_credentials_t xcred;
    109 
    110 
    111   gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
    112 #ifdef GCRYCTL_INITIALIZATION_FINISHED
    113   gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
    114 #endif
    115   gnutls_global_init ();
    116   gnutls_global_set_log_level (11);
    117 
    118   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_SSL |
    119                         MHD_USE_DEBUG, DEAMON_TEST_PORT,
    120                         NULL, NULL, &http_dummy_ahc, NULL,
    121                         MHD_OPTION_CONNECTION_TIMEOUT, TIME_OUT,
    122                         MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem,
    123                         MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem,
    124                         MHD_OPTION_END);
    125 
    126   if (d == NULL)
    127     {
    128       fprintf (stderr, MHD_E_SERVER_INIT);
    129       return -1;
    130     }
    131 
    132   if (0 != setup_session (&session, &key, &cert, &xcred))
    133     {
    134       fprintf (stderr, "failed to setup session\n");
    135       return 1;
    136     }
    137   errorCount += test_tls_session_time_out (session);
    138   teardown_session (session, &key, &cert, xcred);
    139 
    140   print_test_result (errorCount, argv[0]);
    141 
    142   MHD_stop_daemon (d);
    143   gnutls_global_deinit ();
    144 
    145   return errorCount != 0;
    146 }
    147