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_multi_daemon_test.c
23 * @brief  Testcase for libmicrohttpd multiple HTTPS daemon scenario
24 * @author Sagie Amir
25 */
26
27#include "platform.h"
28#include "microhttpd.h"
29#include <curl/curl.h>
30#include <limits.h>
31#include <sys/stat.h>
32#include <gcrypt.h>
33#include "tls_test_common.h"
34
35extern int curl_check_version (const char *req_version, ...);
36extern const char srv_key_pem[];
37extern const char srv_self_signed_cert_pem[];
38
39/*
40 * assert initiating two separate daemons and having one shut down
41 * doesn't affect the other
42 */
43static int
44test_concurent_daemon_pair (void *cls,
45			    const char *cipher_suite,
46                            int proto_version)
47{
48
49  int ret;
50  struct MHD_Daemon *d1;
51  struct MHD_Daemon *d2;
52
53  d1 = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_SSL |
54                         MHD_USE_DEBUG, DEAMON_TEST_PORT,
55                         NULL, NULL, &http_ahc, NULL,
56                         MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem,
57                         MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem,
58                         MHD_OPTION_END);
59
60  if (d1 == NULL)
61    {
62      fprintf (stderr, MHD_E_SERVER_INIT);
63      return -1;
64    }
65
66  d2 = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_SSL |
67                         MHD_USE_DEBUG, DEAMON_TEST_PORT + 1,
68                         NULL, NULL, &http_ahc, NULL,
69                         MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem,
70                         MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem,
71                         MHD_OPTION_END);
72
73  if (d2 == NULL)
74    {
75      MHD_stop_daemon (d1);
76      fprintf (stderr, MHD_E_SERVER_INIT);
77      return -1;
78    }
79
80  ret =
81    test_daemon_get (NULL, cipher_suite, proto_version, DEAMON_TEST_PORT, 0);
82  ret +=
83    test_daemon_get (NULL, cipher_suite, proto_version,
84                     DEAMON_TEST_PORT + 1, 0);
85
86  MHD_stop_daemon (d2);
87  ret +=
88    test_daemon_get (NULL, cipher_suite, proto_version, DEAMON_TEST_PORT, 0);
89  MHD_stop_daemon (d1);
90  return ret;
91}
92
93
94int
95main (int argc, char *const *argv)
96{
97  unsigned int errorCount = 0;
98  FILE *cert;
99
100  gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
101#ifdef GCRYCTL_INITIALIZATION_FINISHED
102  gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
103#endif
104  if (0 != curl_global_init (CURL_GLOBAL_ALL))
105    {
106      fprintf (stderr, "Error (code: %u). l:%d f:%s\n", errorCount, __LINE__,
107               __FUNCTION__);
108      return -1;
109    }
110  if ((cert = setup_ca_cert ()) == NULL)
111    {
112      fprintf (stderr, MHD_E_TEST_FILE_CREAT);
113      return -1;
114    }
115
116  const char *aes256_sha = "AES256-SHA";
117  if (curl_uses_nss_ssl() == 0)
118    {
119      aes256_sha = "rsa_aes_256_sha";
120    }
121
122  errorCount +=
123    test_concurent_daemon_pair (NULL, aes256_sha, CURL_SSLVERSION_TLSv1);
124
125  print_test_result (errorCount, "concurent_daemon_pair");
126
127  curl_global_cleanup ();
128  fclose (cert);
129  if (0 != remove (ca_cert_file_name))
130    fprintf (stderr,
131	     "Failed to remove `%s'\n",
132	     ca_cert_file_name);
133  return errorCount != 0;
134}
135