unit_test.c 4.6 KB

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