aboutsummaryrefslogtreecommitdiff
path: root/src/testcurl/https/mhds_get_test.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testcurl/https/mhds_get_test.c')
-rw-r--r--src/testcurl/https/mhds_get_test.c361
1 files changed, 361 insertions, 0 deletions
diff --git a/src/testcurl/https/mhds_get_test.c b/src/testcurl/https/mhds_get_test.c
new file mode 100644
index 00000000..302c6e9a
--- /dev/null
+++ b/src/testcurl/https/mhds_get_test.c
@@ -0,0 +1,361 @@
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
30#include <limits.h>
31#include <sys/stat.h>
32
33#include "gnutls.h"
34#include <curl/curl.h>
35
36#define DEBUG 0
37
38#define PAGE_NOT_FOUND "<html><head><title>File not found</title></head><body>File not found</body></html>"
39
40#define MHD_E_MEM "Error: memory error\n"
41#define MHD_E_SERVER_INIT "Error: failed to start server\n"
42#define MHD_E_TEST_FILE_CREAT "Error: failed to setup test file\n"
43#define MHD_E_CERT_FILE_CREAT "Error: failed to setup test certificate\n"
44#define MHD_E_KEY_FILE_CREAT "Error: failed to setup test certificate\n"
45
46#include "tls_test_keys.h"
47
48const char *test_file_name = "https_test_file";
49const char test_file_data[] = "Hello World\n";
50
51int curl_check_version (const char *req_version, ...);
52
53struct CBC
54{
55 char *buf;
56 size_t pos;
57 size_t size;
58};
59
60static size_t
61copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
62{
63 struct CBC *cbc = ctx;
64
65 if (cbc->pos + size * nmemb > cbc->size)
66 return 0; /* overflow */
67 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
68 cbc->pos += size * nmemb;
69 return size * nmemb;
70}
71
72static int
73file_reader (void *cls, size_t pos, char *buf, int max)
74{
75 FILE *file = cls;
76 fseek (file, pos, SEEK_SET);
77 return fread (buf, 1, max, file);
78}
79
80/* HTTP access handler call back */
81static int
82http_ahc (void *cls, struct MHD_Connection *connection,
83 const char *url, const char *method, const char *upload_data,
84 const char *version, unsigned int *upload_data_size, void **ptr)
85{
86 static int aptr;
87 struct MHD_Response *response;
88 int ret;
89 FILE *file;
90 struct stat buf;
91
92 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
93 return MHD_NO; /* unexpected method */
94 if (&aptr != *ptr)
95 {
96 /* do never respond on first call */
97 *ptr = &aptr;
98 return MHD_YES;
99 }
100 *ptr = NULL; /* reset when done */
101
102 file = fopen (url, "r");
103 if (file == NULL)
104 {
105 response = MHD_create_response_from_data (strlen (PAGE_NOT_FOUND),
106 (void *) PAGE_NOT_FOUND,
107 MHD_NO, MHD_NO);
108 ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response);
109 MHD_destroy_response (response);
110 }
111 else
112 {
113 stat (url, &buf);
114 response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k PAGE_NOT_FOUND size */
115 &file_reader, file,
116 (MHD_ContentReaderFreeCallback)
117 & fclose);
118 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
119 MHD_destroy_response (response);
120 }
121 return ret;
122}
123
124/*
125 * test HTTPS transfer
126 * @param test_fd: file to attempt transfering
127 */
128static int
129test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version)
130{
131 CURL *c;
132 struct CBC cbc;
133 CURLcode errornum;
134 char *doc_path;
135 size_t doc_path_len;
136 char url[255];
137 struct stat statb;
138
139 stat (test_file_name, &statb);
140
141 int len = statb.st_size;
142
143 /* used to memcmp local copy & deamon supplied copy */
144 unsigned char *mem_test_file_local;
145
146 /* setup test file path, url */
147 doc_path_len = PATH_MAX > 4096 ? 4096 : PATH_MAX;
148 if (NULL == (doc_path = malloc (doc_path_len)))
149 {
150 fclose (test_fd);
151 fprintf (stderr, MHD_E_MEM);
152 return -1;
153 }
154 if (getcwd (doc_path, doc_path_len) == NULL)
155 {
156 fclose (test_fd);
157 free (doc_path);
158 fprintf (stderr, "Error: failed to get working directory. %s\n",
159 strerror (errno));
160 return -1;
161 }
162
163 if (NULL == (mem_test_file_local = malloc (len)))
164 {
165 fclose (test_fd);
166 fprintf (stderr, MHD_E_MEM);
167 return -1;
168 }
169
170 fseek (test_fd, 0, SEEK_SET);
171 if (fread (mem_test_file_local, sizeof (char), len, test_fd) != len)
172 {
173 fclose (test_fd);
174 fprintf (stderr, "Error: failed to read test file. %s\n",
175 strerror (errno));
176 return -1;
177 }
178
179 if (NULL == (cbc.buf = malloc (sizeof (char) * len)))
180 {
181 fclose (test_fd);
182 fprintf (stderr, MHD_E_MEM);
183 return -1;
184 }
185 cbc.size = len;
186 cbc.pos = 0;
187
188 /* construct url - this might use doc_path */
189 sprintf (url, "%s%s/%s", "https://localhost:42433",
190 doc_path, test_file_name);
191
192 c = curl_easy_init ();
193#if DEBUG
194 curl_easy_setopt (c, CURLOPT_VERBOSE, 1);
195#endif
196 curl_easy_setopt (c, CURLOPT_URL, url);
197 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
198 curl_easy_setopt (c, CURLOPT_TIMEOUT, 2L);
199 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 2L);
200 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
201 curl_easy_setopt (c, CURLOPT_FILE, &cbc);
202
203 /* TLS options */
204 curl_easy_setopt (c, CURLOPT_SSLVERSION, proto_version);
205 curl_easy_setopt (c, CURLOPT_SSL_CIPHER_LIST, cipher_suite);
206
207 /* currently skip any peer authentication */
208 curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0);
209 curl_easy_setopt (c, CURLOPT_SSL_VERIFYHOST, 0);
210
211 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
212
213 /* NOTE: use of CONNECTTIMEOUT without also
214 setting NOSIGNAL results in really weird
215 crashes on my system! */
216 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
217 if (CURLE_OK != (errornum = curl_easy_perform (c)))
218 {
219 fprintf (stderr, "curl_easy_perform failed: `%s'\n",
220 curl_easy_strerror (errornum));
221 curl_easy_cleanup (c);
222 return errornum;
223 }
224
225 curl_easy_cleanup (c);
226
227 if (memcmp (cbc.buf, mem_test_file_local, len) != 0)
228 {
229 fprintf (stderr, "Error: local file & received file differ.\n");
230 free (cbc.buf);
231 free (mem_test_file_local);
232 return -1;
233 }
234
235 free (mem_test_file_local);
236 free (cbc.buf);
237 free (doc_path);
238 return 0;
239}
240
241static int
242test_cipher_option (FILE * test_fd, char *cipher_suite, int proto_version)
243{
244
245 int ret;
246 int ciper[] = { MHD_GNUTLS_CIPHER_3DES_CBC, 0 };
247 struct MHD_Daemon *d;
248 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_SSL |
249 MHD_USE_DEBUG, 42433,
250 NULL, NULL, &http_ahc, NULL,
251 MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem,
252 MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem,
253 MHD_OPTION_CIPHER_ALGORITHM, ciper, MHD_OPTION_END);
254
255 if (d == NULL)
256 {
257 fprintf (stderr, MHD_E_SERVER_INIT);
258 return -1;
259 }
260
261 ret = test_daemon_get (test_fd, cipher_suite, proto_version);
262
263 MHD_stop_daemon (d);
264 return ret;
265}
266
267/* setup a temporary transfer test file */
268static FILE *
269setupTestFile ()
270{
271 FILE *test_fd;
272
273 if (NULL == (test_fd = fopen (test_file_name, "w+")))
274 {
275 fprintf (stderr, "Error: failed to open `%s': %s\n",
276 test_file_name, strerror (errno));
277 return NULL;
278 }
279 if (fwrite (test_file_data, sizeof (char), strlen (test_file_data), test_fd)
280 != strlen (test_file_data))
281 {
282 fprintf (stderr, "Error: failed to write `%s. %s'\n",
283 test_file_name, strerror (errno));
284 return NULL;
285 }
286 if (fflush (test_fd))
287 {
288 fprintf (stderr, "Error: failed to flush test file stream. %s\n",
289 strerror (errno));
290 return NULL;
291 }
292
293 return test_fd;
294}
295
296/* perform a HTTP GET request via SSL/TLS */
297int
298test_secure_get (FILE * test_fd, char *cipher_suite, int proto_version)
299{
300 int ret;
301 struct MHD_Daemon *d;
302
303 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_SSL |
304 MHD_USE_DEBUG, 42433,
305 NULL, NULL, &http_ahc, NULL,
306 MHD_OPTION_HTTPS_MEM_KEY, srv_signed_key_pem,
307 MHD_OPTION_HTTPS_MEM_CERT, srv_signed_cert_pem,
308 MHD_OPTION_END);
309
310 if (d == NULL)
311 {
312 fprintf (stderr, MHD_E_SERVER_INIT);
313 return -1;
314 }
315
316 ret = test_daemon_get (test_fd, cipher_suite, proto_version);
317
318 MHD_stop_daemon (d);
319 return ret;
320}
321
322int
323main (int argc, char *const *argv)
324{
325 FILE *test_fd;
326 unsigned int errorCount = 0;
327
328 /* gnutls_global_set_log_level(11); */
329
330 if (curl_check_version (MHD_REQ_CURL_VERSION, MHD_REQ_CURL_OPENSSL_VERSION))
331 {
332 return -1;
333 }
334
335 if ((test_fd = setupTestFile ()) == NULL)
336 {
337 fprintf (stderr, MHD_E_TEST_FILE_CREAT);
338 return -1;
339 }
340
341 if (0 != curl_global_init (CURL_GLOBAL_ALL))
342 {
343 fprintf (stderr, "Error: %s\n", strerror (errno));
344 return -1;
345 }
346
347 errorCount +=
348 test_secure_get (test_fd, "AES256-SHA", CURL_SSLVERSION_TLSv1);
349 errorCount +=
350 test_secure_get (test_fd, "AES256-SHA", CURL_SSLVERSION_SSLv3);
351 errorCount +=
352 test_cipher_option (test_fd, "DES-CBC3-SHA", CURL_SSLVERSION_TLSv1);
353
354
355 curl_global_cleanup ();
356 fclose (test_fd);
357
358 remove (test_file_name);
359
360 return errorCount != 0;
361}