aboutsummaryrefslogtreecommitdiff
path: root/src/testcurl/https/tls_test_common.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testcurl/https/tls_test_common.c')
-rw-r--r--src/testcurl/https/tls_test_common.c317
1 files changed, 161 insertions, 156 deletions
diff --git a/src/testcurl/https/tls_test_common.c b/src/testcurl/https/tls_test_common.c
index 367bed00..eb7cb14c 100644
--- a/src/testcurl/https/tls_test_common.c
+++ b/src/testcurl/https/tls_test_common.c
@@ -33,26 +33,26 @@ setup_ca_cert ()
33 FILE *cert_fd; 33 FILE *cert_fd;
34 34
35 if (NULL == (cert_fd = fopen (ca_cert_file_name, "wb+"))) 35 if (NULL == (cert_fd = fopen (ca_cert_file_name, "wb+")))
36 { 36 {
37 fprintf (stderr, "Error: failed to open `%s': %s\n", 37 fprintf (stderr, "Error: failed to open `%s': %s\n",
38 ca_cert_file_name, strerror (errno)); 38 ca_cert_file_name, strerror (errno));
39 return NULL; 39 return NULL;
40 } 40 }
41 if (fwrite (ca_cert_pem, sizeof (char), strlen (ca_cert_pem) + 1, cert_fd) 41 if (fwrite (ca_cert_pem, sizeof (char), strlen (ca_cert_pem) + 1, cert_fd)
42 != strlen (ca_cert_pem) + 1) 42 != strlen (ca_cert_pem) + 1)
43 { 43 {
44 fprintf (stderr, "Error: failed to write `%s. %s'\n", 44 fprintf (stderr, "Error: failed to write `%s. %s'\n",
45 ca_cert_file_name, strerror (errno)); 45 ca_cert_file_name, strerror (errno));
46 fclose (cert_fd); 46 fclose (cert_fd);
47 return NULL; 47 return NULL;
48 } 48 }
49 if (fflush (cert_fd)) 49 if (fflush (cert_fd))
50 { 50 {
51 fprintf (stderr, "Error: failed to flush ca cert file stream. %s\n", 51 fprintf (stderr, "Error: failed to flush ca cert file stream. %s\n",
52 strerror (errno)); 52 strerror (errno));
53 fclose (cert_fd); 53 fclose (cert_fd);
54 return NULL; 54 return NULL;
55 } 55 }
56 return cert_fd; 56 return cert_fd;
57} 57}
58 58
@@ -62,23 +62,23 @@ setup_ca_cert ()
62 */ 62 */
63int 63int
64test_daemon_get (void *cls, 64test_daemon_get (void *cls,
65 const char *cipher_suite, int proto_version, 65 const char *cipher_suite, int proto_version,
66 int port, 66 int port,
67 int ver_peer) 67 int ver_peer)
68{ 68{
69 CURL *c; 69 CURL *c;
70 struct CBC cbc; 70 struct CBC cbc;
71 CURLcode errornum; 71 CURLcode errornum;
72 char url[255]; 72 char url[255];
73 size_t len; 73 size_t len;
74 (void)cls; /* Unused. Silent compiler warning. */ 74 (void) cls; /* Unused. Silent compiler warning. */
75 75
76 len = strlen (test_data); 76 len = strlen (test_data);
77 if (NULL == (cbc.buf = malloc (sizeof (char) * len))) 77 if (NULL == (cbc.buf = malloc (sizeof (char) * len)))
78 { 78 {
79 fprintf (stderr, MHD_E_MEM); 79 fprintf (stderr, MHD_E_MEM);
80 return -1; 80 return -1;
81 } 81 }
82 cbc.size = len; 82 cbc.size = len;
83 cbc.pos = 0; 83 cbc.pos = 0;
84 84
@@ -115,22 +115,22 @@ test_daemon_get (void *cls,
115 crashes on my system! */ 115 crashes on my system! */
116 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); 116 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
117 if (CURLE_OK != (errornum = curl_easy_perform (c))) 117 if (CURLE_OK != (errornum = curl_easy_perform (c)))
118 { 118 {
119 fprintf (stderr, "curl_easy_perform failed: `%s'\n", 119 fprintf (stderr, "curl_easy_perform failed: `%s'\n",
120 curl_easy_strerror (errornum)); 120 curl_easy_strerror (errornum));
121 curl_easy_cleanup (c); 121 curl_easy_cleanup (c);
122 free (cbc.buf); 122 free (cbc.buf);
123 return errornum; 123 return errornum;
124 } 124 }
125 125
126 curl_easy_cleanup (c); 126 curl_easy_cleanup (c);
127 127
128 if (memcmp (cbc.buf, test_data, len) != 0) 128 if (memcmp (cbc.buf, test_data, len) != 0)
129 { 129 {
130 fprintf (stderr, "Error: local file & received file differ.\n"); 130 fprintf (stderr, "Error: local file & received file differ.\n");
131 free (cbc.buf); 131 free (cbc.buf);
132 return -1; 132 return -1;
133 } 133 }
134 134
135 free (cbc.buf); 135 free (cbc.buf);
136 return 0; 136 return 0;
@@ -141,7 +141,9 @@ void
141print_test_result (int test_outcome, char *test_name) 141print_test_result (int test_outcome, char *test_name)
142{ 142{
143 if (test_outcome != 0) 143 if (test_outcome != 0)
144 fprintf (stderr, "running test: %s [fail: %u]\n", test_name, (unsigned int)test_outcome); 144 fprintf (stderr, "running test: %s [fail: %u]\n", test_name, (unsigned
145 int)
146 test_outcome);
145#if 0 147#if 0
146 else 148 else
147 fprintf (stdout, "running test: %s [pass]\n", test_name); 149 fprintf (stdout, "running test: %s [pass]\n", test_name);
@@ -171,21 +173,21 @@ http_ahc (void *cls, struct MHD_Connection *connection,
171 static int aptr; 173 static int aptr;
172 struct MHD_Response *response; 174 struct MHD_Response *response;
173 int ret; 175 int ret;
174 (void)cls;(void)url;(void)version; /* Unused. Silent compiler warning. */ 176 (void) cls; (void) url; (void) version; /* Unused. Silent compiler warning. */
175 (void)upload_data;(void)upload_data_size; /* Unused. Silent compiler warning. */ 177 (void) upload_data; (void) upload_data_size; /* Unused. Silent compiler warning. */
176 178
177 if (0 != strcmp (method, MHD_HTTP_METHOD_GET)) 179 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
178 return MHD_NO; /* unexpected method */ 180 return MHD_NO; /* unexpected method */
179 if (&aptr != *ptr) 181 if (&aptr != *ptr)
180 { 182 {
181 /* do never respond on first call */ 183 /* do never respond on first call */
182 *ptr = &aptr; 184 *ptr = &aptr;
183 return MHD_YES; 185 return MHD_YES;
184 } 186 }
185 *ptr = NULL; /* reset when done */ 187 *ptr = NULL; /* reset when done */
186 response = MHD_create_response_from_buffer (strlen (test_data), 188 response = MHD_create_response_from_buffer (strlen (test_data),
187 (void *) test_data, 189 (void *) test_data,
188 MHD_RESPMEM_PERSISTENT); 190 MHD_RESPMEM_PERSISTENT);
189 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 191 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
190 MHD_destroy_response (response); 192 MHD_destroy_response (response);
191 return ret; 193 return ret;
@@ -198,9 +200,9 @@ http_dummy_ahc (void *cls, struct MHD_Connection *connection,
198 const char *upload_data, size_t *upload_data_size, 200 const char *upload_data, size_t *upload_data_size,
199 void **ptr) 201 void **ptr)
200{ 202{
201 (void)cls;(void)connection;(void)url;(void)method;(void)version; /* Unused. Silent compiler warning. */ 203 (void) cls; (void) connection; (void) url; (void) method; (void) version; /* Unused. Silent compiler warning. */
202 (void)upload_data;(void)upload_data_size;(void)ptr; /* Unused. Silent compiler warning. */ 204 (void) upload_data; (void) upload_data_size; (void) ptr; /* Unused. Silent compiler warning. */
203 return 0; 205 return 0;
204} 206}
205 207
206/** 208/**
@@ -213,7 +215,7 @@ http_dummy_ahc (void *cls, struct MHD_Connection *connection,
213 */ 215 */
214/* TODO have test wrap consider a NULL cbc */ 216/* TODO have test wrap consider a NULL cbc */
215int 217int
216send_curl_req (char *url, struct CBC * cbc, const char *cipher_suite, 218send_curl_req (char *url, struct CBC *cbc, const char *cipher_suite,
217 int proto_version) 219 int proto_version)
218{ 220{
219 CURL *c; 221 CURL *c;
@@ -228,10 +230,10 @@ send_curl_req (char *url, struct CBC * cbc, const char *cipher_suite,
228 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 60L); 230 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 60L);
229 231
230 if (cbc != NULL) 232 if (cbc != NULL)
231 { 233 {
232 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer); 234 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
233 curl_easy_setopt (c, CURLOPT_FILE, cbc); 235 curl_easy_setopt (c, CURLOPT_FILE, cbc);
234 } 236 }
235 237
236 /* TLS options */ 238 /* TLS options */
237 curl_easy_setopt (c, CURLOPT_SSLVERSION, proto_version); 239 curl_easy_setopt (c, CURLOPT_SSLVERSION, proto_version);
@@ -248,12 +250,12 @@ send_curl_req (char *url, struct CBC * cbc, const char *cipher_suite,
248 crashes on my system! */ 250 crashes on my system! */
249 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); 251 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
250 if (CURLE_OK != (errornum = curl_easy_perform (c))) 252 if (CURLE_OK != (errornum = curl_easy_perform (c)))
251 { 253 {
252 fprintf (stderr, "curl_easy_perform failed: `%s'\n", 254 fprintf (stderr, "curl_easy_perform failed: `%s'\n",
253 curl_easy_strerror (errornum)); 255 curl_easy_strerror (errornum));
254 curl_easy_cleanup (c); 256 curl_easy_cleanup (c);
255 return errornum; 257 return errornum;
256 } 258 }
257 curl_easy_cleanup (c); 259 curl_easy_cleanup (c);
258 260
259 return CURLE_OK; 261 return CURLE_OK;
@@ -286,27 +288,27 @@ gen_test_file_url (char *url,
286 size_t i; 288 size_t i;
287#endif /* ! WINDOWS */ 289#endif /* ! WINDOWS */
288 if (NULL == (doc_path = malloc (doc_path_len))) 290 if (NULL == (doc_path = malloc (doc_path_len)))
289 { 291 {
290 fprintf (stderr, MHD_E_MEM); 292 fprintf (stderr, MHD_E_MEM);
291 return -1; 293 return -1;
292 } 294 }
293 if (NULL == getcwd (doc_path, doc_path_len)) 295 if (NULL == getcwd (doc_path, doc_path_len))
294 { 296 {
295 fprintf (stderr, 297 fprintf (stderr,
296 "Error: failed to get working directory. %s\n", 298 "Error: failed to get working directory. %s\n",
297 strerror (errno)); 299 strerror (errno));
298 free (doc_path); 300 free (doc_path);
299 return -1; 301 return -1;
300 } 302 }
301#ifdef WINDOWS 303#ifdef WINDOWS
302 for (i = 0; i < doc_path_len; i++) 304 for (i = 0; i < doc_path_len; i++)
303 { 305 {
304 if (doc_path[i] == 0) 306 if (doc_path[i] == 0)
305 break; 307 break;
306 if (doc_path[i] == '\\') 308 if (doc_path[i] == '\\')
307 { 309 {
308 doc_path[i] = '/'; 310 doc_path[i] = '/';
309 } 311 }
310 if (doc_path[i] != ':') 312 if (doc_path[i] != ':')
311 continue; 313 continue;
312 if (i == 0) 314 if (i == 0)
@@ -322,7 +324,7 @@ gen_test_file_url (char *url,
322 "https://127.0.0.1", 324 "https://127.0.0.1",
323 port, 325 port,
324 doc_path, 326 doc_path,
325 "urlpath") >= (long long)url_len) 327 "urlpath") >= (long long) url_len)
326 ret = -1; 328 ret = -1;
327 329
328 free (doc_path); 330 free (doc_path);
@@ -343,41 +345,41 @@ test_https_transfer (void *cls,
343 int ret = 0; 345 int ret = 0;
344 struct CBC cbc; 346 struct CBC cbc;
345 char url[255]; 347 char url[255];
346 (void)cls; /* Unused. Silent compiler warning. */ 348 (void) cls; /* Unused. Silent compiler warning. */
347 349
348 len = strlen (test_data); 350 len = strlen (test_data);
349 if (NULL == (cbc.buf = malloc (sizeof (char) * len))) 351 if (NULL == (cbc.buf = malloc (sizeof (char) * len)))
350 { 352 {
351 fprintf (stderr, MHD_E_MEM); 353 fprintf (stderr, MHD_E_MEM);
352 return -1; 354 return -1;
353 } 355 }
354 cbc.size = len; 356 cbc.size = len;
355 cbc.pos = 0; 357 cbc.pos = 0;
356 358
357 if (gen_test_file_url (url, 359 if (gen_test_file_url (url,
358 sizeof (url), 360 sizeof (url),
359 port)) 361 port))
360 { 362 {
361 ret = -1; 363 ret = -1;
362 goto cleanup; 364 goto cleanup;
363 } 365 }
364 366
365 if (CURLE_OK != 367 if (CURLE_OK !=
366 send_curl_req (url, &cbc, cipher_suite, proto_version)) 368 send_curl_req (url, &cbc, cipher_suite, proto_version))
367 { 369 {
368 ret = -1; 370 ret = -1;
369 goto cleanup; 371 goto cleanup;
370 } 372 }
371 373
372 /* compare test file & daemon responce */ 374 /* compare test file & daemon responce */
373 if ( (len != strlen (test_data)) || 375 if ( (len != strlen (test_data)) ||
374 (memcmp (cbc.buf, 376 (memcmp (cbc.buf,
375 test_data, 377 test_data,
376 len) != 0) ) 378 len) != 0) )
377 { 379 {
378 fprintf (stderr, "Error: local file & received file differ.\n"); 380 fprintf (stderr, "Error: local file & received file differ.\n");
379 ret = -1; 381 ret = -1;
380 } 382 }
381cleanup: 383cleanup:
382 free (cbc.buf); 384 free (cbc.buf);
383 return ret; 385 return ret;
@@ -392,28 +394,29 @@ cleanup:
392 * @return port number on success or zero on failure 394 * @return port number on success or zero on failure
393 */ 395 */
394int 396int
395setup_testcase (struct MHD_Daemon **d, int port, int daemon_flags, va_list arg_list) 397setup_testcase (struct MHD_Daemon **d, int port, int daemon_flags, va_list
398 arg_list)
396{ 399{
397 *d = MHD_start_daemon_va (daemon_flags, port, 400 *d = MHD_start_daemon_va (daemon_flags, port,
398 NULL, NULL, &http_ahc, NULL, arg_list); 401 NULL, NULL, &http_ahc, NULL, arg_list);
399 402
400 if (*d == NULL) 403 if (*d == NULL)
401 { 404 {
402 fprintf (stderr, MHD_E_SERVER_INIT); 405 fprintf (stderr, MHD_E_SERVER_INIT);
403 return 0; 406 return 0;
404 } 407 }
405 408
406 if (0 == port) 409 if (0 == port)
410 {
411 const union MHD_DaemonInfo *dinfo;
412 dinfo = MHD_get_daemon_info (*d, MHD_DAEMON_INFO_BIND_PORT);
413 if ((NULL == dinfo) ||(0 == dinfo->port) )
407 { 414 {
408 const union MHD_DaemonInfo *dinfo; 415 MHD_stop_daemon (*d);
409 dinfo = MHD_get_daemon_info (*d, MHD_DAEMON_INFO_BIND_PORT); 416 return 0;
410 if (NULL == dinfo || 0 == dinfo->port)
411 {
412 MHD_stop_daemon (*d);
413 return 0;
414 }
415 port = (int)dinfo->port;
416 } 417 }
418 port = (int) dinfo->port;
419 }
417 420
418 return port; 421 return port;
419} 422}
@@ -425,10 +428,10 @@ teardown_testcase (struct MHD_Daemon *d)
425} 428}
426 429
427int 430int
428setup_session (gnutls_session_t * session, 431setup_session (gnutls_session_t *session,
429 gnutls_datum_t * key, 432 gnutls_datum_t *key,
430 gnutls_datum_t * cert, 433 gnutls_datum_t *cert,
431 gnutls_certificate_credentials_t * xcred) 434 gnutls_certificate_credentials_t *xcred)
432{ 435{
433 int ret; 436 int ret;
434 const char *err_pos; 437 const char *err_pos;
@@ -437,42 +440,42 @@ setup_session (gnutls_session_t * session,
437 key->size = strlen (srv_key_pem) + 1; 440 key->size = strlen (srv_key_pem) + 1;
438 key->data = malloc (key->size); 441 key->data = malloc (key->size);
439 if (NULL == key->data) 442 if (NULL == key->data)
440 { 443 {
441 gnutls_certificate_free_credentials (*xcred); 444 gnutls_certificate_free_credentials (*xcred);
442 return -1; 445 return -1;
443 } 446 }
444 memcpy (key->data, srv_key_pem, key->size); 447 memcpy (key->data, srv_key_pem, key->size);
445 cert->size = strlen (srv_self_signed_cert_pem) + 1; 448 cert->size = strlen (srv_self_signed_cert_pem) + 1;
446 cert->data = malloc (cert->size); 449 cert->data = malloc (cert->size);
447 if (NULL == cert->data) 450 if (NULL == cert->data)
448 { 451 {
449 gnutls_certificate_free_credentials (*xcred); 452 gnutls_certificate_free_credentials (*xcred);
450 free (key->data); 453 free (key->data);
451 return -1; 454 return -1;
452 } 455 }
453 memcpy (cert->data, srv_self_signed_cert_pem, cert->size); 456 memcpy (cert->data, srv_self_signed_cert_pem, cert->size);
454 gnutls_certificate_set_x509_key_mem (*xcred, cert, key, 457 gnutls_certificate_set_x509_key_mem (*xcred, cert, key,
455 GNUTLS_X509_FMT_PEM); 458 GNUTLS_X509_FMT_PEM);
456 gnutls_init (session, GNUTLS_CLIENT); 459 gnutls_init (session, GNUTLS_CLIENT);
457 ret = gnutls_priority_set_direct (*session, 460 ret = gnutls_priority_set_direct (*session,
458 "NORMAL", &err_pos); 461 "NORMAL", &err_pos);
459 if (ret < 0) 462 if (ret < 0)
460 { 463 {
461 gnutls_deinit (*session); 464 gnutls_deinit (*session);
462 gnutls_certificate_free_credentials (*xcred); 465 gnutls_certificate_free_credentials (*xcred);
463 free (key->data); 466 free (key->data);
464 return -1; 467 return -1;
465 } 468 }
466 gnutls_credentials_set (*session, 469 gnutls_credentials_set (*session,
467 GNUTLS_CRD_CERTIFICATE, 470 GNUTLS_CRD_CERTIFICATE,
468 *xcred); 471 *xcred);
469 return 0; 472 return 0;
470} 473}
471 474
472int 475int
473teardown_session (gnutls_session_t session, 476teardown_session (gnutls_session_t session,
474 gnutls_datum_t * key, 477 gnutls_datum_t *key,
475 gnutls_datum_t * cert, 478 gnutls_datum_t *cert,
476 gnutls_certificate_credentials_t xcred) 479 gnutls_certificate_credentials_t xcred)
477{ 480{
478 free (key->data); 481 free (key->data);
@@ -489,37 +492,37 @@ teardown_session (gnutls_session_t session,
489/* TODO test_wrap: change sig to (setup_func, test, va_list test_arg) */ 492/* TODO test_wrap: change sig to (setup_func, test, va_list test_arg) */
490int 493int
491test_wrap (const char *test_name, int 494test_wrap (const char *test_name, int
492 (*test_function) (void * cls, int port, const char *cipher_suite, 495 (*test_function)(void *cls, int port, const char *cipher_suite,
493 int proto_version), void * cls, 496 int proto_version), void *cls,
494 int port, 497 int port,
495 int daemon_flags, const char *cipher_suite, int proto_version, ...) 498 int daemon_flags, const char *cipher_suite, int proto_version, ...)
496{ 499{
497 int ret; 500 int ret;
498 va_list arg_list; 501 va_list arg_list;
499 struct MHD_Daemon *d; 502 struct MHD_Daemon *d;
500 (void)cls; /* Unused. Silent compiler warning. */ 503 (void) cls; /* Unused. Silent compiler warning. */
501 504
502 va_start (arg_list, proto_version); 505 va_start (arg_list, proto_version);
503 port = setup_testcase (&d, port, daemon_flags, arg_list); 506 port = setup_testcase (&d, port, daemon_flags, arg_list);
504 if (0 == port) 507 if (0 == port)
505 { 508 {
506 va_end (arg_list); 509 va_end (arg_list);
507 fprintf (stderr, "Failed to setup testcase %s\n", test_name); 510 fprintf (stderr, "Failed to setup testcase %s\n", test_name);
508 return -1; 511 return -1;
509 } 512 }
510#if 0 513#if 0
511 fprintf (stdout, "running test: %s ", test_name); 514 fprintf (stdout, "running test: %s ", test_name);
512#endif 515#endif
513 ret = test_function (NULL, port, cipher_suite, proto_version); 516 ret = test_function (NULL, port, cipher_suite, proto_version);
514#if 0 517#if 0
515 if (ret == 0) 518 if (ret == 0)
516 { 519 {
517 fprintf (stdout, "[pass]\n"); 520 fprintf (stdout, "[pass]\n");
518 } 521 }
519 else 522 else
520 { 523 {
521 fprintf (stdout, "[fail]\n"); 524 fprintf (stdout, "[fail]\n");
522 } 525 }
523#endif 526#endif
524 teardown_testcase (d); 527 teardown_testcase (d);
525 va_end (arg_list); 528 va_end (arg_list);
@@ -532,17 +535,19 @@ testsuite_curl_global_init (void)
532{ 535{
533 CURLcode res; 536 CURLcode res;
534#if LIBCURL_VERSION_NUM >= 0x073800 537#if LIBCURL_VERSION_NUM >= 0x073800
535 if (CURLSSLSET_OK != curl_global_sslset(CURLSSLBACKEND_GNUTLS, NULL, NULL)) 538 if (CURLSSLSET_OK != curl_global_sslset (CURLSSLBACKEND_GNUTLS, NULL, NULL))
536 { 539 {
537 if (CURLSSLSET_TOO_LATE == curl_global_sslset(CURLSSLBACKEND_OPENSSL, NULL, NULL)) 540 if (CURLSSLSET_TOO_LATE == curl_global_sslset (CURLSSLBACKEND_OPENSSL, NULL,
538 fprintf (stderr, "WARNING: libcurl was already initialised.\n"); 541 NULL))
539 } 542 fprintf (stderr, "WARNING: libcurl was already initialised.\n");
543 }
540#endif /* LIBCURL_VERSION_NUM >= 0x07380 */ 544#endif /* LIBCURL_VERSION_NUM >= 0x07380 */
541 res = curl_global_init (CURL_GLOBAL_ALL); 545 res = curl_global_init (CURL_GLOBAL_ALL);
542 if (CURLE_OK != res) 546 if (CURLE_OK != res)
543 { 547 {
544 fprintf (stderr, "libcurl initialisation error: %s\n", curl_easy_strerror(res)); 548 fprintf (stderr, "libcurl initialisation error: %s\n", curl_easy_strerror (
545 return 0; 549 res));
546 } 550 return 0;
551 }
547 return 1; 552 return 1;
548} 553}