unit_test.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. static void test_parse_http_request() {
  9. struct mg_request_info ri;
  10. char req1[] = "GET / HTTP/1.1\r\n\r\n";
  11. char req2[] = "BLAH / HTTP/1.1\r\n\r\n";
  12. char req3[] = "GET / HTTP/1.1\r\nBah\r\n";
  13. char req4[] = "GET / HTTP/1.1\r\nA: foo bar\r\nB: bar\r\nbaz\r\n\r\n";
  14. ASSERT(parse_http_request(req1, sizeof(req1), &ri) == sizeof(req1) - 1);
  15. ASSERT(strcmp(ri.http_version, "1.1") == 0);
  16. ASSERT(ri.num_headers == 0);
  17. ASSERT(parse_http_request(req2, sizeof(req2), &ri) == -1);
  18. ASSERT(parse_http_request(req3, sizeof(req3), &ri) == -1);
  19. // TODO(lsm): Fix this. Header value may span multiple lines.
  20. ASSERT(parse_http_request(req4, sizeof(req4), &ri) == sizeof(req4) - 1);
  21. ASSERT(ri.num_headers == 3);
  22. ASSERT(strcmp(ri.http_headers[0].name, "A") == 0);
  23. ASSERT(strcmp(ri.http_headers[0].value, "foo bar") == 0);
  24. ASSERT(strcmp(ri.http_headers[1].name, "B") == 0);
  25. ASSERT(strcmp(ri.http_headers[1].value, "bar") == 0);
  26. ASSERT(strcmp(ri.http_headers[2].name, "baz\r\n\r") == 0);
  27. ASSERT(strcmp(ri.http_headers[2].value, "") == 0);
  28. // TODO(lsm): add more tests.
  29. }
  30. static void test_should_keep_alive(void) {
  31. struct mg_connection conn;
  32. struct mg_context ctx;
  33. char req1[] = "GET / HTTP/1.1\r\n\r\n";
  34. char req2[] = "GET / HTTP/1.0\r\n\r\n";
  35. char req3[] = "GET / HTTP/1.1\r\nConnection: close\r\n\r\n";
  36. char req4[] = "GET / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";
  37. memset(&conn, 0, sizeof(conn));
  38. conn.ctx = &ctx;
  39. parse_http_request(req1, sizeof(req1), &conn.request_info);
  40. ctx.config[ENABLE_KEEP_ALIVE] = "no";
  41. ASSERT(should_keep_alive(&conn) == 0);
  42. ctx.config[ENABLE_KEEP_ALIVE] = "yes";
  43. ASSERT(should_keep_alive(&conn) == 1);
  44. conn.must_close = 1;
  45. ASSERT(should_keep_alive(&conn) == 0);
  46. conn.must_close = 0;
  47. parse_http_request(req2, sizeof(req2), &conn.request_info);
  48. ASSERT(should_keep_alive(&conn) == 0);
  49. parse_http_request(req3, sizeof(req3), &conn.request_info);
  50. ASSERT(should_keep_alive(&conn) == 0);
  51. parse_http_request(req4, sizeof(req4), &conn.request_info);
  52. ASSERT(should_keep_alive(&conn) == 1);
  53. conn.status_code = 401;
  54. ASSERT(should_keep_alive(&conn) == 0);
  55. conn.status_code = 200;
  56. conn.must_close = 1;
  57. ASSERT(should_keep_alive(&conn) == 0);
  58. }
  59. static void test_match_prefix(void) {
  60. ASSERT(match_prefix("/api", 4, "/api") == 4);
  61. ASSERT(match_prefix("/a/", 3, "/a/b/c") == 3);
  62. ASSERT(match_prefix("/a/", 3, "/ab/c") == -1);
  63. ASSERT(match_prefix("/*/", 3, "/ab/c") == 4);
  64. ASSERT(match_prefix("**", 2, "/a/b/c") == 6);
  65. ASSERT(match_prefix("/*", 2, "/a/b/c") == 2);
  66. ASSERT(match_prefix("*/*", 3, "/a/b/c") == 2);
  67. ASSERT(match_prefix("**/", 3, "/a/b/c") == 5);
  68. ASSERT(match_prefix("**.foo|**.bar", 13, "a.bar") == 5);
  69. ASSERT(match_prefix("a|b|cd", 6, "cdef") == 2);
  70. ASSERT(match_prefix("a|b|c?", 6, "cdef") == 2);
  71. ASSERT(match_prefix("a|?|cd", 6, "cdef") == 1);
  72. ASSERT(match_prefix("/a/**.cgi", 9, "/foo/bar/x.cgi") == -1);
  73. ASSERT(match_prefix("/a/**.cgi", 9, "/a/bar/x.cgi") == 12);
  74. ASSERT(match_prefix("**/", 3, "/a/b/c") == 5);
  75. ASSERT(match_prefix("**/$", 4, "/a/b/c") == -1);
  76. ASSERT(match_prefix("**/$", 4, "/a/b/") == 5);
  77. ASSERT(match_prefix("$", 1, "") == 0);
  78. ASSERT(match_prefix("$", 1, "x") == -1);
  79. ASSERT(match_prefix("*$", 2, "x") == 1);
  80. ASSERT(match_prefix("/$", 2, "/") == 1);
  81. ASSERT(match_prefix("**/$", 4, "/a/b/c") == -1);
  82. ASSERT(match_prefix("**/$", 4, "/a/b/") == 5);
  83. ASSERT(match_prefix("*", 1, "/hello/") == 0);
  84. ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.b/") == -1);
  85. ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.b") == 6);
  86. ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.a") == 6);
  87. }
  88. static void test_remove_double_dots() {
  89. struct { char before[20], after[20]; } data[] = {
  90. {"////a", "/a"},
  91. {"/.....", "/."},
  92. {"/......", "/"},
  93. {"...", "..."},
  94. {"/...///", "/./"},
  95. {"/a...///", "/a.../"},
  96. {"/.x", "/.x"},
  97. {"/\\", "/"},
  98. {"/a\\", "/a\\"},
  99. {"/a\\\\...", "/a\\."},
  100. };
  101. size_t i;
  102. for (i = 0; i < ARRAY_SIZE(data); i++) {
  103. printf("[%s] -> [%s]\n", data[i].before, data[i].after);
  104. remove_double_dots_and_double_slashes(data[i].before);
  105. ASSERT(strcmp(data[i].before, data[i].after) == 0);
  106. }
  107. }
  108. static const char *fetch_data = "hello world!\n";
  109. static void *event_handler(enum mg_event event,
  110. struct mg_connection *conn) {
  111. const struct mg_request_info *request_info = mg_get_request_info(conn);
  112. if (event == MG_NEW_REQUEST && !strcmp(request_info->uri, "/data")) {
  113. mg_printf(conn, "HTTP/1.1 200 OK\r\n"
  114. "Content-Length: %d\r\n"
  115. "Content-Type: text/plain\r\n\r\n"
  116. "%s", (int) strlen(fetch_data), fetch_data);
  117. return "";
  118. } else if (event == MG_EVENT_LOG) {
  119. printf("%s\n", mg_get_log_message(conn));
  120. }
  121. return NULL;
  122. }
  123. static void test_mg_fetch(void) {
  124. static const char *options[] = {
  125. "document_root", ".",
  126. "listening_ports", "33796",
  127. NULL,
  128. };
  129. char buf[2000], buf2[2000];
  130. int length;
  131. struct mg_context *ctx;
  132. struct mg_request_info ri;
  133. const char *tmp_file = "temporary_file_name_for_unit_test.txt";
  134. struct mgstat st;
  135. FILE *fp;
  136. ASSERT((ctx = mg_start(event_handler, NULL, options)) != NULL);
  137. // Failed fetch, pass invalid URL
  138. ASSERT(mg_fetch(ctx, "localhost", tmp_file, buf, sizeof(buf), &ri) == NULL);
  139. ASSERT(mg_fetch(ctx, "localhost:33796", tmp_file,
  140. buf, sizeof(buf), &ri) == NULL);
  141. ASSERT(mg_fetch(ctx, "http://$$$.$$$", tmp_file,
  142. buf, sizeof(buf), &ri) == NULL);
  143. // Failed fetch, pass invalid file name
  144. ASSERT(mg_fetch(ctx, "http://localhost:33796/data",
  145. "/this/file/must/not/exist/ever",
  146. buf, sizeof(buf), &ri) == NULL);
  147. // Successful fetch
  148. ASSERT((fp = mg_fetch(ctx, "http://localhost:33796/data",
  149. tmp_file, buf, sizeof(buf), &ri)) != NULL);
  150. ASSERT(ri.num_headers == 2);
  151. ASSERT(!strcmp(ri.request_method, "HTTP/1.1"));
  152. ASSERT(!strcmp(ri.uri, "200"));
  153. ASSERT(!strcmp(ri.http_version, "OK"));
  154. ASSERT((length = ftell(fp)) == (int) strlen(fetch_data));
  155. fseek(fp, 0, SEEK_SET);
  156. ASSERT(fread(buf2, 1, length, fp) == (size_t) length);
  157. ASSERT(memcmp(buf2, fetch_data, length) == 0);
  158. fclose(fp);
  159. // Fetch big file, mongoose.c
  160. ASSERT((fp = mg_fetch(ctx, "http://localhost:33796/mongoose.c",
  161. tmp_file, buf, sizeof(buf), &ri)) != NULL);
  162. ASSERT(mg_stat("mongoose.c", &st) == 0);
  163. ASSERT(st.size == ftell(fp));
  164. ASSERT(!strcmp(ri.request_method, "HTTP/1.1"));
  165. remove(tmp_file);
  166. mg_stop(ctx);
  167. }
  168. static void test_base64_encode(void) {
  169. const char *in[] = {"a", "ab", "abc", "abcd", NULL};
  170. const char *out[] = {"YQ==", "YWI=", "YWJj", "YWJjZA=="};
  171. char buf[100];
  172. int i;
  173. for (i = 0; in[i] != NULL; i++) {
  174. base64_encode((unsigned char *) in[i], strlen(in[i]), buf);
  175. printf("[%s] [%s]\n", out[i], buf);
  176. ASSERT(!strcmp(buf, out[i]));
  177. }
  178. }
  179. static void test_mg_get_var(void) {
  180. static const char *post[] = {"a=1&&b=2&d&=&c=3%20&e=", NULL};
  181. char buf[20];
  182. ASSERT(mg_get_var(post[0], strlen(post[0]), "a", buf, sizeof(buf)) == 1);
  183. ASSERT(buf[0] == '1' && buf[1] == '\0');
  184. ASSERT(mg_get_var(post[0], strlen(post[0]), "b", buf, sizeof(buf)) == 1);
  185. ASSERT(buf[0] == '2' && buf[1] == '\0');
  186. ASSERT(mg_get_var(post[0], strlen(post[0]), "c", buf, sizeof(buf)) == 2);
  187. ASSERT(buf[0] == '3' && buf[1] == ' ' && buf[2] == '\0');
  188. ASSERT(mg_get_var(post[0], strlen(post[0]), "e", buf, sizeof(buf)) == 0);
  189. ASSERT(buf[0] == '\0');
  190. ASSERT(mg_get_var(post[0], strlen(post[0]), "d", buf, sizeof(buf)) == -1);
  191. ASSERT(mg_get_var(post[0], strlen(post[0]), "c", buf, 2) == -1);
  192. ASSERT(mg_get_var(post[0], strlen(post[0]), "x", NULL, 10) == -2);
  193. ASSERT(mg_get_var(post[0], strlen(post[0]), "x", buf, 0) == -2);
  194. }
  195. static void test_set_throttle(void) {
  196. ASSERT(set_throttle(NULL, 0x0a000001, "/") == 0);
  197. ASSERT(set_throttle("10.0.0.0/8=20", 0x0a000001, "/") == 20);
  198. ASSERT(set_throttle("10.0.0.0/8=0.5k", 0x0a000001, "/") == 512);
  199. ASSERT(set_throttle("10.0.0.0/8=17m", 0x0a000001, "/") == 1048576 * 17);
  200. ASSERT(set_throttle("10.0.0.0/8=1x", 0x0a000001, "/") == 0);
  201. ASSERT(set_throttle("10.0.0.0/8=5,0.0.0.0/0=10", 0x0a000001, "/") == 10);
  202. ASSERT(set_throttle("10.0.0.0/8=5,/foo/**=7", 0x0a000001, "/index") == 5);
  203. ASSERT(set_throttle("10.0.0.0/8=5,/foo/**=7", 0x0a000001, "/foo/x") == 7);
  204. ASSERT(set_throttle("10.0.0.0/8=5,/foo/**=7", 0x0b000001, "/foxo/x") == 0);
  205. ASSERT(set_throttle("10.0.0.0/8=5,*=1", 0x0b000001, "/foxo/x") == 1);
  206. }
  207. static void test_next_option(void) {
  208. const char *p, *list = "x/8,/y**=1;2k,z";
  209. struct vec a, b;
  210. int i;
  211. ASSERT(next_option(NULL, &a, &b) == NULL);
  212. for (i = 0, p = list; (p = next_option(p, &a, &b)) != NULL; i++) {
  213. ASSERT(i != 0 || (a.ptr == list && a.len == 3 && b.len == 0));
  214. ASSERT(i != 1 || (a.ptr == list + 4 && a.len == 4 && b.ptr == list + 9 &&
  215. b.len == 4));
  216. ASSERT(i != 2 || (a.ptr == list + 14 && a.len == 1 && b.len == 0));
  217. }
  218. }
  219. int main(void) {
  220. test_base64_encode();
  221. test_match_prefix();
  222. test_remove_double_dots();
  223. test_should_keep_alive();
  224. test_parse_http_request();
  225. test_mg_fetch();
  226. test_mg_get_var();
  227. test_set_throttle();
  228. test_next_option();
  229. return 0;
  230. }