chiark / gitweb /
util: add macro for iterating through all prefixes of a path
[elogind.git] / src / test / test-path-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Zbigniew JÄ™drzejewski-Szmek
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <stdio.h>
23
24 #include "path-util.h"
25 #include "util.h"
26 #include "macro.h"
27
28
29 static void test_path(void) {
30         assert_se(path_equal("/goo", "/goo"));
31         assert_se(path_equal("//goo", "/goo"));
32         assert_se(path_equal("//goo/////", "/goo"));
33         assert_se(path_equal("goo/////", "goo"));
34
35         assert_se(path_equal("/goo/boo", "/goo//boo"));
36         assert_se(path_equal("//goo/boo", "/goo/boo//"));
37
38         assert_se(path_equal("/", "///"));
39
40         assert_se(!path_equal("/x", "x/"));
41         assert_se(!path_equal("x/", "/"));
42
43         assert_se(!path_equal("/x/./y", "x/y"));
44         assert_se(!path_equal("x/.y", "x/y"));
45
46         assert_se(path_is_absolute("/"));
47         assert_se(!path_is_absolute("./"));
48
49         assert_se(is_path("/dir"));
50         assert_se(is_path("a/b"));
51         assert_se(!is_path("."));
52
53         assert_se(streq(path_get_file_name("./aa/bb/../file.da."), "file.da."));
54         assert_se(streq(path_get_file_name("/aa///.file"), ".file"));
55         assert_se(streq(path_get_file_name("/aa///file..."), "file..."));
56         assert_se(streq(path_get_file_name("file.../"), ""));
57
58 #define test_parent(x, y) {                                \
59                 char _cleanup_free_ *z = NULL;             \
60                 int r = path_get_parent(x, &z);            \
61                 printf("expected: %s\n", y ? y : "error"); \
62                 printf("actual: %s\n", r<0 ? "error" : z); \
63                 assert_se((y==NULL) ^ (r==0));             \
64                 assert_se(y==NULL || path_equal(z, y));    \
65         }
66
67         test_parent("./aa/bb/../file.da.", "./aa/bb/..");
68         test_parent("/aa///.file", "/aa///");
69         test_parent("/aa///file...", "/aa///");
70         test_parent("file.../", NULL);
71
72         assert_se(path_is_mount_point("/", true));
73         assert_se(path_is_mount_point("/", false));
74
75         {
76                 char p1[] = "aaa/bbb////ccc";
77                 char p2[] = "//aaa/.////ccc";
78                 char p3[] = "/./";
79
80                 assert(path_equal(path_kill_slashes(p1), "aaa/bbb/ccc"));
81                 assert(path_equal(path_kill_slashes(p2), "/aaa/./ccc"));
82                 assert(path_equal(path_kill_slashes(p3), "/./"));
83         }
84 }
85
86 static void test_find_binary(void) {
87         char *p;
88
89         assert(find_binary("/bin/sh", &p) == 0);
90         puts(p);
91         assert(streq(p, "/bin/sh"));
92         free(p);
93
94         assert(find_binary("./test-path-util", &p) == 0);
95         puts(p);
96         assert(endswith(p, "/test-path-util"));
97         assert(path_is_absolute(p));
98         free(p);
99
100         assert(find_binary("sh", &p) == 0);
101         puts(p);
102         assert(endswith(p, "/sh"));
103         assert(path_is_absolute(p));
104         free(p);
105
106         assert(find_binary("xxxx-xxxx", &p) == -ENOENT);
107 }
108
109 static void test_prefixes(void) {
110         static const char* values[] = { "/a/b/c", "/a/b", "/a", "", NULL};
111         unsigned i = 0;
112         char s[PATH_MAX];
113
114         PATH_FOREACH_PREFIX(s, "/a/b/c/d") {
115                 log_error("---%s---", s);
116                 assert_se(streq(s, values[i++]));
117         }
118
119         assert_se(values[i] == NULL);
120
121         i = 0;
122         PATH_FOREACH_PREFIX(s, "////a////b////c///d///////")
123                 assert_se(streq(s, values[i++]));
124
125         assert_se(values[i] == NULL);
126
127         PATH_FOREACH_PREFIX(s, "////")
128                 assert_se(streq(s, ""));
129
130         PATH_FOREACH_PREFIX(s, "")
131                 assert_not_reached("wut?");
132
133 }
134
135 int main(void) {
136         test_path();
137         test_find_binary();
138         test_prefixes();
139         return 0;
140 }