private.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788
  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_match_prefix_strlen)
  261. {
  262. /* Copyright (c) 2022 the CivetWeb developers */
  263. ck_assert_int_eq(5, match_prefix_strlen("/Test", "/test"));
  264. ck_assert_int_eq(-1, match_prefix_strlen("/Test", "/my/test"));
  265. ck_assert_int_eq(3, match_prefix_strlen("/my", "/my/test"));
  266. ck_assert_int_eq(-1, match_prefix_strlen("/my$", "/my/test"));
  267. ck_assert_int_eq(8, match_prefix_strlen("/*/Test", "/my/test"));
  268. ck_assert_int_eq(17,
  269. match_prefix_strlen("/api/*/*.cgi", "/api/obj/prop.cgi"));
  270. ck_assert_int_eq(-1,
  271. match_prefix_strlen("/abc/*/*.cgi", "/api/obj/prop.cgi"));
  272. ck_assert_int_eq(18,
  273. match_prefix_strlen("/api/*/*.cgi", "/api/obj/other.cgi"));
  274. ck_assert_int_eq(-1,
  275. match_prefix_strlen("/api/*/*.cgi",
  276. "/api/obj/too/deep.cgi"));
  277. ck_assert_int_eq(17,
  278. match_prefix_strlen("/api/*/*.cgi$", "/api/obj/prop.cgi"));
  279. ck_assert_int_eq(18,
  280. match_prefix_strlen("/api/*/*.cgi$",
  281. "/api/obj/other.cgi"));
  282. ck_assert_int_eq(17,
  283. match_prefix_strlen("/api/*/*.cgi",
  284. "/api/obj/prop.cgiZZZ"));
  285. ck_assert_int_eq(-1,
  286. match_prefix_strlen("/api/*/*.cgi$",
  287. "/api/obj/prop.cgiZZZ"));
  288. ck_assert_int_eq(-1, match_prefix_strlen("/*/*.cgi", "/api/obj/prop.cgi"));
  289. ck_assert_int_eq(7, match_prefix_strlen("I????IT", "ItestIT"));
  290. ck_assert_int_eq(-1, match_prefix_strlen("I????IT", "IseeIT"));
  291. ck_assert_int_eq(23, match_prefix_strlen("**$", "EveryThing/matches this"));
  292. ck_assert_int_eq(23,
  293. match_prefix_strlen("?**$", "EveryThing/matches this"));
  294. ck_assert_int_eq(23,
  295. match_prefix_strlen("**?$", "EveryThing/matches this"));
  296. ck_assert_int_eq(0, match_prefix_strlen("**$", ""));
  297. ck_assert_int_eq(-1, match_prefix_strlen("?**$", ""));
  298. ck_assert_int_eq(-1, match_prefix_strlen("**?$", ""));
  299. ck_assert_int_eq(-1, match_prefix_strlen("/**?$", "/"));
  300. ck_assert_int_eq(1, match_prefix_strlen("/**$", "/"));
  301. ck_assert_int_eq(-1, match_prefix_strlen("//", "/"));
  302. ck_assert_int_eq(1, match_prefix_strlen("/", "//"));
  303. ck_assert_int_eq(-1, match_prefix_strlen("/$", "//"));
  304. /* ? pattern should not match / character */
  305. ck_assert_int_eq(-1, match_prefix_strlen("/?", "//"));
  306. ck_assert_int_eq(3, match_prefix_strlen("/?/$", "/a/"));
  307. ck_assert_int_eq(-1, match_prefix_strlen("/?/$", "///"));
  308. /* Pattern From UserManual.md */
  309. ck_assert_int_eq(20,
  310. match_prefix_strlen("**.cgi$", "anywhere/anyname.cgi"));
  311. ck_assert_int_eq(-1, match_prefix_strlen("**.cgi$", "name.cgi.not.at.end"));
  312. ck_assert_int_eq(4, match_prefix_strlen("/foo", "/foo"));
  313. ck_assert_int_eq(4, match_prefix_strlen("/foo", "/foobar"));
  314. ck_assert_int_eq(-1, match_prefix_strlen("/foo", "not.at.start./foo"));
  315. ck_assert_int_eq(1, match_prefix_strlen("**a$|**b$", "a"));
  316. ck_assert_int_eq(2, match_prefix_strlen("**a$|**b$", "xb"));
  317. ck_assert_int_eq(-1, match_prefix_strlen("**a$|**b$", "abc"));
  318. ck_assert_int_eq(14,
  319. match_prefix_strlen("/data/????.css$", "/data/12.4.css"));
  320. ck_assert_int_eq(-1,
  321. match_prefix_strlen("/data/????.css$", "/data/12/4.css"));
  322. ck_assert_int_eq(-1,
  323. match_prefix_strlen("/data/????.css$", "/data/../4.css"));
  324. ck_assert_int_eq(-1,
  325. match_prefix_strlen("/data/????.css$", "/else/12.4.css"));
  326. ck_assert_int_eq(-1,
  327. match_prefix_strlen("/data/????.css$", "/data/1234.cssx"));
  328. ck_assert_int_eq(13, match_prefix_strlen("/data/*.js$", "/data/1234.js"));
  329. ck_assert_int_eq(17,
  330. match_prefix_strlen("/data/*.js$", "/data/12345678.js"));
  331. ck_assert_int_eq(-1, match_prefix_strlen("/data/*.js$", "/else/1234.js"));
  332. ck_assert_int_eq(-1,
  333. match_prefix_strlen("/data/*.js$", "/data/../some.js"));
  334. ck_assert_int_eq(-1, match_prefix_strlen("/data/*.js$", "/data//x.js"));
  335. ck_assert_int_eq(-1, match_prefix_strlen("/data/*.js$", "/data/./x.js"));
  336. ck_assert_int_eq(34,
  337. match_prefix_strlen("/api/*/*.cgi$",
  338. "/api/resourcetype/resourcename.cgi"));
  339. ck_assert_int_eq(-1,
  340. match_prefix_strlen("/api/*/*.cgi$",
  341. "/api/resourcename.cgi"));
  342. ck_assert_int_eq(-1,
  343. match_prefix_strlen("/*.jpg$|/*.jpeg$",
  344. "/somewhere/something.txt"));
  345. ck_assert_int_eq(-1,
  346. match_prefix_strlen("/*.jpg$|/*.jpeg$", "/something.txt"));
  347. ck_assert_int_eq(10, match_prefix_strlen("/*.jpg$|/*.jpeg$", "/image.jpg"));
  348. ck_assert_int_eq(11,
  349. match_prefix_strlen("/*.jpg$|/*.jpeg$", "/image.jpeg"));
  350. ck_assert_int_eq(-1,
  351. match_prefix_strlen("/*.jpg$|/*.jpeg$",
  352. "/image.jpeg.exe"));
  353. ck_assert_int_eq(-1,
  354. match_prefix_strlen("/*.jpg$|/*.jpeg$", "/sub/image.jpg"));
  355. ck_assert_int_eq(-1,
  356. match_prefix_strlen("/*.jpg$|/*.jpeg$",
  357. "/sub/image.jpeg"));
  358. ck_assert_int_eq(-1,
  359. match_prefix_strlen("**.jpg$|**.jpeg$",
  360. "/somewhere/something.txt"));
  361. ck_assert_int_eq(-1,
  362. match_prefix_strlen("**.jpg$|**.jpeg$", "/something.txt"));
  363. ck_assert_int_eq(10, match_prefix_strlen("**.jpg$|**.jpeg$", "/image.jpg"));
  364. ck_assert_int_eq(11,
  365. match_prefix_strlen("**.jpg$|**.jpeg$", "/image.jpeg"));
  366. ck_assert_int_eq(-1,
  367. match_prefix_strlen("**.jpg$|**.jpeg$",
  368. "/image.jpeg.exe"));
  369. ck_assert_int_eq(14,
  370. match_prefix_strlen("**.jpg$|**.jpeg$", "/sub/image.jpg"));
  371. ck_assert_int_eq(15,
  372. match_prefix_strlen("**.jpg$|**.jpeg$",
  373. "/sub/image.jpeg"));
  374. ck_assert_int_eq(14,
  375. match_prefix_strlen("/*/*.jpg$|/*/*.jpeg$",
  376. "/sub/image.jpg"));
  377. ck_assert_int_eq(15,
  378. match_prefix_strlen("/*/*.jpg$|/*/*.jpeg$",
  379. "/sub/image.jpeg"));
  380. }
  381. END_TEST
  382. START_TEST(test_match_prefix_fuzz)
  383. {
  384. /* Copyright (c) 2022 the CivetWeb developers */
  385. {
  386. /* From fuzz test */
  387. const char *pat = "**cacc//d/?dad?";
  388. const char *str =
  389. "dbbddb/cb/ddcdbcbbab/dcdcbbbcaaacdbcac/dbdcadaa/bcaca/d/a/adcad";
  390. ck_assert_int_eq(6, match_prefix(pat, 1, str));
  391. ck_assert_int_eq(63, match_prefix(pat, 2, str));
  392. ck_assert_int_eq(61, match_prefix(pat, 3, str));
  393. ck_assert_int_eq(62, match_prefix(pat, 4, str));
  394. ck_assert_int_eq(52, match_prefix(pat, 5, str));
  395. ck_assert_int_eq(-1, match_prefix(pat, 6, str));
  396. }
  397. {
  398. /* From fuzz test */
  399. const char *pat = "a/a*d**/?*/*cdd";
  400. const char *str =
  401. "a/aaddba/ddadbaacac//bcaadbc/badaccbdadadcbb//ccd/dcbacdcddc//c";
  402. ck_assert_int_eq(0, match_prefix(pat, 0, str));
  403. ck_assert_int_eq(1, match_prefix(pat, 1, str));
  404. ck_assert_int_eq(2, match_prefix(pat, 2, str));
  405. ck_assert_int_eq(3, match_prefix(pat, 3, str));
  406. ck_assert_int_eq(8, match_prefix(pat, 4, str));
  407. ck_assert_int_eq(6, match_prefix(pat, 5, str));
  408. ck_assert_int_eq(8, match_prefix(pat, 6, str));
  409. ck_assert_int_eq(63, match_prefix(pat, 7, str));
  410. ck_assert_int_eq(62, match_prefix(pat, 8, str));
  411. ck_assert_int_eq(63, match_prefix(pat, 9, str));
  412. ck_assert_int_eq(63, match_prefix(pat, 10, str));
  413. ck_assert_int_eq(61, match_prefix(pat, 11, str));
  414. ck_assert_int_eq(61, match_prefix(pat, 12, str));
  415. ck_assert_int_eq(60, match_prefix(pat, 13, str));
  416. ck_assert_int_eq(58, match_prefix(pat, 14, str));
  417. ck_assert_int_eq(59, match_prefix(pat, 15, str));
  418. ck_assert_int_eq(59, match_prefix_strlen(pat, str));
  419. }
  420. {
  421. /* From fuzz test */
  422. const char *pat = "cc/?**ba????**b";
  423. const char *str =
  424. "cc/babdb/cbb/baa/da/cd///ccabbcdcdaa/dbacbdbadaccb/dbdcc/cdbbac";
  425. ck_assert_int_eq(0, match_prefix(pat, 0, str));
  426. ck_assert_int_eq(1, match_prefix(pat, 1, str));
  427. ck_assert_int_eq(2, match_prefix(pat, 2, str));
  428. ck_assert_int_eq(3, match_prefix(pat, 3, str));
  429. ck_assert_int_eq(4, match_prefix(pat, 4, str));
  430. ck_assert_int_eq(8, match_prefix(pat, 5, str));
  431. ck_assert_int_eq(63, match_prefix(pat, 6, str));
  432. ck_assert_int_eq(61, match_prefix(pat, 7, str));
  433. ck_assert_int_eq(62, match_prefix(pat, 8, str));
  434. ck_assert_int_eq(63, match_prefix(pat, 9, str));
  435. ck_assert_int_eq(47, match_prefix(pat, 10, str));
  436. ck_assert_int_eq(48, match_prefix(pat, 11, str));
  437. ck_assert_int_eq(49, match_prefix(pat, 12, str));
  438. ck_assert_int_eq(50, match_prefix(pat, 13, str));
  439. ck_assert_int_eq(63, match_prefix(pat, 14, str));
  440. ck_assert_int_eq(61, match_prefix(pat, 15, str));
  441. ck_assert_int_eq(61, match_prefix_strlen(pat, str));
  442. }
  443. {
  444. /* From fuzz test */
  445. const char *pat = "?**ba*db*b?*/a?";
  446. const char *str =
  447. "bd/bcdddabbd//bcb//acbcaaac/dcbbcdadabadba/bd/baadbabcc/a/bcb//";
  448. ck_assert_int_eq(0, match_prefix(pat, 0, str));
  449. ck_assert_int_eq(1, match_prefix(pat, 1, str));
  450. ck_assert_int_eq(2, match_prefix(pat, 2, str));
  451. ck_assert_int_eq(63, match_prefix(pat, 3, str));
  452. ck_assert_int_eq(61, match_prefix(pat, 4, str));
  453. ck_assert_int_eq(52, match_prefix(pat, 5, str));
  454. ck_assert_int_eq(55, match_prefix(pat, 6, str));
  455. ck_assert_int_eq(50, match_prefix(pat, 7, str));
  456. ck_assert_int_eq(51, match_prefix(pat, 8, str));
  457. ck_assert_int_eq(55, match_prefix(pat, 9, str));
  458. ck_assert_int_eq(53, match_prefix(pat, 10, str));
  459. ck_assert_int_eq(54, match_prefix(pat, 11, str));
  460. ck_assert_int_eq(55, match_prefix(pat, 12, str));
  461. ck_assert_int_eq(56, match_prefix(pat, 13, str));
  462. ck_assert_int_eq(57, match_prefix(pat, 14, str));
  463. ck_assert_int_eq(-1, match_prefix(pat, 15, str));
  464. ck_assert_int_eq(-1, match_prefix_strlen(pat, str));
  465. }
  466. {
  467. /* From fuzz test */
  468. const char *pat = "?b*da*bc?c**//*";
  469. const char *str =
  470. "dbadabcbcbbba/a///d//dcdd////daccbcaaa/a/bacddab/bdcbbdd/bbaa/a";
  471. ck_assert_int_eq(0, match_prefix(pat, 0, str));
  472. ck_assert_int_eq(1, match_prefix(pat, 1, str));
  473. ck_assert_int_eq(2, match_prefix(pat, 2, str));
  474. ck_assert_int_eq(13, match_prefix(pat, 3, str));
  475. ck_assert_int_eq(4, match_prefix(pat, 4, str));
  476. ck_assert_int_eq(5, match_prefix(pat, 5, str));
  477. ck_assert_int_eq(13, match_prefix(pat, 6, str));
  478. ck_assert_int_eq(12, match_prefix(pat, 7, str));
  479. ck_assert_int_eq(9, match_prefix(pat, 8, str));
  480. ck_assert_int_eq(10, match_prefix(pat, 9, str));
  481. ck_assert_int_eq(9, match_prefix(pat, 10, str));
  482. ck_assert_int_eq(13, match_prefix(pat, 11, str));
  483. ck_assert_int_eq(63, match_prefix(pat, 12, str));
  484. ck_assert_int_eq(62, match_prefix(pat, 13, str));
  485. ck_assert_int_eq(29, match_prefix(pat, 14, str));
  486. ck_assert_int_eq(38, match_prefix(pat, 15, str));
  487. ck_assert_int_eq(38, match_prefix_strlen(pat, str));
  488. }
  489. {
  490. /* From fuzz test */
  491. const char *pat = "?**c**dc**b*c*a";
  492. const char *str = "cba/a/bbcdcccc/bcacacdadab/dad/";
  493. ck_assert_int_eq(0, match_prefix(pat, 0, str));
  494. ck_assert_int_eq(1, match_prefix(pat, 1, str));
  495. ck_assert_int_eq(3, match_prefix(pat, 2, str));
  496. ck_assert_int_eq(31, match_prefix(pat, 3, str));
  497. ck_assert_int_eq(21, match_prefix(pat, 4, str));
  498. ck_assert_int_eq(26, match_prefix(pat, 5, str));
  499. ck_assert_int_eq(31, match_prefix(pat, 6, str));
  500. ck_assert_int_eq(30, match_prefix(pat, 7, str));
  501. ck_assert_int_eq(11, match_prefix(pat, 8, str));
  502. ck_assert_int_eq(14, match_prefix(pat, 9, str));
  503. ck_assert_int_eq(31, match_prefix(pat, 10, str));
  504. ck_assert_int_eq(26, match_prefix(pat, 11, str));
  505. ck_assert_int_eq(26, match_prefix(pat, 12, str));
  506. ck_assert_int_eq(21, match_prefix(pat, 13, str));
  507. ck_assert_int_eq(26, match_prefix(pat, 14, str));
  508. ck_assert_int_eq(25, match_prefix(pat, 15, str));
  509. ck_assert_int_eq(25, match_prefix_strlen(pat, str));
  510. }
  511. {
  512. /* From fuzz test */
  513. const char *pat = "$|*ca|**c/c*b|a";
  514. const char *str = "bcdaa/a//acdc/bac/caacacdcbcdc/";
  515. ck_assert_int_eq(0, match_prefix(pat, 0, str));
  516. ck_assert_int_eq(-1, match_prefix(pat, 1, str));
  517. ck_assert_int_eq(0, match_prefix(pat, 2, str));
  518. ck_assert_int_eq(5, match_prefix(pat, 3, str));
  519. ck_assert_int_eq(2, match_prefix(pat, 4, str));
  520. ck_assert_int_eq(-1, match_prefix(pat, 5, str));
  521. ck_assert_int_eq(0, match_prefix(pat, 6, str));
  522. ck_assert_int_eq(5, match_prefix(pat, 7, str));
  523. ck_assert_int_eq(31, match_prefix(pat, 8, str));
  524. ck_assert_int_eq(30, match_prefix(pat, 9, str));
  525. ck_assert_int_eq(31, match_prefix(pat, 10, str));
  526. ck_assert_int_eq(19, match_prefix(pat, 11, str));
  527. ck_assert_int_eq(30, match_prefix(pat, 12, str));
  528. ck_assert_int_eq(27, match_prefix(pat, 13, str));
  529. ck_assert_int_eq(27, match_prefix(pat, 14, str));
  530. ck_assert_int_eq(27, match_prefix(pat, 15, str));
  531. ck_assert_int_eq(27, match_prefix_strlen(pat, str));
  532. }
  533. {
  534. /* From fuzz test */
  535. const char *pat = "*b?c|?$|*b?b**?";
  536. const char *str = "cbbbddddda/dbcdbbbccdcb/a//ddab";
  537. ck_assert_int_eq(0, match_prefix(pat, 0, str));
  538. ck_assert_int_eq(10, match_prefix(pat, 1, str));
  539. ck_assert_int_eq(4, match_prefix(pat, 2, str));
  540. ck_assert_int_eq(5, match_prefix(pat, 3, str));
  541. ck_assert_int_eq(-1, match_prefix(pat, 4, str));
  542. ck_assert_int_eq(0, match_prefix(pat, 5, str));
  543. ck_assert_int_eq(1, match_prefix(pat, 6, str));
  544. ck_assert_int_eq(-1, match_prefix(pat, 7, str));
  545. ck_assert_int_eq(0, match_prefix(pat, 8, str));
  546. ck_assert_int_eq(10, match_prefix(pat, 9, str));
  547. ck_assert_int_eq(4, match_prefix(pat, 10, str));
  548. ck_assert_int_eq(5, match_prefix(pat, 11, str));
  549. ck_assert_int_eq(4, match_prefix(pat, 12, str));
  550. ck_assert_int_eq(10, match_prefix(pat, 13, str));
  551. ck_assert_int_eq(31, match_prefix(pat, 14, str));
  552. ck_assert_int_eq(31, match_prefix(pat, 15, str));
  553. ck_assert_int_eq(31, match_prefix_strlen(pat, str));
  554. }
  555. {
  556. /* From fuzz test */
  557. const char *pat = "c|b?*$|*ca*/*ba";
  558. const char *str = "bdccacb/aaadbadd/ccaca/c/cdcb/d";
  559. ck_assert_int_eq(0, match_prefix(pat, 0, str));
  560. ck_assert_int_eq(-1, match_prefix(pat, 1, str));
  561. ck_assert_int_eq(0, match_prefix(pat, 2, str));
  562. ck_assert_int_eq(1, match_prefix(pat, 3, str));
  563. ck_assert_int_eq(2, match_prefix(pat, 4, str));
  564. ck_assert_int_eq(7, match_prefix(pat, 5, str));
  565. ck_assert_int_eq(-1, match_prefix(pat, 6, str));
  566. ck_assert_int_eq(0, match_prefix(pat, 7, str));
  567. ck_assert_int_eq(7, match_prefix(pat, 8, str));
  568. ck_assert_int_eq(6, match_prefix(pat, 9, str));
  569. ck_assert_int_eq(5, match_prefix(pat, 10, str));
  570. ck_assert_int_eq(7, match_prefix(pat, 11, str));
  571. ck_assert_int_eq(8, match_prefix(pat, 12, str));
  572. ck_assert_int_eq(16, match_prefix(pat, 13, str));
  573. ck_assert_int_eq(13, match_prefix(pat, 14, str));
  574. ck_assert_int_eq(14, match_prefix(pat, 15, str));
  575. ck_assert_int_eq(14, match_prefix_strlen(pat, str));
  576. }
  577. }
  578. END_TEST
  579. START_TEST(test_remove_dot_segments)
  580. {
  581. int i;
  582. struct {
  583. const char *input;
  584. const char *expected_output;
  585. } tests[] = {
  586. {"/path/to/file.ext", "/path/to/file.ext"},
  587. {"/file.ext", "/file.ext"},
  588. {"/path/../file.ext", "/file.ext"},
  589. {"/../to/file.ext", "/to/file.ext"},
  590. {"/../../file.ext", "/file.ext"},
  591. {"/./../file.ext", "/file.ext"},
  592. {"/.././file.ext", "/file.ext"},
  593. {"/././file.ext", "/file.ext"},
  594. {"/././file.ext", "/file.ext"},
  595. {"/path/.to/..file.ext", "/path/.to/..file.ext"},
  596. {"/file", "/file"},
  597. {"/path/", "/path/"},
  598. {"file.ext", "file.ext"},
  599. {"./file.ext", "file.ext"},
  600. {"../file.ext", "file.ext"},
  601. {".file.ext", ".file.ext"},
  602. {"..file.ext", "..file.ext"},
  603. {"file", "file"},
  604. {"/x/../", "/"},
  605. {"/x/../../", "/"},
  606. {"/x/.././", "/"},
  607. {"/./x/.././", "/"},
  608. /* Windows specific */
  609. {"\\file.ext", "/file.ext"},
  610. {"\\..\\file.ext", "/file.ext"},
  611. {"/file.", "/file"},
  612. {"/path\\to.\\.\\file.", "/path/to/file"},
  613. /* Multiple dots and slashes */
  614. {"\\//\\\\x", "/x"},
  615. {"//", "/"},
  616. {"/./", "/"},
  617. {"/../", "/"},
  618. {"/.../", "/"},
  619. {"/..../", "/"},
  620. {"/...../", "/"},
  621. {"/...../", "/"},
  622. {"/...//", "/"},
  623. {"/..././", "/"},
  624. {"/.../../", "/"},
  625. {"/.../.../", "/"},
  626. /* Test cases from issues */
  627. {"/foo/bar/baz/../qux.txt", "/foo/bar/qux.txt"},
  628. {"/../alice/bob/../carol/david/frank/../../grace.ext/hugo/../irene.jpg",
  629. "/alice/carol/grace.ext/irene.jpg"},
  630. {"/a/b/..", "/a/"},
  631. {"/a/b/c/../d/../..", "/a/"},
  632. {NULL, NULL}};
  633. mark_point();
  634. for (i = 0; (tests[i].input != NULL); i++) {
  635. char inout[256];
  636. strcpy(inout, tests[i].input);
  637. remove_dot_segments(inout);
  638. ck_assert_str_eq(inout, tests[i].expected_output);
  639. }
  640. }
  641. END_TEST
  642. START_TEST(test_is_valid_uri)
  643. {
  644. /* is_valid_uri is superseeded by get_uri_type */
  645. ck_assert_int_eq(2, get_uri_type("/api"));
  646. ck_assert_int_eq(2, get_uri_type("/api/"));
  647. ck_assert_int_eq(2,
  648. get_uri_type("/some/long/path%20with%20space/file.xyz"));
  649. ck_assert_int_eq(0, get_uri_type("api"));
  650. ck_assert_int_eq(1, get_uri_type("*"));
  651. ck_assert_int_eq(0, get_uri_type("*xy"));
  652. ck_assert_int_eq(3, get_uri_type("http://somewhere/"));
  653. ck_assert_int_eq(3, get_uri_type("https://somewhere/some/file.html"));
  654. ck_assert_int_eq(4, get_uri_type("http://somewhere:8080/"));
  655. ck_assert_int_eq(4, get_uri_type("https://somewhere:8080/some/file.html"));
  656. }
  657. END_TEST
  658. START_TEST(test_next_option)
  659. {
  660. /* Adapted from unit_test.c */
  661. /* Copyright (c) 2013-2015 the Civetweb developers */
  662. /* Copyright (c) 2004-2013 Sergey Lyubka */
  663. const char *p, *list = "x/8,/y**=1;2k,z";
  664. struct vec a, b;
  665. int i;
  666. mark_point();
  667. ck_assert(next_option(NULL, &a, &b) == NULL);
  668. for (i = 0, p = list; (p = next_option(p, &a, &b)) != NULL; i++) {
  669. ck_assert(i != 0 || (a.ptr == list && a.len == 3 && b.len == 0));
  670. ck_assert(i != 1
  671. || (a.ptr == list + 4 && a.len == 4 && b.ptr == list + 9
  672. && b.len == 4));
  673. ck_assert(i != 2 || (a.ptr == list + 14 && a.len == 1 && b.len == 0));
  674. }
  675. }
  676. END_TEST
  677. START_TEST(test_skip_quoted)
  678. {
  679. /* Adapted from unit_test.c */
  680. /* Copyright (c) 2013-2015 the Civetweb developers */
  681. /* Copyright (c) 2004-2013 Sergey Lyubka */
  682. char x[] = "a=1, b=2, c='hi \' there', d='here\\, there'", *s = x, *p;
  683. mark_point();
  684. p = skip_quoted(&s, ", ", ", ", 0);
  685. ck_assert(p != NULL && !strcmp(p, "a=1"));
  686. p = skip_quoted(&s, ", ", ", ", 0);
  687. ck_assert(p != NULL && !strcmp(p, "b=2"));
  688. p = skip_quoted(&s, ",", " ", 0);
  689. ck_assert(p != NULL && !strcmp(p, "c='hi \' there'"));
  690. p = skip_quoted(&s, ",", " ", '\\');
  691. ck_assert(p != NULL && !strcmp(p, "d='here, there'"));
  692. ck_assert(*s == 0);
  693. }
  694. END_TEST
  695. static int
  696. alloc_vprintf_wrapper(char **buf, size_t size, const char *fmt, ...)
  697. {
  698. /* Test helper function - adapted from unit_test.c */
  699. /* Copyright (c) 2013-2015 the Civetweb developers */
  700. /* Copyright (c) 2004-2013 Sergey Lyubka */
  701. va_list ap;
  702. int ret = 0;
  703. mark_point();
  704. va_start(ap, fmt);
  705. ret = alloc_vprintf(buf, *buf, size, fmt, ap);
  706. va_end(ap);
  707. return ret;
  708. }
  709. START_TEST(test_alloc_vprintf)
  710. {
  711. /* Adapted from unit_test.c */
  712. /* Copyright (c) 2013-2015 the Civetweb developers */
  713. /* Copyright (c) 2004-2013 Sergey Lyubka */
  714. char buf[MG_BUF_LEN], *p = buf;
  715. mark_point();
  716. ck_assert(alloc_vprintf_wrapper(&p, sizeof(buf), "%s", "hi") == 2);
  717. ck_assert(p == buf);
  718. ck_assert(alloc_vprintf_wrapper(&p, sizeof(buf), "%s", "") == 0);
  719. ck_assert(p == buf);
  720. ck_assert(alloc_vprintf_wrapper(&p, sizeof(buf), "") == 0);
  721. ck_assert(p == buf);
  722. /* Pass small buffer, make sure alloc_printf allocates */
  723. ck_assert(alloc_vprintf_wrapper(&p, 1, "%s", "hello") == 5);
  724. ck_assert(p != buf);
  725. mg_free(p);
  726. p = buf;
  727. /* Test new wrapper implementation */
  728. ck_assert(alloc_printf(&p, "%s", "hello") == 5);
  729. ck_assert(p != buf);
  730. mg_free(p);
  731. p = buf;
  732. }
  733. END_TEST
  734. START_TEST(test_mg_vsnprintf)
  735. {
  736. char buf[16];
  737. int is_trunc;
  738. #if defined(_WIN32)
  739. /* If the string is truncated, mg_snprintf calls mg_cry.
  740. * If DEBUG is defined, mg_cry calls DEBUG_TRACE.
  741. * In DEBUG_TRACE_FUNC, flockfile(stdout) is called.
  742. * For Windows, flockfile/funlockfile calls
  743. * pthread_mutex_lock/_unlock(&global_log_file_lock).
  744. * So, we need to initialize global_log_file_lock:
  745. */
  746. pthread_mutex_init(&global_log_file_lock, &pthread_mutex_attr);
  747. #endif
  748. memset(buf, 0, sizeof(buf));
  749. mark_point();
  750. is_trunc = 777;
  751. mg_snprintf(NULL, &is_trunc, buf, 10, "%8i", 123);
  752. ck_assert_str_eq(buf, " 123");
  753. ck_assert_int_eq(is_trunc, 0);
  754. is_trunc = 777;
  755. mg_snprintf(NULL, &is_trunc, buf, 10, "%9i", 123);
  756. ck_assert_str_eq(buf, " 123");
  757. ck_assert_int_eq(is_trunc, 0);
  758. is_trunc = 777;
  759. mg_snprintf(NULL, &is_trunc, buf, 9, "%9i", 123);
  760. ck_assert_str_eq(buf, " 12");
  761. ck_assert_int_eq(is_trunc, 1);
  762. is_trunc = 777;
  763. mg_snprintf(NULL, &is_trunc, buf, 8, "%9i", 123);
  764. ck_assert_str_eq(buf, " 1");
  765. ck_assert_int_eq(is_trunc, 1);
  766. is_trunc = 777;
  767. mg_snprintf(NULL, &is_trunc, buf, 7, "%9i", 123);
  768. ck_assert_str_eq(buf, " ");
  769. ck_assert_int_eq(is_trunc, 1);
  770. is_trunc = 777;
  771. strcpy(buf, "1234567890");
  772. mg_snprintf(NULL, &is_trunc, buf, 0, "%i", 543);
  773. ck_assert_str_eq(buf, "1234567890");
  774. ck_assert_int_eq(is_trunc, 1);
  775. }
  776. END_TEST
  777. START_TEST(test_mg_strcasestr)
  778. {
  779. /* Adapted from unit_test.c */
  780. /* Copyright (c) 2013-2015 the Civetweb developers */
  781. /* Copyright (c) 2004-2013 Sergey Lyubka */
  782. static const char *big1 = "abcdef";
  783. mark_point();
  784. ck_assert(mg_strcasestr("Y", "X") == NULL);
  785. ck_assert(mg_strcasestr("Y", "y") != NULL);
  786. ck_assert(mg_strcasestr(big1, "X") == NULL);
  787. ck_assert(mg_strcasestr(big1, "CD") == big1 + 2);
  788. ck_assert(mg_strcasestr("aa", "AAB") == NULL);
  789. }
  790. END_TEST
  791. START_TEST(test_parse_port_string)
  792. {
  793. /* Adapted from unit_test.c */
  794. /* Copyright (c) 2013-2020 the Civetweb developers */
  795. /* Copyright (c) 2004-2013 Sergey Lyubka */
  796. struct t_test_parse_port_string {
  797. const char *port_string;
  798. int valid;
  799. int ip_family;
  800. uint16_t port_num;
  801. };
  802. static struct t_test_parse_port_string testdata[] =
  803. { {"0", 1, 4, 0},
  804. {"1", 1, 4, 1},
  805. {"65535", 1, 4, 65535},
  806. {"65536", 0, 0, 0},
  807. {"1s", 1, 4, 1},
  808. {"1r", 1, 4, 1},
  809. {"1k", 0, 0, 0},
  810. {"1.2.3", 0, 0, 0},
  811. {"1.2.3.", 0, 0, 0},
  812. {"1.2.3.4", 0, 0, 0},
  813. {"1.2.3.4:", 0, 0, 0},
  814. {"1.2.3.4:0", 1, 4, 0},
  815. {"1.2.3.4:1", 1, 4, 1},
  816. {"1.2.3.4:65535", 1, 4, 65535},
  817. {"1.2.3.4:65536", 0, 0, 0},
  818. {"1.2.3.4:1s", 1, 4, 1},
  819. {"1.2.3.4:1r", 1, 4, 1},
  820. {"1.2.3.4:1k", 0, 0, 0},
  821. #if defined(USE_IPV6)
  822. /* IPv6 config */
  823. {"[::1]:123", 1, 6, 123},
  824. {"[::]:80", 1, 6, 80},
  825. {"[3ffe:2a00:100:7031::1]:900", 1, 6, 900},
  826. /* IPv4 + IPv6 config */
  827. {"+80", 1, 4 + 6, 80},
  828. #else
  829. /* IPv6 config: invalid if IPv6 is not activated */
  830. {"[::1]:123", 0, 0, 123},
  831. {"[::]:80", 0, 0, 80},
  832. {"[3ffe:2a00:100:7031::1]:900", 0, 0, 900},
  833. /* IPv4 + IPv6 config: only IPv4 if IPv6 is not activated */
  834. {"+80", 1, 4, 80},
  835. #endif
  836. {NULL, 0, 0, 0} };
  837. struct socket so;
  838. struct vec vec;
  839. int ip_family;
  840. int i, ret;
  841. mark_point();
  842. for (i = 0; testdata[i].port_string != NULL; i++) {
  843. vec.ptr = testdata[i].port_string;
  844. vec.len = strlen(vec.ptr);
  845. ip_family = 123;
  846. ret = parse_port_string(&vec, &so, &ip_family);
  847. if ((ret != testdata[i].valid)
  848. || (ip_family != testdata[i].ip_family)) {
  849. ck_abort_msg("Port string [%s]: "
  850. "expected valid=%i, family=%i; "
  851. "got valid=%i, family=%i",
  852. testdata[i].port_string,
  853. testdata[i].valid,
  854. testdata[i].ip_family,
  855. ret,
  856. ip_family);
  857. }
  858. if (ip_family == 4) {
  859. ck_assert_int_eq((int)so.lsa.sin.sin_family, (int)AF_INET);
  860. }
  861. if (ip_family == 6) {
  862. ck_assert_int_eq((int)so.lsa.sin.sin_family, (int)AF_INET6);
  863. }
  864. if (ret) {
  865. /* Test valid strings only */
  866. ck_assert_int_eq(htons(so.lsa.sin.sin_port), testdata[i].port_num);
  867. }
  868. }
  869. /* special case: localhost can be ipv4 or ipv6 */
  870. vec.ptr = "localhost:123";
  871. vec.len = strlen(vec.ptr);
  872. ret = parse_port_string(&vec, &so, &ip_family);
  873. if (ret != 1) {
  874. ck_abort_msg("IP of localhost seems to be unknown on this system (%i)",
  875. (int)ret);
  876. }
  877. if ((ip_family != 4) && (ip_family != 6)) {
  878. ck_abort_msg("IP family for localhost must be 4 or 6 but is %i",
  879. (int)ip_family);
  880. }
  881. ck_assert_int_eq((int)htons(so.lsa.sin.sin_port), (int)123);
  882. }
  883. END_TEST
  884. START_TEST(test_parse_http_headers)
  885. {
  886. char buf[2048];
  887. char *ptr;
  888. int ret;
  889. struct mg_header hdr[MG_MAX_HEADERS];
  890. memset(hdr, 0, sizeof(hdr));
  891. memset(buf, 0, sizeof(buf));
  892. ptr = &buf[0];
  893. ret = parse_http_headers(&ptr, hdr);
  894. ck_assert_int_eq(ret, 0);
  895. ck_assert_ptr_eq(ptr, &buf[0]);
  896. ck_assert_ptr_eq(hdr[0].name, NULL);
  897. ck_assert_ptr_eq(hdr[0].value, NULL);
  898. ck_assert_ptr_eq(hdr[1].name, NULL);
  899. ck_assert_ptr_eq(hdr[1].value, NULL);
  900. ck_assert_ptr_eq(hdr[2].name, NULL);
  901. ck_assert_ptr_eq(hdr[2].value, NULL);
  902. memset(hdr, 0, sizeof(hdr));
  903. memset(buf, 0, sizeof(buf));
  904. strcpy(buf, "\r\n");
  905. ptr = &buf[0];
  906. ret = parse_http_headers(&ptr, hdr);
  907. ck_assert_int_eq(ret, 0);
  908. ck_assert_ptr_eq(ptr, &buf[0]);
  909. ck_assert_ptr_eq(hdr[0].name, NULL);
  910. ck_assert_ptr_eq(hdr[0].value, NULL);
  911. ck_assert_ptr_eq(hdr[1].name, NULL);
  912. ck_assert_ptr_eq(hdr[1].value, NULL);
  913. ck_assert_ptr_eq(hdr[2].name, NULL);
  914. ck_assert_ptr_eq(hdr[2].value, NULL);
  915. memset(hdr, 0, sizeof(hdr));
  916. memset(buf, 0, sizeof(buf));
  917. strcpy(buf, "a\r\n");
  918. ptr = &buf[0];
  919. ret = parse_http_headers(&ptr, hdr);
  920. ck_assert_int_eq(ret, -1);
  921. ck_assert_ptr_eq(ptr, &buf[0]);
  922. ck_assert_ptr_eq(hdr[0].name, NULL);
  923. ck_assert_ptr_eq(hdr[0].value, NULL);
  924. ck_assert_ptr_eq(hdr[1].name, NULL);
  925. ck_assert_ptr_eq(hdr[1].value, NULL);
  926. ck_assert_ptr_eq(hdr[2].name, NULL);
  927. ck_assert_ptr_eq(hdr[2].value, NULL);
  928. memset(hdr, 0, sizeof(hdr));
  929. memset(buf, 0, sizeof(buf));
  930. strcpy(buf, "a:b");
  931. ptr = &buf[0];
  932. ret = parse_http_headers(&ptr, hdr);
  933. ck_assert_int_eq(ret, 1);
  934. ck_assert_ptr_eq(ptr, &buf[3]);
  935. ck_assert_str_eq(hdr[0].name, "a");
  936. ck_assert_str_eq(hdr[0].value, "b");
  937. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  938. ck_assert_ptr_eq(hdr[0].value, &buf[2]);
  939. ck_assert_ptr_eq(hdr[1].name, NULL);
  940. ck_assert_ptr_eq(hdr[1].value, NULL);
  941. ck_assert_ptr_eq(hdr[2].name, NULL);
  942. ck_assert_ptr_eq(hdr[2].value, NULL);
  943. memset(hdr, 0, sizeof(hdr));
  944. memset(buf, 0, sizeof(buf));
  945. strcpy(buf, "a:b\r\n");
  946. ptr = &buf[0];
  947. ret = parse_http_headers(&ptr, hdr);
  948. ck_assert_int_eq(ret, 1);
  949. ck_assert_ptr_eq(ptr, &buf[5]);
  950. ck_assert_str_eq(hdr[0].name, "a");
  951. ck_assert_str_eq(hdr[0].value, "b");
  952. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  953. ck_assert_ptr_eq(hdr[0].value, &buf[2]);
  954. ck_assert_ptr_eq(hdr[1].name, NULL);
  955. ck_assert_ptr_eq(hdr[1].value, NULL);
  956. ck_assert_ptr_eq(hdr[2].name, NULL);
  957. ck_assert_ptr_eq(hdr[2].value, NULL);
  958. memset(hdr, 0, sizeof(hdr));
  959. memset(buf, 0, sizeof(buf));
  960. strcpy(buf, "a:b\r\n\r\n");
  961. ptr = &buf[0];
  962. ret = parse_http_headers(&ptr, hdr);
  963. ck_assert_int_eq(ret, 1);
  964. ck_assert_ptr_eq(ptr, &buf[5]);
  965. ck_assert_str_eq(hdr[0].name, "a");
  966. ck_assert_str_eq(hdr[0].value, "b");
  967. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  968. ck_assert_ptr_eq(hdr[0].value, &buf[2]);
  969. ck_assert_ptr_eq(hdr[1].name, NULL);
  970. ck_assert_ptr_eq(hdr[1].value, NULL);
  971. ck_assert_ptr_eq(hdr[2].name, NULL);
  972. ck_assert_ptr_eq(hdr[2].value, NULL);
  973. memset(hdr, 0, sizeof(hdr));
  974. memset(buf, 0, sizeof(buf));
  975. strcpy(buf, "a: b\r\n");
  976. ptr = &buf[0];
  977. ret = parse_http_headers(&ptr, hdr);
  978. ck_assert_int_eq(ret, 1);
  979. ck_assert_ptr_eq(ptr, &buf[6]);
  980. ck_assert_str_eq(hdr[0].name, "a");
  981. ck_assert_str_eq(hdr[0].value, "b");
  982. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  983. ck_assert_ptr_eq(hdr[0].value, &buf[3]);
  984. ck_assert_ptr_eq(hdr[1].name, NULL);
  985. ck_assert_ptr_eq(hdr[1].value, NULL);
  986. ck_assert_ptr_eq(hdr[2].name, NULL);
  987. ck_assert_ptr_eq(hdr[2].value, NULL);
  988. memset(hdr, 0, sizeof(hdr));
  989. memset(buf, 0, sizeof(buf));
  990. strcpy(buf, "a :b\r\n");
  991. ptr = &buf[0];
  992. ret = parse_http_headers(&ptr, hdr);
  993. ck_assert_int_eq(ret, 1);
  994. ck_assert_ptr_eq(ptr, &buf[6]);
  995. ck_assert_str_eq(hdr[0].name, "a");
  996. ck_assert_str_eq(hdr[0].value, "b");
  997. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  998. ck_assert_ptr_eq(hdr[0].value, &buf[3]);
  999. ck_assert_ptr_eq(hdr[1].name, NULL);
  1000. ck_assert_ptr_eq(hdr[1].value, NULL);
  1001. ck_assert_ptr_eq(hdr[2].name, NULL);
  1002. ck_assert_ptr_eq(hdr[2].value, NULL);
  1003. memset(hdr, 0, sizeof(hdr));
  1004. memset(buf, 0, sizeof(buf));
  1005. strcpy(buf, "a : b\r\n");
  1006. ptr = &buf[0];
  1007. ret = parse_http_headers(&ptr, hdr);
  1008. ck_assert_int_eq(ret, 1);
  1009. ck_assert_ptr_eq(ptr, &buf[7]);
  1010. ck_assert_str_eq(hdr[0].name, "a");
  1011. ck_assert_str_eq(hdr[0].value, "b");
  1012. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  1013. ck_assert_ptr_eq(hdr[0].value, &buf[4]);
  1014. ck_assert_ptr_eq(hdr[1].name, NULL);
  1015. ck_assert_ptr_eq(hdr[1].value, NULL);
  1016. ck_assert_ptr_eq(hdr[2].name, NULL);
  1017. ck_assert_ptr_eq(hdr[2].value, NULL);
  1018. memset(hdr, 0, sizeof(hdr));
  1019. memset(buf, 0, sizeof(buf));
  1020. strcpy(buf, "a: b\r\nc: d\r\n");
  1021. ptr = &buf[0];
  1022. ret = parse_http_headers(&ptr, hdr);
  1023. ck_assert_int_eq(ret, 2);
  1024. ck_assert_ptr_eq(ptr, &buf[12]);
  1025. ck_assert_str_eq(hdr[0].name, "a");
  1026. ck_assert_str_eq(hdr[0].value, "b");
  1027. ck_assert_str_eq(hdr[1].name, "c");
  1028. ck_assert_str_eq(hdr[1].value, "d");
  1029. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  1030. ck_assert_ptr_eq(hdr[0].value, &buf[3]);
  1031. ck_assert_ptr_eq(hdr[1].name, &buf[6]);
  1032. ck_assert_ptr_eq(hdr[1].value, &buf[9]);
  1033. ck_assert_ptr_eq(hdr[2].name, NULL);
  1034. ck_assert_ptr_eq(hdr[2].value, NULL);
  1035. memset(hdr, 0, sizeof(hdr));
  1036. memset(buf, 0, sizeof(buf));
  1037. strcpy(buf, "a: b\r\nc: d\r\n\r\n");
  1038. ptr = &buf[0];
  1039. ret = parse_http_headers(&ptr, hdr);
  1040. ck_assert_int_eq(ret, 2);
  1041. ck_assert_ptr_eq(ptr, &buf[12]);
  1042. ck_assert_str_eq(hdr[0].name, "a");
  1043. ck_assert_str_eq(hdr[0].value, "b");
  1044. ck_assert_str_eq(hdr[1].name, "c");
  1045. ck_assert_str_eq(hdr[1].value, "d");
  1046. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  1047. ck_assert_ptr_eq(hdr[0].value, &buf[3]);
  1048. ck_assert_ptr_eq(hdr[1].name, &buf[6]);
  1049. ck_assert_ptr_eq(hdr[1].value, &buf[9]);
  1050. ck_assert_ptr_eq(hdr[2].name, NULL);
  1051. ck_assert_ptr_eq(hdr[2].value, NULL);
  1052. memset(hdr, 0, sizeof(hdr));
  1053. memset(buf, 0, sizeof(buf));
  1054. strcpy(buf, "a: b\r\nc: d\r\n\r\ne: f\r\n");
  1055. ptr = &buf[0];
  1056. ret = parse_http_headers(&ptr, hdr);
  1057. ck_assert_int_eq(ret, 2);
  1058. ck_assert_ptr_eq(ptr, &buf[12]);
  1059. ck_assert_str_eq(hdr[0].name, "a");
  1060. ck_assert_str_eq(hdr[0].value, "b");
  1061. ck_assert_str_eq(hdr[1].name, "c");
  1062. ck_assert_str_eq(hdr[1].value, "d");
  1063. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  1064. ck_assert_ptr_eq(hdr[0].value, &buf[3]);
  1065. ck_assert_ptr_eq(hdr[1].name, &buf[6]);
  1066. ck_assert_ptr_eq(hdr[1].value, &buf[9]);
  1067. ck_assert_ptr_eq(hdr[2].name, NULL);
  1068. ck_assert_ptr_eq(hdr[2].value, NULL);
  1069. memset(hdr, 0, sizeof(hdr));
  1070. memset(buf, 0, sizeof(buf));
  1071. strcpy(buf, "a: b\r\nc: d");
  1072. ptr = &buf[0];
  1073. ret = parse_http_headers(&ptr, hdr);
  1074. ck_assert_int_eq(ret, 2);
  1075. ck_assert_ptr_eq(ptr, &buf[10]);
  1076. ck_assert_str_eq(hdr[0].name, "a");
  1077. ck_assert_str_eq(hdr[0].value, "b");
  1078. ck_assert_str_eq(hdr[1].name, "c");
  1079. ck_assert_str_eq(hdr[1].value, "d");
  1080. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  1081. ck_assert_ptr_eq(hdr[0].value, &buf[3]);
  1082. ck_assert_ptr_eq(hdr[1].name, &buf[6]);
  1083. ck_assert_ptr_eq(hdr[1].value, &buf[9]);
  1084. ck_assert_ptr_eq(hdr[2].name, NULL);
  1085. ck_assert_ptr_eq(hdr[2].value, NULL);
  1086. memset(hdr, 0, sizeof(hdr));
  1087. memset(buf, 0, sizeof(buf));
  1088. strcpy(buf, "a: b\r\nc: d\r\ne");
  1089. ptr = &buf[0];
  1090. ret = parse_http_headers(&ptr, hdr);
  1091. ck_assert_int_eq(ret, -1);
  1092. /* The following could be undefined, since ret == -1 */
  1093. ck_assert_ptr_eq(ptr, &buf[12]);
  1094. ck_assert_str_eq(hdr[0].name, "a");
  1095. ck_assert_str_eq(hdr[0].value, "b");
  1096. ck_assert_str_eq(hdr[1].name, "c");
  1097. ck_assert_str_eq(hdr[1].value, "d");
  1098. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  1099. ck_assert_ptr_eq(hdr[0].value, &buf[3]);
  1100. ck_assert_ptr_eq(hdr[1].name, &buf[6]);
  1101. ck_assert_ptr_eq(hdr[1].value, &buf[9]);
  1102. ck_assert_ptr_eq(hdr[2].name, NULL);
  1103. ck_assert_ptr_eq(hdr[2].value, NULL);
  1104. memset(hdr, 0, sizeof(hdr));
  1105. memset(buf, 0, sizeof(buf));
  1106. strcpy(buf, "a: b\r\nc: d\r\nefg:");
  1107. ptr = &buf[0];
  1108. ret = parse_http_headers(&ptr, hdr);
  1109. ck_assert_int_eq(ret, 3);
  1110. ck_assert_ptr_eq(ptr, &buf[16]);
  1111. ck_assert_str_eq(hdr[0].name, "a");
  1112. ck_assert_str_eq(hdr[0].value, "b");
  1113. ck_assert_str_eq(hdr[1].name, "c");
  1114. ck_assert_str_eq(hdr[1].value, "d");
  1115. ck_assert_str_eq(hdr[2].name, "efg");
  1116. ck_assert_str_eq(hdr[2].value, "");
  1117. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  1118. ck_assert_ptr_eq(hdr[0].value, &buf[3]);
  1119. ck_assert_ptr_eq(hdr[1].name, &buf[6]);
  1120. ck_assert_ptr_eq(hdr[1].value, &buf[9]);
  1121. ck_assert_ptr_eq(hdr[2].name, &buf[12]);
  1122. ck_assert_ptr_eq(hdr[2].value, &buf[16]);
  1123. }
  1124. END_TEST
  1125. START_TEST(test_encode_decode)
  1126. {
  1127. char buf[128];
  1128. const char *alpha = "abcdefghijklmnopqrstuvwxyz";
  1129. const char *nonalpha = " !\"#$%&'()*+,-./0123456789:;<=>?@";
  1130. const char *nonalpha_url_enc1 =
  1131. "%20%21%22%23$%25%26%27()%2a%2b,-.%2f0123456789%3a;%3c%3d%3e%3f%40";
  1132. const char *nonalpha_url_enc2 =
  1133. "%20!%22%23%24%25%26'()*%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40";
  1134. int ret;
  1135. size_t len;
  1136. mark_point();
  1137. memset(buf, 77, sizeof(buf));
  1138. ret = mg_url_encode(alpha, buf, sizeof(buf));
  1139. ck_assert_int_eq(ret, (int)strlen(buf));
  1140. ck_assert_int_eq(ret, (int)strlen(alpha));
  1141. ck_assert_str_eq(buf, alpha);
  1142. memset(buf, 77, sizeof(buf));
  1143. ret = mg_url_encode(nonalpha, buf, sizeof(buf));
  1144. ck_assert_int_eq(ret, (int)strlen(buf));
  1145. ck_assert_int_eq(ret, (int)strlen(nonalpha_url_enc1));
  1146. ck_assert_str_eq(buf, nonalpha_url_enc1);
  1147. memset(buf, 77, sizeof(buf));
  1148. ret = mg_url_decode(alpha, (int)strlen(alpha), buf, sizeof(buf), 0);
  1149. ck_assert_int_eq(ret, (int)strlen(buf));
  1150. ck_assert_int_eq(ret, (int)strlen(alpha));
  1151. ck_assert_str_eq(buf, alpha);
  1152. memset(buf, 77, sizeof(buf));
  1153. ret = mg_url_decode(
  1154. nonalpha_url_enc1, (int)strlen(nonalpha_url_enc1), buf, sizeof(buf), 0);
  1155. ck_assert_int_eq(ret, (int)strlen(buf));
  1156. ck_assert_int_eq(ret, (int)strlen(nonalpha));
  1157. ck_assert_str_eq(buf, nonalpha);
  1158. memset(buf, 77, sizeof(buf));
  1159. ret = mg_url_decode(
  1160. nonalpha_url_enc2, (int)strlen(nonalpha_url_enc2), buf, sizeof(buf), 0);
  1161. ck_assert_int_eq(ret, (int)strlen(buf));
  1162. ck_assert_int_eq(ret, (int)strlen(nonalpha));
  1163. ck_assert_str_eq(buf, nonalpha);
  1164. /* Test url_decode_in_place */
  1165. strcpy(buf, "AbcDef");
  1166. url_decode_in_place(buf);
  1167. ck_assert_str_eq(buf, "AbcDef");
  1168. strcpy(buf, "A%20B+C");
  1169. url_decode_in_place(buf);
  1170. ck_assert_str_eq(buf, "A B C");
  1171. /* len could be unused, if base64_decode is not tested because USE_LUA is
  1172. * not defined */
  1173. (void)len;
  1174. }
  1175. END_TEST
  1176. START_TEST(test_mask_data)
  1177. {
  1178. #if defined(USE_WEBSOCKET)
  1179. char in[1024];
  1180. char out[1024];
  1181. int i;
  1182. #endif
  1183. uint32_t mask = 0x61626364;
  1184. /* TODO: adapt test for big endian */
  1185. ck_assert((*(unsigned char *)&mask) == 0x64u);
  1186. #if defined(USE_WEBSOCKET)
  1187. memset(in, 0, sizeof(in));
  1188. memset(out, 99, sizeof(out));
  1189. mask_data(in, sizeof(out), 0, out);
  1190. ck_assert(!memcmp(out, in, sizeof(out)));
  1191. for (i = 0; i < 1024; i++) {
  1192. in[i] = (char)((unsigned char)i);
  1193. }
  1194. mask_data(in, 107, 0, out);
  1195. ck_assert(!memcmp(out, in, 107));
  1196. mask_data(in, 256, 0x01010101, out);
  1197. for (i = 0; i < 256; i++) {
  1198. ck_assert_int_eq((int)((unsigned char)out[i]),
  1199. (int)(((unsigned char)in[i]) ^ (char)1u));
  1200. }
  1201. for (i = 256; i < (int)sizeof(out); i++) {
  1202. ck_assert_int_eq((int)((unsigned char)out[i]), (int)0);
  1203. }
  1204. /* TODO: check this for big endian */
  1205. mask_data(in, 5, 0x01020304, out);
  1206. ck_assert_uint_eq((unsigned char)out[0], 0u ^ 4u);
  1207. ck_assert_uint_eq((unsigned char)out[1], 1u ^ 3u);
  1208. ck_assert_uint_eq((unsigned char)out[2], 2u ^ 2u);
  1209. ck_assert_uint_eq((unsigned char)out[3], 3u ^ 1u);
  1210. ck_assert_uint_eq((unsigned char)out[4], 4u ^ 4u);
  1211. #endif
  1212. }
  1213. END_TEST
  1214. START_TEST(test_parse_date_string)
  1215. {
  1216. #if !defined(NO_CACHING)
  1217. time_t now = time(0);
  1218. struct tm *tm = gmtime(&now);
  1219. char date[64] = {0};
  1220. unsigned long i;
  1221. ck_assert_uint_eq((unsigned long)parse_date_string("1/Jan/1970 00:01:02"),
  1222. 62ul);
  1223. ck_assert_uint_eq((unsigned long)parse_date_string("1 Jan 1970 00:02:03"),
  1224. 123ul);
  1225. ck_assert_uint_eq((unsigned long)parse_date_string("1-Jan-1970 00:03:04"),
  1226. 184ul);
  1227. ck_assert_uint_eq((unsigned long)parse_date_string(
  1228. "Xyz, 1 Jan 1970 00:04:05"),
  1229. 245ul);
  1230. gmt_time_string(date, sizeof(date), &now);
  1231. ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
  1232. sprintf(date,
  1233. "%02u %s %04u %02u:%02u:%02u",
  1234. (unsigned int)tm->tm_mday,
  1235. month_names[tm->tm_mon],
  1236. (unsigned int)(tm->tm_year + 1900),
  1237. (unsigned int)tm->tm_hour,
  1238. (unsigned int)tm->tm_min,
  1239. (unsigned int)tm->tm_sec);
  1240. ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
  1241. gmt_time_string(date, 1, NULL);
  1242. ck_assert_str_eq(date, "");
  1243. gmt_time_string(date, 6, NULL);
  1244. ck_assert_str_eq(date,
  1245. "Thu, "); /* part of "Thu, 01 Jan 1970 00:00:00 GMT" */
  1246. gmt_time_string(date, sizeof(date), NULL);
  1247. ck_assert_str_eq(date, "Thu, 01 Jan 1970 00:00:00 GMT");
  1248. for (i = 2ul; i < 0x8000000ul; i += i / 2) {
  1249. now = (time_t)i;
  1250. gmt_time_string(date, sizeof(date), &now);
  1251. ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
  1252. tm = gmtime(&now);
  1253. sprintf(date,
  1254. "%02u-%s-%04u %02u:%02u:%02u",
  1255. (unsigned int)tm->tm_mday,
  1256. month_names[tm->tm_mon],
  1257. (unsigned int)(tm->tm_year + 1900),
  1258. (unsigned int)tm->tm_hour,
  1259. (unsigned int)tm->tm_min,
  1260. (unsigned int)tm->tm_sec);
  1261. ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
  1262. }
  1263. #endif
  1264. }
  1265. END_TEST
  1266. START_TEST(test_sha1)
  1267. {
  1268. #ifdef SHA1_DIGEST_SIZE
  1269. SHA_CTX sha_ctx;
  1270. uint8_t digest[SHA1_DIGEST_SIZE] = {0};
  1271. char str[48] = {0};
  1272. int i;
  1273. const char *test_str;
  1274. ck_assert_uint_eq(sizeof(digest), 20);
  1275. ck_assert_uint_gt(sizeof(str), sizeof(digest) * 2 + 1);
  1276. /* empty string */
  1277. SHA1_Init(&sha_ctx);
  1278. SHA1_Final(digest, &sha_ctx);
  1279. bin2str(str, digest, sizeof(digest));
  1280. ck_assert_uint_eq(strlen(str), 40);
  1281. ck_assert_str_eq(str, "da39a3ee5e6b4b0d3255bfef95601890afd80709");
  1282. /* empty string */
  1283. SHA1_Init(&sha_ctx);
  1284. SHA1_Update(&sha_ctx, (uint8_t *)"abc", 0);
  1285. SHA1_Final(digest, &sha_ctx);
  1286. bin2str(str, digest, sizeof(digest));
  1287. ck_assert_uint_eq(strlen(str), 40);
  1288. ck_assert_str_eq(str, "da39a3ee5e6b4b0d3255bfef95601890afd80709");
  1289. /* "abc" */
  1290. SHA1_Init(&sha_ctx);
  1291. SHA1_Update(&sha_ctx, (uint8_t *)"abc", 3);
  1292. SHA1_Final(digest, &sha_ctx);
  1293. bin2str(str, digest, sizeof(digest));
  1294. ck_assert_uint_eq(strlen(str), 40);
  1295. ck_assert_str_eq(str, "a9993e364706816aba3e25717850c26c9cd0d89d");
  1296. /* "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" */
  1297. test_str = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  1298. SHA1_Init(&sha_ctx);
  1299. SHA1_Update(&sha_ctx, (uint8_t *)test_str, (uint32_t)strlen(test_str));
  1300. SHA1_Final(digest, &sha_ctx);
  1301. bin2str(str, digest, sizeof(digest));
  1302. ck_assert_uint_eq(strlen(str), 40);
  1303. ck_assert_str_eq(str, "84983e441c3bd26ebaae4aa1f95129e5e54670f1");
  1304. /* a million "a" */
  1305. SHA1_Init(&sha_ctx);
  1306. for (i = 0; i < 1000000; i++) {
  1307. SHA1_Update(&sha_ctx, (uint8_t *)"a", 1);
  1308. }
  1309. SHA1_Final(digest, &sha_ctx);
  1310. bin2str(str, digest, sizeof(digest));
  1311. ck_assert_uint_eq(strlen(str), 40);
  1312. ck_assert_str_eq(str, "34aa973cd4c4daa4f61eeb2bdbad27316534016f");
  1313. /* a million "a" in blocks of 10 */
  1314. SHA1_Init(&sha_ctx);
  1315. for (i = 0; i < 100000; i++) {
  1316. SHA1_Update(&sha_ctx, (uint8_t *)"aaaaaaaaaa", 10);
  1317. }
  1318. SHA1_Final(digest, &sha_ctx);
  1319. bin2str(str, digest, sizeof(digest));
  1320. ck_assert_uint_eq(strlen(str), 40);
  1321. ck_assert_str_eq(str, "34aa973cd4c4daa4f61eeb2bdbad27316534016f");
  1322. #else
  1323. /* Can not test, if SHA1 is not included */
  1324. ck_assert(1);
  1325. #endif
  1326. }
  1327. END_TEST
  1328. START_TEST(test_config_options)
  1329. {
  1330. /* Check size of config_options vs. number of options in enum. */
  1331. ck_assert_ptr_eq(NULL, config_options[NUM_OPTIONS].name);
  1332. ck_assert_int_eq((int)MG_CONFIG_TYPE_UNKNOWN,
  1333. config_options[NUM_OPTIONS].type);
  1334. ck_assert_uint_eq(sizeof(config_options) / sizeof(config_options[0]),
  1335. (size_t)(NUM_OPTIONS + 1));
  1336. /* Check option enums vs. option names. */
  1337. /* Check if the order in
  1338. * static struct mg_option config_options[]
  1339. * is the same as in the option enum
  1340. * This test allows to reorder config_options and the enum,
  1341. * and check if the order is still consistent. */
  1342. ck_assert_str_eq("cgi_pattern", config_options[CGI_EXTENSIONS].name);
  1343. ck_assert_str_eq("cgi_environment", config_options[CGI_ENVIRONMENT].name);
  1344. ck_assert_str_eq("put_delete_auth_file",
  1345. config_options[PUT_DELETE_PASSWORDS_FILE].name);
  1346. ck_assert_str_eq("cgi_interpreter", config_options[CGI_INTERPRETER].name);
  1347. ck_assert_str_eq("protect_uri", config_options[PROTECT_URI].name);
  1348. ck_assert_str_eq("authentication_domain",
  1349. config_options[AUTHENTICATION_DOMAIN].name);
  1350. ck_assert_str_eq("enable_auth_domain_check",
  1351. config_options[ENABLE_AUTH_DOMAIN_CHECK].name);
  1352. ck_assert_str_eq("ssi_pattern", config_options[SSI_EXTENSIONS].name);
  1353. ck_assert_str_eq("throttle", config_options[THROTTLE].name);
  1354. ck_assert_str_eq("access_log_file", config_options[ACCESS_LOG_FILE].name);
  1355. ck_assert_str_eq("enable_directory_listing",
  1356. config_options[ENABLE_DIRECTORY_LISTING].name);
  1357. ck_assert_str_eq("error_log_file", config_options[ERROR_LOG_FILE].name);
  1358. ck_assert_str_eq("global_auth_file",
  1359. config_options[GLOBAL_PASSWORDS_FILE].name);
  1360. ck_assert_str_eq("index_files", config_options[INDEX_FILES].name);
  1361. ck_assert_str_eq("enable_keep_alive",
  1362. config_options[ENABLE_KEEP_ALIVE].name);
  1363. ck_assert_str_eq("access_control_list",
  1364. config_options[ACCESS_CONTROL_LIST].name);
  1365. ck_assert_str_eq("extra_mime_types", config_options[EXTRA_MIME_TYPES].name);
  1366. ck_assert_str_eq("listening_ports", config_options[LISTENING_PORTS].name);
  1367. ck_assert_str_eq("document_root", config_options[DOCUMENT_ROOT].name);
  1368. ck_assert_str_eq("ssl_certificate", config_options[SSL_CERTIFICATE].name);
  1369. ck_assert_str_eq("ssl_certificate_chain",
  1370. config_options[SSL_CERTIFICATE_CHAIN].name);
  1371. ck_assert_str_eq("num_threads", config_options[NUM_THREADS].name);
  1372. ck_assert_str_eq("run_as_user", config_options[RUN_AS_USER].name);
  1373. ck_assert_str_eq("url_rewrite_patterns",
  1374. config_options[URL_REWRITE_PATTERN].name);
  1375. ck_assert_str_eq("hide_files_patterns", config_options[HIDE_FILES].name);
  1376. ck_assert_str_eq("request_timeout_ms",
  1377. config_options[REQUEST_TIMEOUT].name);
  1378. ck_assert_str_eq("keep_alive_timeout_ms",
  1379. config_options[KEEP_ALIVE_TIMEOUT].name);
  1380. ck_assert_str_eq("linger_timeout_ms", config_options[LINGER_TIMEOUT].name);
  1381. ck_assert_str_eq("listen_backlog",
  1382. config_options[LISTEN_BACKLOG_SIZE].name);
  1383. ck_assert_str_eq("ssl_verify_peer",
  1384. config_options[SSL_DO_VERIFY_PEER].name);
  1385. ck_assert_str_eq("ssl_ca_path", config_options[SSL_CA_PATH].name);
  1386. ck_assert_str_eq("ssl_ca_file", config_options[SSL_CA_FILE].name);
  1387. ck_assert_str_eq("ssl_verify_depth", config_options[SSL_VERIFY_DEPTH].name);
  1388. ck_assert_str_eq("ssl_default_verify_paths",
  1389. config_options[SSL_DEFAULT_VERIFY_PATHS].name);
  1390. ck_assert_str_eq("ssl_cipher_list", config_options[SSL_CIPHER_LIST].name);
  1391. ck_assert_str_eq("ssl_protocol_version",
  1392. config_options[SSL_PROTOCOL_VERSION].name);
  1393. ck_assert_str_eq("ssl_short_trust", config_options[SSL_SHORT_TRUST].name);
  1394. #if defined(USE_WEBSOCKET)
  1395. ck_assert_str_eq("websocket_timeout_ms",
  1396. config_options[WEBSOCKET_TIMEOUT].name);
  1397. ck_assert_str_eq("enable_websocket_ping_pong",
  1398. config_options[ENABLE_WEBSOCKET_PING_PONG].name);
  1399. #endif
  1400. ck_assert_str_eq("decode_url", config_options[DECODE_URL].name);
  1401. ck_assert_str_eq("decode_query_string",
  1402. config_options[DECODE_QUERY_STRING].name);
  1403. #if defined(USE_LUA)
  1404. ck_assert_str_eq("lua_preload_file", config_options[LUA_PRELOAD_FILE].name);
  1405. ck_assert_str_eq("lua_script_pattern",
  1406. config_options[LUA_SCRIPT_EXTENSIONS].name);
  1407. ck_assert_str_eq("lua_server_page_pattern",
  1408. config_options[LUA_SERVER_PAGE_EXTENSIONS].name);
  1409. #endif
  1410. #if defined(USE_DUKTAPE)
  1411. ck_assert_str_eq("duktape_script_pattern",
  1412. config_options[DUKTAPE_SCRIPT_EXTENSIONS].name);
  1413. #endif
  1414. #if defined(USE_WEBSOCKET)
  1415. ck_assert_str_eq("websocket_root", config_options[WEBSOCKET_ROOT].name);
  1416. #endif
  1417. #if defined(USE_LUA) && defined(USE_WEBSOCKET)
  1418. ck_assert_str_eq("lua_websocket_pattern",
  1419. config_options[LUA_WEBSOCKET_EXTENSIONS].name);
  1420. #endif
  1421. ck_assert_str_eq("access_control_allow_origin",
  1422. config_options[ACCESS_CONTROL_ALLOW_ORIGIN].name);
  1423. ck_assert_str_eq("access_control_allow_methods",
  1424. config_options[ACCESS_CONTROL_ALLOW_METHODS].name);
  1425. ck_assert_str_eq("access_control_allow_headers",
  1426. config_options[ACCESS_CONTROL_ALLOW_HEADERS].name);
  1427. ck_assert_str_eq("error_pages", config_options[ERROR_PAGES].name);
  1428. ck_assert_str_eq("tcp_nodelay", config_options[CONFIG_TCP_NODELAY].name);
  1429. #if !defined(NO_CACHING)
  1430. ck_assert_str_eq("static_file_max_age",
  1431. config_options[STATIC_FILE_MAX_AGE].name);
  1432. #endif
  1433. #if !defined(NO_SSL)
  1434. ck_assert_str_eq("strict_transport_security_max_age",
  1435. config_options[STRICT_HTTPS_MAX_AGE].name);
  1436. #endif
  1437. #if defined(__linux__)
  1438. ck_assert_str_eq("allow_sendfile_call",
  1439. config_options[ALLOW_SENDFILE_CALL].name);
  1440. #endif
  1441. #if defined(_WIN32)
  1442. ck_assert_str_eq("case_sensitive",
  1443. config_options[CASE_SENSITIVE_FILES].name);
  1444. #endif
  1445. #if defined(USE_LUA)
  1446. ck_assert_str_eq("lua_background_script",
  1447. config_options[LUA_BACKGROUND_SCRIPT].name);
  1448. ck_assert_str_eq("lua_background_script_params",
  1449. config_options[LUA_BACKGROUND_SCRIPT_PARAMS].name);
  1450. #endif
  1451. ck_assert_str_eq("additional_header",
  1452. config_options[ADDITIONAL_HEADER].name);
  1453. ck_assert_str_eq("max_request_size", config_options[MAX_REQUEST_SIZE].name);
  1454. ck_assert_str_eq("allow_index_script_resource",
  1455. config_options[ALLOW_INDEX_SCRIPT_SUB_RES].name);
  1456. }
  1457. END_TEST
  1458. #if !defined(REPLACE_CHECK_FOR_LOCAL_DEBUGGING)
  1459. Suite *
  1460. make_private_suite(void)
  1461. {
  1462. Suite *const suite = suite_create("Private");
  1463. TCase *const tcase_http_message = tcase_create("HTTP Message");
  1464. TCase *const tcase_http_keep_alive = tcase_create("HTTP Keep Alive");
  1465. TCase *const tcase_url_parsing_1 = tcase_create("URL Parsing 1");
  1466. TCase *const tcase_url_parsing_2 = tcase_create("URL Parsing 2");
  1467. TCase *const tcase_url_parsing_3 = tcase_create("URL Parsing 3");
  1468. TCase *const tcase_internal_parse_1 = tcase_create("Internal Parsing 1");
  1469. TCase *const tcase_internal_parse_2 = tcase_create("Internal Parsing 2");
  1470. TCase *const tcase_internal_parse_3 = tcase_create("Internal Parsing 3");
  1471. TCase *const tcase_internal_parse_4 = tcase_create("Internal Parsing 4");
  1472. TCase *const tcase_internal_parse_5 = tcase_create("Internal Parsing 5");
  1473. TCase *const tcase_internal_parse_6 = tcase_create("Internal Parsing 6");
  1474. TCase *const tcase_internal_parse_7 = tcase_create("Internal Parsing 7");
  1475. TCase *const tcase_encode_decode = tcase_create("Encode Decode");
  1476. TCase *const tcase_mask_data = tcase_create("Mask Data");
  1477. TCase *const tcase_parse_date_string = tcase_create("Date Parsing");
  1478. TCase *const tcase_sha1 = tcase_create("SHA1");
  1479. TCase *const tcase_config_options = tcase_create("Config Options");
  1480. tcase_add_test(tcase_http_message, test_parse_http_message);
  1481. tcase_set_timeout(tcase_http_message, civetweb_min_test_timeout);
  1482. suite_add_tcase(suite, tcase_http_message);
  1483. tcase_add_test(tcase_http_keep_alive, test_should_keep_alive);
  1484. tcase_set_timeout(tcase_http_keep_alive, civetweb_min_test_timeout);
  1485. suite_add_tcase(suite, tcase_http_keep_alive);
  1486. tcase_add_test(tcase_url_parsing_1, test_match_prefix);
  1487. tcase_add_test(tcase_url_parsing_1, test_match_prefix_strlen);
  1488. tcase_add_test(tcase_url_parsing_1, test_match_prefix_fuzz);
  1489. tcase_set_timeout(tcase_url_parsing_1, civetweb_min_test_timeout);
  1490. suite_add_tcase(suite, tcase_url_parsing_1);
  1491. tcase_add_test(tcase_url_parsing_2, test_remove_dot_segments);
  1492. tcase_set_timeout(tcase_url_parsing_2, civetweb_min_test_timeout);
  1493. suite_add_tcase(suite, tcase_url_parsing_2);
  1494. tcase_add_test(tcase_url_parsing_3, test_is_valid_uri);
  1495. tcase_set_timeout(tcase_url_parsing_3, civetweb_min_test_timeout);
  1496. suite_add_tcase(suite, tcase_url_parsing_3);
  1497. tcase_add_test(tcase_internal_parse_1, test_next_option);
  1498. tcase_set_timeout(tcase_internal_parse_1, civetweb_min_test_timeout);
  1499. suite_add_tcase(suite, tcase_internal_parse_1);
  1500. tcase_add_test(tcase_internal_parse_2, test_skip_quoted);
  1501. tcase_set_timeout(tcase_internal_parse_2, civetweb_min_test_timeout);
  1502. suite_add_tcase(suite, tcase_internal_parse_2);
  1503. tcase_add_test(tcase_internal_parse_3, test_mg_strcasestr);
  1504. tcase_set_timeout(tcase_internal_parse_3, civetweb_min_test_timeout);
  1505. suite_add_tcase(suite, tcase_internal_parse_3);
  1506. tcase_add_test(tcase_internal_parse_4, test_alloc_vprintf);
  1507. tcase_set_timeout(tcase_internal_parse_4, civetweb_min_test_timeout);
  1508. suite_add_tcase(suite, tcase_internal_parse_4);
  1509. tcase_add_test(tcase_internal_parse_5, test_mg_vsnprintf);
  1510. tcase_set_timeout(tcase_internal_parse_5, civetweb_min_test_timeout);
  1511. suite_add_tcase(suite, tcase_internal_parse_5);
  1512. tcase_add_test(tcase_internal_parse_6, test_parse_port_string);
  1513. tcase_set_timeout(tcase_internal_parse_6, civetweb_min_test_timeout);
  1514. suite_add_tcase(suite, tcase_internal_parse_6);
  1515. tcase_add_test(tcase_internal_parse_7, test_parse_http_headers);
  1516. tcase_set_timeout(tcase_internal_parse_7, civetweb_min_test_timeout);
  1517. suite_add_tcase(suite, tcase_internal_parse_7);
  1518. tcase_add_test(tcase_encode_decode, test_encode_decode);
  1519. tcase_set_timeout(tcase_encode_decode, civetweb_min_test_timeout);
  1520. suite_add_tcase(suite, tcase_encode_decode);
  1521. tcase_add_test(tcase_mask_data, test_mask_data);
  1522. tcase_set_timeout(tcase_mask_data, civetweb_min_test_timeout);
  1523. suite_add_tcase(suite, tcase_mask_data);
  1524. tcase_add_test(tcase_parse_date_string, test_parse_date_string);
  1525. tcase_set_timeout(tcase_parse_date_string, civetweb_min_test_timeout);
  1526. suite_add_tcase(suite, tcase_parse_date_string);
  1527. tcase_add_test(tcase_sha1, test_sha1);
  1528. tcase_set_timeout(tcase_sha1, civetweb_min_test_timeout);
  1529. suite_add_tcase(suite, tcase_sha1);
  1530. tcase_add_test(tcase_config_options, test_config_options);
  1531. tcase_set_timeout(tcase_config_options, civetweb_min_test_timeout);
  1532. suite_add_tcase(suite, tcase_config_options);
  1533. return suite;
  1534. }
  1535. #endif
  1536. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  1537. /* Used to debug test cases without using the check framework */
  1538. void
  1539. MAIN_PRIVATE(void)
  1540. {
  1541. #if defined(_WIN32)
  1542. /* test_parse_port_string requires WSAStartup for IPv6 */
  1543. WSADATA data;
  1544. WSAStartup(MAKEWORD(2, 2), &data);
  1545. #endif
  1546. test_alloc_vprintf(0);
  1547. test_mg_vsnprintf(0);
  1548. test_remove_dot_segments(0);
  1549. test_parse_date_string(0);
  1550. test_parse_port_string(0);
  1551. test_parse_http_message(0);
  1552. test_parse_http_headers(0);
  1553. test_sha1(0);
  1554. #if defined(_WIN32)
  1555. WSACleanup();
  1556. #endif
  1557. }
  1558. #endif