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