private.c 21 KB

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