private.c 26 KB

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