Merge pull request #27918 from yuwata/chase-filename

chase: handle root path more carefully in chase_and_open()
This commit is contained in:
Daan De Meyer
2023-06-06 15:01:37 +02:00
committed by GitHub
4 changed files with 88 additions and 13 deletions
+31 -5
View File
@@ -17,6 +17,19 @@
static const char *arg_test_dir = NULL;
static void test_chase_extract_filename_one(const char *path, const char *root, const char *expected) {
_cleanup_free_ char *ret1 = NULL, *ret2 = NULL, *fname = NULL;
log_debug("/* %s(path=%s, root=%s) */", __func__, path, strnull(root));
assert_se(chase(path, root, CHASE_EXTRACT_FILENAME, &ret1, NULL) > 0);
assert_se(streq(ret1, expected));
assert_se(chase(path, root, 0, &ret2, NULL) > 0);
assert_se(chase_extract_filename(ret2, root, &fname) >= 0);
assert_se(streq(fname, expected));
}
TEST(chase) {
_cleanup_free_ char *result = NULL, *pwd = NULL;
_cleanup_close_ int pfd = -EBADF;
@@ -111,6 +124,19 @@ TEST(chase) {
assert_se(path_equal(result, temp));
result = mfree(result);
/* Tests for CHASE_EXTRACT_FILENAME and chase_extract_filename() */
p = strjoina(temp, "/start");
pslash = strjoina(p, "/");
test_chase_extract_filename_one(p, NULL, "usr");
test_chase_extract_filename_one(pslash, NULL, "usr");
test_chase_extract_filename_one(p, temp, "usr");
test_chase_extract_filename_one(pslash, temp, "usr");
p = strjoina(temp, "/slash");
test_chase_extract_filename_one(p, NULL, ".");
test_chase_extract_filename_one(p, temp, ".");
/* Paths that would "escape" outside of the "root" */
p = strjoina(temp, "/6dots");
@@ -643,11 +669,6 @@ TEST(chaseat) {
result = mfree(result);
}
static int intro(void) {
arg_test_dir = saved_argv[1];
return EXIT_SUCCESS;
}
TEST(chaseat_prefix_root) {
_cleanup_free_ char *cwd = NULL, *ret = NULL, *expected = NULL;
@@ -685,4 +706,9 @@ TEST(chaseat_prefix_root) {
assert_se(streq(ret, expected));
}
static int intro(void) {
arg_test_dir = saved_argv[1];
return EXIT_SUCCESS;
}
DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);
+11
View File
@@ -600,6 +600,17 @@ TEST(path_startswith) {
test_path_startswith_one("/foo/bar/barfoo/", "////foo/bar/barfoo/", "/foo/bar/barfoo/", "");
test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfoo", "/foo/bar/barfoo/", "");
test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo", "/foo/./", "bar///barfoo/./.");
test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo/", "/foo/./", "bar///barfoo/./.");
test_path_startswith_one("/foo/./bar///barfoo/./.", "/", "/", "foo/./bar///barfoo/./.");
test_path_startswith_one("/foo/./bar///barfoo/./.", "////", "/", "foo/./bar///barfoo/./.");
test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo//bar/////barfoo///", "/foo/./bar///barfoo/./.", "");
test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo/bar/barfoo////", "/foo/./bar///barfoo/./.", "");
test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo/bar///barfoo/", "/foo/./bar///barfoo/./.", "");
test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo////bar/barfoo/", "/foo/./bar///barfoo/./.", "");
test_path_startswith_one("/foo/./bar///barfoo/./.", "////foo/bar/barfoo/", "/foo/./bar///barfoo/./.", "");
test_path_startswith_one("/foo/./bar///barfoo/./.", "/foo/bar/barfoo", "/foo/./bar///barfoo/./.", "");
test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfooa/", NULL, NULL);
test_path_startswith_one("/foo/bar/barfoo/", "/foo/bar/barfooa", NULL, NULL);
test_path_startswith_one("/foo/bar/barfoo/", "", NULL, NULL);