tal/path: new module
[ccan] / ccan / tal / path / test / run-readlink.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 *link, *ctx = tal_strdup(NULL, "ctx");
8
9         plan_tests(12);
10
11         unlink("run-readlink-link");
12
13         link = path_readlink(ctx, "run-readlink-link");
14         ok1(errno == ENOENT);
15         ok1(!link);
16
17         link = path_readlink(ctx, take(tal_strdup(ctx, "run-readlink-link")));
18         ok1(errno == ENOENT);
19         ok1(!link);
20         ok1(tal_first(ctx) == NULL);
21
22         if (symlink("/tmp", "run-readlink-link") != 0)
23                 abort();
24
25         link = path_readlink(ctx, "run-readlink-link");
26         ok1(tal_parent(link) == ctx);
27         ok1(streq(link, "/tmp"));
28         tal_free(link);
29
30         link = path_readlink(ctx, take(tal_strdup(ctx, "run-readlink-link")));
31         ok1(tal_parent(link) == ctx);
32         ok1(streq(link, "/tmp"));
33         ok1(tal_first(ctx) == link && tal_next(ctx, link) == NULL);
34
35         unlink("run-readlink-link");
36
37         if (symlink("some-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-long-name", "run-readlink-link") != 0)
38                 abort();
39
40         link = path_readlink(ctx, "run-readlink-link");
41         ok1(tal_parent(link) == ctx);
42         ok1(streq(link, "some-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-long-name"));
43         tal_free(ctx);
44
45         return exit_status();
46 }