private.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /* Copyright (c) 2015 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. #define _CRT_SECURE_NO_WARNINGS
  27. #define CIVETWEB_API static
  28. #endif
  29. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  30. #define HAVE_STDINT
  31. #undef MEMORY_DEBUGGING
  32. #endif
  33. #include "../src/civetweb.c"
  34. #include <stdlib.h>
  35. #include "private.h"
  36. /* This unit test file uses the excellent Check unit testing library.
  37. * The API documentation is available here:
  38. * http://check.sourceforge.net/doc/check_html/index.html
  39. */
  40. START_TEST(test_parse_http_message)
  41. {
  42. /* Adapted from unit_test.c */
  43. /* Copyright (c) 2013-2015 the Civetweb developers */
  44. /* Copyright (c) 2004-2013 Sergey Lyubka */
  45. struct mg_request_info ri;
  46. char empty[] = "";
  47. char req1[] = "GET / HTTP/1.1\r\n\r\n";
  48. char req2[] = "BLAH / HTTP/1.1\r\n\r\n";
  49. char req3[] = "GET / HTTP/1.1\r\nBah\r\n";
  50. char req4[] = "GET / HTTP/1.1\r\nA: foo bar\r\nB: bar\r\nbaz\r\n\r\n";
  51. char req5[] = "GET / HTTP/1.1\r\n\r\n";
  52. char req6[] = "G";
  53. char req7[] = " blah ";
  54. char req8[] = " HTTP/1.1 200 OK \n\n";
  55. char req9[] = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n";
  56. ck_assert_int_eq(sizeof(req9) - 1,
  57. parse_http_message(req9, sizeof(req9), &ri));
  58. ck_assert_int_eq(1, ri.num_headers);
  59. ck_assert_int_eq(sizeof(req1) - 1,
  60. parse_http_message(req1, sizeof(req1), &ri));
  61. ck_assert_str_eq("1.1", ri.http_version);
  62. ck_assert_int_eq(0, ri.num_headers);
  63. ck_assert_int_eq(-1, parse_http_message(req2, sizeof(req2), &ri));
  64. ck_assert_int_eq(0, parse_http_message(req3, sizeof(req3), &ri));
  65. ck_assert_int_eq(0, parse_http_message(req6, sizeof(req6), &ri));
  66. ck_assert_int_eq(0, parse_http_message(req7, sizeof(req7), &ri));
  67. ck_assert_int_eq(0, parse_http_message(empty, 0, &ri));
  68. ck_assert_int_eq(sizeof(req8) - 1,
  69. parse_http_message(req8, sizeof(req8), &ri));
  70. /* TODO(lsm): Fix this. Header value may span multiple lines. */
  71. ck_assert_int_eq(sizeof(req4) - 1,
  72. parse_http_message(req4, sizeof(req4), &ri));
  73. ck_assert_str_eq("1.1", ri.http_version);
  74. ck_assert_int_eq(3, ri.num_headers);
  75. ck_assert_str_eq("A", ri.http_headers[0].name);
  76. ck_assert_str_eq("foo bar", ri.http_headers[0].value);
  77. ck_assert_str_eq("B", ri.http_headers[1].name);
  78. ck_assert_str_eq("bar", ri.http_headers[1].value);
  79. ck_assert_str_eq("baz", ri.http_headers[2].name);
  80. ck_assert(ri.http_headers[2].value == NULL);
  81. ck_assert_int_eq(sizeof(req5) - 1,
  82. parse_http_message(req5, sizeof(req5), &ri));
  83. ck_assert_str_eq("GET", ri.request_method);
  84. ck_assert_str_eq("1.1", ri.http_version);
  85. }
  86. END_TEST
  87. START_TEST(test_should_keep_alive)
  88. {
  89. /* Adapted from unit_test.c */
  90. /* Copyright (c) 2013-2015 the Civetweb developers */
  91. /* Copyright (c) 2004-2013 Sergey Lyubka */
  92. struct mg_connection conn;
  93. struct mg_context ctx;
  94. char req1[] = "GET / HTTP/1.1\r\n\r\n";
  95. char req2[] = "GET / HTTP/1.0\r\n\r\n";
  96. char req3[] = "GET / HTTP/1.1\r\nConnection: close\r\n\r\n";
  97. char req4[] = "GET / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";
  98. char yes[] = "yes";
  99. char no[] = "no";
  100. memset(&conn, 0, sizeof(conn));
  101. conn.ctx = &ctx;
  102. ck_assert_int_eq(parse_http_message(req1, sizeof(req1), &conn.request_info),
  103. sizeof(req1) - 1);
  104. ctx.config[ENABLE_KEEP_ALIVE] = no;
  105. ck_assert_int_eq(should_keep_alive(&conn), 0);
  106. ctx.config[ENABLE_KEEP_ALIVE] = yes;
  107. ck_assert_int_eq(should_keep_alive(&conn), 1);
  108. conn.must_close = 1;
  109. ck_assert_int_eq(should_keep_alive(&conn), 0);
  110. conn.must_close = 0;
  111. parse_http_message(req2, sizeof(req2), &conn.request_info);
  112. ck_assert_int_eq(should_keep_alive(&conn), 0);
  113. parse_http_message(req3, sizeof(req3), &conn.request_info);
  114. ck_assert_int_eq(should_keep_alive(&conn), 0);
  115. parse_http_message(req4, sizeof(req4), &conn.request_info);
  116. ck_assert_int_eq(should_keep_alive(&conn), 1);
  117. conn.status_code = 401;
  118. ck_assert_int_eq(should_keep_alive(&conn), 0);
  119. conn.status_code = 200;
  120. conn.must_close = 1;
  121. ck_assert_int_eq(should_keep_alive(&conn), 0);
  122. }
  123. END_TEST
  124. START_TEST(test_match_prefix)
  125. {
  126. /* Adapted from unit_test.c */
  127. /* Copyright (c) 2013-2015 the Civetweb developers */
  128. /* Copyright (c) 2004-2013 Sergey Lyubka */
  129. ck_assert_int_eq(4, match_prefix("/api", 4, "/api"));
  130. ck_assert_int_eq(3, match_prefix("/a/", 3, "/a/b/c"));
  131. ck_assert_int_eq(-1, match_prefix("/a/", 3, "/ab/c"));
  132. ck_assert_int_eq(4, match_prefix("/*/", 3, "/ab/c"));
  133. ck_assert_int_eq(6, match_prefix("**", 2, "/a/b/c"));
  134. ck_assert_int_eq(2, match_prefix("/*", 2, "/a/b/c"));
  135. ck_assert_int_eq(2, match_prefix("*/*", 3, "/a/b/c"));
  136. ck_assert_int_eq(5, match_prefix("**/", 3, "/a/b/c"));
  137. ck_assert_int_eq(5, match_prefix("**.foo|**.bar", 13, "a.bar"));
  138. ck_assert_int_eq(2, match_prefix("a|b|cd", 6, "cdef"));
  139. ck_assert_int_eq(2, match_prefix("a|b|c?", 6, "cdef"));
  140. ck_assert_int_eq(1, match_prefix("a|?|cd", 6, "cdef"));
  141. ck_assert_int_eq(-1, match_prefix("/a/**.cgi", 9, "/foo/bar/x.cgi"));
  142. ck_assert_int_eq(12, match_prefix("/a/**.cgi", 9, "/a/bar/x.cgi"));
  143. ck_assert_int_eq(5, match_prefix("**/", 3, "/a/b/c"));
  144. ck_assert_int_eq(-1, match_prefix("**/$", 4, "/a/b/c"));
  145. ck_assert_int_eq(5, match_prefix("**/$", 4, "/a/b/"));
  146. ck_assert_int_eq(0, match_prefix("$", 1, ""));
  147. ck_assert_int_eq(-1, match_prefix("$", 1, "x"));
  148. ck_assert_int_eq(1, match_prefix("*$", 2, "x"));
  149. ck_assert_int_eq(1, match_prefix("/$", 2, "/"));
  150. ck_assert_int_eq(-1, match_prefix("**/$", 4, "/a/b/c"));
  151. ck_assert_int_eq(5, match_prefix("**/$", 4, "/a/b/"));
  152. ck_assert_int_eq(0, match_prefix("*", 1, "/hello/"));
  153. ck_assert_int_eq(-1, match_prefix("**.a$|**.b$", 11, "/a/b.b/"));
  154. ck_assert_int_eq(6, match_prefix("**.a$|**.b$", 11, "/a/b.b"));
  155. ck_assert_int_eq(6, match_prefix("**.a$|**.b$", 11, "/a/B.A"));
  156. ck_assert_int_eq(5, match_prefix("**o$", 4, "HELLO"));
  157. }
  158. END_TEST
  159. START_TEST(test_remove_double_dots_and_double_slashes)
  160. {
  161. /* Adapted from unit_test.c */
  162. /* Copyright (c) 2013-2015 the Civetweb developers */
  163. /* Copyright (c) 2004-2013 Sergey Lyubka */
  164. struct {
  165. char before[20], after[20];
  166. } data[] = {
  167. {"////a", "/a"},
  168. {"/.....", "/."},
  169. {"/......", "/"},
  170. {"...", "..."},
  171. {"/...///", "/./"},
  172. {"/a...///", "/a.../"},
  173. {"/.x", "/.x"},
  174. {"/\\", "/"},
  175. {"/a\\", "/a\\"},
  176. {"/a\\\\...", "/a\\."},
  177. };
  178. size_t i;
  179. for (i = 0; i < ARRAY_SIZE(data); i++) {
  180. remove_double_dots_and_double_slashes(data[i].before);
  181. ck_assert_str_eq(data[i].before, data[i].after);
  182. }
  183. }
  184. END_TEST
  185. START_TEST(test_is_valid_uri)
  186. {
  187. /* is_valid_uri is superseeded by get_uri_type */
  188. ck_assert_int_eq(2, get_uri_type("/api"));
  189. ck_assert_int_eq(2, get_uri_type("/api/"));
  190. ck_assert_int_eq(2,
  191. get_uri_type("/some/long/path%20with%20space/file.xyz"));
  192. ck_assert_int_eq(0, get_uri_type("api"));
  193. ck_assert_int_eq(1, get_uri_type("*"));
  194. ck_assert_int_eq(0, get_uri_type("*xy"));
  195. ck_assert_int_eq(3, get_uri_type("http://somewhere/"));
  196. ck_assert_int_eq(3, get_uri_type("https://somewhere/some/file.html"));
  197. ck_assert_int_eq(4, get_uri_type("http://somewhere:8080/"));
  198. ck_assert_int_eq(4, get_uri_type("https://somewhere:8080/some/file.html"));
  199. }
  200. END_TEST
  201. START_TEST(test_next_option)
  202. {
  203. /* Adapted from unit_test.c */
  204. /* Copyright (c) 2013-2015 the Civetweb developers */
  205. /* Copyright (c) 2004-2013 Sergey Lyubka */
  206. const char *p, *list = "x/8,/y**=1;2k,z";
  207. struct vec a, b;
  208. int i;
  209. ck_assert(next_option(NULL, &a, &b) == NULL);
  210. for (i = 0, p = list; (p = next_option(p, &a, &b)) != NULL; i++) {
  211. ck_assert(i != 0 || (a.ptr == list && a.len == 3 && b.len == 0));
  212. ck_assert(i != 1
  213. || (a.ptr == list + 4 && a.len == 4 && b.ptr == list + 9
  214. && b.len == 4));
  215. ck_assert(i != 2 || (a.ptr == list + 14 && a.len == 1 && b.len == 0));
  216. }
  217. }
  218. END_TEST
  219. START_TEST(test_skip_quoted)
  220. {
  221. /* Adapted from unit_test.c */
  222. /* Copyright (c) 2013-2015 the Civetweb developers */
  223. /* Copyright (c) 2004-2013 Sergey Lyubka */
  224. char x[] = "a=1, b=2, c='hi \' there', d='here\\, there'", *s = x, *p;
  225. p = skip_quoted(&s, ", ", ", ", 0);
  226. ck_assert(p != NULL && !strcmp(p, "a=1"));
  227. p = skip_quoted(&s, ", ", ", ", 0);
  228. ck_assert(p != NULL && !strcmp(p, "b=2"));
  229. p = skip_quoted(&s, ",", " ", 0);
  230. ck_assert(p != NULL && !strcmp(p, "c='hi \' there'"));
  231. p = skip_quoted(&s, ",", " ", '\\');
  232. ck_assert(p != NULL && !strcmp(p, "d='here, there'"));
  233. ck_assert(*s == 0);
  234. }
  235. END_TEST
  236. static int
  237. alloc_printf(char **buf, size_t size, const char *fmt, ...)
  238. {
  239. /* Test helper function - adapted from unit_test.c */
  240. /* Copyright (c) 2013-2015 the Civetweb developers */
  241. /* Copyright (c) 2004-2013 Sergey Lyubka */
  242. va_list ap;
  243. int ret = 0;
  244. va_start(ap, fmt);
  245. ret = alloc_vprintf(buf, *buf, size, fmt, ap);
  246. va_end(ap);
  247. return ret;
  248. }
  249. START_TEST(test_alloc_vprintf)
  250. {
  251. /* Adapted from unit_test.c */
  252. /* Copyright (c) 2013-2015 the Civetweb developers */
  253. /* Copyright (c) 2004-2013 Sergey Lyubka */
  254. char buf[MG_BUF_LEN], *p = buf;
  255. ck_assert(alloc_printf(&p, sizeof(buf), "%s", "hi") == 2);
  256. ck_assert(p == buf);
  257. ck_assert(alloc_printf(&p, sizeof(buf), "%s", "") == 0);
  258. ck_assert(alloc_printf(&p, sizeof(buf), "") == 0);
  259. /* Pass small buffer, make sure alloc_printf allocates */
  260. ck_assert(alloc_printf(&p, 1, "%s", "hello") == 5);
  261. ck_assert(p != buf);
  262. mg_free(p);
  263. }
  264. END_TEST
  265. START_TEST(test_mg_strcasestr)
  266. {
  267. /* Adapted from unit_test.c */
  268. /* Copyright (c) 2013-2015 the Civetweb developers */
  269. /* Copyright (c) 2004-2013 Sergey Lyubka */
  270. static const char *big1 = "abcdef";
  271. ck_assert(mg_strcasestr("Y", "X") == NULL);
  272. ck_assert(mg_strcasestr("Y", "y") != NULL);
  273. ck_assert(mg_strcasestr(big1, "X") == NULL);
  274. ck_assert(mg_strcasestr(big1, "CD") == big1 + 2);
  275. ck_assert(mg_strcasestr("aa", "AAB") == NULL);
  276. }
  277. END_TEST
  278. START_TEST(test_parse_port_string)
  279. {
  280. /* Adapted from unit_test.c */
  281. /* Copyright (c) 2013-2015 the Civetweb developers */
  282. /* Copyright (c) 2004-2013 Sergey Lyubka */
  283. static const char *valid[] =
  284. { "0",
  285. "1",
  286. "1s",
  287. "1r",
  288. "1.2.3.4:1",
  289. "1.2.3.4:1s",
  290. "1.2.3.4:1r",
  291. #if defined(USE_IPV6)
  292. "[::1]:123",
  293. "[::]:80",
  294. "[3ffe:2a00:100:7031::1]:900",
  295. #endif
  296. NULL };
  297. static const char *invalid[] = {
  298. "99999", "1k", "1.2.3", "1.2.3.4:", "1.2.3.4:2p", NULL};
  299. struct socket so;
  300. struct vec vec;
  301. int i;
  302. for (i = 0; valid[i] != NULL; i++) {
  303. vec.ptr = valid[i];
  304. vec.len = strlen(vec.ptr);
  305. ck_assert(parse_port_string(&vec, &so) != 0);
  306. }
  307. for (i = 0; invalid[i] != NULL; i++) {
  308. vec.ptr = invalid[i];
  309. vec.len = strlen(vec.ptr);
  310. ck_assert(parse_port_string(&vec, &so) == 0);
  311. }
  312. }
  313. END_TEST
  314. START_TEST(test_encode_decode)
  315. {
  316. char buf[128];
  317. const char *alpha = "abcdefghijklmnopqrstuvwxyz";
  318. const char *nonalpha = " !\"#$%&'()*+,-./0123456789:;<=>?@";
  319. const char *nonalpha_url_enc1 =
  320. "%20%21%22%23$%25%26%27()%2a%2b,-.%2f0123456789%3a;%3c%3d%3e%3f%40";
  321. const char *nonalpha_url_enc2 =
  322. "%20!%22%23%24%25%26'()*%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40";
  323. int ret;
  324. size_t len;
  325. #if defined(USE_WEBSOCKET) || defined(USE_LUA)
  326. const char *alpha_b64_enc = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXo=";
  327. const char *nonalpha_b64_enc =
  328. "ICEiIyQlJicoKSorLC0uLzAxMjM0NTY3ODk6Ozw9Pj9A";
  329. memset(buf, 77, sizeof(buf));
  330. base64_encode((unsigned char *)"a", 1, buf);
  331. ck_assert_str_eq(buf, "YQ==");
  332. memset(buf, 77, sizeof(buf));
  333. base64_encode((unsigned char *)"ab", 1, buf);
  334. ck_assert_str_eq(buf, "YQ==");
  335. memset(buf, 77, sizeof(buf));
  336. base64_encode((unsigned char *)"ab", 2, buf);
  337. ck_assert_str_eq(buf, "YWI=");
  338. memset(buf, 77, sizeof(buf));
  339. base64_encode((unsigned char *)alpha, 3, buf);
  340. ck_assert_str_eq(buf, "YWJj");
  341. memset(buf, 77, sizeof(buf));
  342. base64_encode((unsigned char *)alpha, 4, buf);
  343. ck_assert_str_eq(buf, "YWJjZA==");
  344. memset(buf, 77, sizeof(buf));
  345. base64_encode((unsigned char *)alpha, 5, buf);
  346. ck_assert_str_eq(buf, "YWJjZGU=");
  347. memset(buf, 77, sizeof(buf));
  348. base64_encode((unsigned char *)alpha, 6, buf);
  349. ck_assert_str_eq(buf, "YWJjZGVm");
  350. memset(buf, 77, sizeof(buf));
  351. base64_encode((unsigned char *)alpha, (int)strlen(alpha), buf);
  352. ck_assert_str_eq(buf, alpha_b64_enc);
  353. memset(buf, 77, sizeof(buf));
  354. base64_encode((unsigned char *)nonalpha, (int)strlen(nonalpha), buf);
  355. ck_assert_str_eq(buf, nonalpha_b64_enc);
  356. #endif
  357. #if defined(USE_LUA)
  358. memset(buf, 77, sizeof(buf));
  359. len = 9999;
  360. ret = base64_decode((unsigned char *)alpha_b64_enc,
  361. (int)strlen(alpha_b64_enc),
  362. buf,
  363. &len);
  364. ck_assert_int_eq(ret, -1);
  365. ck_assert_uint_eq((unsigned int)len, (unsigned int)strlen(alpha));
  366. ck_assert_str_eq(buf, alpha);
  367. memset(buf, 77, sizeof(buf));
  368. len = 9999;
  369. ret = base64_decode((unsigned char *)"AAA*AAA", 7, buf, &len);
  370. ck_assert_int_eq(ret, 3);
  371. #endif
  372. memset(buf, 77, sizeof(buf));
  373. ret = mg_url_encode(alpha, buf, sizeof(buf));
  374. ck_assert_int_eq(ret, (int)strlen(buf));
  375. ck_assert_int_eq(ret, (int)strlen(alpha));
  376. ck_assert_str_eq(buf, alpha);
  377. memset(buf, 77, sizeof(buf));
  378. ret = mg_url_encode(nonalpha, buf, sizeof(buf));
  379. ck_assert_int_eq(ret, (int)strlen(buf));
  380. ck_assert_int_eq(ret, (int)strlen(nonalpha_url_enc1));
  381. ck_assert_str_eq(buf, nonalpha_url_enc1);
  382. memset(buf, 77, sizeof(buf));
  383. ret = mg_url_decode(alpha, (int)strlen(alpha), buf, sizeof(buf), 0);
  384. ck_assert_int_eq(ret, (int)strlen(buf));
  385. ck_assert_int_eq(ret, (int)strlen(alpha));
  386. ck_assert_str_eq(buf, alpha);
  387. memset(buf, 77, sizeof(buf));
  388. ret = mg_url_decode(
  389. nonalpha_url_enc1, (int)strlen(nonalpha_url_enc1), buf, sizeof(buf), 0);
  390. ck_assert_int_eq(ret, (int)strlen(buf));
  391. ck_assert_int_eq(ret, (int)strlen(nonalpha));
  392. ck_assert_str_eq(buf, nonalpha);
  393. memset(buf, 77, sizeof(buf));
  394. ret = mg_url_decode(
  395. nonalpha_url_enc2, (int)strlen(nonalpha_url_enc2), buf, sizeof(buf), 0);
  396. ck_assert_int_eq(ret, (int)strlen(buf));
  397. ck_assert_int_eq(ret, (int)strlen(nonalpha));
  398. ck_assert_str_eq(buf, nonalpha);
  399. /* len could be unused, if base64_decode is not tested because USE_LUA is
  400. * not defined */
  401. (void)len;
  402. }
  403. END_TEST
  404. START_TEST(test_mask_data)
  405. {
  406. #if defined(USE_WEBSOCKET)
  407. char in[1024];
  408. char out[1024];
  409. int i;
  410. #endif
  411. uint32_t mask = 0x61626364;
  412. /* TODO: adapt test for big endian */
  413. ck_assert((*(unsigned char *)&mask) == 0x64u);
  414. #if defined(USE_WEBSOCKET)
  415. memset(in, 0, sizeof(in));
  416. memset(out, 99, sizeof(out));
  417. mask_data(in, sizeof(out), 0, out);
  418. ck_assert(!memcmp(out, in, sizeof(out)));
  419. for (i = 0; i < 1024; i++) {
  420. in[i] = (char)((unsigned char)i);
  421. }
  422. mask_data(in, 107, 0, out);
  423. ck_assert(!memcmp(out, in, 107));
  424. mask_data(in, 256, 0x01010101, out);
  425. for (i = 0; i < 256; i++) {
  426. ck_assert_int_eq((int)((unsigned char)out[i]),
  427. (int)(((unsigned char)in[i]) ^ (char)1u));
  428. }
  429. for (i = 256; i < (int)sizeof(out); i++) {
  430. ck_assert_int_eq((int)((unsigned char)out[i]), (int)0);
  431. }
  432. /* TODO: check this for big endian */
  433. mask_data(in, 5, 0x01020304, out);
  434. ck_assert_uint_eq((unsigned char)out[0], 0u ^ 4u);
  435. ck_assert_uint_eq((unsigned char)out[1], 1u ^ 3u);
  436. ck_assert_uint_eq((unsigned char)out[2], 2u ^ 2u);
  437. ck_assert_uint_eq((unsigned char)out[3], 3u ^ 1u);
  438. ck_assert_uint_eq((unsigned char)out[4], 4u ^ 4u);
  439. #endif
  440. }
  441. END_TEST
  442. Suite *
  443. make_private_suite(void)
  444. {
  445. Suite *const suite = suite_create("Private");
  446. TCase *const tcase_http_message = tcase_create("HTTP Message");
  447. TCase *const tcase_url_parsing = tcase_create("URL Parsing");
  448. TCase *const tcase_internal_parse = tcase_create("Internal Parsing");
  449. TCase *const tcase_encode_decode = tcase_create("Encode Decode");
  450. TCase *const tcase_mask_data = tcase_create("Mask Data");
  451. tcase_add_test(tcase_http_message, test_parse_http_message);
  452. tcase_add_test(tcase_http_message, test_should_keep_alive);
  453. tcase_set_timeout(tcase_http_message, civetweb_min_test_timeout);
  454. suite_add_tcase(suite, tcase_http_message);
  455. tcase_add_test(tcase_url_parsing, test_match_prefix);
  456. tcase_add_test(tcase_url_parsing,
  457. test_remove_double_dots_and_double_slashes);
  458. tcase_add_test(tcase_url_parsing, test_is_valid_uri);
  459. tcase_set_timeout(tcase_url_parsing, civetweb_min_test_timeout);
  460. suite_add_tcase(suite, tcase_url_parsing);
  461. tcase_add_test(tcase_internal_parse, test_next_option);
  462. tcase_add_test(tcase_internal_parse, test_skip_quoted);
  463. tcase_add_test(tcase_internal_parse, test_mg_strcasestr);
  464. tcase_add_test(tcase_internal_parse, test_alloc_vprintf);
  465. tcase_add_test(tcase_internal_parse, test_parse_port_string);
  466. tcase_set_timeout(tcase_internal_parse, civetweb_min_test_timeout);
  467. suite_add_tcase(suite, tcase_internal_parse);
  468. tcase_add_test(tcase_encode_decode, test_encode_decode);
  469. tcase_set_timeout(tcase_encode_decode, civetweb_min_test_timeout);
  470. suite_add_tcase(suite, tcase_encode_decode);
  471. tcase_add_test(tcase_mask_data, test_mask_data);
  472. tcase_set_timeout(tcase_mask_data, civetweb_min_test_timeout);
  473. suite_add_tcase(suite, tcase_mask_data);
  474. return suite;
  475. }