summaryrefslogtreecommitdiff
path: root/src/test/test-path-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2015-05-13 17:42:10 +0200
committerLennart Poettering <lennart@poettering.net>2015-05-13 17:42:10 +0200
commit1d13f648d0fade38194db74b4f82ca68c8a26856 (patch)
treeb85c22b0007e0c8c6e612b8c9f5606bcd84f0451 /src/test/test-path-util.c
parent8b44a3d22c1fdfc5ce5fcb77e38a90ec02ba8019 (diff)
util: add generic calls for prefixing a root directory to a path
So far a number of utilities implemented their own calls for this, unify them in prefix_root() and prefix_roota(). The former uses heap memory, the latter allocates from the stack via alloca(). Port over most users of a --root= logic.
Diffstat (limited to 'src/test/test-path-util.c')
-rw-r--r--src/test/test-path-util.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c
index e5b9c28bc0..09f0f2f89e 100644
--- a/src/test/test-path-util.c
+++ b/src/test/test-path-util.c
@@ -294,6 +294,34 @@ static void test_path_startswith(void) {
assert_se(!path_startswith("/foo/bar/barfoo/", "/f/b/b/"));
}
+static void test_prefix_root_one(const char *r, const char *p, const char *expected) {
+ _cleanup_free_ char *s = NULL;
+ const char *t;
+
+ assert_se(s = prefix_root(r, p));
+ assert_se(streq_ptr(s, expected));
+
+ t = prefix_roota(r, p);
+ assert_se(t);
+ assert_se(streq_ptr(t, expected));
+}
+
+static void test_prefix_root(void) {
+ test_prefix_root_one("/", "/foo", "/foo");
+ test_prefix_root_one(NULL, "/foo", "/foo");
+ test_prefix_root_one("", "/foo", "/foo");
+ test_prefix_root_one("///", "/foo", "/foo");
+ test_prefix_root_one("/", "////foo", "/foo");
+ test_prefix_root_one(NULL, "////foo", "/foo");
+
+ test_prefix_root_one("/foo", "/bar", "/foo/bar");
+ test_prefix_root_one("/foo", "bar", "/foo/bar");
+ test_prefix_root_one("foo", "bar", "foo/bar");
+ test_prefix_root_one("/foo/", "/bar", "/foo/bar");
+ test_prefix_root_one("/foo/", "//bar", "/foo/bar");
+ test_prefix_root_one("/foo///", "//bar", "/foo/bar");
+}
+
int main(int argc, char **argv) {
test_path();
test_find_binary(argv[0], true);
@@ -304,6 +332,7 @@ int main(int argc, char **argv) {
test_make_relative();
test_strv_resolve();
test_path_startswith();
+ test_prefix_root();
return 0;
}