unit_test.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. int main(void) {
  180. test_base64_encode();
  181. test_match_prefix();
  182. test_remove_double_dots();
  183. test_should_keep_alive();
  184. test_parse_http_request();
  185. test_mg_fetch();
  186. return 0;
  187. }