private.c 49 KB

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