libmicrohttpd2

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

test_tls.c (11726B)


      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 #if defined(MHD_SUPPORT_EPOLL) \
     71   && (defined(MHD_SUPPORT_GNUTLS) || defined(MHD_SUPPORT_MBEDTLS))
     72   struct MHD_DaemonOptionAndValue thread1epoll[] = {
     73     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_EPOLL),
     74     MHD_D_OPTION_WM_WORKER_THREADS (1),
     75     MHD_D_OPTION_TERMINATE ()
     76   };
     77   struct MHD_DaemonOptionAndValue thread2epoll[] = {
     78     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_EPOLL),
     79     MHD_D_OPTION_WM_WORKER_THREADS (1),
     80     MHD_D_OPTION_TERMINATE ()
     81   };
     82 #endif /* MHD_SUPPORT_EPOLL && (MHD_SUPPORT_GNUTLS || MHD_SUPPORT_MBEDTLS) */
     83   struct MHD_DaemonOptionAndValue thread1auto[] = {
     84     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_AUTO),
     85     MHD_D_OPTION_WM_WORKER_THREADS (1),
     86     MHD_D_OPTION_TERMINATE ()
     87   };
     88   struct MHD_DaemonOptionAndValue external0auto[] = {
     89     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_AUTO),
     90     MHD_D_OPTION_WM_EXTERNAL_PERIODIC (),
     91     MHD_D_OPTION_TERMINATE ()
     92   };
     93   struct ServerType
     94   {
     95     const char *label;
     96     MHDT_ServerSetup server_setup;
     97     void *server_setup_cls;
     98     MHDT_ServerRunner server_runner;
     99     void *server_runner_cls;
    100   } configs[] = {
    101 #ifdef MHD_SUPPORT_SELECT
    102     {
    103       .label = "single threaded select",
    104       .server_setup = &MHDT_server_setup_tls,
    105       .server_setup_cls = thread1select,
    106       .server_runner = &MHDT_server_run_minimal,
    107     },
    108     {
    109       .label = "multi-threaded select",
    110       .server_setup = &MHDT_server_setup_tls,
    111       .server_setup_cls = thread2select,
    112       .server_runner = &MHDT_server_run_minimal,
    113     },
    114 #  if MHD_SUPPORT_GNUTLS
    115     {
    116       .label = "multi-threaded select, forcing GnuTLS",
    117       .server_setup = &MHDT_server_setup_gnutls,
    118       .server_setup_cls = thread2select,
    119       .server_runner = &MHDT_server_run_minimal,
    120     },
    121 #  endif
    122 #endif
    123 #ifdef MHD_SUPPORT_POLL
    124     {
    125       .label = "single threaded poll",
    126       .server_setup = &MHDT_server_setup_tls,
    127       .server_setup_cls = thread1poll,
    128       .server_runner = &MHDT_server_run_minimal,
    129     },
    130     {
    131       .label = "multi-threaded poll",
    132       .server_setup = &MHDT_server_setup_tls,
    133       .server_setup_cls = thread2poll,
    134       .server_runner = &MHDT_server_run_minimal,
    135     },
    136 #  ifdef MHD_SUPPORT_GNUTLS
    137     {
    138       .label = "multi-threaded poll, forcing GnuTLS",
    139       .server_setup = &MHDT_server_setup_gnutls,
    140       .server_setup_cls = thread2poll,
    141       .server_runner = &MHDT_server_run_minimal,
    142     },
    143 #  endif
    144 #endif
    145 #if defined(MHD_SUPPORT_EPOLL) \
    146     && (defined(MHD_SUPPORT_GNUTLS) || defined(MHD_SUPPORT_MBEDTLS))
    147     {
    148       .label = "single threaded epoll",
    149       .server_setup = &MHDT_server_setup_tls,
    150       .server_setup_cls = thread1epoll,
    151       .server_runner = &MHDT_server_run_minimal,
    152     },
    153     {
    154       .label = "multi-threaded epoll",
    155       .server_setup = &MHDT_server_setup_tls,
    156       .server_setup_cls = thread2epoll,
    157       .server_runner = &MHDT_server_run_minimal,
    158     },
    159 #  ifdef MHD_SUPPORT_GNUTLS
    160     {
    161       .label = "multi-threaded epoll, forcing GnuTLS",
    162       .server_setup = &MHDT_server_setup_gnutls,
    163       .server_setup_cls = thread2epoll,
    164       .server_runner = &MHDT_server_run_minimal,
    165     },
    166 #  endif
    167 #endif /* MHD_SUPPORT_EPOLL && (MHD_SUPPORT_GNUTLS || MHD_SUPPORT_MBEDTLS) */
    168     {
    169       .label = "auto-selected mode, single threaded",
    170       .server_setup = &MHDT_server_setup_tls,
    171       .server_setup_cls = thread1auto,
    172       .server_runner = &MHDT_server_run_minimal,
    173     },
    174 #ifdef MHD_SUPPORT_GNUTLS
    175     {
    176       .label = "auto-selected mode, single threaded, forcing GnuTLS",
    177       .server_setup = &MHDT_server_setup_gnutls,
    178       .server_setup_cls = thread1auto,
    179       .server_runner = &MHDT_server_run_minimal,
    180     },
    181 #endif
    182 #ifdef MHD_SUPPORT_OPENSSL
    183     {
    184       .label = "auto-selected mode, single threaded, forcing OpenSSL",
    185       .server_setup = &MHDT_server_setup_openssl,
    186       .server_setup_cls = thread1auto,
    187       .server_runner = &MHDT_server_run_minimal,
    188     },
    189 #endif
    190 #if 1
    191     /* FIXME: remove once MHD_daemon_process_blocking
    192        has been implemented */
    193     {
    194       .label = "END"
    195     },
    196 #endif
    197     {
    198       .label = "auto-selected external event loop mode, no threads",
    199       .server_setup = &MHDT_server_setup_tls,
    200       .server_setup_cls = external0auto,
    201       .server_runner = &MHDT_server_run_blocking,
    202     },
    203     {
    204       .label = "END"
    205     }
    206   };
    207   struct MHDT_Phase phases[] = {
    208     {
    209       .label = "simple get",
    210       .server_cb = &MHDT_server_reply_text,
    211       .server_cb_cls = (void *)"Hello world",
    212       .client_cb = &MHDT_client_get_root,
    213       .client_cb_cls = "Hello world",
    214       .timeout_ms = 2500,
    215       .use_tls = true,
    216       .http_version = 1,
    217     },
    218     {
    219       .label = "GET with sendfile",
    220       .server_cb = &MHDT_server_reply_file,
    221       .server_cb_cls = (void *)"Hello world",
    222       .client_cb = &MHDT_client_get_root,
    223       .client_cb_cls = "Hello world",
    224       .timeout_ms = 2500,
    225       .use_tls = true,
    226       .http_version = 1,
    227     },
    228     {
    229       .label = "client PUT with content-length",
    230       .server_cb = &MHDT_server_reply_check_upload,
    231       .server_cb_cls = (void *)"simple-upload-value",
    232       .client_cb = &MHDT_client_put_data,
    233       .client_cb_cls = "simple-upload-value",
    234       .timeout_ms = 2500,
    235       .use_tls = true,
    236       .http_version = 1,
    237     },
    238     {
    239       .label = "client PUT with 2 chunks",
    240       .server_cb = &MHDT_server_reply_check_upload,
    241       .server_cb_cls = (void *)"chunky-upload-value",
    242       .client_cb = &MHDT_client_chunk_data,
    243       .client_cb_cls = "chunky-upload-value",
    244       .timeout_ms = 2500,
    245       .use_tls = true,
    246       .http_version = 1,
    247     },
    248     {
    249       .label = "client request with custom header",
    250       .server_cb = &MHDT_server_reply_check_header,
    251       .server_cb_cls = (void *)"C-Header:testvalue",
    252       .client_cb = &MHDT_client_set_header,
    253       .client_cb_cls = "C-Header:testvalue",
    254       .timeout_ms = 2500,
    255       .use_tls = true,
    256       .http_version = 1,
    257     },
    258     {
    259       .label = "server response with custom header",
    260       .server_cb = &MHDT_server_reply_with_header,
    261       .server_cb_cls = (void *)"X-Header:testvalue",
    262       .client_cb = &MHDT_client_expect_header,
    263       .client_cb_cls = "X-Header:testvalue",
    264       .timeout_ms = 2500,
    265       .use_tls = true,
    266       .http_version = 1,
    267     },
    268     {
    269       .label = "URL with query parameters 1",
    270       .server_cb = &MHDT_server_reply_check_query,
    271       .server_cb_cls = (void *)"a=b&c",
    272       .client_cb = &MHDT_client_get_with_query,
    273       .client_cb_cls = "?a=b&c",
    274       .timeout_ms = 5000,
    275       .num_clients = 4,
    276       .use_tls = true,
    277       .http_version = 1,
    278     },
    279     {
    280       .label = "URL with query parameters 2",
    281       .server_cb = &MHDT_server_reply_check_query,
    282       .server_cb_cls = (void *)"a=b&c",  /* a => b, c => NULL */
    283       .client_cb = &MHDT_client_get_with_query,
    284       .client_cb_cls = "?c&a=b",
    285       .timeout_ms = 5000,
    286       .num_clients = 1,
    287       .use_tls = true,
    288       .http_version = 1,
    289     },
    290     {
    291       .label = "URL with query parameters 3",
    292       .server_cb = &MHDT_server_reply_check_query,
    293       .server_cb_cls = (void *)"a=&c",  /* a => "", c => NULL */
    294       .client_cb = &MHDT_client_get_with_query,
    295       .client_cb_cls = "?c&a=",
    296       .timeout_ms = 5000,
    297       .num_clients = 1,
    298       .use_tls = true,
    299       .http_version = 1,
    300     },
    301     {
    302       .label = "URL with query parameters 4",
    303       .server_cb = &MHDT_server_reply_check_query,
    304       .server_cb_cls = (void *)"a=",  /* a => "" */
    305       .client_cb = &MHDT_client_get_with_query,
    306       .client_cb_cls = "?a=",
    307       .timeout_ms = 5000,
    308       .num_clients = 1,
    309       .use_tls = true,
    310       .http_version = 1,
    311     },
    312     {
    313       .label = "URL with query parameters 5",
    314       .server_cb = &MHDT_server_reply_check_query,
    315       .server_cb_cls = (void *)"a=b",  /* a => "b" */
    316       .client_cb = &MHDT_client_get_with_query,
    317       .client_cb_cls = "?a=b",
    318       .timeout_ms = 5000,
    319       .num_clients = 1,
    320       .use_tls = true,
    321       .http_version = 1,
    322     },
    323     {
    324       .label = "chunked response get",
    325       .server_cb = &MHDT_server_reply_chunked_text,
    326       .server_cb_cls = (void *)"Hello world",
    327       .client_cb = &MHDT_client_get_root,
    328       .client_cb_cls = "Hello world",
    329       .timeout_ms = 2500,
    330       .use_tls = true,
    331       .http_version = 1,
    332     },
    333     // TODO: chunked download
    334     {
    335       .label = NULL,
    336     },
    337   };
    338   unsigned int i;
    339   int ret = 0;
    340 
    341   (void)argc;  /* Unused. Silence compiler warning. */
    342   (void)argv;  /* Unused. Silence compiler warning. */
    343 
    344   for (i = 0; NULL != configs[i].server_setup; i++)
    345   {
    346     fprintf (stderr,
    347              "Running TLS tests with server setup '%s'\n",
    348              configs[i].label);
    349     ret = MHDT_test (configs[i].server_setup,
    350                      configs[i].server_setup_cls,
    351                      configs[i].server_runner,
    352                      configs[i].server_runner_cls,
    353                      phases);
    354     if (0 != ret)
    355     {
    356       fprintf (stderr,
    357                "Test failed with server of type '%s' (%u)\n",
    358                configs[i].label,
    359                i);
    360       break;
    361     }
    362   }
    363   return ret;
    364 }