test_https_get_parallel_threads.c 5.2 KB

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