diff options
Diffstat (limited to 'src/testcurl/https/mhds_get_test.c')
-rw-r--r-- | src/testcurl/https/mhds_get_test.c | 252 |
1 files changed, 10 insertions, 242 deletions
diff --git a/src/testcurl/https/mhds_get_test.c b/src/testcurl/https/mhds_get_test.c index 7ba1cd5a..ce0fcbda 100644 --- a/src/testcurl/https/mhds_get_test.c +++ b/src/testcurl/https/mhds_get_test.c | |||
@@ -33,214 +33,13 @@ | |||
33 | #include "gnutls.h" | 33 | #include "gnutls.h" |
34 | #include <curl/curl.h> | 34 | #include <curl/curl.h> |
35 | 35 | ||
36 | #define DEBUG 0 | 36 | #include "tls_test_common.h" |
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 | |||
48 | const char *test_file_name = "https_test_file"; | ||
49 | const char test_file_data[] = "Hello World\n"; | ||
50 | 37 | ||
51 | int curl_check_version (const char *req_version, ...); | 38 | int curl_check_version (const char *req_version, ...); |
52 | 39 | extern const char srv_key_pem[]; | |
53 | struct CBC | 40 | extern const char srv_self_signed_cert_pem[]; |
54 | { | 41 | extern const char srv_signed_cert_pem[]; |
55 | char *buf; | 42 | extern const char srv_signed_key_pem[]; |
56 | size_t pos; | ||
57 | size_t size; | ||
58 | }; | ||
59 | |||
60 | static size_t | ||
61 | copyBuffer (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 | |||
72 | static int | ||
73 | file_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 */ | ||
81 | static int | ||
82 | http_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 | */ | ||
128 | static int | ||
129 | test_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 | fprintf (stderr, MHD_E_MEM); | ||
151 | return -1; | ||
152 | } | ||
153 | if (getcwd (doc_path, doc_path_len) == NULL) | ||
154 | { | ||
155 | fprintf (stderr, "Error: failed to get working directory. %s\n", | ||
156 | strerror (errno)); | ||
157 | free (doc_path); | ||
158 | return -1; | ||
159 | } | ||
160 | |||
161 | if (NULL == (mem_test_file_local = malloc (len))) | ||
162 | { | ||
163 | fprintf (stderr, MHD_E_MEM); | ||
164 | free (doc_path); | ||
165 | return -1; | ||
166 | } | ||
167 | |||
168 | fseek (test_fd, 0, SEEK_SET); | ||
169 | if (fread (mem_test_file_local, sizeof (char), len, test_fd) != len) | ||
170 | { | ||
171 | free (mem_test_file_local); | ||
172 | free (doc_path); | ||
173 | fprintf (stderr, "Error: failed to read test file. %s\n", | ||
174 | strerror (errno)); | ||
175 | return -1; | ||
176 | } | ||
177 | |||
178 | if (NULL == (cbc.buf = malloc (len))) | ||
179 | { | ||
180 | free (mem_test_file_local); | ||
181 | free (doc_path); | ||
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, ©Buffer); | ||
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 | free (mem_test_file_local); | ||
223 | free (doc_path); | ||
224 | free (cbc.buf); | ||
225 | return errornum; | ||
226 | } | ||
227 | |||
228 | curl_easy_cleanup (c); | ||
229 | |||
230 | if (memcmp (cbc.buf, mem_test_file_local, len) != 0) | ||
231 | { | ||
232 | fprintf (stderr, "Error: local file & received file differ.\n"); | ||
233 | free (cbc.buf); | ||
234 | free (mem_test_file_local); | ||
235 | free (doc_path); | ||
236 | return -1; | ||
237 | } | ||
238 | |||
239 | free (mem_test_file_local); | ||
240 | free (cbc.buf); | ||
241 | free (doc_path); | ||
242 | return 0; | ||
243 | } | ||
244 | 43 | ||
245 | static int | 44 | static int |
246 | test_cipher_option (FILE * test_fd, char *cipher_suite, int proto_version) | 45 | test_cipher_option (FILE * test_fd, char *cipher_suite, int proto_version) |
@@ -262,43 +61,12 @@ test_cipher_option (FILE * test_fd, char *cipher_suite, int proto_version) | |||
262 | return -1; | 61 | return -1; |
263 | } | 62 | } |
264 | 63 | ||
265 | ret = test_daemon_get (test_fd, cipher_suite, proto_version); | 64 | ret = test_https_transfer (test_fd, cipher_suite, proto_version); |
266 | 65 | ||
267 | MHD_stop_daemon (d); | 66 | MHD_stop_daemon (d); |
268 | return ret; | 67 | return ret; |
269 | } | 68 | } |
270 | 69 | ||
271 | /* setup a temporary transfer test file */ | ||
272 | static FILE * | ||
273 | setupTestFile () | ||
274 | { | ||
275 | FILE *test_fd; | ||
276 | |||
277 | if (NULL == (test_fd = fopen (test_file_name, "w+"))) | ||
278 | { | ||
279 | fprintf (stderr, "Error: failed to open `%s': %s\n", | ||
280 | test_file_name, strerror (errno)); | ||
281 | return NULL; | ||
282 | } | ||
283 | if (fwrite (test_file_data, sizeof (char), strlen (test_file_data), test_fd) | ||
284 | != strlen (test_file_data)) | ||
285 | { | ||
286 | fprintf (stderr, "Error: failed to write `%s. %s'\n", | ||
287 | test_file_name, strerror (errno)); | ||
288 | fclose (test_fd); | ||
289 | return NULL; | ||
290 | } | ||
291 | if (fflush (test_fd)) | ||
292 | { | ||
293 | fprintf (stderr, "Error: failed to flush test file stream. %s\n", | ||
294 | strerror (errno)); | ||
295 | fclose (test_fd); | ||
296 | return NULL; | ||
297 | } | ||
298 | |||
299 | return test_fd; | ||
300 | } | ||
301 | |||
302 | /* perform a HTTP GET request via SSL/TLS */ | 70 | /* perform a HTTP GET request via SSL/TLS */ |
303 | int | 71 | int |
304 | test_secure_get (FILE * test_fd, char *cipher_suite, int proto_version) | 72 | test_secure_get (FILE * test_fd, char *cipher_suite, int proto_version) |
@@ -319,7 +87,7 @@ test_secure_get (FILE * test_fd, char *cipher_suite, int proto_version) | |||
319 | return -1; | 87 | return -1; |
320 | } | 88 | } |
321 | 89 | ||
322 | ret = test_daemon_get (test_fd, cipher_suite, proto_version); | 90 | ret = test_https_transfer (test_fd, cipher_suite, proto_version); |
323 | 91 | ||
324 | MHD_stop_daemon (d); | 92 | MHD_stop_daemon (d); |
325 | return ret; | 93 | return ret; |
@@ -338,7 +106,7 @@ main (int argc, char *const *argv) | |||
338 | return -1; | 106 | return -1; |
339 | } | 107 | } |
340 | 108 | ||
341 | if ((test_fd = setupTestFile ()) == NULL) | 109 | if ((test_fd = setup_test_file ()) == NULL) |
342 | { | 110 | { |
343 | fprintf (stderr, MHD_E_TEST_FILE_CREAT); | 111 | fprintf (stderr, MHD_E_TEST_FILE_CREAT); |
344 | return -1; | 112 | return -1; |
@@ -358,11 +126,11 @@ main (int argc, char *const *argv) | |||
358 | errorCount += | 126 | errorCount += |
359 | test_cipher_option (test_fd, "DES-CBC3-SHA", CURL_SSLVERSION_TLSv1); | 127 | test_cipher_option (test_fd, "DES-CBC3-SHA", CURL_SSLVERSION_TLSv1); |
360 | 128 | ||
129 | print_test_result (errorCount, argv[0]); | ||
361 | 130 | ||
362 | curl_global_cleanup (); | 131 | curl_global_cleanup (); |
363 | fclose (test_fd); | 132 | fclose (test_fd); |
364 | 133 | remove (TEST_FILE_NAME); | |
365 | remove (test_file_name); | ||
366 | 134 | ||
367 | return errorCount != 0; | 135 | return errorCount != 0; |
368 | } | 136 | } |