unit_test.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. // Copyright (c) 2004-2013 Sergey Lyubka
  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. // Unit test for the civetweb web server. Tests embedded API.
  22. #define USE_WEBSOCKET
  23. #define USE_LUA
  24. #ifndef _WIN32
  25. #define __cdecl
  26. #define USE_IPV6
  27. #endif
  28. // USE_* definitions must be made before #include "civetweb.c" !
  29. #include "civetweb.c"
  30. static int s_total_tests = 0;
  31. static int s_failed_tests = 0;
  32. #define FAIL(str, line) do { \
  33. printf("Fail on line %d: [%s]\n", line, str); \
  34. s_failed_tests++; \
  35. } while (0)
  36. #define ASSERT(expr) do { \
  37. s_total_tests++; \
  38. if (!(expr)) FAIL(#expr, __LINE__); \
  39. } while (0)
  40. #define HTTP_PORT "56789"
  41. #define HTTPS_PORT "56790"
  42. #define HTTP_PORT2 "56791"
  43. #define LISTENING_ADDR \
  44. "127.0.0.1:" HTTP_PORT "r" \
  45. ",127.0.0.1:" HTTPS_PORT "s" \
  46. ",127.0.0.1:" HTTP_PORT2
  47. static void test_parse_http_message() {
  48. struct mg_request_info ri;
  49. char req1[] = "GET / HTTP/1.1\r\n\r\n";
  50. char req2[] = "BLAH / HTTP/1.1\r\n\r\n";
  51. char req3[] = "GET / HTTP/1.1\r\nBah\r\n";
  52. char req4[] = "GET / HTTP/1.1\r\nA: foo bar\r\nB: bar\r\nbaz\r\n\r\n";
  53. char req5[] = "GET / HTTP/1.1\r\n\r\n";
  54. char req6[] = "G";
  55. char req7[] = " blah ";
  56. char req8[] = " HTTP/1.1 200 OK \n\n";
  57. char req9[] = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n";
  58. ASSERT(parse_http_message(req9, sizeof(req9), &ri) == sizeof(req9) - 1);
  59. ASSERT(ri.num_headers == 1);
  60. ASSERT(parse_http_message(req1, sizeof(req1), &ri) == sizeof(req1) - 1);
  61. ASSERT(strcmp(ri.http_version, "1.1") == 0);
  62. ASSERT(ri.num_headers == 0);
  63. ASSERT(parse_http_message(req2, sizeof(req2), &ri) == -1);
  64. ASSERT(parse_http_message(req3, sizeof(req3), &ri) == 0);
  65. ASSERT(parse_http_message(req6, sizeof(req6), &ri) == 0);
  66. ASSERT(parse_http_message(req7, sizeof(req7), &ri) == 0);
  67. ASSERT(parse_http_message("", 0, &ri) == 0);
  68. ASSERT(parse_http_message(req8, sizeof(req8), &ri) == sizeof(req8) - 1);
  69. // TODO(lsm): Fix this. Header value may span multiple lines.
  70. ASSERT(parse_http_message(req4, sizeof(req4), &ri) == sizeof(req4) - 1);
  71. ASSERT(strcmp(ri.http_version, "1.1") == 0);
  72. ASSERT(ri.num_headers == 3);
  73. ASSERT(strcmp(ri.http_headers[0].name, "A") == 0);
  74. ASSERT(strcmp(ri.http_headers[0].value, "foo bar") == 0);
  75. ASSERT(strcmp(ri.http_headers[1].name, "B") == 0);
  76. ASSERT(strcmp(ri.http_headers[1].value, "bar") == 0);
  77. ASSERT(strcmp(ri.http_headers[2].name, "baz\r\n\r") == 0);
  78. ASSERT(strcmp(ri.http_headers[2].value, "") == 0);
  79. ASSERT(parse_http_message(req5, sizeof(req5), &ri) == sizeof(req5) - 1);
  80. ASSERT(strcmp(ri.request_method, "GET") == 0);
  81. ASSERT(strcmp(ri.http_version, "1.1") == 0);
  82. }
  83. static void test_should_keep_alive(void) {
  84. struct mg_connection conn;
  85. struct mg_context ctx;
  86. char req1[] = "GET / HTTP/1.1\r\n\r\n";
  87. char req2[] = "GET / HTTP/1.0\r\n\r\n";
  88. char req3[] = "GET / HTTP/1.1\r\nConnection: close\r\n\r\n";
  89. char req4[] = "GET / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";
  90. memset(&conn, 0, sizeof(conn));
  91. conn.ctx = &ctx;
  92. ASSERT(parse_http_message(req1, sizeof(req1), &conn.request_info) ==
  93. sizeof(req1) - 1);
  94. ctx.config[ENABLE_KEEP_ALIVE] = "no";
  95. ASSERT(should_keep_alive(&conn) == 0);
  96. ctx.config[ENABLE_KEEP_ALIVE] = "yes";
  97. ASSERT(should_keep_alive(&conn) == 1);
  98. conn.must_close = 1;
  99. ASSERT(should_keep_alive(&conn) == 0);
  100. conn.must_close = 0;
  101. parse_http_message(req2, sizeof(req2), &conn.request_info);
  102. ASSERT(should_keep_alive(&conn) == 0);
  103. parse_http_message(req3, sizeof(req3), &conn.request_info);
  104. ASSERT(should_keep_alive(&conn) == 0);
  105. parse_http_message(req4, sizeof(req4), &conn.request_info);
  106. ASSERT(should_keep_alive(&conn) == 1);
  107. conn.status_code = 401;
  108. ASSERT(should_keep_alive(&conn) == 0);
  109. conn.status_code = 200;
  110. conn.must_close = 1;
  111. ASSERT(should_keep_alive(&conn) == 0);
  112. }
  113. static void test_match_prefix(void) {
  114. ASSERT(match_prefix("/api", 4, "/api") == 4);
  115. ASSERT(match_prefix("/a/", 3, "/a/b/c") == 3);
  116. ASSERT(match_prefix("/a/", 3, "/ab/c") == -1);
  117. ASSERT(match_prefix("/*/", 3, "/ab/c") == 4);
  118. ASSERT(match_prefix("**", 2, "/a/b/c") == 6);
  119. ASSERT(match_prefix("/*", 2, "/a/b/c") == 2);
  120. ASSERT(match_prefix("*/*", 3, "/a/b/c") == 2);
  121. ASSERT(match_prefix("**/", 3, "/a/b/c") == 5);
  122. ASSERT(match_prefix("**.foo|**.bar", 13, "a.bar") == 5);
  123. ASSERT(match_prefix("a|b|cd", 6, "cdef") == 2);
  124. ASSERT(match_prefix("a|b|c?", 6, "cdef") == 2);
  125. ASSERT(match_prefix("a|?|cd", 6, "cdef") == 1);
  126. ASSERT(match_prefix("/a/**.cgi", 9, "/foo/bar/x.cgi") == -1);
  127. ASSERT(match_prefix("/a/**.cgi", 9, "/a/bar/x.cgi") == 12);
  128. ASSERT(match_prefix("**/", 3, "/a/b/c") == 5);
  129. ASSERT(match_prefix("**/$", 4, "/a/b/c") == -1);
  130. ASSERT(match_prefix("**/$", 4, "/a/b/") == 5);
  131. ASSERT(match_prefix("$", 1, "") == 0);
  132. ASSERT(match_prefix("$", 1, "x") == -1);
  133. ASSERT(match_prefix("*$", 2, "x") == 1);
  134. ASSERT(match_prefix("/$", 2, "/") == 1);
  135. ASSERT(match_prefix("**/$", 4, "/a/b/c") == -1);
  136. ASSERT(match_prefix("**/$", 4, "/a/b/") == 5);
  137. ASSERT(match_prefix("*", 1, "/hello/") == 0);
  138. ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.b/") == -1);
  139. ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.b") == 6);
  140. ASSERT(match_prefix("**.a$|**.b$", 11, "/a/B.A") == 6);
  141. ASSERT(match_prefix("**o$", 4, "HELLO") == 5);
  142. }
  143. static void test_remove_double_dots() {
  144. struct { char before[20], after[20]; } data[] = {
  145. {"////a", "/a"},
  146. {"/.....", "/."},
  147. {"/......", "/"},
  148. {"...", "..."},
  149. {"/...///", "/./"},
  150. {"/a...///", "/a.../"},
  151. {"/.x", "/.x"},
  152. {"/\\", "/"},
  153. {"/a\\", "/a\\"},
  154. {"/a\\\\...", "/a\\."},
  155. };
  156. size_t i;
  157. for (i = 0; i < ARRAY_SIZE(data); i++) {
  158. remove_double_dots_and_double_slashes(data[i].before);
  159. ASSERT(strcmp(data[i].before, data[i].after) == 0);
  160. }
  161. }
  162. static char *read_file(const char *path, int *size) {
  163. FILE *fp;
  164. struct stat st;
  165. char *data = NULL;
  166. if ((fp = fopen(path, "rb")) != NULL && !fstat(fileno(fp), &st)) {
  167. *size = (int) st.st_size;
  168. ASSERT((data = malloc(*size)) != NULL);
  169. ASSERT(fread(data, 1, *size, fp) == (size_t) *size);
  170. fclose(fp);
  171. }
  172. return data;
  173. }
  174. static const char *fetch_data = "hello world!\n";
  175. static const char *inmemory_file_data = "hi there";
  176. static const char *upload_filename = "upload_test.txt";
  177. static const char *upload_filename2 = "upload_test2.txt";
  178. static const char *upload_ok_message = "upload successful";
  179. static const char *open_file_cb(const struct mg_connection *conn,
  180. const char *path, size_t *size) {
  181. (void) conn;
  182. if (!strcmp(path, "./blah")) {
  183. *size = strlen(inmemory_file_data);
  184. return inmemory_file_data;
  185. }
  186. return NULL;
  187. }
  188. static void upload_cb(struct mg_connection *conn, const char *path) {
  189. const struct mg_request_info *ri = mg_get_request_info(conn);
  190. char *p1, *p2;
  191. int len1, len2;
  192. if (atoi(ri->query_string) == 1) {
  193. ASSERT(!strcmp(path, "./upload_test.txt"));
  194. ASSERT((p1 = read_file("civetweb.c", &len1)) != NULL);
  195. ASSERT((p2 = read_file(path, &len2)) != NULL);
  196. ASSERT(len1 == len2);
  197. ASSERT(memcmp(p1, p2, len1) == 0);
  198. free(p1), free(p2);
  199. remove(upload_filename);
  200. } else if (atoi(ri->query_string) == 2) {
  201. if (!strcmp(path, "./upload_test.txt")) {
  202. ASSERT((p1 = read_file("civetweb.h", &len1)) != NULL);
  203. ASSERT((p2 = read_file(path, &len2)) != NULL);
  204. ASSERT(len1 == len2);
  205. ASSERT(memcmp(p1, p2, len1) == 0);
  206. free(p1), free(p2);
  207. remove(upload_filename);
  208. } else if (!strcmp(path, "./upload_test2.txt")) {
  209. ASSERT((p1 = read_file("README.md", &len1)) != NULL);
  210. ASSERT((p2 = read_file(path, &len2)) != NULL);
  211. ASSERT(len1 == len2);
  212. ASSERT(memcmp(p1, p2, len1) == 0);
  213. free(p1), free(p2);
  214. remove(upload_filename);
  215. } else {
  216. ASSERT(0);
  217. }
  218. } else {
  219. ASSERT(0);
  220. }
  221. mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Length: %d\r\n\r\n%s",
  222. (int) strlen(upload_ok_message), upload_ok_message);
  223. }
  224. static int begin_request_handler_cb(struct mg_connection *conn) {
  225. const struct mg_request_info *ri = mg_get_request_info(conn);
  226. if (!strcmp(ri->uri, "/data")) {
  227. mg_printf(conn, "HTTP/1.1 200 OK\r\n"
  228. "Content-Type: text/plain\r\n\r\n"
  229. "%s", fetch_data);
  230. close_connection(conn);
  231. return 1;
  232. }
  233. if (!strcmp(ri->uri, "/upload")) {
  234. ASSERT(ri->query_string != NULL);
  235. ASSERT(mg_upload(conn, ".") == atoi(ri->query_string));
  236. }
  237. return 0;
  238. }
  239. static int log_message_cb(const struct mg_connection *conn, const char *msg) {
  240. (void) conn;
  241. printf("%s\n", msg);
  242. return 0;
  243. }
  244. static const struct mg_callbacks CALLBACKS = {
  245. &begin_request_handler_cb, NULL, &log_message_cb, NULL, NULL, NULL, NULL,
  246. &open_file_cb, NULL, &upload_cb, NULL
  247. };
  248. static const char *OPTIONS[] = {
  249. "document_root", ".",
  250. "listening_ports", LISTENING_ADDR,
  251. "enable_keep_alive", "yes",
  252. "ssl_certificate", "build/ssl_cert.pem",
  253. NULL,
  254. };
  255. static char *read_conn(struct mg_connection *conn, int *size) {
  256. char buf[100], *data = NULL;
  257. int len;
  258. *size = 0;
  259. while ((len = mg_read(conn, buf, sizeof(buf))) > 0) {
  260. *size += len;
  261. ASSERT((data = realloc(data, *size)) != NULL);
  262. memcpy(data + *size - len, buf, len);
  263. }
  264. return data;
  265. }
  266. static void test_mg_download(void) {
  267. char *p1, *p2, ebuf[100];
  268. int len1, len2, port = atoi(HTTPS_PORT);
  269. struct mg_connection *conn;
  270. struct mg_context *ctx;
  271. ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL);
  272. ASSERT(mg_download(NULL, port, 0, ebuf, sizeof(ebuf), "%s", "") == NULL);
  273. ASSERT(mg_download("localhost", 0, 0, ebuf, sizeof(ebuf), "%s", "") == NULL);
  274. ASSERT(mg_download("localhost", port, 1, ebuf, sizeof(ebuf),
  275. "%s", "") == NULL);
  276. // Fetch nonexistent file, should see 404
  277. ASSERT((conn = mg_download("localhost", port, 1, ebuf, sizeof(ebuf), "%s",
  278. "GET /gimbec HTTP/1.0\r\n\r\n")) != NULL);
  279. ASSERT(strcmp(conn->request_info.uri, "404") == 0);
  280. mg_close_connection(conn);
  281. ASSERT((conn = mg_download("google.com", 443, 1, ebuf, sizeof(ebuf), "%s",
  282. "GET / HTTP/1.0\r\n\r\n")) != NULL);
  283. mg_close_connection(conn);
  284. // Fetch civetweb.c, should succeed
  285. ASSERT((conn = mg_download("localhost", port, 1, ebuf, sizeof(ebuf), "%s",
  286. "GET /civetweb.c HTTP/1.0\r\n\r\n")) != NULL);
  287. ASSERT(!strcmp(conn->request_info.uri, "200"));
  288. ASSERT((p1 = read_conn(conn, &len1)) != NULL);
  289. ASSERT((p2 = read_file("civetweb.c", &len2)) != NULL);
  290. ASSERT(len1 == len2);
  291. ASSERT(memcmp(p1, p2, len1) == 0);
  292. free(p1), free(p2);
  293. mg_close_connection(conn);
  294. // Fetch in-memory file, should succeed.
  295. ASSERT((conn = mg_download("localhost", port, 1, ebuf, sizeof(ebuf), "%s",
  296. "GET /blah HTTP/1.1\r\n\r\n")) != NULL);
  297. ASSERT((p1 = read_conn(conn, &len1)) != NULL);
  298. ASSERT(len1 == (int) strlen(inmemory_file_data));
  299. ASSERT(memcmp(p1, inmemory_file_data, len1) == 0);
  300. free(p1);
  301. mg_close_connection(conn);
  302. // Fetch in-memory data with no Content-Length, should succeed.
  303. ASSERT((conn = mg_download("localhost", port, 1, ebuf, sizeof(ebuf), "%s",
  304. "GET /data HTTP/1.1\r\n\r\n")) != NULL);
  305. ASSERT((p1 = read_conn(conn, &len1)) != NULL);
  306. ASSERT(len1 == (int) strlen(fetch_data));
  307. ASSERT(memcmp(p1, fetch_data, len1) == 0);
  308. free(p1);
  309. mg_close_connection(conn);
  310. // Test SSL redirect, IP address
  311. ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0,
  312. ebuf, sizeof(ebuf), "%s",
  313. "GET /foo HTTP/1.1\r\n\r\n")) != NULL);
  314. ASSERT(strcmp(conn->request_info.uri, "302") == 0);
  315. ASSERT(strcmp(mg_get_header(conn, "Location"),
  316. "https://127.0.0.1:" HTTPS_PORT "/foo") == 0);
  317. mg_close_connection(conn);
  318. // Test SSL redirect, Host:
  319. ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0,
  320. ebuf, sizeof(ebuf), "%s",
  321. "GET /foo HTTP/1.1\r\nHost: a.b:77\n\n")) != NULL);
  322. ASSERT(strcmp(conn->request_info.uri, "302") == 0);
  323. ASSERT(strcmp(mg_get_header(conn, "Location"),
  324. "https://a.b:" HTTPS_PORT "/foo") == 0);
  325. mg_close_connection(conn);
  326. mg_stop(ctx);
  327. }
  328. static int alloc_printf(char **buf, size_t size, char *fmt, ...) {
  329. va_list ap;
  330. va_start(ap, fmt);
  331. return alloc_vprintf(buf, size, fmt, ap);
  332. }
  333. static void test_mg_upload(void) {
  334. static const char *boundary = "OOO___MY_BOUNDARY___OOO";
  335. struct mg_context *ctx;
  336. struct mg_connection *conn;
  337. char ebuf[100], buf[20], *file_data, *file2_data, *post_data;
  338. int file_len, file2_len, post_data_len;
  339. ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL);
  340. // Upload one file
  341. ASSERT((file_data = read_file("civetweb.c", &file_len)) != NULL);
  342. post_data = NULL;
  343. post_data_len = alloc_printf(&post_data, 0,
  344. "--%s\r\n"
  345. "Content-Disposition: form-data; "
  346. "name=\"file\"; "
  347. "filename=\"%s\"\r\n\r\n"
  348. "%.*s\r\n"
  349. "--%s--\r\n",
  350. boundary, upload_filename,
  351. file_len, file_data, boundary);
  352. ASSERT(post_data_len > 0);
  353. ASSERT((conn = mg_download("localhost", atoi(HTTPS_PORT), 1,
  354. ebuf, sizeof(ebuf),
  355. "POST /upload?1 HTTP/1.1\r\n"
  356. "Content-Length: %d\r\n"
  357. "Content-Type: multipart/form-data; "
  358. "boundary=%s\r\n\r\n"
  359. "%.*s", post_data_len, boundary,
  360. post_data_len, post_data)) != NULL);
  361. free(file_data), free(post_data);
  362. ASSERT(mg_read(conn, buf, sizeof(buf)) == (int) strlen(upload_ok_message));
  363. ASSERT(memcmp(buf, upload_ok_message, strlen(upload_ok_message)) == 0);
  364. mg_close_connection(conn);
  365. // Upload two files
  366. ASSERT((file_data = read_file("civetweb.h", &file_len)) != NULL);
  367. ASSERT((file2_data = read_file("README.md", &file2_len)) != NULL);
  368. post_data = NULL;
  369. post_data_len = alloc_printf(&post_data, 0,
  370. // First file
  371. "--%s\r\n"
  372. "Content-Disposition: form-data; "
  373. "name=\"file\"; "
  374. "filename=\"%s\"\r\n\r\n"
  375. "%.*s\r\n"
  376. // Second file
  377. "--%s\r\n"
  378. "Content-Disposition: form-data; "
  379. "name=\"file\"; "
  380. "filename=\"%s\"\r\n\r\n"
  381. "%.*s\r\n"
  382. // Final boundary
  383. "--%s--\r\n",
  384. boundary, upload_filename,
  385. file_len, file_data,
  386. boundary, upload_filename2,
  387. file2_len, file2_data,
  388. boundary);
  389. ASSERT(post_data_len > 0);
  390. ASSERT((conn = mg_download("localhost", atoi(HTTPS_PORT), 1,
  391. ebuf, sizeof(ebuf),
  392. "POST /upload?2 HTTP/1.1\r\n"
  393. "Content-Length: %d\r\n"
  394. "Content-Type: multipart/form-data; "
  395. "boundary=%s\r\n\r\n"
  396. "%.*s", post_data_len, boundary,
  397. post_data_len, post_data)) != NULL);
  398. free(file_data), free(file2_data), free(post_data);
  399. ASSERT(mg_read(conn, buf, sizeof(buf)) == (int) strlen(upload_ok_message));
  400. ASSERT(memcmp(buf, upload_ok_message, strlen(upload_ok_message)) == 0);
  401. mg_close_connection(conn);
  402. mg_stop(ctx);
  403. }
  404. static void test_base64_encode(void) {
  405. const char *in[] = {"a", "ab", "abc", "abcd", NULL};
  406. const char *out[] = {"YQ==", "YWI=", "YWJj", "YWJjZA=="};
  407. char buf[100];
  408. int i;
  409. for (i = 0; in[i] != NULL; i++) {
  410. base64_encode((unsigned char *) in[i], strlen(in[i]), buf);
  411. ASSERT(!strcmp(buf, out[i]));
  412. }
  413. }
  414. static void test_mg_get_var(void) {
  415. static const char *post[] = {
  416. "a=1&&b=2&d&=&c=3%20&e=",
  417. "q=&st=2012%2F11%2F13+17%3A05&et=&team_id=",
  418. NULL
  419. };
  420. char buf[20];
  421. ASSERT(mg_get_var(post[0], strlen(post[0]), "a", buf, sizeof(buf)) == 1);
  422. ASSERT(buf[0] == '1' && buf[1] == '\0');
  423. ASSERT(mg_get_var(post[0], strlen(post[0]), "b", buf, sizeof(buf)) == 1);
  424. ASSERT(buf[0] == '2' && buf[1] == '\0');
  425. ASSERT(mg_get_var(post[0], strlen(post[0]), "c", buf, sizeof(buf)) == 2);
  426. ASSERT(buf[0] == '3' && buf[1] == ' ' && buf[2] == '\0');
  427. ASSERT(mg_get_var(post[0], strlen(post[0]), "e", buf, sizeof(buf)) == 0);
  428. ASSERT(buf[0] == '\0');
  429. ASSERT(mg_get_var(post[0], strlen(post[0]), "d", buf, sizeof(buf)) == -1);
  430. ASSERT(mg_get_var(post[0], strlen(post[0]), "c", buf, 2) == -2);
  431. ASSERT(mg_get_var(post[0], strlen(post[0]), "x", NULL, 10) == -2);
  432. ASSERT(mg_get_var(post[0], strlen(post[0]), "x", buf, 0) == -2);
  433. ASSERT(mg_get_var(post[1], strlen(post[1]), "st", buf, 16) == -2);
  434. ASSERT(mg_get_var(post[1], strlen(post[1]), "st", buf, 17) == 16);
  435. }
  436. static void test_set_throttle(void) {
  437. ASSERT(set_throttle(NULL, 0x0a000001, "/") == 0);
  438. ASSERT(set_throttle("10.0.0.0/8=20", 0x0a000001, "/") == 20);
  439. ASSERT(set_throttle("10.0.0.0/8=0.5k", 0x0a000001, "/") == 512);
  440. ASSERT(set_throttle("10.0.0.0/8=17m", 0x0a000001, "/") == 1048576 * 17);
  441. ASSERT(set_throttle("10.0.0.0/8=1x", 0x0a000001, "/") == 0);
  442. ASSERT(set_throttle("10.0.0.0/8=5,0.0.0.0/0=10", 0x0a000001, "/") == 10);
  443. ASSERT(set_throttle("10.0.0.0/8=5,/foo/**=7", 0x0a000001, "/index") == 5);
  444. ASSERT(set_throttle("10.0.0.0/8=5,/foo/**=7", 0x0a000001, "/foo/x") == 7);
  445. ASSERT(set_throttle("10.0.0.0/8=5,/foo/**=7", 0x0b000001, "/foxo/x") == 0);
  446. ASSERT(set_throttle("10.0.0.0/8=5,*=1", 0x0b000001, "/foxo/x") == 1);
  447. }
  448. static void test_next_option(void) {
  449. const char *p, *list = "x/8,/y**=1;2k,z";
  450. struct vec a, b;
  451. int i;
  452. ASSERT(next_option(NULL, &a, &b) == NULL);
  453. for (i = 0, p = list; (p = next_option(p, &a, &b)) != NULL; i++) {
  454. ASSERT(i != 0 || (a.ptr == list && a.len == 3 && b.len == 0));
  455. ASSERT(i != 1 || (a.ptr == list + 4 && a.len == 4 && b.ptr == list + 9 &&
  456. b.len == 4));
  457. ASSERT(i != 2 || (a.ptr == list + 14 && a.len == 1 && b.len == 0));
  458. }
  459. }
  460. #ifdef USE_LUA
  461. static void check_lua_expr(lua_State *L, const char *expr, const char *value) {
  462. const char *v, *var_name = "myVar";
  463. char buf[100];
  464. snprintf(buf, sizeof(buf), "%s = %s", var_name, expr);
  465. (void) luaL_dostring(L, buf);
  466. lua_getglobal(L, var_name);
  467. v = lua_tostring(L, -1);
  468. ASSERT((value == NULL && v == NULL) ||
  469. (value != NULL && v != NULL && !strcmp(value, v)));
  470. }
  471. static void test_lua(void) {
  472. static struct mg_connection conn;
  473. static struct mg_context ctx;
  474. char http_request[] = "POST /foo/bar HTTP/1.1\r\n"
  475. "Content-Length: 12\r\n"
  476. "Connection: close\r\n\r\nhello world!";
  477. lua_State *L = luaL_newstate();
  478. conn.ctx = &ctx;
  479. conn.buf = http_request;
  480. conn.buf_size = conn.data_len = strlen(http_request);
  481. conn.request_len = parse_http_message(conn.buf, conn.data_len,
  482. &conn.request_info);
  483. conn.content_len = conn.data_len - conn.request_len;
  484. prepare_lua_environment(&conn, L);
  485. ASSERT(lua_gettop(L) == 0);
  486. check_lua_expr(L, "'hi'", "hi");
  487. check_lua_expr(L, "mg.request_info.request_method", "POST");
  488. check_lua_expr(L, "mg.request_info.uri", "/foo/bar");
  489. check_lua_expr(L, "mg.request_info.num_headers", "2");
  490. check_lua_expr(L, "mg.request_info.remote_ip", "0");
  491. check_lua_expr(L, "mg.request_info.http_headers['Content-Length']", "12");
  492. check_lua_expr(L, "mg.request_info.http_headers['Connection']", "close");
  493. (void) luaL_dostring(L, "post = mg.read()");
  494. check_lua_expr(L, "# post", "12");
  495. check_lua_expr(L, "post", "hello world!");
  496. lua_close(L);
  497. }
  498. #endif
  499. static void test_mg_stat(void) {
  500. static struct mg_context ctx;
  501. struct file file = STRUCT_FILE_INITIALIZER;
  502. ASSERT(!mg_stat(fc(&ctx), " does not exist ", &file));
  503. }
  504. static void test_skip_quoted(void) {
  505. char x[] = "a=1, b=2 c='hi \' there'", *s = x, *p;
  506. p = skip_quoted(&s, ", ", ", ", 0);
  507. ASSERT(p != NULL && !strcmp(p, "a=1"));
  508. p = skip_quoted(&s, ", ", ", ", 0);
  509. ASSERT(p != NULL && !strcmp(p, "b=2"));
  510. // TODO(lsm): fix this
  511. #if 0
  512. p = skip_quoted(&s, "'", ", ", '\\');
  513. p = skip_quoted(&s, "'", ", ", '\\');
  514. printf("[%s]\n", p);
  515. ASSERT(p != NULL && !strcmp(p, "hi ' there"));
  516. #endif
  517. }
  518. static void test_alloc_vprintf(void) {
  519. char buf[MG_BUF_LEN], *p = buf;
  520. ASSERT(alloc_printf(&p, sizeof(buf), "%s", "hi") == 2);
  521. ASSERT(p == buf);
  522. ASSERT(alloc_printf(&p, sizeof(buf), "%s", "") == 0);
  523. ASSERT(alloc_printf(&p, sizeof(buf), "") == 0);
  524. // Pass small buffer, make sure alloc_printf allocates
  525. ASSERT(alloc_printf(&p, 1, "%s", "hello") == 5);
  526. ASSERT(p != buf);
  527. free(p);
  528. }
  529. static void test_request_replies(void) {
  530. char ebuf[100];
  531. int i, port = atoi(HTTPS_PORT);
  532. struct mg_connection *conn;
  533. struct mg_context *ctx;
  534. static struct { const char *request, *reply_regex; } tests[] = {
  535. {
  536. "GET test/hello.txt HTTP/1.0\r\nRange: bytes=3-5\r\n\r\n",
  537. "^HTTP/1.1 206 Partial Content"
  538. },
  539. {NULL, NULL},
  540. };
  541. ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL);
  542. for (i = 0; tests[i].request != NULL; i++) {
  543. ASSERT((conn = mg_download("localhost", port, 1, ebuf, sizeof(ebuf), "%s",
  544. tests[i].request)) != NULL);
  545. mg_close_connection(conn);
  546. }
  547. mg_stop(ctx);
  548. }
  549. static int api_callback(struct mg_connection *conn) {
  550. struct mg_request_info *ri = mg_get_request_info(conn);
  551. char post_data[100] = "";
  552. ASSERT(ri->user_data == (void *) 123);
  553. ASSERT(ri->num_headers == 2);
  554. ASSERT(strcmp(mg_get_header(conn, "host"), "blah.com") == 0);
  555. ASSERT(mg_read(conn, post_data, sizeof(post_data)) == 3);
  556. ASSERT(memcmp(post_data, "b=1", 3) == 0);
  557. ASSERT(ri->query_string != NULL);
  558. ASSERT(ri->remote_ip > 0);
  559. ASSERT(ri->remote_port > 0);
  560. ASSERT(strcmp(ri->http_version, "1.0") == 0);
  561. mg_printf(conn, "HTTP/1.0 200 OK\r\n\r\n");
  562. return 1;
  563. }
  564. static void test_api_calls(void) {
  565. char ebuf[100];
  566. struct mg_callbacks callbacks;
  567. struct mg_connection *conn;
  568. struct mg_context *ctx;
  569. static const char *request = "POST /?a=%20&b=&c=xx HTTP/1.0\r\n"
  570. "Host: blah.com\n" // More spaces before
  571. "content-length: 3\r\n" // Lower case header name
  572. "\r\nb=123456"; // Content size > content-length, test for mg_read()
  573. memset(&callbacks, 0, sizeof(callbacks));
  574. callbacks.begin_request = api_callback;
  575. ASSERT((ctx = mg_start(&callbacks, (void *) 123, OPTIONS)) != NULL);
  576. ASSERT((conn = mg_download("localhost", atoi(HTTPS_PORT), 1,
  577. ebuf, sizeof(ebuf), "%s", request)) != NULL);
  578. mg_close_connection(conn);
  579. mg_stop(ctx);
  580. }
  581. static void test_url_decode(void) {
  582. char buf[100];
  583. ASSERT(mg_url_decode("foo", 3, buf, 3, 0) == -1); // No space for \0
  584. ASSERT(mg_url_decode("foo", 3, buf, 4, 0) == 3);
  585. ASSERT(strcmp(buf, "foo") == 0);
  586. ASSERT(mg_url_decode("a+", 2, buf, sizeof(buf), 0) == 2);
  587. ASSERT(strcmp(buf, "a+") == 0);
  588. ASSERT(mg_url_decode("a+", 2, buf, sizeof(buf), 1) == 2);
  589. ASSERT(strcmp(buf, "a ") == 0);
  590. ASSERT(mg_url_decode("%61", 1, buf, sizeof(buf), 1) == 1);
  591. ASSERT(strcmp(buf, "%") == 0);
  592. ASSERT(mg_url_decode("%61", 2, buf, sizeof(buf), 1) == 2);
  593. ASSERT(strcmp(buf, "%6") == 0);
  594. ASSERT(mg_url_decode("%61", 3, buf, sizeof(buf), 1) == 1);
  595. ASSERT(strcmp(buf, "a") == 0);
  596. }
  597. static void test_mg_strcasestr(void) {
  598. static const char *big1 = "abcdef";
  599. ASSERT(mg_strcasestr("Y", "X") == NULL);
  600. ASSERT(mg_strcasestr("Y", "y") != NULL);
  601. ASSERT(mg_strcasestr(big1, "X") == NULL);
  602. ASSERT(mg_strcasestr(big1, "CD") == big1 + 2);
  603. ASSERT(mg_strcasestr("aa", "AAB") == NULL);
  604. }
  605. static void test_mg_get_cookie(void) {
  606. char buf[20];
  607. ASSERT(mg_get_cookie("", "foo", NULL, sizeof(buf)) == -2);
  608. ASSERT(mg_get_cookie("", "foo", buf, 0) == -2);
  609. ASSERT(mg_get_cookie("", "foo", buf, sizeof(buf)) == -1);
  610. ASSERT(mg_get_cookie("", NULL, buf, sizeof(buf)) == -1);
  611. ASSERT(mg_get_cookie("a=1; b=2; c; d", "a", buf, sizeof(buf)) == 1);
  612. ASSERT(strcmp(buf, "1") == 0);
  613. ASSERT(mg_get_cookie("a=1; b=2; c; d", "b", buf, sizeof(buf)) == 1);
  614. ASSERT(strcmp(buf, "2") == 0);
  615. ASSERT(mg_get_cookie("a=1; b=123", "b", buf, sizeof(buf)) == 3);
  616. ASSERT(strcmp(buf, "123") == 0);
  617. ASSERT(mg_get_cookie("a=1; b=2; c; d", "c", buf, sizeof(buf)) == -1);
  618. }
  619. static void test_strtoll(void) {
  620. ASSERT(strtoll("0", NULL, 10) == 0);
  621. ASSERT(strtoll("123", NULL, 10) == 123);
  622. ASSERT(strtoll("-34", NULL, 10) == -34);
  623. ASSERT(strtoll("3566626116", NULL, 10) == 3566626116);
  624. }
  625. static void test_parse_port_string(void) {
  626. static const char *valid[] = {
  627. "1", "1s", "1r", "1.2.3.4:1", "1.2.3.4:1s", "1.2.3.4:1r",
  628. #if defined(USE_IPV6)
  629. "[::1]:123", "[3ffe:2a00:100:7031::1]:900",
  630. #endif
  631. NULL
  632. };
  633. static const char *invalid[] = {
  634. "0", "99999", "1k", "1.2.3", "1.2.3.4:", "1.2.3.4:2p",
  635. NULL
  636. };
  637. struct socket so;
  638. struct vec vec;
  639. int i;
  640. for (i = 0; valid[i] != NULL; i++) {
  641. vec.ptr = valid[i];
  642. vec.len = strlen(vec.ptr);
  643. ASSERT(parse_port_string(&vec, &so) != 0);
  644. }
  645. for (i = 0; invalid[i] != NULL; i++) {
  646. vec.ptr = invalid[i];
  647. vec.len = strlen(vec.ptr);
  648. ASSERT(parse_port_string(&vec, &so) == 0);
  649. }
  650. }
  651. int __cdecl main(void) {
  652. test_parse_port_string();
  653. test_mg_strcasestr();
  654. test_alloc_vprintf();
  655. test_base64_encode();
  656. test_match_prefix();
  657. test_remove_double_dots();
  658. test_should_keep_alive();
  659. test_parse_http_message();
  660. test_mg_download();
  661. test_mg_get_var();
  662. test_set_throttle();
  663. test_next_option();
  664. test_mg_stat();
  665. test_skip_quoted();
  666. test_mg_upload();
  667. test_request_replies();
  668. test_api_calls();
  669. test_url_decode();
  670. test_mg_get_cookie();
  671. test_strtoll();
  672. #ifdef USE_LUA
  673. test_lua();
  674. #endif
  675. printf("TOTAL TESTS: %d, FAILED: %d\n", s_total_tests, s_failed_tests);
  676. return s_failed_tests == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
  677. }