Browse Source

Add unit test for "remove_dot_segments"

bel2125 5 years ago
parent
commit
24262432b7
1 changed files with 37 additions and 24 deletions
  1. 37 24
      unittest/private.c

+ 37 - 24
unittest/private.c

@@ -323,33 +323,47 @@ START_TEST(test_match_prefix)
 END_TEST
 
 
-START_TEST(test_remove_double_dots_and_double_slashes)
+START_TEST(test_remove_dot_segments)
 {
-	/* Adapted from unit_test.c */
-	/* Copyright (c) 2013-2015 the Civetweb developers */
-	/* Copyright (c) 2004-2013 Sergey Lyubka */
 	struct {
-		char before[20], after[20];
-	} data[] = {
-	    {"////a", "/a"},
-	    {"/.....", "/."},
-	    {"/......", "/"},
-	    {"..", "."},
-	    {"...", "."},
-	    {"/...///", "/./"},
-	    {"/a...///", "/a.../"},
-	    {"/.x", "/.x"},
-	    {"/\\", "/"},
-	    {"/a\\", "/a\\"},
-	    {"/a\\\\...", "/a\\."},
+		const char *input;
+		const char *expected_output;
+	} tests[] = {{"/path/to/file.ext", "/path/to/file.ext"},
+	             {"/file.ext", "/file.ext"},
+	             {"/path/../file.ext", "/file.ext"},
+	             {"/../to/file.ext", "/to/file.ext"},
+	             {"/../../file.ext", "/file.ext"},
+	             {"/./../file.ext", "/file.ext"},
+	             {"/.././file.ext", "/file.ext"},
+	             {"/././file.ext", "/file.ext"},
+	             {"/././file.ext", "/file.ext"},
+	             {"/path/.to/..file.ext", "/path/.to/..file.ext"},
+	             {"/file", "/file"},
+	             {"/path/", "/path/"},
+
+	             {"file.ext", "file.ext"},
+	             {"./file.ext", "file.ext"},
+	             {"../file.ext", "file.ext"},
+	             {".file.ext", ".file.ext"},
+	             {"..file.ext", "..file.ext"},
+	             {"file", "file"},
+
+	             /* Windows specific */
+	             {"\\file.ext", "/file.ext"},
+	             {"\\..\\file.ext", "/file.ext"},
+	             {"/file.", "/file"},
+	             {"/path\\to.\\.\\file.", "/path/to/file"},
+
+	             {NULL, NULL}
 	};
-	size_t i;
 
 	mark_point();
 
-	for (i = 0; i < ARRAY_SIZE(data); i++) {
-		remove_double_dots_and_double_slashes(data[i].before);
-		ck_assert_str_eq(data[i].before, data[i].after);
+	for (int i = 0; (tests[i].input != NULL); i++) {
+		char inout[256];
+		strcpy(inout, tests[i].input);
+		remove_dot_segments(inout);
+		ck_assert_str_eq(inout, tests[i].expected_output);
 	}
 }
 END_TEST
@@ -1106,8 +1120,7 @@ make_private_suite(void)
 	tcase_set_timeout(tcase_url_parsing_1, civetweb_min_test_timeout);
 	suite_add_tcase(suite, tcase_url_parsing_1);
 
-	tcase_add_test(tcase_url_parsing_2,
-	               test_remove_double_dots_and_double_slashes);
+	tcase_add_test(tcase_url_parsing_2, test_remove_dot_segments);
 	tcase_set_timeout(tcase_url_parsing_2, civetweb_min_test_timeout);
 	suite_add_tcase(suite, tcase_url_parsing_2);
 
@@ -1178,7 +1191,7 @@ MAIN_PRIVATE(void)
 
 	test_alloc_vprintf(0);
 	test_mg_vsnprintf(0);
-	test_remove_double_dots_and_double_slashes(0);
+	test_remove_dot_segments(0);
 	test_parse_date_string(0);
 	test_parse_port_string(0);
 	test_parse_http_message(0);