private.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849
  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_mg_match)
  580. {
  581. /* Copyright (c) 2022 the CivetWeb developers */
  582. struct mg_match_context mcx;
  583. ck_assert_int_eq(4, mg_match_alternatives("a*D", 3, "abcde", NULL));
  584. memset(&mcx, 0, sizeof(mcx));
  585. mcx.case_sensitive = 0;
  586. ck_assert_int_eq(4, mg_match_alternatives("a*D", 3, "abcde", &mcx));
  587. ck_assert_int_eq(1, (int)mcx.num_matches);
  588. ck_assert_int_eq(2, (int)mcx.match[0].len);
  589. ck_assert(!memcmp(mcx.match[0].str, "bc", 2));
  590. memset(&mcx, 0, sizeof(mcx));
  591. mcx.case_sensitive = 1;
  592. ck_assert_int_eq(-1, mg_match_alternatives("a*D", 3, "abcde", &mcx));
  593. ck_assert_int_eq(0, (int)mcx.num_matches);
  594. memset(&mcx, 0, sizeof(mcx));
  595. mcx.case_sensitive = 1;
  596. ck_assert_int_eq(4, mg_match_alternatives("a??d", 4, "abcde", &mcx));
  597. ck_assert_int_eq(1, (int)mcx.num_matches);
  598. ck_assert_int_eq(2, (int)mcx.match[0].len);
  599. ck_assert(!memcmp(mcx.match[0].str, "bc", 2));
  600. memset(&mcx, 0, sizeof(mcx));
  601. mcx.case_sensitive = 1;
  602. ck_assert_int_eq(5, mg_match_alternatives("a??d*", 5, "abcde", &mcx));
  603. ck_assert_int_eq(2, (int)mcx.num_matches);
  604. ck_assert_int_eq(2, (int)mcx.match[0].len);
  605. ck_assert(!memcmp(mcx.match[0].str, "bc", 2));
  606. ck_assert_int_eq(1, (int)mcx.match[1].len);
  607. ck_assert(!memcmp(mcx.match[1].str, "e", 1));
  608. memset(&mcx, 0, sizeof(mcx));
  609. mcx.case_sensitive = 1;
  610. ck_assert_int_eq(4, mg_match_alternatives("a??d*", 5, "abcd", &mcx));
  611. ck_assert_int_eq(2, (int)mcx.num_matches);
  612. ck_assert_int_eq(2, (int)mcx.match[0].len);
  613. ck_assert(!memcmp(mcx.match[0].str, "bc", 2));
  614. ck_assert_int_eq(0, (int)mcx.match[1].len);
  615. memset(&mcx, 0, sizeof(mcx));
  616. mcx.case_sensitive = 0;
  617. ck_assert_int_eq(2, mg_match_alternatives("a?|?B", 5, "ABC", &mcx));
  618. ck_assert_int_eq(1, (int)mcx.num_matches);
  619. ck_assert_int_eq(1, (int)mcx.match[0].len);
  620. ck_assert(!memcmp(mcx.match[0].str, "B", 1));
  621. memset(&mcx, 0, sizeof(mcx));
  622. mcx.case_sensitive = 1;
  623. ck_assert_int_eq(2, mg_match_alternatives("a?|?B", 5, "ABC", &mcx));
  624. ck_assert_int_eq(1, (int)mcx.num_matches);
  625. ck_assert_int_eq(1, (int)mcx.match[0].len);
  626. ck_assert(!memcmp(mcx.match[0].str, "A", 1));
  627. }
  628. END_TEST
  629. START_TEST(test_remove_dot_segments)
  630. {
  631. int i;
  632. struct {
  633. const char *input;
  634. const char *expected_output;
  635. } tests[] = {
  636. {"/path/to/file.ext", "/path/to/file.ext"},
  637. {"/file.ext", "/file.ext"},
  638. {"/path/../file.ext", "/file.ext"},
  639. {"/../to/file.ext", "/to/file.ext"},
  640. {"/../../file.ext", "/file.ext"},
  641. {"/./../file.ext", "/file.ext"},
  642. {"/.././file.ext", "/file.ext"},
  643. {"/././file.ext", "/file.ext"},
  644. {"/././file.ext", "/file.ext"},
  645. {"/path/.to/..file.ext", "/path/.to/..file.ext"},
  646. {"/file", "/file"},
  647. {"/path/", "/path/"},
  648. {"file.ext", "file.ext"},
  649. {"./file.ext", "file.ext"},
  650. {"../file.ext", "file.ext"},
  651. {".file.ext", ".file.ext"},
  652. {"..file.ext", "..file.ext"},
  653. {"file", "file"},
  654. {"/x/../", "/"},
  655. {"/x/../../", "/"},
  656. {"/x/.././", "/"},
  657. {"/./x/.././", "/"},
  658. /* Windows specific */
  659. {"\\file.ext", "/file.ext"},
  660. {"\\..\\file.ext", "/file.ext"},
  661. {"/file.", "/file"},
  662. {"/path\\to.\\.\\file.", "/path/to/file"},
  663. /* Multiple dots and slashes */
  664. {"\\//\\\\x", "/x"},
  665. {"//", "/"},
  666. {"/./", "/"},
  667. {"/../", "/"},
  668. {"/.../", "/"},
  669. {"/..../", "/"},
  670. {"/...../", "/"},
  671. {"/...../", "/"},
  672. {"/...//", "/"},
  673. {"/..././", "/"},
  674. {"/.../../", "/"},
  675. {"/.../.../", "/"},
  676. /* Test cases from issues */
  677. {"/foo/bar/baz/../qux.txt", "/foo/bar/qux.txt"},
  678. {"/../alice/bob/../carol/david/frank/../../grace.ext/hugo/../irene.jpg",
  679. "/alice/carol/grace.ext/irene.jpg"},
  680. {"/a/b/..", "/a/"},
  681. {"/a/b/c/../d/../..", "/a/"},
  682. {NULL, NULL}};
  683. mark_point();
  684. for (i = 0; (tests[i].input != NULL); i++) {
  685. char inout[256];
  686. strcpy(inout, tests[i].input);
  687. remove_dot_segments(inout);
  688. ck_assert_str_eq(inout, tests[i].expected_output);
  689. }
  690. }
  691. END_TEST
  692. START_TEST(test_is_valid_uri)
  693. {
  694. /* is_valid_uri is superseeded by get_uri_type */
  695. ck_assert_int_eq(2, get_uri_type("/api"));
  696. ck_assert_int_eq(2, get_uri_type("/api/"));
  697. ck_assert_int_eq(2,
  698. get_uri_type("/some/long/path%20with%20space/file.xyz"));
  699. ck_assert_int_eq(0, get_uri_type("api"));
  700. ck_assert_int_eq(1, get_uri_type("*"));
  701. ck_assert_int_eq(0, get_uri_type("*xy"));
  702. ck_assert_int_eq(3, get_uri_type("http://somewhere/"));
  703. ck_assert_int_eq(3, get_uri_type("https://somewhere/some/file.html"));
  704. ck_assert_int_eq(4, get_uri_type("http://somewhere:8080/"));
  705. ck_assert_int_eq(4, get_uri_type("https://somewhere:8080/some/file.html"));
  706. }
  707. END_TEST
  708. START_TEST(test_next_option)
  709. {
  710. /* Adapted from unit_test.c */
  711. /* Copyright (c) 2013-2015 the Civetweb developers */
  712. /* Copyright (c) 2004-2013 Sergey Lyubka */
  713. const char *p, *list = "x/8,/y**=1;2k,z";
  714. struct vec a, b;
  715. int i;
  716. mark_point();
  717. ck_assert(next_option(NULL, &a, &b) == NULL);
  718. for (i = 0, p = list; (p = next_option(p, &a, &b)) != NULL; i++) {
  719. ck_assert(i != 0 || (a.ptr == list && a.len == 3 && b.len == 0));
  720. ck_assert(i != 1
  721. || (a.ptr == list + 4 && a.len == 4 && b.ptr == list + 9
  722. && b.len == 4));
  723. ck_assert(i != 2 || (a.ptr == list + 14 && a.len == 1 && b.len == 0));
  724. }
  725. }
  726. END_TEST
  727. START_TEST(test_skip_quoted)
  728. {
  729. /* Adapted from unit_test.c */
  730. /* Copyright (c) 2013-2015 the Civetweb developers */
  731. /* Copyright (c) 2004-2013 Sergey Lyubka */
  732. char x[] = "a=1, b=2, c='hi \' there', d='here\\, there'", *s = x, *p;
  733. mark_point();
  734. p = skip_quoted(&s, ", ", ", ", 0);
  735. ck_assert(p != NULL && !strcmp(p, "a=1"));
  736. p = skip_quoted(&s, ", ", ", ", 0);
  737. ck_assert(p != NULL && !strcmp(p, "b=2"));
  738. p = skip_quoted(&s, ",", " ", 0);
  739. ck_assert(p != NULL && !strcmp(p, "c='hi \' there'"));
  740. p = skip_quoted(&s, ",", " ", '\\');
  741. ck_assert(p != NULL && !strcmp(p, "d='here, there'"));
  742. ck_assert(*s == 0);
  743. }
  744. END_TEST
  745. static int
  746. alloc_vprintf_wrapper(char **buf, size_t size, const char *fmt, ...)
  747. {
  748. /* Test helper function - adapted from unit_test.c */
  749. /* Copyright (c) 2013-2015 the Civetweb developers */
  750. /* Copyright (c) 2004-2013 Sergey Lyubka */
  751. va_list ap;
  752. int ret = 0;
  753. mark_point();
  754. va_start(ap, fmt);
  755. ret = alloc_vprintf(buf, *buf, size, fmt, ap);
  756. va_end(ap);
  757. return ret;
  758. }
  759. START_TEST(test_alloc_vprintf)
  760. {
  761. /* Adapted from unit_test.c */
  762. /* Copyright (c) 2013-2015 the Civetweb developers */
  763. /* Copyright (c) 2004-2013 Sergey Lyubka */
  764. char buf[MG_BUF_LEN], *p = buf;
  765. mark_point();
  766. ck_assert(alloc_vprintf_wrapper(&p, sizeof(buf), "%s", "hi") == 2);
  767. ck_assert(p == buf);
  768. ck_assert(alloc_vprintf_wrapper(&p, sizeof(buf), "%s", "") == 0);
  769. ck_assert(p == buf);
  770. ck_assert(alloc_vprintf_wrapper(&p, sizeof(buf), "") == 0);
  771. ck_assert(p == buf);
  772. /* Pass small buffer, make sure alloc_printf allocates */
  773. ck_assert(alloc_vprintf_wrapper(&p, 1, "%s", "hello") == 5);
  774. ck_assert(p != buf);
  775. mg_free(p);
  776. p = buf;
  777. /* Test new wrapper implementation */
  778. ck_assert(alloc_printf(&p, "%s", "hello") == 5);
  779. ck_assert(p != buf);
  780. mg_free(p);
  781. p = buf;
  782. }
  783. END_TEST
  784. START_TEST(test_mg_vsnprintf)
  785. {
  786. char buf[16];
  787. int is_trunc;
  788. #if defined(_WIN32)
  789. /* If the string is truncated, mg_snprintf calls mg_cry.
  790. * If DEBUG is defined, mg_cry calls DEBUG_TRACE.
  791. * In DEBUG_TRACE_FUNC, flockfile(stdout) is called.
  792. * For Windows, flockfile/funlockfile calls
  793. * pthread_mutex_lock/_unlock(&global_log_file_lock).
  794. * So, we need to initialize global_log_file_lock:
  795. */
  796. pthread_mutex_init(&global_log_file_lock, &pthread_mutex_attr);
  797. #endif
  798. memset(buf, 0, sizeof(buf));
  799. mark_point();
  800. is_trunc = 777;
  801. mg_snprintf(NULL, &is_trunc, buf, 10, "%8i", 123);
  802. ck_assert_str_eq(buf, " 123");
  803. ck_assert_int_eq(is_trunc, 0);
  804. is_trunc = 777;
  805. mg_snprintf(NULL, &is_trunc, buf, 10, "%9i", 123);
  806. ck_assert_str_eq(buf, " 123");
  807. ck_assert_int_eq(is_trunc, 0);
  808. is_trunc = 777;
  809. mg_snprintf(NULL, &is_trunc, buf, 9, "%9i", 123);
  810. ck_assert_str_eq(buf, " 12");
  811. ck_assert_int_eq(is_trunc, 1);
  812. is_trunc = 777;
  813. mg_snprintf(NULL, &is_trunc, buf, 8, "%9i", 123);
  814. ck_assert_str_eq(buf, " 1");
  815. ck_assert_int_eq(is_trunc, 1);
  816. is_trunc = 777;
  817. mg_snprintf(NULL, &is_trunc, buf, 7, "%9i", 123);
  818. ck_assert_str_eq(buf, " ");
  819. ck_assert_int_eq(is_trunc, 1);
  820. is_trunc = 777;
  821. strcpy(buf, "1234567890");
  822. mg_snprintf(NULL, &is_trunc, buf, 0, "%i", 543);
  823. ck_assert_str_eq(buf, "1234567890");
  824. ck_assert_int_eq(is_trunc, 1);
  825. }
  826. END_TEST
  827. START_TEST(test_mg_strcasestr)
  828. {
  829. /* Adapted from unit_test.c */
  830. /* Copyright (c) 2013-2015 the Civetweb developers */
  831. /* Copyright (c) 2004-2013 Sergey Lyubka */
  832. static const char *big1 = "abcdef";
  833. mark_point();
  834. ck_assert(mg_strcasestr("Y", "X") == NULL);
  835. ck_assert(mg_strcasestr("Y", "y") != NULL);
  836. ck_assert(mg_strcasestr(big1, "X") == NULL);
  837. ck_assert(mg_strcasestr(big1, "CD") == big1 + 2);
  838. ck_assert(mg_strcasestr("aa", "AAB") == NULL);
  839. }
  840. END_TEST
  841. START_TEST(test_parse_port_string)
  842. {
  843. /* Adapted from unit_test.c */
  844. /* Copyright (c) 2013-2020 the Civetweb developers */
  845. /* Copyright (c) 2004-2013 Sergey Lyubka */
  846. struct t_test_parse_port_string {
  847. const char *port_string;
  848. int valid;
  849. int ip_family;
  850. uint16_t port_num;
  851. };
  852. static struct t_test_parse_port_string testdata[] =
  853. { {"0", 1, 4, 0},
  854. {"1", 1, 4, 1},
  855. {"65535", 1, 4, 65535},
  856. {"65536", 0, 0, 0},
  857. {"1s", 1, 4, 1},
  858. {"1r", 1, 4, 1},
  859. {"1k", 0, 0, 0},
  860. {"1.2.3", 0, 0, 0},
  861. {"1.2.3.", 0, 0, 0},
  862. {"1.2.3.4", 0, 0, 0},
  863. {"1.2.3.4:", 0, 0, 0},
  864. {"1.2.3.4:0", 1, 4, 0},
  865. {"1.2.3.4:1", 1, 4, 1},
  866. {"1.2.3.4:65535", 1, 4, 65535},
  867. {"1.2.3.4:65536", 0, 0, 0},
  868. {"1.2.3.4:1s", 1, 4, 1},
  869. {"1.2.3.4:1r", 1, 4, 1},
  870. {"1.2.3.4:1k", 0, 0, 0},
  871. #if defined(USE_IPV6)
  872. /* IPv6 config */
  873. {"[::1]:123", 1, 6, 123},
  874. {"[::]:80", 1, 6, 80},
  875. {"[3ffe:2a00:100:7031::1]:900", 1, 6, 900},
  876. /* IPv4 + IPv6 config */
  877. {"+80", 1, 4 + 6, 80},
  878. #else
  879. /* IPv6 config: invalid if IPv6 is not activated */
  880. {"[::1]:123", 0, 0, 123},
  881. {"[::]:80", 0, 0, 80},
  882. {"[3ffe:2a00:100:7031::1]:900", 0, 0, 900},
  883. /* IPv4 + IPv6 config: only IPv4 if IPv6 is not activated */
  884. {"+80", 1, 4, 80},
  885. #endif
  886. {NULL, 0, 0, 0} };
  887. struct socket so;
  888. struct vec vec;
  889. int ip_family;
  890. int i, ret;
  891. mark_point();
  892. for (i = 0; testdata[i].port_string != NULL; i++) {
  893. vec.ptr = testdata[i].port_string;
  894. vec.len = strlen(vec.ptr);
  895. ip_family = 123;
  896. ret = parse_port_string(&vec, &so, &ip_family);
  897. if ((ret != testdata[i].valid)
  898. || (ip_family != testdata[i].ip_family)) {
  899. ck_abort_msg("Port string [%s]: "
  900. "expected valid=%i, family=%i; "
  901. "got valid=%i, family=%i",
  902. testdata[i].port_string,
  903. testdata[i].valid,
  904. testdata[i].ip_family,
  905. ret,
  906. ip_family);
  907. }
  908. if (ip_family == 4) {
  909. ck_assert_int_eq((int)so.lsa.sin.sin_family, (int)AF_INET);
  910. }
  911. if (ip_family == 6) {
  912. ck_assert_int_eq((int)so.lsa.sin.sin_family, (int)AF_INET6);
  913. }
  914. if (ret) {
  915. /* Test valid strings only */
  916. ck_assert_int_eq(htons(so.lsa.sin.sin_port), testdata[i].port_num);
  917. }
  918. }
  919. /* special case: localhost can be ipv4 or ipv6 */
  920. vec.ptr = "localhost:123";
  921. vec.len = strlen(vec.ptr);
  922. ret = parse_port_string(&vec, &so, &ip_family);
  923. if (ret != 1) {
  924. ck_abort_msg("IP of localhost seems to be unknown on this system (%i)",
  925. (int)ret);
  926. }
  927. if ((ip_family != 4) && (ip_family != 6)) {
  928. ck_abort_msg("IP family for localhost must be 4 or 6 but is %i",
  929. (int)ip_family);
  930. }
  931. ck_assert_int_eq((int)htons(so.lsa.sin.sin_port), (int)123);
  932. }
  933. END_TEST
  934. START_TEST(test_parse_http_headers)
  935. {
  936. char buf[2048];
  937. char *ptr;
  938. int ret;
  939. struct mg_header hdr[MG_MAX_HEADERS];
  940. memset(hdr, 0, sizeof(hdr));
  941. memset(buf, 0, sizeof(buf));
  942. ptr = &buf[0];
  943. ret = parse_http_headers(&ptr, hdr);
  944. ck_assert_int_eq(ret, 0);
  945. ck_assert_ptr_eq(ptr, &buf[0]);
  946. ck_assert_ptr_eq(hdr[0].name, NULL);
  947. ck_assert_ptr_eq(hdr[0].value, NULL);
  948. ck_assert_ptr_eq(hdr[1].name, NULL);
  949. ck_assert_ptr_eq(hdr[1].value, NULL);
  950. ck_assert_ptr_eq(hdr[2].name, NULL);
  951. ck_assert_ptr_eq(hdr[2].value, NULL);
  952. memset(hdr, 0, sizeof(hdr));
  953. memset(buf, 0, sizeof(buf));
  954. strcpy(buf, "\r\n");
  955. ptr = &buf[0];
  956. ret = parse_http_headers(&ptr, hdr);
  957. ck_assert_int_eq(ret, 0);
  958. ck_assert_ptr_eq(ptr, &buf[0]);
  959. ck_assert_ptr_eq(hdr[0].name, NULL);
  960. ck_assert_ptr_eq(hdr[0].value, NULL);
  961. ck_assert_ptr_eq(hdr[1].name, NULL);
  962. ck_assert_ptr_eq(hdr[1].value, NULL);
  963. ck_assert_ptr_eq(hdr[2].name, NULL);
  964. ck_assert_ptr_eq(hdr[2].value, NULL);
  965. memset(hdr, 0, sizeof(hdr));
  966. memset(buf, 0, sizeof(buf));
  967. strcpy(buf, "a\r\n");
  968. ptr = &buf[0];
  969. ret = parse_http_headers(&ptr, hdr);
  970. ck_assert_int_eq(ret, -1);
  971. ck_assert_ptr_eq(ptr, &buf[0]);
  972. ck_assert_ptr_eq(hdr[0].name, NULL);
  973. ck_assert_ptr_eq(hdr[0].value, NULL);
  974. ck_assert_ptr_eq(hdr[1].name, NULL);
  975. ck_assert_ptr_eq(hdr[1].value, NULL);
  976. ck_assert_ptr_eq(hdr[2].name, NULL);
  977. ck_assert_ptr_eq(hdr[2].value, NULL);
  978. memset(hdr, 0, sizeof(hdr));
  979. memset(buf, 0, sizeof(buf));
  980. strcpy(buf, "a:b");
  981. ptr = &buf[0];
  982. ret = parse_http_headers(&ptr, hdr);
  983. ck_assert_int_eq(ret, 1);
  984. ck_assert_ptr_eq(ptr, &buf[3]);
  985. ck_assert_str_eq(hdr[0].name, "a");
  986. ck_assert_str_eq(hdr[0].value, "b");
  987. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  988. ck_assert_ptr_eq(hdr[0].value, &buf[2]);
  989. ck_assert_ptr_eq(hdr[1].name, NULL);
  990. ck_assert_ptr_eq(hdr[1].value, NULL);
  991. ck_assert_ptr_eq(hdr[2].name, NULL);
  992. ck_assert_ptr_eq(hdr[2].value, NULL);
  993. memset(hdr, 0, sizeof(hdr));
  994. memset(buf, 0, sizeof(buf));
  995. strcpy(buf, "a:b\r\n");
  996. ptr = &buf[0];
  997. ret = parse_http_headers(&ptr, hdr);
  998. ck_assert_int_eq(ret, 1);
  999. ck_assert_ptr_eq(ptr, &buf[5]);
  1000. ck_assert_str_eq(hdr[0].name, "a");
  1001. ck_assert_str_eq(hdr[0].value, "b");
  1002. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  1003. ck_assert_ptr_eq(hdr[0].value, &buf[2]);
  1004. ck_assert_ptr_eq(hdr[1].name, NULL);
  1005. ck_assert_ptr_eq(hdr[1].value, NULL);
  1006. ck_assert_ptr_eq(hdr[2].name, NULL);
  1007. ck_assert_ptr_eq(hdr[2].value, NULL);
  1008. memset(hdr, 0, sizeof(hdr));
  1009. memset(buf, 0, sizeof(buf));
  1010. strcpy(buf, "a:b\r\n\r\n");
  1011. ptr = &buf[0];
  1012. ret = parse_http_headers(&ptr, hdr);
  1013. ck_assert_int_eq(ret, 1);
  1014. ck_assert_ptr_eq(ptr, &buf[5]);
  1015. ck_assert_str_eq(hdr[0].name, "a");
  1016. ck_assert_str_eq(hdr[0].value, "b");
  1017. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  1018. ck_assert_ptr_eq(hdr[0].value, &buf[2]);
  1019. ck_assert_ptr_eq(hdr[1].name, NULL);
  1020. ck_assert_ptr_eq(hdr[1].value, NULL);
  1021. ck_assert_ptr_eq(hdr[2].name, NULL);
  1022. ck_assert_ptr_eq(hdr[2].value, NULL);
  1023. memset(hdr, 0, sizeof(hdr));
  1024. memset(buf, 0, sizeof(buf));
  1025. strcpy(buf, "a: b\r\n");
  1026. ptr = &buf[0];
  1027. ret = parse_http_headers(&ptr, hdr);
  1028. ck_assert_int_eq(ret, 1);
  1029. ck_assert_ptr_eq(ptr, &buf[6]);
  1030. ck_assert_str_eq(hdr[0].name, "a");
  1031. ck_assert_str_eq(hdr[0].value, "b");
  1032. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  1033. ck_assert_ptr_eq(hdr[0].value, &buf[3]);
  1034. ck_assert_ptr_eq(hdr[1].name, NULL);
  1035. ck_assert_ptr_eq(hdr[1].value, NULL);
  1036. ck_assert_ptr_eq(hdr[2].name, NULL);
  1037. ck_assert_ptr_eq(hdr[2].value, NULL);
  1038. memset(hdr, 0, sizeof(hdr));
  1039. memset(buf, 0, sizeof(buf));
  1040. strcpy(buf, "a :b\r\n");
  1041. ptr = &buf[0];
  1042. ret = parse_http_headers(&ptr, hdr);
  1043. ck_assert_int_eq(ret, 1);
  1044. ck_assert_ptr_eq(ptr, &buf[6]);
  1045. ck_assert_str_eq(hdr[0].name, "a");
  1046. ck_assert_str_eq(hdr[0].value, "b");
  1047. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  1048. ck_assert_ptr_eq(hdr[0].value, &buf[3]);
  1049. ck_assert_ptr_eq(hdr[1].name, NULL);
  1050. ck_assert_ptr_eq(hdr[1].value, NULL);
  1051. ck_assert_ptr_eq(hdr[2].name, NULL);
  1052. ck_assert_ptr_eq(hdr[2].value, NULL);
  1053. memset(hdr, 0, sizeof(hdr));
  1054. memset(buf, 0, sizeof(buf));
  1055. strcpy(buf, "a : b\r\n");
  1056. ptr = &buf[0];
  1057. ret = parse_http_headers(&ptr, hdr);
  1058. ck_assert_int_eq(ret, 1);
  1059. ck_assert_ptr_eq(ptr, &buf[7]);
  1060. ck_assert_str_eq(hdr[0].name, "a");
  1061. ck_assert_str_eq(hdr[0].value, "b");
  1062. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  1063. ck_assert_ptr_eq(hdr[0].value, &buf[4]);
  1064. ck_assert_ptr_eq(hdr[1].name, NULL);
  1065. ck_assert_ptr_eq(hdr[1].value, NULL);
  1066. ck_assert_ptr_eq(hdr[2].name, NULL);
  1067. ck_assert_ptr_eq(hdr[2].value, NULL);
  1068. memset(hdr, 0, sizeof(hdr));
  1069. memset(buf, 0, sizeof(buf));
  1070. strcpy(buf, "a: b\r\nc: d\r\n");
  1071. ptr = &buf[0];
  1072. ret = parse_http_headers(&ptr, hdr);
  1073. ck_assert_int_eq(ret, 2);
  1074. ck_assert_ptr_eq(ptr, &buf[12]);
  1075. ck_assert_str_eq(hdr[0].name, "a");
  1076. ck_assert_str_eq(hdr[0].value, "b");
  1077. ck_assert_str_eq(hdr[1].name, "c");
  1078. ck_assert_str_eq(hdr[1].value, "d");
  1079. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  1080. ck_assert_ptr_eq(hdr[0].value, &buf[3]);
  1081. ck_assert_ptr_eq(hdr[1].name, &buf[6]);
  1082. ck_assert_ptr_eq(hdr[1].value, &buf[9]);
  1083. ck_assert_ptr_eq(hdr[2].name, NULL);
  1084. ck_assert_ptr_eq(hdr[2].value, NULL);
  1085. memset(hdr, 0, sizeof(hdr));
  1086. memset(buf, 0, sizeof(buf));
  1087. strcpy(buf, "a: b\r\nc: d\r\n\r\n");
  1088. ptr = &buf[0];
  1089. ret = parse_http_headers(&ptr, hdr);
  1090. ck_assert_int_eq(ret, 2);
  1091. ck_assert_ptr_eq(ptr, &buf[12]);
  1092. ck_assert_str_eq(hdr[0].name, "a");
  1093. ck_assert_str_eq(hdr[0].value, "b");
  1094. ck_assert_str_eq(hdr[1].name, "c");
  1095. ck_assert_str_eq(hdr[1].value, "d");
  1096. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  1097. ck_assert_ptr_eq(hdr[0].value, &buf[3]);
  1098. ck_assert_ptr_eq(hdr[1].name, &buf[6]);
  1099. ck_assert_ptr_eq(hdr[1].value, &buf[9]);
  1100. ck_assert_ptr_eq(hdr[2].name, NULL);
  1101. ck_assert_ptr_eq(hdr[2].value, NULL);
  1102. memset(hdr, 0, sizeof(hdr));
  1103. memset(buf, 0, sizeof(buf));
  1104. strcpy(buf, "a: b\r\nc: d\r\n\r\ne: f\r\n");
  1105. ptr = &buf[0];
  1106. ret = parse_http_headers(&ptr, hdr);
  1107. ck_assert_int_eq(ret, 2);
  1108. ck_assert_ptr_eq(ptr, &buf[12]);
  1109. ck_assert_str_eq(hdr[0].name, "a");
  1110. ck_assert_str_eq(hdr[0].value, "b");
  1111. ck_assert_str_eq(hdr[1].name, "c");
  1112. ck_assert_str_eq(hdr[1].value, "d");
  1113. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  1114. ck_assert_ptr_eq(hdr[0].value, &buf[3]);
  1115. ck_assert_ptr_eq(hdr[1].name, &buf[6]);
  1116. ck_assert_ptr_eq(hdr[1].value, &buf[9]);
  1117. ck_assert_ptr_eq(hdr[2].name, NULL);
  1118. ck_assert_ptr_eq(hdr[2].value, NULL);
  1119. memset(hdr, 0, sizeof(hdr));
  1120. memset(buf, 0, sizeof(buf));
  1121. strcpy(buf, "a: b\r\nc: d");
  1122. ptr = &buf[0];
  1123. ret = parse_http_headers(&ptr, hdr);
  1124. ck_assert_int_eq(ret, 2);
  1125. ck_assert_ptr_eq(ptr, &buf[10]);
  1126. ck_assert_str_eq(hdr[0].name, "a");
  1127. ck_assert_str_eq(hdr[0].value, "b");
  1128. ck_assert_str_eq(hdr[1].name, "c");
  1129. ck_assert_str_eq(hdr[1].value, "d");
  1130. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  1131. ck_assert_ptr_eq(hdr[0].value, &buf[3]);
  1132. ck_assert_ptr_eq(hdr[1].name, &buf[6]);
  1133. ck_assert_ptr_eq(hdr[1].value, &buf[9]);
  1134. ck_assert_ptr_eq(hdr[2].name, NULL);
  1135. ck_assert_ptr_eq(hdr[2].value, NULL);
  1136. memset(hdr, 0, sizeof(hdr));
  1137. memset(buf, 0, sizeof(buf));
  1138. strcpy(buf, "a: b\r\nc: d\r\ne");
  1139. ptr = &buf[0];
  1140. ret = parse_http_headers(&ptr, hdr);
  1141. ck_assert_int_eq(ret, -1);
  1142. /* The following could be undefined, since ret == -1 */
  1143. ck_assert_ptr_eq(ptr, &buf[12]);
  1144. ck_assert_str_eq(hdr[0].name, "a");
  1145. ck_assert_str_eq(hdr[0].value, "b");
  1146. ck_assert_str_eq(hdr[1].name, "c");
  1147. ck_assert_str_eq(hdr[1].value, "d");
  1148. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  1149. ck_assert_ptr_eq(hdr[0].value, &buf[3]);
  1150. ck_assert_ptr_eq(hdr[1].name, &buf[6]);
  1151. ck_assert_ptr_eq(hdr[1].value, &buf[9]);
  1152. ck_assert_ptr_eq(hdr[2].name, NULL);
  1153. ck_assert_ptr_eq(hdr[2].value, NULL);
  1154. memset(hdr, 0, sizeof(hdr));
  1155. memset(buf, 0, sizeof(buf));
  1156. strcpy(buf, "a: b\r\nc: d\r\nefg:");
  1157. ptr = &buf[0];
  1158. ret = parse_http_headers(&ptr, hdr);
  1159. ck_assert_int_eq(ret, 3);
  1160. ck_assert_ptr_eq(ptr, &buf[16]);
  1161. ck_assert_str_eq(hdr[0].name, "a");
  1162. ck_assert_str_eq(hdr[0].value, "b");
  1163. ck_assert_str_eq(hdr[1].name, "c");
  1164. ck_assert_str_eq(hdr[1].value, "d");
  1165. ck_assert_str_eq(hdr[2].name, "efg");
  1166. ck_assert_str_eq(hdr[2].value, "");
  1167. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  1168. ck_assert_ptr_eq(hdr[0].value, &buf[3]);
  1169. ck_assert_ptr_eq(hdr[1].name, &buf[6]);
  1170. ck_assert_ptr_eq(hdr[1].value, &buf[9]);
  1171. ck_assert_ptr_eq(hdr[2].name, &buf[12]);
  1172. ck_assert_ptr_eq(hdr[2].value, &buf[16]);
  1173. }
  1174. END_TEST
  1175. START_TEST(test_encode_decode)
  1176. {
  1177. char buf[128];
  1178. const char *alpha = "abcdefghijklmnopqrstuvwxyz";
  1179. const char *nonalpha = " !\"#$%&'()*+,-./0123456789:;<=>?@";
  1180. const char *nonalpha_url_enc1 =
  1181. "%20%21%22%23$%25%26%27()%2a%2b,-.%2f0123456789%3a;%3c%3d%3e%3f%40";
  1182. const char *nonalpha_url_enc2 =
  1183. "%20!%22%23%24%25%26'()*%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40";
  1184. int ret;
  1185. size_t len;
  1186. mark_point();
  1187. memset(buf, 77, sizeof(buf));
  1188. ret = mg_url_encode(alpha, buf, sizeof(buf));
  1189. ck_assert_int_eq(ret, (int)strlen(buf));
  1190. ck_assert_int_eq(ret, (int)strlen(alpha));
  1191. ck_assert_str_eq(buf, alpha);
  1192. memset(buf, 77, sizeof(buf));
  1193. ret = mg_url_encode(nonalpha, buf, sizeof(buf));
  1194. ck_assert_int_eq(ret, (int)strlen(buf));
  1195. ck_assert_int_eq(ret, (int)strlen(nonalpha_url_enc1));
  1196. ck_assert_str_eq(buf, nonalpha_url_enc1);
  1197. memset(buf, 77, sizeof(buf));
  1198. ret = mg_url_decode(alpha, (int)strlen(alpha), buf, sizeof(buf), 0);
  1199. ck_assert_int_eq(ret, (int)strlen(buf));
  1200. ck_assert_int_eq(ret, (int)strlen(alpha));
  1201. ck_assert_str_eq(buf, alpha);
  1202. memset(buf, 77, sizeof(buf));
  1203. ret = mg_url_decode(
  1204. nonalpha_url_enc1, (int)strlen(nonalpha_url_enc1), buf, sizeof(buf), 0);
  1205. ck_assert_int_eq(ret, (int)strlen(buf));
  1206. ck_assert_int_eq(ret, (int)strlen(nonalpha));
  1207. ck_assert_str_eq(buf, nonalpha);
  1208. memset(buf, 77, sizeof(buf));
  1209. ret = mg_url_decode(
  1210. nonalpha_url_enc2, (int)strlen(nonalpha_url_enc2), buf, sizeof(buf), 0);
  1211. ck_assert_int_eq(ret, (int)strlen(buf));
  1212. ck_assert_int_eq(ret, (int)strlen(nonalpha));
  1213. ck_assert_str_eq(buf, nonalpha);
  1214. /* Test url_decode_in_place */
  1215. strcpy(buf, "AbcDef");
  1216. url_decode_in_place(buf);
  1217. ck_assert_str_eq(buf, "AbcDef");
  1218. strcpy(buf, "A%20B+C");
  1219. url_decode_in_place(buf);
  1220. ck_assert_str_eq(buf, "A B C");
  1221. /* len could be unused, if base64_decode is not tested because USE_LUA is
  1222. * not defined */
  1223. (void)len;
  1224. }
  1225. END_TEST
  1226. START_TEST(test_mask_data)
  1227. {
  1228. #if defined(USE_WEBSOCKET)
  1229. char in[1024];
  1230. char out[1024];
  1231. int i;
  1232. #endif
  1233. uint32_t mask = 0x61626364;
  1234. /* TODO: adapt test for big endian */
  1235. ck_assert((*(unsigned char *)&mask) == 0x64u);
  1236. #if defined(USE_WEBSOCKET)
  1237. memset(in, 0, sizeof(in));
  1238. memset(out, 99, sizeof(out));
  1239. mask_data(in, sizeof(out), 0, out);
  1240. ck_assert(!memcmp(out, in, sizeof(out)));
  1241. for (i = 0; i < 1024; i++) {
  1242. in[i] = (char)((unsigned char)i);
  1243. }
  1244. mask_data(in, 107, 0, out);
  1245. ck_assert(!memcmp(out, in, 107));
  1246. mask_data(in, 256, 0x01010101, out);
  1247. for (i = 0; i < 256; i++) {
  1248. ck_assert_int_eq((int)((unsigned char)out[i]),
  1249. (int)(((unsigned char)in[i]) ^ (char)1u));
  1250. }
  1251. for (i = 256; i < (int)sizeof(out); i++) {
  1252. ck_assert_int_eq((int)((unsigned char)out[i]), (int)0);
  1253. }
  1254. /* TODO: check this for big endian */
  1255. mask_data(in, 5, 0x01020304, out);
  1256. ck_assert_uint_eq((unsigned char)out[0], 0u ^ 4u);
  1257. ck_assert_uint_eq((unsigned char)out[1], 1u ^ 3u);
  1258. ck_assert_uint_eq((unsigned char)out[2], 2u ^ 2u);
  1259. ck_assert_uint_eq((unsigned char)out[3], 3u ^ 1u);
  1260. ck_assert_uint_eq((unsigned char)out[4], 4u ^ 4u);
  1261. #endif
  1262. }
  1263. END_TEST
  1264. START_TEST(test_parse_date_string)
  1265. {
  1266. #if !defined(NO_CACHING)
  1267. time_t now = time(0);
  1268. struct tm *tm = gmtime(&now);
  1269. char date[64] = {0};
  1270. unsigned long i;
  1271. ck_assert_uint_eq((unsigned long)parse_date_string("1/Jan/1970 00:01:02"),
  1272. 62ul);
  1273. ck_assert_uint_eq((unsigned long)parse_date_string("1 Jan 1970 00:02:03"),
  1274. 123ul);
  1275. ck_assert_uint_eq((unsigned long)parse_date_string("1-Jan-1970 00:03:04"),
  1276. 184ul);
  1277. ck_assert_uint_eq((unsigned long)parse_date_string(
  1278. "Xyz, 1 Jan 1970 00:04:05"),
  1279. 245ul);
  1280. gmt_time_string(date, sizeof(date), &now);
  1281. ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
  1282. sprintf(date,
  1283. "%02u %s %04u %02u:%02u:%02u",
  1284. (unsigned int)tm->tm_mday,
  1285. month_names[tm->tm_mon],
  1286. (unsigned int)(tm->tm_year + 1900),
  1287. (unsigned int)tm->tm_hour,
  1288. (unsigned int)tm->tm_min,
  1289. (unsigned int)tm->tm_sec);
  1290. ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
  1291. gmt_time_string(date, 1, NULL);
  1292. ck_assert_str_eq(date, "");
  1293. gmt_time_string(date, 6, NULL);
  1294. ck_assert_str_eq(date,
  1295. "Thu, "); /* part of "Thu, 01 Jan 1970 00:00:00 GMT" */
  1296. gmt_time_string(date, sizeof(date), NULL);
  1297. ck_assert_str_eq(date, "Thu, 01 Jan 1970 00:00:00 GMT");
  1298. for (i = 2ul; i < 0x8000000ul; i += i / 2) {
  1299. now = (time_t)i;
  1300. gmt_time_string(date, sizeof(date), &now);
  1301. ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
  1302. tm = gmtime(&now);
  1303. sprintf(date,
  1304. "%02u-%s-%04u %02u:%02u:%02u",
  1305. (unsigned int)tm->tm_mday,
  1306. month_names[tm->tm_mon],
  1307. (unsigned int)(tm->tm_year + 1900),
  1308. (unsigned int)tm->tm_hour,
  1309. (unsigned int)tm->tm_min,
  1310. (unsigned int)tm->tm_sec);
  1311. ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
  1312. }
  1313. #endif
  1314. }
  1315. END_TEST
  1316. START_TEST(test_sha1)
  1317. {
  1318. #ifdef SHA1_DIGEST_SIZE
  1319. SHA_CTX sha_ctx;
  1320. uint8_t digest[SHA1_DIGEST_SIZE] = {0};
  1321. char str[48] = {0};
  1322. int i;
  1323. const char *test_str;
  1324. ck_assert_uint_eq(sizeof(digest), 20);
  1325. ck_assert_uint_gt(sizeof(str), sizeof(digest) * 2 + 1);
  1326. /* empty string */
  1327. SHA1_Init(&sha_ctx);
  1328. SHA1_Final(digest, &sha_ctx);
  1329. bin2str(str, digest, sizeof(digest));
  1330. ck_assert_uint_eq(strlen(str), 40);
  1331. ck_assert_str_eq(str, "da39a3ee5e6b4b0d3255bfef95601890afd80709");
  1332. /* empty string */
  1333. SHA1_Init(&sha_ctx);
  1334. SHA1_Update(&sha_ctx, (uint8_t *)"abc", 0);
  1335. SHA1_Final(digest, &sha_ctx);
  1336. bin2str(str, digest, sizeof(digest));
  1337. ck_assert_uint_eq(strlen(str), 40);
  1338. ck_assert_str_eq(str, "da39a3ee5e6b4b0d3255bfef95601890afd80709");
  1339. /* "abc" */
  1340. SHA1_Init(&sha_ctx);
  1341. SHA1_Update(&sha_ctx, (uint8_t *)"abc", 3);
  1342. SHA1_Final(digest, &sha_ctx);
  1343. bin2str(str, digest, sizeof(digest));
  1344. ck_assert_uint_eq(strlen(str), 40);
  1345. ck_assert_str_eq(str, "a9993e364706816aba3e25717850c26c9cd0d89d");
  1346. /* "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" */
  1347. test_str = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  1348. SHA1_Init(&sha_ctx);
  1349. SHA1_Update(&sha_ctx, (uint8_t *)test_str, (uint32_t)strlen(test_str));
  1350. SHA1_Final(digest, &sha_ctx);
  1351. bin2str(str, digest, sizeof(digest));
  1352. ck_assert_uint_eq(strlen(str), 40);
  1353. ck_assert_str_eq(str, "84983e441c3bd26ebaae4aa1f95129e5e54670f1");
  1354. /* a million "a" */
  1355. SHA1_Init(&sha_ctx);
  1356. for (i = 0; i < 1000000; i++) {
  1357. SHA1_Update(&sha_ctx, (uint8_t *)"a", 1);
  1358. }
  1359. SHA1_Final(digest, &sha_ctx);
  1360. bin2str(str, digest, sizeof(digest));
  1361. ck_assert_uint_eq(strlen(str), 40);
  1362. ck_assert_str_eq(str, "34aa973cd4c4daa4f61eeb2bdbad27316534016f");
  1363. /* a million "a" in blocks of 10 */
  1364. SHA1_Init(&sha_ctx);
  1365. for (i = 0; i < 100000; i++) {
  1366. SHA1_Update(&sha_ctx, (uint8_t *)"aaaaaaaaaa", 10);
  1367. }
  1368. SHA1_Final(digest, &sha_ctx);
  1369. bin2str(str, digest, sizeof(digest));
  1370. ck_assert_uint_eq(strlen(str), 40);
  1371. ck_assert_str_eq(str, "34aa973cd4c4daa4f61eeb2bdbad27316534016f");
  1372. #else
  1373. /* Can not test, if SHA1 is not included */
  1374. ck_assert(1);
  1375. #endif
  1376. }
  1377. END_TEST
  1378. START_TEST(test_config_options)
  1379. {
  1380. /* Check size of config_options vs. number of options in enum. */
  1381. ck_assert_ptr_eq(NULL, config_options[NUM_OPTIONS].name);
  1382. ck_assert_int_eq((int)MG_CONFIG_TYPE_UNKNOWN,
  1383. config_options[NUM_OPTIONS].type);
  1384. ck_assert_uint_eq(sizeof(config_options) / sizeof(config_options[0]),
  1385. (size_t)(NUM_OPTIONS + 1));
  1386. /* Check option enums vs. option names. */
  1387. /* Check if the order in
  1388. * static struct mg_option config_options[]
  1389. * is the same as in the option enum
  1390. * This test allows to reorder config_options and the enum,
  1391. * and check if the order is still consistent. */
  1392. ck_assert_str_eq("cgi_pattern", config_options[CGI_EXTENSIONS].name);
  1393. ck_assert_str_eq("cgi_environment", config_options[CGI_ENVIRONMENT].name);
  1394. ck_assert_str_eq("put_delete_auth_file",
  1395. config_options[PUT_DELETE_PASSWORDS_FILE].name);
  1396. ck_assert_str_eq("cgi_interpreter", config_options[CGI_INTERPRETER].name);
  1397. ck_assert_str_eq("protect_uri", config_options[PROTECT_URI].name);
  1398. ck_assert_str_eq("authentication_domain",
  1399. config_options[AUTHENTICATION_DOMAIN].name);
  1400. ck_assert_str_eq("enable_auth_domain_check",
  1401. config_options[ENABLE_AUTH_DOMAIN_CHECK].name);
  1402. ck_assert_str_eq("ssi_pattern", config_options[SSI_EXTENSIONS].name);
  1403. ck_assert_str_eq("throttle", config_options[THROTTLE].name);
  1404. ck_assert_str_eq("access_log_file", config_options[ACCESS_LOG_FILE].name);
  1405. ck_assert_str_eq("enable_directory_listing",
  1406. config_options[ENABLE_DIRECTORY_LISTING].name);
  1407. ck_assert_str_eq("error_log_file", config_options[ERROR_LOG_FILE].name);
  1408. ck_assert_str_eq("global_auth_file",
  1409. config_options[GLOBAL_PASSWORDS_FILE].name);
  1410. ck_assert_str_eq("index_files", config_options[INDEX_FILES].name);
  1411. ck_assert_str_eq("enable_keep_alive",
  1412. config_options[ENABLE_KEEP_ALIVE].name);
  1413. ck_assert_str_eq("access_control_list",
  1414. config_options[ACCESS_CONTROL_LIST].name);
  1415. ck_assert_str_eq("extra_mime_types", config_options[EXTRA_MIME_TYPES].name);
  1416. ck_assert_str_eq("listening_ports", config_options[LISTENING_PORTS].name);
  1417. ck_assert_str_eq("document_root", config_options[DOCUMENT_ROOT].name);
  1418. ck_assert_str_eq("ssl_certificate", config_options[SSL_CERTIFICATE].name);
  1419. ck_assert_str_eq("ssl_certificate_chain",
  1420. config_options[SSL_CERTIFICATE_CHAIN].name);
  1421. ck_assert_str_eq("num_threads", config_options[NUM_THREADS].name);
  1422. ck_assert_str_eq("run_as_user", config_options[RUN_AS_USER].name);
  1423. ck_assert_str_eq("url_rewrite_patterns",
  1424. config_options[URL_REWRITE_PATTERN].name);
  1425. ck_assert_str_eq("hide_files_patterns", config_options[HIDE_FILES].name);
  1426. ck_assert_str_eq("request_timeout_ms",
  1427. config_options[REQUEST_TIMEOUT].name);
  1428. ck_assert_str_eq("keep_alive_timeout_ms",
  1429. config_options[KEEP_ALIVE_TIMEOUT].name);
  1430. ck_assert_str_eq("linger_timeout_ms", config_options[LINGER_TIMEOUT].name);
  1431. ck_assert_str_eq("listen_backlog",
  1432. config_options[LISTEN_BACKLOG_SIZE].name);
  1433. ck_assert_str_eq("ssl_verify_peer",
  1434. config_options[SSL_DO_VERIFY_PEER].name);
  1435. ck_assert_str_eq("ssl_ca_path", config_options[SSL_CA_PATH].name);
  1436. ck_assert_str_eq("ssl_ca_file", config_options[SSL_CA_FILE].name);
  1437. ck_assert_str_eq("ssl_verify_depth", config_options[SSL_VERIFY_DEPTH].name);
  1438. ck_assert_str_eq("ssl_default_verify_paths",
  1439. config_options[SSL_DEFAULT_VERIFY_PATHS].name);
  1440. ck_assert_str_eq("ssl_cipher_list", config_options[SSL_CIPHER_LIST].name);
  1441. ck_assert_str_eq("ssl_protocol_version",
  1442. config_options[SSL_PROTOCOL_VERSION].name);
  1443. ck_assert_str_eq("ssl_short_trust", config_options[SSL_SHORT_TRUST].name);
  1444. #if defined(USE_WEBSOCKET)
  1445. ck_assert_str_eq("websocket_timeout_ms",
  1446. config_options[WEBSOCKET_TIMEOUT].name);
  1447. ck_assert_str_eq("enable_websocket_ping_pong",
  1448. config_options[ENABLE_WEBSOCKET_PING_PONG].name);
  1449. #endif
  1450. ck_assert_str_eq("decode_url", config_options[DECODE_URL].name);
  1451. ck_assert_str_eq("decode_query_string",
  1452. config_options[DECODE_QUERY_STRING].name);
  1453. #if defined(USE_LUA)
  1454. ck_assert_str_eq("lua_preload_file", config_options[LUA_PRELOAD_FILE].name);
  1455. ck_assert_str_eq("lua_script_pattern",
  1456. config_options[LUA_SCRIPT_EXTENSIONS].name);
  1457. ck_assert_str_eq("lua_server_page_pattern",
  1458. config_options[LUA_SERVER_PAGE_EXTENSIONS].name);
  1459. #endif
  1460. #if defined(USE_DUKTAPE)
  1461. ck_assert_str_eq("duktape_script_pattern",
  1462. config_options[DUKTAPE_SCRIPT_EXTENSIONS].name);
  1463. #endif
  1464. #if defined(USE_WEBSOCKET)
  1465. ck_assert_str_eq("websocket_root", config_options[WEBSOCKET_ROOT].name);
  1466. #endif
  1467. #if defined(USE_LUA) && defined(USE_WEBSOCKET)
  1468. ck_assert_str_eq("lua_websocket_pattern",
  1469. config_options[LUA_WEBSOCKET_EXTENSIONS].name);
  1470. #endif
  1471. ck_assert_str_eq("access_control_allow_origin",
  1472. config_options[ACCESS_CONTROL_ALLOW_ORIGIN].name);
  1473. ck_assert_str_eq("access_control_allow_methods",
  1474. config_options[ACCESS_CONTROL_ALLOW_METHODS].name);
  1475. ck_assert_str_eq("access_control_allow_headers",
  1476. config_options[ACCESS_CONTROL_ALLOW_HEADERS].name);
  1477. ck_assert_str_eq("error_pages", config_options[ERROR_PAGES].name);
  1478. ck_assert_str_eq("tcp_nodelay", config_options[CONFIG_TCP_NODELAY].name);
  1479. #if !defined(NO_CACHING)
  1480. ck_assert_str_eq("static_file_max_age",
  1481. config_options[STATIC_FILE_MAX_AGE].name);
  1482. #endif
  1483. #if !defined(NO_SSL)
  1484. ck_assert_str_eq("strict_transport_security_max_age",
  1485. config_options[STRICT_HTTPS_MAX_AGE].name);
  1486. #endif
  1487. #if defined(__linux__)
  1488. ck_assert_str_eq("allow_sendfile_call",
  1489. config_options[ALLOW_SENDFILE_CALL].name);
  1490. #endif
  1491. #if defined(_WIN32)
  1492. ck_assert_str_eq("case_sensitive",
  1493. config_options[CASE_SENSITIVE_FILES].name);
  1494. #endif
  1495. #if defined(USE_LUA)
  1496. ck_assert_str_eq("lua_background_script",
  1497. config_options[LUA_BACKGROUND_SCRIPT].name);
  1498. ck_assert_str_eq("lua_background_script_params",
  1499. config_options[LUA_BACKGROUND_SCRIPT_PARAMS].name);
  1500. #endif
  1501. ck_assert_str_eq("additional_header",
  1502. config_options[ADDITIONAL_HEADER].name);
  1503. ck_assert_str_eq("max_request_size", config_options[MAX_REQUEST_SIZE].name);
  1504. ck_assert_str_eq("allow_index_script_resource",
  1505. config_options[ALLOW_INDEX_SCRIPT_SUB_RES].name);
  1506. }
  1507. END_TEST
  1508. #if !defined(REPLACE_CHECK_FOR_LOCAL_DEBUGGING)
  1509. Suite *
  1510. make_private_suite(void)
  1511. {
  1512. Suite *const suite = suite_create("Private");
  1513. TCase *const tcase_http_message = tcase_create("HTTP Message");
  1514. TCase *const tcase_http_keep_alive = tcase_create("HTTP Keep Alive");
  1515. TCase *const tcase_url_parsing_1 = tcase_create("URL Parsing 1");
  1516. TCase *const tcase_url_parsing_2 = tcase_create("URL Parsing 2");
  1517. TCase *const tcase_url_parsing_3 = tcase_create("URL Parsing 3");
  1518. TCase *const tcase_internal_parse_1 = tcase_create("Internal Parsing 1");
  1519. TCase *const tcase_internal_parse_2 = tcase_create("Internal Parsing 2");
  1520. TCase *const tcase_internal_parse_3 = tcase_create("Internal Parsing 3");
  1521. TCase *const tcase_internal_parse_4 = tcase_create("Internal Parsing 4");
  1522. TCase *const tcase_internal_parse_5 = tcase_create("Internal Parsing 5");
  1523. TCase *const tcase_internal_parse_6 = tcase_create("Internal Parsing 6");
  1524. TCase *const tcase_internal_parse_7 = tcase_create("Internal Parsing 7");
  1525. TCase *const tcase_encode_decode = tcase_create("Encode Decode");
  1526. TCase *const tcase_mask_data = tcase_create("Mask Data");
  1527. TCase *const tcase_parse_date_string = tcase_create("Date Parsing");
  1528. TCase *const tcase_sha1 = tcase_create("SHA1");
  1529. TCase *const tcase_config_options = tcase_create("Config Options");
  1530. tcase_add_test(tcase_http_message, test_parse_http_message);
  1531. tcase_set_timeout(tcase_http_message, civetweb_min_test_timeout);
  1532. suite_add_tcase(suite, tcase_http_message);
  1533. tcase_add_test(tcase_http_keep_alive, test_should_keep_alive);
  1534. tcase_set_timeout(tcase_http_keep_alive, civetweb_min_test_timeout);
  1535. suite_add_tcase(suite, tcase_http_keep_alive);
  1536. tcase_add_test(tcase_url_parsing_1, test_match_prefix);
  1537. tcase_add_test(tcase_url_parsing_1, test_match_prefix_strlen);
  1538. tcase_add_test(tcase_url_parsing_1, test_match_prefix_fuzz);
  1539. tcase_add_test(tcase_url_parsing_1, test_mg_match);
  1540. tcase_set_timeout(tcase_url_parsing_1, civetweb_min_test_timeout);
  1541. suite_add_tcase(suite, tcase_url_parsing_1);
  1542. tcase_add_test(tcase_url_parsing_2, test_remove_dot_segments);
  1543. tcase_set_timeout(tcase_url_parsing_2, civetweb_min_test_timeout);
  1544. suite_add_tcase(suite, tcase_url_parsing_2);
  1545. tcase_add_test(tcase_url_parsing_3, test_is_valid_uri);
  1546. tcase_set_timeout(tcase_url_parsing_3, civetweb_min_test_timeout);
  1547. suite_add_tcase(suite, tcase_url_parsing_3);
  1548. tcase_add_test(tcase_internal_parse_1, test_next_option);
  1549. tcase_set_timeout(tcase_internal_parse_1, civetweb_min_test_timeout);
  1550. suite_add_tcase(suite, tcase_internal_parse_1);
  1551. tcase_add_test(tcase_internal_parse_2, test_skip_quoted);
  1552. tcase_set_timeout(tcase_internal_parse_2, civetweb_min_test_timeout);
  1553. suite_add_tcase(suite, tcase_internal_parse_2);
  1554. tcase_add_test(tcase_internal_parse_3, test_mg_strcasestr);
  1555. tcase_set_timeout(tcase_internal_parse_3, civetweb_min_test_timeout);
  1556. suite_add_tcase(suite, tcase_internal_parse_3);
  1557. tcase_add_test(tcase_internal_parse_4, test_alloc_vprintf);
  1558. tcase_set_timeout(tcase_internal_parse_4, civetweb_min_test_timeout);
  1559. suite_add_tcase(suite, tcase_internal_parse_4);
  1560. tcase_add_test(tcase_internal_parse_5, test_mg_vsnprintf);
  1561. tcase_set_timeout(tcase_internal_parse_5, civetweb_min_test_timeout);
  1562. suite_add_tcase(suite, tcase_internal_parse_5);
  1563. tcase_add_test(tcase_internal_parse_6, test_parse_port_string);
  1564. tcase_set_timeout(tcase_internal_parse_6, civetweb_min_test_timeout);
  1565. suite_add_tcase(suite, tcase_internal_parse_6);
  1566. tcase_add_test(tcase_internal_parse_7, test_parse_http_headers);
  1567. tcase_set_timeout(tcase_internal_parse_7, civetweb_min_test_timeout);
  1568. suite_add_tcase(suite, tcase_internal_parse_7);
  1569. tcase_add_test(tcase_encode_decode, test_encode_decode);
  1570. tcase_set_timeout(tcase_encode_decode, civetweb_min_test_timeout);
  1571. suite_add_tcase(suite, tcase_encode_decode);
  1572. tcase_add_test(tcase_mask_data, test_mask_data);
  1573. tcase_set_timeout(tcase_mask_data, civetweb_min_test_timeout);
  1574. suite_add_tcase(suite, tcase_mask_data);
  1575. tcase_add_test(tcase_parse_date_string, test_parse_date_string);
  1576. tcase_set_timeout(tcase_parse_date_string, civetweb_min_test_timeout);
  1577. suite_add_tcase(suite, tcase_parse_date_string);
  1578. tcase_add_test(tcase_sha1, test_sha1);
  1579. tcase_set_timeout(tcase_sha1, civetweb_min_test_timeout);
  1580. suite_add_tcase(suite, tcase_sha1);
  1581. tcase_add_test(tcase_config_options, test_config_options);
  1582. tcase_set_timeout(tcase_config_options, civetweb_min_test_timeout);
  1583. suite_add_tcase(suite, tcase_config_options);
  1584. return suite;
  1585. }
  1586. #endif
  1587. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  1588. /* Used to debug test cases without using the check framework */
  1589. void
  1590. MAIN_PRIVATE(void)
  1591. {
  1592. #if defined(_WIN32)
  1593. /* test_parse_port_string requires WSAStartup for IPv6 */
  1594. WSADATA data;
  1595. WSAStartup(MAKEWORD(2, 2), &data);
  1596. #endif
  1597. test_alloc_vprintf(0);
  1598. test_mg_vsnprintf(0);
  1599. test_remove_dot_segments(0);
  1600. test_parse_date_string(0);
  1601. test_parse_port_string(0);
  1602. test_parse_http_message(0);
  1603. test_parse_http_headers(0);
  1604. test_sha1(0);
  1605. #if defined(_WIN32)
  1606. WSACleanup();
  1607. #endif
  1608. }
  1609. #endif