aboutsummaryrefslogtreecommitdiff
path: root/src/testcurl/https/mhds_multi_daemon_test.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testcurl/https/mhds_multi_daemon_test.c')
-rw-r--r--src/testcurl/https/mhds_multi_daemon_test.c178
1 files changed, 24 insertions, 154 deletions
diff --git a/src/testcurl/https/mhds_multi_daemon_test.c b/src/testcurl/https/mhds_multi_daemon_test.c
index d663902c..910989ab 100644
--- a/src/testcurl/https/mhds_multi_daemon_test.c
+++ b/src/testcurl/https/mhds_multi_daemon_test.c
@@ -30,93 +30,14 @@
30#include <limits.h> 30#include <limits.h>
31#include <sys/stat.h> 31#include <sys/stat.h>
32 32
33#define DEBUG_CURL_VERBOSE 0 33#include "tls_test_common.h"
34
35#define PAGE_NOT_FOUND "<html><head><title>File not found</title></head><body>File not found</body></html>"
36
37#define MHD_E_MEM "Error: memory error\n"
38#define MHD_E_SERVER_INIT "Error: failed to start server\n"
39#define MHD_E_TEST_FILE_CREAT "Error: failed to setup test file\n"
40
41#include "tls_test_keys.h"
42
43const char *test_file_name = "https_test_file";
44const char test_file_data[] = "Hello World\n";
45 34
46extern int curl_check_version (const char *req_version, ...); 35extern int curl_check_version (const char *req_version, ...);
36extern const char srv_key_pem[];
37extern const char srv_self_signed_cert_pem[];
47 38
48struct CBC 39/* TODO mv to common */
49{ 40/**
50 char *buf;
51 size_t pos;
52 size_t size;
53};
54
55static size_t
56copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
57{
58 struct CBC *cbc = ctx;
59
60 if (cbc->pos + size * nmemb > cbc->size)
61 return 0; /* overflow */
62 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
63 cbc->pos += size * nmemb;
64 return size * nmemb;
65}
66
67static int
68file_reader (void *cls, size_t pos, char *buf, int max)
69{
70 FILE *file = cls;
71 fseek (file, pos, SEEK_SET);
72 return fread (buf, 1, max, file);
73}
74
75/* HTTP access handler call back */
76static int
77http_ahc (void *cls, struct MHD_Connection *connection,
78 const char *url, const char *method, const char *upload_data,
79 const char *version, unsigned int *upload_data_size, void **ptr)
80{
81 static int aptr;
82 struct MHD_Response *response;
83 int ret;
84 FILE *file;
85 struct stat buf;
86
87 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
88 return MHD_NO; /* unexpected method */
89 if (&aptr != *ptr)
90 {
91 /* do never respond on first call */
92 *ptr = &aptr;
93 return MHD_YES;
94 }
95 *ptr = NULL; /* reset when done */
96
97 file = fopen (url, "r");
98 if (file == NULL)
99 {
100 response = MHD_create_response_from_data (strlen (PAGE_NOT_FOUND),
101 (void *) PAGE_NOT_FOUND,
102 MHD_NO, MHD_NO);
103 ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response);
104 MHD_destroy_response (response);
105 }
106 else
107 {
108 stat (url, &buf);
109 response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k PAGE_NOT_FOUND size */
110 &file_reader, file,
111 (MHD_ContentReaderFreeCallback)
112 & fclose);
113 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
114 MHD_destroy_response (response);
115 }
116 return ret;
117}
118
119/*
120 * perform cURL request for file 41 * perform cURL request for file
121 * @param test_fd: file to attempt transferring 42 * @param test_fd: file to attempt transferring
122 */ 43 */
@@ -127,33 +48,16 @@ test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version,
127 CURL *c; 48 CURL *c;
128 struct CBC cbc; 49 struct CBC cbc;
129 CURLcode errornum; 50 CURLcode errornum;
130 char *doc_path;
131 size_t doc_path_len;
132 char url[255]; 51 char url[255];
133 size_t len; 52 size_t len;
134 struct stat file_stat; 53 struct stat file_stat;
135 54
136 stat (test_file_name, &file_stat); 55 stat (TEST_FILE_NAME, &file_stat);
137 len = file_stat.st_size; 56 len = file_stat.st_size;
138 57
139 /* used to memcmp local copy & deamon supplied copy */ 58 /* used to memcmp local copy & deamon supplied copy */
140 unsigned char *mem_test_file_local; 59 unsigned char *mem_test_file_local;
141 60
142 /* setup test file path, url */
143 doc_path_len = PATH_MAX > 4096 ? 4096 : PATH_MAX;
144 if (NULL == (doc_path = malloc (doc_path_len)))
145 {
146 fprintf (stderr, MHD_E_MEM);
147 return -1;
148 }
149 if (getcwd (doc_path, doc_path_len) == NULL)
150 {
151 free (doc_path);
152 fprintf (stderr, "Error: failed to get working directory. %s\n",
153 strerror (errno));
154 return -1;
155 }
156
157 mem_test_file_local = malloc (len); 61 mem_test_file_local = malloc (len);
158 fseek (test_fd, 0, SEEK_SET); 62 fseek (test_fd, 0, SEEK_SET);
159 if (fread (mem_test_file_local, sizeof (char), len, test_fd) != len) 63 if (fread (mem_test_file_local, sizeof (char), len, test_fd) != len)
@@ -161,7 +65,6 @@ test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version,
161 fprintf (stderr, "Error: failed to read test file. %s\n", 65 fprintf (stderr, "Error: failed to read test file. %s\n",
162 strerror (errno)); 66 strerror (errno));
163 free (mem_test_file_local); 67 free (mem_test_file_local);
164 free (doc_path);
165 return -1; 68 return -1;
166 } 69 }
167 70
@@ -169,19 +72,18 @@ test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version,
169 { 72 {
170 fprintf (stderr, "Error: failed to read test file. %s\n", 73 fprintf (stderr, "Error: failed to read test file. %s\n",
171 strerror (errno)); 74 strerror (errno));
172 free (mem_test_file_local);
173 free (doc_path);
174 return -1; 75 return -1;
175 } 76 }
176 cbc.size = len; 77 cbc.size = len;
177 cbc.pos = 0; 78 cbc.pos = 0;
178 79
179 /* construct url */ 80 if (gen_test_file_url (url, port))
180 sprintf (url, "%s:%d%s/%s", "https://localhost", port, doc_path, 81 {
181 test_file_name); 82 return -1;
83 }
182 84
183 c = curl_easy_init (); 85 c = curl_easy_init ();
184#if DEBUG_CURL_VERBOSE 86#if DEBUG_HTTPS_TEST
185 curl_easy_setopt (c, CURLOPT_VERBOSE, 1); 87 curl_easy_setopt (c, CURLOPT_VERBOSE, 1);
186#endif 88#endif
187 curl_easy_setopt (c, CURLOPT_URL, url); 89 curl_easy_setopt (c, CURLOPT_URL, url);
@@ -210,9 +112,6 @@ test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version,
210 fprintf (stderr, "curl_easy_perform failed: `%s'\n", 112 fprintf (stderr, "curl_easy_perform failed: `%s'\n",
211 curl_easy_strerror (errornum)); 113 curl_easy_strerror (errornum));
212 curl_easy_cleanup (c); 114 curl_easy_cleanup (c);
213 free (mem_test_file_local);
214 free (doc_path);
215 free (cbc.buf);
216 return errornum; 115 return errornum;
217 } 116 }
218 117
@@ -224,13 +123,11 @@ test_daemon_get (FILE * test_fd, char *cipher_suite, int proto_version,
224 fprintf (stderr, "Error: local file & received file differ.\n"); 123 fprintf (stderr, "Error: local file & received file differ.\n");
225 free (mem_test_file_local); 124 free (mem_test_file_local);
226 free (cbc.buf); 125 free (cbc.buf);
227 free (doc_path);
228 return -1; 126 return -1;
229 } 127 }
230 128
231 free (mem_test_file_local); 129 free (mem_test_file_local);
232 free (cbc.buf); 130 free (cbc.buf);
233 free (doc_path);
234 return 0; 131 return 0;
235} 132}
236 133
@@ -247,7 +144,7 @@ test_concurent_daemon_pair (FILE * test_fd, char *cipher_suite,
247 struct MHD_Daemon *d1; 144 struct MHD_Daemon *d1;
248 struct MHD_Daemon *d2; 145 struct MHD_Daemon *d2;
249 d1 = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_SSL | 146 d1 = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_SSL |
250 MHD_USE_DEBUG, 42433, 147 MHD_USE_DEBUG, DEAMON_TEST_PORT,
251 NULL, NULL, &http_ahc, NULL, 148 NULL, NULL, &http_ahc, NULL,
252 MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem, 149 MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem,
253 MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem, 150 MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem,
@@ -260,7 +157,7 @@ test_concurent_daemon_pair (FILE * test_fd, char *cipher_suite,
260 } 157 }
261 158
262 d2 = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_SSL | 159 d2 = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_SSL |
263 MHD_USE_DEBUG, 42434, 160 MHD_USE_DEBUG, DEAMON_TEST_PORT + 1,
264 NULL, NULL, &http_ahc, NULL, 161 NULL, NULL, &http_ahc, NULL,
265 MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem, 162 MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem,
266 MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem, 163 MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem,
@@ -268,50 +165,24 @@ test_concurent_daemon_pair (FILE * test_fd, char *cipher_suite,
268 165
269 if (d2 == NULL) 166 if (d2 == NULL)
270 { 167 {
271 MHD_stop_daemon(d1); 168 MHD_stop_daemon (d1);
272 fprintf (stderr, MHD_E_SERVER_INIT); 169 fprintf (stderr, MHD_E_SERVER_INIT);
273 return -1; 170 return -1;
274 } 171 }
275 172
276 ret = test_daemon_get (test_fd, cipher_suite, proto_version, 42433); 173 ret =
277 ret += test_daemon_get (test_fd, cipher_suite, proto_version, 42434); 174 test_daemon_get (test_fd, cipher_suite, proto_version, DEAMON_TEST_PORT);
175 ret +=
176 test_daemon_get (test_fd, cipher_suite, proto_version,
177 DEAMON_TEST_PORT + 1);
278 178
279 MHD_stop_daemon (d2); 179 MHD_stop_daemon (d2);
280 ret += test_daemon_get (test_fd, cipher_suite, proto_version, 42433); 180 ret +=
181 test_daemon_get (test_fd, cipher_suite, proto_version, DEAMON_TEST_PORT);
281 MHD_stop_daemon (d1); 182 MHD_stop_daemon (d1);
282 return ret; 183 return ret;
283} 184}
284 185
285FILE *
286setupTestFile ()
287{
288 FILE *test_fd;
289
290 if (NULL == (test_fd = fopen (test_file_name, "w+")))
291 {
292 fprintf (stderr, "Error: failed to open `%s': %s\n",
293 test_file_name, strerror (errno));
294 return NULL;
295 }
296 if (fwrite (test_file_data, sizeof (char), strlen (test_file_data), test_fd)
297 != strlen (test_file_data))
298 {
299 fprintf (stderr, "Error: failed to write `%s. %s'\n",
300 test_file_name, strerror (errno));
301 fclose (test_fd);
302 return NULL;
303 }
304 if (fflush (test_fd))
305 {
306 fprintf (stderr, "Error: failed to flush test file stream. %s\n",
307 strerror (errno));
308 fclose (test_fd);
309 return NULL;
310 }
311
312 return test_fd;
313}
314
315int 186int
316main (int argc, char *const *argv) 187main (int argc, char *const *argv)
317{ 188{
@@ -323,7 +194,7 @@ main (int argc, char *const *argv)
323 return -1; 194 return -1;
324 } 195 }
325 196
326 if ((test_fd = setupTestFile ()) == NULL) 197 if ((test_fd = setup_test_file ()) == NULL)
327 { 198 {
328 fprintf (stderr, MHD_E_TEST_FILE_CREAT); 199 fprintf (stderr, MHD_E_TEST_FILE_CREAT);
329 return -1; 200 return -1;
@@ -340,12 +211,11 @@ main (int argc, char *const *argv)
340 errorCount += 211 errorCount +=
341 test_concurent_daemon_pair (test_fd, "AES256-SHA", CURL_SSLVERSION_SSLv3); 212 test_concurent_daemon_pair (test_fd, "AES256-SHA", CURL_SSLVERSION_SSLv3);
342 213
343 if (errorCount != 0) 214 print_test_result (errorCount, "concurent_daemon_pair");
344 fprintf (stderr, "Failed test: %s.\n", __FILE__);
345 215
346 curl_global_cleanup (); 216 curl_global_cleanup ();
347 fclose (test_fd); 217 fclose (test_fd);
348 218
349 remove (test_file_name); 219 remove (TEST_FILE_NAME);
350 return errorCount != 0; 220 return errorCount != 0;
351} 221}