private.c 49 KB

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