diff options
Diffstat (limited to 'src/testcurl/https/test_https_get_select.c')
-rw-r--r-- | src/testcurl/https/test_https_get_select.c | 229 |
1 files changed, 229 insertions, 0 deletions
diff --git a/src/testcurl/https/test_https_get_select.c b/src/testcurl/https/test_https_get_select.c new file mode 100644 index 00000000..20f8eca4 --- /dev/null +++ b/src/testcurl/https/test_https_get_select.c | |||
@@ -0,0 +1,229 @@ | |||
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 Testcase for libmicrohttpd HTTPS GET operations | ||
24 | * @author Sagie Amir | ||
25 | */ | ||
26 | |||
27 | #include "platform.h" | ||
28 | #include "microhttpd.h" | ||
29 | #include <limits.h> | ||
30 | #include <sys/stat.h> | ||
31 | #include <curl/curl.h> | ||
32 | #include <gcrypt.h> | ||
33 | #include "tls_test_common.h" | ||
34 | |||
35 | extern const char srv_key_pem[]; | ||
36 | extern const char srv_self_signed_cert_pem[]; | ||
37 | extern const char srv_signed_cert_pem[]; | ||
38 | extern const char srv_signed_key_pem[]; | ||
39 | |||
40 | static int oneone; | ||
41 | |||
42 | static int | ||
43 | ahc_echo (void *cls, | ||
44 | struct MHD_Connection *connection, | ||
45 | const char *url, | ||
46 | const char *method, | ||
47 | const char *version, | ||
48 | const char *upload_data, size_t *upload_data_size, | ||
49 | void **unused) | ||
50 | { | ||
51 | static int ptr; | ||
52 | const char *me = cls; | ||
53 | struct MHD_Response *response; | ||
54 | int ret; | ||
55 | |||
56 | if (0 != strcmp (me, method)) | ||
57 | return MHD_NO; /* unexpected method */ | ||
58 | if (&ptr != *unused) | ||
59 | { | ||
60 | *unused = &ptr; | ||
61 | return MHD_YES; | ||
62 | } | ||
63 | *unused = NULL; | ||
64 | response = MHD_create_response_from_buffer (strlen (url), | ||
65 | (void *) url, | ||
66 | MHD_RESPMEM_MUST_COPY); | ||
67 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | ||
68 | MHD_destroy_response (response); | ||
69 | if (ret == MHD_NO) | ||
70 | abort (); | ||
71 | return ret; | ||
72 | } | ||
73 | |||
74 | static int | ||
75 | testExternalGet () | ||
76 | { | ||
77 | struct MHD_Daemon *d; | ||
78 | CURL *c; | ||
79 | char buf[2048]; | ||
80 | struct CBC cbc; | ||
81 | CURLM *multi; | ||
82 | CURLMcode mret; | ||
83 | fd_set rs; | ||
84 | fd_set ws; | ||
85 | fd_set es; | ||
86 | int max; | ||
87 | int running; | ||
88 | struct CURLMsg *msg; | ||
89 | time_t start; | ||
90 | struct timeval tv; | ||
91 | |||
92 | multi = NULL; | ||
93 | cbc.buf = buf; | ||
94 | cbc.size = 2048; | ||
95 | cbc.pos = 0; | ||
96 | d = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL, | ||
97 | 1082, NULL, NULL, &ahc_echo, "GET", | ||
98 | MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem, | ||
99 | MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem, | ||
100 | MHD_OPTION_END); | ||
101 | if (d == NULL) | ||
102 | return 256; | ||
103 | |||
104 | char *aes256_sha = "AES256-SHA"; | ||
105 | if (curl_uses_nss_ssl() == 0) | ||
106 | { | ||
107 | aes256_sha = "rsa_aes_256_sha"; | ||
108 | } | ||
109 | |||
110 | c = curl_easy_init (); | ||
111 | curl_easy_setopt (c, CURLOPT_URL, "https://127.0.0.1:1082/hello_world"); | ||
112 | curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); | ||
113 | curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); | ||
114 | /* TLS options */ | ||
115 | curl_easy_setopt (c, CURLOPT_SSLVERSION, CURL_SSLVERSION_SSLv3); | ||
116 | curl_easy_setopt (c, CURLOPT_SSL_CIPHER_LIST, aes256_sha); | ||
117 | curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0); | ||
118 | curl_easy_setopt (c, CURLOPT_SSL_VERIFYHOST, 0); | ||
119 | curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); | ||
120 | if (oneone) | ||
121 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | ||
122 | else | ||
123 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); | ||
124 | curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); | ||
125 | curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L); | ||
126 | /* NOTE: use of CONNECTTIMEOUT without also | ||
127 | setting NOSIGNAL results in really weird | ||
128 | crashes on my system! */ | ||
129 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); | ||
130 | |||
131 | |||
132 | multi = curl_multi_init (); | ||
133 | if (multi == NULL) | ||
134 | { | ||
135 | curl_easy_cleanup (c); | ||
136 | MHD_stop_daemon (d); | ||
137 | return 512; | ||
138 | } | ||
139 | mret = curl_multi_add_handle (multi, c); | ||
140 | if (mret != CURLM_OK) | ||
141 | { | ||
142 | curl_multi_cleanup (multi); | ||
143 | curl_easy_cleanup (c); | ||
144 | MHD_stop_daemon (d); | ||
145 | return 1024; | ||
146 | } | ||
147 | start = time (NULL); | ||
148 | while ((time (NULL) - start < 5) && (multi != NULL)) | ||
149 | { | ||
150 | max = 0; | ||
151 | FD_ZERO (&rs); | ||
152 | FD_ZERO (&ws); | ||
153 | FD_ZERO (&es); | ||
154 | mret = curl_multi_fdset (multi, &rs, &ws, &es, &max); | ||
155 | if (mret != CURLM_OK) | ||
156 | { | ||
157 | curl_multi_remove_handle (multi, c); | ||
158 | curl_multi_cleanup (multi); | ||
159 | curl_easy_cleanup (c); | ||
160 | MHD_stop_daemon (d); | ||
161 | return 2048; | ||
162 | } | ||
163 | if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max)) | ||
164 | { | ||
165 | curl_multi_remove_handle (multi, c); | ||
166 | curl_multi_cleanup (multi); | ||
167 | curl_easy_cleanup (c); | ||
168 | MHD_stop_daemon (d); | ||
169 | return 4096; | ||
170 | } | ||
171 | tv.tv_sec = 0; | ||
172 | tv.tv_usec = 1000; | ||
173 | select (max + 1, &rs, &ws, &es, &tv); | ||
174 | curl_multi_perform (multi, &running); | ||
175 | if (running == 0) | ||
176 | { | ||
177 | msg = curl_multi_info_read (multi, &running); | ||
178 | if (msg == NULL) | ||
179 | break; | ||
180 | if (msg->msg == CURLMSG_DONE) | ||
181 | { | ||
182 | if (msg->data.result != CURLE_OK) | ||
183 | printf ("%s failed at %s:%d: `%s'\n", | ||
184 | "curl_multi_perform", | ||
185 | __FILE__, | ||
186 | __LINE__, curl_easy_strerror (msg->data.result)); | ||
187 | curl_multi_remove_handle (multi, c); | ||
188 | curl_multi_cleanup (multi); | ||
189 | curl_easy_cleanup (c); | ||
190 | c = NULL; | ||
191 | multi = NULL; | ||
192 | } | ||
193 | } | ||
194 | MHD_run (d); | ||
195 | } | ||
196 | if (multi != NULL) | ||
197 | { | ||
198 | curl_multi_remove_handle (multi, c); | ||
199 | curl_easy_cleanup (c); | ||
200 | curl_multi_cleanup (multi); | ||
201 | } | ||
202 | MHD_stop_daemon (d); | ||
203 | if (cbc.pos != strlen ("/hello_world")) | ||
204 | return 8192; | ||
205 | if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) | ||
206 | return 16384; | ||
207 | return 0; | ||
208 | } | ||
209 | |||
210 | GCRY_THREAD_OPTION_PTHREAD_IMPL; | ||
211 | |||
212 | int | ||
213 | main (int argc, char *const *argv) | ||
214 | { | ||
215 | unsigned int errorCount = 0; | ||
216 | |||
217 | gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); | ||
218 | if (!gcry_check_version (GCRYPT_VERSION)) | ||
219 | abort (); | ||
220 | if (0 != curl_global_init (CURL_GLOBAL_ALL)) | ||
221 | { | ||
222 | fprintf (stderr, "Error: %s\n", strerror (errno)); | ||
223 | return -1; | ||
224 | } | ||
225 | if (0 != (errorCount = testExternalGet ())) | ||
226 | fprintf (stderr, "Fail: %d\n", errorCount); | ||
227 | curl_global_cleanup (); | ||
228 | return errorCount != 0; | ||
229 | } | ||