unit_test.c 12 KB

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