private.c 24 KB

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