private.c 28 KB

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