tal/path: new module
[ccan] / ccan / tal / path / test / run-simplify.c
1 #include <ccan/tal/path/path.h>
2 #include <ccan/tal/path/path.c>
3 #include <ccan/tap/tap.h>
4
5 int main(void)
6 {
7         char cwd[1024], *path, *ctx = tal_strdup(NULL, "ctx");
8
9         plan_tests(87);
10
11         if (!getcwd(cwd, sizeof(cwd)))
12                 abort();
13
14         rmdir("run-simplify-foo");
15         unlink("run-simplify-link");
16         if (mkdir("run-simplify-foo", 0700) != 0)
17                 abort();
18         if (symlink("run-simplify-foo", "run-simplify-link") != 0)
19                 abort();
20
21         /* Handling of . and .. */
22         path = path_simplify(ctx, ".");
23         ok1(streq(path, "."));
24         ok1(tal_parent(path) == ctx);
25         tal_free(path);
26
27         path = path_simplify(ctx, "./");
28         ok1(streq(path, "."));
29         ok1(tal_parent(path) == ctx);
30         tal_free(path);
31
32         path = path_simplify(ctx, "..");
33         ok1(streq(path, ".."));
34         ok1(tal_parent(path) == ctx);
35         tal_free(path);
36
37         path = path_simplify(ctx, "../");
38         ok1(streq(path, ".."));
39         ok1(tal_parent(path) == ctx);
40         tal_free(path);
41
42         path = path_simplify(ctx, "./..");
43         ok1(streq(path, ".."));
44         ok1(tal_parent(path) == ctx);
45         tal_free(path);
46
47         path = path_simplify(ctx, "./../");
48         ok1(streq(path, ".."));
49         ok1(tal_parent(path) == ctx);
50         tal_free(path);
51
52         path = path_simplify(ctx, "./../.");
53         ok1(streq(path, ".."));
54         ok1(tal_parent(path) == ctx);
55         tal_free(path);
56
57         path = path_simplify(ctx, "./.././");
58         ok1(streq(path, ".."));
59         ok1(tal_parent(path) == ctx);
60         tal_free(path);
61
62         path = path_simplify(ctx, "./../..");
63         ok1(streq(path, "../.."));
64         ok1(tal_parent(path) == ctx);
65         tal_free(path);
66
67         path = path_simplify(ctx, "./../../");
68         ok1(streq(path, "../.."));
69         ok1(tal_parent(path) == ctx);
70         tal_free(path);
71
72         /* Handling of /. and /.. */
73         path = path_simplify(ctx, "/");
74         ok1(streq(path, "/"));
75         ok1(tal_parent(path) == ctx);
76         tal_free(path);
77
78         path = path_simplify(ctx, "//");
79         ok1(streq(path, "/"));
80         ok1(tal_parent(path) == ctx);
81         tal_free(path);
82
83         path = path_simplify(ctx, "/.");
84         ok1(streq(path, "/"));
85         ok1(tal_parent(path) == ctx);
86         tal_free(path);
87
88         path = path_simplify(ctx, "/./");
89         ok1(streq(path, "/"));
90         ok1(tal_parent(path) == ctx);
91         tal_free(path);
92
93         path = path_simplify(ctx, "/..");
94         ok1(streq(path, "/"));
95         ok1(tal_parent(path) == ctx);
96         tal_free(path);
97
98         path = path_simplify(ctx, "/../");
99         ok1(streq(path, "/"));
100         ok1(tal_parent(path) == ctx);
101         tal_free(path);
102
103         path = path_simplify(ctx, "/./..");
104         ok1(streq(path, "/"));
105         ok1(tal_parent(path) == ctx);
106         tal_free(path);
107
108         path = path_simplify(ctx, "/./../");
109         ok1(streq(path, "/"));
110         ok1(tal_parent(path) == ctx);
111         tal_free(path);
112
113         path = path_simplify(ctx, "/./../.");
114         ok1(streq(path, "/"));
115         ok1(tal_parent(path) == ctx);
116         tal_free(path);
117
118         path = path_simplify(ctx, "/./.././");
119         ok1(streq(path, "/"));
120         ok1(tal_parent(path) == ctx);
121         tal_free(path);
122
123         path = path_simplify(ctx, "/./../..");
124         ok1(streq(path, "/"));
125         ok1(tal_parent(path) == ctx);
126         tal_free(path);
127
128         path = path_simplify(ctx, "/./../../");
129         ok1(streq(path, "/"));
130         ok1(tal_parent(path) == ctx);
131         tal_free(path);
132
133         /* Don't trace back over a symlink link */
134         path = path_simplify(ctx, "run-simplify-foo");
135         ok1(streq(path, "run-simplify-foo"));
136         ok1(tal_parent(path) == ctx);
137         tal_free(path);
138
139         path = path_simplify(ctx, "./run-simplify-foo");
140         ok1(streq(path, "run-simplify-foo"));
141         ok1(tal_parent(path) == ctx);
142         tal_free(path);
143
144         path = path_simplify(ctx, "./run-simplify-foo/.");
145         ok1(streq(path, "run-simplify-foo"));
146         ok1(tal_parent(path) == ctx);
147         tal_free(path);
148
149         path = path_simplify(ctx, "run-simplify-link");
150         ok1(streq(path, "run-simplify-link"));
151         ok1(tal_parent(path) == ctx);
152         tal_free(path);
153
154         path = path_simplify(ctx, "./run-simplify-link");
155         ok1(streq(path, "run-simplify-link"));
156         ok1(tal_parent(path) == ctx);
157         tal_free(path);
158
159         path = path_simplify(ctx, "./run-simplify-link/.");
160         ok1(streq(path, "run-simplify-link"));
161         ok1(tal_parent(path) == ctx);
162         tal_free(path);
163
164         path = path_simplify(ctx, "run-simplify-foo/..");
165         ok1(streq(path, "."));
166         ok1(tal_parent(path) == ctx);
167         tal_free(path);
168
169         path = path_simplify(ctx, "run-simplify-foo//..");
170         ok1(streq(path, "."));
171         ok1(tal_parent(path) == ctx);
172         tal_free(path);
173
174         path = path_simplify(ctx, "run-simplify-foo//../");
175         ok1(streq(path, "."));
176         ok1(tal_parent(path) == ctx);
177         tal_free(path);
178
179         /* This is expected to be a real directory. */
180         path = path_simplify(ctx, "/tmp");
181         ok1(streq(path, "/tmp"));
182         ok1(tal_parent(path) == ctx);
183         tal_free(path);
184
185         path = path_simplify(ctx, "/tmp/");
186         ok1(streq(path, "/tmp"));
187         ok1(tal_parent(path) == ctx);
188         tal_free(path);
189
190         path = path_simplify(ctx, "/tmp/.");
191         ok1(streq(path, "/tmp"));
192         ok1(tal_parent(path) == ctx);
193         tal_free(path);
194
195         path = path_simplify(ctx, "/./tmp/.");
196         ok1(streq(path, "/tmp"));
197         ok1(tal_parent(path) == ctx);
198         tal_free(path);
199
200         path = path_simplify(ctx, "/../tmp/.");
201         ok1(streq(path, "/tmp"));
202         ok1(tal_parent(path) == ctx);
203         tal_free(path);
204
205         path = path_simplify(ctx, "/tmp/..");
206         ok1(streq(path, "/"));
207         ok1(tal_parent(path) == ctx);
208         tal_free(path);
209
210         path = path_simplify(ctx, "/tmp/../");
211         ok1(streq(path, "/"));
212         ok1(tal_parent(path) == ctx);
213         tal_free(path);
214
215         path = path_simplify(ctx, "/tmp/../tmp");
216         ok1(streq(path, "/tmp"));
217         ok1(tal_parent(path) == ctx);
218         tal_free(path);
219
220         path = path_simplify(ctx, "/tmp/../tmp/");
221         ok1(streq(path, "/tmp"));
222         ok1(tal_parent(path) == ctx);
223         tal_free(path);
224
225         path = path_simplify(ctx, "/tmp/../tmp/.");
226         ok1(streq(path, "/tmp"));
227         ok1(tal_parent(path) == ctx);
228         tal_free(path);
229
230         /* take tests */
231         path = path_simplify(ctx, take(tal_strdup(ctx, "/tmp/../tmp/.")));
232         ok1(streq(path, "/tmp"));
233         ok1(tal_parent(path) == ctx);
234         tal_free(path);
235         ok1(tal_first(ctx) == NULL);
236
237         path = path_simplify(ctx, take(NULL));
238         ok1(!path);
239         ok1(tal_first(ctx) == NULL);
240
241         tal_free(ctx);
242
243         return exit_status();
244 }