unit_test.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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) {
  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\r\n"
  117. "%s", (int) strlen(fetch_data), fetch_data);
  118. return "";
  119. } else if (event == MG_EVENT_LOG) {
  120. printf("%s\n", request_info->log_message);
  121. }
  122. return NULL;
  123. }
  124. static void test_mg_fetch(void) {
  125. static const char *options[] = {
  126. "document_root", ".",
  127. "listening_ports", "33796",
  128. NULL,
  129. };
  130. char buf[1000];
  131. int length;
  132. struct mg_context *ctx;
  133. struct mg_request_info ri;
  134. const char *tmp_file = "temporary_file_name_for_unit_test.txt";
  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, &ri) == NULL);
  139. ASSERT(mg_fetch(ctx, "localhost:33796", tmp_file, &ri) == NULL);
  140. ASSERT(mg_fetch(ctx, "http://$$$.$$$", tmp_file, &ri) == NULL);
  141. // Failed fetch, pass invalid file name
  142. ASSERT(mg_fetch(ctx, "http://localhost:33796/data",
  143. "/this/file/must/not/exist/ever", &ri) == NULL);
  144. // Successful fetch
  145. ASSERT((fp = mg_fetch(ctx, "http://localhost:33796/data",
  146. tmp_file, &ri)) != NULL);
  147. ASSERT((length = ftell(fp)) == (int) strlen(fetch_data));
  148. fseek(fp, 0, SEEK_SET);
  149. ASSERT(fread(buf, 1, length, fp) == length);
  150. ASSERT(memcmp(buf, fetch_data, length) == 0);
  151. remove(tmp_file);
  152. mg_stop(ctx);
  153. }
  154. int main(void) {
  155. test_match_prefix();
  156. test_remove_double_dots();
  157. test_should_keep_alive();
  158. test_parse_http_request();
  159. test_mg_fetch();
  160. return 0;
  161. }