1 /* Copyright (c) 2015-2018 the Civetweb developers
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  */
21 
22 /**
23  * We include the source file so that we have access to the internal private
24  * static functions
25  */
26 #ifdef _MSC_VER
27 #ifndef _CRT_SECURE_NO_WARNINGS
28 #define _CRT_SECURE_NO_WARNINGS
29 #endif
30 #define CIVETWEB_API static
31 #endif
32 
33 #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
34 #undef MEMORY_DEBUGGING
35 #endif
36 
37 #include "../src/civetweb.c"
38 
39 #include <stdlib.h>
40 #include <time.h>
41 
42 #include "private.h"
43 
44 
45 /* This unit test file uses the excellent Check unit testing library.
46  * The API documentation is available here:
47  * http://check.sourceforge.net/doc/check_html/index.html
48  */
49 
50 static char tmp_parse_buffer[1024];
51 
52 static int
test_parse_http_response(char * buf,int len,struct mg_response_info * ri)53 test_parse_http_response(char *buf, int len, struct mg_response_info *ri)
54 {
55 	ck_assert_int_lt(len, (int)sizeof(tmp_parse_buffer));
56 	memcpy(tmp_parse_buffer, buf, (size_t)len);
57 	return parse_http_response(tmp_parse_buffer, len, ri);
58 }
59 
60 static int
test_parse_http_request(char * buf,int len,struct mg_request_info * ri)61 test_parse_http_request(char *buf, int len, struct mg_request_info *ri)
62 {
63 	ck_assert_int_lt(len, (int)sizeof(tmp_parse_buffer));
64 	memcpy(tmp_parse_buffer, buf, (size_t)len);
65 	return parse_http_request(tmp_parse_buffer, len, ri);
66 }
67 
68 
START_TEST(test_parse_http_message)69 START_TEST(test_parse_http_message)
70 {
71 	/* Adapted from unit_test.c */
72 	/* Copyright (c) 2013-2015 the Civetweb developers */
73 	/* Copyright (c) 2004-2013 Sergey Lyubka */
74 	struct mg_request_info ri;
75 	struct mg_response_info respi;
76 	char empty[] = "";
77 	char space[] = " \x00";
78 	char req1[] = "GET / HTTP/1.1\r\n\r\n";
79 	char req2[] = "BLAH / HTTP/1.1\r\n\r\n";
80 	char req3[] = "GET / HTTP/1.1\nKey: Val\n\n";
81 	char req4[] =
82 	    "GET / HTTP/1.1\r\nA: foo bar\r\nB: bar\r\nskip\r\nbaz:\r\n\r\n";
83 	char req5[] = "GET / HTTP/1.0\n\n";
84 	char req6[] = "G";
85 	char req7[] = " blah ";
86 	char req8[] = "HTTP/1.0 404 Not Found\n\n";
87 	char req9[] = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n";
88 
89 	char req10[] = "GET / HTTP/1.1\r\nA: foo bar\r\nB: bar\r\n\r\n";
90 
91 	char req11[] = "GET /\r\nError: X\r\n\r\n";
92 
93 	char req12[] =
94 	    "POST /a/b/c.d?e=f&g HTTP/1.1\r\nKey1: val1\r\nKey2: val2\r\n\r\nBODY";
95 
96 
97 	int lenreq1 = (int)strlen(req1);
98 	int lenreq2 = (int)strlen(req2);
99 	int lenreq3 = (int)strlen(req3);
100 	int lenreq4 = (int)strlen(req4);
101 	int lenreq5 = (int)strlen(req5);
102 	int lenreq6 = (int)strlen(req6);
103 	int lenreq7 = (int)strlen(req7);
104 	int lenreq8 = (int)strlen(req8);
105 	int lenreq9 = (int)strlen(req9);
106 	int lenreq10 = (int)strlen(req10);
107 	int lenreq11 = (int)strlen(req11);
108 	int lenreq12 = (int)strlen(req12);
109 	int lenhdr12 = lenreq12 - 4; /* length without body */
110 
111 	mark_point();
112 
113 	/* An empty string is neither a complete request nor a complete
114 	 * response, so it must return 0 */
115 	ck_assert_int_eq(0, get_http_header_len(empty, 0));
116 	ck_assert_int_eq(0, test_parse_http_request(empty, 0, &ri));
117 	ck_assert_int_eq(0, test_parse_http_response(empty, 0, &respi));
118 
119 	/* Same is true for a leading space */
120 	ck_assert_int_eq(0, get_http_header_len(space, 1));
121 	ck_assert_int_eq(0, test_parse_http_request(space, 1, &ri));
122 	ck_assert_int_eq(0, test_parse_http_response(space, 1, &respi));
123 
124 	/* But a control character (like 0) makes it invalid */
125 	ck_assert_int_eq(-1, get_http_header_len(space, 2));
126 	ck_assert_int_eq(-1, test_parse_http_request(space, 2, &ri));
127 	ck_assert_int_eq(-1, test_parse_http_response(space, 2, &respi));
128 
129 
130 	/* req1 minus 1 byte at the end is incomplete */
131 	ck_assert_int_eq(0, get_http_header_len(req1, lenreq1 - 1));
132 
133 
134 	/* req1 minus 1 byte at the start is complete but invalid */
135 	ck_assert_int_eq(lenreq1 - 1, get_http_header_len(req1 + 1, lenreq1 - 1));
136 	ck_assert_int_eq(-1, test_parse_http_request(req1 + 1, lenreq1 - 1, &ri));
137 
138 
139 	/* req1 is a valid request */
140 	ck_assert_int_eq(lenreq1, get_http_header_len(req1, lenreq1));
141 	ck_assert_int_eq(-1, test_parse_http_response(req1, lenreq1, &respi));
142 	ck_assert_int_eq(lenreq1, test_parse_http_request(req1, lenreq1, &ri));
143 	ck_assert_str_eq("1.1", ri.http_version);
144 	ck_assert_int_eq(0, ri.num_headers);
145 
146 
147 	/* req2 is a complete, but invalid request */
148 	ck_assert_int_eq(lenreq2, get_http_header_len(req2, lenreq2));
149 	ck_assert_int_eq(-1, test_parse_http_request(req2, lenreq2, &ri));
150 
151 
152 	/* req3 is a complete and valid request */
153 	ck_assert_int_eq(lenreq3, get_http_header_len(req3, lenreq3));
154 	ck_assert_int_eq(lenreq3, test_parse_http_request(req3, lenreq3, &ri));
155 	ck_assert_int_eq(-1, test_parse_http_response(req3, lenreq3, &respi));
156 
157 
158 	/* Multiline header are obsolete, so return an error
159 	 * (https://tools.ietf.org/html/rfc7230#section-3.2.4). */
160 	ck_assert_int_eq(-1, test_parse_http_request(req4, lenreq4, &ri));
161 
162 
163 	/* req5 is a complete and valid request (also somewhat malformed,
164 	 * since it uses \n\n instead of \r\n\r\n) */
165 	ck_assert_int_eq(lenreq5, get_http_header_len(req5, lenreq5));
166 	ck_assert_int_eq(-1, test_parse_http_response(req5, lenreq5, &respi));
167 	ck_assert_int_eq(lenreq5, test_parse_http_request(req5, lenreq5, &ri));
168 	ck_assert_str_eq("GET", ri.request_method);
169 	ck_assert_str_eq("1.0", ri.http_version);
170 
171 
172 	/* req6 is incomplete */
173 	ck_assert_int_eq(0, get_http_header_len(req6, lenreq6));
174 	ck_assert_int_eq(0, test_parse_http_request(req6, lenreq6, &ri));
175 
176 
177 	/* req7 is invalid, but not yet complete */
178 	ck_assert_int_eq(0, get_http_header_len(req7, lenreq7));
179 	ck_assert_int_eq(0, test_parse_http_request(req7, lenreq7, &ri));
180 
181 
182 	/* req8 is a valid response */
183 	ck_assert_int_eq(lenreq8, get_http_header_len(req8, lenreq8));
184 	ck_assert_int_eq(-1, test_parse_http_request(req8, lenreq8, &ri));
185 	ck_assert_int_eq(lenreq8, test_parse_http_response(req8, lenreq8, &respi));
186 
187 
188 	/* req9 is a valid response */
189 	ck_assert_int_eq(lenreq9, get_http_header_len(req9, lenreq9));
190 	ck_assert_int_eq(-1, test_parse_http_request(req9, lenreq9, &ri));
191 	ck_assert_int_eq(lenreq9, test_parse_http_response(req9, lenreq9, &respi));
192 	ck_assert_int_eq(1, respi.num_headers);
193 
194 
195 	/* req10 is a valid request */
196 	ck_assert_int_eq(lenreq10, get_http_header_len(req10, lenreq10));
197 	ck_assert_int_eq(lenreq10, test_parse_http_request(req10, lenreq10, &ri));
198 	ck_assert_str_eq("1.1", ri.http_version);
199 	ck_assert_int_eq(2, ri.num_headers);
200 	ck_assert_str_eq("A", ri.http_headers[0].name);
201 	ck_assert_str_eq("foo bar", ri.http_headers[0].value);
202 	ck_assert_str_eq("B", ri.http_headers[1].name);
203 	ck_assert_str_eq("bar", ri.http_headers[1].value);
204 
205 
206 	/* req11 is a complete but valid request */
207 	ck_assert_int_eq(-1, test_parse_http_request(req11, lenreq11, &ri));
208 
209 
210 	/* req12 is a valid request with body data */
211 	ck_assert_int_gt(lenreq12, lenhdr12);
212 	ck_assert_int_eq(lenhdr12, get_http_header_len(req12, lenreq12));
213 	ck_assert_int_eq(lenhdr12, test_parse_http_request(req12, lenreq12, &ri));
214 }
215 END_TEST
216 
217 
START_TEST(test_should_keep_alive)218 START_TEST(test_should_keep_alive)
219 {
220 	/* Adapted from unit_test.c */
221 	/* Copyright (c) 2013-2015 the Civetweb developers */
222 	/* Copyright (c) 2004-2013 Sergey Lyubka */
223 	struct mg_connection conn;
224 	struct mg_context ctx;
225 	char req1[] = "GET / HTTP/1.1\r\n\r\n";
226 	char req2[] = "GET / HTTP/1.0\r\n\r\n";
227 	char req3[] = "GET / HTTP/1.1\r\nConnection: close\r\n\r\n";
228 	char req4[] = "GET / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";
229 	char yes[] = "yes";
230 	char no[] = "no";
231 
232 	int lenreq1 = (int)strlen(req1);
233 	int lenreq2 = (int)strlen(req2);
234 	int lenreq3 = (int)strlen(req3);
235 	int lenreq4 = (int)strlen(req4);
236 
237 	mark_point();
238 
239 	memset(&ctx, 0, sizeof(ctx));
240 	memset(&conn, 0, sizeof(conn));
241 	conn.phys_ctx = &ctx;
242 	conn.dom_ctx = &(ctx.dd);
243 	ck_assert_int_eq(test_parse_http_request(req1, lenreq1, &conn.request_info),
244 	                 lenreq1);
245 	conn.connection_type = 1; /* Valid request */
246 	ck_assert_int_eq(conn.request_info.num_headers, 0);
247 
248 	ctx.dd.config[ENABLE_KEEP_ALIVE] = no;
249 	ck_assert_int_eq(should_keep_alive(&conn), 0);
250 
251 	ctx.dd.config[ENABLE_KEEP_ALIVE] = yes;
252 	ck_assert_int_eq(should_keep_alive(&conn), 1);
253 
254 	conn.must_close = 1;
255 	ck_assert_int_eq(should_keep_alive(&conn), 0);
256 
257 	conn.must_close = 0;
258 	test_parse_http_request(req2, lenreq2, &conn.request_info);
259 	conn.connection_type = 1; /* Valid request */
260 	ck_assert_int_eq(conn.request_info.num_headers, 0);
261 	ck_assert_int_eq(should_keep_alive(&conn), 0);
262 
263 	test_parse_http_request(req3, lenreq3, &conn.request_info);
264 	conn.connection_type = 1; /* Valid request */
265 	ck_assert_int_eq(conn.request_info.num_headers, 1);
266 	ck_assert_int_eq(should_keep_alive(&conn), 0);
267 
268 	test_parse_http_request(req4, lenreq4, &conn.request_info);
269 	conn.connection_type = 1; /* Valid request */
270 	ck_assert_int_eq(conn.request_info.num_headers, 1);
271 	ck_assert_int_eq(should_keep_alive(&conn), 1);
272 
273 	conn.status_code = 200;
274 	conn.must_close = 1;
275 	ck_assert_int_eq(should_keep_alive(&conn), 0);
276 
277 	conn.status_code = 200;
278 	conn.must_close = 0;
279 	ck_assert_int_eq(should_keep_alive(&conn), 1);
280 
281 	conn.status_code = 200;
282 	conn.must_close = 0;
283 	conn.connection_type = 0; /* invalid */
284 	ck_assert_int_eq(should_keep_alive(&conn), 0);
285 }
286 END_TEST
287 
288 
START_TEST(test_match_prefix)289 START_TEST(test_match_prefix)
290 {
291 	/* Adapted from unit_test.c */
292 	/* Copyright (c) 2013-2015 the Civetweb developers */
293 	/* Copyright (c) 2004-2013 Sergey Lyubka */
294 	ck_assert_int_eq(4, match_prefix("/api", 4, "/api"));
295 	ck_assert_int_eq(3, match_prefix("/a/", 3, "/a/b/c"));
296 	ck_assert_int_eq(-1, match_prefix("/a/", 3, "/ab/c"));
297 	ck_assert_int_eq(4, match_prefix("/*/", 3, "/ab/c"));
298 	ck_assert_int_eq(6, match_prefix("**", 2, "/a/b/c"));
299 	ck_assert_int_eq(2, match_prefix("/*", 2, "/a/b/c"));
300 	ck_assert_int_eq(2, match_prefix("*/*", 3, "/a/b/c"));
301 	ck_assert_int_eq(5, match_prefix("**/", 3, "/a/b/c"));
302 	ck_assert_int_eq(5, match_prefix("**.foo|**.bar", 13, "a.bar"));
303 	ck_assert_int_eq(2, match_prefix("a|b|cd", 6, "cdef"));
304 	ck_assert_int_eq(2, match_prefix("a|b|c?", 6, "cdef"));
305 	ck_assert_int_eq(1, match_prefix("a|?|cd", 6, "cdef"));
306 	ck_assert_int_eq(-1, match_prefix("/a/**.cgi", 9, "/foo/bar/x.cgi"));
307 	ck_assert_int_eq(12, match_prefix("/a/**.cgi", 9, "/a/bar/x.cgi"));
308 	ck_assert_int_eq(5, match_prefix("**/", 3, "/a/b/c"));
309 	ck_assert_int_eq(-1, match_prefix("**/$", 4, "/a/b/c"));
310 	ck_assert_int_eq(5, match_prefix("**/$", 4, "/a/b/"));
311 	ck_assert_int_eq(0, match_prefix("$", 1, ""));
312 	ck_assert_int_eq(-1, match_prefix("$", 1, "x"));
313 	ck_assert_int_eq(1, match_prefix("*$", 2, "x"));
314 	ck_assert_int_eq(1, match_prefix("/$", 2, "/"));
315 	ck_assert_int_eq(-1, match_prefix("**/$", 4, "/a/b/c"));
316 	ck_assert_int_eq(5, match_prefix("**/$", 4, "/a/b/"));
317 	ck_assert_int_eq(0, match_prefix("*", 1, "/hello/"));
318 	ck_assert_int_eq(-1, match_prefix("**.a$|**.b$", 11, "/a/b.b/"));
319 	ck_assert_int_eq(6, match_prefix("**.a$|**.b$", 11, "/a/b.b"));
320 	ck_assert_int_eq(6, match_prefix("**.a$|**.b$", 11, "/a/B.A"));
321 	ck_assert_int_eq(5, match_prefix("**o$", 4, "HELLO"));
322 }
323 END_TEST
324 
325 
START_TEST(test_remove_double_dots_and_double_slashes)326 START_TEST(test_remove_double_dots_and_double_slashes)
327 {
328 	/* Adapted from unit_test.c */
329 	/* Copyright (c) 2013-2015 the Civetweb developers */
330 	/* Copyright (c) 2004-2013 Sergey Lyubka */
331 	struct {
332 		char before[20], after[20];
333 	} data[] = {
334 	    {"////a", "/a"},
335 	    {"/.....", "/."},
336 	    {"/......", "/"},
337 	    {"..", "."},
338 	    {"...", "."},
339 	    {"/...///", "/./"},
340 	    {"/a...///", "/a.../"},
341 	    {"/.x", "/.x"},
342 	    {"/\\", "/"},
343 	    {"/a\\", "/a\\"},
344 	    {"/a\\\\...", "/a\\."},
345 	};
346 	size_t i;
347 
348 	mark_point();
349 
350 	for (i = 0; i < ARRAY_SIZE(data); i++) {
351 		remove_double_dots_and_double_slashes(data[i].before);
352 		ck_assert_str_eq(data[i].before, data[i].after);
353 	}
354 }
355 END_TEST
356 
357 
START_TEST(test_is_valid_uri)358 START_TEST(test_is_valid_uri)
359 {
360 	/* is_valid_uri is superseeded by get_uri_type */
361 	ck_assert_int_eq(2, get_uri_type("/api"));
362 	ck_assert_int_eq(2, get_uri_type("/api/"));
363 	ck_assert_int_eq(2,
364 	                 get_uri_type("/some/long/path%20with%20space/file.xyz"));
365 	ck_assert_int_eq(0, get_uri_type("api"));
366 	ck_assert_int_eq(1, get_uri_type("*"));
367 	ck_assert_int_eq(0, get_uri_type("*xy"));
368 	ck_assert_int_eq(3, get_uri_type("http://somewhere/"));
369 	ck_assert_int_eq(3, get_uri_type("https://somewhere/some/file.html"));
370 	ck_assert_int_eq(4, get_uri_type("http://somewhere:8080/"));
371 	ck_assert_int_eq(4, get_uri_type("https://somewhere:8080/some/file.html"));
372 }
373 END_TEST
374 
375 
START_TEST(test_next_option)376 START_TEST(test_next_option)
377 {
378 	/* Adapted from unit_test.c */
379 	/* Copyright (c) 2013-2015 the Civetweb developers */
380 	/* Copyright (c) 2004-2013 Sergey Lyubka */
381 	const char *p, *list = "x/8,/y**=1;2k,z";
382 	struct vec a, b;
383 	int i;
384 
385 	mark_point();
386 
387 	ck_assert(next_option(NULL, &a, &b) == NULL);
388 	for (i = 0, p = list; (p = next_option(p, &a, &b)) != NULL; i++) {
389 		ck_assert(i != 0 || (a.ptr == list && a.len == 3 && b.len == 0));
390 		ck_assert(i != 1
391 		          || (a.ptr == list + 4 && a.len == 4 && b.ptr == list + 9
392 		              && b.len == 4));
393 		ck_assert(i != 2 || (a.ptr == list + 14 && a.len == 1 && b.len == 0));
394 	}
395 }
396 END_TEST
397 
398 
START_TEST(test_skip_quoted)399 START_TEST(test_skip_quoted)
400 {
401 	/* Adapted from unit_test.c */
402 	/* Copyright (c) 2013-2015 the Civetweb developers */
403 	/* Copyright (c) 2004-2013 Sergey Lyubka */
404 	char x[] = "a=1, b=2, c='hi \' there', d='here\\, there'", *s = x, *p;
405 
406 	mark_point();
407 
408 	p = skip_quoted(&s, ", ", ", ", 0);
409 	ck_assert(p != NULL && !strcmp(p, "a=1"));
410 
411 	p = skip_quoted(&s, ", ", ", ", 0);
412 	ck_assert(p != NULL && !strcmp(p, "b=2"));
413 
414 	p = skip_quoted(&s, ",", " ", 0);
415 	ck_assert(p != NULL && !strcmp(p, "c='hi \' there'"));
416 
417 	p = skip_quoted(&s, ",", " ", '\\');
418 	ck_assert(p != NULL && !strcmp(p, "d='here, there'"));
419 	ck_assert(*s == 0);
420 }
421 END_TEST
422 
423 
424 static int
alloc_printf(char ** buf,size_t size,const char * fmt,...)425 alloc_printf(char **buf, size_t size, const char *fmt, ...)
426 {
427 	/* Test helper function - adapted from unit_test.c */
428 	/* Copyright (c) 2013-2015 the Civetweb developers */
429 	/* Copyright (c) 2004-2013 Sergey Lyubka */
430 	va_list ap;
431 	int ret = 0;
432 
433 	mark_point();
434 
435 	va_start(ap, fmt);
436 	ret = alloc_vprintf(buf, *buf, size, fmt, ap);
437 	va_end(ap);
438 
439 	return ret;
440 }
441 
442 
443 static int
alloc_printf2(char ** buf,const char * fmt,...)444 alloc_printf2(char **buf, const char *fmt, ...)
445 {
446 	/* Test alternative implementation */
447 	va_list ap;
448 	int ret = 0;
449 
450 	mark_point();
451 
452 	va_start(ap, fmt);
453 	ret = alloc_vprintf2(buf, fmt, ap);
454 	va_end(ap);
455 
456 	return ret;
457 }
458 
459 
START_TEST(test_alloc_vprintf)460 START_TEST(test_alloc_vprintf)
461 {
462 	/* Adapted from unit_test.c */
463 	/* Copyright (c) 2013-2015 the Civetweb developers */
464 	/* Copyright (c) 2004-2013 Sergey Lyubka */
465 	char buf[MG_BUF_LEN], *p = buf;
466 	mark_point();
467 
468 	ck_assert(alloc_printf(&p, sizeof(buf), "%s", "hi") == 2);
469 	ck_assert(p == buf);
470 
471 	ck_assert(alloc_printf(&p, sizeof(buf), "%s", "") == 0);
472 	ck_assert(p == buf);
473 
474 	ck_assert(alloc_printf(&p, sizeof(buf), "") == 0);
475 	ck_assert(p == buf);
476 
477 	/* Pass small buffer, make sure alloc_printf allocates */
478 	ck_assert(alloc_printf(&p, 1, "%s", "hello") == 5);
479 	ck_assert(p != buf);
480 	mg_free(p);
481 	p = buf;
482 
483 	/* Test alternative implementation */
484 	ck_assert(alloc_printf2(&p, "%s", "hello") == 5);
485 	ck_assert(p != buf);
486 	mg_free(p);
487 	p = buf;
488 }
489 END_TEST
490 
491 
START_TEST(test_mg_vsnprintf)492 START_TEST(test_mg_vsnprintf)
493 {
494 	char buf[16];
495 	int is_trunc;
496 
497 #if defined(_WIN32)
498 	/* If the string is truncated, mg_snprintf calls mg_cry.
499 	 * If DEBUG is defined, mg_cry calls DEBUG_TRACE.
500 	 * In DEBUG_TRACE_FUNC, flockfile(stdout) is called.
501 	 * For Windows, flockfile/funlockfile calls
502 	 * pthread_mutex_lock/_unlock(&global_log_file_lock).
503 	 * So, we need to initialize global_log_file_lock:
504 	 */
505 	pthread_mutex_init(&global_log_file_lock, &pthread_mutex_attr);
506 #endif
507 
508 	memset(buf, 0, sizeof(buf));
509 	mark_point();
510 
511 	is_trunc = 777;
512 	mg_snprintf(NULL, &is_trunc, buf, 10, "%8i", 123);
513 	ck_assert_str_eq(buf, "     123");
514 	ck_assert_int_eq(is_trunc, 0);
515 
516 	is_trunc = 777;
517 	mg_snprintf(NULL, &is_trunc, buf, 10, "%9i", 123);
518 	ck_assert_str_eq(buf, "      123");
519 	ck_assert_int_eq(is_trunc, 0);
520 
521 	is_trunc = 777;
522 	mg_snprintf(NULL, &is_trunc, buf, 9, "%9i", 123);
523 	ck_assert_str_eq(buf, "      12");
524 	ck_assert_int_eq(is_trunc, 1);
525 
526 	is_trunc = 777;
527 	mg_snprintf(NULL, &is_trunc, buf, 8, "%9i", 123);
528 	ck_assert_str_eq(buf, "      1");
529 	ck_assert_int_eq(is_trunc, 1);
530 
531 	is_trunc = 777;
532 	mg_snprintf(NULL, &is_trunc, buf, 7, "%9i", 123);
533 	ck_assert_str_eq(buf, "      ");
534 	ck_assert_int_eq(is_trunc, 1);
535 
536 	is_trunc = 777;
537 	strcpy(buf, "1234567890");
538 	mg_snprintf(NULL, &is_trunc, buf, 0, "%i", 543);
539 	ck_assert_str_eq(buf, "1234567890");
540 	ck_assert_int_eq(is_trunc, 1);
541 }
542 END_TEST
543 
544 
START_TEST(test_mg_strcasestr)545 START_TEST(test_mg_strcasestr)
546 {
547 	/* Adapted from unit_test.c */
548 	/* Copyright (c) 2013-2015 the Civetweb developers */
549 	/* Copyright (c) 2004-2013 Sergey Lyubka */
550 	static const char *big1 = "abcdef";
551 	mark_point();
552 
553 	ck_assert(mg_strcasestr("Y", "X") == NULL);
554 	ck_assert(mg_strcasestr("Y", "y") != NULL);
555 	ck_assert(mg_strcasestr(big1, "X") == NULL);
556 	ck_assert(mg_strcasestr(big1, "CD") == big1 + 2);
557 	ck_assert(mg_strcasestr("aa", "AAB") == NULL);
558 }
559 END_TEST
560 
561 
START_TEST(test_parse_port_string)562 START_TEST(test_parse_port_string)
563 {
564 	/* Adapted from unit_test.c */
565 	/* Copyright (c) 2013-2018 the Civetweb developers */
566 	/* Copyright (c) 2004-2013 Sergey Lyubka */
567 	struct t_test_parse_port_string {
568 		const char *port_string;
569 		int valid;
570 		int ip_family;
571 	};
572 
573 	static struct t_test_parse_port_string testdata[] =
574 	{ {"0", 1, 4},
575 	  {"1", 1, 4},
576 	  {"65535", 1, 4},
577 	  {"65536", 0, 0},
578 
579 	  {"1s", 1, 4},
580 	  {"1r", 1, 4},
581 	  {"1k", 0, 0},
582 
583 	  {"1.2.3", 0, 0},
584 	  {"1.2.3.", 0, 0},
585 	  {"1.2.3.4", 0, 0},
586 	  {"1.2.3.4:", 0, 0},
587 
588 	  {"1.2.3.4:0", 1, 4},
589 	  {"1.2.3.4:1", 1, 4},
590 	  {"1.2.3.4:65535", 1, 4},
591 	  {"1.2.3.4:65536", 0, 0},
592 
593 	  {"1.2.3.4:1s", 1, 4},
594 	  {"1.2.3.4:1r", 1, 4},
595 	  {"1.2.3.4:1k", 0, 0},
596 
597 #if defined(USE_IPV6)
598 	  /* IPv6 config */
599 	  {"[::1]:123", 1, 6},
600 	  {"[::]:80", 1, 6},
601 	  {"[3ffe:2a00:100:7031::1]:900", 1, 6},
602 
603 	  /* IPv4 + IPv6 config */
604 	  {"+80", 1, 4 + 6},
605 #else
606 	  /* IPv6 config: invalid if IPv6 is not activated */
607 	  {"[::1]:123", 0, 0},
608 	  {"[::]:80", 0, 0},
609 	  {"[3ffe:2a00:100:7031::1]:900", 0, 0},
610 
611 	  /* IPv4 + IPv6 config: only IPv4 if IPv6 is not activated */
612 	  {"+80", 1, 4},
613 #endif
614 
615 	  {NULL, 0, 0} };
616 
617 	struct socket so;
618 	struct vec vec;
619 	int ip_family;
620 	int i, ret;
621 
622 	mark_point();
623 
624 	for (i = 0; testdata[i].port_string != NULL; i++) {
625 		vec.ptr = testdata[i].port_string;
626 		vec.len = strlen(vec.ptr);
627 
628 		ip_family = 123;
629 		ret = parse_port_string(&vec, &so, &ip_family);
630 
631 		if ((ret != testdata[i].valid)
632 		    || (ip_family != testdata[i].ip_family)) {
633 			ck_abort_msg("Port string [%s]: "
634 			             "expected valid=%i, family=%i; "
635 			             "got valid=%i, family=%i",
636 			             testdata[i].port_string,
637 			             testdata[i].valid,
638 			             testdata[i].ip_family,
639 			             ret,
640 			             ip_family);
641 		}
642 	}
643 }
644 END_TEST
645 
646 
START_TEST(test_encode_decode)647 START_TEST(test_encode_decode)
648 {
649 	char buf[128];
650 	const char *alpha = "abcdefghijklmnopqrstuvwxyz";
651 	const char *nonalpha = " !\"#$%&'()*+,-./0123456789:;<=>?@";
652 	const char *nonalpha_url_enc1 =
653 	    "%20%21%22%23$%25%26%27()%2a%2b,-.%2f0123456789%3a;%3c%3d%3e%3f%40";
654 	const char *nonalpha_url_enc2 =
655 	    "%20!%22%23%24%25%26'()*%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40";
656 	int ret;
657 	size_t len;
658 
659 #if defined(USE_WEBSOCKET) || defined(USE_LUA)
660 	const char *alpha_b64_enc = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXo=";
661 	const char *nonalpha_b64_enc =
662 	    "ICEiIyQlJicoKSorLC0uLzAxMjM0NTY3ODk6Ozw9Pj9A";
663 
664 	mark_point();
665 
666 	memset(buf, 77, sizeof(buf));
667 	base64_encode((unsigned char *)"a", 1, buf);
668 	ck_assert_str_eq(buf, "YQ==");
669 
670 	memset(buf, 77, sizeof(buf));
671 	base64_encode((unsigned char *)"ab", 1, buf);
672 	ck_assert_str_eq(buf, "YQ==");
673 
674 	memset(buf, 77, sizeof(buf));
675 	base64_encode((unsigned char *)"ab", 2, buf);
676 	ck_assert_str_eq(buf, "YWI=");
677 
678 	memset(buf, 77, sizeof(buf));
679 	base64_encode((unsigned char *)alpha, 3, buf);
680 	ck_assert_str_eq(buf, "YWJj");
681 
682 	memset(buf, 77, sizeof(buf));
683 	base64_encode((unsigned char *)alpha, 4, buf);
684 	ck_assert_str_eq(buf, "YWJjZA==");
685 
686 	memset(buf, 77, sizeof(buf));
687 	base64_encode((unsigned char *)alpha, 5, buf);
688 	ck_assert_str_eq(buf, "YWJjZGU=");
689 
690 	memset(buf, 77, sizeof(buf));
691 	base64_encode((unsigned char *)alpha, 6, buf);
692 	ck_assert_str_eq(buf, "YWJjZGVm");
693 
694 	memset(buf, 77, sizeof(buf));
695 	base64_encode((unsigned char *)alpha, (int)strlen(alpha), buf);
696 	ck_assert_str_eq(buf, alpha_b64_enc);
697 
698 	memset(buf, 77, sizeof(buf));
699 	base64_encode((unsigned char *)nonalpha, (int)strlen(nonalpha), buf);
700 	ck_assert_str_eq(buf, nonalpha_b64_enc);
701 #endif
702 
703 #if defined(USE_LUA)
704 	memset(buf, 77, sizeof(buf));
705 	len = 9999;
706 	ret = base64_decode((unsigned char *)alpha_b64_enc,
707 	                    (int)strlen(alpha_b64_enc),
708 	                    buf,
709 	                    &len);
710 	ck_assert_int_eq(ret, -1);
711 	ck_assert_uint_eq((unsigned int)len, (unsigned int)strlen(alpha));
712 	ck_assert_str_eq(buf, alpha);
713 
714 	memset(buf, 77, sizeof(buf));
715 	len = 9999;
716 	ret = base64_decode((unsigned char *)"AAA*AAA", 7, buf, &len);
717 	ck_assert_int_eq(ret, 3);
718 #endif
719 
720 	memset(buf, 77, sizeof(buf));
721 	ret = mg_url_encode(alpha, buf, sizeof(buf));
722 	ck_assert_int_eq(ret, (int)strlen(buf));
723 	ck_assert_int_eq(ret, (int)strlen(alpha));
724 	ck_assert_str_eq(buf, alpha);
725 
726 	memset(buf, 77, sizeof(buf));
727 	ret = mg_url_encode(nonalpha, buf, sizeof(buf));
728 	ck_assert_int_eq(ret, (int)strlen(buf));
729 	ck_assert_int_eq(ret, (int)strlen(nonalpha_url_enc1));
730 	ck_assert_str_eq(buf, nonalpha_url_enc1);
731 
732 	memset(buf, 77, sizeof(buf));
733 	ret = mg_url_decode(alpha, (int)strlen(alpha), buf, sizeof(buf), 0);
734 	ck_assert_int_eq(ret, (int)strlen(buf));
735 	ck_assert_int_eq(ret, (int)strlen(alpha));
736 	ck_assert_str_eq(buf, alpha);
737 
738 	memset(buf, 77, sizeof(buf));
739 	ret = mg_url_decode(
740 	    nonalpha_url_enc1, (int)strlen(nonalpha_url_enc1), buf, sizeof(buf), 0);
741 	ck_assert_int_eq(ret, (int)strlen(buf));
742 	ck_assert_int_eq(ret, (int)strlen(nonalpha));
743 	ck_assert_str_eq(buf, nonalpha);
744 
745 	memset(buf, 77, sizeof(buf));
746 	ret = mg_url_decode(
747 	    nonalpha_url_enc2, (int)strlen(nonalpha_url_enc2), buf, sizeof(buf), 0);
748 	ck_assert_int_eq(ret, (int)strlen(buf));
749 	ck_assert_int_eq(ret, (int)strlen(nonalpha));
750 	ck_assert_str_eq(buf, nonalpha);
751 
752 	/* len could be unused, if base64_decode is not tested because USE_LUA is
753 	 * not defined */
754 	(void)len;
755 }
756 END_TEST
757 
758 
START_TEST(test_mask_data)759 START_TEST(test_mask_data)
760 {
761 #if defined(USE_WEBSOCKET)
762 	char in[1024];
763 	char out[1024];
764 	int i;
765 #endif
766 
767 	uint32_t mask = 0x61626364;
768 	/* TODO: adapt test for big endian */
769 	ck_assert((*(unsigned char *)&mask) == 0x64u);
770 
771 #if defined(USE_WEBSOCKET)
772 	memset(in, 0, sizeof(in));
773 	memset(out, 99, sizeof(out));
774 
775 	mask_data(in, sizeof(out), 0, out);
776 	ck_assert(!memcmp(out, in, sizeof(out)));
777 
778 	for (i = 0; i < 1024; i++) {
779 		in[i] = (char)((unsigned char)i);
780 	}
781 	mask_data(in, 107, 0, out);
782 	ck_assert(!memcmp(out, in, 107));
783 
784 	mask_data(in, 256, 0x01010101, out);
785 	for (i = 0; i < 256; i++) {
786 		ck_assert_int_eq((int)((unsigned char)out[i]),
787 		                 (int)(((unsigned char)in[i]) ^ (char)1u));
788 	}
789 	for (i = 256; i < (int)sizeof(out); i++) {
790 		ck_assert_int_eq((int)((unsigned char)out[i]), (int)0);
791 	}
792 
793 	/* TODO: check this for big endian */
794 	mask_data(in, 5, 0x01020304, out);
795 	ck_assert_uint_eq((unsigned char)out[0], 0u ^ 4u);
796 	ck_assert_uint_eq((unsigned char)out[1], 1u ^ 3u);
797 	ck_assert_uint_eq((unsigned char)out[2], 2u ^ 2u);
798 	ck_assert_uint_eq((unsigned char)out[3], 3u ^ 1u);
799 	ck_assert_uint_eq((unsigned char)out[4], 4u ^ 4u);
800 #endif
801 }
802 END_TEST
803 
804 
START_TEST(test_parse_date_string)805 START_TEST(test_parse_date_string)
806 {
807 #if !defined(NO_CACHING)
808 	time_t now = time(0);
809 	struct tm *tm = gmtime(&now);
810 	char date[64] = {0};
811 	unsigned long i;
812 
813 	ck_assert_uint_eq((unsigned long)parse_date_string("1/Jan/1970 00:01:02"),
814 	                  62ul);
815 	ck_assert_uint_eq((unsigned long)parse_date_string("1 Jan 1970 00:02:03"),
816 	                  123ul);
817 	ck_assert_uint_eq((unsigned long)parse_date_string("1-Jan-1970 00:03:04"),
818 	                  184ul);
819 	ck_assert_uint_eq((unsigned long)parse_date_string(
820 	                      "Xyz, 1 Jan 1970 00:04:05"),
821 	                  245ul);
822 
823 	gmt_time_string(date, sizeof(date), &now);
824 	ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
825 
826 	sprintf(date,
827 	        "%02u %s %04u %02u:%02u:%02u",
828 	        (unsigned int)tm->tm_mday,
829 	        month_names[tm->tm_mon],
830 	        (unsigned int)(tm->tm_year + 1900),
831 	        (unsigned int)tm->tm_hour,
832 	        (unsigned int)tm->tm_min,
833 	        (unsigned int)tm->tm_sec);
834 	ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
835 
836 	gmt_time_string(date, 1, NULL);
837 	ck_assert_str_eq(date, "");
838 	gmt_time_string(date, 6, NULL);
839 	ck_assert_str_eq(date,
840 	                 "Thu, "); /* part of "Thu, 01 Jan 1970 00:00:00 GMT" */
841 	gmt_time_string(date, sizeof(date), NULL);
842 	ck_assert_str_eq(date, "Thu, 01 Jan 1970 00:00:00 GMT");
843 
844 	for (i = 2ul; i < 0x8000000ul; i += i / 2) {
845 		now = (time_t)i;
846 
847 		gmt_time_string(date, sizeof(date), &now);
848 		ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
849 
850 		tm = gmtime(&now);
851 		sprintf(date,
852 		        "%02u-%s-%04u %02u:%02u:%02u",
853 		        (unsigned int)tm->tm_mday,
854 		        month_names[tm->tm_mon],
855 		        (unsigned int)(tm->tm_year + 1900),
856 		        (unsigned int)tm->tm_hour,
857 		        (unsigned int)tm->tm_min,
858 		        (unsigned int)tm->tm_sec);
859 		ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
860 	}
861 #endif
862 }
863 END_TEST
864 
865 
START_TEST(test_sha1)866 START_TEST(test_sha1)
867 {
868 #ifdef SHA1_DIGEST_SIZE
869 	SHA_CTX sha_ctx;
870 	uint8_t digest[SHA1_DIGEST_SIZE] = {0};
871 	char str[48] = {0};
872 	int i;
873 	const char *test_str;
874 
875 	ck_assert_uint_eq(sizeof(digest), 20);
876 	ck_assert_uint_gt(sizeof(str), sizeof(digest) * 2 + 1);
877 
878 	/* empty string */
879 	SHA1_Init(&sha_ctx);
880 	SHA1_Final(digest, &sha_ctx);
881 	bin2str(str, digest, sizeof(digest));
882 	ck_assert_uint_eq(strlen(str), 40);
883 	ck_assert_str_eq(str, "da39a3ee5e6b4b0d3255bfef95601890afd80709");
884 
885 	/* empty string */
886 	SHA1_Init(&sha_ctx);
887 	SHA1_Update(&sha_ctx, (uint8_t *)"abc", 0);
888 	SHA1_Final(digest, &sha_ctx);
889 	bin2str(str, digest, sizeof(digest));
890 	ck_assert_uint_eq(strlen(str), 40);
891 	ck_assert_str_eq(str, "da39a3ee5e6b4b0d3255bfef95601890afd80709");
892 
893 	/* "abc" */
894 	SHA1_Init(&sha_ctx);
895 	SHA1_Update(&sha_ctx, (uint8_t *)"abc", 3);
896 	SHA1_Final(digest, &sha_ctx);
897 	bin2str(str, digest, sizeof(digest));
898 	ck_assert_uint_eq(strlen(str), 40);
899 	ck_assert_str_eq(str, "a9993e364706816aba3e25717850c26c9cd0d89d");
900 
901 	/* "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" */
902 	test_str = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
903 	SHA1_Init(&sha_ctx);
904 	SHA1_Update(&sha_ctx, (uint8_t *)test_str, (uint32_t)strlen(test_str));
905 	SHA1_Final(digest, &sha_ctx);
906 	bin2str(str, digest, sizeof(digest));
907 	ck_assert_uint_eq(strlen(str), 40);
908 	ck_assert_str_eq(str, "84983e441c3bd26ebaae4aa1f95129e5e54670f1");
909 
910 	/* a million "a" */
911 	SHA1_Init(&sha_ctx);
912 	for (i = 0; i < 1000000; i++) {
913 		SHA1_Update(&sha_ctx, (uint8_t *)"a", 1);
914 	}
915 	SHA1_Final(digest, &sha_ctx);
916 	bin2str(str, digest, sizeof(digest));
917 	ck_assert_uint_eq(strlen(str), 40);
918 	ck_assert_str_eq(str, "34aa973cd4c4daa4f61eeb2bdbad27316534016f");
919 
920 	/* a million "a" in blocks of 10 */
921 	SHA1_Init(&sha_ctx);
922 	for (i = 0; i < 100000; i++) {
923 		SHA1_Update(&sha_ctx, (uint8_t *)"aaaaaaaaaa", 10);
924 	}
925 	SHA1_Final(digest, &sha_ctx);
926 	bin2str(str, digest, sizeof(digest));
927 	ck_assert_uint_eq(strlen(str), 40);
928 	ck_assert_str_eq(str, "34aa973cd4c4daa4f61eeb2bdbad27316534016f");
929 #else
930 	/* Can not test, if SHA1 is not included */
931 	ck_assert(1);
932 #endif
933 }
934 END_TEST
935 
936 
START_TEST(test_config_options)937 START_TEST(test_config_options)
938 {
939 	/* Check size of config_options vs. number of options in enum. */
940 	ck_assert_ptr_eq(NULL, config_options[NUM_OPTIONS].name);
941 	ck_assert_int_eq((int)MG_CONFIG_TYPE_UNKNOWN,
942 	                 config_options[NUM_OPTIONS].type);
943 	ck_assert_uint_eq(sizeof(config_options) / sizeof(config_options[0]),
944 	                  (size_t)(NUM_OPTIONS + 1));
945 
946 	/* Check option enums vs. option names. */
947 	/* Check if the order in
948 	 * static struct mg_option config_options[]
949 	 * is the same as in the option enum
950 	 * This test allows to reorder config_options and the enum,
951 	 * and check if the order is still consistent. */
952 	ck_assert_str_eq("cgi_pattern", config_options[CGI_EXTENSIONS].name);
953 	ck_assert_str_eq("cgi_environment", config_options[CGI_ENVIRONMENT].name);
954 	ck_assert_str_eq("put_delete_auth_file",
955 	                 config_options[PUT_DELETE_PASSWORDS_FILE].name);
956 	ck_assert_str_eq("cgi_interpreter", config_options[CGI_INTERPRETER].name);
957 	ck_assert_str_eq("protect_uri", config_options[PROTECT_URI].name);
958 	ck_assert_str_eq("authentication_domain",
959 	                 config_options[AUTHENTICATION_DOMAIN].name);
960 	ck_assert_str_eq("enable_auth_domain_check",
961 	                 config_options[ENABLE_AUTH_DOMAIN_CHECK].name);
962 	ck_assert_str_eq("ssi_pattern", config_options[SSI_EXTENSIONS].name);
963 	ck_assert_str_eq("throttle", config_options[THROTTLE].name);
964 	ck_assert_str_eq("access_log_file", config_options[ACCESS_LOG_FILE].name);
965 	ck_assert_str_eq("enable_directory_listing",
966 	                 config_options[ENABLE_DIRECTORY_LISTING].name);
967 	ck_assert_str_eq("error_log_file", config_options[ERROR_LOG_FILE].name);
968 	ck_assert_str_eq("global_auth_file",
969 	                 config_options[GLOBAL_PASSWORDS_FILE].name);
970 	ck_assert_str_eq("index_files", config_options[INDEX_FILES].name);
971 	ck_assert_str_eq("enable_keep_alive",
972 	                 config_options[ENABLE_KEEP_ALIVE].name);
973 	ck_assert_str_eq("access_control_list",
974 	                 config_options[ACCESS_CONTROL_LIST].name);
975 	ck_assert_str_eq("extra_mime_types", config_options[EXTRA_MIME_TYPES].name);
976 	ck_assert_str_eq("listening_ports", config_options[LISTENING_PORTS].name);
977 	ck_assert_str_eq("document_root", config_options[DOCUMENT_ROOT].name);
978 	ck_assert_str_eq("ssl_certificate", config_options[SSL_CERTIFICATE].name);
979 	ck_assert_str_eq("ssl_certificate_chain",
980 	                 config_options[SSL_CERTIFICATE_CHAIN].name);
981 	ck_assert_str_eq("num_threads", config_options[NUM_THREADS].name);
982 	ck_assert_str_eq("run_as_user", config_options[RUN_AS_USER].name);
983 	ck_assert_str_eq("url_rewrite_patterns",
984 	                 config_options[URL_REWRITE_PATTERN].name);
985 	ck_assert_str_eq("hide_files_patterns", config_options[HIDE_FILES].name);
986 	ck_assert_str_eq("request_timeout_ms",
987 	                 config_options[REQUEST_TIMEOUT].name);
988 	ck_assert_str_eq("keep_alive_timeout_ms",
989 	                 config_options[KEEP_ALIVE_TIMEOUT].name);
990 	ck_assert_str_eq("linger_timeout_ms", config_options[LINGER_TIMEOUT].name);
991 	ck_assert_str_eq("max_connections", config_options[MAX_CONNECTIONS].name);
992 	ck_assert_str_eq("ssl_verify_peer",
993 	                 config_options[SSL_DO_VERIFY_PEER].name);
994 	ck_assert_str_eq("ssl_ca_path", config_options[SSL_CA_PATH].name);
995 	ck_assert_str_eq("ssl_ca_file", config_options[SSL_CA_FILE].name);
996 	ck_assert_str_eq("ssl_verify_depth", config_options[SSL_VERIFY_DEPTH].name);
997 	ck_assert_str_eq("ssl_default_verify_paths",
998 	                 config_options[SSL_DEFAULT_VERIFY_PATHS].name);
999 	ck_assert_str_eq("ssl_cipher_list", config_options[SSL_CIPHER_LIST].name);
1000 	ck_assert_str_eq("ssl_protocol_version",
1001 	                 config_options[SSL_PROTOCOL_VERSION].name);
1002 	ck_assert_str_eq("ssl_short_trust", config_options[SSL_SHORT_TRUST].name);
1003 
1004 #if defined(USE_WEBSOCKET)
1005 	ck_assert_str_eq("websocket_timeout_ms",
1006 	                 config_options[WEBSOCKET_TIMEOUT].name);
1007 	ck_assert_str_eq("enable_websocket_ping_pong",
1008 	                 config_options[ENABLE_WEBSOCKET_PING_PONG].name);
1009 #endif
1010 
1011 	ck_assert_str_eq("decode_url", config_options[DECODE_URL].name);
1012 
1013 #if defined(USE_LUA)
1014 	ck_assert_str_eq("lua_preload_file", config_options[LUA_PRELOAD_FILE].name);
1015 	ck_assert_str_eq("lua_script_pattern",
1016 	                 config_options[LUA_SCRIPT_EXTENSIONS].name);
1017 	ck_assert_str_eq("lua_server_page_pattern",
1018 	                 config_options[LUA_SERVER_PAGE_EXTENSIONS].name);
1019 #endif
1020 #if defined(USE_DUKTAPE)
1021 	ck_assert_str_eq("duktape_script_pattern",
1022 	                 config_options[DUKTAPE_SCRIPT_EXTENSIONS].name);
1023 #endif
1024 #if defined(USE_WEBSOCKET)
1025 	ck_assert_str_eq("websocket_root", config_options[WEBSOCKET_ROOT].name);
1026 #endif
1027 #if defined(USE_LUA) && defined(USE_WEBSOCKET)
1028 	ck_assert_str_eq("lua_websocket_pattern",
1029 	                 config_options[LUA_WEBSOCKET_EXTENSIONS].name);
1030 #endif
1031 
1032 	ck_assert_str_eq("access_control_allow_origin",
1033 	                 config_options[ACCESS_CONTROL_ALLOW_ORIGIN].name);
1034 	ck_assert_str_eq("access_control_allow_methods",
1035 	                 config_options[ACCESS_CONTROL_ALLOW_METHODS].name);
1036 	ck_assert_str_eq("access_control_allow_headers",
1037 	                 config_options[ACCESS_CONTROL_ALLOW_HEADERS].name);
1038 	ck_assert_str_eq("error_pages", config_options[ERROR_PAGES].name);
1039 	ck_assert_str_eq("tcp_nodelay", config_options[CONFIG_TCP_NODELAY].name);
1040 
1041 
1042 #if !defined(NO_CACHING)
1043 	ck_assert_str_eq("static_file_max_age",
1044 	                 config_options[STATIC_FILE_MAX_AGE].name);
1045 #endif
1046 #if !defined(NO_SSL)
1047 	ck_assert_str_eq("strict_transport_security_max_age",
1048 	                 config_options[STRICT_HTTPS_MAX_AGE].name);
1049 #endif
1050 #if defined(__linux__)
1051 	ck_assert_str_eq("allow_sendfile_call",
1052 	                 config_options[ALLOW_SENDFILE_CALL].name);
1053 #endif
1054 #if defined(_WIN32)
1055 	ck_assert_str_eq("case_sensitive",
1056 	                 config_options[CASE_SENSITIVE_FILES].name);
1057 #endif
1058 #if defined(USE_LUA)
1059 	ck_assert_str_eq("lua_background_script",
1060 	                 config_options[LUA_BACKGROUND_SCRIPT].name);
1061 	ck_assert_str_eq("lua_background_script_params",
1062 	                 config_options[LUA_BACKGROUND_SCRIPT_PARAMS].name);
1063 #endif
1064 
1065 	ck_assert_str_eq("additional_header",
1066 	                 config_options[ADDITIONAL_HEADER].name);
1067 	ck_assert_str_eq("max_request_size", config_options[MAX_REQUEST_SIZE].name);
1068 	ck_assert_str_eq("allow_index_script_resource",
1069 	                 config_options[ALLOW_INDEX_SCRIPT_SUB_RES].name);
1070 }
1071 END_TEST
1072 
1073 
1074 #if !defined(REPLACE_CHECK_FOR_LOCAL_DEBUGGING)
1075 Suite *
make_private_suite(void)1076 make_private_suite(void)
1077 {
1078 	Suite *const suite = suite_create("Private");
1079 
1080 	TCase *const tcase_http_message = tcase_create("HTTP Message");
1081 	TCase *const tcase_http_keep_alive = tcase_create("HTTP Keep Alive");
1082 	TCase *const tcase_url_parsing_1 = tcase_create("URL Parsing 1");
1083 	TCase *const tcase_url_parsing_2 = tcase_create("URL Parsing 2");
1084 	TCase *const tcase_url_parsing_3 = tcase_create("URL Parsing 3");
1085 	TCase *const tcase_internal_parse_1 = tcase_create("Internal Parsing 1");
1086 	TCase *const tcase_internal_parse_2 = tcase_create("Internal Parsing 2");
1087 	TCase *const tcase_internal_parse_3 = tcase_create("Internal Parsing 3");
1088 	TCase *const tcase_internal_parse_4 = tcase_create("Internal Parsing 4");
1089 	TCase *const tcase_internal_parse_5 = tcase_create("Internal Parsing 5");
1090 	TCase *const tcase_internal_parse_6 = tcase_create("Internal Parsing 6");
1091 	TCase *const tcase_encode_decode = tcase_create("Encode Decode");
1092 	TCase *const tcase_mask_data = tcase_create("Mask Data");
1093 	TCase *const tcase_parse_date_string = tcase_create("Date Parsing");
1094 	TCase *const tcase_sha1 = tcase_create("SHA1");
1095 	TCase *const tcase_config_options = tcase_create("Config Options");
1096 
1097 	tcase_add_test(tcase_http_message, test_parse_http_message);
1098 	tcase_set_timeout(tcase_http_message, civetweb_min_test_timeout);
1099 	suite_add_tcase(suite, tcase_http_message);
1100 
1101 	tcase_add_test(tcase_http_keep_alive, test_should_keep_alive);
1102 	tcase_set_timeout(tcase_http_keep_alive, civetweb_min_test_timeout);
1103 	suite_add_tcase(suite, tcase_http_keep_alive);
1104 
1105 	tcase_add_test(tcase_url_parsing_1, test_match_prefix);
1106 	tcase_set_timeout(tcase_url_parsing_1, civetweb_min_test_timeout);
1107 	suite_add_tcase(suite, tcase_url_parsing_1);
1108 
1109 	tcase_add_test(tcase_url_parsing_2,
1110 	               test_remove_double_dots_and_double_slashes);
1111 	tcase_set_timeout(tcase_url_parsing_2, civetweb_min_test_timeout);
1112 	suite_add_tcase(suite, tcase_url_parsing_2);
1113 
1114 	tcase_add_test(tcase_url_parsing_3, test_is_valid_uri);
1115 	tcase_set_timeout(tcase_url_parsing_3, civetweb_min_test_timeout);
1116 	suite_add_tcase(suite, tcase_url_parsing_3);
1117 
1118 	tcase_add_test(tcase_internal_parse_1, test_next_option);
1119 	tcase_set_timeout(tcase_internal_parse_1, civetweb_min_test_timeout);
1120 	suite_add_tcase(suite, tcase_internal_parse_1);
1121 
1122 	tcase_add_test(tcase_internal_parse_2, test_skip_quoted);
1123 	tcase_set_timeout(tcase_internal_parse_2, civetweb_min_test_timeout);
1124 	suite_add_tcase(suite, tcase_internal_parse_2);
1125 
1126 	tcase_add_test(tcase_internal_parse_3, test_mg_strcasestr);
1127 	tcase_set_timeout(tcase_internal_parse_3, civetweb_min_test_timeout);
1128 	suite_add_tcase(suite, tcase_internal_parse_3);
1129 
1130 	tcase_add_test(tcase_internal_parse_4, test_alloc_vprintf);
1131 	tcase_set_timeout(tcase_internal_parse_4, civetweb_min_test_timeout);
1132 	suite_add_tcase(suite, tcase_internal_parse_4);
1133 
1134 	tcase_add_test(tcase_internal_parse_5, test_mg_vsnprintf);
1135 	tcase_set_timeout(tcase_internal_parse_5, civetweb_min_test_timeout);
1136 	suite_add_tcase(suite, tcase_internal_parse_5);
1137 
1138 	tcase_add_test(tcase_internal_parse_6, test_parse_port_string);
1139 	tcase_set_timeout(tcase_internal_parse_6, civetweb_min_test_timeout);
1140 	suite_add_tcase(suite, tcase_internal_parse_6);
1141 
1142 	tcase_add_test(tcase_encode_decode, test_encode_decode);
1143 	tcase_set_timeout(tcase_encode_decode, civetweb_min_test_timeout);
1144 	suite_add_tcase(suite, tcase_encode_decode);
1145 
1146 	tcase_add_test(tcase_mask_data, test_mask_data);
1147 	tcase_set_timeout(tcase_mask_data, civetweb_min_test_timeout);
1148 	suite_add_tcase(suite, tcase_mask_data);
1149 
1150 	tcase_add_test(tcase_parse_date_string, test_parse_date_string);
1151 	tcase_set_timeout(tcase_parse_date_string, civetweb_min_test_timeout);
1152 	suite_add_tcase(suite, tcase_parse_date_string);
1153 
1154 	tcase_add_test(tcase_sha1, test_sha1);
1155 	tcase_set_timeout(tcase_sha1, civetweb_min_test_timeout);
1156 	suite_add_tcase(suite, tcase_sha1);
1157 
1158 	tcase_add_test(tcase_config_options, test_config_options);
1159 	tcase_set_timeout(tcase_config_options, civetweb_min_test_timeout);
1160 	suite_add_tcase(suite, tcase_config_options);
1161 
1162 	return suite;
1163 }
1164 #endif
1165 
1166 
1167 #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
1168 /* Used to debug test cases without using the check framework */
1169 
1170 void
MAIN_PRIVATE(void)1171 MAIN_PRIVATE(void)
1172 {
1173 #if defined(_WIN32)
1174 	/* test_parse_port_string requires WSAStartup for IPv6 */
1175 	WSADATA data;
1176 	WSAStartup(MAKEWORD(2, 2), &data);
1177 #endif
1178 
1179 	test_alloc_vprintf(0);
1180 	test_mg_vsnprintf(0);
1181 	test_remove_double_dots_and_double_slashes(0);
1182 	test_parse_date_string(0);
1183 	test_parse_port_string(0);
1184 	test_parse_http_message(0);
1185 	test_sha1(0);
1186 
1187 #if defined(_WIN32)
1188 	WSACleanup();
1189 #endif
1190 }
1191 
1192 #endif
1193