diff options
Diffstat (limited to 'src/testcurl/https/test_https_time_out.c')
-rw-r--r-- | src/testcurl/https/test_https_time_out.c | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/src/testcurl/https/test_https_time_out.c b/src/testcurl/https/test_https_time_out.c new file mode 100644 index 00000000..cafe5545 --- /dev/null +++ b/src/testcurl/https/test_https_time_out.c | |||
@@ -0,0 +1,130 @@ | |||
1 | /* | ||
2 | This file is part of libmicrohttpd | ||
3 | (C) 2007 Christian Grothoff | ||
4 | |||
5 | libmicrohttpd is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | libmicrohttpd is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with libmicrohttpd; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file mhds_get_test.c | ||
23 | * @brief: daemon TLS alert response test-case | ||
24 | * | ||
25 | * @author Sagie Amir | ||
26 | */ | ||
27 | |||
28 | #include "platform.h" | ||
29 | #include "microhttpd.h" | ||
30 | #include "internal.h" | ||
31 | #include "tls_test_common.h" | ||
32 | |||
33 | extern const char srv_key_pem[]; | ||
34 | extern const char srv_self_signed_cert_pem[]; | ||
35 | |||
36 | static const int TIME_OUT = 3; | ||
37 | |||
38 | char *http_get_req = "GET / HTTP/1.1\r\n\r\n"; | ||
39 | |||
40 | static int | ||
41 | test_tls_session_time_out (gnutls_session_t session) | ||
42 | { | ||
43 | int sd, ret; | ||
44 | struct sockaddr_in sa; | ||
45 | |||
46 | sd = socket (AF_INET, SOCK_STREAM, 0); | ||
47 | if (sd == -1) | ||
48 | { | ||
49 | fprintf (stderr, "Failed to create socket: %s\n", strerror (errno)); | ||
50 | return -1; | ||
51 | } | ||
52 | |||
53 | memset (&sa, '\0', sizeof (struct sockaddr_in)); | ||
54 | sa.sin_family = AF_INET; | ||
55 | sa.sin_port = htons (DEAMON_TEST_PORT); | ||
56 | inet_pton (AF_INET, "127.0.0.1", &sa.sin_addr); | ||
57 | |||
58 | gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) (long) sd); | ||
59 | |||
60 | ret = connect (sd, &sa, sizeof (struct sockaddr_in)); | ||
61 | |||
62 | if (ret < 0) | ||
63 | { | ||
64 | fprintf (stderr, "Error: %s\n", MHD_E_FAILED_TO_CONNECT); | ||
65 | return -1; | ||
66 | } | ||
67 | |||
68 | ret = gnutls_handshake (session); | ||
69 | if (ret < 0) | ||
70 | { | ||
71 | fprintf (stderr, "Handshake failed\n"); | ||
72 | return -1; | ||
73 | } | ||
74 | |||
75 | sleep (TIME_OUT + 1); | ||
76 | |||
77 | /* check that server has closed the connection */ | ||
78 | /* TODO better RST trigger */ | ||
79 | if (send (sd, "", 1, 0) == 0) | ||
80 | { | ||
81 | fprintf (stderr, "Connection failed to time-out\n"); | ||
82 | return -1; | ||
83 | } | ||
84 | |||
85 | close (sd); | ||
86 | return 0; | ||
87 | } | ||
88 | |||
89 | int | ||
90 | main (int argc, char *const *argv) | ||
91 | { | ||
92 | int errorCount = 0;; | ||
93 | struct MHD_Daemon *d; | ||
94 | gnutls_session_t session; | ||
95 | gnutls_datum_t key; | ||
96 | gnutls_datum_t cert; | ||
97 | gnutls_certificate_credentials_t xcred; | ||
98 | |||
99 | gnutls_global_init (); | ||
100 | gnutls_global_set_log_level (11); | ||
101 | |||
102 | d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_SSL | | ||
103 | MHD_USE_DEBUG, DEAMON_TEST_PORT, | ||
104 | NULL, NULL, &http_dummy_ahc, NULL, | ||
105 | MHD_OPTION_CONNECTION_TIMEOUT, TIME_OUT, | ||
106 | MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem, | ||
107 | MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem, | ||
108 | MHD_OPTION_END); | ||
109 | |||
110 | if (d == NULL) | ||
111 | { | ||
112 | fprintf (stderr, MHD_E_SERVER_INIT); | ||
113 | return -1; | ||
114 | } | ||
115 | |||
116 | if (0 != setup_session (&session, &key, &cert, &xcred)) | ||
117 | { | ||
118 | fprintf (stderr, "failed to setup session\n"); | ||
119 | return 1; | ||
120 | } | ||
121 | errorCount += test_tls_session_time_out (session); | ||
122 | teardown_session (session, &key, &cert, xcred); | ||
123 | |||
124 | print_test_result (errorCount, argv[0]); | ||
125 | |||
126 | MHD_stop_daemon (d); | ||
127 | gnutls_global_deinit (); | ||
128 | |||
129 | return errorCount != 0; | ||
130 | } | ||