unit_test.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include "mongoose.c"
  2. static void test_match_prefix(void) {
  3. assert(match_prefix("/a/", 3, "/a/b/c") == 3);
  4. assert(match_prefix("/a/", 3, "/ab/c") == -1);
  5. assert(match_prefix("/*/", 3, "/ab/c") == 4);
  6. assert(match_prefix("**", 2, "/a/b/c") == 6);
  7. assert(match_prefix("/*", 2, "/a/b/c") == 2);
  8. assert(match_prefix("*/*", 3, "/a/b/c") == 2);
  9. assert(match_prefix("**/", 3, "/a/b/c") == 5);
  10. assert(match_prefix("**.foo|**.bar", 13, "a.bar") == 5);
  11. assert(match_prefix("a|b|cd", 6, "cdef") == 2);
  12. assert(match_prefix("a|b|c?", 6, "cdef") == 2);
  13. assert(match_prefix("a|?|cd", 6, "cdef") == 1);
  14. assert(match_prefix("/a/**.cgi", 9, "/foo/bar/x.cgi") == -1);
  15. assert(match_prefix("/a/**.cgi", 9, "/a/bar/x.cgi") == 12);
  16. assert(match_prefix("**/", 3, "/a/b/c") == 5);
  17. assert(match_prefix("**/$", 4, "/a/b/c") == -1);
  18. assert(match_prefix("**/$", 4, "/a/b/") == 5);
  19. assert(match_prefix("$", 1, "") == 0);
  20. assert(match_prefix("$", 1, "x") == -1);
  21. assert(match_prefix("*$", 2, "x") == 1);
  22. assert(match_prefix("/$", 2, "/") == 1);
  23. assert(match_prefix("**/$", 4, "/a/b/c") == -1);
  24. assert(match_prefix("**/$", 4, "/a/b/") == 5);
  25. assert(match_prefix("*", 1, "/hello/") == 0);
  26. assert(match_prefix("**.a$|**.b$", 11, "/a/b.b/") == -1);
  27. assert(match_prefix("**.a$|**.b$", 11, "/a/b.b") == 6);
  28. assert(match_prefix("**.a$|**.b$", 11, "/a/b.a") == 6);
  29. }
  30. static void test_remove_double_dots() {
  31. struct { char before[20], after[20]; } data[] = {
  32. {"////a", "/a"},
  33. {"/.....", "/."},
  34. {"/......", "/"},
  35. {"...", "..."},
  36. {"/...///", "/./"},
  37. {"/a...///", "/a.../"},
  38. {"/.x", "/.x"},
  39. #if defined(_WIN32)
  40. {"/\\", "/"},
  41. #else
  42. {"/\\", "/\\"},
  43. #endif
  44. {"/a\\", "/a\\"},
  45. };
  46. size_t i;
  47. for (i = 0; i < ARRAY_SIZE(data); i++) {
  48. //printf("[%s] -> [%s]\n", data[i].before, data[i].after);
  49. remove_double_dots_and_double_slashes(data[i].before);
  50. assert(strcmp(data[i].before, data[i].after) == 0);
  51. }
  52. }
  53. int main(void) {
  54. test_match_prefix();
  55. test_remove_double_dots();
  56. return 0;
  57. }