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 tls_thread_mode_test.c
23 * @brief  Testcase for libmicrohttpd HTTPS GET operations
24 * @author Sagie Amir
25 * @author Christian Grothoff
26 *
27 * TODO: add test for external select!
28 */
29
30#include "platform.h"
31#include "microhttpd.h"
32#include <sys/stat.h>
33#include <limits.h>
34#include <curl/curl.h>
35#include <pthread.h>
36#include <gcrypt.h>
37#include "tls_test_common.h"
38
39#if defined(CPU_COUNT) && (CPU_COUNT+0) < 4
40#undef CPU_COUNT
41#endif
42#if !defined(CPU_COUNT)
43#define CPU_COUNT 4
44#endif
45
46extern const char srv_key_pem[];
47extern const char srv_self_signed_cert_pem[];
48
49int curl_check_version (const char *req_version, ...);
50
51/**
52 * used when spawning multiple threads executing curl server requests
53 *
54 */
55static void *
56https_transfer_thread_adapter (void *args)
57{
58  static int nonnull;
59  struct https_test_data *cargs = args;
60  int ret;
61
62  /* time spread incomming requests */
63  usleep ((useconds_t) 10.0 * ((double) rand ()) / ((double) RAND_MAX));
64  ret = test_https_transfer (cargs->cls,
65                             cargs->cipher_suite, cargs->proto_version);
66  if (ret == 0)
67    return NULL;
68  return &nonnull;
69}
70
71/**
72 * Test non-parallel requests.
73 *
74 * @return: 0 upon all client requests returning '0', -1 otherwise.
75 *
76 * TODO : make client_count a parameter - numver of curl client threads to spawn
77 */
78static int
79test_single_client (void *cls, const char *cipher_suite,
80                    int curl_proto_version)
81{
82  void *client_thread_ret;
83  struct https_test_data client_args =
84    { NULL, cipher_suite, curl_proto_version };
85
86  client_thread_ret = https_transfer_thread_adapter (&client_args);
87  if (client_thread_ret != NULL)
88    return -1;
89  return 0;
90}
91
92
93/**
94 * Test parallel request handling.
95 *
96 * @return: 0 upon all client requests returning '0', -1 otherwise.
97 *
98 * TODO : make client_count a parameter - numver of curl client threads to spawn
99 */
100static int
101test_parallel_clients (void *cls, const char *cipher_suite,
102                       int curl_proto_version)
103{
104  int i;
105  int client_count = (CPU_COUNT - 1);
106  void *client_thread_ret;
107  pthread_t client_arr[client_count];
108  struct https_test_data client_args =
109    { NULL, cipher_suite, curl_proto_version };
110
111  for (i = 0; i < client_count; ++i)
112    {
113      if (pthread_create (&client_arr[i], NULL,
114                          &https_transfer_thread_adapter, &client_args) != 0)
115        {
116          fprintf (stderr, "Error: failed to spawn test client threads.\n");
117
118          return -1;
119        }
120    }
121
122  /* check all client requests fulfilled correctly */
123  for (i = 0; i < client_count; ++i)
124    {
125      if ((pthread_join (client_arr[i], &client_thread_ret) != 0) ||
126          (client_thread_ret != NULL))
127        return -1;
128    }
129
130  return 0;
131}
132
133
134int
135main (int argc, char *const *argv)
136{
137  unsigned int errorCount = 0;
138  const char *ssl_version;
139
140  /* initialize random seed used by curl clients */
141  unsigned int iseed = (unsigned int) time (NULL);
142
143#ifdef GCRYCTL_INITIALIZATION_FINISHED
144  gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
145#endif
146  srand (iseed);
147  ssl_version = curl_version_info (CURLVERSION_NOW)->ssl_version;
148  if (NULL == ssl_version)
149  {
150    fprintf (stderr, "Curl does not support SSL.  Cannot run the test.\n");
151    return 0;
152  }
153  if (0 != strncmp (ssl_version, "GnuTLS", 6))
154  {
155    fprintf (stderr, "This test can be run only with libcurl-gnutls.\n");
156    return 0;
157  }
158  if (0 != curl_global_init (CURL_GLOBAL_ALL))
159    {
160      fprintf (stderr, "Error: %s\n", strerror (errno));
161      return -1;
162    }
163
164  char *aes256_sha = "AES256-SHA";
165  if (curl_uses_nss_ssl() == 0)
166    {
167      aes256_sha = "rsa_aes_256_sha";
168    }
169
170  errorCount +=
171    test_wrap ("multi threaded daemon, single client", &test_single_client,
172               NULL,
173               MHD_USE_SSL | MHD_USE_DEBUG | MHD_USE_THREAD_PER_CONNECTION,
174               aes256_sha, CURL_SSLVERSION_TLSv1, MHD_OPTION_HTTPS_MEM_KEY,
175               srv_key_pem, MHD_OPTION_HTTPS_MEM_CERT,
176               srv_self_signed_cert_pem, MHD_OPTION_END);
177
178  errorCount +=
179    test_wrap ("multi threaded daemon, parallel client",
180               &test_parallel_clients, NULL,
181               MHD_USE_SSL | MHD_USE_DEBUG | MHD_USE_THREAD_PER_CONNECTION,
182               aes256_sha, CURL_SSLVERSION_TLSv1, MHD_OPTION_HTTPS_MEM_KEY,
183               srv_key_pem, MHD_OPTION_HTTPS_MEM_CERT,
184               srv_self_signed_cert_pem, MHD_OPTION_END);
185
186  if (errorCount != 0)
187    fprintf (stderr, "Failed test: %s.\n", argv[0]);
188
189  curl_global_cleanup ();
190  return errorCount != 0;
191}
192