chase: handle root path more carefully in chase_and_open()

chase_and_open() may be called with relative root path.
This commit is contained in:
Yu Watanabe
2023-06-05 15:50:46 +09:00
parent c0dff4205b
commit 3991f35fbd
3 changed files with 72 additions and 8 deletions
+26
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");