unit_test.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include "mongoose.c"
  2. #define FATAL(str, line) do { \
  3. printf("Fail on line %d: [%s]\n", line, str); \
  4. abort(); \
  5. } while (0)
  6. #define ASSERT(expr) do { if (!(expr)) FATAL(#expr, __LINE__); } while (0)
  7. static void test_parse_http_request() {
  8. struct mg_request_info ri;
  9. char req1[] = "GET / HTTP/1.1\r\n\r\n";
  10. char req2[] = "BLAH / HTTP/1.1\r\n\r\n";
  11. char req3[] = "GET / HTTP/1.1\r\nBah\r\n";
  12. char req4[] = "GET / HTTP/1.1\r\nA: foo bar\r\nB: bar\r\nbaz\r\n\r\n";
  13. ASSERT(parse_http_request(req1, sizeof(req1), &ri) == sizeof(req1) - 1);
  14. ASSERT(strcmp(ri.http_version, "1.1") == 0);
  15. ASSERT(ri.num_headers == 0);
  16. ASSERT(parse_http_request(req2, sizeof(req2), &ri) == -1);
  17. ASSERT(parse_http_request(req3, sizeof(req3), &ri) == -1);
  18. // TODO(lsm): Fix this. Header value may span multiple lines.
  19. ASSERT(parse_http_request(req4, sizeof(req4), &ri) == sizeof(req4) - 1);
  20. ASSERT(ri.num_headers == 3);
  21. ASSERT(strcmp(ri.http_headers[0].name, "A") == 0);
  22. ASSERT(strcmp(ri.http_headers[0].value, "foo bar") == 0);
  23. ASSERT(strcmp(ri.http_headers[1].name, "B") == 0);
  24. ASSERT(strcmp(ri.http_headers[1].value, "bar") == 0);
  25. ASSERT(strcmp(ri.http_headers[2].name, "baz\r\n\r") == 0);
  26. ASSERT(strcmp(ri.http_headers[2].value, "") == 0);
  27. // TODO(lsm): add more tests.
  28. }
  29. static void test_should_keep_alive(void) {
  30. struct mg_connection conn;
  31. struct mg_context ctx;
  32. char req1[] = "GET / HTTP/1.1\r\n\r\n";
  33. char req2[] = "GET / HTTP/1.0\r\n\r\n";
  34. char req3[] = "GET / HTTP/1.1\r\nConnection: close\r\n\r\n";
  35. char req4[] = "GET / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";
  36. memset(&conn, 0, sizeof(conn));
  37. conn.ctx = &ctx;
  38. parse_http_request(req1, sizeof(req1), &conn.request_info);
  39. ctx.config[ENABLE_KEEP_ALIVE] = "no";
  40. ASSERT(should_keep_alive(&conn) == 0);
  41. ctx.config[ENABLE_KEEP_ALIVE] = "yes";
  42. ASSERT(should_keep_alive(&conn) == 1);
  43. conn.must_close = 1;
  44. ASSERT(should_keep_alive(&conn) == 0);
  45. conn.must_close = 0;
  46. parse_http_request(req2, sizeof(req2), &conn.request_info);
  47. ASSERT(should_keep_alive(&conn) == 0);
  48. parse_http_request(req3, sizeof(req3), &conn.request_info);
  49. ASSERT(should_keep_alive(&conn) == 0);
  50. parse_http_request(req4, sizeof(req4), &conn.request_info);
  51. ASSERT(should_keep_alive(&conn) == 1);
  52. conn.request_info.status_code = 401;
  53. ASSERT(should_keep_alive(&conn) == 0);
  54. conn.request_info.status_code = 200;
  55. conn.must_close = 1;
  56. ASSERT(should_keep_alive(&conn) == 0);
  57. }
  58. static void test_match_prefix(void) {
  59. ASSERT(match_prefix("/api", 4, "/api") == 4);
  60. ASSERT(match_prefix("/a/", 3, "/a/b/c") == 3);
  61. ASSERT(match_prefix("/a/", 3, "/ab/c") == -1);
  62. ASSERT(match_prefix("/*/", 3, "/ab/c") == 4);
  63. ASSERT(match_prefix("**", 2, "/a/b/c") == 6);
  64. ASSERT(match_prefix("/*", 2, "/a/b/c") == 2);
  65. ASSERT(match_prefix("*/*", 3, "/a/b/c") == 2);
  66. ASSERT(match_prefix("**/", 3, "/a/b/c") == 5);
  67. ASSERT(match_prefix("**.foo|**.bar", 13, "a.bar") == 5);
  68. ASSERT(match_prefix("a|b|cd", 6, "cdef") == 2);
  69. ASSERT(match_prefix("a|b|c?", 6, "cdef") == 2);
  70. ASSERT(match_prefix("a|?|cd", 6, "cdef") == 1);
  71. ASSERT(match_prefix("/a/**.cgi", 9, "/foo/bar/x.cgi") == -1);
  72. ASSERT(match_prefix("/a/**.cgi", 9, "/a/bar/x.cgi") == 12);
  73. ASSERT(match_prefix("**/", 3, "/a/b/c") == 5);
  74. ASSERT(match_prefix("**/$", 4, "/a/b/c") == -1);
  75. ASSERT(match_prefix("**/$", 4, "/a/b/") == 5);
  76. ASSERT(match_prefix("$", 1, "") == 0);
  77. ASSERT(match_prefix("$", 1, "x") == -1);
  78. ASSERT(match_prefix("*$", 2, "x") == 1);
  79. ASSERT(match_prefix("/$", 2, "/") == 1);
  80. ASSERT(match_prefix("**/$", 4, "/a/b/c") == -1);
  81. ASSERT(match_prefix("**/$", 4, "/a/b/") == 5);
  82. ASSERT(match_prefix("*", 1, "/hello/") == 0);
  83. ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.b/") == -1);
  84. ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.b") == 6);
  85. ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.a") == 6);
  86. }
  87. static void test_remove_double_dots() {
  88. struct { char before[20], after[20]; } data[] = {
  89. {"////a", "/a"},
  90. {"/.....", "/."},
  91. {"/......", "/"},
  92. {"...", "..."},
  93. {"/...///", "/./"},
  94. {"/a...///", "/a.../"},
  95. {"/.x", "/.x"},
  96. #if defined(_WIN32)
  97. {"/\\", "/"},
  98. #else
  99. {"/\\", "/\\"},
  100. #endif
  101. {"/a\\", "/a\\"},
  102. };
  103. size_t i;
  104. for (i = 0; i < ARRAY_SIZE(data); i++) {
  105. //printf("[%s] -> [%s]\n", data[i].before, data[i].after);
  106. remove_double_dots_and_double_slashes(data[i].before);
  107. ASSERT(strcmp(data[i].before, data[i].after) == 0);
  108. }
  109. }
  110. static const char *fetch_data = "hello world!\n";
  111. static void *event_handler(enum mg_event event,
  112. struct mg_connection *conn) {
  113. const struct mg_request_info *request_info = mg_get_request_info(conn);
  114. if (event == MG_NEW_REQUEST && !strcmp(request_info->uri, "/data")) {
  115. mg_printf(conn, "HTTP/1.1 200 OK\r\n"
  116. "Content-Length: %d\r\n"
  117. "Content-Type: text/plain\r\n\r\n"
  118. "%s", (int) strlen(fetch_data), fetch_data);
  119. return "";
  120. } else if (event == MG_EVENT_LOG) {
  121. printf("%s\n", request_info->log_message);
  122. }
  123. return NULL;
  124. }
  125. static void test_mg_fetch(void) {
  126. static const char *options[] = {
  127. "document_root", ".",
  128. "listening_ports", "33796",
  129. NULL,
  130. };
  131. char buf[2000], buf2[2000];
  132. int length;
  133. struct mg_context *ctx;
  134. struct mg_request_info ri;
  135. const char *tmp_file = "temporary_file_name_for_unit_test.txt";
  136. struct mgstat st;
  137. FILE *fp;
  138. ASSERT((ctx = mg_start(event_handler, NULL, options)) != NULL);
  139. // Failed fetch, pass invalid URL
  140. ASSERT(mg_fetch(ctx, "localhost", tmp_file, buf, sizeof(buf), &ri) == NULL);
  141. ASSERT(mg_fetch(ctx, "localhost:33796", tmp_file,
  142. buf, sizeof(buf), &ri) == NULL);
  143. ASSERT(mg_fetch(ctx, "http://$$$.$$$", tmp_file,
  144. buf, sizeof(buf), &ri) == NULL);
  145. // Failed fetch, pass invalid file name
  146. ASSERT(mg_fetch(ctx, "http://localhost:33796/data",
  147. "/this/file/must/not/exist/ever",
  148. buf, sizeof(buf), &ri) == NULL);
  149. // Successful fetch
  150. ASSERT((fp = mg_fetch(ctx, "http://localhost:33796/data",
  151. tmp_file, buf, sizeof(buf), &ri)) != NULL);
  152. ASSERT(ri.num_headers == 2);
  153. ASSERT(!strcmp(ri.request_method, "HTTP/1.1"));
  154. ASSERT(!strcmp(ri.uri, "200"));
  155. ASSERT(!strcmp(ri.http_version, "OK"));
  156. ASSERT((length = ftell(fp)) == (int) strlen(fetch_data));
  157. fseek(fp, 0, SEEK_SET);
  158. ASSERT(fread(buf2, 1, length, fp) == (size_t) length);
  159. ASSERT(memcmp(buf2, fetch_data, length) == 0);
  160. fclose(fp);
  161. // Fetch big file, mongoose.c
  162. ASSERT((fp = mg_fetch(ctx, "http://localhost:33796/mongoose.c",
  163. tmp_file, buf, sizeof(buf), &ri)) != NULL);
  164. ASSERT(mg_stat("mongoose.c", &st) == 0);
  165. ASSERT(st.size == ftell(fp));
  166. ASSERT(!strcmp(ri.request_method, "HTTP/1.1"));
  167. remove(tmp_file);
  168. mg_stop(ctx);
  169. }
  170. int main(void) {
  171. test_match_prefix();
  172. test_remove_double_dots();
  173. test_should_keep_alive();
  174. test_parse_http_request();
  175. test_mg_fetch();
  176. return 0;
  177. }