Home | History | Annotate | Download | only in testcurl
      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  Testcase for libmicrohttpd HTTPS GET operations
     24  * @author Sagie Amir
     25  */
     26 
     27 #include "platform.h"
     28 #include "microhttpd.h"
     29 
     30 #define MHD_E_MEM "Error: memory error\n"
     31 #define MHD_E_SERVER_INIT "Error: failed to start server\n"
     32 
     33 const int DEBUG_GNUTLS_LOG_LEVEL = 0;
     34 const char *test_file_name = "https_test_file";
     35 const char test_file_data[] = "Hello World\n";
     36 
     37 static int
     38 ahc_echo (void *cls,
     39           struct MHD_Connection *connection,
     40           const char *url,
     41           const char *method,
     42           const char *version,
     43           const char *upload_data, size_t *upload_data_size,
     44           void **unused)
     45 {
     46   return 0;
     47 }
     48 
     49 int
     50 test_wrap (char *test_name, int (*test) (void))
     51 {
     52   int ret;
     53 
     54   fprintf (stdout, "running test: %s ", test_name);
     55   ret = test ();
     56   if (ret == 0)
     57     {
     58       fprintf (stdout, "[pass]\n");
     59     }
     60   else
     61     {
     62       fprintf (stdout, "[fail]\n");
     63     }
     64   return ret;
     65 }
     66 
     67 
     68 /**
     69  * Test daemon initialization with the MHD_OPTION_SOCK_ADDR option
     70  */
     71 static int
     72 test_ip_addr_option ()
     73 {
     74   struct MHD_Daemon *d;
     75   struct sockaddr_in daemon_ip_addr;
     76 #if HAVE_INET6
     77   struct sockaddr_in6 daemon_ip_addr6;
     78 #endif
     79 
     80   memset (&daemon_ip_addr, 0, sizeof (struct sockaddr_in));
     81   daemon_ip_addr.sin_family = AF_INET;
     82   daemon_ip_addr.sin_port = htons (4233);
     83   daemon_ip_addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
     84 
     85 #if HAVE_INET6
     86   memset (&daemon_ip_addr6, 0, sizeof (struct sockaddr_in6));
     87   daemon_ip_addr6.sin6_family = AF_INET6;
     88   daemon_ip_addr6.sin6_port = htons (4233);
     89   daemon_ip_addr6.sin6_addr = in6addr_loopback;
     90 #endif
     91 
     92   d = MHD_start_daemon (MHD_USE_DEBUG, 4233,
     93                         NULL, NULL, &ahc_echo, NULL, MHD_OPTION_SOCK_ADDR,
     94                         &daemon_ip_addr, MHD_OPTION_END);
     95 
     96   if (d == 0)
     97     return -1;
     98 
     99   MHD_stop_daemon (d);
    100 
    101 #if HAVE_INET6
    102   d = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_IPv6, 4233,
    103                         NULL, NULL, &ahc_echo, NULL, MHD_OPTION_SOCK_ADDR,
    104                         &daemon_ip_addr6, MHD_OPTION_END);
    105 
    106   if (d == 0)
    107     return -1;
    108 
    109   MHD_stop_daemon (d);
    110 #endif
    111 
    112   return 0;
    113 }
    114 
    115 /* setup a temporary transfer test file */
    116 int
    117 main (int argc, char *const *argv)
    118 {
    119   unsigned int errorCount = 0;
    120 
    121   errorCount += test_wrap ("ip addr option", &test_ip_addr_option);
    122 
    123   return errorCount != 0;
    124 }
    125