private.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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. START_TEST(test_parse_http_message)
  43. {
  44. /* Adapted from unit_test.c */
  45. /* Copyright (c) 2013-2015 the Civetweb developers */
  46. /* Copyright (c) 2004-2013 Sergey Lyubka */
  47. struct mg_request_info ri;
  48. struct mg_response_info respi;
  49. char empty[] = "";
  50. char req1[] = "GET / HTTP/1.1\r\n\r\n";
  51. char req2[] = "BLAH / HTTP/1.1\r\n\r\n";
  52. char req3[] = "GET / HTTP/1.1\nKey: Val\n\n";
  53. char req4[] =
  54. "GET / HTTP/1.1\r\nA: foo bar\r\nB: bar\r\nskip\r\nbaz:\r\n\r\n";
  55. char req5[] = "GET / HTTP/1.0\n\n";
  56. char req6[] = "G";
  57. char req7[] = " blah ";
  58. char req8[] = "HTTP/1.0 404 Not Found\n\n";
  59. char req9[] = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n";
  60. char req10[] = "GET / HTTP/1.1\r\nA: foo bar\r\nB: bar\r\n\r\n";
  61. char req11[] = "GET /\r\nError: X\r\n\r\n";
  62. char req12[] =
  63. "POST /a/b/c.d?e=f&g HTTP/1.1\r\nKey1: val1\r\nKey2: val2\r\n\r\nBODY";
  64. int lenreq1 = (int)strlen(req1);
  65. int lenreq2 = (int)strlen(req2);
  66. int lenreq3 = (int)strlen(req3);
  67. int lenreq4 = (int)strlen(req4);
  68. int lenreq5 = (int)strlen(req5);
  69. int lenreq6 = (int)strlen(req6);
  70. int lenreq7 = (int)strlen(req7);
  71. int lenreq8 = (int)strlen(req8);
  72. int lenreq9 = (int)strlen(req9);
  73. int lenreq10 = (int)strlen(req10);
  74. int lenreq11 = (int)strlen(req11);
  75. int lenreq12 = (int)strlen(req12);
  76. int lenhdr12 = lenreq12 - 4; /* length without body */
  77. /* An empty string is neither a complete request nor a complete
  78. * response, so it must return 0 */
  79. ck_assert_int_eq(0, get_http_header_len(empty, 0));
  80. ck_assert_int_eq(0, parse_http_request(empty, 0, &ri));
  81. ck_assert_int_eq(0, parse_http_response(empty, 0, &ri));
  82. /* Same is true for a leading space */
  83. ck_assert_int_eq(0, get_http_header_len(" ", 0));
  84. ck_assert_int_eq(0, parse_http_request(" ", 0, &ri));
  85. ck_assert_int_eq(0, parse_http_response(" ", 0, &ri));
  86. /* req1 is a valid request */
  87. ck_assert_int_eq(lenreq1, get_http_header_len(req1, lenreq1));
  88. ck_assert_int_eq(lenreq1, parse_http_request(req1, lenreq1, &ri));
  89. ck_assert_str_eq("1.1", ri.http_version);
  90. ck_assert_int_eq(0, ri.num_headers);
  91. ck_assert_int_eq(-1, parse_http_response(req1, lenreq1, &respi));
  92. /* req1 minus 1 byte at the end is incomplete */
  93. ck_assert_int_eq(0, get_http_header_len(req1, lenreq1 - 1));
  94. /* req1 minus 1 byte at the start is complete but invalid */
  95. ck_assert_int_eq(lenreq1 - 1, get_http_header_len(req1 + 1, lenreq1 - 1));
  96. ck_assert_int_eq(-1, parse_http_request(req1 + 1, lenreq1 - 1, &ri));
  97. /* req2 is a complete, but invalid request */
  98. ck_assert_int_eq(lenreq2, get_http_header_len(req2, lenreq2));
  99. ck_assert_int_eq(-1, parse_http_request(req2, lenreq2, &ri));
  100. /* req3 is a complete and valid request */
  101. ck_assert_int_eq(lenreq3, get_http_header_len(req3, lenreq3));
  102. ck_assert_int_eq(lenreq3, parse_http_request(req3, lenreq3, &ri));
  103. ck_assert_int_eq(-1, parse_http_response(req3, lenreq3, &respi));
  104. /* Multiline header are obsolete, so return an error
  105. * (https://tools.ietf.org/html/rfc7230#section-3.2.4). */
  106. ck_assert_int_eq(-1, parse_http_request(req4, lenreq4, &ri));
  107. /* req5 is a complete and valid request (also somewhat malformed,
  108. * since it uses \n\n instead of \r\n\r\n) */
  109. ck_assert_int_eq(lenreq5, get_http_header_len(req5, lenreq5));
  110. ck_assert_int_eq(lenreq5, parse_http_request(req5, lenreq5, &ri));
  111. ck_assert_str_eq("GET", ri.request_method);
  112. ck_assert_str_eq("1.0", ri.http_version);
  113. ck_assert_int_eq(-1, parse_http_response(req5, lenreq5, &respi));
  114. /* req6 is incomplete */
  115. ck_assert_int_eq(0, get_http_header_len(req6, lenreq6));
  116. ck_assert_int_eq(0, parse_http_request(req6, lenreq6, &ri));
  117. /* req7 is invalid, but not yet complete */
  118. ck_assert_int_eq(0, get_http_header_len(req7, lenreq7));
  119. ck_assert_int_eq(0, parse_http_request(req7, lenreq7, &ri));
  120. /* req8 is a valid response */
  121. ck_assert_int_eq(lenreq8, get_http_header_len(req8, lenreq8));
  122. ck_assert_int_eq(-1, parse_http_request(req8, lenreq8, &ri));
  123. ck_assert_int_eq(lenreq8, parse_http_response(req8, lenreq8, &respi));
  124. /* req9 is a valid response */
  125. ck_assert_int_eq(lenreq9, get_http_header_len(req9, lenreq9));
  126. ck_assert_int_eq(-1, parse_http_request(req9, lenreq9, &ri));
  127. ck_assert_int_eq(lenreq9, parse_http_response(req9, lenreq9, &respi));
  128. ck_assert_int_eq(1, respi.num_headers);
  129. /* req10 is a valid request */
  130. ck_assert_int_eq(lenreq10, get_http_header_len(req10, lenreq10));
  131. ck_assert_int_eq(lenreq10, parse_http_request(req10, lenreq10, &ri));
  132. ck_assert_str_eq("1.1", ri.http_version);
  133. ck_assert_int_eq(2, ri.num_headers);
  134. ck_assert_str_eq("A", ri.http_headers[0].name);
  135. ck_assert_str_eq("foo bar", ri.http_headers[0].value);
  136. ck_assert_str_eq("B", ri.http_headers[1].name);
  137. ck_assert_str_eq("bar", ri.http_headers[1].value);
  138. /* req11 is a complete but valid request */
  139. ck_assert_int_eq(-1, parse_http_request(req11, lenreq11, &ri));
  140. /* req12 is a valid request with body data */
  141. ck_assert_int_gt(lenreq12, lenhdr12);
  142. ck_assert_int_eq(lenhdr12, get_http_header_len(req12, lenreq12));
  143. ck_assert_int_eq(lenhdr12, parse_http_request(req12, lenreq12, &ri));
  144. }
  145. END_TEST
  146. START_TEST(test_should_keep_alive)
  147. {
  148. /* Adapted from unit_test.c */
  149. /* Copyright (c) 2013-2015 the Civetweb developers */
  150. /* Copyright (c) 2004-2013 Sergey Lyubka */
  151. struct mg_connection conn;
  152. struct mg_context ctx;
  153. char req1[] = "GET / HTTP/1.1\r\n\r\n";
  154. char req2[] = "GET / HTTP/1.0\r\n\r\n";
  155. char req3[] = "GET / HTTP/1.1\r\nConnection: close\r\n\r\n";
  156. char req4[] = "GET / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";
  157. char yes[] = "yes";
  158. char no[] = "no";
  159. int lenreq1 = (int)strlen(req1);
  160. int lenreq2 = (int)strlen(req2);
  161. int lenreq3 = (int)strlen(req3);
  162. int lenreq4 = (int)strlen(req4);
  163. memset(&conn, 0, sizeof(conn));
  164. conn.ctx = &ctx;
  165. ck_assert_int_eq(parse_http_request(req1, lenreq1, &conn.request_info),
  166. lenreq1);
  167. ctx.config[ENABLE_KEEP_ALIVE] = no;
  168. ck_assert_int_eq(should_keep_alive(&conn), 0);
  169. ctx.config[ENABLE_KEEP_ALIVE] = yes;
  170. ck_assert_int_eq(should_keep_alive(&conn), 1);
  171. conn.must_close = 1;
  172. ck_assert_int_eq(should_keep_alive(&conn), 0);
  173. conn.must_close = 0;
  174. parse_http_request(req2, lenreq2, &conn.request_info);
  175. ck_assert_int_eq(should_keep_alive(&conn), 0);
  176. parse_http_request(req3, lenreq3, &conn.request_info);
  177. ck_assert_int_eq(should_keep_alive(&conn), 0);
  178. parse_http_request(req4, lenreq4, &conn.request_info);
  179. ck_assert_int_eq(should_keep_alive(&conn), 1);
  180. conn.status_code = 401;
  181. ck_assert_int_eq(should_keep_alive(&conn), 0);
  182. conn.status_code = 200;
  183. conn.must_close = 1;
  184. ck_assert_int_eq(should_keep_alive(&conn), 0);
  185. }
  186. END_TEST
  187. START_TEST(test_match_prefix)
  188. {
  189. /* Adapted from unit_test.c */
  190. /* Copyright (c) 2013-2015 the Civetweb developers */
  191. /* Copyright (c) 2004-2013 Sergey Lyubka */
  192. ck_assert_int_eq(4, match_prefix("/api", 4, "/api"));
  193. ck_assert_int_eq(3, match_prefix("/a/", 3, "/a/b/c"));
  194. ck_assert_int_eq(-1, match_prefix("/a/", 3, "/ab/c"));
  195. ck_assert_int_eq(4, match_prefix("/*/", 3, "/ab/c"));
  196. ck_assert_int_eq(6, match_prefix("**", 2, "/a/b/c"));
  197. ck_assert_int_eq(2, match_prefix("/*", 2, "/a/b/c"));
  198. ck_assert_int_eq(2, match_prefix("*/*", 3, "/a/b/c"));
  199. ck_assert_int_eq(5, match_prefix("**/", 3, "/a/b/c"));
  200. ck_assert_int_eq(5, match_prefix("**.foo|**.bar", 13, "a.bar"));
  201. ck_assert_int_eq(2, match_prefix("a|b|cd", 6, "cdef"));
  202. ck_assert_int_eq(2, match_prefix("a|b|c?", 6, "cdef"));
  203. ck_assert_int_eq(1, match_prefix("a|?|cd", 6, "cdef"));
  204. ck_assert_int_eq(-1, match_prefix("/a/**.cgi", 9, "/foo/bar/x.cgi"));
  205. ck_assert_int_eq(12, match_prefix("/a/**.cgi", 9, "/a/bar/x.cgi"));
  206. ck_assert_int_eq(5, match_prefix("**/", 3, "/a/b/c"));
  207. ck_assert_int_eq(-1, match_prefix("**/$", 4, "/a/b/c"));
  208. ck_assert_int_eq(5, match_prefix("**/$", 4, "/a/b/"));
  209. ck_assert_int_eq(0, match_prefix("$", 1, ""));
  210. ck_assert_int_eq(-1, match_prefix("$", 1, "x"));
  211. ck_assert_int_eq(1, match_prefix("*$", 2, "x"));
  212. ck_assert_int_eq(1, match_prefix("/$", 2, "/"));
  213. ck_assert_int_eq(-1, match_prefix("**/$", 4, "/a/b/c"));
  214. ck_assert_int_eq(5, match_prefix("**/$", 4, "/a/b/"));
  215. ck_assert_int_eq(0, match_prefix("*", 1, "/hello/"));
  216. ck_assert_int_eq(-1, match_prefix("**.a$|**.b$", 11, "/a/b.b/"));
  217. ck_assert_int_eq(6, match_prefix("**.a$|**.b$", 11, "/a/b.b"));
  218. ck_assert_int_eq(6, match_prefix("**.a$|**.b$", 11, "/a/B.A"));
  219. ck_assert_int_eq(5, match_prefix("**o$", 4, "HELLO"));
  220. }
  221. END_TEST
  222. START_TEST(test_remove_double_dots_and_double_slashes)
  223. {
  224. /* Adapted from unit_test.c */
  225. /* Copyright (c) 2013-2015 the Civetweb developers */
  226. /* Copyright (c) 2004-2013 Sergey Lyubka */
  227. struct {
  228. char before[20], after[20];
  229. } data[] = {
  230. {"////a", "/a"},
  231. {"/.....", "/."},
  232. {"/......", "/"},
  233. {"..", "."},
  234. {"...", "."},
  235. {"/...///", "/./"},
  236. {"/a...///", "/a.../"},
  237. {"/.x", "/.x"},
  238. {"/\\", "/"},
  239. {"/a\\", "/a\\"},
  240. {"/a\\\\...", "/a\\."},
  241. };
  242. size_t i;
  243. for (i = 0; i < ARRAY_SIZE(data); i++) {
  244. remove_double_dots_and_double_slashes(data[i].before);
  245. ck_assert_str_eq(data[i].before, data[i].after);
  246. }
  247. }
  248. END_TEST
  249. START_TEST(test_is_valid_uri)
  250. {
  251. /* is_valid_uri is superseeded by get_uri_type */
  252. ck_assert_int_eq(2, get_uri_type("/api"));
  253. ck_assert_int_eq(2, get_uri_type("/api/"));
  254. ck_assert_int_eq(2,
  255. get_uri_type("/some/long/path%20with%20space/file.xyz"));
  256. ck_assert_int_eq(0, get_uri_type("api"));
  257. ck_assert_int_eq(1, get_uri_type("*"));
  258. ck_assert_int_eq(0, get_uri_type("*xy"));
  259. ck_assert_int_eq(3, get_uri_type("http://somewhere/"));
  260. ck_assert_int_eq(3, get_uri_type("https://somewhere/some/file.html"));
  261. ck_assert_int_eq(4, get_uri_type("http://somewhere:8080/"));
  262. ck_assert_int_eq(4, get_uri_type("https://somewhere:8080/some/file.html"));
  263. }
  264. END_TEST
  265. START_TEST(test_next_option)
  266. {
  267. /* Adapted from unit_test.c */
  268. /* Copyright (c) 2013-2015 the Civetweb developers */
  269. /* Copyright (c) 2004-2013 Sergey Lyubka */
  270. const char *p, *list = "x/8,/y**=1;2k,z";
  271. struct vec a, b;
  272. int i;
  273. ck_assert(next_option(NULL, &a, &b) == NULL);
  274. for (i = 0, p = list; (p = next_option(p, &a, &b)) != NULL; i++) {
  275. ck_assert(i != 0 || (a.ptr == list && a.len == 3 && b.len == 0));
  276. ck_assert(i != 1
  277. || (a.ptr == list + 4 && a.len == 4 && b.ptr == list + 9
  278. && b.len == 4));
  279. ck_assert(i != 2 || (a.ptr == list + 14 && a.len == 1 && b.len == 0));
  280. }
  281. }
  282. END_TEST
  283. START_TEST(test_skip_quoted)
  284. {
  285. /* Adapted from unit_test.c */
  286. /* Copyright (c) 2013-2015 the Civetweb developers */
  287. /* Copyright (c) 2004-2013 Sergey Lyubka */
  288. char x[] = "a=1, b=2, c='hi \' there', d='here\\, there'", *s = x, *p;
  289. p = skip_quoted(&s, ", ", ", ", 0);
  290. ck_assert(p != NULL && !strcmp(p, "a=1"));
  291. p = skip_quoted(&s, ", ", ", ", 0);
  292. ck_assert(p != NULL && !strcmp(p, "b=2"));
  293. p = skip_quoted(&s, ",", " ", 0);
  294. ck_assert(p != NULL && !strcmp(p, "c='hi \' there'"));
  295. p = skip_quoted(&s, ",", " ", '\\');
  296. ck_assert(p != NULL && !strcmp(p, "d='here, there'"));
  297. ck_assert(*s == 0);
  298. }
  299. END_TEST
  300. static int
  301. alloc_printf(char **buf, size_t size, const char *fmt, ...)
  302. {
  303. /* Test helper function - adapted from unit_test.c */
  304. /* Copyright (c) 2013-2015 the Civetweb developers */
  305. /* Copyright (c) 2004-2013 Sergey Lyubka */
  306. va_list ap;
  307. int ret = 0;
  308. va_start(ap, fmt);
  309. ret = alloc_vprintf(buf, *buf, size, fmt, ap);
  310. va_end(ap);
  311. return ret;
  312. }
  313. static int
  314. alloc_printf2(char **buf, const char *fmt, ...)
  315. {
  316. /* Test alternative implementation */
  317. va_list ap;
  318. int ret = 0;
  319. va_start(ap, fmt);
  320. ret = alloc_vprintf2(buf, fmt, ap);
  321. va_end(ap);
  322. return ret;
  323. }
  324. START_TEST(test_alloc_vprintf)
  325. {
  326. /* Adapted from unit_test.c */
  327. /* Copyright (c) 2013-2015 the Civetweb developers */
  328. /* Copyright (c) 2004-2013 Sergey Lyubka */
  329. char buf[MG_BUF_LEN], *p = buf;
  330. ck_assert(alloc_printf(&p, sizeof(buf), "%s", "hi") == 2);
  331. ck_assert(p == buf);
  332. ck_assert(alloc_printf(&p, sizeof(buf), "%s", "") == 0);
  333. ck_assert(p == buf);
  334. ck_assert(alloc_printf(&p, sizeof(buf), "") == 0);
  335. ck_assert(p == buf);
  336. /* Pass small buffer, make sure alloc_printf allocates */
  337. ck_assert(alloc_printf(&p, 1, "%s", "hello") == 5);
  338. ck_assert(p != buf);
  339. mg_free(p);
  340. p = buf;
  341. /* Test alternative implementation */
  342. ck_assert(alloc_printf2(&p, "%s", "hello") == 5);
  343. ck_assert(p != buf);
  344. mg_free(p);
  345. p = buf;
  346. }
  347. END_TEST
  348. START_TEST(test_mg_vsnprintf)
  349. {
  350. char buf[16];
  351. int is_trunc;
  352. memset(buf, 0, sizeof(buf));
  353. is_trunc = 777;
  354. mg_snprintf(NULL, &is_trunc, buf, 10, "%8i", 123);
  355. ck_assert_str_eq(buf, " 123");
  356. ck_assert_int_eq(is_trunc, 0);
  357. is_trunc = 777;
  358. mg_snprintf(NULL, &is_trunc, buf, 10, "%9i", 123);
  359. ck_assert_str_eq(buf, " 123");
  360. ck_assert_int_eq(is_trunc, 0);
  361. is_trunc = 777;
  362. mg_snprintf(NULL, &is_trunc, buf, 9, "%9i", 123);
  363. ck_assert_str_eq(buf, " 12");
  364. ck_assert_int_eq(is_trunc, 1);
  365. is_trunc = 777;
  366. mg_snprintf(NULL, &is_trunc, buf, 8, "%9i", 123);
  367. ck_assert_str_eq(buf, " 1");
  368. ck_assert_int_eq(is_trunc, 1);
  369. is_trunc = 777;
  370. mg_snprintf(NULL, &is_trunc, buf, 7, "%9i", 123);
  371. ck_assert_str_eq(buf, " ");
  372. ck_assert_int_eq(is_trunc, 1);
  373. strcpy(buf, "1234567890");
  374. mg_snprintf(NULL, &is_trunc, buf, 0, "%i", 543);
  375. ck_assert_str_eq(buf, "1234567890");
  376. }
  377. END_TEST
  378. START_TEST(test_mg_strcasestr)
  379. {
  380. /* Adapted from unit_test.c */
  381. /* Copyright (c) 2013-2015 the Civetweb developers */
  382. /* Copyright (c) 2004-2013 Sergey Lyubka */
  383. static const char *big1 = "abcdef";
  384. ck_assert(mg_strcasestr("Y", "X") == NULL);
  385. ck_assert(mg_strcasestr("Y", "y") != NULL);
  386. ck_assert(mg_strcasestr(big1, "X") == NULL);
  387. ck_assert(mg_strcasestr(big1, "CD") == big1 + 2);
  388. ck_assert(mg_strcasestr("aa", "AAB") == NULL);
  389. }
  390. END_TEST
  391. START_TEST(test_parse_port_string)
  392. {
  393. /* Adapted from unit_test.c */
  394. /* Copyright (c) 2013-2015 the Civetweb developers */
  395. /* Copyright (c) 2004-2013 Sergey Lyubka */
  396. static const char *valid[] =
  397. { "0",
  398. "1",
  399. "1s",
  400. "1r",
  401. "1.2.3.4:1",
  402. "1.2.3.4:1s",
  403. "1.2.3.4:1r",
  404. #if defined(USE_IPV6)
  405. "[::1]:123",
  406. "[::]:80",
  407. "[3ffe:2a00:100:7031::1]:900",
  408. "+80",
  409. #endif
  410. NULL };
  411. static const char *invalid[] = {
  412. "99999", "1k", "1.2.3", "1.2.3.4:", "1.2.3.4:2p", NULL};
  413. struct socket so;
  414. struct vec vec;
  415. int ip_family;
  416. int i;
  417. for (i = 0; valid[i] != NULL; i++) {
  418. vec.ptr = valid[i];
  419. vec.len = strlen(vec.ptr);
  420. ip_family = 123;
  421. ck_assert_int_ne(parse_port_string(&vec, &so, &ip_family), 0);
  422. if (i < 7) {
  423. ck_assert_int_eq(ip_family, 4);
  424. } else if (i < 10) {
  425. ck_assert_int_eq(ip_family, 6);
  426. } else {
  427. ck_assert_int_eq(ip_family, 4 + 6);
  428. }
  429. }
  430. for (i = 0; invalid[i] != NULL; i++) {
  431. vec.ptr = invalid[i];
  432. vec.len = strlen(vec.ptr);
  433. ip_family = 123;
  434. ck_assert_int_eq(parse_port_string(&vec, &so, &ip_family), 0);
  435. ck_assert_int_eq(ip_family, 0);
  436. }
  437. }
  438. END_TEST
  439. START_TEST(test_encode_decode)
  440. {
  441. char buf[128];
  442. const char *alpha = "abcdefghijklmnopqrstuvwxyz";
  443. const char *nonalpha = " !\"#$%&'()*+,-./0123456789:;<=>?@";
  444. const char *nonalpha_url_enc1 =
  445. "%20%21%22%23$%25%26%27()%2a%2b,-.%2f0123456789%3a;%3c%3d%3e%3f%40";
  446. const char *nonalpha_url_enc2 =
  447. "%20!%22%23%24%25%26'()*%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40";
  448. int ret;
  449. size_t len;
  450. #if defined(USE_WEBSOCKET) || defined(USE_LUA)
  451. const char *alpha_b64_enc = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXo=";
  452. const char *nonalpha_b64_enc =
  453. "ICEiIyQlJicoKSorLC0uLzAxMjM0NTY3ODk6Ozw9Pj9A";
  454. memset(buf, 77, sizeof(buf));
  455. base64_encode((unsigned char *)"a", 1, buf);
  456. ck_assert_str_eq(buf, "YQ==");
  457. memset(buf, 77, sizeof(buf));
  458. base64_encode((unsigned char *)"ab", 1, buf);
  459. ck_assert_str_eq(buf, "YQ==");
  460. memset(buf, 77, sizeof(buf));
  461. base64_encode((unsigned char *)"ab", 2, buf);
  462. ck_assert_str_eq(buf, "YWI=");
  463. memset(buf, 77, sizeof(buf));
  464. base64_encode((unsigned char *)alpha, 3, buf);
  465. ck_assert_str_eq(buf, "YWJj");
  466. memset(buf, 77, sizeof(buf));
  467. base64_encode((unsigned char *)alpha, 4, buf);
  468. ck_assert_str_eq(buf, "YWJjZA==");
  469. memset(buf, 77, sizeof(buf));
  470. base64_encode((unsigned char *)alpha, 5, buf);
  471. ck_assert_str_eq(buf, "YWJjZGU=");
  472. memset(buf, 77, sizeof(buf));
  473. base64_encode((unsigned char *)alpha, 6, buf);
  474. ck_assert_str_eq(buf, "YWJjZGVm");
  475. memset(buf, 77, sizeof(buf));
  476. base64_encode((unsigned char *)alpha, (int)strlen(alpha), buf);
  477. ck_assert_str_eq(buf, alpha_b64_enc);
  478. memset(buf, 77, sizeof(buf));
  479. base64_encode((unsigned char *)nonalpha, (int)strlen(nonalpha), buf);
  480. ck_assert_str_eq(buf, nonalpha_b64_enc);
  481. #endif
  482. #if defined(USE_LUA)
  483. memset(buf, 77, sizeof(buf));
  484. len = 9999;
  485. ret = base64_decode((unsigned char *)alpha_b64_enc,
  486. (int)strlen(alpha_b64_enc),
  487. buf,
  488. &len);
  489. ck_assert_int_eq(ret, -1);
  490. ck_assert_uint_eq((unsigned int)len, (unsigned int)strlen(alpha));
  491. ck_assert_str_eq(buf, alpha);
  492. memset(buf, 77, sizeof(buf));
  493. len = 9999;
  494. ret = base64_decode((unsigned char *)"AAA*AAA", 7, buf, &len);
  495. ck_assert_int_eq(ret, 3);
  496. #endif
  497. memset(buf, 77, sizeof(buf));
  498. ret = mg_url_encode(alpha, buf, sizeof(buf));
  499. ck_assert_int_eq(ret, (int)strlen(buf));
  500. ck_assert_int_eq(ret, (int)strlen(alpha));
  501. ck_assert_str_eq(buf, alpha);
  502. memset(buf, 77, sizeof(buf));
  503. ret = mg_url_encode(nonalpha, buf, sizeof(buf));
  504. ck_assert_int_eq(ret, (int)strlen(buf));
  505. ck_assert_int_eq(ret, (int)strlen(nonalpha_url_enc1));
  506. ck_assert_str_eq(buf, nonalpha_url_enc1);
  507. memset(buf, 77, sizeof(buf));
  508. ret = mg_url_decode(alpha, (int)strlen(alpha), buf, sizeof(buf), 0);
  509. ck_assert_int_eq(ret, (int)strlen(buf));
  510. ck_assert_int_eq(ret, (int)strlen(alpha));
  511. ck_assert_str_eq(buf, alpha);
  512. memset(buf, 77, sizeof(buf));
  513. ret = mg_url_decode(
  514. nonalpha_url_enc1, (int)strlen(nonalpha_url_enc1), buf, sizeof(buf), 0);
  515. ck_assert_int_eq(ret, (int)strlen(buf));
  516. ck_assert_int_eq(ret, (int)strlen(nonalpha));
  517. ck_assert_str_eq(buf, nonalpha);
  518. memset(buf, 77, sizeof(buf));
  519. ret = mg_url_decode(
  520. nonalpha_url_enc2, (int)strlen(nonalpha_url_enc2), buf, sizeof(buf), 0);
  521. ck_assert_int_eq(ret, (int)strlen(buf));
  522. ck_assert_int_eq(ret, (int)strlen(nonalpha));
  523. ck_assert_str_eq(buf, nonalpha);
  524. /* len could be unused, if base64_decode is not tested because USE_LUA is
  525. * not defined */
  526. (void)len;
  527. }
  528. END_TEST
  529. START_TEST(test_mask_data)
  530. {
  531. #if defined(USE_WEBSOCKET)
  532. char in[1024];
  533. char out[1024];
  534. int i;
  535. #endif
  536. uint32_t mask = 0x61626364;
  537. /* TODO: adapt test for big endian */
  538. ck_assert((*(unsigned char *)&mask) == 0x64u);
  539. #if defined(USE_WEBSOCKET)
  540. memset(in, 0, sizeof(in));
  541. memset(out, 99, sizeof(out));
  542. mask_data(in, sizeof(out), 0, out);
  543. ck_assert(!memcmp(out, in, sizeof(out)));
  544. for (i = 0; i < 1024; i++) {
  545. in[i] = (char)((unsigned char)i);
  546. }
  547. mask_data(in, 107, 0, out);
  548. ck_assert(!memcmp(out, in, 107));
  549. mask_data(in, 256, 0x01010101, out);
  550. for (i = 0; i < 256; i++) {
  551. ck_assert_int_eq((int)((unsigned char)out[i]),
  552. (int)(((unsigned char)in[i]) ^ (char)1u));
  553. }
  554. for (i = 256; i < (int)sizeof(out); i++) {
  555. ck_assert_int_eq((int)((unsigned char)out[i]), (int)0);
  556. }
  557. /* TODO: check this for big endian */
  558. mask_data(in, 5, 0x01020304, out);
  559. ck_assert_uint_eq((unsigned char)out[0], 0u ^ 4u);
  560. ck_assert_uint_eq((unsigned char)out[1], 1u ^ 3u);
  561. ck_assert_uint_eq((unsigned char)out[2], 2u ^ 2u);
  562. ck_assert_uint_eq((unsigned char)out[3], 3u ^ 1u);
  563. ck_assert_uint_eq((unsigned char)out[4], 4u ^ 4u);
  564. #endif
  565. }
  566. END_TEST
  567. START_TEST(test_parse_date_string)
  568. {
  569. #if !defined(NO_CACHING)
  570. time_t now = time(0);
  571. struct tm *tm = gmtime(&now);
  572. char date[64] = {0};
  573. unsigned long i;
  574. ck_assert_uint_eq((unsigned long)parse_date_string("1/Jan/1970 00:01:02"),
  575. 62ul);
  576. ck_assert_uint_eq((unsigned long)parse_date_string("1 Jan 1970 00:02:03"),
  577. 123ul);
  578. ck_assert_uint_eq((unsigned long)parse_date_string("1-Jan-1970 00:03:04"),
  579. 184ul);
  580. ck_assert_uint_eq((unsigned long)parse_date_string(
  581. "Xyz, 1 Jan 1970 00:04:05"),
  582. 245ul);
  583. gmt_time_string(date, sizeof(date), &now);
  584. ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
  585. sprintf(date,
  586. "%02u %s %04u %02u:%02u:%02u",
  587. tm->tm_mday,
  588. month_names[tm->tm_mon],
  589. tm->tm_year + 1900,
  590. tm->tm_hour,
  591. tm->tm_min,
  592. tm->tm_sec);
  593. ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
  594. gmt_time_string(date, 1, NULL);
  595. ck_assert_str_eq(date, "");
  596. gmt_time_string(date, 6, NULL);
  597. ck_assert_str_eq(date,
  598. "Thu, "); /* part of "Thu, 01 Jan 1970 00:00:00 GMT" */
  599. gmt_time_string(date, sizeof(date), NULL);
  600. ck_assert_str_eq(date, "Thu, 01 Jan 1970 00:00:00 GMT");
  601. for (i = 2ul; i < 0x8000000ul; i += i / 2) {
  602. now = (time_t)i;
  603. gmt_time_string(date, sizeof(date), &now);
  604. ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
  605. tm = gmtime(&now);
  606. sprintf(date,
  607. "%02u-%s-%04u %02u:%02u:%02u",
  608. tm->tm_mday,
  609. month_names[tm->tm_mon],
  610. tm->tm_year + 1900,
  611. tm->tm_hour,
  612. tm->tm_min,
  613. tm->tm_sec);
  614. ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
  615. }
  616. #endif
  617. }
  618. END_TEST
  619. START_TEST(test_sha1)
  620. {
  621. #ifdef SHA1_DIGEST_SIZE
  622. SHA_CTX sha_ctx;
  623. uint8_t digest[SHA1_DIGEST_SIZE] = {0};
  624. char str[48] = {0};
  625. int i;
  626. const char *test_str;
  627. ck_assert_uint_eq(sizeof(digest), 20);
  628. ck_assert_uint_gt(sizeof(str), sizeof(digest) * 2 + 1);
  629. /* empty string */
  630. SHA1_Init(&sha_ctx);
  631. SHA1_Final(digest, &sha_ctx);
  632. bin2str(str, digest, sizeof(digest));
  633. ck_assert_uint_eq(strlen(str), 40);
  634. ck_assert_str_eq(str, "da39a3ee5e6b4b0d3255bfef95601890afd80709");
  635. /* empty string */
  636. SHA1_Init(&sha_ctx);
  637. SHA1_Update(&sha_ctx, (uint8_t *)"abc", 0);
  638. SHA1_Final(digest, &sha_ctx);
  639. bin2str(str, digest, sizeof(digest));
  640. ck_assert_uint_eq(strlen(str), 40);
  641. ck_assert_str_eq(str, "da39a3ee5e6b4b0d3255bfef95601890afd80709");
  642. /* "abc" */
  643. SHA1_Init(&sha_ctx);
  644. SHA1_Update(&sha_ctx, (uint8_t *)"abc", 3);
  645. SHA1_Final(digest, &sha_ctx);
  646. bin2str(str, digest, sizeof(digest));
  647. ck_assert_uint_eq(strlen(str), 40);
  648. ck_assert_str_eq(str, "a9993e364706816aba3e25717850c26c9cd0d89d");
  649. /* "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" */
  650. test_str = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  651. SHA1_Init(&sha_ctx);
  652. SHA1_Update(&sha_ctx, (uint8_t *)test_str, (uint32_t)strlen(test_str));
  653. SHA1_Final(digest, &sha_ctx);
  654. bin2str(str, digest, sizeof(digest));
  655. ck_assert_uint_eq(strlen(str), 40);
  656. ck_assert_str_eq(str, "84983e441c3bd26ebaae4aa1f95129e5e54670f1");
  657. /* a million "a" */
  658. SHA1_Init(&sha_ctx);
  659. for (i = 0; i < 1000000; i++) {
  660. SHA1_Update(&sha_ctx, (uint8_t *)"a", 1);
  661. }
  662. SHA1_Final(digest, &sha_ctx);
  663. bin2str(str, digest, sizeof(digest));
  664. ck_assert_uint_eq(strlen(str), 40);
  665. ck_assert_str_eq(str, "34aa973cd4c4daa4f61eeb2bdbad27316534016f");
  666. /* a million "a" in blocks of 10 */
  667. SHA1_Init(&sha_ctx);
  668. for (i = 0; i < 100000; i++) {
  669. SHA1_Update(&sha_ctx, (uint8_t *)"aaaaaaaaaa", 10);
  670. }
  671. SHA1_Final(digest, &sha_ctx);
  672. bin2str(str, digest, sizeof(digest));
  673. ck_assert_uint_eq(strlen(str), 40);
  674. ck_assert_str_eq(str, "34aa973cd4c4daa4f61eeb2bdbad27316534016f");
  675. #else
  676. /* Can not test, if SHA1 is not included */
  677. ck_assert(1);
  678. #endif
  679. }
  680. END_TEST
  681. Suite *
  682. make_private_suite(void)
  683. {
  684. Suite *const suite = suite_create("Private");
  685. TCase *const tcase_http_message = tcase_create("HTTP Message");
  686. TCase *const tcase_url_parsing = tcase_create("URL Parsing");
  687. TCase *const tcase_internal_parse = tcase_create("Internal Parsing");
  688. TCase *const tcase_encode_decode = tcase_create("Encode Decode");
  689. TCase *const tcase_mask_data = tcase_create("Mask Data");
  690. TCase *const tcase_parse_date_string = tcase_create("Date Parsing");
  691. TCase *const tcase_sha1 = tcase_create("SHA1");
  692. tcase_add_test(tcase_http_message, test_parse_http_message);
  693. tcase_add_test(tcase_http_message, test_should_keep_alive);
  694. tcase_set_timeout(tcase_http_message, civetweb_min_test_timeout);
  695. suite_add_tcase(suite, tcase_http_message);
  696. tcase_add_test(tcase_url_parsing, test_match_prefix);
  697. tcase_add_test(tcase_url_parsing,
  698. test_remove_double_dots_and_double_slashes);
  699. tcase_add_test(tcase_url_parsing, test_is_valid_uri);
  700. tcase_set_timeout(tcase_url_parsing, civetweb_min_test_timeout);
  701. suite_add_tcase(suite, tcase_url_parsing);
  702. tcase_add_test(tcase_internal_parse, test_next_option);
  703. tcase_add_test(tcase_internal_parse, test_skip_quoted);
  704. tcase_add_test(tcase_internal_parse, test_mg_strcasestr);
  705. tcase_add_test(tcase_internal_parse, test_alloc_vprintf);
  706. tcase_add_test(tcase_internal_parse, test_mg_vsnprintf);
  707. tcase_add_test(tcase_internal_parse, test_parse_port_string);
  708. tcase_set_timeout(tcase_internal_parse, civetweb_min_test_timeout);
  709. suite_add_tcase(suite, tcase_internal_parse);
  710. tcase_add_test(tcase_encode_decode, test_encode_decode);
  711. tcase_set_timeout(tcase_encode_decode, civetweb_min_test_timeout);
  712. suite_add_tcase(suite, tcase_encode_decode);
  713. tcase_add_test(tcase_mask_data, test_mask_data);
  714. tcase_set_timeout(tcase_mask_data, civetweb_min_test_timeout);
  715. suite_add_tcase(suite, tcase_mask_data);
  716. tcase_add_test(tcase_parse_date_string, test_parse_date_string);
  717. tcase_set_timeout(tcase_parse_date_string, civetweb_min_test_timeout);
  718. suite_add_tcase(suite, tcase_parse_date_string);
  719. tcase_add_test(tcase_sha1, test_sha1);
  720. tcase_set_timeout(tcase_sha1, civetweb_min_test_timeout);
  721. suite_add_tcase(suite, tcase_sha1);
  722. return suite;
  723. }
  724. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  725. /* Used to debug test cases without using the check framework */
  726. void
  727. MAIN_PRIVATE(void)
  728. {
  729. #if defined(_WIN32)
  730. /* test_parse_port_string requires WSAStartup for IPv6 */
  731. WSADATA data;
  732. WSAStartup(MAKEWORD(2, 2), &data);
  733. #endif
  734. test_alloc_vprintf(0);
  735. test_mg_vsnprintf(0);
  736. test_remove_double_dots_and_double_slashes(0);
  737. test_parse_date_string(0);
  738. test_parse_port_string(0);
  739. test_parse_http_message(0);
  740. test_sha1(0);
  741. #if defined(_WIN32)
  742. WSACleanup();
  743. #endif
  744. }
  745. #endif