unit_test.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 mongoose web server. Tests embedded API.
  22. #define USE_WEBSOCKET
  23. #define USE_LUA
  24. #include "mongoose.c"
  25. #define FATAL(str, line) do { \
  26. printf("Fail on line %d: [%s]\n", line, str); \
  27. abort(); \
  28. } while (0)
  29. #define ASSERT(expr) do { if (!(expr)) FATAL(#expr, __LINE__); } while (0)
  30. #define HTTP_PORT "56789"
  31. #define HTTPS_PORT "56790"
  32. #define LISTENING_ADDR "127.0.0.1:" HTTP_PORT "r,127.0.0.1:" HTTPS_PORT "s"
  33. static void test_parse_http_message() {
  34. struct mg_request_info ri;
  35. char req1[] = "GET / HTTP/1.1\r\n\r\n";
  36. char req2[] = "BLAH / HTTP/1.1\r\n\r\n";
  37. char req3[] = "GET / HTTP/1.1\r\nBah\r\n";
  38. char req4[] = "GET / HTTP/1.1\r\nA: foo bar\r\nB: bar\r\nbaz\r\n\r\n";
  39. char req5[] = "GET / HTTP/1.1\r\n\r\n";
  40. char req6[] = "G";
  41. char req7[] = " blah ";
  42. char req8[] = " HTTP/1.1 200 OK \n\n";
  43. ASSERT(parse_http_message(req1, sizeof(req1), &ri) == sizeof(req1) - 1);
  44. ASSERT(strcmp(ri.http_version, "1.1") == 0);
  45. ASSERT(ri.num_headers == 0);
  46. ASSERT(parse_http_message(req2, sizeof(req2), &ri) == -1);
  47. ASSERT(parse_http_message(req3, sizeof(req3), &ri) == 0);
  48. ASSERT(parse_http_message(req6, sizeof(req6), &ri) == 0);
  49. ASSERT(parse_http_message(req7, sizeof(req7), &ri) == 0);
  50. ASSERT(parse_http_message("", 0, &ri) == 0);
  51. ASSERT(parse_http_message(req8, sizeof(req8), &ri) == sizeof(req8) - 1);
  52. // TODO(lsm): Fix this. Header value may span multiple lines.
  53. ASSERT(parse_http_message(req4, sizeof(req4), &ri) == sizeof(req4) - 1);
  54. ASSERT(strcmp(ri.http_version, "1.1") == 0);
  55. ASSERT(ri.num_headers == 3);
  56. ASSERT(strcmp(ri.http_headers[0].name, "A") == 0);
  57. ASSERT(strcmp(ri.http_headers[0].value, "foo bar") == 0);
  58. ASSERT(strcmp(ri.http_headers[1].name, "B") == 0);
  59. ASSERT(strcmp(ri.http_headers[1].value, "bar") == 0);
  60. ASSERT(strcmp(ri.http_headers[2].name, "baz\r\n\r") == 0);
  61. ASSERT(strcmp(ri.http_headers[2].value, "") == 0);
  62. ASSERT(parse_http_message(req5, sizeof(req5), &ri) == sizeof(req5) - 1);
  63. ASSERT(strcmp(ri.request_method, "GET") == 0);
  64. ASSERT(strcmp(ri.http_version, "1.1") == 0);
  65. }
  66. static void test_should_keep_alive(void) {
  67. struct mg_connection conn;
  68. struct mg_context ctx;
  69. char req1[] = "GET / HTTP/1.1\r\n\r\n";
  70. char req2[] = "GET / HTTP/1.0\r\n\r\n";
  71. char req3[] = "GET / HTTP/1.1\r\nConnection: close\r\n\r\n";
  72. char req4[] = "GET / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";
  73. memset(&conn, 0, sizeof(conn));
  74. conn.ctx = &ctx;
  75. ASSERT(parse_http_message(req1, sizeof(req1), &conn.request_info) ==
  76. sizeof(req1) - 1);
  77. ctx.config[ENABLE_KEEP_ALIVE] = "no";
  78. ASSERT(should_keep_alive(&conn) == 0);
  79. ctx.config[ENABLE_KEEP_ALIVE] = "yes";
  80. ASSERT(should_keep_alive(&conn) == 1);
  81. conn.must_close = 1;
  82. ASSERT(should_keep_alive(&conn) == 0);
  83. conn.must_close = 0;
  84. parse_http_message(req2, sizeof(req2), &conn.request_info);
  85. ASSERT(should_keep_alive(&conn) == 0);
  86. parse_http_message(req3, sizeof(req3), &conn.request_info);
  87. ASSERT(should_keep_alive(&conn) == 0);
  88. parse_http_message(req4, sizeof(req4), &conn.request_info);
  89. ASSERT(should_keep_alive(&conn) == 1);
  90. conn.status_code = 401;
  91. ASSERT(should_keep_alive(&conn) == 0);
  92. conn.status_code = 200;
  93. conn.must_close = 1;
  94. ASSERT(should_keep_alive(&conn) == 0);
  95. }
  96. static void test_match_prefix(void) {
  97. ASSERT(match_prefix("/api", 4, "/api") == 4);
  98. ASSERT(match_prefix("/a/", 3, "/a/b/c") == 3);
  99. ASSERT(match_prefix("/a/", 3, "/ab/c") == -1);
  100. ASSERT(match_prefix("/*/", 3, "/ab/c") == 4);
  101. ASSERT(match_prefix("**", 2, "/a/b/c") == 6);
  102. ASSERT(match_prefix("/*", 2, "/a/b/c") == 2);
  103. ASSERT(match_prefix("*/*", 3, "/a/b/c") == 2);
  104. ASSERT(match_prefix("**/", 3, "/a/b/c") == 5);
  105. ASSERT(match_prefix("**.foo|**.bar", 13, "a.bar") == 5);
  106. ASSERT(match_prefix("a|b|cd", 6, "cdef") == 2);
  107. ASSERT(match_prefix("a|b|c?", 6, "cdef") == 2);
  108. ASSERT(match_prefix("a|?|cd", 6, "cdef") == 1);
  109. ASSERT(match_prefix("/a/**.cgi", 9, "/foo/bar/x.cgi") == -1);
  110. ASSERT(match_prefix("/a/**.cgi", 9, "/a/bar/x.cgi") == 12);
  111. ASSERT(match_prefix("**/", 3, "/a/b/c") == 5);
  112. ASSERT(match_prefix("**/$", 4, "/a/b/c") == -1);
  113. ASSERT(match_prefix("**/$", 4, "/a/b/") == 5);
  114. ASSERT(match_prefix("$", 1, "") == 0);
  115. ASSERT(match_prefix("$", 1, "x") == -1);
  116. ASSERT(match_prefix("*$", 2, "x") == 1);
  117. ASSERT(match_prefix("/$", 2, "/") == 1);
  118. ASSERT(match_prefix("**/$", 4, "/a/b/c") == -1);
  119. ASSERT(match_prefix("**/$", 4, "/a/b/") == 5);
  120. ASSERT(match_prefix("*", 1, "/hello/") == 0);
  121. ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.b/") == -1);
  122. ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.b") == 6);
  123. ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.a") == 6);
  124. }
  125. static void test_remove_double_dots() {
  126. struct { char before[20], after[20]; } data[] = {
  127. {"////a", "/a"},
  128. {"/.....", "/."},
  129. {"/......", "/"},
  130. {"...", "..."},
  131. {"/...///", "/./"},
  132. {"/a...///", "/a.../"},
  133. {"/.x", "/.x"},
  134. {"/\\", "/"},
  135. {"/a\\", "/a\\"},
  136. {"/a\\\\...", "/a\\."},
  137. };
  138. size_t i;
  139. for (i = 0; i < ARRAY_SIZE(data); i++) {
  140. #if 0
  141. printf("[%s] -> [%s]\n", data[i].before, data[i].after);
  142. #endif
  143. remove_double_dots_and_double_slashes(data[i].before);
  144. ASSERT(strcmp(data[i].before, data[i].after) == 0);
  145. }
  146. }
  147. static const char *fetch_data = "hello world!\n";
  148. static const char *inmemory_file_data = "hi there";
  149. static void *event_handler(enum mg_event event, struct mg_connection *conn) {
  150. const struct mg_request_info *request_info = mg_get_request_info(conn);
  151. if (event == MG_NEW_REQUEST && !strcmp(request_info->uri, "/data")) {
  152. mg_printf(conn, "HTTP/1.1 200 OK\r\n"
  153. "Content-Length: %d\r\n"
  154. "Content-Type: text/plain\r\n\r\n"
  155. "%s", (int) strlen(fetch_data), fetch_data);
  156. return "";
  157. } else if (event == MG_OPEN_FILE) {
  158. const char *path = request_info->ev_data;
  159. if (strcmp(path, "./blah") == 0) {
  160. mg_get_request_info(conn)->ev_data =
  161. (void *) (int) strlen(inmemory_file_data);
  162. return (void *) inmemory_file_data;
  163. }
  164. } else if (event == MG_EVENT_LOG) {
  165. }
  166. return NULL;
  167. }
  168. static const char *OPTIONS[] = {
  169. "document_root", ".",
  170. "listening_ports", LISTENING_ADDR,
  171. "ssl_certificate", "build/ssl_cert.pem",
  172. NULL,
  173. };
  174. static void test_mg_upload(void) {
  175. struct mg_context *ctx;
  176. ASSERT((ctx = mg_start(event_handler, NULL, OPTIONS)) != NULL);
  177. mg_stop(ctx);
  178. }
  179. static char *read_file(const char *path, int *size) {
  180. FILE *fp;
  181. struct stat st;
  182. char *data = NULL;
  183. if ((fp = fopen(path, "r")) != NULL && !fstat(fileno(fp), &st)) {
  184. *size = st.st_size;
  185. ASSERT((data = malloc(*size)) != NULL);
  186. ASSERT(fread(data, 1, *size, fp) == (size_t) *size);
  187. fclose(fp);
  188. }
  189. return data;
  190. }
  191. static char *read_conn(struct mg_connection *conn, int *size) {
  192. char buf[100], *data = NULL;
  193. int len;
  194. *size = 0;
  195. while ((len = mg_read(conn, buf, sizeof(buf))) > 0) {
  196. *size += len;
  197. ASSERT((data = realloc(data, *size)) != NULL);
  198. memcpy(data + *size - len, buf, len);
  199. }
  200. return data;
  201. }
  202. static void test_mg_download(void) {
  203. char *p1, *p2, ebuf[100];
  204. int len1, len2, port = atoi(HTTPS_PORT);
  205. struct mg_connection *conn;
  206. struct mg_context *ctx;
  207. ASSERT((ctx = mg_start(event_handler, NULL, OPTIONS)) != NULL);
  208. ASSERT(mg_download(NULL, port, 0, ebuf, sizeof(ebuf), "") == NULL);
  209. ASSERT(mg_download("localhost", 0, 0, ebuf, sizeof(ebuf), "") == NULL);
  210. ASSERT(mg_download("localhost", port, 1, ebuf, sizeof(ebuf), "") == NULL);
  211. // Fetch nonexistent file, should see 404
  212. ASSERT((conn = mg_download("localhost", port, 1, ebuf, sizeof(ebuf), "%s",
  213. "GET /gimbec HTTP/1.0\r\n\r\n")) != NULL);
  214. ASSERT(strcmp(conn->request_info.uri, "404") == 0);
  215. mg_close_connection(conn);
  216. // Fetch mongoose.c, should succeed
  217. ASSERT((conn = mg_download("localhost", port, 1, ebuf, sizeof(ebuf), "%s",
  218. "GET /mongoose.c HTTP/1.0\r\n\r\n")) != NULL);
  219. ASSERT(!strcmp(conn->request_info.uri, "200"));
  220. ASSERT((p1 = read_conn(conn, &len1)) != NULL);
  221. ASSERT((p2 = read_file("mongoose.c", &len2)) != NULL);
  222. ASSERT(len1 == len2);
  223. ASSERT(memcmp(p1, p2, len1) == 0);
  224. free(p1), free(p2);
  225. mg_close_connection(conn);
  226. // Fetch in-memory file, should succeed.
  227. ASSERT((conn = mg_download("localhost", port, 1, ebuf, sizeof(ebuf), "%s",
  228. "GET /blah HTTP/1.1\r\n\r\n")) != NULL);
  229. ASSERT((p1 = read_conn(conn, &len1)) != NULL);
  230. ASSERT(len1 == (int) strlen(inmemory_file_data));
  231. ASSERT(memcmp(p1, inmemory_file_data, len1) == 0);
  232. free(p1);
  233. mg_close_connection(conn);
  234. // Test SSL redirect, IP address
  235. ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0,
  236. ebuf, sizeof(ebuf), "%s",
  237. "GET /foo HTTP/1.1\r\n\r\n")) != NULL);
  238. ASSERT(strcmp(conn->request_info.uri, "302") == 0);
  239. ASSERT(strcmp(mg_get_header(conn, "Location"),
  240. "https://127.0.0.1:" HTTPS_PORT "/foo") == 0);
  241. mg_close_connection(conn);
  242. // Test SSL redirect, Host:
  243. ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0,
  244. ebuf, sizeof(ebuf), "%s",
  245. "GET /foo HTTP/1.1\r\nHost: a.b:77\n\n")) != NULL);
  246. ASSERT(strcmp(conn->request_info.uri, "302") == 0);
  247. ASSERT(strcmp(mg_get_header(conn, "Location"),
  248. "https://a.b:" HTTPS_PORT "/foo") == 0);
  249. mg_close_connection(conn);
  250. mg_stop(ctx);
  251. }
  252. static void test_base64_encode(void) {
  253. const char *in[] = {"a", "ab", "abc", "abcd", NULL};
  254. const char *out[] = {"YQ==", "YWI=", "YWJj", "YWJjZA=="};
  255. char buf[100];
  256. int i;
  257. for (i = 0; in[i] != NULL; i++) {
  258. base64_encode((unsigned char *) in[i], strlen(in[i]), buf);
  259. ASSERT(!strcmp(buf, out[i]));
  260. }
  261. }
  262. static void test_mg_get_var(void) {
  263. static const char *post[] = {
  264. "a=1&&b=2&d&=&c=3%20&e=",
  265. "q=&st=2012%2F11%2F13+17%3A05&et=&team_id=",
  266. NULL
  267. };
  268. char buf[20];
  269. ASSERT(mg_get_var(post[0], strlen(post[0]), "a", buf, sizeof(buf)) == 1);
  270. ASSERT(buf[0] == '1' && buf[1] == '\0');
  271. ASSERT(mg_get_var(post[0], strlen(post[0]), "b", buf, sizeof(buf)) == 1);
  272. ASSERT(buf[0] == '2' && buf[1] == '\0');
  273. ASSERT(mg_get_var(post[0], strlen(post[0]), "c", buf, sizeof(buf)) == 2);
  274. ASSERT(buf[0] == '3' && buf[1] == ' ' && buf[2] == '\0');
  275. ASSERT(mg_get_var(post[0], strlen(post[0]), "e", buf, sizeof(buf)) == 0);
  276. ASSERT(buf[0] == '\0');
  277. ASSERT(mg_get_var(post[0], strlen(post[0]), "d", buf, sizeof(buf)) == -1);
  278. ASSERT(mg_get_var(post[0], strlen(post[0]), "c", buf, 2) == -2);
  279. ASSERT(mg_get_var(post[0], strlen(post[0]), "x", NULL, 10) == -2);
  280. ASSERT(mg_get_var(post[0], strlen(post[0]), "x", buf, 0) == -2);
  281. ASSERT(mg_get_var(post[1], strlen(post[1]), "st", buf, 16) == -2);
  282. ASSERT(mg_get_var(post[1], strlen(post[1]), "st", buf, 17) == 16);
  283. }
  284. static void test_set_throttle(void) {
  285. ASSERT(set_throttle(NULL, 0x0a000001, "/") == 0);
  286. ASSERT(set_throttle("10.0.0.0/8=20", 0x0a000001, "/") == 20);
  287. ASSERT(set_throttle("10.0.0.0/8=0.5k", 0x0a000001, "/") == 512);
  288. ASSERT(set_throttle("10.0.0.0/8=17m", 0x0a000001, "/") == 1048576 * 17);
  289. ASSERT(set_throttle("10.0.0.0/8=1x", 0x0a000001, "/") == 0);
  290. ASSERT(set_throttle("10.0.0.0/8=5,0.0.0.0/0=10", 0x0a000001, "/") == 10);
  291. ASSERT(set_throttle("10.0.0.0/8=5,/foo/**=7", 0x0a000001, "/index") == 5);
  292. ASSERT(set_throttle("10.0.0.0/8=5,/foo/**=7", 0x0a000001, "/foo/x") == 7);
  293. ASSERT(set_throttle("10.0.0.0/8=5,/foo/**=7", 0x0b000001, "/foxo/x") == 0);
  294. ASSERT(set_throttle("10.0.0.0/8=5,*=1", 0x0b000001, "/foxo/x") == 1);
  295. }
  296. static void test_next_option(void) {
  297. const char *p, *list = "x/8,/y**=1;2k,z";
  298. struct vec a, b;
  299. int i;
  300. ASSERT(next_option(NULL, &a, &b) == NULL);
  301. for (i = 0, p = list; (p = next_option(p, &a, &b)) != NULL; i++) {
  302. ASSERT(i != 0 || (a.ptr == list && a.len == 3 && b.len == 0));
  303. ASSERT(i != 1 || (a.ptr == list + 4 && a.len == 4 && b.ptr == list + 9 &&
  304. b.len == 4));
  305. ASSERT(i != 2 || (a.ptr == list + 14 && a.len == 1 && b.len == 0));
  306. }
  307. }
  308. #ifdef USE_LUA
  309. static void check_lua_expr(lua_State *L, const char *expr, const char *value) {
  310. const char *v, *var_name = "myVar";
  311. char buf[100];
  312. snprintf(buf, sizeof(buf), "%s = %s", var_name, expr);
  313. luaL_dostring(L, buf);
  314. lua_getglobal(L, var_name);
  315. v = lua_tostring(L, -1);
  316. #if 0
  317. printf("%s: %s: [%s] [%s]\n", __func__, expr, v == NULL ? "null" : v, value);
  318. #endif
  319. ASSERT((value == NULL && v == NULL) ||
  320. (value != NULL && v != NULL && !strcmp(value, v)));
  321. }
  322. static void test_lua(void) {
  323. static struct mg_connection conn;
  324. static struct mg_context ctx;
  325. char http_request[] = "POST /foo/bar HTTP/1.1\r\n"
  326. "Content-Length: 12\r\n"
  327. "Connection: close\r\n\r\nhello world!";
  328. lua_State *L = luaL_newstate();
  329. conn.ctx = &ctx;
  330. conn.buf = http_request;
  331. conn.buf_size = conn.data_len = strlen(http_request);
  332. conn.request_len = parse_http_message(conn.buf, conn.data_len,
  333. &conn.request_info);
  334. conn.content_len = conn.data_len - conn.request_len;
  335. prepare_lua_environment(&conn, L);
  336. ASSERT(lua_gettop(L) == 0);
  337. check_lua_expr(L, "'hi'", "hi");
  338. check_lua_expr(L, "request_info.request_method", "POST");
  339. check_lua_expr(L, "request_info.uri", "/foo/bar");
  340. check_lua_expr(L, "request_info.num_headers", "2");
  341. check_lua_expr(L, "request_info.remote_ip", "0");
  342. check_lua_expr(L, "request_info.http_headers['Content-Length']", "12");
  343. check_lua_expr(L, "request_info.http_headers['Connection']", "close");
  344. luaL_dostring(L, "post = read()");
  345. check_lua_expr(L, "# post", "12");
  346. check_lua_expr(L, "post", "hello world!");
  347. lua_close(L);
  348. }
  349. #endif
  350. static void *user_data_tester(enum mg_event event, struct mg_connection *conn) {
  351. struct mg_request_info *ri = mg_get_request_info(conn);
  352. ASSERT(ri->user_data == (void *) 123);
  353. ASSERT(event == MG_NEW_REQUEST || event == MG_INIT_SSL);
  354. return NULL;
  355. }
  356. static void test_user_data(void) {
  357. struct mg_context *ctx;
  358. ASSERT((ctx = mg_start(user_data_tester, (void *) 123, OPTIONS)) != NULL);
  359. ASSERT(ctx->user_data == (void *) 123);
  360. call_user(fc(ctx), MG_NEW_REQUEST);
  361. mg_stop(ctx);
  362. }
  363. static void test_mg_stat(void) {
  364. static struct mg_context ctx;
  365. struct file file = STRUCT_FILE_INITIALIZER;
  366. ASSERT(!mg_stat(fc(&ctx), " does not exist ", &file));
  367. }
  368. static void test_skip_quoted(void) {
  369. char x[] = "a=1, b=2 c='hi \' there'", *s = x, *p;
  370. p = skip_quoted(&s, ", ", ", ", 0);
  371. ASSERT(p != NULL && !strcmp(p, "a=1"));
  372. p = skip_quoted(&s, ", ", ", ", 0);
  373. ASSERT(p != NULL && !strcmp(p, "b=2"));
  374. // TODO(lsm): fix this
  375. #if 0
  376. p = skip_quoted(&s, "'", ", ", '\\');
  377. p = skip_quoted(&s, "'", ", ", '\\');
  378. printf("[%s]\n", p);
  379. ASSERT(p != NULL && !strcmp(p, "hi ' there"));
  380. #endif
  381. }
  382. int __cdecl main(void) {
  383. test_base64_encode();
  384. test_match_prefix();
  385. test_remove_double_dots();
  386. test_should_keep_alive();
  387. test_parse_http_message();
  388. test_mg_download();
  389. test_mg_get_var();
  390. test_set_throttle();
  391. test_next_option();
  392. test_user_data();
  393. test_mg_stat();
  394. test_mg_upload();
  395. #ifdef USE_LUA
  396. test_lua();
  397. #endif
  398. test_skip_quoted();
  399. printf("%s\n", "PASSED");
  400. return 0;
  401. }