test_https_get_parallel.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. This file is part of libmicrohttpd
  3. (C) 2007 Christian Grothoff
  4. libmicrohttpd is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 2, or (at your
  7. option) any later version.
  8. libmicrohttpd is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with libmicrohttpd; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file tls_thread_mode_test.c
  19. * @brief Testcase for libmicrohttpd HTTPS GET operations
  20. * @author Sagie Amir
  21. * @author Christian Grothoff
  22. */
  23. #include "platform.h"
  24. #include "microhttpd.h"
  25. #include <sys/stat.h>
  26. #include <limits.h>
  27. #include <curl/curl.h>
  28. #include <gcrypt.h>
  29. #include "tls_test_common.h"
  30. extern const char srv_key_pem[];
  31. extern const char srv_self_signed_cert_pem[];
  32. int curl_check_version (const char *req_version, ...);
  33. /**
  34. * used when spawning multiple threads executing curl server requests
  35. *
  36. */
  37. static void *
  38. https_transfer_thread_adapter (void *args)
  39. {
  40. static int nonnull;
  41. struct https_test_data *cargs = args;
  42. int ret;
  43. /* time spread incomming requests */
  44. usleep ((useconds_t) 10.0 * ((double) rand ()) / ((double) RAND_MAX));
  45. ret = test_https_transfer (NULL,
  46. cargs->cipher_suite, cargs->proto_version);
  47. if (ret == 0)
  48. return NULL;
  49. return &nonnull;
  50. }
  51. /**
  52. * Test non-parallel requests.
  53. *
  54. * @return: 0 upon all client requests returning '0', -1 otherwise.
  55. *
  56. * TODO : make client_count a parameter - number of curl client threads to spawn
  57. */
  58. static int
  59. test_single_client (void *cls, const char *cipher_suite,
  60. int curl_proto_version)
  61. {
  62. void *client_thread_ret;
  63. struct https_test_data client_args =
  64. { NULL, cipher_suite, curl_proto_version };
  65. client_thread_ret = https_transfer_thread_adapter (&client_args);
  66. if (client_thread_ret != NULL)
  67. return -1;
  68. return 0;
  69. }
  70. /**
  71. * Test parallel request handling.
  72. *
  73. * @return: 0 upon all client requests returning '0', -1 otherwise.
  74. *
  75. * TODO : make client_count a parameter - numver of curl client threads to spawn
  76. */
  77. static int
  78. test_parallel_clients (void * cls, const char *cipher_suite,
  79. int curl_proto_version)
  80. {
  81. int i;
  82. int client_count = 3;
  83. void *client_thread_ret;
  84. pthread_t client_arr[client_count];
  85. struct https_test_data client_args =
  86. { NULL, cipher_suite, curl_proto_version };
  87. for (i = 0; i < client_count; ++i)
  88. {
  89. if (pthread_create (&client_arr[i], NULL,
  90. &https_transfer_thread_adapter, &client_args) != 0)
  91. {
  92. fprintf (stderr, "Error: failed to spawn test client threads.\n");
  93. return -1;
  94. }
  95. }
  96. /* check all client requests fulfilled correctly */
  97. for (i = 0; i < client_count; ++i)
  98. {
  99. if ((pthread_join (client_arr[i], &client_thread_ret) != 0) ||
  100. (client_thread_ret != NULL))
  101. return -1;
  102. }
  103. return 0;
  104. }
  105. GCRY_THREAD_OPTION_PTHREAD_IMPL;
  106. int
  107. main (int argc, char *const *argv)
  108. {
  109. unsigned int errorCount = 0;
  110. /* initialize random seed used by curl clients */
  111. unsigned int iseed = (unsigned int) time (NULL);
  112. srand (iseed);
  113. gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
  114. if (0 != curl_global_init (CURL_GLOBAL_ALL))
  115. {
  116. fprintf (stderr, "Error: %s\n", strerror (errno));
  117. return -1;
  118. }
  119. char *aes256_sha = "AES256-SHA";
  120. if (curl_uses_nss_ssl() == 0)
  121. {
  122. aes256_sha = "rsa_aes_256_sha";
  123. }
  124. errorCount +=
  125. test_wrap ("single threaded daemon, single client", &test_single_client,
  126. NULL,
  127. MHD_USE_SELECT_INTERNALLY | MHD_USE_SSL | MHD_USE_DEBUG,
  128. aes256_sha, CURL_SSLVERSION_TLSv1, MHD_OPTION_HTTPS_MEM_KEY,
  129. srv_key_pem, MHD_OPTION_HTTPS_MEM_CERT,
  130. srv_self_signed_cert_pem, MHD_OPTION_END);
  131. errorCount +=
  132. test_wrap ("single threaded daemon, parallel clients",
  133. &test_parallel_clients, NULL,
  134. MHD_USE_SELECT_INTERNALLY | MHD_USE_SSL | MHD_USE_DEBUG,
  135. aes256_sha, CURL_SSLVERSION_TLSv1, MHD_OPTION_HTTPS_MEM_KEY,
  136. srv_key_pem, MHD_OPTION_HTTPS_MEM_CERT,
  137. srv_self_signed_cert_pem, MHD_OPTION_END);
  138. curl_global_cleanup ();
  139. return errorCount != 0;
  140. }