test_https_get_parallel.c 5.5 KB

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