private.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  1. /* Copyright (c) 2015-2019 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. * We include the source file so that we have access to the internal private
  23. * static functions
  24. */
  25. #ifdef _MSC_VER
  26. #ifndef _CRT_SECURE_NO_WARNINGS
  27. #define _CRT_SECURE_NO_WARNINGS
  28. #endif
  29. #endif
  30. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  31. #undef MEMORY_DEBUGGING
  32. #endif
  33. #ifdef _MSC_VER
  34. /* Since the C file is included, declare all API functions as static,
  35. * to avoid linker errors in the test (seems to be required for MS
  36. * toolchain).
  37. */
  38. #define CIVETWEB_API static
  39. #endif
  40. /* Include C File, so static functions can be called from here */
  41. #include "civetweb.h"
  42. #include "../src/civetweb.c"
  43. /* Standard includes for the test code below */
  44. #include <stdlib.h>
  45. #include <time.h>
  46. #include "private.h"
  47. /* This unit test file uses the excellent Check unit testing library.
  48. * The API documentation is available here:
  49. * http://check.sourceforge.net/doc/check_html/index.html
  50. */
  51. static char tmp_parse_buffer[1024];
  52. static int
  53. test_parse_http_response(char *buf, int len, struct mg_response_info *ri)
  54. {
  55. ck_assert_int_lt(len, (int)sizeof(tmp_parse_buffer));
  56. memcpy(tmp_parse_buffer, buf, (size_t)len);
  57. return parse_http_response(tmp_parse_buffer, len, ri);
  58. }
  59. static int
  60. test_parse_http_request(char *buf, int len, struct mg_request_info *ri)
  61. {
  62. ck_assert_int_lt(len, (int)sizeof(tmp_parse_buffer));
  63. memcpy(tmp_parse_buffer, buf, (size_t)len);
  64. return parse_http_request(tmp_parse_buffer, len, ri);
  65. }
  66. START_TEST(test_parse_http_message)
  67. {
  68. /* Adapted from unit_test.c */
  69. /* Copyright (c) 2013-2015 the Civetweb developers */
  70. /* Copyright (c) 2004-2013 Sergey Lyubka */
  71. struct mg_request_info ri;
  72. struct mg_response_info respi;
  73. char empty[] = "";
  74. char space[] = " \x00";
  75. char req1[] = "GET / HTTP/1.1\r\n\r\n";
  76. char req2[] = "BLAH / HTTP/1.1\r\n\r\n";
  77. char req3[] = "GET / HTTP/1.1\nKey: Val\n\n";
  78. char req4[] =
  79. "GET / HTTP/1.1\r\nA: foo bar\r\nB: bar\r\nskip\r\nbaz:\r\n\r\n";
  80. char req5[] = "GET / HTTP/1.0\n\n";
  81. char req6[] = "G";
  82. char req7[] = " blah ";
  83. char req8[] = "HTTP/1.0 404 Not Found\n\n";
  84. char req9[] = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n";
  85. char req10[] = "GET / HTTP/1.1\r\nA: foo bar\r\nB: bar\r\n\r\n";
  86. char req11[] = "GET /\r\nError: X\r\n\r\n";
  87. char req12[] =
  88. "POST /a/b/c.d?e=f&g HTTP/1.1\r\nKey1: val1\r\nKey2: val2\r\n\r\nBODY";
  89. int lenreq1 = (int)strlen(req1);
  90. int lenreq2 = (int)strlen(req2);
  91. int lenreq3 = (int)strlen(req3);
  92. int lenreq4 = (int)strlen(req4);
  93. int lenreq5 = (int)strlen(req5);
  94. int lenreq6 = (int)strlen(req6);
  95. int lenreq7 = (int)strlen(req7);
  96. int lenreq8 = (int)strlen(req8);
  97. int lenreq9 = (int)strlen(req9);
  98. int lenreq10 = (int)strlen(req10);
  99. int lenreq11 = (int)strlen(req11);
  100. int lenreq12 = (int)strlen(req12);
  101. int lenhdr12 = lenreq12 - 4; /* length without body */
  102. mark_point();
  103. /* An empty string is neither a complete request nor a complete
  104. * response, so it must return 0 */
  105. ck_assert_int_eq(0, get_http_header_len(empty, 0));
  106. ck_assert_int_eq(0, test_parse_http_request(empty, 0, &ri));
  107. ck_assert_int_eq(0, test_parse_http_response(empty, 0, &respi));
  108. /* Same is true for a leading space */
  109. ck_assert_int_eq(0, get_http_header_len(space, 1));
  110. ck_assert_int_eq(0, test_parse_http_request(space, 1, &ri));
  111. ck_assert_int_eq(0, test_parse_http_response(space, 1, &respi));
  112. /* But a control character (like 0) makes it invalid */
  113. ck_assert_int_eq(-1, get_http_header_len(space, 2));
  114. ck_assert_int_eq(-1, test_parse_http_request(space, 2, &ri));
  115. ck_assert_int_eq(-1, test_parse_http_response(space, 2, &respi));
  116. /* req1 minus 1 byte at the end is incomplete */
  117. ck_assert_int_eq(0, get_http_header_len(req1, lenreq1 - 1));
  118. /* req1 minus 1 byte at the start is complete but invalid */
  119. ck_assert_int_eq(lenreq1 - 1, get_http_header_len(req1 + 1, lenreq1 - 1));
  120. ck_assert_int_eq(-1, test_parse_http_request(req1 + 1, lenreq1 - 1, &ri));
  121. /* req1 is a valid request */
  122. ck_assert_int_eq(lenreq1, get_http_header_len(req1, lenreq1));
  123. ck_assert_int_eq(-1, test_parse_http_response(req1, lenreq1, &respi));
  124. ck_assert_int_eq(lenreq1, test_parse_http_request(req1, lenreq1, &ri));
  125. ck_assert_str_eq("1.1", ri.http_version);
  126. ck_assert_int_eq(0, ri.num_headers);
  127. /* req2 is a complete, but invalid request */
  128. ck_assert_int_eq(lenreq2, get_http_header_len(req2, lenreq2));
  129. ck_assert_int_eq(-1, test_parse_http_request(req2, lenreq2, &ri));
  130. /* req3 is a complete and valid request */
  131. ck_assert_int_eq(lenreq3, get_http_header_len(req3, lenreq3));
  132. ck_assert_int_eq(lenreq3, test_parse_http_request(req3, lenreq3, &ri));
  133. ck_assert_int_eq(-1, test_parse_http_response(req3, lenreq3, &respi));
  134. /* Multiline header are obsolete, so return an error
  135. * (https://tools.ietf.org/html/rfc7230#section-3.2.4). */
  136. ck_assert_int_eq(-1, test_parse_http_request(req4, lenreq4, &ri));
  137. /* req5 is a complete and valid request (also somewhat malformed,
  138. * since it uses \n\n instead of \r\n\r\n) */
  139. ck_assert_int_eq(lenreq5, get_http_header_len(req5, lenreq5));
  140. ck_assert_int_eq(-1, test_parse_http_response(req5, lenreq5, &respi));
  141. ck_assert_int_eq(lenreq5, test_parse_http_request(req5, lenreq5, &ri));
  142. ck_assert_str_eq("GET", ri.request_method);
  143. ck_assert_str_eq("1.0", ri.http_version);
  144. /* req6 is incomplete */
  145. ck_assert_int_eq(0, get_http_header_len(req6, lenreq6));
  146. ck_assert_int_eq(0, test_parse_http_request(req6, lenreq6, &ri));
  147. /* req7 is invalid, but not yet complete */
  148. ck_assert_int_eq(0, get_http_header_len(req7, lenreq7));
  149. ck_assert_int_eq(0, test_parse_http_request(req7, lenreq7, &ri));
  150. /* req8 is a valid response */
  151. ck_assert_int_eq(lenreq8, get_http_header_len(req8, lenreq8));
  152. ck_assert_int_eq(-1, test_parse_http_request(req8, lenreq8, &ri));
  153. ck_assert_int_eq(lenreq8, test_parse_http_response(req8, lenreq8, &respi));
  154. /* req9 is a valid response */
  155. ck_assert_int_eq(lenreq9, get_http_header_len(req9, lenreq9));
  156. ck_assert_int_eq(-1, test_parse_http_request(req9, lenreq9, &ri));
  157. ck_assert_int_eq(lenreq9, test_parse_http_response(req9, lenreq9, &respi));
  158. ck_assert_int_eq(1, respi.num_headers);
  159. /* req10 is a valid request */
  160. ck_assert_int_eq(lenreq10, get_http_header_len(req10, lenreq10));
  161. ck_assert_int_eq(lenreq10, test_parse_http_request(req10, lenreq10, &ri));
  162. ck_assert_str_eq("1.1", ri.http_version);
  163. ck_assert_int_eq(2, ri.num_headers);
  164. ck_assert_str_eq("A", ri.http_headers[0].name);
  165. ck_assert_str_eq("foo bar", ri.http_headers[0].value);
  166. ck_assert_str_eq("B", ri.http_headers[1].name);
  167. ck_assert_str_eq("bar", ri.http_headers[1].value);
  168. /* req11 is a complete but valid request */
  169. ck_assert_int_eq(-1, test_parse_http_request(req11, lenreq11, &ri));
  170. /* req12 is a valid request with body data */
  171. ck_assert_int_gt(lenreq12, lenhdr12);
  172. ck_assert_int_eq(lenhdr12, get_http_header_len(req12, lenreq12));
  173. ck_assert_int_eq(lenhdr12, test_parse_http_request(req12, lenreq12, &ri));
  174. }
  175. END_TEST
  176. START_TEST(test_should_keep_alive)
  177. {
  178. /* Adapted from unit_test.c */
  179. /* Copyright (c) 2013-2015 the Civetweb developers */
  180. /* Copyright (c) 2004-2013 Sergey Lyubka */
  181. struct mg_connection conn;
  182. struct mg_context ctx;
  183. char req1[] = "GET / HTTP/1.1\r\n\r\n";
  184. char req2[] = "GET / HTTP/1.0\r\n\r\n";
  185. char req3[] = "GET / HTTP/1.1\r\nConnection: close\r\n\r\n";
  186. char req4[] = "GET / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";
  187. char yes[] = "yes";
  188. char no[] = "no";
  189. int lenreq1 = (int)strlen(req1);
  190. int lenreq2 = (int)strlen(req2);
  191. int lenreq3 = (int)strlen(req3);
  192. int lenreq4 = (int)strlen(req4);
  193. mark_point();
  194. memset(&ctx, 0, sizeof(ctx));
  195. memset(&conn, 0, sizeof(conn));
  196. conn.phys_ctx = &ctx;
  197. conn.dom_ctx = &(ctx.dd);
  198. ck_assert_int_eq(test_parse_http_request(req1, lenreq1, &conn.request_info),
  199. lenreq1);
  200. conn.connection_type = 1; /* Valid request */
  201. ck_assert_int_eq(conn.request_info.num_headers, 0);
  202. ctx.dd.config[ENABLE_KEEP_ALIVE] = no;
  203. ck_assert_int_eq(should_keep_alive(&conn), 0);
  204. ctx.dd.config[ENABLE_KEEP_ALIVE] = yes;
  205. ck_assert_int_eq(should_keep_alive(&conn), 1);
  206. conn.must_close = 1;
  207. ck_assert_int_eq(should_keep_alive(&conn), 0);
  208. conn.must_close = 0;
  209. test_parse_http_request(req2, lenreq2, &conn.request_info);
  210. conn.connection_type = 1; /* Valid request */
  211. ck_assert_int_eq(conn.request_info.num_headers, 0);
  212. ck_assert_int_eq(should_keep_alive(&conn), 0);
  213. test_parse_http_request(req3, lenreq3, &conn.request_info);
  214. conn.connection_type = 1; /* Valid request */
  215. ck_assert_int_eq(conn.request_info.num_headers, 1);
  216. ck_assert_int_eq(should_keep_alive(&conn), 0);
  217. test_parse_http_request(req4, lenreq4, &conn.request_info);
  218. conn.connection_type = 1; /* Valid request */
  219. ck_assert_int_eq(conn.request_info.num_headers, 1);
  220. ck_assert_int_eq(should_keep_alive(&conn), 1);
  221. conn.status_code = 200;
  222. conn.must_close = 1;
  223. ck_assert_int_eq(should_keep_alive(&conn), 0);
  224. conn.status_code = 200;
  225. conn.must_close = 0;
  226. ck_assert_int_eq(should_keep_alive(&conn), 1);
  227. conn.status_code = 200;
  228. conn.must_close = 0;
  229. conn.connection_type = 0; /* invalid */
  230. ck_assert_int_eq(should_keep_alive(&conn), 0);
  231. }
  232. END_TEST
  233. START_TEST(test_match_prefix)
  234. {
  235. /* Adapted from unit_test.c */
  236. /* Copyright (c) 2013-2015 the Civetweb developers */
  237. /* Copyright (c) 2004-2013 Sergey Lyubka */
  238. ck_assert_int_eq(4, match_prefix("/api", 4, "/api"));
  239. ck_assert_int_eq(3, match_prefix("/a/", 3, "/a/b/c"));
  240. ck_assert_int_eq(-1, match_prefix("/a/", 3, "/ab/c"));
  241. ck_assert_int_eq(4, match_prefix("/*/", 3, "/ab/c"));
  242. ck_assert_int_eq(6, match_prefix("**", 2, "/a/b/c"));
  243. ck_assert_int_eq(2, match_prefix("/*", 2, "/a/b/c"));
  244. ck_assert_int_eq(2, match_prefix("*/*", 3, "/a/b/c"));
  245. ck_assert_int_eq(5, match_prefix("**/", 3, "/a/b/c"));
  246. ck_assert_int_eq(5, match_prefix("**.foo|**.bar", 13, "a.bar"));
  247. ck_assert_int_eq(2, match_prefix("a|b|cd", 6, "cdef"));
  248. ck_assert_int_eq(2, match_prefix("a|b|c?", 6, "cdef"));
  249. ck_assert_int_eq(1, match_prefix("a|?|cd", 6, "cdef"));
  250. ck_assert_int_eq(-1, match_prefix("/a/**.cgi", 9, "/foo/bar/x.cgi"));
  251. ck_assert_int_eq(12, match_prefix("/a/**.cgi", 9, "/a/bar/x.cgi"));
  252. ck_assert_int_eq(5, match_prefix("**/", 3, "/a/b/c"));
  253. ck_assert_int_eq(-1, match_prefix("**/$", 4, "/a/b/c"));
  254. ck_assert_int_eq(5, match_prefix("**/$", 4, "/a/b/"));
  255. ck_assert_int_eq(0, match_prefix("$", 1, ""));
  256. ck_assert_int_eq(-1, match_prefix("$", 1, "x"));
  257. ck_assert_int_eq(1, match_prefix("*$", 2, "x"));
  258. ck_assert_int_eq(1, match_prefix("/$", 2, "/"));
  259. ck_assert_int_eq(-1, match_prefix("**/$", 4, "/a/b/c"));
  260. ck_assert_int_eq(5, match_prefix("**/$", 4, "/a/b/"));
  261. ck_assert_int_eq(0, match_prefix("*", 1, "/hello/"));
  262. ck_assert_int_eq(-1, match_prefix("**.a$|**.b$", 11, "/a/b.b/"));
  263. ck_assert_int_eq(6, match_prefix("**.a$|**.b$", 11, "/a/b.b"));
  264. ck_assert_int_eq(6, match_prefix("**.a$|**.b$", 11, "/a/B.A"));
  265. ck_assert_int_eq(5, match_prefix("**o$", 4, "HELLO"));
  266. }
  267. END_TEST
  268. START_TEST(test_remove_dot_segments)
  269. {
  270. struct {
  271. const char *input;
  272. const char *expected_output;
  273. } tests[] = {{"/path/to/file.ext", "/path/to/file.ext"},
  274. {"/file.ext", "/file.ext"},
  275. {"/path/../file.ext", "/file.ext"},
  276. {"/../to/file.ext", "/to/file.ext"},
  277. {"/../../file.ext", "/file.ext"},
  278. {"/./../file.ext", "/file.ext"},
  279. {"/.././file.ext", "/file.ext"},
  280. {"/././file.ext", "/file.ext"},
  281. {"/././file.ext", "/file.ext"},
  282. {"/path/.to/..file.ext", "/path/.to/..file.ext"},
  283. {"/file", "/file"},
  284. {"/path/", "/path/"},
  285. {"file.ext", "file.ext"},
  286. {"./file.ext", "file.ext"},
  287. {"../file.ext", "file.ext"},
  288. {".file.ext", ".file.ext"},
  289. {"..file.ext", "..file.ext"},
  290. {"file", "file"},
  291. /* Windows specific */
  292. {"\\file.ext", "/file.ext"},
  293. {"\\..\\file.ext", "/file.ext"},
  294. {"/file.", "/file"},
  295. {"/path\\to.\\.\\file.", "/path/to/file"},
  296. {NULL, NULL}};
  297. mark_point();
  298. for (int i = 0; (tests[i].input != NULL); i++) {
  299. char inout[256];
  300. strcpy(inout, tests[i].input);
  301. remove_dot_segments(inout);
  302. ck_assert_str_eq(inout, tests[i].expected_output);
  303. }
  304. }
  305. END_TEST
  306. START_TEST(test_is_valid_uri)
  307. {
  308. /* is_valid_uri is superseeded by get_uri_type */
  309. ck_assert_int_eq(2, get_uri_type("/api"));
  310. ck_assert_int_eq(2, get_uri_type("/api/"));
  311. ck_assert_int_eq(2,
  312. get_uri_type("/some/long/path%20with%20space/file.xyz"));
  313. ck_assert_int_eq(0, get_uri_type("api"));
  314. ck_assert_int_eq(1, get_uri_type("*"));
  315. ck_assert_int_eq(0, get_uri_type("*xy"));
  316. ck_assert_int_eq(3, get_uri_type("http://somewhere/"));
  317. ck_assert_int_eq(3, get_uri_type("https://somewhere/some/file.html"));
  318. ck_assert_int_eq(4, get_uri_type("http://somewhere:8080/"));
  319. ck_assert_int_eq(4, get_uri_type("https://somewhere:8080/some/file.html"));
  320. }
  321. END_TEST
  322. START_TEST(test_next_option)
  323. {
  324. /* Adapted from unit_test.c */
  325. /* Copyright (c) 2013-2015 the Civetweb developers */
  326. /* Copyright (c) 2004-2013 Sergey Lyubka */
  327. const char *p, *list = "x/8,/y**=1;2k,z";
  328. struct vec a, b;
  329. int i;
  330. mark_point();
  331. ck_assert(next_option(NULL, &a, &b) == NULL);
  332. for (i = 0, p = list; (p = next_option(p, &a, &b)) != NULL; i++) {
  333. ck_assert(i != 0 || (a.ptr == list && a.len == 3 && b.len == 0));
  334. ck_assert(i != 1
  335. || (a.ptr == list + 4 && a.len == 4 && b.ptr == list + 9
  336. && b.len == 4));
  337. ck_assert(i != 2 || (a.ptr == list + 14 && a.len == 1 && b.len == 0));
  338. }
  339. }
  340. END_TEST
  341. START_TEST(test_skip_quoted)
  342. {
  343. /* Adapted from unit_test.c */
  344. /* Copyright (c) 2013-2015 the Civetweb developers */
  345. /* Copyright (c) 2004-2013 Sergey Lyubka */
  346. char x[] = "a=1, b=2, c='hi \' there', d='here\\, there'", *s = x, *p;
  347. mark_point();
  348. p = skip_quoted(&s, ", ", ", ", 0);
  349. ck_assert(p != NULL && !strcmp(p, "a=1"));
  350. p = skip_quoted(&s, ", ", ", ", 0);
  351. ck_assert(p != NULL && !strcmp(p, "b=2"));
  352. p = skip_quoted(&s, ",", " ", 0);
  353. ck_assert(p != NULL && !strcmp(p, "c='hi \' there'"));
  354. p = skip_quoted(&s, ",", " ", '\\');
  355. ck_assert(p != NULL && !strcmp(p, "d='here, there'"));
  356. ck_assert(*s == 0);
  357. }
  358. END_TEST
  359. static int
  360. alloc_printf(char **buf, size_t size, const char *fmt, ...)
  361. {
  362. /* Test helper function - adapted from unit_test.c */
  363. /* Copyright (c) 2013-2015 the Civetweb developers */
  364. /* Copyright (c) 2004-2013 Sergey Lyubka */
  365. va_list ap;
  366. int ret = 0;
  367. mark_point();
  368. va_start(ap, fmt);
  369. ret = alloc_vprintf(buf, *buf, size, fmt, ap);
  370. va_end(ap);
  371. return ret;
  372. }
  373. static int
  374. alloc_printf2(char **buf, const char *fmt, ...)
  375. {
  376. /* Test alternative implementation */
  377. va_list ap;
  378. int ret = 0;
  379. mark_point();
  380. va_start(ap, fmt);
  381. ret = alloc_vprintf2(buf, fmt, ap);
  382. va_end(ap);
  383. return ret;
  384. }
  385. START_TEST(test_alloc_vprintf)
  386. {
  387. /* Adapted from unit_test.c */
  388. /* Copyright (c) 2013-2015 the Civetweb developers */
  389. /* Copyright (c) 2004-2013 Sergey Lyubka */
  390. char buf[MG_BUF_LEN], *p = buf;
  391. mark_point();
  392. ck_assert(alloc_printf(&p, sizeof(buf), "%s", "hi") == 2);
  393. ck_assert(p == buf);
  394. ck_assert(alloc_printf(&p, sizeof(buf), "%s", "") == 0);
  395. ck_assert(p == buf);
  396. ck_assert(alloc_printf(&p, sizeof(buf), "") == 0);
  397. ck_assert(p == buf);
  398. /* Pass small buffer, make sure alloc_printf allocates */
  399. ck_assert(alloc_printf(&p, 1, "%s", "hello") == 5);
  400. ck_assert(p != buf);
  401. mg_free(p);
  402. p = buf;
  403. /* Test alternative implementation */
  404. ck_assert(alloc_printf2(&p, "%s", "hello") == 5);
  405. ck_assert(p != buf);
  406. mg_free(p);
  407. p = buf;
  408. }
  409. END_TEST
  410. START_TEST(test_mg_vsnprintf)
  411. {
  412. char buf[16];
  413. int is_trunc;
  414. #if defined(_WIN32)
  415. /* If the string is truncated, mg_snprintf calls mg_cry.
  416. * If DEBUG is defined, mg_cry calls DEBUG_TRACE.
  417. * In DEBUG_TRACE_FUNC, flockfile(stdout) is called.
  418. * For Windows, flockfile/funlockfile calls
  419. * pthread_mutex_lock/_unlock(&global_log_file_lock).
  420. * So, we need to initialize global_log_file_lock:
  421. */
  422. pthread_mutex_init(&global_log_file_lock, &pthread_mutex_attr);
  423. #endif
  424. memset(buf, 0, sizeof(buf));
  425. mark_point();
  426. is_trunc = 777;
  427. mg_snprintf(NULL, &is_trunc, buf, 10, "%8i", 123);
  428. ck_assert_str_eq(buf, " 123");
  429. ck_assert_int_eq(is_trunc, 0);
  430. is_trunc = 777;
  431. mg_snprintf(NULL, &is_trunc, buf, 10, "%9i", 123);
  432. ck_assert_str_eq(buf, " 123");
  433. ck_assert_int_eq(is_trunc, 0);
  434. is_trunc = 777;
  435. mg_snprintf(NULL, &is_trunc, buf, 9, "%9i", 123);
  436. ck_assert_str_eq(buf, " 12");
  437. ck_assert_int_eq(is_trunc, 1);
  438. is_trunc = 777;
  439. mg_snprintf(NULL, &is_trunc, buf, 8, "%9i", 123);
  440. ck_assert_str_eq(buf, " 1");
  441. ck_assert_int_eq(is_trunc, 1);
  442. is_trunc = 777;
  443. mg_snprintf(NULL, &is_trunc, buf, 7, "%9i", 123);
  444. ck_assert_str_eq(buf, " ");
  445. ck_assert_int_eq(is_trunc, 1);
  446. is_trunc = 777;
  447. strcpy(buf, "1234567890");
  448. mg_snprintf(NULL, &is_trunc, buf, 0, "%i", 543);
  449. ck_assert_str_eq(buf, "1234567890");
  450. ck_assert_int_eq(is_trunc, 1);
  451. }
  452. END_TEST
  453. START_TEST(test_mg_strcasestr)
  454. {
  455. /* Adapted from unit_test.c */
  456. /* Copyright (c) 2013-2015 the Civetweb developers */
  457. /* Copyright (c) 2004-2013 Sergey Lyubka */
  458. static const char *big1 = "abcdef";
  459. mark_point();
  460. ck_assert(mg_strcasestr("Y", "X") == NULL);
  461. ck_assert(mg_strcasestr("Y", "y") != NULL);
  462. ck_assert(mg_strcasestr(big1, "X") == NULL);
  463. ck_assert(mg_strcasestr(big1, "CD") == big1 + 2);
  464. ck_assert(mg_strcasestr("aa", "AAB") == NULL);
  465. }
  466. END_TEST
  467. START_TEST(test_parse_port_string)
  468. {
  469. /* Adapted from unit_test.c */
  470. /* Copyright (c) 2013-2018 the Civetweb developers */
  471. /* Copyright (c) 2004-2013 Sergey Lyubka */
  472. struct t_test_parse_port_string {
  473. const char *port_string;
  474. int valid;
  475. int ip_family;
  476. };
  477. static struct t_test_parse_port_string testdata[] =
  478. { {"0", 1, 4},
  479. {"1", 1, 4},
  480. {"65535", 1, 4},
  481. {"65536", 0, 0},
  482. {"1s", 1, 4},
  483. {"1r", 1, 4},
  484. {"1k", 0, 0},
  485. {"1.2.3", 0, 0},
  486. {"1.2.3.", 0, 0},
  487. {"1.2.3.4", 0, 0},
  488. {"1.2.3.4:", 0, 0},
  489. {"1.2.3.4:0", 1, 4},
  490. {"1.2.3.4:1", 1, 4},
  491. {"1.2.3.4:65535", 1, 4},
  492. {"1.2.3.4:65536", 0, 0},
  493. {"1.2.3.4:1s", 1, 4},
  494. {"1.2.3.4:1r", 1, 4},
  495. {"1.2.3.4:1k", 0, 0},
  496. #if defined(USE_IPV6)
  497. /* IPv6 config */
  498. {"[::1]:123", 1, 6},
  499. {"[::]:80", 1, 6},
  500. {"[3ffe:2a00:100:7031::1]:900", 1, 6},
  501. /* IPv4 + IPv6 config */
  502. {"+80", 1, 4 + 6},
  503. #else
  504. /* IPv6 config: invalid if IPv6 is not activated */
  505. {"[::1]:123", 0, 0},
  506. {"[::]:80", 0, 0},
  507. {"[3ffe:2a00:100:7031::1]:900", 0, 0},
  508. /* IPv4 + IPv6 config: only IPv4 if IPv6 is not activated */
  509. {"+80", 1, 4},
  510. #endif
  511. {NULL, 0, 0} };
  512. struct socket so;
  513. struct vec vec;
  514. int ip_family;
  515. int i, ret;
  516. mark_point();
  517. for (i = 0; testdata[i].port_string != NULL; i++) {
  518. vec.ptr = testdata[i].port_string;
  519. vec.len = strlen(vec.ptr);
  520. ip_family = 123;
  521. ret = parse_port_string(&vec, &so, &ip_family);
  522. if ((ret != testdata[i].valid)
  523. || (ip_family != testdata[i].ip_family)) {
  524. ck_abort_msg("Port string [%s]: "
  525. "expected valid=%i, family=%i; "
  526. "got valid=%i, family=%i",
  527. testdata[i].port_string,
  528. testdata[i].valid,
  529. testdata[i].ip_family,
  530. ret,
  531. ip_family);
  532. }
  533. }
  534. }
  535. END_TEST
  536. START_TEST(test_encode_decode)
  537. {
  538. char buf[128];
  539. const char *alpha = "abcdefghijklmnopqrstuvwxyz";
  540. const char *nonalpha = " !\"#$%&'()*+,-./0123456789:;<=>?@";
  541. const char *nonalpha_url_enc1 =
  542. "%20%21%22%23$%25%26%27()%2a%2b,-.%2f0123456789%3a;%3c%3d%3e%3f%40";
  543. const char *nonalpha_url_enc2 =
  544. "%20!%22%23%24%25%26'()*%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40";
  545. int ret;
  546. size_t len;
  547. #if defined(USE_WEBSOCKET) || defined(USE_LUA)
  548. const char *alpha_b64_enc = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXo=";
  549. const char *nonalpha_b64_enc =
  550. "ICEiIyQlJicoKSorLC0uLzAxMjM0NTY3ODk6Ozw9Pj9A";
  551. mark_point();
  552. memset(buf, 77, sizeof(buf));
  553. base64_encode((unsigned char *)"a", 1, buf);
  554. ck_assert_str_eq(buf, "YQ==");
  555. memset(buf, 77, sizeof(buf));
  556. base64_encode((unsigned char *)"ab", 1, buf);
  557. ck_assert_str_eq(buf, "YQ==");
  558. memset(buf, 77, sizeof(buf));
  559. base64_encode((unsigned char *)"ab", 2, buf);
  560. ck_assert_str_eq(buf, "YWI=");
  561. memset(buf, 77, sizeof(buf));
  562. base64_encode((unsigned char *)alpha, 3, buf);
  563. ck_assert_str_eq(buf, "YWJj");
  564. memset(buf, 77, sizeof(buf));
  565. base64_encode((unsigned char *)alpha, 4, buf);
  566. ck_assert_str_eq(buf, "YWJjZA==");
  567. memset(buf, 77, sizeof(buf));
  568. base64_encode((unsigned char *)alpha, 5, buf);
  569. ck_assert_str_eq(buf, "YWJjZGU=");
  570. memset(buf, 77, sizeof(buf));
  571. base64_encode((unsigned char *)alpha, 6, buf);
  572. ck_assert_str_eq(buf, "YWJjZGVm");
  573. memset(buf, 77, sizeof(buf));
  574. base64_encode((unsigned char *)alpha, (int)strlen(alpha), buf);
  575. ck_assert_str_eq(buf, alpha_b64_enc);
  576. memset(buf, 77, sizeof(buf));
  577. base64_encode((unsigned char *)nonalpha, (int)strlen(nonalpha), buf);
  578. ck_assert_str_eq(buf, nonalpha_b64_enc);
  579. #endif
  580. #if defined(USE_LUA)
  581. memset(buf, 77, sizeof(buf));
  582. len = 9999;
  583. ret = base64_decode((unsigned char *)alpha_b64_enc,
  584. (int)strlen(alpha_b64_enc),
  585. buf,
  586. &len);
  587. ck_assert_int_eq(ret, -1);
  588. ck_assert_uint_eq((unsigned int)len, (unsigned int)strlen(alpha));
  589. ck_assert_str_eq(buf, alpha);
  590. memset(buf, 77, sizeof(buf));
  591. len = 9999;
  592. ret = base64_decode((unsigned char *)"AAA*AAA", 7, buf, &len);
  593. ck_assert_int_eq(ret, 3);
  594. #endif
  595. memset(buf, 77, sizeof(buf));
  596. ret = mg_url_encode(alpha, buf, sizeof(buf));
  597. ck_assert_int_eq(ret, (int)strlen(buf));
  598. ck_assert_int_eq(ret, (int)strlen(alpha));
  599. ck_assert_str_eq(buf, alpha);
  600. memset(buf, 77, sizeof(buf));
  601. ret = mg_url_encode(nonalpha, buf, sizeof(buf));
  602. ck_assert_int_eq(ret, (int)strlen(buf));
  603. ck_assert_int_eq(ret, (int)strlen(nonalpha_url_enc1));
  604. ck_assert_str_eq(buf, nonalpha_url_enc1);
  605. memset(buf, 77, sizeof(buf));
  606. ret = mg_url_decode(alpha, (int)strlen(alpha), buf, sizeof(buf), 0);
  607. ck_assert_int_eq(ret, (int)strlen(buf));
  608. ck_assert_int_eq(ret, (int)strlen(alpha));
  609. ck_assert_str_eq(buf, alpha);
  610. memset(buf, 77, sizeof(buf));
  611. ret = mg_url_decode(
  612. nonalpha_url_enc1, (int)strlen(nonalpha_url_enc1), buf, sizeof(buf), 0);
  613. ck_assert_int_eq(ret, (int)strlen(buf));
  614. ck_assert_int_eq(ret, (int)strlen(nonalpha));
  615. ck_assert_str_eq(buf, nonalpha);
  616. memset(buf, 77, sizeof(buf));
  617. ret = mg_url_decode(
  618. nonalpha_url_enc2, (int)strlen(nonalpha_url_enc2), buf, sizeof(buf), 0);
  619. ck_assert_int_eq(ret, (int)strlen(buf));
  620. ck_assert_int_eq(ret, (int)strlen(nonalpha));
  621. ck_assert_str_eq(buf, nonalpha);
  622. /* len could be unused, if base64_decode is not tested because USE_LUA is
  623. * not defined */
  624. (void)len;
  625. }
  626. END_TEST
  627. START_TEST(test_mask_data)
  628. {
  629. #if defined(USE_WEBSOCKET)
  630. char in[1024];
  631. char out[1024];
  632. int i;
  633. #endif
  634. uint32_t mask = 0x61626364;
  635. /* TODO: adapt test for big endian */
  636. ck_assert((*(unsigned char *)&mask) == 0x64u);
  637. #if defined(USE_WEBSOCKET)
  638. memset(in, 0, sizeof(in));
  639. memset(out, 99, sizeof(out));
  640. mask_data(in, sizeof(out), 0, out);
  641. ck_assert(!memcmp(out, in, sizeof(out)));
  642. for (i = 0; i < 1024; i++) {
  643. in[i] = (char)((unsigned char)i);
  644. }
  645. mask_data(in, 107, 0, out);
  646. ck_assert(!memcmp(out, in, 107));
  647. mask_data(in, 256, 0x01010101, out);
  648. for (i = 0; i < 256; i++) {
  649. ck_assert_int_eq((int)((unsigned char)out[i]),
  650. (int)(((unsigned char)in[i]) ^ (char)1u));
  651. }
  652. for (i = 256; i < (int)sizeof(out); i++) {
  653. ck_assert_int_eq((int)((unsigned char)out[i]), (int)0);
  654. }
  655. /* TODO: check this for big endian */
  656. mask_data(in, 5, 0x01020304, out);
  657. ck_assert_uint_eq((unsigned char)out[0], 0u ^ 4u);
  658. ck_assert_uint_eq((unsigned char)out[1], 1u ^ 3u);
  659. ck_assert_uint_eq((unsigned char)out[2], 2u ^ 2u);
  660. ck_assert_uint_eq((unsigned char)out[3], 3u ^ 1u);
  661. ck_assert_uint_eq((unsigned char)out[4], 4u ^ 4u);
  662. #endif
  663. }
  664. END_TEST
  665. START_TEST(test_parse_date_string)
  666. {
  667. #if !defined(NO_CACHING)
  668. time_t now = time(0);
  669. struct tm *tm = gmtime(&now);
  670. char date[64] = {0};
  671. unsigned long i;
  672. ck_assert_uint_eq((unsigned long)parse_date_string("1/Jan/1970 00:01:02"),
  673. 62ul);
  674. ck_assert_uint_eq((unsigned long)parse_date_string("1 Jan 1970 00:02:03"),
  675. 123ul);
  676. ck_assert_uint_eq((unsigned long)parse_date_string("1-Jan-1970 00:03:04"),
  677. 184ul);
  678. ck_assert_uint_eq((unsigned long)parse_date_string(
  679. "Xyz, 1 Jan 1970 00:04:05"),
  680. 245ul);
  681. gmt_time_string(date, sizeof(date), &now);
  682. ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
  683. sprintf(date,
  684. "%02u %s %04u %02u:%02u:%02u",
  685. (unsigned int)tm->tm_mday,
  686. month_names[tm->tm_mon],
  687. (unsigned int)(tm->tm_year + 1900),
  688. (unsigned int)tm->tm_hour,
  689. (unsigned int)tm->tm_min,
  690. (unsigned int)tm->tm_sec);
  691. ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
  692. gmt_time_string(date, 1, NULL);
  693. ck_assert_str_eq(date, "");
  694. gmt_time_string(date, 6, NULL);
  695. ck_assert_str_eq(date,
  696. "Thu, "); /* part of "Thu, 01 Jan 1970 00:00:00 GMT" */
  697. gmt_time_string(date, sizeof(date), NULL);
  698. ck_assert_str_eq(date, "Thu, 01 Jan 1970 00:00:00 GMT");
  699. for (i = 2ul; i < 0x8000000ul; i += i / 2) {
  700. now = (time_t)i;
  701. gmt_time_string(date, sizeof(date), &now);
  702. ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
  703. tm = gmtime(&now);
  704. sprintf(date,
  705. "%02u-%s-%04u %02u:%02u:%02u",
  706. (unsigned int)tm->tm_mday,
  707. month_names[tm->tm_mon],
  708. (unsigned int)(tm->tm_year + 1900),
  709. (unsigned int)tm->tm_hour,
  710. (unsigned int)tm->tm_min,
  711. (unsigned int)tm->tm_sec);
  712. ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
  713. }
  714. #endif
  715. }
  716. END_TEST
  717. START_TEST(test_sha1)
  718. {
  719. #ifdef SHA1_DIGEST_SIZE
  720. SHA_CTX sha_ctx;
  721. uint8_t digest[SHA1_DIGEST_SIZE] = {0};
  722. char str[48] = {0};
  723. int i;
  724. const char *test_str;
  725. ck_assert_uint_eq(sizeof(digest), 20);
  726. ck_assert_uint_gt(sizeof(str), sizeof(digest) * 2 + 1);
  727. /* empty string */
  728. SHA1_Init(&sha_ctx);
  729. SHA1_Final(digest, &sha_ctx);
  730. bin2str(str, digest, sizeof(digest));
  731. ck_assert_uint_eq(strlen(str), 40);
  732. ck_assert_str_eq(str, "da39a3ee5e6b4b0d3255bfef95601890afd80709");
  733. /* empty string */
  734. SHA1_Init(&sha_ctx);
  735. SHA1_Update(&sha_ctx, (uint8_t *)"abc", 0);
  736. SHA1_Final(digest, &sha_ctx);
  737. bin2str(str, digest, sizeof(digest));
  738. ck_assert_uint_eq(strlen(str), 40);
  739. ck_assert_str_eq(str, "da39a3ee5e6b4b0d3255bfef95601890afd80709");
  740. /* "abc" */
  741. SHA1_Init(&sha_ctx);
  742. SHA1_Update(&sha_ctx, (uint8_t *)"abc", 3);
  743. SHA1_Final(digest, &sha_ctx);
  744. bin2str(str, digest, sizeof(digest));
  745. ck_assert_uint_eq(strlen(str), 40);
  746. ck_assert_str_eq(str, "a9993e364706816aba3e25717850c26c9cd0d89d");
  747. /* "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" */
  748. test_str = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  749. SHA1_Init(&sha_ctx);
  750. SHA1_Update(&sha_ctx, (uint8_t *)test_str, (uint32_t)strlen(test_str));
  751. SHA1_Final(digest, &sha_ctx);
  752. bin2str(str, digest, sizeof(digest));
  753. ck_assert_uint_eq(strlen(str), 40);
  754. ck_assert_str_eq(str, "84983e441c3bd26ebaae4aa1f95129e5e54670f1");
  755. /* a million "a" */
  756. SHA1_Init(&sha_ctx);
  757. for (i = 0; i < 1000000; i++) {
  758. SHA1_Update(&sha_ctx, (uint8_t *)"a", 1);
  759. }
  760. SHA1_Final(digest, &sha_ctx);
  761. bin2str(str, digest, sizeof(digest));
  762. ck_assert_uint_eq(strlen(str), 40);
  763. ck_assert_str_eq(str, "34aa973cd4c4daa4f61eeb2bdbad27316534016f");
  764. /* a million "a" in blocks of 10 */
  765. SHA1_Init(&sha_ctx);
  766. for (i = 0; i < 100000; i++) {
  767. SHA1_Update(&sha_ctx, (uint8_t *)"aaaaaaaaaa", 10);
  768. }
  769. SHA1_Final(digest, &sha_ctx);
  770. bin2str(str, digest, sizeof(digest));
  771. ck_assert_uint_eq(strlen(str), 40);
  772. ck_assert_str_eq(str, "34aa973cd4c4daa4f61eeb2bdbad27316534016f");
  773. #else
  774. /* Can not test, if SHA1 is not included */
  775. ck_assert(1);
  776. #endif
  777. }
  778. END_TEST
  779. START_TEST(test_config_options)
  780. {
  781. /* Check size of config_options vs. number of options in enum. */
  782. ck_assert_ptr_eq(NULL, config_options[NUM_OPTIONS].name);
  783. ck_assert_int_eq((int)MG_CONFIG_TYPE_UNKNOWN,
  784. config_options[NUM_OPTIONS].type);
  785. ck_assert_uint_eq(sizeof(config_options) / sizeof(config_options[0]),
  786. (size_t)(NUM_OPTIONS + 1));
  787. /* Check option enums vs. option names. */
  788. /* Check if the order in
  789. * static struct mg_option config_options[]
  790. * is the same as in the option enum
  791. * This test allows to reorder config_options and the enum,
  792. * and check if the order is still consistent. */
  793. ck_assert_str_eq("cgi_pattern", config_options[CGI_EXTENSIONS].name);
  794. ck_assert_str_eq("cgi_environment", config_options[CGI_ENVIRONMENT].name);
  795. ck_assert_str_eq("put_delete_auth_file",
  796. config_options[PUT_DELETE_PASSWORDS_FILE].name);
  797. ck_assert_str_eq("cgi_interpreter", config_options[CGI_INTERPRETER].name);
  798. ck_assert_str_eq("protect_uri", config_options[PROTECT_URI].name);
  799. ck_assert_str_eq("authentication_domain",
  800. config_options[AUTHENTICATION_DOMAIN].name);
  801. ck_assert_str_eq("enable_auth_domain_check",
  802. config_options[ENABLE_AUTH_DOMAIN_CHECK].name);
  803. ck_assert_str_eq("ssi_pattern", config_options[SSI_EXTENSIONS].name);
  804. ck_assert_str_eq("throttle", config_options[THROTTLE].name);
  805. ck_assert_str_eq("access_log_file", config_options[ACCESS_LOG_FILE].name);
  806. ck_assert_str_eq("enable_directory_listing",
  807. config_options[ENABLE_DIRECTORY_LISTING].name);
  808. ck_assert_str_eq("error_log_file", config_options[ERROR_LOG_FILE].name);
  809. ck_assert_str_eq("global_auth_file",
  810. config_options[GLOBAL_PASSWORDS_FILE].name);
  811. ck_assert_str_eq("index_files", config_options[INDEX_FILES].name);
  812. ck_assert_str_eq("enable_keep_alive",
  813. config_options[ENABLE_KEEP_ALIVE].name);
  814. ck_assert_str_eq("access_control_list",
  815. config_options[ACCESS_CONTROL_LIST].name);
  816. ck_assert_str_eq("extra_mime_types", config_options[EXTRA_MIME_TYPES].name);
  817. ck_assert_str_eq("listening_ports", config_options[LISTENING_PORTS].name);
  818. ck_assert_str_eq("document_root", config_options[DOCUMENT_ROOT].name);
  819. ck_assert_str_eq("ssl_certificate", config_options[SSL_CERTIFICATE].name);
  820. ck_assert_str_eq("ssl_certificate_chain",
  821. config_options[SSL_CERTIFICATE_CHAIN].name);
  822. ck_assert_str_eq("num_threads", config_options[NUM_THREADS].name);
  823. ck_assert_str_eq("run_as_user", config_options[RUN_AS_USER].name);
  824. ck_assert_str_eq("url_rewrite_patterns",
  825. config_options[URL_REWRITE_PATTERN].name);
  826. ck_assert_str_eq("hide_files_patterns", config_options[HIDE_FILES].name);
  827. ck_assert_str_eq("request_timeout_ms",
  828. config_options[REQUEST_TIMEOUT].name);
  829. ck_assert_str_eq("keep_alive_timeout_ms",
  830. config_options[KEEP_ALIVE_TIMEOUT].name);
  831. ck_assert_str_eq("linger_timeout_ms", config_options[LINGER_TIMEOUT].name);
  832. ck_assert_str_eq("max_connections", config_options[MAX_CONNECTIONS].name);
  833. ck_assert_str_eq("ssl_verify_peer",
  834. config_options[SSL_DO_VERIFY_PEER].name);
  835. ck_assert_str_eq("ssl_ca_path", config_options[SSL_CA_PATH].name);
  836. ck_assert_str_eq("ssl_ca_file", config_options[SSL_CA_FILE].name);
  837. ck_assert_str_eq("ssl_verify_depth", config_options[SSL_VERIFY_DEPTH].name);
  838. ck_assert_str_eq("ssl_default_verify_paths",
  839. config_options[SSL_DEFAULT_VERIFY_PATHS].name);
  840. ck_assert_str_eq("ssl_cipher_list", config_options[SSL_CIPHER_LIST].name);
  841. ck_assert_str_eq("ssl_protocol_version",
  842. config_options[SSL_PROTOCOL_VERSION].name);
  843. ck_assert_str_eq("ssl_short_trust", config_options[SSL_SHORT_TRUST].name);
  844. #if defined(USE_WEBSOCKET)
  845. ck_assert_str_eq("websocket_timeout_ms",
  846. config_options[WEBSOCKET_TIMEOUT].name);
  847. ck_assert_str_eq("enable_websocket_ping_pong",
  848. config_options[ENABLE_WEBSOCKET_PING_PONG].name);
  849. #endif
  850. ck_assert_str_eq("decode_url", config_options[DECODE_URL].name);
  851. #if defined(USE_LUA)
  852. ck_assert_str_eq("lua_preload_file", config_options[LUA_PRELOAD_FILE].name);
  853. ck_assert_str_eq("lua_script_pattern",
  854. config_options[LUA_SCRIPT_EXTENSIONS].name);
  855. ck_assert_str_eq("lua_server_page_pattern",
  856. config_options[LUA_SERVER_PAGE_EXTENSIONS].name);
  857. #endif
  858. #if defined(USE_DUKTAPE)
  859. ck_assert_str_eq("duktape_script_pattern",
  860. config_options[DUKTAPE_SCRIPT_EXTENSIONS].name);
  861. #endif
  862. #if defined(USE_WEBSOCKET)
  863. ck_assert_str_eq("websocket_root", config_options[WEBSOCKET_ROOT].name);
  864. #endif
  865. #if defined(USE_LUA) && defined(USE_WEBSOCKET)
  866. ck_assert_str_eq("lua_websocket_pattern",
  867. config_options[LUA_WEBSOCKET_EXTENSIONS].name);
  868. #endif
  869. ck_assert_str_eq("access_control_allow_origin",
  870. config_options[ACCESS_CONTROL_ALLOW_ORIGIN].name);
  871. ck_assert_str_eq("access_control_allow_methods",
  872. config_options[ACCESS_CONTROL_ALLOW_METHODS].name);
  873. ck_assert_str_eq("access_control_allow_headers",
  874. config_options[ACCESS_CONTROL_ALLOW_HEADERS].name);
  875. ck_assert_str_eq("error_pages", config_options[ERROR_PAGES].name);
  876. ck_assert_str_eq("tcp_nodelay", config_options[CONFIG_TCP_NODELAY].name);
  877. #if !defined(NO_CACHING)
  878. ck_assert_str_eq("static_file_max_age",
  879. config_options[STATIC_FILE_MAX_AGE].name);
  880. #endif
  881. #if !defined(NO_SSL)
  882. ck_assert_str_eq("strict_transport_security_max_age",
  883. config_options[STRICT_HTTPS_MAX_AGE].name);
  884. #endif
  885. #if defined(__linux__)
  886. ck_assert_str_eq("allow_sendfile_call",
  887. config_options[ALLOW_SENDFILE_CALL].name);
  888. #endif
  889. #if defined(_WIN32)
  890. ck_assert_str_eq("case_sensitive",
  891. config_options[CASE_SENSITIVE_FILES].name);
  892. #endif
  893. #if defined(USE_LUA)
  894. ck_assert_str_eq("lua_background_script",
  895. config_options[LUA_BACKGROUND_SCRIPT].name);
  896. ck_assert_str_eq("lua_background_script_params",
  897. config_options[LUA_BACKGROUND_SCRIPT_PARAMS].name);
  898. #endif
  899. ck_assert_str_eq("additional_header",
  900. config_options[ADDITIONAL_HEADER].name);
  901. ck_assert_str_eq("max_request_size", config_options[MAX_REQUEST_SIZE].name);
  902. ck_assert_str_eq("allow_index_script_resource",
  903. config_options[ALLOW_INDEX_SCRIPT_SUB_RES].name);
  904. }
  905. END_TEST
  906. #if !defined(REPLACE_CHECK_FOR_LOCAL_DEBUGGING)
  907. Suite *
  908. make_private_suite(void)
  909. {
  910. Suite *const suite = suite_create("Private");
  911. TCase *const tcase_http_message = tcase_create("HTTP Message");
  912. TCase *const tcase_http_keep_alive = tcase_create("HTTP Keep Alive");
  913. TCase *const tcase_url_parsing_1 = tcase_create("URL Parsing 1");
  914. TCase *const tcase_url_parsing_2 = tcase_create("URL Parsing 2");
  915. TCase *const tcase_url_parsing_3 = tcase_create("URL Parsing 3");
  916. TCase *const tcase_internal_parse_1 = tcase_create("Internal Parsing 1");
  917. TCase *const tcase_internal_parse_2 = tcase_create("Internal Parsing 2");
  918. TCase *const tcase_internal_parse_3 = tcase_create("Internal Parsing 3");
  919. TCase *const tcase_internal_parse_4 = tcase_create("Internal Parsing 4");
  920. TCase *const tcase_internal_parse_5 = tcase_create("Internal Parsing 5");
  921. TCase *const tcase_internal_parse_6 = tcase_create("Internal Parsing 6");
  922. TCase *const tcase_encode_decode = tcase_create("Encode Decode");
  923. TCase *const tcase_mask_data = tcase_create("Mask Data");
  924. TCase *const tcase_parse_date_string = tcase_create("Date Parsing");
  925. TCase *const tcase_sha1 = tcase_create("SHA1");
  926. TCase *const tcase_config_options = tcase_create("Config Options");
  927. tcase_add_test(tcase_http_message, test_parse_http_message);
  928. tcase_set_timeout(tcase_http_message, civetweb_min_test_timeout);
  929. suite_add_tcase(suite, tcase_http_message);
  930. tcase_add_test(tcase_http_keep_alive, test_should_keep_alive);
  931. tcase_set_timeout(tcase_http_keep_alive, civetweb_min_test_timeout);
  932. suite_add_tcase(suite, tcase_http_keep_alive);
  933. tcase_add_test(tcase_url_parsing_1, test_match_prefix);
  934. tcase_set_timeout(tcase_url_parsing_1, civetweb_min_test_timeout);
  935. suite_add_tcase(suite, tcase_url_parsing_1);
  936. tcase_add_test(tcase_url_parsing_2, test_remove_dot_segments);
  937. tcase_set_timeout(tcase_url_parsing_2, civetweb_min_test_timeout);
  938. suite_add_tcase(suite, tcase_url_parsing_2);
  939. tcase_add_test(tcase_url_parsing_3, test_is_valid_uri);
  940. tcase_set_timeout(tcase_url_parsing_3, civetweb_min_test_timeout);
  941. suite_add_tcase(suite, tcase_url_parsing_3);
  942. tcase_add_test(tcase_internal_parse_1, test_next_option);
  943. tcase_set_timeout(tcase_internal_parse_1, civetweb_min_test_timeout);
  944. suite_add_tcase(suite, tcase_internal_parse_1);
  945. tcase_add_test(tcase_internal_parse_2, test_skip_quoted);
  946. tcase_set_timeout(tcase_internal_parse_2, civetweb_min_test_timeout);
  947. suite_add_tcase(suite, tcase_internal_parse_2);
  948. tcase_add_test(tcase_internal_parse_3, test_mg_strcasestr);
  949. tcase_set_timeout(tcase_internal_parse_3, civetweb_min_test_timeout);
  950. suite_add_tcase(suite, tcase_internal_parse_3);
  951. tcase_add_test(tcase_internal_parse_4, test_alloc_vprintf);
  952. tcase_set_timeout(tcase_internal_parse_4, civetweb_min_test_timeout);
  953. suite_add_tcase(suite, tcase_internal_parse_4);
  954. tcase_add_test(tcase_internal_parse_5, test_mg_vsnprintf);
  955. tcase_set_timeout(tcase_internal_parse_5, civetweb_min_test_timeout);
  956. suite_add_tcase(suite, tcase_internal_parse_5);
  957. tcase_add_test(tcase_internal_parse_6, test_parse_port_string);
  958. tcase_set_timeout(tcase_internal_parse_6, civetweb_min_test_timeout);
  959. suite_add_tcase(suite, tcase_internal_parse_6);
  960. tcase_add_test(tcase_encode_decode, test_encode_decode);
  961. tcase_set_timeout(tcase_encode_decode, civetweb_min_test_timeout);
  962. suite_add_tcase(suite, tcase_encode_decode);
  963. tcase_add_test(tcase_mask_data, test_mask_data);
  964. tcase_set_timeout(tcase_mask_data, civetweb_min_test_timeout);
  965. suite_add_tcase(suite, tcase_mask_data);
  966. tcase_add_test(tcase_parse_date_string, test_parse_date_string);
  967. tcase_set_timeout(tcase_parse_date_string, civetweb_min_test_timeout);
  968. suite_add_tcase(suite, tcase_parse_date_string);
  969. tcase_add_test(tcase_sha1, test_sha1);
  970. tcase_set_timeout(tcase_sha1, civetweb_min_test_timeout);
  971. suite_add_tcase(suite, tcase_sha1);
  972. tcase_add_test(tcase_config_options, test_config_options);
  973. tcase_set_timeout(tcase_config_options, civetweb_min_test_timeout);
  974. suite_add_tcase(suite, tcase_config_options);
  975. return suite;
  976. }
  977. #endif
  978. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  979. /* Used to debug test cases without using the check framework */
  980. void
  981. MAIN_PRIVATE(void)
  982. {
  983. #if defined(_WIN32)
  984. /* test_parse_port_string requires WSAStartup for IPv6 */
  985. WSADATA data;
  986. WSAStartup(MAKEWORD(2, 2), &data);
  987. #endif
  988. test_alloc_vprintf(0);
  989. test_mg_vsnprintf(0);
  990. test_remove_dot_segments(0);
  991. test_parse_date_string(0);
  992. test_parse_port_string(0);
  993. test_parse_http_message(0);
  994. test_sha1(0);
  995. #if defined(_WIN32)
  996. WSACleanup();
  997. #endif
  998. }
  999. #endif