libmicrohttpd2

HTTP server C library (MHD 2.x, alpha)
Log | Files | Refs | README | LICENSE

test_tls.c (11452B)


      1 /* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */
      2 /*
      3   This file is part of GNU libmicrohttpd.
      4   Copyright (C) 2016, 2024 Christian Grothoff & Evgeny Grin (Karlson2k)
      5 
      6   GNU libmicrohttpd is free software; you can redistribute it and/or
      7   modify it under the terms of the GNU Lesser General Public
      8   License as published by the Free Software Foundation; either
      9   version 2.1 of the License, or (at your option) any later version.
     10 
     11   GNU libmicrohttpd is distributed in the hope that it will be useful,
     12   but WITHOUT ANY WARRANTY; without even the implied warranty of
     13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14   Lesser General Public License for more details.
     15 
     16   Alternatively, you can redistribute GNU libmicrohttpd and/or
     17   modify it under the terms of the GNU General Public License as
     18   published by the Free Software Foundation; either version 2 of
     19   the License, or (at your option) any later version, together
     20   with the eCos exception, as follows:
     21 
     22     As a special exception, if other files instantiate templates or
     23     use macros or inline functions from this file, or you compile this
     24     file and link it with other works to produce a work based on this
     25     file, this file does not by itself cause the resulting work to be
     26     covered by the GNU General Public License. However the source code
     27     for this file must still be made available in accordance with
     28     section (3) of the GNU General Public License v2.
     29 
     30     This exception does not invalidate any other reasons why a work
     31     based on this file might be covered by the GNU General Public
     32     License.
     33 
     34   You should have received copies of the GNU Lesser General Public
     35   License and the GNU General Public License along with this library;
     36   if not, see <https://www.gnu.org/licenses/>.
     37 */
     38 
     39 /**
     40  * @file test_tls.c
     41  * @brief  test with client against TLS server
     42  * @author Christian Grothoff
     43  */
     44 #include "libtest.h"
     45 
     46 
     47 int
     48 main (int argc, char *argv[])
     49 {
     50   struct MHD_DaemonOptionAndValue thread1select[] = {
     51     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_SELECT),
     52     MHD_D_OPTION_WM_WORKER_THREADS (1),
     53     MHD_D_OPTION_TERMINATE ()
     54   };
     55   struct MHD_DaemonOptionAndValue thread2select[] = {
     56     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_SELECT),
     57     MHD_D_OPTION_WM_WORKER_THREADS (2),
     58     MHD_D_OPTION_TERMINATE ()
     59   };
     60   struct MHD_DaemonOptionAndValue thread1poll[] = {
     61     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_POLL),
     62     MHD_D_OPTION_WM_WORKER_THREADS (1),
     63     MHD_D_OPTION_TERMINATE ()
     64   };
     65   struct MHD_DaemonOptionAndValue thread2poll[] = {
     66     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_POLL),
     67     MHD_D_OPTION_WM_WORKER_THREADS (1),
     68     MHD_D_OPTION_TERMINATE ()
     69   };
     70 #ifdef MHD_SUPPORT_EPOLL
     71   struct MHD_DaemonOptionAndValue thread1epoll[] = {
     72     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_EPOLL),
     73     MHD_D_OPTION_WM_WORKER_THREADS (1),
     74     MHD_D_OPTION_TERMINATE ()
     75   };
     76   struct MHD_DaemonOptionAndValue thread2epoll[] = {
     77     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_EPOLL),
     78     MHD_D_OPTION_WM_WORKER_THREADS (1),
     79     MHD_D_OPTION_TERMINATE ()
     80   };
     81 #endif /* MHD_SUPPORT_EPOLL */
     82   struct MHD_DaemonOptionAndValue thread1auto[] = {
     83     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_AUTO),
     84     MHD_D_OPTION_WM_WORKER_THREADS (1),
     85     MHD_D_OPTION_TERMINATE ()
     86   };
     87   struct MHD_DaemonOptionAndValue external0auto[] = {
     88     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_AUTO),
     89     MHD_D_OPTION_WM_EXTERNAL_PERIODIC (),
     90     MHD_D_OPTION_TERMINATE ()
     91   };
     92   struct ServerType
     93   {
     94     const char *label;
     95     MHDT_ServerSetup server_setup;
     96     void *server_setup_cls;
     97     MHDT_ServerRunner server_runner;
     98     void *server_runner_cls;
     99   } configs[] = {
    100 #ifdef MHD_SUPPORT_SELECT
    101     {
    102       .label = "single threaded select",
    103       .server_setup = &MHDT_server_setup_tls,
    104       .server_setup_cls = thread1select,
    105       .server_runner = &MHDT_server_run_minimal,
    106     },
    107     {
    108       .label = "multi-threaded select",
    109       .server_setup = &MHDT_server_setup_tls,
    110       .server_setup_cls = thread2select,
    111       .server_runner = &MHDT_server_run_minimal,
    112     },
    113 #if MHD_SUPPORT_GNUTLS
    114     {
    115       .label = "multi-threaded select, forcing GnuTLS",
    116       .server_setup = &MHDT_server_setup_gnutls,
    117       .server_setup_cls = thread2select,
    118       .server_runner = &MHDT_server_run_minimal,
    119     },
    120 #endif
    121 #endif
    122 #ifdef MHD_SUPPORT_POLL
    123     {
    124       .label = "single threaded poll",
    125       .server_setup = &MHDT_server_setup_tls,
    126       .server_setup_cls = thread1poll,
    127       .server_runner = &MHDT_server_run_minimal,
    128     },
    129     {
    130       .label = "multi-threaded poll",
    131       .server_setup = &MHDT_server_setup_tls,
    132       .server_setup_cls = thread2poll,
    133       .server_runner = &MHDT_server_run_minimal,
    134     },
    135 #ifdef MHD_SUPPORT_GNUTLS
    136     {
    137       .label = "multi-threaded poll, forcing GnuTLS",
    138       .server_setup = &MHDT_server_setup_gnutls,
    139       .server_setup_cls = thread2poll,
    140       .server_runner = &MHDT_server_run_minimal,
    141     },
    142 #endif
    143 #endif
    144 #ifdef MHD_SUPPORT_EPOLL
    145     {
    146       .label = "single threaded epoll",
    147       .server_setup = &MHDT_server_setup_tls,
    148       .server_setup_cls = thread1epoll,
    149       .server_runner = &MHDT_server_run_minimal,
    150     },
    151     {
    152       .label = "multi-threaded epoll",
    153       .server_setup = &MHDT_server_setup_tls,
    154       .server_setup_cls = thread2epoll,
    155       .server_runner = &MHDT_server_run_minimal,
    156     },
    157 #ifdef MHD_SUPPORT_GNUTLS
    158     {
    159       .label = "multi-threaded epoll, forcing GnuTLS",
    160       .server_setup = &MHDT_server_setup_gnutls,
    161       .server_setup_cls = thread2epoll,
    162       .server_runner = &MHDT_server_run_minimal,
    163     },
    164 #endif
    165 #endif
    166     {
    167       .label = "auto-selected mode, single threaded",
    168       .server_setup = &MHDT_server_setup_tls,
    169       .server_setup_cls = thread1auto,
    170       .server_runner = &MHDT_server_run_minimal,
    171     },
    172 #ifdef MHD_SUPPORT_GNUTLS
    173     {
    174       .label = "auto-selected mode, single threaded, forcing GnuTLS",
    175       .server_setup = &MHDT_server_setup_gnutls,
    176       .server_setup_cls = thread1auto,
    177       .server_runner = &MHDT_server_run_minimal,
    178     },
    179 #endif
    180 #ifdef MHD_SUPPORT_OPENSSL
    181     {
    182       .label = "auto-selected mode, single threaded, forcing OpenSSL",
    183       .server_setup = &MHDT_server_setup_openssl,
    184       .server_setup_cls = thread1auto,
    185       .server_runner = &MHDT_server_run_minimal,
    186     },
    187 #endif
    188 #if 1
    189     /* FIXME: remove once MHD_daemon_process_blocking
    190        has been implemented */
    191     {
    192       .label = "END"
    193     },
    194 #endif
    195     {
    196       .label = "auto-selected external event loop mode, no threads",
    197       .server_setup = &MHDT_server_setup_tls,
    198       .server_setup_cls = external0auto,
    199       .server_runner = &MHDT_server_run_blocking,
    200     },
    201     {
    202       .label = "END"
    203     }
    204   };
    205   struct MHDT_Phase phases[] = {
    206     {
    207       .label = "simple get",
    208       .server_cb = &MHDT_server_reply_text,
    209       .server_cb_cls = (void *) "Hello world",
    210       .client_cb = &MHDT_client_get_root,
    211       .client_cb_cls = "Hello world",
    212       .timeout_ms = 2500,
    213       .use_tls = true,
    214       .http_version = 1,
    215     },
    216     {
    217       .label = "GET with sendfile",
    218       .server_cb = &MHDT_server_reply_file,
    219       .server_cb_cls = (void *) "Hello world",
    220       .client_cb = &MHDT_client_get_root,
    221       .client_cb_cls = "Hello world",
    222       .timeout_ms = 2500,
    223       .use_tls = true,
    224       .http_version = 1,
    225     },
    226     {
    227       .label = "client PUT with content-length",
    228       .server_cb = &MHDT_server_reply_check_upload,
    229       .server_cb_cls = (void *) "simple-upload-value",
    230       .client_cb = &MHDT_client_put_data,
    231       .client_cb_cls = "simple-upload-value",
    232       .timeout_ms = 2500,
    233       .use_tls = true,
    234       .http_version = 1,
    235     },
    236     {
    237       .label = "client PUT with 2 chunks",
    238       .server_cb = &MHDT_server_reply_check_upload,
    239       .server_cb_cls = (void *) "chunky-upload-value",
    240       .client_cb = &MHDT_client_chunk_data,
    241       .client_cb_cls = "chunky-upload-value",
    242       .timeout_ms = 2500,
    243       .use_tls = true,
    244       .http_version = 1,
    245     },
    246     {
    247       .label = "client request with custom header",
    248       .server_cb = &MHDT_server_reply_check_header,
    249       .server_cb_cls = (void *) "C-Header:testvalue",
    250       .client_cb = &MHDT_client_set_header,
    251       .client_cb_cls = "C-Header:testvalue",
    252       .timeout_ms = 2500,
    253       .use_tls = true,
    254       .http_version = 1,
    255     },
    256     {
    257       .label = "server response with custom header",
    258       .server_cb = &MHDT_server_reply_with_header,
    259       .server_cb_cls = (void *) "X-Header:testvalue",
    260       .client_cb = &MHDT_client_expect_header,
    261       .client_cb_cls = "X-Header:testvalue",
    262       .timeout_ms = 2500,
    263       .use_tls = true,
    264       .http_version = 1,
    265     },
    266     {
    267       .label = "URL with query parameters 1",
    268       .server_cb = &MHDT_server_reply_check_query,
    269       .server_cb_cls = (void *) "a=b&c",
    270       .client_cb = &MHDT_client_get_with_query,
    271       .client_cb_cls = "?a=b&c",
    272       .timeout_ms = 5000,
    273       .num_clients = 4,
    274       .use_tls = true,
    275       .http_version = 1,
    276     },
    277     {
    278       .label = "URL with query parameters 2",
    279       .server_cb = &MHDT_server_reply_check_query,
    280       .server_cb_cls = (void *) "a=b&c", /* a => b, c => NULL */
    281       .client_cb = &MHDT_client_get_with_query,
    282       .client_cb_cls = "?c&a=b",
    283       .timeout_ms = 5000,
    284       .num_clients = 1,
    285       .use_tls = true,
    286       .http_version = 1,
    287     },
    288     {
    289       .label = "URL with query parameters 3",
    290       .server_cb = &MHDT_server_reply_check_query,
    291       .server_cb_cls = (void *) "a=&c", /* a => "", c => NULL */
    292       .client_cb = &MHDT_client_get_with_query,
    293       .client_cb_cls = "?c&a=",
    294       .timeout_ms = 5000,
    295       .num_clients = 1,
    296       .use_tls = true,
    297       .http_version = 1,
    298     },
    299     {
    300       .label = "URL with query parameters 4",
    301       .server_cb = &MHDT_server_reply_check_query,
    302       .server_cb_cls = (void *) "a=", /* a => "" */
    303       .client_cb = &MHDT_client_get_with_query,
    304       .client_cb_cls = "?a=",
    305       .timeout_ms = 5000,
    306       .num_clients = 1,
    307       .use_tls = true,
    308       .http_version = 1,
    309     },
    310     {
    311       .label = "URL with query parameters 5",
    312       .server_cb = &MHDT_server_reply_check_query,
    313       .server_cb_cls = (void *) "a=b", /* a => "b" */
    314       .client_cb = &MHDT_client_get_with_query,
    315       .client_cb_cls = "?a=b",
    316       .timeout_ms = 5000,
    317       .num_clients = 1,
    318       .use_tls = true,
    319       .http_version = 1,
    320     },
    321     {
    322       .label = "chunked response get",
    323       .server_cb = &MHDT_server_reply_chunked_text,
    324       .server_cb_cls = (void *) "Hello world",
    325       .client_cb = &MHDT_client_get_root,
    326       .client_cb_cls = "Hello world",
    327       .timeout_ms = 2500,
    328       .use_tls = true,
    329       .http_version = 1,
    330     },
    331     // TODO: chunked download
    332     {
    333       .label = NULL,
    334     },
    335   };
    336   unsigned int i;
    337   int ret = 0;
    338 
    339   (void) argc; /* Unused. Silence compiler warning. */
    340   (void) argv; /* Unused. Silence compiler warning. */
    341 
    342   for (i = 0; NULL != configs[i].server_setup; i++)
    343   {
    344     fprintf (stderr,
    345              "Running TLS tests with server setup '%s'\n",
    346              configs[i].label);
    347     ret = MHDT_test (configs[i].server_setup,
    348                      configs[i].server_setup_cls,
    349                      configs[i].server_runner,
    350                      configs[i].server_runner_cls,
    351                      phases);
    352     if (0 != ret)
    353     {
    354       fprintf (stderr,
    355                "Test failed with server of type '%s' (%u)\n",
    356                configs[i].label,
    357                i);
    358       break;
    359     }
    360   }
    361   return ret;
    362 }