tal/path: new module
[ccan] / ccan / tal / path / test / run-canon.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, *path2, *ctx = tal_strdup(NULL, "ctx");
8
9         plan_tests(15);
10
11         if (!getcwd(cwd, sizeof(cwd)))
12                 abort();
13
14         unlink("run-canon-link");
15         rmdir("run-canon-foo");
16         if (mkdir("run-canon-foo", 0700) != 0)
17                 abort();
18         if (symlink("run-canon-foo", "run-canon-link") != 0)
19                 abort();
20
21         path = path_canon(ctx, "run-canon-foo");
22         ok1(tal_parent(path) == ctx);
23         ok1(strends(path, "run-canon-foo"));
24         ok1(strstarts(path, cwd));
25         ok1(path[strlen(cwd)] == PATH_SEP);
26         ok1(strlen(path) == strlen(cwd) + 1 + strlen("run-canon-foo"));
27         tal_free(path);
28
29         ok1(!path_canon(ctx, take(NULL)));
30         ok1(tal_first(ctx) == NULL);
31
32         /* Test take doesn't leak. */
33         ok1(tal_first(ctx) == NULL);
34         path = path_canon(ctx, take(tal_strdup(ctx, "run-canon-foo")));
35         ok1(strends(path, "run-canon-foo"));
36         ok1(strstarts(path, cwd));
37         ok1(path[strlen(cwd)] == PATH_SEP);
38         ok1(strlen(path) == strlen(cwd) + 1 + strlen("run-canon-foo"));
39         ok1(tal_first(ctx) == path && tal_next(ctx, path) == NULL);
40         path2 = path_canon(ctx, "run-canon-link");
41         ok1(streq(path2, path));
42
43         unlink("run-canon-link");
44         if (symlink(".", "run-canon-link") != 0)
45                 abort();
46
47         path = path_canon(ctx, "run-canon-link");
48         ok1(streq(path, cwd));
49
50         tal_free(ctx);
51
52         return exit_status();
53 }