unit_test.c 15 KB

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