private.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  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. }
  375. END_TEST
  376. START_TEST(test_remove_dot_segments)
  377. {
  378. int i;
  379. struct {
  380. const char *input;
  381. const char *expected_output;
  382. } tests[] = {
  383. {"/path/to/file.ext", "/path/to/file.ext"},
  384. {"/file.ext", "/file.ext"},
  385. {"/path/../file.ext", "/file.ext"},
  386. {"/../to/file.ext", "/to/file.ext"},
  387. {"/../../file.ext", "/file.ext"},
  388. {"/./../file.ext", "/file.ext"},
  389. {"/.././file.ext", "/file.ext"},
  390. {"/././file.ext", "/file.ext"},
  391. {"/././file.ext", "/file.ext"},
  392. {"/path/.to/..file.ext", "/path/.to/..file.ext"},
  393. {"/file", "/file"},
  394. {"/path/", "/path/"},
  395. {"file.ext", "file.ext"},
  396. {"./file.ext", "file.ext"},
  397. {"../file.ext", "file.ext"},
  398. {".file.ext", ".file.ext"},
  399. {"..file.ext", "..file.ext"},
  400. {"file", "file"},
  401. {"/x/../", "/"},
  402. {"/x/../../", "/"},
  403. {"/x/.././", "/"},
  404. {"/./x/.././", "/"},
  405. /* Windows specific */
  406. {"\\file.ext", "/file.ext"},
  407. {"\\..\\file.ext", "/file.ext"},
  408. {"/file.", "/file"},
  409. {"/path\\to.\\.\\file.", "/path/to/file"},
  410. /* Multiple dots and slashes */
  411. {"\\//\\\\x", "/x"},
  412. {"//", "/"},
  413. {"/./", "/"},
  414. {"/../", "/"},
  415. {"/.../", "/"},
  416. {"/..../", "/"},
  417. {"/...../", "/"},
  418. {"/...../", "/"},
  419. {"/...//", "/"},
  420. {"/..././", "/"},
  421. {"/.../../", "/"},
  422. {"/.../.../", "/"},
  423. /* Test cases from issues */
  424. {"/foo/bar/baz/../qux.txt", "/foo/bar/qux.txt"},
  425. {"/../alice/bob/../carol/david/frank/../../grace.ext/hugo/../irene.jpg",
  426. "/alice/carol/grace.ext/irene.jpg"},
  427. {"/a/b/..", "/a/"},
  428. {"/a/b/c/../d/../..", "/a/"},
  429. {NULL, NULL}};
  430. mark_point();
  431. for (i = 0; (tests[i].input != NULL); i++) {
  432. char inout[256];
  433. strcpy(inout, tests[i].input);
  434. remove_dot_segments(inout);
  435. ck_assert_str_eq(inout, tests[i].expected_output);
  436. }
  437. }
  438. END_TEST
  439. START_TEST(test_is_valid_uri)
  440. {
  441. /* is_valid_uri is superseeded by get_uri_type */
  442. ck_assert_int_eq(2, get_uri_type("/api"));
  443. ck_assert_int_eq(2, get_uri_type("/api/"));
  444. ck_assert_int_eq(2,
  445. get_uri_type("/some/long/path%20with%20space/file.xyz"));
  446. ck_assert_int_eq(0, get_uri_type("api"));
  447. ck_assert_int_eq(1, get_uri_type("*"));
  448. ck_assert_int_eq(0, get_uri_type("*xy"));
  449. ck_assert_int_eq(3, get_uri_type("http://somewhere/"));
  450. ck_assert_int_eq(3, get_uri_type("https://somewhere/some/file.html"));
  451. ck_assert_int_eq(4, get_uri_type("http://somewhere:8080/"));
  452. ck_assert_int_eq(4, get_uri_type("https://somewhere:8080/some/file.html"));
  453. }
  454. END_TEST
  455. START_TEST(test_next_option)
  456. {
  457. /* Adapted from unit_test.c */
  458. /* Copyright (c) 2013-2015 the Civetweb developers */
  459. /* Copyright (c) 2004-2013 Sergey Lyubka */
  460. const char *p, *list = "x/8,/y**=1;2k,z";
  461. struct vec a, b;
  462. int i;
  463. mark_point();
  464. ck_assert(next_option(NULL, &a, &b) == NULL);
  465. for (i = 0, p = list; (p = next_option(p, &a, &b)) != NULL; i++) {
  466. ck_assert(i != 0 || (a.ptr == list && a.len == 3 && b.len == 0));
  467. ck_assert(i != 1
  468. || (a.ptr == list + 4 && a.len == 4 && b.ptr == list + 9
  469. && b.len == 4));
  470. ck_assert(i != 2 || (a.ptr == list + 14 && a.len == 1 && b.len == 0));
  471. }
  472. }
  473. END_TEST
  474. START_TEST(test_skip_quoted)
  475. {
  476. /* Adapted from unit_test.c */
  477. /* Copyright (c) 2013-2015 the Civetweb developers */
  478. /* Copyright (c) 2004-2013 Sergey Lyubka */
  479. char x[] = "a=1, b=2, c='hi \' there', d='here\\, there'", *s = x, *p;
  480. mark_point();
  481. p = skip_quoted(&s, ", ", ", ", 0);
  482. ck_assert(p != NULL && !strcmp(p, "a=1"));
  483. p = skip_quoted(&s, ", ", ", ", 0);
  484. ck_assert(p != NULL && !strcmp(p, "b=2"));
  485. p = skip_quoted(&s, ",", " ", 0);
  486. ck_assert(p != NULL && !strcmp(p, "c='hi \' there'"));
  487. p = skip_quoted(&s, ",", " ", '\\');
  488. ck_assert(p != NULL && !strcmp(p, "d='here, there'"));
  489. ck_assert(*s == 0);
  490. }
  491. END_TEST
  492. static int
  493. alloc_vprintf_wrapper(char **buf, size_t size, const char *fmt, ...)
  494. {
  495. /* Test helper function - adapted from unit_test.c */
  496. /* Copyright (c) 2013-2015 the Civetweb developers */
  497. /* Copyright (c) 2004-2013 Sergey Lyubka */
  498. va_list ap;
  499. int ret = 0;
  500. mark_point();
  501. va_start(ap, fmt);
  502. ret = alloc_vprintf(buf, *buf, size, fmt, ap);
  503. va_end(ap);
  504. return ret;
  505. }
  506. START_TEST(test_alloc_vprintf)
  507. {
  508. /* Adapted from unit_test.c */
  509. /* Copyright (c) 2013-2015 the Civetweb developers */
  510. /* Copyright (c) 2004-2013 Sergey Lyubka */
  511. char buf[MG_BUF_LEN], *p = buf;
  512. mark_point();
  513. ck_assert(alloc_vprintf_wrapper(&p, sizeof(buf), "%s", "hi") == 2);
  514. ck_assert(p == buf);
  515. ck_assert(alloc_vprintf_wrapper(&p, sizeof(buf), "%s", "") == 0);
  516. ck_assert(p == buf);
  517. ck_assert(alloc_vprintf_wrapper(&p, sizeof(buf), "") == 0);
  518. ck_assert(p == buf);
  519. /* Pass small buffer, make sure alloc_printf allocates */
  520. ck_assert(alloc_vprintf_wrapper(&p, 1, "%s", "hello") == 5);
  521. ck_assert(p != buf);
  522. mg_free(p);
  523. p = buf;
  524. /* Test new wrapper implementation */
  525. ck_assert(alloc_printf(&p, "%s", "hello") == 5);
  526. ck_assert(p != buf);
  527. mg_free(p);
  528. p = buf;
  529. }
  530. END_TEST
  531. START_TEST(test_mg_vsnprintf)
  532. {
  533. char buf[16];
  534. int is_trunc;
  535. #if defined(_WIN32)
  536. /* If the string is truncated, mg_snprintf calls mg_cry.
  537. * If DEBUG is defined, mg_cry calls DEBUG_TRACE.
  538. * In DEBUG_TRACE_FUNC, flockfile(stdout) is called.
  539. * For Windows, flockfile/funlockfile calls
  540. * pthread_mutex_lock/_unlock(&global_log_file_lock).
  541. * So, we need to initialize global_log_file_lock:
  542. */
  543. pthread_mutex_init(&global_log_file_lock, &pthread_mutex_attr);
  544. #endif
  545. memset(buf, 0, sizeof(buf));
  546. mark_point();
  547. is_trunc = 777;
  548. mg_snprintf(NULL, &is_trunc, buf, 10, "%8i", 123);
  549. ck_assert_str_eq(buf, " 123");
  550. ck_assert_int_eq(is_trunc, 0);
  551. is_trunc = 777;
  552. mg_snprintf(NULL, &is_trunc, buf, 10, "%9i", 123);
  553. ck_assert_str_eq(buf, " 123");
  554. ck_assert_int_eq(is_trunc, 0);
  555. is_trunc = 777;
  556. mg_snprintf(NULL, &is_trunc, buf, 9, "%9i", 123);
  557. ck_assert_str_eq(buf, " 12");
  558. ck_assert_int_eq(is_trunc, 1);
  559. is_trunc = 777;
  560. mg_snprintf(NULL, &is_trunc, buf, 8, "%9i", 123);
  561. ck_assert_str_eq(buf, " 1");
  562. ck_assert_int_eq(is_trunc, 1);
  563. is_trunc = 777;
  564. mg_snprintf(NULL, &is_trunc, buf, 7, "%9i", 123);
  565. ck_assert_str_eq(buf, " ");
  566. ck_assert_int_eq(is_trunc, 1);
  567. is_trunc = 777;
  568. strcpy(buf, "1234567890");
  569. mg_snprintf(NULL, &is_trunc, buf, 0, "%i", 543);
  570. ck_assert_str_eq(buf, "1234567890");
  571. ck_assert_int_eq(is_trunc, 1);
  572. }
  573. END_TEST
  574. START_TEST(test_mg_strcasestr)
  575. {
  576. /* Adapted from unit_test.c */
  577. /* Copyright (c) 2013-2015 the Civetweb developers */
  578. /* Copyright (c) 2004-2013 Sergey Lyubka */
  579. static const char *big1 = "abcdef";
  580. mark_point();
  581. ck_assert(mg_strcasestr("Y", "X") == NULL);
  582. ck_assert(mg_strcasestr("Y", "y") != NULL);
  583. ck_assert(mg_strcasestr(big1, "X") == NULL);
  584. ck_assert(mg_strcasestr(big1, "CD") == big1 + 2);
  585. ck_assert(mg_strcasestr("aa", "AAB") == NULL);
  586. }
  587. END_TEST
  588. START_TEST(test_parse_port_string)
  589. {
  590. /* Adapted from unit_test.c */
  591. /* Copyright (c) 2013-2020 the Civetweb developers */
  592. /* Copyright (c) 2004-2013 Sergey Lyubka */
  593. struct t_test_parse_port_string {
  594. const char *port_string;
  595. int valid;
  596. int ip_family;
  597. uint16_t port_num;
  598. };
  599. static struct t_test_parse_port_string testdata[] =
  600. { {"0", 1, 4, 0},
  601. {"1", 1, 4, 1},
  602. {"65535", 1, 4, 65535},
  603. {"65536", 0, 0, 0},
  604. {"1s", 1, 4, 1},
  605. {"1r", 1, 4, 1},
  606. {"1k", 0, 0, 0},
  607. {"1.2.3", 0, 0, 0},
  608. {"1.2.3.", 0, 0, 0},
  609. {"1.2.3.4", 0, 0, 0},
  610. {"1.2.3.4:", 0, 0, 0},
  611. {"1.2.3.4:0", 1, 4, 0},
  612. {"1.2.3.4:1", 1, 4, 1},
  613. {"1.2.3.4:65535", 1, 4, 65535},
  614. {"1.2.3.4:65536", 0, 0, 0},
  615. {"1.2.3.4:1s", 1, 4, 1},
  616. {"1.2.3.4:1r", 1, 4, 1},
  617. {"1.2.3.4:1k", 0, 0, 0},
  618. #if defined(USE_IPV6)
  619. /* IPv6 config */
  620. {"[::1]:123", 1, 6, 123},
  621. {"[::]:80", 1, 6, 80},
  622. {"[3ffe:2a00:100:7031::1]:900", 1, 6, 900},
  623. /* IPv4 + IPv6 config */
  624. {"+80", 1, 4 + 6, 80},
  625. #else
  626. /* IPv6 config: invalid if IPv6 is not activated */
  627. {"[::1]:123", 0, 0, 123},
  628. {"[::]:80", 0, 0, 80},
  629. {"[3ffe:2a00:100:7031::1]:900", 0, 0, 900},
  630. /* IPv4 + IPv6 config: only IPv4 if IPv6 is not activated */
  631. {"+80", 1, 4, 80},
  632. #endif
  633. {NULL, 0, 0, 0} };
  634. struct socket so;
  635. struct vec vec;
  636. int ip_family;
  637. int i, ret;
  638. mark_point();
  639. for (i = 0; testdata[i].port_string != NULL; i++) {
  640. vec.ptr = testdata[i].port_string;
  641. vec.len = strlen(vec.ptr);
  642. ip_family = 123;
  643. ret = parse_port_string(&vec, &so, &ip_family);
  644. if ((ret != testdata[i].valid)
  645. || (ip_family != testdata[i].ip_family)) {
  646. ck_abort_msg("Port string [%s]: "
  647. "expected valid=%i, family=%i; "
  648. "got valid=%i, family=%i",
  649. testdata[i].port_string,
  650. testdata[i].valid,
  651. testdata[i].ip_family,
  652. ret,
  653. ip_family);
  654. }
  655. if (ip_family == 4) {
  656. ck_assert_int_eq((int)so.lsa.sin.sin_family, (int)AF_INET);
  657. }
  658. if (ip_family == 6) {
  659. ck_assert_int_eq((int)so.lsa.sin.sin_family, (int)AF_INET6);
  660. }
  661. if (ret) {
  662. /* Test valid strings only */
  663. ck_assert_int_eq(htons(so.lsa.sin.sin_port), testdata[i].port_num);
  664. }
  665. }
  666. /* special case: localhost can be ipv4 or ipv6 */
  667. vec.ptr = "localhost:123";
  668. vec.len = strlen(vec.ptr);
  669. ret = parse_port_string(&vec, &so, &ip_family);
  670. if (ret != 1) {
  671. ck_abort_msg("IP of localhost seems to be unknown on this system (%i)",
  672. (int)ret);
  673. }
  674. if ((ip_family != 4) && (ip_family != 6)) {
  675. ck_abort_msg("IP family for localhost must be 4 or 6 but is %i",
  676. (int)ip_family);
  677. }
  678. ck_assert_int_eq((int)htons(so.lsa.sin.sin_port), (int)123);
  679. }
  680. END_TEST
  681. START_TEST(test_parse_http_headers)
  682. {
  683. char buf[2048];
  684. char *ptr;
  685. int ret;
  686. struct mg_header hdr[MG_MAX_HEADERS];
  687. memset(hdr, 0, sizeof(hdr));
  688. memset(buf, 0, sizeof(buf));
  689. ptr = &buf[0];
  690. ret = parse_http_headers(&ptr, hdr);
  691. ck_assert_int_eq(ret, 0);
  692. ck_assert_ptr_eq(ptr, &buf[0]);
  693. ck_assert_ptr_eq(hdr[0].name, NULL);
  694. ck_assert_ptr_eq(hdr[0].value, NULL);
  695. ck_assert_ptr_eq(hdr[1].name, NULL);
  696. ck_assert_ptr_eq(hdr[1].value, NULL);
  697. ck_assert_ptr_eq(hdr[2].name, NULL);
  698. ck_assert_ptr_eq(hdr[2].value, NULL);
  699. memset(hdr, 0, sizeof(hdr));
  700. memset(buf, 0, sizeof(buf));
  701. strcpy(buf, "\r\n");
  702. ptr = &buf[0];
  703. ret = parse_http_headers(&ptr, hdr);
  704. ck_assert_int_eq(ret, 0);
  705. ck_assert_ptr_eq(ptr, &buf[0]);
  706. ck_assert_ptr_eq(hdr[0].name, NULL);
  707. ck_assert_ptr_eq(hdr[0].value, NULL);
  708. ck_assert_ptr_eq(hdr[1].name, NULL);
  709. ck_assert_ptr_eq(hdr[1].value, NULL);
  710. ck_assert_ptr_eq(hdr[2].name, NULL);
  711. ck_assert_ptr_eq(hdr[2].value, NULL);
  712. memset(hdr, 0, sizeof(hdr));
  713. memset(buf, 0, sizeof(buf));
  714. strcpy(buf, "a\r\n");
  715. ptr = &buf[0];
  716. ret = parse_http_headers(&ptr, hdr);
  717. ck_assert_int_eq(ret, -1);
  718. ck_assert_ptr_eq(ptr, &buf[0]);
  719. ck_assert_ptr_eq(hdr[0].name, NULL);
  720. ck_assert_ptr_eq(hdr[0].value, NULL);
  721. ck_assert_ptr_eq(hdr[1].name, NULL);
  722. ck_assert_ptr_eq(hdr[1].value, NULL);
  723. ck_assert_ptr_eq(hdr[2].name, NULL);
  724. ck_assert_ptr_eq(hdr[2].value, NULL);
  725. memset(hdr, 0, sizeof(hdr));
  726. memset(buf, 0, sizeof(buf));
  727. strcpy(buf, "a:b");
  728. ptr = &buf[0];
  729. ret = parse_http_headers(&ptr, hdr);
  730. ck_assert_int_eq(ret, 1);
  731. ck_assert_ptr_eq(ptr, &buf[3]);
  732. ck_assert_str_eq(hdr[0].name, "a");
  733. ck_assert_str_eq(hdr[0].value, "b");
  734. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  735. ck_assert_ptr_eq(hdr[0].value, &buf[2]);
  736. ck_assert_ptr_eq(hdr[1].name, NULL);
  737. ck_assert_ptr_eq(hdr[1].value, NULL);
  738. ck_assert_ptr_eq(hdr[2].name, NULL);
  739. ck_assert_ptr_eq(hdr[2].value, NULL);
  740. memset(hdr, 0, sizeof(hdr));
  741. memset(buf, 0, sizeof(buf));
  742. strcpy(buf, "a:b\r\n");
  743. ptr = &buf[0];
  744. ret = parse_http_headers(&ptr, hdr);
  745. ck_assert_int_eq(ret, 1);
  746. ck_assert_ptr_eq(ptr, &buf[5]);
  747. ck_assert_str_eq(hdr[0].name, "a");
  748. ck_assert_str_eq(hdr[0].value, "b");
  749. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  750. ck_assert_ptr_eq(hdr[0].value, &buf[2]);
  751. ck_assert_ptr_eq(hdr[1].name, NULL);
  752. ck_assert_ptr_eq(hdr[1].value, NULL);
  753. ck_assert_ptr_eq(hdr[2].name, NULL);
  754. ck_assert_ptr_eq(hdr[2].value, NULL);
  755. memset(hdr, 0, sizeof(hdr));
  756. memset(buf, 0, sizeof(buf));
  757. strcpy(buf, "a:b\r\n\r\n");
  758. ptr = &buf[0];
  759. ret = parse_http_headers(&ptr, hdr);
  760. ck_assert_int_eq(ret, 1);
  761. ck_assert_ptr_eq(ptr, &buf[5]);
  762. ck_assert_str_eq(hdr[0].name, "a");
  763. ck_assert_str_eq(hdr[0].value, "b");
  764. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  765. ck_assert_ptr_eq(hdr[0].value, &buf[2]);
  766. ck_assert_ptr_eq(hdr[1].name, NULL);
  767. ck_assert_ptr_eq(hdr[1].value, NULL);
  768. ck_assert_ptr_eq(hdr[2].name, NULL);
  769. ck_assert_ptr_eq(hdr[2].value, NULL);
  770. memset(hdr, 0, sizeof(hdr));
  771. memset(buf, 0, sizeof(buf));
  772. strcpy(buf, "a: b\r\n");
  773. ptr = &buf[0];
  774. ret = parse_http_headers(&ptr, hdr);
  775. ck_assert_int_eq(ret, 1);
  776. ck_assert_ptr_eq(ptr, &buf[6]);
  777. ck_assert_str_eq(hdr[0].name, "a");
  778. ck_assert_str_eq(hdr[0].value, "b");
  779. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  780. ck_assert_ptr_eq(hdr[0].value, &buf[3]);
  781. ck_assert_ptr_eq(hdr[1].name, NULL);
  782. ck_assert_ptr_eq(hdr[1].value, NULL);
  783. ck_assert_ptr_eq(hdr[2].name, NULL);
  784. ck_assert_ptr_eq(hdr[2].value, NULL);
  785. memset(hdr, 0, sizeof(hdr));
  786. memset(buf, 0, sizeof(buf));
  787. strcpy(buf, "a :b\r\n");
  788. ptr = &buf[0];
  789. ret = parse_http_headers(&ptr, hdr);
  790. ck_assert_int_eq(ret, 1);
  791. ck_assert_ptr_eq(ptr, &buf[6]);
  792. ck_assert_str_eq(hdr[0].name, "a");
  793. ck_assert_str_eq(hdr[0].value, "b");
  794. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  795. ck_assert_ptr_eq(hdr[0].value, &buf[3]);
  796. ck_assert_ptr_eq(hdr[1].name, NULL);
  797. ck_assert_ptr_eq(hdr[1].value, NULL);
  798. ck_assert_ptr_eq(hdr[2].name, NULL);
  799. ck_assert_ptr_eq(hdr[2].value, NULL);
  800. memset(hdr, 0, sizeof(hdr));
  801. memset(buf, 0, sizeof(buf));
  802. strcpy(buf, "a : b\r\n");
  803. ptr = &buf[0];
  804. ret = parse_http_headers(&ptr, hdr);
  805. ck_assert_int_eq(ret, 1);
  806. ck_assert_ptr_eq(ptr, &buf[7]);
  807. ck_assert_str_eq(hdr[0].name, "a");
  808. ck_assert_str_eq(hdr[0].value, "b");
  809. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  810. ck_assert_ptr_eq(hdr[0].value, &buf[4]);
  811. ck_assert_ptr_eq(hdr[1].name, NULL);
  812. ck_assert_ptr_eq(hdr[1].value, NULL);
  813. ck_assert_ptr_eq(hdr[2].name, NULL);
  814. ck_assert_ptr_eq(hdr[2].value, NULL);
  815. memset(hdr, 0, sizeof(hdr));
  816. memset(buf, 0, sizeof(buf));
  817. strcpy(buf, "a: b\r\nc: d\r\n");
  818. ptr = &buf[0];
  819. ret = parse_http_headers(&ptr, hdr);
  820. ck_assert_int_eq(ret, 2);
  821. ck_assert_ptr_eq(ptr, &buf[12]);
  822. ck_assert_str_eq(hdr[0].name, "a");
  823. ck_assert_str_eq(hdr[0].value, "b");
  824. ck_assert_str_eq(hdr[1].name, "c");
  825. ck_assert_str_eq(hdr[1].value, "d");
  826. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  827. ck_assert_ptr_eq(hdr[0].value, &buf[3]);
  828. ck_assert_ptr_eq(hdr[1].name, &buf[6]);
  829. ck_assert_ptr_eq(hdr[1].value, &buf[9]);
  830. ck_assert_ptr_eq(hdr[2].name, NULL);
  831. ck_assert_ptr_eq(hdr[2].value, NULL);
  832. memset(hdr, 0, sizeof(hdr));
  833. memset(buf, 0, sizeof(buf));
  834. strcpy(buf, "a: b\r\nc: d\r\n\r\n");
  835. ptr = &buf[0];
  836. ret = parse_http_headers(&ptr, hdr);
  837. ck_assert_int_eq(ret, 2);
  838. ck_assert_ptr_eq(ptr, &buf[12]);
  839. ck_assert_str_eq(hdr[0].name, "a");
  840. ck_assert_str_eq(hdr[0].value, "b");
  841. ck_assert_str_eq(hdr[1].name, "c");
  842. ck_assert_str_eq(hdr[1].value, "d");
  843. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  844. ck_assert_ptr_eq(hdr[0].value, &buf[3]);
  845. ck_assert_ptr_eq(hdr[1].name, &buf[6]);
  846. ck_assert_ptr_eq(hdr[1].value, &buf[9]);
  847. ck_assert_ptr_eq(hdr[2].name, NULL);
  848. ck_assert_ptr_eq(hdr[2].value, NULL);
  849. memset(hdr, 0, sizeof(hdr));
  850. memset(buf, 0, sizeof(buf));
  851. strcpy(buf, "a: b\r\nc: d\r\n\r\ne: f\r\n");
  852. ptr = &buf[0];
  853. ret = parse_http_headers(&ptr, hdr);
  854. ck_assert_int_eq(ret, 2);
  855. ck_assert_ptr_eq(ptr, &buf[12]);
  856. ck_assert_str_eq(hdr[0].name, "a");
  857. ck_assert_str_eq(hdr[0].value, "b");
  858. ck_assert_str_eq(hdr[1].name, "c");
  859. ck_assert_str_eq(hdr[1].value, "d");
  860. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  861. ck_assert_ptr_eq(hdr[0].value, &buf[3]);
  862. ck_assert_ptr_eq(hdr[1].name, &buf[6]);
  863. ck_assert_ptr_eq(hdr[1].value, &buf[9]);
  864. ck_assert_ptr_eq(hdr[2].name, NULL);
  865. ck_assert_ptr_eq(hdr[2].value, NULL);
  866. memset(hdr, 0, sizeof(hdr));
  867. memset(buf, 0, sizeof(buf));
  868. strcpy(buf, "a: b\r\nc: d");
  869. ptr = &buf[0];
  870. ret = parse_http_headers(&ptr, hdr);
  871. ck_assert_int_eq(ret, 2);
  872. ck_assert_ptr_eq(ptr, &buf[10]);
  873. ck_assert_str_eq(hdr[0].name, "a");
  874. ck_assert_str_eq(hdr[0].value, "b");
  875. ck_assert_str_eq(hdr[1].name, "c");
  876. ck_assert_str_eq(hdr[1].value, "d");
  877. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  878. ck_assert_ptr_eq(hdr[0].value, &buf[3]);
  879. ck_assert_ptr_eq(hdr[1].name, &buf[6]);
  880. ck_assert_ptr_eq(hdr[1].value, &buf[9]);
  881. ck_assert_ptr_eq(hdr[2].name, NULL);
  882. ck_assert_ptr_eq(hdr[2].value, NULL);
  883. memset(hdr, 0, sizeof(hdr));
  884. memset(buf, 0, sizeof(buf));
  885. strcpy(buf, "a: b\r\nc: d\r\ne");
  886. ptr = &buf[0];
  887. ret = parse_http_headers(&ptr, hdr);
  888. ck_assert_int_eq(ret, -1);
  889. /* The following could be undefined, since ret == -1 */
  890. ck_assert_ptr_eq(ptr, &buf[12]);
  891. ck_assert_str_eq(hdr[0].name, "a");
  892. ck_assert_str_eq(hdr[0].value, "b");
  893. ck_assert_str_eq(hdr[1].name, "c");
  894. ck_assert_str_eq(hdr[1].value, "d");
  895. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  896. ck_assert_ptr_eq(hdr[0].value, &buf[3]);
  897. ck_assert_ptr_eq(hdr[1].name, &buf[6]);
  898. ck_assert_ptr_eq(hdr[1].value, &buf[9]);
  899. ck_assert_ptr_eq(hdr[2].name, NULL);
  900. ck_assert_ptr_eq(hdr[2].value, NULL);
  901. memset(hdr, 0, sizeof(hdr));
  902. memset(buf, 0, sizeof(buf));
  903. strcpy(buf, "a: b\r\nc: d\r\nefg:");
  904. ptr = &buf[0];
  905. ret = parse_http_headers(&ptr, hdr);
  906. ck_assert_int_eq(ret, 3);
  907. ck_assert_ptr_eq(ptr, &buf[16]);
  908. ck_assert_str_eq(hdr[0].name, "a");
  909. ck_assert_str_eq(hdr[0].value, "b");
  910. ck_assert_str_eq(hdr[1].name, "c");
  911. ck_assert_str_eq(hdr[1].value, "d");
  912. ck_assert_str_eq(hdr[2].name, "efg");
  913. ck_assert_str_eq(hdr[2].value, "");
  914. ck_assert_ptr_eq(hdr[0].name, &buf[0]);
  915. ck_assert_ptr_eq(hdr[0].value, &buf[3]);
  916. ck_assert_ptr_eq(hdr[1].name, &buf[6]);
  917. ck_assert_ptr_eq(hdr[1].value, &buf[9]);
  918. ck_assert_ptr_eq(hdr[2].name, &buf[12]);
  919. ck_assert_ptr_eq(hdr[2].value, &buf[16]);
  920. }
  921. END_TEST
  922. START_TEST(test_encode_decode)
  923. {
  924. char buf[128];
  925. const char *alpha = "abcdefghijklmnopqrstuvwxyz";
  926. const char *nonalpha = " !\"#$%&'()*+,-./0123456789:;<=>?@";
  927. const char *nonalpha_url_enc1 =
  928. "%20%21%22%23$%25%26%27()%2a%2b,-.%2f0123456789%3a;%3c%3d%3e%3f%40";
  929. const char *nonalpha_url_enc2 =
  930. "%20!%22%23%24%25%26'()*%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40";
  931. int ret;
  932. size_t len;
  933. mark_point();
  934. memset(buf, 77, sizeof(buf));
  935. ret = mg_url_encode(alpha, buf, sizeof(buf));
  936. ck_assert_int_eq(ret, (int)strlen(buf));
  937. ck_assert_int_eq(ret, (int)strlen(alpha));
  938. ck_assert_str_eq(buf, alpha);
  939. memset(buf, 77, sizeof(buf));
  940. ret = mg_url_encode(nonalpha, buf, sizeof(buf));
  941. ck_assert_int_eq(ret, (int)strlen(buf));
  942. ck_assert_int_eq(ret, (int)strlen(nonalpha_url_enc1));
  943. ck_assert_str_eq(buf, nonalpha_url_enc1);
  944. memset(buf, 77, sizeof(buf));
  945. ret = mg_url_decode(alpha, (int)strlen(alpha), buf, sizeof(buf), 0);
  946. ck_assert_int_eq(ret, (int)strlen(buf));
  947. ck_assert_int_eq(ret, (int)strlen(alpha));
  948. ck_assert_str_eq(buf, alpha);
  949. memset(buf, 77, sizeof(buf));
  950. ret = mg_url_decode(
  951. nonalpha_url_enc1, (int)strlen(nonalpha_url_enc1), buf, sizeof(buf), 0);
  952. ck_assert_int_eq(ret, (int)strlen(buf));
  953. ck_assert_int_eq(ret, (int)strlen(nonalpha));
  954. ck_assert_str_eq(buf, nonalpha);
  955. memset(buf, 77, sizeof(buf));
  956. ret = mg_url_decode(
  957. nonalpha_url_enc2, (int)strlen(nonalpha_url_enc2), buf, sizeof(buf), 0);
  958. ck_assert_int_eq(ret, (int)strlen(buf));
  959. ck_assert_int_eq(ret, (int)strlen(nonalpha));
  960. ck_assert_str_eq(buf, nonalpha);
  961. /* Test url_decode_in_place */
  962. strcpy(buf, "AbcDef");
  963. url_decode_in_place(buf);
  964. ck_assert_str_eq(buf, "AbcDef");
  965. strcpy(buf, "A%20B+C");
  966. url_decode_in_place(buf);
  967. ck_assert_str_eq(buf, "A B C");
  968. /* len could be unused, if base64_decode is not tested because USE_LUA is
  969. * not defined */
  970. (void)len;
  971. }
  972. END_TEST
  973. START_TEST(test_mask_data)
  974. {
  975. #if defined(USE_WEBSOCKET)
  976. char in[1024];
  977. char out[1024];
  978. int i;
  979. #endif
  980. uint32_t mask = 0x61626364;
  981. /* TODO: adapt test for big endian */
  982. ck_assert((*(unsigned char *)&mask) == 0x64u);
  983. #if defined(USE_WEBSOCKET)
  984. memset(in, 0, sizeof(in));
  985. memset(out, 99, sizeof(out));
  986. mask_data(in, sizeof(out), 0, out);
  987. ck_assert(!memcmp(out, in, sizeof(out)));
  988. for (i = 0; i < 1024; i++) {
  989. in[i] = (char)((unsigned char)i);
  990. }
  991. mask_data(in, 107, 0, out);
  992. ck_assert(!memcmp(out, in, 107));
  993. mask_data(in, 256, 0x01010101, out);
  994. for (i = 0; i < 256; i++) {
  995. ck_assert_int_eq((int)((unsigned char)out[i]),
  996. (int)(((unsigned char)in[i]) ^ (char)1u));
  997. }
  998. for (i = 256; i < (int)sizeof(out); i++) {
  999. ck_assert_int_eq((int)((unsigned char)out[i]), (int)0);
  1000. }
  1001. /* TODO: check this for big endian */
  1002. mask_data(in, 5, 0x01020304, out);
  1003. ck_assert_uint_eq((unsigned char)out[0], 0u ^ 4u);
  1004. ck_assert_uint_eq((unsigned char)out[1], 1u ^ 3u);
  1005. ck_assert_uint_eq((unsigned char)out[2], 2u ^ 2u);
  1006. ck_assert_uint_eq((unsigned char)out[3], 3u ^ 1u);
  1007. ck_assert_uint_eq((unsigned char)out[4], 4u ^ 4u);
  1008. #endif
  1009. }
  1010. END_TEST
  1011. START_TEST(test_parse_date_string)
  1012. {
  1013. #if !defined(NO_CACHING)
  1014. time_t now = time(0);
  1015. struct tm *tm = gmtime(&now);
  1016. char date[64] = {0};
  1017. unsigned long i;
  1018. ck_assert_uint_eq((unsigned long)parse_date_string("1/Jan/1970 00:01:02"),
  1019. 62ul);
  1020. ck_assert_uint_eq((unsigned long)parse_date_string("1 Jan 1970 00:02:03"),
  1021. 123ul);
  1022. ck_assert_uint_eq((unsigned long)parse_date_string("1-Jan-1970 00:03:04"),
  1023. 184ul);
  1024. ck_assert_uint_eq((unsigned long)parse_date_string(
  1025. "Xyz, 1 Jan 1970 00:04:05"),
  1026. 245ul);
  1027. gmt_time_string(date, sizeof(date), &now);
  1028. ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
  1029. sprintf(date,
  1030. "%02u %s %04u %02u:%02u:%02u",
  1031. (unsigned int)tm->tm_mday,
  1032. month_names[tm->tm_mon],
  1033. (unsigned int)(tm->tm_year + 1900),
  1034. (unsigned int)tm->tm_hour,
  1035. (unsigned int)tm->tm_min,
  1036. (unsigned int)tm->tm_sec);
  1037. ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
  1038. gmt_time_string(date, 1, NULL);
  1039. ck_assert_str_eq(date, "");
  1040. gmt_time_string(date, 6, NULL);
  1041. ck_assert_str_eq(date,
  1042. "Thu, "); /* part of "Thu, 01 Jan 1970 00:00:00 GMT" */
  1043. gmt_time_string(date, sizeof(date), NULL);
  1044. ck_assert_str_eq(date, "Thu, 01 Jan 1970 00:00:00 GMT");
  1045. for (i = 2ul; i < 0x8000000ul; i += i / 2) {
  1046. now = (time_t)i;
  1047. gmt_time_string(date, sizeof(date), &now);
  1048. ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
  1049. tm = gmtime(&now);
  1050. sprintf(date,
  1051. "%02u-%s-%04u %02u:%02u:%02u",
  1052. (unsigned int)tm->tm_mday,
  1053. month_names[tm->tm_mon],
  1054. (unsigned int)(tm->tm_year + 1900),
  1055. (unsigned int)tm->tm_hour,
  1056. (unsigned int)tm->tm_min,
  1057. (unsigned int)tm->tm_sec);
  1058. ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
  1059. }
  1060. #endif
  1061. }
  1062. END_TEST
  1063. START_TEST(test_sha1)
  1064. {
  1065. #ifdef SHA1_DIGEST_SIZE
  1066. SHA_CTX sha_ctx;
  1067. uint8_t digest[SHA1_DIGEST_SIZE] = {0};
  1068. char str[48] = {0};
  1069. int i;
  1070. const char *test_str;
  1071. ck_assert_uint_eq(sizeof(digest), 20);
  1072. ck_assert_uint_gt(sizeof(str), sizeof(digest) * 2 + 1);
  1073. /* empty string */
  1074. SHA1_Init(&sha_ctx);
  1075. SHA1_Final(digest, &sha_ctx);
  1076. bin2str(str, digest, sizeof(digest));
  1077. ck_assert_uint_eq(strlen(str), 40);
  1078. ck_assert_str_eq(str, "da39a3ee5e6b4b0d3255bfef95601890afd80709");
  1079. /* empty string */
  1080. SHA1_Init(&sha_ctx);
  1081. SHA1_Update(&sha_ctx, (uint8_t *)"abc", 0);
  1082. SHA1_Final(digest, &sha_ctx);
  1083. bin2str(str, digest, sizeof(digest));
  1084. ck_assert_uint_eq(strlen(str), 40);
  1085. ck_assert_str_eq(str, "da39a3ee5e6b4b0d3255bfef95601890afd80709");
  1086. /* "abc" */
  1087. SHA1_Init(&sha_ctx);
  1088. SHA1_Update(&sha_ctx, (uint8_t *)"abc", 3);
  1089. SHA1_Final(digest, &sha_ctx);
  1090. bin2str(str, digest, sizeof(digest));
  1091. ck_assert_uint_eq(strlen(str), 40);
  1092. ck_assert_str_eq(str, "a9993e364706816aba3e25717850c26c9cd0d89d");
  1093. /* "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" */
  1094. test_str = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  1095. SHA1_Init(&sha_ctx);
  1096. SHA1_Update(&sha_ctx, (uint8_t *)test_str, (uint32_t)strlen(test_str));
  1097. SHA1_Final(digest, &sha_ctx);
  1098. bin2str(str, digest, sizeof(digest));
  1099. ck_assert_uint_eq(strlen(str), 40);
  1100. ck_assert_str_eq(str, "84983e441c3bd26ebaae4aa1f95129e5e54670f1");
  1101. /* a million "a" */
  1102. SHA1_Init(&sha_ctx);
  1103. for (i = 0; i < 1000000; i++) {
  1104. SHA1_Update(&sha_ctx, (uint8_t *)"a", 1);
  1105. }
  1106. SHA1_Final(digest, &sha_ctx);
  1107. bin2str(str, digest, sizeof(digest));
  1108. ck_assert_uint_eq(strlen(str), 40);
  1109. ck_assert_str_eq(str, "34aa973cd4c4daa4f61eeb2bdbad27316534016f");
  1110. /* a million "a" in blocks of 10 */
  1111. SHA1_Init(&sha_ctx);
  1112. for (i = 0; i < 100000; i++) {
  1113. SHA1_Update(&sha_ctx, (uint8_t *)"aaaaaaaaaa", 10);
  1114. }
  1115. SHA1_Final(digest, &sha_ctx);
  1116. bin2str(str, digest, sizeof(digest));
  1117. ck_assert_uint_eq(strlen(str), 40);
  1118. ck_assert_str_eq(str, "34aa973cd4c4daa4f61eeb2bdbad27316534016f");
  1119. #else
  1120. /* Can not test, if SHA1 is not included */
  1121. ck_assert(1);
  1122. #endif
  1123. }
  1124. END_TEST
  1125. START_TEST(test_config_options)
  1126. {
  1127. /* Check size of config_options vs. number of options in enum. */
  1128. ck_assert_ptr_eq(NULL, config_options[NUM_OPTIONS].name);
  1129. ck_assert_int_eq((int)MG_CONFIG_TYPE_UNKNOWN,
  1130. config_options[NUM_OPTIONS].type);
  1131. ck_assert_uint_eq(sizeof(config_options) / sizeof(config_options[0]),
  1132. (size_t)(NUM_OPTIONS + 1));
  1133. /* Check option enums vs. option names. */
  1134. /* Check if the order in
  1135. * static struct mg_option config_options[]
  1136. * is the same as in the option enum
  1137. * This test allows to reorder config_options and the enum,
  1138. * and check if the order is still consistent. */
  1139. ck_assert_str_eq("cgi_pattern", config_options[CGI_EXTENSIONS].name);
  1140. ck_assert_str_eq("cgi_environment", config_options[CGI_ENVIRONMENT].name);
  1141. ck_assert_str_eq("put_delete_auth_file",
  1142. config_options[PUT_DELETE_PASSWORDS_FILE].name);
  1143. ck_assert_str_eq("cgi_interpreter", config_options[CGI_INTERPRETER].name);
  1144. ck_assert_str_eq("protect_uri", config_options[PROTECT_URI].name);
  1145. ck_assert_str_eq("authentication_domain",
  1146. config_options[AUTHENTICATION_DOMAIN].name);
  1147. ck_assert_str_eq("enable_auth_domain_check",
  1148. config_options[ENABLE_AUTH_DOMAIN_CHECK].name);
  1149. ck_assert_str_eq("ssi_pattern", config_options[SSI_EXTENSIONS].name);
  1150. ck_assert_str_eq("throttle", config_options[THROTTLE].name);
  1151. ck_assert_str_eq("access_log_file", config_options[ACCESS_LOG_FILE].name);
  1152. ck_assert_str_eq("enable_directory_listing",
  1153. config_options[ENABLE_DIRECTORY_LISTING].name);
  1154. ck_assert_str_eq("error_log_file", config_options[ERROR_LOG_FILE].name);
  1155. ck_assert_str_eq("global_auth_file",
  1156. config_options[GLOBAL_PASSWORDS_FILE].name);
  1157. ck_assert_str_eq("index_files", config_options[INDEX_FILES].name);
  1158. ck_assert_str_eq("enable_keep_alive",
  1159. config_options[ENABLE_KEEP_ALIVE].name);
  1160. ck_assert_str_eq("access_control_list",
  1161. config_options[ACCESS_CONTROL_LIST].name);
  1162. ck_assert_str_eq("extra_mime_types", config_options[EXTRA_MIME_TYPES].name);
  1163. ck_assert_str_eq("listening_ports", config_options[LISTENING_PORTS].name);
  1164. ck_assert_str_eq("document_root", config_options[DOCUMENT_ROOT].name);
  1165. ck_assert_str_eq("ssl_certificate", config_options[SSL_CERTIFICATE].name);
  1166. ck_assert_str_eq("ssl_certificate_chain",
  1167. config_options[SSL_CERTIFICATE_CHAIN].name);
  1168. ck_assert_str_eq("num_threads", config_options[NUM_THREADS].name);
  1169. ck_assert_str_eq("run_as_user", config_options[RUN_AS_USER].name);
  1170. ck_assert_str_eq("url_rewrite_patterns",
  1171. config_options[URL_REWRITE_PATTERN].name);
  1172. ck_assert_str_eq("hide_files_patterns", config_options[HIDE_FILES].name);
  1173. ck_assert_str_eq("request_timeout_ms",
  1174. config_options[REQUEST_TIMEOUT].name);
  1175. ck_assert_str_eq("keep_alive_timeout_ms",
  1176. config_options[KEEP_ALIVE_TIMEOUT].name);
  1177. ck_assert_str_eq("linger_timeout_ms", config_options[LINGER_TIMEOUT].name);
  1178. ck_assert_str_eq("listen_backlog",
  1179. config_options[LISTEN_BACKLOG_SIZE].name);
  1180. ck_assert_str_eq("ssl_verify_peer",
  1181. config_options[SSL_DO_VERIFY_PEER].name);
  1182. ck_assert_str_eq("ssl_ca_path", config_options[SSL_CA_PATH].name);
  1183. ck_assert_str_eq("ssl_ca_file", config_options[SSL_CA_FILE].name);
  1184. ck_assert_str_eq("ssl_verify_depth", config_options[SSL_VERIFY_DEPTH].name);
  1185. ck_assert_str_eq("ssl_default_verify_paths",
  1186. config_options[SSL_DEFAULT_VERIFY_PATHS].name);
  1187. ck_assert_str_eq("ssl_cipher_list", config_options[SSL_CIPHER_LIST].name);
  1188. ck_assert_str_eq("ssl_protocol_version",
  1189. config_options[SSL_PROTOCOL_VERSION].name);
  1190. ck_assert_str_eq("ssl_short_trust", config_options[SSL_SHORT_TRUST].name);
  1191. #if defined(USE_WEBSOCKET)
  1192. ck_assert_str_eq("websocket_timeout_ms",
  1193. config_options[WEBSOCKET_TIMEOUT].name);
  1194. ck_assert_str_eq("enable_websocket_ping_pong",
  1195. config_options[ENABLE_WEBSOCKET_PING_PONG].name);
  1196. #endif
  1197. ck_assert_str_eq("decode_url", config_options[DECODE_URL].name);
  1198. ck_assert_str_eq("decode_query_string",
  1199. config_options[DECODE_QUERY_STRING].name);
  1200. #if defined(USE_LUA)
  1201. ck_assert_str_eq("lua_preload_file", config_options[LUA_PRELOAD_FILE].name);
  1202. ck_assert_str_eq("lua_script_pattern",
  1203. config_options[LUA_SCRIPT_EXTENSIONS].name);
  1204. ck_assert_str_eq("lua_server_page_pattern",
  1205. config_options[LUA_SERVER_PAGE_EXTENSIONS].name);
  1206. #endif
  1207. #if defined(USE_DUKTAPE)
  1208. ck_assert_str_eq("duktape_script_pattern",
  1209. config_options[DUKTAPE_SCRIPT_EXTENSIONS].name);
  1210. #endif
  1211. #if defined(USE_WEBSOCKET)
  1212. ck_assert_str_eq("websocket_root", config_options[WEBSOCKET_ROOT].name);
  1213. #endif
  1214. #if defined(USE_LUA) && defined(USE_WEBSOCKET)
  1215. ck_assert_str_eq("lua_websocket_pattern",
  1216. config_options[LUA_WEBSOCKET_EXTENSIONS].name);
  1217. #endif
  1218. ck_assert_str_eq("access_control_allow_origin",
  1219. config_options[ACCESS_CONTROL_ALLOW_ORIGIN].name);
  1220. ck_assert_str_eq("access_control_allow_methods",
  1221. config_options[ACCESS_CONTROL_ALLOW_METHODS].name);
  1222. ck_assert_str_eq("access_control_allow_headers",
  1223. config_options[ACCESS_CONTROL_ALLOW_HEADERS].name);
  1224. ck_assert_str_eq("error_pages", config_options[ERROR_PAGES].name);
  1225. ck_assert_str_eq("tcp_nodelay", config_options[CONFIG_TCP_NODELAY].name);
  1226. #if !defined(NO_CACHING)
  1227. ck_assert_str_eq("static_file_max_age",
  1228. config_options[STATIC_FILE_MAX_AGE].name);
  1229. #endif
  1230. #if !defined(NO_SSL)
  1231. ck_assert_str_eq("strict_transport_security_max_age",
  1232. config_options[STRICT_HTTPS_MAX_AGE].name);
  1233. #endif
  1234. #if defined(__linux__)
  1235. ck_assert_str_eq("allow_sendfile_call",
  1236. config_options[ALLOW_SENDFILE_CALL].name);
  1237. #endif
  1238. #if defined(_WIN32)
  1239. ck_assert_str_eq("case_sensitive",
  1240. config_options[CASE_SENSITIVE_FILES].name);
  1241. #endif
  1242. #if defined(USE_LUA)
  1243. ck_assert_str_eq("lua_background_script",
  1244. config_options[LUA_BACKGROUND_SCRIPT].name);
  1245. ck_assert_str_eq("lua_background_script_params",
  1246. config_options[LUA_BACKGROUND_SCRIPT_PARAMS].name);
  1247. #endif
  1248. ck_assert_str_eq("additional_header",
  1249. config_options[ADDITIONAL_HEADER].name);
  1250. ck_assert_str_eq("max_request_size", config_options[MAX_REQUEST_SIZE].name);
  1251. ck_assert_str_eq("allow_index_script_resource",
  1252. config_options[ALLOW_INDEX_SCRIPT_SUB_RES].name);
  1253. }
  1254. END_TEST
  1255. #if !defined(REPLACE_CHECK_FOR_LOCAL_DEBUGGING)
  1256. Suite *
  1257. make_private_suite(void)
  1258. {
  1259. Suite *const suite = suite_create("Private");
  1260. TCase *const tcase_http_message = tcase_create("HTTP Message");
  1261. TCase *const tcase_http_keep_alive = tcase_create("HTTP Keep Alive");
  1262. TCase *const tcase_url_parsing_1 = tcase_create("URL Parsing 1");
  1263. TCase *const tcase_url_parsing_2 = tcase_create("URL Parsing 2");
  1264. TCase *const tcase_url_parsing_3 = tcase_create("URL Parsing 3");
  1265. TCase *const tcase_internal_parse_1 = tcase_create("Internal Parsing 1");
  1266. TCase *const tcase_internal_parse_2 = tcase_create("Internal Parsing 2");
  1267. TCase *const tcase_internal_parse_3 = tcase_create("Internal Parsing 3");
  1268. TCase *const tcase_internal_parse_4 = tcase_create("Internal Parsing 4");
  1269. TCase *const tcase_internal_parse_5 = tcase_create("Internal Parsing 5");
  1270. TCase *const tcase_internal_parse_6 = tcase_create("Internal Parsing 6");
  1271. TCase *const tcase_internal_parse_7 = tcase_create("Internal Parsing 7");
  1272. TCase *const tcase_encode_decode = tcase_create("Encode Decode");
  1273. TCase *const tcase_mask_data = tcase_create("Mask Data");
  1274. TCase *const tcase_parse_date_string = tcase_create("Date Parsing");
  1275. TCase *const tcase_sha1 = tcase_create("SHA1");
  1276. TCase *const tcase_config_options = tcase_create("Config Options");
  1277. tcase_add_test(tcase_http_message, test_parse_http_message);
  1278. tcase_set_timeout(tcase_http_message, civetweb_min_test_timeout);
  1279. suite_add_tcase(suite, tcase_http_message);
  1280. tcase_add_test(tcase_http_keep_alive, test_should_keep_alive);
  1281. tcase_set_timeout(tcase_http_keep_alive, civetweb_min_test_timeout);
  1282. suite_add_tcase(suite, tcase_http_keep_alive);
  1283. tcase_add_test(tcase_url_parsing_1, test_match_prefix);
  1284. tcase_add_test(tcase_url_parsing_1, test_match_prefix_strlen);
  1285. tcase_set_timeout(tcase_url_parsing_1, civetweb_min_test_timeout);
  1286. suite_add_tcase(suite, tcase_url_parsing_1);
  1287. tcase_add_test(tcase_url_parsing_2, test_remove_dot_segments);
  1288. tcase_set_timeout(tcase_url_parsing_2, civetweb_min_test_timeout);
  1289. suite_add_tcase(suite, tcase_url_parsing_2);
  1290. tcase_add_test(tcase_url_parsing_3, test_is_valid_uri);
  1291. tcase_set_timeout(tcase_url_parsing_3, civetweb_min_test_timeout);
  1292. suite_add_tcase(suite, tcase_url_parsing_3);
  1293. tcase_add_test(tcase_internal_parse_1, test_next_option);
  1294. tcase_set_timeout(tcase_internal_parse_1, civetweb_min_test_timeout);
  1295. suite_add_tcase(suite, tcase_internal_parse_1);
  1296. tcase_add_test(tcase_internal_parse_2, test_skip_quoted);
  1297. tcase_set_timeout(tcase_internal_parse_2, civetweb_min_test_timeout);
  1298. suite_add_tcase(suite, tcase_internal_parse_2);
  1299. tcase_add_test(tcase_internal_parse_3, test_mg_strcasestr);
  1300. tcase_set_timeout(tcase_internal_parse_3, civetweb_min_test_timeout);
  1301. suite_add_tcase(suite, tcase_internal_parse_3);
  1302. tcase_add_test(tcase_internal_parse_4, test_alloc_vprintf);
  1303. tcase_set_timeout(tcase_internal_parse_4, civetweb_min_test_timeout);
  1304. suite_add_tcase(suite, tcase_internal_parse_4);
  1305. tcase_add_test(tcase_internal_parse_5, test_mg_vsnprintf);
  1306. tcase_set_timeout(tcase_internal_parse_5, civetweb_min_test_timeout);
  1307. suite_add_tcase(suite, tcase_internal_parse_5);
  1308. tcase_add_test(tcase_internal_parse_6, test_parse_port_string);
  1309. tcase_set_timeout(tcase_internal_parse_6, civetweb_min_test_timeout);
  1310. suite_add_tcase(suite, tcase_internal_parse_6);
  1311. tcase_add_test(tcase_internal_parse_7, test_parse_http_headers);
  1312. tcase_set_timeout(tcase_internal_parse_7, civetweb_min_test_timeout);
  1313. suite_add_tcase(suite, tcase_internal_parse_7);
  1314. tcase_add_test(tcase_encode_decode, test_encode_decode);
  1315. tcase_set_timeout(tcase_encode_decode, civetweb_min_test_timeout);
  1316. suite_add_tcase(suite, tcase_encode_decode);
  1317. tcase_add_test(tcase_mask_data, test_mask_data);
  1318. tcase_set_timeout(tcase_mask_data, civetweb_min_test_timeout);
  1319. suite_add_tcase(suite, tcase_mask_data);
  1320. tcase_add_test(tcase_parse_date_string, test_parse_date_string);
  1321. tcase_set_timeout(tcase_parse_date_string, civetweb_min_test_timeout);
  1322. suite_add_tcase(suite, tcase_parse_date_string);
  1323. tcase_add_test(tcase_sha1, test_sha1);
  1324. tcase_set_timeout(tcase_sha1, civetweb_min_test_timeout);
  1325. suite_add_tcase(suite, tcase_sha1);
  1326. tcase_add_test(tcase_config_options, test_config_options);
  1327. tcase_set_timeout(tcase_config_options, civetweb_min_test_timeout);
  1328. suite_add_tcase(suite, tcase_config_options);
  1329. return suite;
  1330. }
  1331. #endif
  1332. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  1333. /* Used to debug test cases without using the check framework */
  1334. void
  1335. MAIN_PRIVATE(void)
  1336. {
  1337. #if defined(_WIN32)
  1338. /* test_parse_port_string requires WSAStartup for IPv6 */
  1339. WSADATA data;
  1340. WSAStartup(MAKEWORD(2, 2), &data);
  1341. #endif
  1342. test_alloc_vprintf(0);
  1343. test_mg_vsnprintf(0);
  1344. test_remove_dot_segments(0);
  1345. test_parse_date_string(0);
  1346. test_parse_port_string(0);
  1347. test_parse_http_message(0);
  1348. test_parse_http_headers(0);
  1349. test_sha1(0);
  1350. #if defined(_WIN32)
  1351. WSACleanup();
  1352. #endif
  1353. }
  1354. #endif