private.c 29 KB

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