Blame


1 1c11b35c 2019-01-12 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 1c11b35c 2019-01-12 stsp *
4 1c11b35c 2019-01-12 stsp * Permission to use, copy, modify, and distribute this software for any
5 1c11b35c 2019-01-12 stsp * purpose with or without fee is hereby granted, provided that the above
6 1c11b35c 2019-01-12 stsp * copyright notice and this permission notice appear in all copies.
7 1c11b35c 2019-01-12 stsp *
8 1c11b35c 2019-01-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 1c11b35c 2019-01-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 1c11b35c 2019-01-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 1c11b35c 2019-01-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 1c11b35c 2019-01-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 1c11b35c 2019-01-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 1c11b35c 2019-01-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 1c11b35c 2019-01-12 stsp */
16 09e05ed5 2023-03-08 thomas
17 09e05ed5 2023-03-08 thomas #include "got_compat.h"
18 1c11b35c 2019-01-12 stsp
19 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
20 e08cc72d 2019-02-05 stsp
21 e08cc72d 2019-02-05 stsp #include <string.h>
22 1c11b35c 2019-01-12 stsp #include <stdlib.h>
23 1c11b35c 2019-01-12 stsp #include <stdarg.h>
24 1c11b35c 2019-01-12 stsp #include <stdio.h>
25 1c11b35c 2019-01-12 stsp #include <unistd.h>
26 1c11b35c 2019-01-12 stsp #include <err.h>
27 1c11b35c 2019-01-12 stsp
28 1c11b35c 2019-01-12 stsp #include "got_error.h"
29 324d37e7 2019-05-11 stsp #include "got_path.h"
30 1c11b35c 2019-01-12 stsp
31 1c11b35c 2019-01-12 stsp #ifndef nitems
32 1c11b35c 2019-01-12 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
33 1c11b35c 2019-01-12 stsp #endif
34 1c11b35c 2019-01-12 stsp
35 1c11b35c 2019-01-12 stsp static int verbose;
36 7fb414ae 2020-08-08 stsp static int quiet;
37 1c11b35c 2019-01-12 stsp
38 ef20f542 2022-06-26 thomas static void
39 0c6f49ba 2022-07-01 thomas test_printf(const char *fmt, ...)
40 1c11b35c 2019-01-12 stsp {
41 1c11b35c 2019-01-12 stsp va_list ap;
42 1c11b35c 2019-01-12 stsp
43 1c11b35c 2019-01-12 stsp if (!verbose)
44 1c11b35c 2019-01-12 stsp return;
45 1c11b35c 2019-01-12 stsp
46 1c11b35c 2019-01-12 stsp va_start(ap, fmt);
47 1c11b35c 2019-01-12 stsp vprintf(fmt, ap);
48 1c11b35c 2019-01-12 stsp va_end(ap);
49 1c11b35c 2019-01-12 stsp }
50 1c11b35c 2019-01-12 stsp
51 1c11b35c 2019-01-12 stsp static int
52 1beed999 2019-01-12 stsp path_cmp(void)
53 1c11b35c 2019-01-12 stsp {
54 fa8129f7 2022-03-22 thomas const struct path_cmp_test {
55 1c11b35c 2019-01-12 stsp const char *path1;
56 1c11b35c 2019-01-12 stsp const char *path2;
57 1c11b35c 2019-01-12 stsp int expected;
58 1c11b35c 2019-01-12 stsp } test_data[] = {
59 1c11b35c 2019-01-12 stsp { "", "", 0 },
60 1c11b35c 2019-01-12 stsp { "/", "/", 0 },
61 1c11b35c 2019-01-12 stsp { "/a", "/b", -1 },
62 1c11b35c 2019-01-12 stsp { "x/a", "x.a", -1 },
63 1c11b35c 2019-01-12 stsp { "x.a", "x/a", 1 },
64 e08cc72d 2019-02-05 stsp { "//foo", "/bar", 1 },
65 1c11b35c 2019-01-12 stsp { "/foo", "/bar", 1 },
66 e08cc72d 2019-02-05 stsp { "foo", "bar", 1 },
67 1c11b35c 2019-01-12 stsp { "/foo/sub", "/bar", 1 },
68 1c11b35c 2019-01-12 stsp { "/foo", "/bar/sub", 1 },
69 1c11b35c 2019-01-12 stsp { "/foo/", "/bar", 1 },
70 1c11b35c 2019-01-12 stsp { "/foo", "/bar/", 1 },
71 1c11b35c 2019-01-12 stsp { "/foo/", "/bar/", 1 },
72 1c11b35c 2019-01-12 stsp { "/bar/", "/bar/", 0 },
73 e08cc72d 2019-02-05 stsp { "/bar/", "/bar", 0 },
74 e08cc72d 2019-02-05 stsp { "//bar//", "/bar/", 0 },
75 e08cc72d 2019-02-05 stsp { "//bar//", "/bar////", 0 },
76 e08cc72d 2019-02-05 stsp { "/bar/sub", "/bar.", -1 },
77 1c11b35c 2019-01-12 stsp { "/bar/sub", "/bar/", 1 },
78 e08cc72d 2019-02-05 stsp { "/bar/sub/", "/bar///", 1 },
79 1c11b35c 2019-01-12 stsp { "/bar/sub/sub2", "/bar/", 1 },
80 1c11b35c 2019-01-12 stsp { "/bar/sub/sub2", "/bar", 1 },
81 1c11b35c 2019-01-12 stsp { "/bar.sub.sub2", "/bar", 1 },
82 1c11b35c 2019-01-12 stsp { "/bar/sub/sub2", "/bar.c", -1 },
83 1c11b35c 2019-01-12 stsp };
84 6059809a 2020-12-17 stsp size_t i;
85 1c11b35c 2019-01-12 stsp
86 1c11b35c 2019-01-12 stsp for (i = 0; i < nitems(test_data); i++) {
87 1c11b35c 2019-01-12 stsp const char *path1 = test_data[i].path1;
88 1c11b35c 2019-01-12 stsp const char *path2 = test_data[i].path2;
89 1c11b35c 2019-01-12 stsp int expected = test_data[i].expected;
90 d572f586 2019-08-02 stsp int cmp = got_path_cmp(path1, path2,
91 d572f586 2019-08-02 stsp strlen(path1), strlen(path2));
92 1c11b35c 2019-01-12 stsp
93 1c11b35c 2019-01-12 stsp if (cmp != expected) {
94 1c11b35c 2019-01-12 stsp test_printf("%d: '%s' vs '%s' == %d; expected %d\n",
95 1c11b35c 2019-01-12 stsp i, path1, path2, cmp, expected);
96 1c11b35c 2019-01-12 stsp return 0;
97 1c11b35c 2019-01-12 stsp }
98 1c11b35c 2019-01-12 stsp }
99 1c11b35c 2019-01-12 stsp
100 1c11b35c 2019-01-12 stsp return 1;
101 1c11b35c 2019-01-12 stsp }
102 1c11b35c 2019-01-12 stsp
103 e08cc72d 2019-02-05 stsp const char *path_list_input[] = {
104 e08cc72d 2019-02-05 stsp "", "/", "a", "/b", "/bar", "bar/sub", "/bar/sub", "/bar/",
105 e08cc72d 2019-02-05 stsp "/bar.c", "/bar/sub/sub2", "/bar.sub.sub2", "/foo",
106 e08cc72d 2019-02-05 stsp "/foo/sub", "/foo/", "/foo/", "x/a",
107 e08cc72d 2019-02-05 stsp };
108 e08cc72d 2019-02-05 stsp const char *path_list_expected[] = {
109 e08cc72d 2019-02-05 stsp "",
110 e08cc72d 2019-02-05 stsp "a",
111 e08cc72d 2019-02-05 stsp "/b",
112 e08cc72d 2019-02-05 stsp "/bar",
113 e08cc72d 2019-02-05 stsp "bar/sub",
114 e08cc72d 2019-02-05 stsp "/bar/sub/sub2",
115 e08cc72d 2019-02-05 stsp "/bar.c",
116 e08cc72d 2019-02-05 stsp "/bar.sub.sub2",
117 e08cc72d 2019-02-05 stsp "/foo",
118 e08cc72d 2019-02-05 stsp "/foo/sub",
119 e08cc72d 2019-02-05 stsp "x/a",
120 e08cc72d 2019-02-05 stsp };
121 e08cc72d 2019-02-05 stsp
122 e08cc72d 2019-02-05 stsp /* If inserting pathlist_input in reverse the result is slightly different. */
123 e08cc72d 2019-02-05 stsp const char *path_list_expected_reverse[] = {
124 e08cc72d 2019-02-05 stsp "/",
125 e08cc72d 2019-02-05 stsp "a",
126 e08cc72d 2019-02-05 stsp "/b",
127 e08cc72d 2019-02-05 stsp "/bar/",
128 e08cc72d 2019-02-05 stsp "/bar/sub",
129 e08cc72d 2019-02-05 stsp "/bar/sub/sub2",
130 e08cc72d 2019-02-05 stsp "/bar.c",
131 e08cc72d 2019-02-05 stsp "/bar.sub.sub2",
132 e08cc72d 2019-02-05 stsp "/foo/",
133 e08cc72d 2019-02-05 stsp "/foo/sub",
134 e08cc72d 2019-02-05 stsp "x/a",
135 e08cc72d 2019-02-05 stsp };
136 e08cc72d 2019-02-05 stsp
137 e08cc72d 2019-02-05 stsp
138 e08cc72d 2019-02-05 stsp static int
139 e08cc72d 2019-02-05 stsp path_list(void)
140 e08cc72d 2019-02-05 stsp {
141 e08cc72d 2019-02-05 stsp const struct got_error *err = NULL;
142 e08cc72d 2019-02-05 stsp struct got_pathlist_head paths;
143 e08cc72d 2019-02-05 stsp struct got_pathlist_entry *pe;
144 6059809a 2020-12-17 stsp size_t i;
145 e08cc72d 2019-02-05 stsp
146 e08cc72d 2019-02-05 stsp TAILQ_INIT(&paths);
147 e08cc72d 2019-02-05 stsp for (i = 0; i < nitems(path_list_input); i++) {
148 3d8df59c 2019-02-05 stsp err = got_pathlist_insert(NULL, &paths, path_list_input[i],
149 3d8df59c 2019-02-05 stsp NULL);
150 e08cc72d 2019-02-05 stsp if (err) {
151 e08cc72d 2019-02-05 stsp test_printf("%s\n", __func__, err->msg);
152 e08cc72d 2019-02-05 stsp return 0;
153 e08cc72d 2019-02-05 stsp }
154 e08cc72d 2019-02-05 stsp }
155 e08cc72d 2019-02-05 stsp
156 e08cc72d 2019-02-05 stsp i = 0;
157 e08cc72d 2019-02-05 stsp TAILQ_FOREACH(pe, &paths, entry) {
158 e08cc72d 2019-02-05 stsp test_printf("'%s' -- '%s'\n", pe->path, path_list_expected[i]);
159 e08cc72d 2019-02-05 stsp if (i >= nitems(path_list_expected)) {
160 e08cc72d 2019-02-05 stsp test_printf("too many elements on list\n");
161 e08cc72d 2019-02-05 stsp return 0;
162 e08cc72d 2019-02-05 stsp }
163 e08cc72d 2019-02-05 stsp if (strcmp(pe->path, path_list_expected[i]) != 0) {
164 e08cc72d 2019-02-05 stsp test_printf("unordered elements on list\n");
165 e08cc72d 2019-02-05 stsp return 0;
166 e08cc72d 2019-02-05 stsp }
167 e08cc72d 2019-02-05 stsp i++;
168 e08cc72d 2019-02-05 stsp }
169 e08cc72d 2019-02-05 stsp
170 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
171 e08cc72d 2019-02-05 stsp return 1;
172 e08cc72d 2019-02-05 stsp }
173 e08cc72d 2019-02-05 stsp
174 e08cc72d 2019-02-05 stsp static int
175 e08cc72d 2019-02-05 stsp path_list_reverse_input(void)
176 e08cc72d 2019-02-05 stsp {
177 e08cc72d 2019-02-05 stsp const struct got_error *err = NULL;
178 e08cc72d 2019-02-05 stsp struct got_pathlist_head paths;
179 e08cc72d 2019-02-05 stsp struct got_pathlist_entry *pe;
180 6059809a 2020-12-17 stsp size_t i;
181 e08cc72d 2019-02-05 stsp
182 e08cc72d 2019-02-05 stsp TAILQ_INIT(&paths);
183 a4153d5b 2020-12-17 stsp for (i = nitems(path_list_input); i > 0;) {
184 a4153d5b 2020-12-17 stsp err = got_pathlist_insert(NULL, &paths, path_list_input[--i],
185 3d8df59c 2019-02-05 stsp NULL);
186 e08cc72d 2019-02-05 stsp if (err) {
187 e08cc72d 2019-02-05 stsp test_printf("%s\n", __func__, err->msg);
188 e08cc72d 2019-02-05 stsp return 0;
189 e08cc72d 2019-02-05 stsp }
190 e08cc72d 2019-02-05 stsp }
191 e08cc72d 2019-02-05 stsp
192 e08cc72d 2019-02-05 stsp i = 0;
193 e08cc72d 2019-02-05 stsp TAILQ_FOREACH(pe, &paths, entry) {
194 e08cc72d 2019-02-05 stsp test_printf("'%s' -- '%s'\n", pe->path,
195 e08cc72d 2019-02-05 stsp path_list_expected_reverse[i]);
196 e08cc72d 2019-02-05 stsp if (i >= nitems(path_list_expected_reverse)) {
197 e08cc72d 2019-02-05 stsp test_printf("too many elements on list\n");
198 e08cc72d 2019-02-05 stsp return 0;
199 e08cc72d 2019-02-05 stsp }
200 e08cc72d 2019-02-05 stsp if (strcmp(pe->path, path_list_expected_reverse[i]) != 0) {
201 e08cc72d 2019-02-05 stsp test_printf("unordered elements on list\n");
202 e08cc72d 2019-02-05 stsp return 0;
203 e08cc72d 2019-02-05 stsp }
204 e08cc72d 2019-02-05 stsp i++;
205 e08cc72d 2019-02-05 stsp }
206 e08cc72d 2019-02-05 stsp
207 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
208 e08cc72d 2019-02-05 stsp return 1;
209 e08cc72d 2019-02-05 stsp }
210 e08cc72d 2019-02-05 stsp
211 1c11b35c 2019-01-12 stsp #define RUN_TEST(expr, name) \
212 1c11b35c 2019-01-12 stsp { test_ok = (expr); \
213 7fb414ae 2020-08-08 stsp if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
214 1c11b35c 2019-01-12 stsp failure = (failure || !test_ok); }
215 1c11b35c 2019-01-12 stsp
216 ef20f542 2022-06-26 thomas static void
217 1c11b35c 2019-01-12 stsp usage(void)
218 1c11b35c 2019-01-12 stsp {
219 7fb414ae 2020-08-08 stsp fprintf(stderr, "usage: path_test [-v] [-q]\n");
220 1c11b35c 2019-01-12 stsp }
221 1c11b35c 2019-01-12 stsp
222 1c11b35c 2019-01-12 stsp int
223 1c11b35c 2019-01-12 stsp main(int argc, char *argv[])
224 1c11b35c 2019-01-12 stsp {
225 1c11b35c 2019-01-12 stsp int test_ok = 0, failure = 0;
226 1c11b35c 2019-01-12 stsp int ch;
227 1c11b35c 2019-01-12 stsp
228 1c11b35c 2019-01-12 stsp #ifndef PROFILE
229 1c11b35c 2019-01-12 stsp if (pledge("stdio", NULL) == -1)
230 1c11b35c 2019-01-12 stsp err(1, "pledge");
231 1c11b35c 2019-01-12 stsp #endif
232 1c11b35c 2019-01-12 stsp
233 f7065961 2022-10-27 thomas while ((ch = getopt(argc, argv, "qv")) != -1) {
234 1c11b35c 2019-01-12 stsp switch (ch) {
235 7fb414ae 2020-08-08 stsp case 'q':
236 7fb414ae 2020-08-08 stsp quiet = 1;
237 7fb414ae 2020-08-08 stsp verbose = 0;
238 7fb414ae 2020-08-08 stsp break;
239 f7065961 2022-10-27 thomas case 'v':
240 f7065961 2022-10-27 thomas verbose = 1;
241 f7065961 2022-10-27 thomas quiet = 0;
242 f7065961 2022-10-27 thomas break;
243 1c11b35c 2019-01-12 stsp default:
244 1c11b35c 2019-01-12 stsp usage();
245 1c11b35c 2019-01-12 stsp return 1;
246 1c11b35c 2019-01-12 stsp }
247 1c11b35c 2019-01-12 stsp }
248 1c11b35c 2019-01-12 stsp argc -= optind;
249 1c11b35c 2019-01-12 stsp argv += optind;
250 1c11b35c 2019-01-12 stsp
251 1beed999 2019-01-12 stsp RUN_TEST(path_cmp(), "path_cmp");
252 e08cc72d 2019-02-05 stsp RUN_TEST(path_list(), "path_list");
253 e08cc72d 2019-02-05 stsp RUN_TEST(path_list_reverse_input(), "path_list_reverse_input");
254 1c11b35c 2019-01-12 stsp
255 1c11b35c 2019-01-12 stsp return failure ? 1 : 0;
256 1c11b35c 2019-01-12 stsp }