test_https_time_out.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. /**
  18. * @file test_https_time_out.c
  19. * @brief: daemon TLS alert response test-case
  20. *
  21. * @author Sagie Amir
  22. */
  23. #include "platform.h"
  24. #include "microhttpd.h"
  25. #include "internal.h"
  26. #include "tls_test_common.h"
  27. #include <gcrypt.h>
  28. #include "mhd_sockets.h" /* only macros used */
  29. #ifdef _WIN32
  30. #ifndef WIN32_LEAN_AND_MEAN
  31. #define WIN32_LEAN_AND_MEAN 1
  32. #endif /* !WIN32_LEAN_AND_MEAN */
  33. #include <windows.h>
  34. #endif
  35. extern const char srv_key_pem[];
  36. extern const char srv_self_signed_cert_pem[];
  37. static const int TIME_OUT = 3;
  38. static int
  39. test_tls_session_time_out (gnutls_session_t session)
  40. {
  41. int ret;
  42. MHD_socket sd;
  43. struct sockaddr_in sa;
  44. sd = socket (AF_INET, SOCK_STREAM, 0);
  45. if (sd == -1)
  46. {
  47. fprintf (stderr, "Failed to create socket: %s\n", strerror (errno));
  48. return -1;
  49. }
  50. memset (&sa, '\0', sizeof (struct sockaddr_in));
  51. sa.sin_family = AF_INET;
  52. sa.sin_port = htons (DEAMON_TEST_PORT);
  53. sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
  54. gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) (intptr_t) sd);
  55. ret = connect (sd, (struct sockaddr *) &sa, sizeof (struct sockaddr_in));
  56. if (ret < 0)
  57. {
  58. fprintf (stderr, "Error: %s\n", MHD_E_FAILED_TO_CONNECT);
  59. MHD_socket_close_ (sd);
  60. return -1;
  61. }
  62. ret = gnutls_handshake (session);
  63. if (ret < 0)
  64. {
  65. fprintf (stderr, "Handshake failed\n");
  66. MHD_socket_close_ (sd);
  67. return -1;
  68. }
  69. sleep (TIME_OUT + 1);
  70. /* check that server has closed the connection */
  71. /* TODO better RST trigger */
  72. if (send (sd, "", 1, 0) == 0)
  73. {
  74. fprintf (stderr, "Connection failed to time-out\n");
  75. MHD_socket_close_ (sd);
  76. return -1;
  77. }
  78. MHD_socket_close_ (sd);
  79. return 0;
  80. }
  81. int
  82. main (int argc, char *const *argv)
  83. {
  84. int errorCount = 0;;
  85. struct MHD_Daemon *d;
  86. gnutls_session_t session;
  87. gnutls_datum_t key;
  88. gnutls_datum_t cert;
  89. gnutls_certificate_credentials_t xcred;
  90. gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
  91. #ifdef GCRYCTL_INITIALIZATION_FINISHED
  92. gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
  93. #endif
  94. gnutls_global_init ();
  95. gnutls_global_set_log_level (11);
  96. d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_SSL |
  97. MHD_USE_DEBUG, DEAMON_TEST_PORT,
  98. NULL, NULL, &http_dummy_ahc, NULL,
  99. MHD_OPTION_CONNECTION_TIMEOUT, TIME_OUT,
  100. MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem,
  101. MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem,
  102. MHD_OPTION_END);
  103. if (NULL == d)
  104. {
  105. fprintf (stderr, MHD_E_SERVER_INIT);
  106. return -1;
  107. }
  108. if (0 != setup_session (&session, &key, &cert, &xcred))
  109. {
  110. fprintf (stderr, "failed to setup session\n");
  111. return 1;
  112. }
  113. errorCount += test_tls_session_time_out (session);
  114. teardown_session (session, &key, &cert, xcred);
  115. print_test_result (errorCount, argv[0]);
  116. MHD_stop_daemon (d);
  117. gnutls_global_deinit ();
  118. return errorCount != 0;
  119. }