private.c 24 KB

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