private.c 37 KB

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