unit_test.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. #if defined(_WIN32)
  98. {"/\\", "/"},
  99. #else
  100. {"/\\", "/\\"},
  101. #endif
  102. {"/a\\", "/a\\"},
  103. };
  104. size_t i;
  105. for (i = 0; i < ARRAY_SIZE(data); i++) {
  106. //printf("[%s] -> [%s]\n", data[i].before, data[i].after);
  107. remove_double_dots_and_double_slashes(data[i].before);
  108. ASSERT(strcmp(data[i].before, data[i].after) == 0);
  109. }
  110. }
  111. static const char *fetch_data = "hello world!\n";
  112. static void *event_handler(enum mg_event event,
  113. struct mg_connection *conn) {
  114. const struct mg_request_info *request_info = mg_get_request_info(conn);
  115. if (event == MG_NEW_REQUEST && !strcmp(request_info->uri, "/data")) {
  116. mg_printf(conn, "HTTP/1.1 200 OK\r\n"
  117. "Content-Length: %d\r\n"
  118. "Content-Type: text/plain\r\n\r\n"
  119. "%s", (int) strlen(fetch_data), fetch_data);
  120. return "";
  121. } else if (event == MG_EVENT_LOG) {
  122. printf("%s\n", mg_get_log_message(conn));
  123. }
  124. return NULL;
  125. }
  126. static void test_mg_fetch(void) {
  127. static const char *options[] = {
  128. "document_root", ".",
  129. "listening_ports", "33796",
  130. NULL,
  131. };
  132. char buf[2000], buf2[2000];
  133. int length;
  134. struct mg_context *ctx;
  135. struct mg_request_info ri;
  136. const char *tmp_file = "temporary_file_name_for_unit_test.txt";
  137. struct mgstat st;
  138. FILE *fp;
  139. ASSERT((ctx = mg_start(event_handler, NULL, options)) != NULL);
  140. // Failed fetch, pass invalid URL
  141. ASSERT(mg_fetch(ctx, "localhost", tmp_file, buf, sizeof(buf), &ri) == NULL);
  142. ASSERT(mg_fetch(ctx, "localhost:33796", tmp_file,
  143. buf, sizeof(buf), &ri) == NULL);
  144. ASSERT(mg_fetch(ctx, "http://$$$.$$$", tmp_file,
  145. buf, sizeof(buf), &ri) == NULL);
  146. // Failed fetch, pass invalid file name
  147. ASSERT(mg_fetch(ctx, "http://localhost:33796/data",
  148. "/this/file/must/not/exist/ever",
  149. buf, sizeof(buf), &ri) == NULL);
  150. // Successful fetch
  151. ASSERT((fp = mg_fetch(ctx, "http://localhost:33796/data",
  152. tmp_file, buf, sizeof(buf), &ri)) != NULL);
  153. ASSERT(ri.num_headers == 2);
  154. ASSERT(!strcmp(ri.request_method, "HTTP/1.1"));
  155. ASSERT(!strcmp(ri.uri, "200"));
  156. ASSERT(!strcmp(ri.http_version, "OK"));
  157. ASSERT((length = ftell(fp)) == (int) strlen(fetch_data));
  158. fseek(fp, 0, SEEK_SET);
  159. ASSERT(fread(buf2, 1, length, fp) == (size_t) length);
  160. ASSERT(memcmp(buf2, fetch_data, length) == 0);
  161. fclose(fp);
  162. // Fetch big file, mongoose.c
  163. ASSERT((fp = mg_fetch(ctx, "http://localhost:33796/mongoose.c",
  164. tmp_file, buf, sizeof(buf), &ri)) != NULL);
  165. ASSERT(mg_stat("mongoose.c", &st) == 0);
  166. ASSERT(st.size == ftell(fp));
  167. ASSERT(!strcmp(ri.request_method, "HTTP/1.1"));
  168. remove(tmp_file);
  169. mg_stop(ctx);
  170. }
  171. static void test_base64_encode(void) {
  172. const char *in[] = {"a", "ab", "abc", "abcd", NULL};
  173. const char *out[] = {"YQ==", "YWI=", "YWJj", "YWJjZA=="};
  174. char buf[100];
  175. int i;
  176. for (i = 0; in[i] != NULL; i++) {
  177. base64_encode((unsigned char *) in[i], strlen(in[i]), buf);
  178. printf("[%s] [%s]\n", out[i], buf);
  179. ASSERT(!strcmp(buf, out[i]));
  180. }
  181. }
  182. int main(void) {
  183. test_base64_encode();
  184. test_match_prefix();
  185. test_remove_double_dots();
  186. test_should_keep_alive();
  187. test_parse_http_request();
  188. test_mg_fetch();
  189. return 0;
  190. }