private.c 38 KB

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