private.c 39 KB

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