unit_test.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. ASSERT(parse_http_request(req1, &ri) == 1);
  13. ASSERT(strcmp(ri.http_version, "1.1") == 0);
  14. ASSERT(ri.num_headers == 0);
  15. ASSERT(parse_http_request(req2, &ri) == 0);
  16. // TODO(lsm): Fix this. Bah is not a valid header.
  17. ASSERT(parse_http_request(req3, &ri) == 1);
  18. ASSERT(ri.num_headers == 1);
  19. ASSERT(strcmp(ri.http_headers[0].name, "Bah\r\n") == 0);
  20. // TODO(lsm): add more tests.
  21. }
  22. static void test_should_keep_alive(void) {
  23. struct mg_connection conn;
  24. struct mg_context ctx;
  25. char req1[] = "GET / HTTP/1.1\r\n\r\n";
  26. char req2[] = "GET / HTTP/1.0\r\n\r\n";
  27. char req3[] = "GET / HTTP/1.1\r\nConnection: close\r\n\r\n";
  28. char req4[] = "GET / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";
  29. memset(&conn, 0, sizeof(conn));
  30. conn.ctx = &ctx;
  31. parse_http_request(req1, &conn.request_info);
  32. ctx.config[ENABLE_KEEP_ALIVE] = "no";
  33. ASSERT(should_keep_alive(&conn) == 0);
  34. ctx.config[ENABLE_KEEP_ALIVE] = "yes";
  35. ASSERT(should_keep_alive(&conn) == 1);
  36. conn.must_close = 1;
  37. ASSERT(should_keep_alive(&conn) == 0);
  38. conn.must_close = 0;
  39. parse_http_request(req2, &conn.request_info);
  40. ASSERT(should_keep_alive(&conn) == 0);
  41. parse_http_request(req3, &conn.request_info);
  42. ASSERT(should_keep_alive(&conn) == 0);
  43. parse_http_request(req4, &conn.request_info);
  44. ASSERT(should_keep_alive(&conn) == 1);
  45. conn.request_info.status_code = 401;
  46. ASSERT(should_keep_alive(&conn) == 0);
  47. conn.request_info.status_code = 200;
  48. conn.must_close = 1;
  49. ASSERT(should_keep_alive(&conn) == 0);
  50. }
  51. static void test_match_prefix(void) {
  52. ASSERT(match_prefix("/api", 4, "/api") == 4);
  53. ASSERT(match_prefix("/a/", 3, "/a/b/c") == 3);
  54. ASSERT(match_prefix("/a/", 3, "/ab/c") == -1);
  55. ASSERT(match_prefix("/*/", 3, "/ab/c") == 4);
  56. ASSERT(match_prefix("**", 2, "/a/b/c") == 6);
  57. ASSERT(match_prefix("/*", 2, "/a/b/c") == 2);
  58. ASSERT(match_prefix("*/*", 3, "/a/b/c") == 2);
  59. ASSERT(match_prefix("**/", 3, "/a/b/c") == 5);
  60. ASSERT(match_prefix("**.foo|**.bar", 13, "a.bar") == 5);
  61. ASSERT(match_prefix("a|b|cd", 6, "cdef") == 2);
  62. ASSERT(match_prefix("a|b|c?", 6, "cdef") == 2);
  63. ASSERT(match_prefix("a|?|cd", 6, "cdef") == 1);
  64. ASSERT(match_prefix("/a/**.cgi", 9, "/foo/bar/x.cgi") == -1);
  65. ASSERT(match_prefix("/a/**.cgi", 9, "/a/bar/x.cgi") == 12);
  66. ASSERT(match_prefix("**/", 3, "/a/b/c") == 5);
  67. ASSERT(match_prefix("**/$", 4, "/a/b/c") == -1);
  68. ASSERT(match_prefix("**/$", 4, "/a/b/") == 5);
  69. ASSERT(match_prefix("$", 1, "") == 0);
  70. ASSERT(match_prefix("$", 1, "x") == -1);
  71. ASSERT(match_prefix("*$", 2, "x") == 1);
  72. ASSERT(match_prefix("/$", 2, "/") == 1);
  73. ASSERT(match_prefix("**/$", 4, "/a/b/c") == -1);
  74. ASSERT(match_prefix("**/$", 4, "/a/b/") == 5);
  75. ASSERT(match_prefix("*", 1, "/hello/") == 0);
  76. ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.b/") == -1);
  77. ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.b") == 6);
  78. ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.a") == 6);
  79. }
  80. static void test_remove_double_dots() {
  81. struct { char before[20], after[20]; } data[] = {
  82. {"////a", "/a"},
  83. {"/.....", "/."},
  84. {"/......", "/"},
  85. {"...", "..."},
  86. {"/...///", "/./"},
  87. {"/a...///", "/a.../"},
  88. {"/.x", "/.x"},
  89. #if defined(_WIN32)
  90. {"/\\", "/"},
  91. #else
  92. {"/\\", "/\\"},
  93. #endif
  94. {"/a\\", "/a\\"},
  95. };
  96. size_t i;
  97. for (i = 0; i < ARRAY_SIZE(data); i++) {
  98. //printf("[%s] -> [%s]\n", data[i].before, data[i].after);
  99. remove_double_dots_and_double_slashes(data[i].before);
  100. ASSERT(strcmp(data[i].before, data[i].after) == 0);
  101. }
  102. }
  103. int main(void) {
  104. test_match_prefix();
  105. test_remove_double_dots();
  106. test_should_keep_alive();
  107. test_parse_http_request();
  108. return 0;
  109. }