diff options
Diffstat (limited to 'src/daemon/daemontest.c')
-rw-r--r-- | src/daemon/daemontest.c | 215 |
1 files changed, 198 insertions, 17 deletions
diff --git a/src/daemon/daemontest.c b/src/daemon/daemontest.c index 5ebe1e24..b8d98efc 100644 --- a/src/daemon/daemontest.c +++ b/src/daemon/daemontest.c | |||
@@ -32,7 +32,15 @@ | |||
32 | #include <string.h> | 32 | #include <string.h> |
33 | 33 | ||
34 | static int testStartError() { | 34 | static int testStartError() { |
35 | return NULL != MHD_start_daemon(0, 0, NULL, NULL, NULL, NULL); | 35 | struct MHD_Daemon * d; |
36 | |||
37 | d = MHD_start_daemon(MHD_USE_DEBUG, 0, NULL, NULL, NULL, NULL); | ||
38 | |||
39 | if(d == NULL) { | ||
40 | return 0; | ||
41 | } else { | ||
42 | return 1; | ||
43 | } | ||
36 | } | 44 | } |
37 | 45 | ||
38 | static int apc_nothing(void * cls, | 46 | static int apc_nothing(void * cls, |
@@ -99,41 +107,184 @@ static int ahc_echo(void * cls, | |||
99 | static int testStartStop() { | 107 | static int testStartStop() { |
100 | struct MHD_Daemon * d; | 108 | struct MHD_Daemon * d; |
101 | 109 | ||
102 | d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_IPv4, | 110 | d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_IPv4 | MHD_USE_DEBUG, |
103 | 1080, | 111 | 1080, |
104 | &apc_nothing, | 112 | &apc_nothing, |
105 | NULL, | 113 | NULL, |
106 | &ahc_nothing, | 114 | &ahc_nothing, |
107 | NULL); | 115 | NULL); |
116 | if (d == NULL) { | ||
117 | return 1; | ||
118 | } | ||
119 | MHD_stop_daemon(d); | ||
120 | return 0; | ||
121 | } | ||
122 | |||
123 | static int testRun() { | ||
124 | struct MHD_Daemon * d; | ||
125 | fd_set read; | ||
126 | int maxfd; | ||
127 | int i; | ||
128 | |||
129 | d = MHD_start_daemon(MHD_USE_IPv4 | MHD_USE_DEBUG, | ||
130 | 1080, | ||
131 | &apc_all, | ||
132 | NULL, | ||
133 | &ahc_nothing, | ||
134 | NULL); | ||
135 | |||
136 | if(d == NULL) { | ||
137 | return 1; | ||
138 | } | ||
139 | fprintf(stderr, "Testing external select!\n"); | ||
140 | i = 0; | ||
141 | while(i < 15) { | ||
142 | MHD_get_fdset(d, &read, &read, &read, &maxfd); | ||
143 | if(MHD_run(d) == MHD_NO) { | ||
144 | MHD_stop_daemon(d); | ||
145 | return 1; | ||
146 | } | ||
147 | sleep(1); | ||
148 | i++; | ||
149 | } | ||
150 | return 0; | ||
151 | } | ||
152 | |||
153 | static int testThread() { | ||
154 | struct MHD_Daemon * d; | ||
155 | d = MHD_start_daemon(MHD_USE_IPv4 | MHD_USE_DEBUG | MHD_USE_SELECT_INTERNALLY, | ||
156 | 1081, | ||
157 | &apc_all, | ||
158 | NULL, | ||
159 | &ahc_nothing, | ||
160 | NULL); | ||
161 | |||
162 | if(d == NULL) { | ||
163 | return 1; | ||
164 | } | ||
165 | |||
166 | fprintf(stderr, "Testing internal select!\n"); | ||
167 | if (MHD_run(d) == MHD_NO) { | ||
168 | return 1; | ||
169 | } else { | ||
170 | sleep(15); | ||
171 | MHD_stop_daemon(d); | ||
172 | } | ||
173 | return 0; | ||
174 | } | ||
175 | |||
176 | static int testMultithread() { | ||
177 | struct MHD_Daemon * d; | ||
178 | d = MHD_start_daemon(MHD_USE_IPv4 | MHD_USE_DEBUG | MHD_USE_THREAD_PER_CONNECTION, | ||
179 | 1082, | ||
180 | &apc_all, | ||
181 | NULL, | ||
182 | &ahc_nothing, | ||
183 | NULL); | ||
184 | |||
185 | if(d == NULL) { | ||
186 | return 1; | ||
187 | } | ||
188 | |||
189 | fprintf(stderr, "Testing thread per connection!\n"); | ||
190 | if (MHD_run(d) == MHD_NO) { | ||
191 | return 1; | ||
192 | } else { | ||
193 | sleep(15); | ||
194 | MHD_stop_daemon(d); | ||
195 | } | ||
196 | return 0; | ||
197 | } | ||
198 | |||
199 | static int testInternalGet() { | ||
200 | struct MHD_Daemon * d; | ||
201 | CURL * c; | ||
202 | char buf[2048]; | ||
203 | struct CBC cbc; | ||
204 | |||
205 | cbc.buf = buf; | ||
206 | cbc.size = 2048; | ||
207 | cbc.pos = 0; | ||
208 | d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_IPv4 | MHD_USE_DEBUG, | ||
209 | 1083, | ||
210 | &apc_all, | ||
211 | NULL, | ||
212 | &ahc_echo, | ||
213 | "GET"); | ||
108 | if (d == NULL) | 214 | if (d == NULL) |
109 | return 1; | 215 | return 1; |
216 | |||
217 | if(MHD_run(d) == MHD_NO) { | ||
218 | MHD_stop_daemon(d); | ||
219 | return 2; | ||
220 | } | ||
221 | |||
222 | c = curl_easy_init(); | ||
223 | curl_easy_setopt(c, | ||
224 | CURLOPT_URL, | ||
225 | "http://localhost:1083/hello_world"); | ||
226 | curl_easy_setopt(c, | ||
227 | CURLOPT_WRITEFUNCTION, | ||
228 | ©Buffer); | ||
229 | curl_easy_setopt(c, | ||
230 | CURLOPT_WRITEDATA, | ||
231 | &cbc); | ||
232 | curl_easy_setopt(c, | ||
233 | CURLOPT_FAILONERROR, | ||
234 | 1); | ||
235 | curl_easy_setopt(c, | ||
236 | CURLOPT_CONNECTTIMEOUT, | ||
237 | 15L); | ||
238 | // NOTE: use of CONNECTTIMEOUT without also | ||
239 | // setting NOSIGNAL results in really weird | ||
240 | // crashes on my system! | ||
241 | curl_easy_setopt(c, | ||
242 | CURLOPT_NOSIGNAL, | ||
243 | 1); | ||
244 | if (CURLE_OK != curl_easy_perform(c)) | ||
245 | return 3; | ||
246 | |||
247 | curl_easy_cleanup(c); | ||
248 | |||
249 | if (cbc.pos != strlen("hello_world")) | ||
250 | return 4; | ||
251 | |||
252 | if (0 != strncmp("hello_world", | ||
253 | cbc.buf, | ||
254 | strlen("hello_world"))) | ||
255 | return 5; | ||
256 | |||
110 | MHD_stop_daemon(d); | 257 | MHD_stop_daemon(d); |
258 | |||
111 | return 0; | 259 | return 0; |
112 | } | 260 | } |
113 | 261 | ||
114 | static int testGet() { | 262 | static int testMultithreadedGet() { |
115 | struct MHD_Daemon * d; | 263 | struct MHD_Daemon * d; |
116 | CURL * c; | 264 | CURL * c; |
117 | int ret; | ||
118 | char buf[2048]; | 265 | char buf[2048]; |
119 | struct CBC cbc; | 266 | struct CBC cbc; |
120 | 267 | ||
121 | cbc.buf = buf; | 268 | cbc.buf = buf; |
122 | cbc.size = 2048; | 269 | cbc.size = 2048; |
123 | cbc.pos = 0; | 270 | cbc.pos = 0; |
124 | ret = 0; | 271 | d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_IPv4 | MHD_USE_DEBUG, |
125 | d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_IPv4, | 272 | 1084, |
126 | 1080, | ||
127 | &apc_all, | 273 | &apc_all, |
128 | NULL, | 274 | NULL, |
129 | &ahc_echo, | 275 | &ahc_echo, |
130 | "GET"); | 276 | "GET"); |
131 | if (d == NULL) | 277 | if (d == NULL) |
132 | return 1; | 278 | return 1; |
279 | |||
280 | if(MHD_run(d) == MHD_NO) | ||
281 | return 2; | ||
282 | |||
283 | |||
133 | c = curl_easy_init(); | 284 | c = curl_easy_init(); |
134 | curl_easy_setopt(c, | 285 | curl_easy_setopt(c, |
135 | CURLOPT_URL, | 286 | CURLOPT_URL, |
136 | "http://localhost:1080/hello_world"); | 287 | "http://localhost:1084/hello_world"); |
137 | curl_easy_setopt(c, | 288 | curl_easy_setopt(c, |
138 | CURLOPT_WRITEFUNCTION, | 289 | CURLOPT_WRITEFUNCTION, |
139 | ©Buffer); | 290 | ©Buffer); |
@@ -146,31 +297,61 @@ static int testGet() { | |||
146 | curl_easy_setopt(c, | 297 | curl_easy_setopt(c, |
147 | CURLOPT_CONNECTTIMEOUT, | 298 | CURLOPT_CONNECTTIMEOUT, |
148 | 15L); | 299 | 15L); |
149 | /* NOTE: use of CONNECTTIMEOUT without also | 300 | // NOTE: use of CONNECTTIMEOUT without also |
150 | setting NOSIGNAL results in really weird | 301 | // setting NOSIGNAL results in really weird |
151 | crashes on my system! */ | 302 | // crashes on my system! |
152 | curl_easy_setopt(c, | 303 | curl_easy_setopt(c, |
153 | CURLOPT_NOSIGNAL, | 304 | CURLOPT_NOSIGNAL, |
154 | 1); | 305 | 1); |
155 | if (CURLE_OK != curl_easy_perform(c)) | 306 | if (CURLE_OK != curl_easy_perform(c)) |
156 | ret = 1; | 307 | return 3; |
157 | curl_easy_cleanup(c); | 308 | curl_easy_cleanup(c); |
158 | if (cbc.pos != strlen("hello_world")) | 309 | if (cbc.pos != strlen("hello_world")) |
159 | ret = 2; | 310 | return 4; |
311 | |||
160 | if (0 != strncmp("hello_world", | 312 | if (0 != strncmp("hello_world", |
161 | cbc.buf, | 313 | cbc.buf, |
162 | strlen("hello_world"))) | 314 | strlen("hello_world"))) |
163 | ret = 3; | 315 | return 5; |
316 | |||
164 | MHD_stop_daemon(d); | 317 | MHD_stop_daemon(d); |
165 | return ret; | 318 | |
319 | return 0; | ||
166 | } | 320 | } |
167 | 321 | ||
168 | int main(int argc, | 322 | int main(int argc, |
169 | char * const * argv) { | 323 | char * const * argv) { |
170 | unsigned int errorCount = 0; | 324 | unsigned int errorCount = 0; |
171 | 325 | fprintf(stderr, "***testStartError()***\n"); | |
326 | fprintf(stderr, "***This test verifies the start function responds to bad arguments correctly***\n"); | ||
172 | errorCount += testStartError(); | 327 | errorCount += testStartError(); |
328 | fprintf(stderr, "errorCount is %i\n", errorCount); | ||
329 | fprintf(stderr, "***testStartStop()***\n"); | ||
330 | fprintf(stderr, "***This test verifies that the daemon can be started and stopped normally***\n"); | ||
173 | errorCount += testStartStop(); | 331 | errorCount += testStartStop(); |
174 | errorCount += testGet(); | 332 | fprintf(stderr, "errorCount is %i\n", errorCount); |
333 | fprintf(stderr, "***testInternalGet()***\n"); | ||
334 | fprintf(stderr, "***This test verifies the functionality of internal select using a canned request***\n"); | ||
335 | errorCount += testInternalGet(); | ||
336 | fprintf(stderr, "errorCount is %i\n", errorCount); | ||
337 | fprintf(stderr, "***testMultithreadedGet()***\n"); | ||
338 | fprintf(stderr, "***This test verifies the functionality of multithreaded connections using a canned request***\n"); | ||
339 | errorCount += testMultithreadedGet(); | ||
340 | fprintf(stderr, "errorCount is %i\n", errorCount); | ||
341 | fprintf(stderr, "***testRun()***\n"); | ||
342 | fprintf(stderr, "***This test verifies the functionality of external select***\n"); | ||
343 | fprintf(stderr, "***The sever will sit on the announced port for 15 seconds and wait for external messages***\n"); | ||
344 | errorCount += testRun(); | ||
345 | fprintf(stderr, "errorCount is %i\n", errorCount); | ||
346 | fprintf(stderr, "***testThread()***\n"); | ||
347 | fprintf(stderr, "***This test verifies the functionality of internal select***\n"); | ||
348 | fprintf(stderr, "***The sever will sit on the announced port for 15 seconds and wait for external messages***\n"); | ||
349 | errorCount += testThread(); | ||
350 | fprintf(stderr, "errorCount is %i\n", errorCount); | ||
351 | fprintf(stderr, "***testMultithread()***\n"); | ||
352 | fprintf(stderr, "***This test verifies the functionality of multithreaded connections***\n"); | ||
353 | fprintf(stderr, "***The sever will sit on the announced port for 15 seconds and wait for external messages***\n"); | ||
354 | errorCount += testMultithread(); | ||
355 | fprintf(stderr, "errorCount is %i\n", errorCount); | ||
175 | return errorCount != 0; /* 0 == pass */ | 356 | return errorCount != 0; /* 0 == pass */ |
176 | } | 357 | } |