diff options
Diffstat (limited to 'src/testcurl/https/tls_multi_thread_mode_test.c')
-rw-r--r-- | src/testcurl/https/tls_multi_thread_mode_test.c | 481 |
1 files changed, 481 insertions, 0 deletions
diff --git a/src/testcurl/https/tls_multi_thread_mode_test.c b/src/testcurl/https/tls_multi_thread_mode_test.c new file mode 100644 index 00000000..5b0533fb --- /dev/null +++ b/src/testcurl/https/tls_multi_thread_mode_test.c | |||
@@ -0,0 +1,481 @@ | |||
1 | /* | ||
2 | This file is part of libmicrohttpd | ||
3 | (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 | |||
33 | #include <sys/stat.h> | ||
34 | #include <limits.h> | ||
35 | #include "gnutls.h" | ||
36 | #include <curl/curl.h> | ||
37 | |||
38 | #define DEBUG_CURL_VERBOSE 0 | ||
39 | #define PAGE_NOT_FOUND "<html><head><title>File not found</title></head><body>File not found</body></html>" | ||
40 | |||
41 | #define MHD_E_MEM "Error: memory error\n" | ||
42 | #define MHD_E_SERVER_INIT "Error: failed to start server\n" | ||
43 | #define MHD_E_TEST_FILE_CREAT "Error: failed to setup test file\n" | ||
44 | #define MHD_E_CERT_FILE_CREAT "Error: failed to setup test certificate\n" | ||
45 | #define MHD_E_KEY_FILE_CREAT "Error: failed to setup test certificate\n" | ||
46 | |||
47 | #include "tls_test_keys.h" | ||
48 | |||
49 | const char *test_file_name = "https_test_file"; | ||
50 | const char test_file_data[] = "Hello World\n"; | ||
51 | |||
52 | int curl_check_version (const char *req_version, ...); | ||
53 | |||
54 | struct CBC | ||
55 | { | ||
56 | char *buf; | ||
57 | size_t pos; | ||
58 | size_t size; | ||
59 | }; | ||
60 | |||
61 | struct https_test_data | ||
62 | { | ||
63 | FILE *test_fd; | ||
64 | char *cipher_suite; | ||
65 | int proto_version; | ||
66 | }; | ||
67 | |||
68 | struct CipherDef | ||
69 | { | ||
70 | int options[2]; | ||
71 | char *curlname; | ||
72 | }; | ||
73 | |||
74 | static size_t | ||
75 | copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx) | ||
76 | { | ||
77 | struct CBC *cbc = ctx; | ||
78 | |||
79 | if (cbc->pos + size * nmemb > cbc->size) | ||
80 | return 0; /* overflow */ | ||
81 | memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb); | ||
82 | cbc->pos += size * nmemb; | ||
83 | return size * nmemb; | ||
84 | } | ||
85 | |||
86 | static int | ||
87 | file_reader (void *cls, size_t pos, char *buf, int max) | ||
88 | { | ||
89 | FILE *file = cls; | ||
90 | fseek (file, pos, SEEK_SET); | ||
91 | return fread (buf, 1, max, file); | ||
92 | } | ||
93 | |||
94 | /* HTTP access handler call back */ | ||
95 | static int | ||
96 | http_ahc (void *cls, struct MHD_Connection *connection, | ||
97 | const char *url, const char *method, const char *upload_data, | ||
98 | const char *version, unsigned int *upload_data_size, void **ptr) | ||
99 | { | ||
100 | static int aptr; | ||
101 | struct MHD_Response *response; | ||
102 | int ret; | ||
103 | FILE *file; | ||
104 | struct stat buf; | ||
105 | |||
106 | if (0 != strcmp (method, MHD_HTTP_METHOD_GET)) | ||
107 | return MHD_NO; /* unexpected method */ | ||
108 | if (&aptr != *ptr) | ||
109 | { | ||
110 | /* do never respond on first call */ | ||
111 | *ptr = &aptr; | ||
112 | return MHD_YES; | ||
113 | } | ||
114 | *ptr = NULL; /* reset when done */ | ||
115 | |||
116 | file = fopen (url, "r"); | ||
117 | if (file == NULL) | ||
118 | { | ||
119 | response = MHD_create_response_from_data (strlen (PAGE_NOT_FOUND), | ||
120 | (void *) PAGE_NOT_FOUND, | ||
121 | MHD_NO, MHD_NO); | ||
122 | ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response); | ||
123 | MHD_destroy_response (response); | ||
124 | } | ||
125 | else | ||
126 | { | ||
127 | stat (url, &buf); | ||
128 | response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k PAGE_NOT_FOUND size */ | ||
129 | &file_reader, file, | ||
130 | (MHD_ContentReaderFreeCallback) | ||
131 | & fclose); | ||
132 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | ||
133 | MHD_destroy_response (response); | ||
134 | } | ||
135 | return ret; | ||
136 | } | ||
137 | |||
138 | |||
139 | |||
140 | /** | ||
141 | * test HTTPS transfer | ||
142 | * @param test_fd: file to attempt transfering | ||
143 | */ | ||
144 | static int | ||
145 | test_https_transfer (FILE * test_fd, char *cipher_suite, int proto_version) | ||
146 | { | ||
147 | CURL *c; | ||
148 | CURLcode errornum; | ||
149 | struct CBC cbc; | ||
150 | char *doc_path; | ||
151 | size_t doc_path_len; | ||
152 | char url[255]; | ||
153 | struct stat statb; | ||
154 | |||
155 | stat (test_file_name, &statb); | ||
156 | |||
157 | int len = statb.st_size; | ||
158 | |||
159 | /* used to memcmp local copy & deamon supplied copy */ | ||
160 | unsigned char *mem_test_file_local; | ||
161 | |||
162 | /* setup test file path, url */ | ||
163 | doc_path_len = PATH_MAX > 4096 ? 4096 : PATH_MAX; | ||
164 | if (NULL == (doc_path = malloc (doc_path_len))) | ||
165 | { | ||
166 | fclose (test_fd); | ||
167 | fprintf (stderr, MHD_E_MEM); | ||
168 | return -1; | ||
169 | } | ||
170 | if (getcwd (doc_path, doc_path_len) == NULL) | ||
171 | { | ||
172 | fclose (test_fd); | ||
173 | free (doc_path); | ||
174 | fprintf (stderr, "Error: failed to get working directory. %s\n", | ||
175 | strerror (errno)); | ||
176 | return -1; | ||
177 | } | ||
178 | |||
179 | if (NULL == (mem_test_file_local = malloc (len))) | ||
180 | { | ||
181 | fclose (test_fd); | ||
182 | free (doc_path); | ||
183 | fprintf (stderr, MHD_E_MEM); | ||
184 | return -1; | ||
185 | } | ||
186 | |||
187 | fseek (test_fd, 0, SEEK_SET); | ||
188 | if (fread (mem_test_file_local, sizeof (char), len, test_fd) != len) | ||
189 | { | ||
190 | fclose (test_fd); | ||
191 | free (doc_path); | ||
192 | fprintf (stderr, "Error: failed to read test file. %s\n", | ||
193 | strerror (errno)); | ||
194 | return -1; | ||
195 | } | ||
196 | |||
197 | if (NULL == (cbc.buf = malloc (len))) | ||
198 | { | ||
199 | fclose (test_fd); | ||
200 | free (doc_path); | ||
201 | free (mem_test_file_local); | ||
202 | fprintf (stderr, MHD_E_MEM); | ||
203 | return -1; | ||
204 | } | ||
205 | cbc.size = len; | ||
206 | cbc.pos = 0; | ||
207 | |||
208 | /* construct url - this might use doc_path */ | ||
209 | sprintf (url, "%s%s/%s", "https://localhost:42433", | ||
210 | doc_path, test_file_name); | ||
211 | |||
212 | c = curl_easy_init (); | ||
213 | #if DEBUG_CURL_VERBOSE | ||
214 | curl_easy_setopt (c, CURLOPT_VERBOSE, 1); | ||
215 | #endif | ||
216 | curl_easy_setopt (c, CURLOPT_URL, url); | ||
217 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); | ||
218 | curl_easy_setopt (c, CURLOPT_TIMEOUT, 15L); | ||
219 | curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L); | ||
220 | curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); | ||
221 | curl_easy_setopt (c, CURLOPT_FILE, &cbc); | ||
222 | |||
223 | /* TLS options */ | ||
224 | curl_easy_setopt (c, CURLOPT_SSLVERSION, proto_version); | ||
225 | curl_easy_setopt (c, CURLOPT_SSL_CIPHER_LIST, cipher_suite); | ||
226 | |||
227 | /* currently skip any peer authentication */ | ||
228 | curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0); | ||
229 | curl_easy_setopt (c, CURLOPT_SSL_VERIFYHOST, 0); | ||
230 | |||
231 | curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); | ||
232 | |||
233 | /* NOTE: use of CONNECTTIMEOUT without also | ||
234 | setting NOSIGNAL results in really weird | ||
235 | crashes on my system! */ | ||
236 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); | ||
237 | if (CURLE_OK != (errornum = curl_easy_perform (c))) | ||
238 | { | ||
239 | fprintf (stderr, "curl_easy_perform failed: `%s'\n", | ||
240 | curl_easy_strerror (errornum)); | ||
241 | curl_easy_cleanup (c); | ||
242 | free (cbc.buf); | ||
243 | free (mem_test_file_local); | ||
244 | free (doc_path); | ||
245 | return errornum; | ||
246 | } | ||
247 | |||
248 | curl_easy_cleanup (c); | ||
249 | |||
250 | if (memcmp (cbc.buf, mem_test_file_local, len) != 0) | ||
251 | { | ||
252 | fprintf (stderr, "Error: local file & received file differ.\n"); | ||
253 | free (cbc.buf); | ||
254 | free (mem_test_file_local); | ||
255 | free (doc_path); | ||
256 | return -1; | ||
257 | } | ||
258 | |||
259 | free (mem_test_file_local); | ||
260 | free (cbc.buf); | ||
261 | free (doc_path); | ||
262 | return 0; | ||
263 | } | ||
264 | |||
265 | /** | ||
266 | * used when spawning multiple threads executing curl server requests | ||
267 | * | ||
268 | */ | ||
269 | static void * | ||
270 | https_transfer_thread_adapter (void *args) | ||
271 | { | ||
272 | static int nonnull; | ||
273 | struct https_test_data *cargs = args; | ||
274 | int ret; | ||
275 | |||
276 | /* time spread incomming requests */ | ||
277 | usleep ((useconds_t) 10.0 * ((double) rand ()) / ((double) RAND_MAX)); | ||
278 | ret = test_https_transfer (cargs->test_fd, | ||
279 | cargs->cipher_suite, cargs->proto_version); | ||
280 | if (ret == 0) | ||
281 | return NULL; | ||
282 | return &nonnull; | ||
283 | } | ||
284 | |||
285 | static FILE * | ||
286 | setupTestFile () | ||
287 | { | ||
288 | FILE *test_fd; | ||
289 | |||
290 | if (NULL == (test_fd = fopen (test_file_name, "w+"))) | ||
291 | { | ||
292 | fprintf (stderr, "Error: failed to open `%s': %s\n", | ||
293 | test_file_name, strerror (errno)); | ||
294 | return NULL; | ||
295 | } | ||
296 | if (fwrite (test_file_data, sizeof (char), strlen (test_file_data), test_fd) | ||
297 | != strlen (test_file_data)) | ||
298 | { | ||
299 | fprintf (stderr, "Error: failed to write `%s. %s'\n", | ||
300 | test_file_name, strerror (errno)); | ||
301 | return NULL; | ||
302 | } | ||
303 | if (fflush (test_fd)) | ||
304 | { | ||
305 | fprintf (stderr, "Error: failed to flush test file stream. %s\n", | ||
306 | strerror (errno)); | ||
307 | return NULL; | ||
308 | } | ||
309 | |||
310 | return test_fd; | ||
311 | } | ||
312 | |||
313 | static int | ||
314 | setup (struct MHD_Daemon **d, int daemon_flags, va_list arg_list) | ||
315 | { | ||
316 | *d = MHD_start_daemon_va (daemon_flags, 42433, | ||
317 | NULL, NULL, &http_ahc, NULL, arg_list); | ||
318 | |||
319 | if (*d == NULL) | ||
320 | { | ||
321 | fprintf (stderr, MHD_E_SERVER_INIT); | ||
322 | return -1; | ||
323 | } | ||
324 | |||
325 | return 0; | ||
326 | } | ||
327 | |||
328 | static void | ||
329 | teardown (struct MHD_Daemon *d) | ||
330 | { | ||
331 | MHD_stop_daemon (d); | ||
332 | } | ||
333 | |||
334 | /* TODO test_wrap: change sig to (setup_func, test, va_list test_arg) & move to test_util.c */ | ||
335 | static int | ||
336 | test_wrap (char *test_name, int | ||
337 | (*test_function) (FILE * test_fd, char *cipher_suite, | ||
338 | int proto_version), FILE * test_fd, | ||
339 | int daemon_flags, char *cipher_suite, int proto_version, ...) | ||
340 | { | ||
341 | int ret; | ||
342 | va_list arg_list; | ||
343 | struct MHD_Daemon *d; | ||
344 | |||
345 | va_start (arg_list, proto_version); | ||
346 | if (setup (&d, daemon_flags, arg_list) != 0) | ||
347 | { | ||
348 | va_end (arg_list); | ||
349 | return -1; | ||
350 | } | ||
351 | |||
352 | fprintf (stdout, "running test: %s ", test_name); | ||
353 | ret = test_function (test_fd, cipher_suite, proto_version); | ||
354 | |||
355 | if (ret == 0) | ||
356 | { | ||
357 | fprintf (stdout, "[pass]\n"); | ||
358 | } | ||
359 | else | ||
360 | { | ||
361 | fprintf (stdout, "[fail]\n"); | ||
362 | } | ||
363 | |||
364 | teardown (d); | ||
365 | va_end (arg_list); | ||
366 | return ret; | ||
367 | } | ||
368 | |||
369 | /** | ||
370 | * Test non-parallel requests. | ||
371 | * | ||
372 | * @return: 0 upon all client requests returning '0', -1 otherwise. | ||
373 | * | ||
374 | * TODO : make client_count a parameter - numver of curl client threads to spawn | ||
375 | */ | ||
376 | static int | ||
377 | test_single_client (FILE * test_fd, char *cipher_suite, | ||
378 | int curl_proto_version) | ||
379 | { | ||
380 | void *client_thread_ret; | ||
381 | struct https_test_data client_args = | ||
382 | { test_fd, cipher_suite, curl_proto_version }; | ||
383 | |||
384 | client_thread_ret = https_transfer_thread_adapter (&client_args); | ||
385 | if (client_thread_ret != NULL) | ||
386 | return -1; | ||
387 | return 0; | ||
388 | } | ||
389 | |||
390 | |||
391 | /** | ||
392 | * Test parallel request handling. | ||
393 | * | ||
394 | * @return: 0 upon all client requests returning '0', -1 otherwise. | ||
395 | * | ||
396 | * TODO : make client_count a parameter - numver of curl client threads to spawn | ||
397 | */ | ||
398 | static int | ||
399 | test_parallel_clients (FILE * test_fd, char *cipher_suite, | ||
400 | int curl_proto_version) | ||
401 | { | ||
402 | int i; | ||
403 | int client_count = 3; | ||
404 | void *client_thread_ret; | ||
405 | pthread_t client_arr[client_count]; | ||
406 | struct https_test_data client_args = | ||
407 | { test_fd, cipher_suite, curl_proto_version }; | ||
408 | |||
409 | for (i = 0; i < client_count; ++i) | ||
410 | { | ||
411 | if (pthread_create (&client_arr[i], NULL, | ||
412 | &https_transfer_thread_adapter, | ||
413 | &client_args) != 0) | ||
414 | { | ||
415 | fprintf (stderr, "Error: failed to spawn test client threads.\n"); | ||
416 | |||
417 | return -1; | ||
418 | } | ||
419 | } | ||
420 | |||
421 | /* check all client requests fulfilled correctly */ | ||
422 | for (i = 0; i < client_count; ++i) | ||
423 | { | ||
424 | if ((pthread_join (client_arr[i], &client_thread_ret) != 0) || | ||
425 | (client_thread_ret != NULL)) | ||
426 | return -1; | ||
427 | } | ||
428 | |||
429 | return 0; | ||
430 | } | ||
431 | |||
432 | |||
433 | int | ||
434 | main (int argc, char *const *argv) | ||
435 | { | ||
436 | FILE *test_fd; | ||
437 | unsigned int errorCount = 0; | ||
438 | |||
439 | /* initialize random seed used by curl clients */ | ||
440 | unsigned int iseed = (unsigned int) time (NULL); | ||
441 | srand (iseed); | ||
442 | |||
443 | if (curl_check_version (MHD_REQ_CURL_VERSION)) | ||
444 | return -1; | ||
445 | |||
446 | if ((test_fd = setupTestFile ()) == NULL) | ||
447 | { | ||
448 | fprintf (stderr, MHD_E_TEST_FILE_CREAT); | ||
449 | return -1; | ||
450 | } | ||
451 | |||
452 | if (0 != curl_global_init (CURL_GLOBAL_ALL)) | ||
453 | { | ||
454 | fprintf (stderr, "Error: %s\n", strerror (errno)); | ||
455 | return -1; | ||
456 | } | ||
457 | |||
458 | errorCount += | ||
459 | test_wrap ("multi threaded daemon, single client", &test_single_client, | ||
460 | test_fd, MHD_USE_SSL | MHD_USE_DEBUG | MHD_USE_THREAD_PER_CONNECTION, "AES256-SHA", | ||
461 | CURL_SSLVERSION_TLSv1, MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem, | ||
462 | MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem, | ||
463 | MHD_OPTION_END); | ||
464 | |||
465 | errorCount += | ||
466 | test_wrap ("multi threaded daemon, parallel client", | ||
467 | &test_parallel_clients, test_fd, MHD_USE_SSL | MHD_USE_DEBUG | MHD_USE_THREAD_PER_CONNECTION, | ||
468 | "AES256-SHA", CURL_SSLVERSION_TLSv1, MHD_OPTION_HTTPS_MEM_KEY, | ||
469 | srv_key_pem, MHD_OPTION_HTTPS_MEM_CERT, | ||
470 | srv_self_signed_cert_pem, MHD_OPTION_END); | ||
471 | |||
472 | if (errorCount != 0) | ||
473 | fprintf (stderr, "Failed test: %s.\n", argv[0]); | ||
474 | |||
475 | curl_global_cleanup (); | ||
476 | fclose (test_fd); | ||
477 | |||
478 | remove (test_file_name); | ||
479 | |||
480 | return errorCount != 0; | ||
481 | } | ||