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 1c11b35c 2019-01-12 stsp
17 e08cc72d 2019-02-05 stsp #include <sys/queue.h>
18 e08cc72d 2019-02-05 stsp
19 e08cc72d 2019-02-05 stsp #include <string.h>
20 1c11b35c 2019-01-12 stsp #include <stdlib.h>
21 1c11b35c 2019-01-12 stsp #include <stdarg.h>
22 1c11b35c 2019-01-12 stsp #include <stdio.h>
23 1c11b35c 2019-01-12 stsp #include <unistd.h>
24 1c11b35c 2019-01-12 stsp #include <err.h>
25 1c11b35c 2019-01-12 stsp
26 1c11b35c 2019-01-12 stsp #include "got_error.h"
27 324d37e7 2019-05-11 stsp #include "got_path.h"
28 1c11b35c 2019-01-12 stsp
29 1c11b35c 2019-01-12 stsp #ifndef nitems
30 1c11b35c 2019-01-12 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
31 1c11b35c 2019-01-12 stsp #endif
32 1c11b35c 2019-01-12 stsp
33 1c11b35c 2019-01-12 stsp static int verbose;
34 7fb414ae 2020-08-08 stsp static int quiet;
35 1c11b35c 2019-01-12 stsp
36 336075a4 2022-06-25 op static void
37 58e31a80 2022-06-27 op test_printf(const char *fmt, ...)
38 1c11b35c 2019-01-12 stsp {
39 1c11b35c 2019-01-12 stsp va_list ap;
40 1c11b35c 2019-01-12 stsp
41 1c11b35c 2019-01-12 stsp if (!verbose)
42 1c11b35c 2019-01-12 stsp return;
43 1c11b35c 2019-01-12 stsp
44 1c11b35c 2019-01-12 stsp va_start(ap, fmt);
45 1c11b35c 2019-01-12 stsp vprintf(fmt, ap);
46 1c11b35c 2019-01-12 stsp va_end(ap);
47 1c11b35c 2019-01-12 stsp }
48 1c11b35c 2019-01-12 stsp
49 1c11b35c 2019-01-12 stsp static int
50 1beed999 2019-01-12 stsp path_cmp(void)
51 1c11b35c 2019-01-12 stsp {
52 d58ddaf3 2022-03-17 naddy const struct path_cmp_test {
53 1c11b35c 2019-01-12 stsp const char *path1;
54 1c11b35c 2019-01-12 stsp const char *path2;
55 1c11b35c 2019-01-12 stsp int expected;
56 1c11b35c 2019-01-12 stsp } test_data[] = {
57 1c11b35c 2019-01-12 stsp { "", "", 0 },
58 1c11b35c 2019-01-12 stsp { "/", "/", 0 },
59 1c11b35c 2019-01-12 stsp { "/a", "/b", -1 },
60 1c11b35c 2019-01-12 stsp { "x/a", "x.a", -1 },
61 1c11b35c 2019-01-12 stsp { "x.a", "x/a", 1 },
62 e08cc72d 2019-02-05 stsp { "//foo", "/bar", 1 },
63 1c11b35c 2019-01-12 stsp { "/foo", "/bar", 1 },
64 e08cc72d 2019-02-05 stsp { "foo", "bar", 1 },
65 1c11b35c 2019-01-12 stsp { "/foo/sub", "/bar", 1 },
66 1c11b35c 2019-01-12 stsp { "/foo", "/bar/sub", 1 },
67 1c11b35c 2019-01-12 stsp { "/foo/", "/bar", 1 },
68 1c11b35c 2019-01-12 stsp { "/foo", "/bar/", 1 },
69 1c11b35c 2019-01-12 stsp { "/foo/", "/bar/", 1 },
70 1c11b35c 2019-01-12 stsp { "/bar/", "/bar/", 0 },
71 e08cc72d 2019-02-05 stsp { "/bar/", "/bar", 0 },
72 e08cc72d 2019-02-05 stsp { "//bar//", "/bar/", 0 },
73 e08cc72d 2019-02-05 stsp { "//bar//", "/bar////", 0 },
74 e08cc72d 2019-02-05 stsp { "/bar/sub", "/bar.", -1 },
75 1c11b35c 2019-01-12 stsp { "/bar/sub", "/bar/", 1 },
76 e08cc72d 2019-02-05 stsp { "/bar/sub/", "/bar///", 1 },
77 1c11b35c 2019-01-12 stsp { "/bar/sub/sub2", "/bar/", 1 },
78 1c11b35c 2019-01-12 stsp { "/bar/sub/sub2", "/bar", 1 },
79 1c11b35c 2019-01-12 stsp { "/bar.sub.sub2", "/bar", 1 },
80 1c11b35c 2019-01-12 stsp { "/bar/sub/sub2", "/bar.c", -1 },
81 1c11b35c 2019-01-12 stsp };
82 6059809a 2020-12-17 stsp size_t i;
83 1c11b35c 2019-01-12 stsp
84 1c11b35c 2019-01-12 stsp for (i = 0; i < nitems(test_data); i++) {
85 1c11b35c 2019-01-12 stsp const char *path1 = test_data[i].path1;
86 1c11b35c 2019-01-12 stsp const char *path2 = test_data[i].path2;
87 1c11b35c 2019-01-12 stsp int expected = test_data[i].expected;
88 d572f586 2019-08-02 stsp int cmp = got_path_cmp(path1, path2,
89 d572f586 2019-08-02 stsp strlen(path1), strlen(path2));
90 1c11b35c 2019-01-12 stsp
91 1c11b35c 2019-01-12 stsp if (cmp != expected) {
92 1c11b35c 2019-01-12 stsp test_printf("%d: '%s' vs '%s' == %d; expected %d\n",
93 1c11b35c 2019-01-12 stsp i, path1, path2, cmp, expected);
94 1c11b35c 2019-01-12 stsp return 0;
95 1c11b35c 2019-01-12 stsp }
96 1c11b35c 2019-01-12 stsp }
97 1c11b35c 2019-01-12 stsp
98 1c11b35c 2019-01-12 stsp return 1;
99 1c11b35c 2019-01-12 stsp }
100 1c11b35c 2019-01-12 stsp
101 e08cc72d 2019-02-05 stsp const char *path_list_input[] = {
102 e08cc72d 2019-02-05 stsp "", "/", "a", "/b", "/bar", "bar/sub", "/bar/sub", "/bar/",
103 e08cc72d 2019-02-05 stsp "/bar.c", "/bar/sub/sub2", "/bar.sub.sub2", "/foo",
104 e08cc72d 2019-02-05 stsp "/foo/sub", "/foo/", "/foo/", "x/a",
105 e08cc72d 2019-02-05 stsp };
106 e08cc72d 2019-02-05 stsp const char *path_list_expected[] = {
107 e08cc72d 2019-02-05 stsp "",
108 e08cc72d 2019-02-05 stsp "a",
109 e08cc72d 2019-02-05 stsp "/b",
110 e08cc72d 2019-02-05 stsp "/bar",
111 e08cc72d 2019-02-05 stsp "bar/sub",
112 e08cc72d 2019-02-05 stsp "/bar/sub/sub2",
113 e08cc72d 2019-02-05 stsp "/bar.c",
114 e08cc72d 2019-02-05 stsp "/bar.sub.sub2",
115 e08cc72d 2019-02-05 stsp "/foo",
116 e08cc72d 2019-02-05 stsp "/foo/sub",
117 e08cc72d 2019-02-05 stsp "x/a",
118 e08cc72d 2019-02-05 stsp };
119 e08cc72d 2019-02-05 stsp
120 e08cc72d 2019-02-05 stsp /* If inserting pathlist_input in reverse the result is slightly different. */
121 e08cc72d 2019-02-05 stsp const char *path_list_expected_reverse[] = {
122 e08cc72d 2019-02-05 stsp "/",
123 e08cc72d 2019-02-05 stsp "a",
124 e08cc72d 2019-02-05 stsp "/b",
125 e08cc72d 2019-02-05 stsp "/bar/",
126 e08cc72d 2019-02-05 stsp "/bar/sub",
127 e08cc72d 2019-02-05 stsp "/bar/sub/sub2",
128 e08cc72d 2019-02-05 stsp "/bar.c",
129 e08cc72d 2019-02-05 stsp "/bar.sub.sub2",
130 e08cc72d 2019-02-05 stsp "/foo/",
131 e08cc72d 2019-02-05 stsp "/foo/sub",
132 e08cc72d 2019-02-05 stsp "x/a",
133 e08cc72d 2019-02-05 stsp };
134 e08cc72d 2019-02-05 stsp
135 e08cc72d 2019-02-05 stsp
136 e08cc72d 2019-02-05 stsp static int
137 e08cc72d 2019-02-05 stsp path_list(void)
138 e08cc72d 2019-02-05 stsp {
139 e08cc72d 2019-02-05 stsp const struct got_error *err = NULL;
140 e08cc72d 2019-02-05 stsp struct got_pathlist_head paths;
141 e08cc72d 2019-02-05 stsp struct got_pathlist_entry *pe;
142 6059809a 2020-12-17 stsp size_t i;
143 e08cc72d 2019-02-05 stsp
144 e08cc72d 2019-02-05 stsp TAILQ_INIT(&paths);
145 e08cc72d 2019-02-05 stsp for (i = 0; i < nitems(path_list_input); i++) {
146 3d8df59c 2019-02-05 stsp err = got_pathlist_insert(NULL, &paths, path_list_input[i],
147 3d8df59c 2019-02-05 stsp NULL);
148 e08cc72d 2019-02-05 stsp if (err) {
149 e08cc72d 2019-02-05 stsp test_printf("%s\n", __func__, err->msg);
150 e08cc72d 2019-02-05 stsp return 0;
151 e08cc72d 2019-02-05 stsp }
152 e08cc72d 2019-02-05 stsp }
153 e08cc72d 2019-02-05 stsp
154 e08cc72d 2019-02-05 stsp i = 0;
155 e08cc72d 2019-02-05 stsp TAILQ_FOREACH(pe, &paths, entry) {
156 e08cc72d 2019-02-05 stsp test_printf("'%s' -- '%s'\n", pe->path, path_list_expected[i]);
157 e08cc72d 2019-02-05 stsp if (i >= nitems(path_list_expected)) {
158 e08cc72d 2019-02-05 stsp test_printf("too many elements on list\n");
159 e08cc72d 2019-02-05 stsp return 0;
160 e08cc72d 2019-02-05 stsp }
161 e08cc72d 2019-02-05 stsp if (strcmp(pe->path, path_list_expected[i]) != 0) {
162 e08cc72d 2019-02-05 stsp test_printf("unordered elements on list\n");
163 e08cc72d 2019-02-05 stsp return 0;
164 e08cc72d 2019-02-05 stsp }
165 e08cc72d 2019-02-05 stsp i++;
166 e08cc72d 2019-02-05 stsp }
167 e08cc72d 2019-02-05 stsp
168 d8bacb93 2023-01-10 mark got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
169 e08cc72d 2019-02-05 stsp return 1;
170 e08cc72d 2019-02-05 stsp }
171 e08cc72d 2019-02-05 stsp
172 e08cc72d 2019-02-05 stsp static int
173 e08cc72d 2019-02-05 stsp path_list_reverse_input(void)
174 e08cc72d 2019-02-05 stsp {
175 e08cc72d 2019-02-05 stsp const struct got_error *err = NULL;
176 e08cc72d 2019-02-05 stsp struct got_pathlist_head paths;
177 e08cc72d 2019-02-05 stsp struct got_pathlist_entry *pe;
178 6059809a 2020-12-17 stsp size_t i;
179 e08cc72d 2019-02-05 stsp
180 e08cc72d 2019-02-05 stsp TAILQ_INIT(&paths);
181 a4153d5b 2020-12-17 stsp for (i = nitems(path_list_input); i > 0;) {
182 a4153d5b 2020-12-17 stsp err = got_pathlist_insert(NULL, &paths, path_list_input[--i],
183 3d8df59c 2019-02-05 stsp NULL);
184 e08cc72d 2019-02-05 stsp if (err) {
185 e08cc72d 2019-02-05 stsp test_printf("%s\n", __func__, err->msg);
186 e08cc72d 2019-02-05 stsp return 0;
187 e08cc72d 2019-02-05 stsp }
188 e08cc72d 2019-02-05 stsp }
189 e08cc72d 2019-02-05 stsp
190 e08cc72d 2019-02-05 stsp i = 0;
191 e08cc72d 2019-02-05 stsp TAILQ_FOREACH(pe, &paths, entry) {
192 e08cc72d 2019-02-05 stsp test_printf("'%s' -- '%s'\n", pe->path,
193 e08cc72d 2019-02-05 stsp path_list_expected_reverse[i]);
194 e08cc72d 2019-02-05 stsp if (i >= nitems(path_list_expected_reverse)) {
195 e08cc72d 2019-02-05 stsp test_printf("too many elements on list\n");
196 e08cc72d 2019-02-05 stsp return 0;
197 e08cc72d 2019-02-05 stsp }
198 e08cc72d 2019-02-05 stsp if (strcmp(pe->path, path_list_expected_reverse[i]) != 0) {
199 e08cc72d 2019-02-05 stsp test_printf("unordered elements on list\n");
200 e08cc72d 2019-02-05 stsp return 0;
201 e08cc72d 2019-02-05 stsp }
202 e08cc72d 2019-02-05 stsp i++;
203 e08cc72d 2019-02-05 stsp }
204 e08cc72d 2019-02-05 stsp
205 d8bacb93 2023-01-10 mark got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
206 e08cc72d 2019-02-05 stsp return 1;
207 e08cc72d 2019-02-05 stsp }
208 e08cc72d 2019-02-05 stsp
209 1c11b35c 2019-01-12 stsp #define RUN_TEST(expr, name) \
210 1c11b35c 2019-01-12 stsp { test_ok = (expr); \
211 7fb414ae 2020-08-08 stsp if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
212 1c11b35c 2019-01-12 stsp failure = (failure || !test_ok); }
213 1c11b35c 2019-01-12 stsp
214 336075a4 2022-06-25 op static void
215 1c11b35c 2019-01-12 stsp usage(void)
216 1c11b35c 2019-01-12 stsp {
217 7fb414ae 2020-08-08 stsp fprintf(stderr, "usage: path_test [-v] [-q]\n");
218 1c11b35c 2019-01-12 stsp }
219 1c11b35c 2019-01-12 stsp
220 1c11b35c 2019-01-12 stsp int
221 1c11b35c 2019-01-12 stsp main(int argc, char *argv[])
222 1c11b35c 2019-01-12 stsp {
223 1c11b35c 2019-01-12 stsp int test_ok = 0, failure = 0;
224 1c11b35c 2019-01-12 stsp int ch;
225 1c11b35c 2019-01-12 stsp
226 1c11b35c 2019-01-12 stsp #ifndef PROFILE
227 1c11b35c 2019-01-12 stsp if (pledge("stdio", NULL) == -1)
228 1c11b35c 2019-01-12 stsp err(1, "pledge");
229 1c11b35c 2019-01-12 stsp #endif
230 1c11b35c 2019-01-12 stsp
231 6f319063 2022-10-27 stsp while ((ch = getopt(argc, argv, "qv")) != -1) {
232 1c11b35c 2019-01-12 stsp switch (ch) {
233 7fb414ae 2020-08-08 stsp case 'q':
234 7fb414ae 2020-08-08 stsp quiet = 1;
235 7fb414ae 2020-08-08 stsp verbose = 0;
236 7fb414ae 2020-08-08 stsp break;
237 6f319063 2022-10-27 stsp case 'v':
238 6f319063 2022-10-27 stsp verbose = 1;
239 6f319063 2022-10-27 stsp quiet = 0;
240 6f319063 2022-10-27 stsp break;
241 1c11b35c 2019-01-12 stsp default:
242 1c11b35c 2019-01-12 stsp usage();
243 1c11b35c 2019-01-12 stsp return 1;
244 1c11b35c 2019-01-12 stsp }
245 1c11b35c 2019-01-12 stsp }
246 1c11b35c 2019-01-12 stsp argc -= optind;
247 1c11b35c 2019-01-12 stsp argv += optind;
248 1c11b35c 2019-01-12 stsp
249 1beed999 2019-01-12 stsp RUN_TEST(path_cmp(), "path_cmp");
250 e08cc72d 2019-02-05 stsp RUN_TEST(path_list(), "path_list");
251 e08cc72d 2019-02-05 stsp RUN_TEST(path_list_reverse_input(), "path_list_reverse_input");
252 1c11b35c 2019-01-12 stsp
253 1c11b35c 2019-01-12 stsp return failure ? 1 : 0;
254 1c11b35c 2019-01-12 stsp }