private.c 25 KB

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