private.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /* Copyright (c) 2015-2016 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. #define HAVE_STDINT
  33. #undef MEMORY_DEBUGGING
  34. #endif
  35. #include "../src/civetweb.c"
  36. #include <stdlib.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. {"/a...///", "/a.../"},
  177. {"/.x", "/.x"},
  178. {"/\\", "/"},
  179. {"/a\\", "/a\\"},
  180. {"/a\\\\...", "/a\\."},
  181. };
  182. size_t i;
  183. for (i = 0; i < ARRAY_SIZE(data); i++) {
  184. remove_double_dots_and_double_slashes(data[i].before);
  185. ck_assert_str_eq(data[i].before, data[i].after);
  186. }
  187. }
  188. END_TEST
  189. START_TEST(test_is_valid_uri)
  190. {
  191. /* is_valid_uri is superseeded by get_uri_type */
  192. ck_assert_int_eq(2, get_uri_type("/api"));
  193. ck_assert_int_eq(2, get_uri_type("/api/"));
  194. ck_assert_int_eq(2,
  195. get_uri_type("/some/long/path%20with%20space/file.xyz"));
  196. ck_assert_int_eq(0, get_uri_type("api"));
  197. ck_assert_int_eq(1, get_uri_type("*"));
  198. ck_assert_int_eq(0, get_uri_type("*xy"));
  199. ck_assert_int_eq(3, get_uri_type("http://somewhere/"));
  200. ck_assert_int_eq(3, get_uri_type("https://somewhere/some/file.html"));
  201. ck_assert_int_eq(4, get_uri_type("http://somewhere:8080/"));
  202. ck_assert_int_eq(4, get_uri_type("https://somewhere:8080/some/file.html"));
  203. }
  204. END_TEST
  205. START_TEST(test_next_option)
  206. {
  207. /* Adapted from unit_test.c */
  208. /* Copyright (c) 2013-2015 the Civetweb developers */
  209. /* Copyright (c) 2004-2013 Sergey Lyubka */
  210. const char *p, *list = "x/8,/y**=1;2k,z";
  211. struct vec a, b;
  212. int i;
  213. ck_assert(next_option(NULL, &a, &b) == NULL);
  214. for (i = 0, p = list; (p = next_option(p, &a, &b)) != NULL; i++) {
  215. ck_assert(i != 0 || (a.ptr == list && a.len == 3 && b.len == 0));
  216. ck_assert(i != 1
  217. || (a.ptr == list + 4 && a.len == 4 && b.ptr == list + 9
  218. && b.len == 4));
  219. ck_assert(i != 2 || (a.ptr == list + 14 && a.len == 1 && b.len == 0));
  220. }
  221. }
  222. END_TEST
  223. START_TEST(test_skip_quoted)
  224. {
  225. /* Adapted from unit_test.c */
  226. /* Copyright (c) 2013-2015 the Civetweb developers */
  227. /* Copyright (c) 2004-2013 Sergey Lyubka */
  228. char x[] = "a=1, b=2, c='hi \' there', d='here\\, there'", *s = x, *p;
  229. p = skip_quoted(&s, ", ", ", ", 0);
  230. ck_assert(p != NULL && !strcmp(p, "a=1"));
  231. p = skip_quoted(&s, ", ", ", ", 0);
  232. ck_assert(p != NULL && !strcmp(p, "b=2"));
  233. p = skip_quoted(&s, ",", " ", 0);
  234. ck_assert(p != NULL && !strcmp(p, "c='hi \' there'"));
  235. p = skip_quoted(&s, ",", " ", '\\');
  236. ck_assert(p != NULL && !strcmp(p, "d='here, there'"));
  237. ck_assert(*s == 0);
  238. }
  239. END_TEST
  240. static int
  241. alloc_printf(char **buf, size_t size, const char *fmt, ...)
  242. {
  243. /* Test helper function - adapted from unit_test.c */
  244. /* Copyright (c) 2013-2015 the Civetweb developers */
  245. /* Copyright (c) 2004-2013 Sergey Lyubka */
  246. va_list ap;
  247. int ret = 0;
  248. va_start(ap, fmt);
  249. ret = alloc_vprintf(buf, *buf, size, fmt, ap);
  250. va_end(ap);
  251. return ret;
  252. }
  253. static int
  254. alloc_printf2(char **buf, const char *fmt, ...)
  255. {
  256. /* Test alternative implementation */
  257. va_list ap;
  258. int ret = 0;
  259. va_start(ap, fmt);
  260. ret = alloc_vprintf2(buf, fmt, ap);
  261. va_end(ap);
  262. return ret;
  263. }
  264. START_TEST(test_alloc_vprintf)
  265. {
  266. /* Adapted from unit_test.c */
  267. /* Copyright (c) 2013-2015 the Civetweb developers */
  268. /* Copyright (c) 2004-2013 Sergey Lyubka */
  269. char buf[MG_BUF_LEN], *p = buf;
  270. ck_assert(alloc_printf(&p, sizeof(buf), "%s", "hi") == 2);
  271. ck_assert(p == buf);
  272. ck_assert(alloc_printf(&p, sizeof(buf), "%s", "") == 0);
  273. ck_assert(p == buf);
  274. ck_assert(alloc_printf(&p, sizeof(buf), "") == 0);
  275. ck_assert(p == buf);
  276. /* Pass small buffer, make sure alloc_printf allocates */
  277. ck_assert(alloc_printf(&p, 1, "%s", "hello") == 5);
  278. ck_assert(p != buf);
  279. mg_free(p);
  280. p = buf;
  281. /* Test alternative implementation */
  282. ck_assert(alloc_printf2(&p, "%s", "hello") == 5);
  283. ck_assert(p != buf);
  284. mg_free(p);
  285. p = buf;
  286. }
  287. END_TEST
  288. START_TEST(test_mg_vsnprintf)
  289. {
  290. char buf[16];
  291. int trunc;
  292. memset(buf, 0, sizeof(buf));
  293. trunc = 777;
  294. mg_snprintf(NULL, &trunc, buf, 10, "%8i", 123);
  295. ck_assert_str_eq(buf, " 123");
  296. ck_assert_int_eq(trunc, 0);
  297. trunc = 777;
  298. mg_snprintf(NULL, &trunc, buf, 10, "%9i", 123);
  299. ck_assert_str_eq(buf, " 123");
  300. ck_assert_int_eq(trunc, 0);
  301. trunc = 777;
  302. mg_snprintf(NULL, &trunc, buf, 9, "%9i", 123);
  303. ck_assert_str_eq(buf, " 12");
  304. ck_assert_int_eq(trunc, 1);
  305. trunc = 777;
  306. mg_snprintf(NULL, &trunc, buf, 8, "%9i", 123);
  307. ck_assert_str_eq(buf, " 1");
  308. ck_assert_int_eq(trunc, 1);
  309. trunc = 777;
  310. mg_snprintf(NULL, &trunc, buf, 7, "%9i", 123);
  311. ck_assert_str_eq(buf, " ");
  312. ck_assert_int_eq(trunc, 1);
  313. strcpy(buf, "1234567890");
  314. mg_snprintf(NULL, &trunc, buf, 0, "%i", 543);
  315. ck_assert_str_eq(buf, "1234567890");
  316. }
  317. END_TEST
  318. START_TEST(test_mg_strcasestr)
  319. {
  320. /* Adapted from unit_test.c */
  321. /* Copyright (c) 2013-2015 the Civetweb developers */
  322. /* Copyright (c) 2004-2013 Sergey Lyubka */
  323. static const char *big1 = "abcdef";
  324. ck_assert(mg_strcasestr("Y", "X") == NULL);
  325. ck_assert(mg_strcasestr("Y", "y") != NULL);
  326. ck_assert(mg_strcasestr(big1, "X") == NULL);
  327. ck_assert(mg_strcasestr(big1, "CD") == big1 + 2);
  328. ck_assert(mg_strcasestr("aa", "AAB") == NULL);
  329. }
  330. END_TEST
  331. START_TEST(test_parse_port_string)
  332. {
  333. /* Adapted from unit_test.c */
  334. /* Copyright (c) 2013-2015 the Civetweb developers */
  335. /* Copyright (c) 2004-2013 Sergey Lyubka */
  336. static const char *valid[] =
  337. { "0",
  338. "1",
  339. "1s",
  340. "1r",
  341. "1.2.3.4:1",
  342. "1.2.3.4:1s",
  343. "1.2.3.4:1r",
  344. #if defined(USE_IPV6)
  345. "[::1]:123",
  346. "[::]:80",
  347. "[3ffe:2a00:100:7031::1]:900",
  348. #endif
  349. NULL };
  350. static const char *invalid[] = {
  351. "99999", "1k", "1.2.3", "1.2.3.4:", "1.2.3.4:2p", NULL};
  352. struct socket so;
  353. struct vec vec;
  354. int i;
  355. for (i = 0; valid[i] != NULL; i++) {
  356. vec.ptr = valid[i];
  357. vec.len = strlen(vec.ptr);
  358. ck_assert(parse_port_string(&vec, &so) != 0);
  359. }
  360. for (i = 0; invalid[i] != NULL; i++) {
  361. vec.ptr = invalid[i];
  362. vec.len = strlen(vec.ptr);
  363. ck_assert(parse_port_string(&vec, &so) == 0);
  364. }
  365. }
  366. END_TEST
  367. START_TEST(test_encode_decode)
  368. {
  369. char buf[128];
  370. const char *alpha = "abcdefghijklmnopqrstuvwxyz";
  371. const char *nonalpha = " !\"#$%&'()*+,-./0123456789:;<=>?@";
  372. const char *nonalpha_url_enc1 =
  373. "%20%21%22%23$%25%26%27()%2a%2b,-.%2f0123456789%3a;%3c%3d%3e%3f%40";
  374. const char *nonalpha_url_enc2 =
  375. "%20!%22%23%24%25%26'()*%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40";
  376. int ret;
  377. size_t len;
  378. #if defined(USE_WEBSOCKET) || defined(USE_LUA)
  379. const char *alpha_b64_enc = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXo=";
  380. const char *nonalpha_b64_enc =
  381. "ICEiIyQlJicoKSorLC0uLzAxMjM0NTY3ODk6Ozw9Pj9A";
  382. memset(buf, 77, sizeof(buf));
  383. base64_encode((unsigned char *)"a", 1, buf);
  384. ck_assert_str_eq(buf, "YQ==");
  385. memset(buf, 77, sizeof(buf));
  386. base64_encode((unsigned char *)"ab", 1, buf);
  387. ck_assert_str_eq(buf, "YQ==");
  388. memset(buf, 77, sizeof(buf));
  389. base64_encode((unsigned char *)"ab", 2, buf);
  390. ck_assert_str_eq(buf, "YWI=");
  391. memset(buf, 77, sizeof(buf));
  392. base64_encode((unsigned char *)alpha, 3, buf);
  393. ck_assert_str_eq(buf, "YWJj");
  394. memset(buf, 77, sizeof(buf));
  395. base64_encode((unsigned char *)alpha, 4, buf);
  396. ck_assert_str_eq(buf, "YWJjZA==");
  397. memset(buf, 77, sizeof(buf));
  398. base64_encode((unsigned char *)alpha, 5, buf);
  399. ck_assert_str_eq(buf, "YWJjZGU=");
  400. memset(buf, 77, sizeof(buf));
  401. base64_encode((unsigned char *)alpha, 6, buf);
  402. ck_assert_str_eq(buf, "YWJjZGVm");
  403. memset(buf, 77, sizeof(buf));
  404. base64_encode((unsigned char *)alpha, (int)strlen(alpha), buf);
  405. ck_assert_str_eq(buf, alpha_b64_enc);
  406. memset(buf, 77, sizeof(buf));
  407. base64_encode((unsigned char *)nonalpha, (int)strlen(nonalpha), buf);
  408. ck_assert_str_eq(buf, nonalpha_b64_enc);
  409. #endif
  410. #if defined(USE_LUA)
  411. memset(buf, 77, sizeof(buf));
  412. len = 9999;
  413. ret = base64_decode((unsigned char *)alpha_b64_enc,
  414. (int)strlen(alpha_b64_enc),
  415. buf,
  416. &len);
  417. ck_assert_int_eq(ret, -1);
  418. ck_assert_uint_eq((unsigned int)len, (unsigned int)strlen(alpha));
  419. ck_assert_str_eq(buf, alpha);
  420. memset(buf, 77, sizeof(buf));
  421. len = 9999;
  422. ret = base64_decode((unsigned char *)"AAA*AAA", 7, buf, &len);
  423. ck_assert_int_eq(ret, 3);
  424. #endif
  425. memset(buf, 77, sizeof(buf));
  426. ret = mg_url_encode(alpha, buf, sizeof(buf));
  427. ck_assert_int_eq(ret, (int)strlen(buf));
  428. ck_assert_int_eq(ret, (int)strlen(alpha));
  429. ck_assert_str_eq(buf, alpha);
  430. memset(buf, 77, sizeof(buf));
  431. ret = mg_url_encode(nonalpha, buf, sizeof(buf));
  432. ck_assert_int_eq(ret, (int)strlen(buf));
  433. ck_assert_int_eq(ret, (int)strlen(nonalpha_url_enc1));
  434. ck_assert_str_eq(buf, nonalpha_url_enc1);
  435. memset(buf, 77, sizeof(buf));
  436. ret = mg_url_decode(alpha, (int)strlen(alpha), buf, sizeof(buf), 0);
  437. ck_assert_int_eq(ret, (int)strlen(buf));
  438. ck_assert_int_eq(ret, (int)strlen(alpha));
  439. ck_assert_str_eq(buf, alpha);
  440. memset(buf, 77, sizeof(buf));
  441. ret = mg_url_decode(
  442. nonalpha_url_enc1, (int)strlen(nonalpha_url_enc1), buf, sizeof(buf), 0);
  443. ck_assert_int_eq(ret, (int)strlen(buf));
  444. ck_assert_int_eq(ret, (int)strlen(nonalpha));
  445. ck_assert_str_eq(buf, nonalpha);
  446. memset(buf, 77, sizeof(buf));
  447. ret = mg_url_decode(
  448. nonalpha_url_enc2, (int)strlen(nonalpha_url_enc2), buf, sizeof(buf), 0);
  449. ck_assert_int_eq(ret, (int)strlen(buf));
  450. ck_assert_int_eq(ret, (int)strlen(nonalpha));
  451. ck_assert_str_eq(buf, nonalpha);
  452. /* len could be unused, if base64_decode is not tested because USE_LUA is
  453. * not defined */
  454. (void)len;
  455. }
  456. END_TEST
  457. START_TEST(test_mask_data)
  458. {
  459. #if defined(USE_WEBSOCKET)
  460. char in[1024];
  461. char out[1024];
  462. int i;
  463. #endif
  464. uint32_t mask = 0x61626364;
  465. /* TODO: adapt test for big endian */
  466. ck_assert((*(unsigned char *)&mask) == 0x64u);
  467. #if defined(USE_WEBSOCKET)
  468. memset(in, 0, sizeof(in));
  469. memset(out, 99, sizeof(out));
  470. mask_data(in, sizeof(out), 0, out);
  471. ck_assert(!memcmp(out, in, sizeof(out)));
  472. for (i = 0; i < 1024; i++) {
  473. in[i] = (char)((unsigned char)i);
  474. }
  475. mask_data(in, 107, 0, out);
  476. ck_assert(!memcmp(out, in, 107));
  477. mask_data(in, 256, 0x01010101, out);
  478. for (i = 0; i < 256; i++) {
  479. ck_assert_int_eq((int)((unsigned char)out[i]),
  480. (int)(((unsigned char)in[i]) ^ (char)1u));
  481. }
  482. for (i = 256; i < (int)sizeof(out); i++) {
  483. ck_assert_int_eq((int)((unsigned char)out[i]), (int)0);
  484. }
  485. /* TODO: check this for big endian */
  486. mask_data(in, 5, 0x01020304, out);
  487. ck_assert_uint_eq((unsigned char)out[0], 0u ^ 4u);
  488. ck_assert_uint_eq((unsigned char)out[1], 1u ^ 3u);
  489. ck_assert_uint_eq((unsigned char)out[2], 2u ^ 2u);
  490. ck_assert_uint_eq((unsigned char)out[3], 3u ^ 1u);
  491. ck_assert_uint_eq((unsigned char)out[4], 4u ^ 4u);
  492. #endif
  493. }
  494. END_TEST
  495. START_TEST(test_parse_date_string)
  496. {
  497. #if !defined(NO_CACHING)
  498. time_t now = time(0);
  499. struct tm *tm = gmtime(&now);
  500. char date[64] = {0};
  501. unsigned long i;
  502. ck_assert_uint_eq((unsigned long)parse_date_string("1/Jan/1970 00:01:02"),
  503. 62ul);
  504. ck_assert_uint_eq((unsigned long)parse_date_string("1 Jan 1970 00:02:03"),
  505. 123ul);
  506. ck_assert_uint_eq((unsigned long)parse_date_string("1-Jan-1970 00:03:04"),
  507. 184ul);
  508. ck_assert_uint_eq((unsigned long)parse_date_string(
  509. "Xyz, 1 Jan 1970 00:04:05"),
  510. 245ul);
  511. gmt_time_string(date, sizeof(date), &now);
  512. ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
  513. sprintf(date,
  514. "%02u %s %04u %02u:%02u:%02u",
  515. tm->tm_mday,
  516. month_names[tm->tm_mon],
  517. tm->tm_year + 1900,
  518. tm->tm_hour,
  519. tm->tm_min,
  520. tm->tm_sec);
  521. ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
  522. gmt_time_string(date, 1, NULL);
  523. ck_assert_str_eq(date, "");
  524. gmt_time_string(date, 6, NULL);
  525. ck_assert_str_eq(date,
  526. "Thu, "); /* part of "Thu, 01 Jan 1970 00:00:00 GMT" */
  527. gmt_time_string(date, sizeof(date), NULL);
  528. ck_assert_str_eq(date, "Thu, 01 Jan 1970 00:00:00 GMT");
  529. for (i = 2ul; i < 0x8000000ul; i += i / 2) {
  530. now = (time_t)i;
  531. gmt_time_string(date, sizeof(date), &now);
  532. ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
  533. tm = gmtime(&now);
  534. sprintf(date,
  535. "%02u-%s-%04u %02u:%02u:%02u",
  536. tm->tm_mday,
  537. month_names[tm->tm_mon],
  538. tm->tm_year + 1900,
  539. tm->tm_hour,
  540. tm->tm_min,
  541. tm->tm_sec);
  542. ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
  543. }
  544. #endif
  545. }
  546. END_TEST
  547. Suite *
  548. make_private_suite(void)
  549. {
  550. Suite *const suite = suite_create("Private");
  551. TCase *const tcase_http_message = tcase_create("HTTP Message");
  552. TCase *const tcase_url_parsing = tcase_create("URL Parsing");
  553. TCase *const tcase_internal_parse = tcase_create("Internal Parsing");
  554. TCase *const tcase_encode_decode = tcase_create("Encode Decode");
  555. TCase *const tcase_mask_data = tcase_create("Mask Data");
  556. TCase *const tcase_parse_date_string = tcase_create("Date Parsing");
  557. tcase_add_test(tcase_http_message, test_parse_http_message);
  558. tcase_add_test(tcase_http_message, test_should_keep_alive);
  559. tcase_set_timeout(tcase_http_message, civetweb_min_test_timeout);
  560. suite_add_tcase(suite, tcase_http_message);
  561. tcase_add_test(tcase_url_parsing, test_match_prefix);
  562. tcase_add_test(tcase_url_parsing,
  563. test_remove_double_dots_and_double_slashes);
  564. tcase_add_test(tcase_url_parsing, test_is_valid_uri);
  565. tcase_set_timeout(tcase_url_parsing, civetweb_min_test_timeout);
  566. suite_add_tcase(suite, tcase_url_parsing);
  567. tcase_add_test(tcase_internal_parse, test_next_option);
  568. tcase_add_test(tcase_internal_parse, test_skip_quoted);
  569. tcase_add_test(tcase_internal_parse, test_mg_strcasestr);
  570. tcase_add_test(tcase_internal_parse, test_alloc_vprintf);
  571. tcase_add_test(tcase_internal_parse, test_mg_vsnprintf);
  572. tcase_add_test(tcase_internal_parse, test_parse_port_string);
  573. tcase_set_timeout(tcase_internal_parse, civetweb_min_test_timeout);
  574. suite_add_tcase(suite, tcase_internal_parse);
  575. tcase_add_test(tcase_encode_decode, test_encode_decode);
  576. tcase_set_timeout(tcase_encode_decode, civetweb_min_test_timeout);
  577. suite_add_tcase(suite, tcase_encode_decode);
  578. tcase_add_test(tcase_mask_data, test_mask_data);
  579. tcase_set_timeout(tcase_mask_data, civetweb_min_test_timeout);
  580. suite_add_tcase(suite, tcase_mask_data);
  581. tcase_add_test(tcase_parse_date_string, test_parse_date_string);
  582. tcase_set_timeout(tcase_mask_data, civetweb_min_test_timeout);
  583. suite_add_tcase(suite, tcase_parse_date_string);
  584. return suite;
  585. }
  586. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  587. /* Used to debug test cases without using the check framework */
  588. void
  589. MAIN_PRIVATE(void)
  590. {
  591. test_alloc_vprintf(0);
  592. test_mg_vsnprintf(0);
  593. test_parse_date_string(0);
  594. test_parse_http_message(0);
  595. }
  596. #endif