diff options
author | lv-426 <oxcafebaby@yahoo.com> | 2008-06-25 13:59:37 +0000 |
---|---|---|
committer | lv-426 <oxcafebaby@yahoo.com> | 2008-06-25 13:59:37 +0000 |
commit | 1207a5ea6b2de79fe47aec8256c6a4a849753444 (patch) | |
tree | c6c54c262209db015de0d998dc1330982a9374be /src/testcurl/daemon_HTTPS_test_get.c | |
parent | ce6e698b741be754d2e1073b7e4f964d9ec4c36d (diff) | |
download | libmicrohttpd-1207a5ea6b2de79fe47aec8256c6a4a849753444.tar.gz libmicrohttpd-1207a5ea6b2de79fe47aec8256c6a4a849753444.zip |
HTTPS daemon test file
currently MHDS state machine unused
Diffstat (limited to 'src/testcurl/daemon_HTTPS_test_get.c')
-rw-r--r-- | src/testcurl/daemon_HTTPS_test_get.c | 276 |
1 files changed, 276 insertions, 0 deletions
diff --git a/src/testcurl/daemon_HTTPS_test_get.c b/src/testcurl/daemon_HTTPS_test_get.c new file mode 100644 index 00000000..a4b957cc --- /dev/null +++ b/src/testcurl/daemon_HTTPS_test_get.c | |||
@@ -0,0 +1,276 @@ | |||
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 daemon_HTTPS_test_get.c | ||
23 | * @brief Testcase for libmicrohttpd GET operations | ||
24 | * @author lv-426 | ||
25 | */ | ||
26 | |||
27 | #include "config.h" | ||
28 | #include "plibc.h" | ||
29 | #include "microhttpd.h" | ||
30 | #include <errno.h> | ||
31 | |||
32 | #include <curl/curl.h> | ||
33 | #include <stdlib.h> | ||
34 | #include <string.h> | ||
35 | #include <time.h> | ||
36 | #include <sys/types.h> | ||
37 | #include <sys/stat.h> | ||
38 | #include <unistd.h> | ||
39 | |||
40 | #define BUF_SIZE 1024 | ||
41 | #define MAX_URL_LEN 255 | ||
42 | |||
43 | /* Test Certificate */ | ||
44 | const char cert_pem[] = | ||
45 | "-----BEGIN CERTIFICATE-----\n" | ||
46 | "MIIB5zCCAVKgAwIBAgIERiYdJzALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n" | ||
47 | "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTExWhcNMDgwNDE3MTMyOTExWjAZMRcw\n" | ||
48 | "FQYDVQQDEw5HbnVUTFMgdGVzdCBDQTCBnDALBgkqhkiG9w0BAQEDgYwAMIGIAoGA\n" | ||
49 | "vuyYeh1vfmslnuggeEKgZAVmQ5ltSdUY7H25WGSygKMUYZ0KT74v8C780qtcNt9T\n" | ||
50 | "7EPH/N6RvB4BprdssgcQLsthR3XKA84jbjjxNCcaGs33lvOz8A1nf8p3hD+cKfRi\n" | ||
51 | "kfYSW2JazLrtCC4yRCas/SPOUxu78of+3HiTfFm/oXUCAwEAAaNDMEEwDwYDVR0T\n" | ||
52 | "AQH/BAUwAwEB/zAPBgNVHQ8BAf8EBQMDBwQAMB0GA1UdDgQWBBTpPBz7rZJu5gak\n" | ||
53 | "Viyi4cBTJ8jylTALBgkqhkiG9w0BAQUDgYEAiaIRqGfp1jPpNeVhABK60SU0KIAy\n" | ||
54 | "njuu7kHq5peUgYn8Jd9zNzExBOEp1VOipGsf6G66oQAhDFp2o8zkz7ZH71zR4HEW\n" | ||
55 | "KoX6n5Emn6DvcEH/9pAhnGxNHJAoS7czTKv/JDZJhkqHxyrE1fuLsg5Qv25DTw7+\n" | ||
56 | "PfqUpIhz5Bbm7J4=\n" "-----END CERTIFICATE-----\n"; | ||
57 | |||
58 | const char key_pem[] = | ||
59 | "-----BEGIN RSA PRIVATE KEY-----\n" | ||
60 | "MIICXAIBAAKBgQC7ZkP18sXXtozMxd/1iDuxyUtqDqGtIFBACIChT1yj0Phsz+Y8\n" | ||
61 | "9+wEdhMXi2SJIlvA3VN8O+18BLuAuSi+jpvGjqClEsv1Vx6i57u3M0mf47tKrmpN\n" | ||
62 | "aP/JEeIyjc49gAuNde/YAIGPKAQDoCKNYQQH+rY3fSEHSdIJYWmYkKNYqQIDAQAB\n" | ||
63 | "AoGADpmARG5CQxS+AesNkGmpauepiCz1JBF/JwnyiX6vEzUh0Ypd39SZztwrDxvF\n" | ||
64 | "PJjQaKVljml1zkJpIDVsqvHdyVdse8M+Qn6hw4x2p5rogdvhhIL1mdWo7jWeVJTF\n" | ||
65 | "RKB7zLdMPs3ySdtcIQaF9nUAQ2KJEvldkO3m/bRJFEp54k0CQQDYy+RlTmwRD6hy\n" | ||
66 | "7UtMjR0H3CSZJeQ8svMCxHLmOluG9H1UKk55ZBYfRTsXniqUkJBZ5wuV1L+pR9EK\n" | ||
67 | "ca89a+1VAkEA3UmBelwEv2u9cAU1QjKjmwju1JgXbrjEohK+3B5y0ESEXPAwNQT9\n" | ||
68 | "TrDM1m9AyxYTWLxX93dI5QwNFJtmbtjeBQJARSCWXhsoaDRG8QZrCSjBxfzTCqZD\n" | ||
69 | "ZXtl807ymCipgJm60LiAt0JLr4LiucAsMZz6+j+quQbSakbFCACB8SLV1QJBAKZQ\n" | ||
70 | "YKf+EPNtnmta/rRKKvySsi3GQZZN+Dt3q0r094XgeTsAqrqujVNfPhTMeP4qEVBX\n" | ||
71 | "/iVX2cmMTSh3w3z8MaECQEp0XJWDVKOwcTW6Ajp9SowtmiZ3YDYo1LF9igb4iaLv\n" | ||
72 | "sWZGfbnU3ryjvkb6YuFjgtzbZDZHWQCo8/cOtOBmPdk=\n" | ||
73 | "-----END RSA PRIVATE KEY-----\n"; | ||
74 | |||
75 | struct CBC | ||
76 | { | ||
77 | char *buf; | ||
78 | size_t pos; | ||
79 | size_t size; | ||
80 | }; | ||
81 | |||
82 | static size_t | ||
83 | copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx) | ||
84 | { | ||
85 | struct CBC *cbc = ctx; | ||
86 | |||
87 | if (cbc->pos + size * nmemb > cbc->size) | ||
88 | return 0; /* overflow */ | ||
89 | memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb); | ||
90 | cbc->pos += size * nmemb; | ||
91 | return size * nmemb; | ||
92 | } | ||
93 | |||
94 | static int | ||
95 | file_reader (void *cls, size_t pos, char *buf, int max) | ||
96 | { | ||
97 | FILE *file = cls; | ||
98 | fseek (file, pos, SEEK_SET); | ||
99 | return fread (buf, 1, max, file); | ||
100 | } | ||
101 | |||
102 | /* HTTP access handler call back */ | ||
103 | static int | ||
104 | http_ahc (void *cls, | ||
105 | struct MHD_Connection *connection, | ||
106 | const char *url, | ||
107 | const char *method, | ||
108 | const char *upload_data, | ||
109 | const char *version, unsigned int *upload_data_size, void **ptr) | ||
110 | { | ||
111 | static int aptr; | ||
112 | static char full_url[MAX_URL_LEN]; | ||
113 | struct MHD_Response *response; | ||
114 | int ret; | ||
115 | FILE *file; | ||
116 | struct stat buf; | ||
117 | |||
118 | // TODO never respond on first call | ||
119 | if (0 != strcmp (method, MHD_HTTP_METHOD_GET)) | ||
120 | return MHD_NO; /* unexpected method */ | ||
121 | if (&aptr != *ptr) | ||
122 | { | ||
123 | /* do never respond on first call */ | ||
124 | *ptr = &aptr; | ||
125 | return MHD_YES; | ||
126 | } | ||
127 | *ptr = NULL; /* reset when done */ | ||
128 | |||
129 | file = fopen (url, "r"); | ||
130 | if (file == NULL) | ||
131 | { | ||
132 | return 1; | ||
133 | } | ||
134 | else | ||
135 | { | ||
136 | stat (&url[1], &buf); | ||
137 | response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k PAGE_NOT_FOUND size */ | ||
138 | &file_reader, file, | ||
139 | (MHD_ContentReaderFreeCallback) | ||
140 | & fclose); | ||
141 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | ||
142 | MHD_destroy_response (response); | ||
143 | } | ||
144 | return ret; | ||
145 | } | ||
146 | |||
147 | static int | ||
148 | test_HTTPS_Get () | ||
149 | { | ||
150 | struct MHD_Daemon *d; | ||
151 | CURL *c; | ||
152 | struct CBC cbc; | ||
153 | CURLcode errornum; | ||
154 | char *doc_path; | ||
155 | char *url; | ||
156 | /* currently use self as test file - consider better alternatives */ | ||
157 | char *test_file_name = "daemon_HTTPS_test_get"; | ||
158 | struct stat test_file_stat; | ||
159 | FILE *key_file, *cert_file, *test_file; | ||
160 | |||
161 | /* used to memcmp local copy & deamon supplied copy */ | ||
162 | unsigned char *mem_test_file_local; | ||
163 | unsigned char *mem_test_file_recv; | ||
164 | |||
165 | /* setup test file path, url */ | ||
166 | doc_path = get_current_dir_name (); | ||
167 | |||
168 | /* construct url - this might use doc_path */ | ||
169 | url = | ||
170 | (char *) malloc (sizeof (char) * | ||
171 | (strlen (test_file_name) + | ||
172 | strlen ("https://127.0.0.1:42433/"))); | ||
173 | strncat (url, "https://127.0.0.1:42433/", strlen ("https://127.0.0.1:42433/")); | ||
174 | strncat (url, test_file_name, strlen (test_file_name)); | ||
175 | |||
176 | /* look for test file used for testing */ | ||
177 | key_file = fopen ("key_file", "w"); | ||
178 | cert_file = fopen ("cert_file", "w"); | ||
179 | test_file = fopen (test_file_name, "r"); | ||
180 | if ( key_file == NULL) | ||
181 | { | ||
182 | fprintf (stderr, "Error : failed to open key_file. errno:%d\n", errno); | ||
183 | return 1; | ||
184 | } | ||
185 | if (!cert_file) | ||
186 | { | ||
187 | fprintf (stderr, "Error : failed to open cert_file. errno:%d\n", errno); | ||
188 | return 1; | ||
189 | } | ||
190 | if (!test_file) | ||
191 | { | ||
192 | fprintf (stderr, "Error : failed to open test_file. errno:%d\n", errno); | ||
193 | return 1; | ||
194 | } | ||
195 | if (stat (test_file_name, &test_file_stat) == -1) | ||
196 | return 1; | ||
197 | |||
198 | /* create test cert & key */ | ||
199 | fwrite (key_pem, 1, sizeof (key_pem), key_file); | ||
200 | fwrite (cert_pem, 1, sizeof (cert_pem), cert_file); | ||
201 | mem_test_file_local = malloc (test_file_stat.st_size); | ||
202 | mem_test_file_recv = malloc (test_file_stat.st_size); | ||
203 | fread (mem_test_file_local, 1, test_file_stat.st_size, test_file); | ||
204 | |||
205 | fclose (key_file); | ||
206 | fclose (cert_file); | ||
207 | fclose (test_file); | ||
208 | |||
209 | cbc.buf = mem_test_file_recv; | ||
210 | cbc.size = test_file_stat.st_size; | ||
211 | cbc.pos = 0; | ||
212 | |||
213 | /* setup test */ | ||
214 | d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, | ||
215 | 42433, NULL, NULL, &http_ahc, NULL, | ||
216 | MHD_OPTION_DOC_ROOT, doc_path, MHD_OPTION_END); | ||
217 | if (d == NULL) | ||
218 | return 1; | ||
219 | |||
220 | c = curl_easy_init (); | ||
221 | curl_easy_setopt (c, CURLOPT_URL, url); | ||
222 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | ||
223 | curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); | ||
224 | curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L); | ||
225 | curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); | ||
226 | curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); | ||
227 | /* TLS options */ | ||
228 | curl_easy_setopt (c, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); | ||
229 | curl_easy_setopt (c, CURLOPT_SSL_CIPHER_LIST, "AES256-SHA"); | ||
230 | /* currently skip peer authentication */ | ||
231 | curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0); | ||
232 | // curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); | ||
233 | |||
234 | // NOTE: use of CONNECTTIMEOUT without also | ||
235 | // setting NOSIGNAL results in really weird | ||
236 | // crashes on my system! | ||
237 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); | ||
238 | if (CURLE_OK != (errornum = curl_easy_perform (c))) | ||
239 | { | ||
240 | fprintf (stderr, | ||
241 | "curl_easy_perform failed: `%s'\n", | ||
242 | curl_easy_strerror (errornum)); | ||
243 | curl_easy_cleanup (c); | ||
244 | MHD_stop_daemon (d); | ||
245 | return 2; | ||
246 | } | ||
247 | curl_easy_cleanup (c); | ||
248 | MHD_stop_daemon (d); | ||
249 | if (remove ("key_file") != 0) | ||
250 | fprintf (stderr, "Error : failed to remove key_file.\n"); | ||
251 | if (remove ("cert_file") != 0) | ||
252 | fprintf (stderr, "Error : failed to remove cert_file.\n"); | ||
253 | |||
254 | fprintf (stderr, "file = %s.\n" , mem_test_file_recv ); | ||
255 | if (memcmp (cbc.buf, mem_test_file_local, test_file_stat.st_size) == 0) | ||
256 | { | ||
257 | // TODO find proper error code | ||
258 | return 1; | ||
259 | } | ||
260 | return 0; | ||
261 | } | ||
262 | |||
263 | int | ||
264 | main (int argc, char *const *argv) | ||
265 | { | ||
266 | |||
267 | unsigned int errorCount = 0; | ||
268 | |||
269 | if (0 != curl_global_init (CURL_GLOBAL_WIN32)) | ||
270 | return 2; | ||
271 | errorCount += test_HTTPS_Get (); | ||
272 | if (errorCount != 0) | ||
273 | fprintf (stderr, "Error (code: %u)\n", errorCount); | ||
274 | curl_global_cleanup (); | ||
275 | return errorCount == 0; /* 0 == pass */ | ||
276 | } | ||