utf8: don't allow NUL in decoded strings.
[ccan] / tools / ccanlint / tests / depends_exist.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/str/str.h>
4 #include <ccan/tal/path/path.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <limits.h>
10 #include <errno.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <err.h>
14 #include <string.h>
15 #include <ctype.h>
16
17 static bool have_dep(struct manifest *m, const char *dep)
18 {
19         struct manifest *i;
20
21         list_for_each(&m->deps, i, list)
22                 if (streq(i->modname, dep + strlen("ccan/")))
23                         return true;
24
25         return false;
26 }
27
28 static bool add_dep(struct manifest *m,
29                     struct list_head *deplist,
30                     const char *dep, struct score *score)
31 {
32         struct stat st;
33         struct manifest *subm;
34         char *dir = path_join(m, ccan_dir, dep);
35
36         /* FIXME: get_manifest has a tendency to exit. */
37         if (stat(dir, &st) != 0) {
38                 score->error = tal_fmt(m, "Could not stat dependency %s: %s",
39                                        dir, strerror(errno));
40                 return false;
41         }
42         subm = get_manifest(m, dir);
43         list_add_tail(deplist, &subm->list);
44         return true;
45 }
46
47 /* FIXME: check this is still true once we reduce features. */
48 static void check_depends_exist(struct manifest *m,
49                                 unsigned int *timeleft UNNEEDED,
50                                 struct score *score)
51 {
52         unsigned int i;
53         char **deps;
54
55         if (safe_mode)
56                 deps = get_safe_ccan_deps(m, m->dir, "depends", true);
57         else
58                 deps = get_deps(m, m->dir, "depends", true,
59                                 get_or_compile_info);
60
61         if (!deps) {
62                 score->error = tal_fmt(m, "Could not extract dependencies");
63                 return;
64         }
65
66         for (i = 0; deps[i]; i++) {
67                 if (!strstarts(deps[i], "ccan/")) {
68                         non_ccan_deps = true;
69                         continue;
70                 }
71
72                 if (!add_dep(m, &m->deps, deps[i], score))
73                         return;
74         }
75
76         score->pass = true;
77         score->score = score->total;
78 }
79
80 static void check_test_depends_exist(struct manifest *m,
81                                      unsigned int *timeleft UNNEEDED,
82                                      struct score *score)
83 {
84         unsigned int i;
85         char **deps;
86         bool needs_tap;
87
88         /* We may need libtap for testing, unless we're "tap" */
89         if (streq(m->modname, "tap")) {
90                 needs_tap = false;
91         } else if (list_empty(&m->run_tests) && list_empty(&m->api_tests)) {
92                 needs_tap = false;
93         } else {
94                 needs_tap = true;
95         }
96
97         if (safe_mode)
98                 deps = get_safe_ccan_deps(m, m->dir, "testdepends", true);
99         else
100                 deps = get_deps(m, m->dir, "testdepends", true,
101                                 get_or_compile_info);
102
103         for (i = 0; deps[i]; i++) {
104                 if (!strstarts(deps[i], "ccan/"))
105                         continue;
106
107                 /* Don't add dependency twice: we can only be on one list!
108                  * Also, it's possible to have circular test depends, so drop
109                  * self-refs. */
110                 if (!have_dep(m, deps[i])
111                     && !streq(deps[i] + strlen("ccan/"), m->modname)
112                     && !add_dep(m, &m->test_deps, deps[i], score))
113                         return;
114
115                 if (streq(deps[i], "ccan/tap")) {
116                         needs_tap = false;
117                 }
118         }
119
120         if (needs_tap && !have_dep(m, "ccan/tap")
121             && !add_dep(m, &m->test_deps, "ccan/tap", score)) {
122                 return;
123         }
124
125         score->pass = true;
126         score->score = score->total;
127 }
128
129 struct ccanlint depends_exist = {
130         .key = "depends_exist",
131         .name = "Module's CCAN dependencies can be found",
132         .compulsory = true,
133         .check = check_depends_exist,
134         .needs = "info_compiles"
135 };
136
137 REGISTER_TEST(depends_exist);
138
139 struct ccanlint test_depends_exist = {
140         .key = "test_depends_exist",
141         .name = "Module's CCAN test dependencies can be found",
142         .compulsory = false,
143         .check = check_test_depends_exist,
144         .needs = "depends_exist"
145 };
146
147 REGISTER_TEST(test_depends_exist);