Blame


1 aaa0878e 2019-01-08 stsp /*
2 aaa0878e 2019-01-08 stsp * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
3 aaa0878e 2019-01-08 stsp *
4 aaa0878e 2019-01-08 stsp * Permission to use, copy, modify, and distribute this software for any
5 aaa0878e 2019-01-08 stsp * purpose with or without fee is hereby granted, provided that the above
6 aaa0878e 2019-01-08 stsp * copyright notice and this permission notice appear in all copies.
7 aaa0878e 2019-01-08 stsp *
8 aaa0878e 2019-01-08 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 aaa0878e 2019-01-08 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 aaa0878e 2019-01-08 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 aaa0878e 2019-01-08 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 aaa0878e 2019-01-08 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 aaa0878e 2019-01-08 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 aaa0878e 2019-01-08 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 aaa0878e 2019-01-08 stsp */
16 aaa0878e 2019-01-08 stsp
17 aaa0878e 2019-01-08 stsp #include <stdarg.h>
18 aaa0878e 2019-01-08 stsp #include <string.h>
19 aaa0878e 2019-01-08 stsp #include <stdio.h>
20 aaa0878e 2019-01-08 stsp #include <stdlib.h>
21 aaa0878e 2019-01-08 stsp #include <unistd.h>
22 aaa0878e 2019-01-08 stsp #include <err.h>
23 aaa0878e 2019-01-08 stsp
24 aaa0878e 2019-01-08 stsp #include "got_error.h"
25 aaa0878e 2019-01-08 stsp
26 aaa0878e 2019-01-08 stsp #include "got_lib_pathset.h"
27 aaa0878e 2019-01-08 stsp
28 aaa0878e 2019-01-08 stsp static int verbose;
29 aaa0878e 2019-01-08 stsp
30 aaa0878e 2019-01-08 stsp void
31 aaa0878e 2019-01-08 stsp test_printf(char *fmt, ...)
32 aaa0878e 2019-01-08 stsp {
33 aaa0878e 2019-01-08 stsp va_list ap;
34 aaa0878e 2019-01-08 stsp
35 aaa0878e 2019-01-08 stsp if (!verbose)
36 aaa0878e 2019-01-08 stsp return;
37 aaa0878e 2019-01-08 stsp
38 aaa0878e 2019-01-08 stsp va_start(ap, fmt);
39 aaa0878e 2019-01-08 stsp vprintf(fmt, ap);
40 aaa0878e 2019-01-08 stsp va_end(ap);
41 aaa0878e 2019-01-08 stsp }
42 aaa0878e 2019-01-08 stsp
43 aaa0878e 2019-01-08 stsp static const char *path1 = "/", *path2 = "/usr", *path3 = "/usr/bin";
44 aaa0878e 2019-01-08 stsp static const char *data1 = "data1", *data2 = "data2", *data3 = "data3";
45 aaa0878e 2019-01-08 stsp
46 aaa0878e 2019-01-08 stsp static const struct got_error *
47 ab847d68 2019-01-08 stsp pathset_add_remove_iter_cb(const char *path, void *data, void *arg)
48 ab847d68 2019-01-08 stsp {
49 aaa0878e 2019-01-08 stsp test_printf("%s\n", path);
50 aaa0878e 2019-01-08 stsp if ((strcmp(path, path1) == 0 && data == (void *)data1) ||
51 aaa0878e 2019-01-08 stsp (strcmp(path, path3) == 0 && data == (void *)data3))
52 aaa0878e 2019-01-08 stsp return NULL;
53 aaa0878e 2019-01-08 stsp abort();
54 aaa0878e 2019-01-08 stsp return NULL; /* not reached */
55 aaa0878e 2019-01-08 stsp }
56 aaa0878e 2019-01-08 stsp
57 aaa0878e 2019-01-08 stsp static int
58 aaa0878e 2019-01-08 stsp pathset_add_remove_iter(void)
59 aaa0878e 2019-01-08 stsp {
60 aaa0878e 2019-01-08 stsp const struct got_error *err = NULL;
61 aaa0878e 2019-01-08 stsp struct got_pathset *set;
62 aaa0878e 2019-01-08 stsp
63 aaa0878e 2019-01-08 stsp set = got_pathset_alloc();
64 aaa0878e 2019-01-08 stsp if (set == NULL) {
65 aaa0878e 2019-01-08 stsp err = got_error_from_errno();
66 aaa0878e 2019-01-08 stsp goto done;
67 aaa0878e 2019-01-08 stsp }
68 aaa0878e 2019-01-08 stsp if (got_pathset_num_elements(set) != 0) {
69 aaa0878e 2019-01-08 stsp err = got_error(GOT_ERR_BAD_PATH);
70 aaa0878e 2019-01-08 stsp goto done;
71 aaa0878e 2019-01-08 stsp }
72 aaa0878e 2019-01-08 stsp
73 aaa0878e 2019-01-08 stsp
74 aaa0878e 2019-01-08 stsp err = got_pathset_add(set, path1, (void *)data1);
75 aaa0878e 2019-01-08 stsp if (err)
76 aaa0878e 2019-01-08 stsp goto done;
77 aaa0878e 2019-01-08 stsp if (got_pathset_num_elements(set) != 1) {
78 aaa0878e 2019-01-08 stsp err = got_error(GOT_ERR_BAD_PATH);
79 aaa0878e 2019-01-08 stsp goto done;
80 aaa0878e 2019-01-08 stsp }
81 aaa0878e 2019-01-08 stsp
82 aaa0878e 2019-01-08 stsp if (!got_pathset_contains(set, path1)) {
83 aaa0878e 2019-01-08 stsp err = got_error(GOT_ERR_BAD_PATH);
84 aaa0878e 2019-01-08 stsp goto done;
85 aaa0878e 2019-01-08 stsp }
86 aaa0878e 2019-01-08 stsp
87 aaa0878e 2019-01-08 stsp err = got_pathset_add(set, path2, (void *)data2);
88 aaa0878e 2019-01-08 stsp if (err)
89 aaa0878e 2019-01-08 stsp goto done;
90 aaa0878e 2019-01-08 stsp if (!got_pathset_contains(set, path2)) {
91 aaa0878e 2019-01-08 stsp err = got_error(GOT_ERR_BAD_PATH);
92 aaa0878e 2019-01-08 stsp goto done;
93 aaa0878e 2019-01-08 stsp }
94 aaa0878e 2019-01-08 stsp if (got_pathset_num_elements(set) != 2) {
95 aaa0878e 2019-01-08 stsp err = got_error(GOT_ERR_BAD_PATH);
96 aaa0878e 2019-01-08 stsp goto done;
97 aaa0878e 2019-01-08 stsp }
98 aaa0878e 2019-01-08 stsp
99 aaa0878e 2019-01-08 stsp err = got_pathset_add(set, path3, (void *)data3);
100 aaa0878e 2019-01-08 stsp if (err)
101 aaa0878e 2019-01-08 stsp goto done;
102 aaa0878e 2019-01-08 stsp if (got_pathset_get(set, path3) != (void *)data3) {
103 aaa0878e 2019-01-08 stsp err = got_error(GOT_ERR_BAD_PATH);
104 aaa0878e 2019-01-08 stsp goto done;
105 aaa0878e 2019-01-08 stsp }
106 aaa0878e 2019-01-08 stsp if (got_pathset_num_elements(set) != 3) {
107 aaa0878e 2019-01-08 stsp err = got_error(GOT_ERR_BAD_PATH);
108 aaa0878e 2019-01-08 stsp goto done;
109 aaa0878e 2019-01-08 stsp }
110 aaa0878e 2019-01-08 stsp
111 aaa0878e 2019-01-08 stsp err = got_pathset_remove(NULL, set, path2);
112 aaa0878e 2019-01-08 stsp if (err)
113 aaa0878e 2019-01-08 stsp goto done;
114 aaa0878e 2019-01-08 stsp if (got_pathset_num_elements(set) != 2) {
115 aaa0878e 2019-01-08 stsp err = got_error(GOT_ERR_BAD_PATH);
116 aaa0878e 2019-01-08 stsp goto done;
117 aaa0878e 2019-01-08 stsp }
118 aaa0878e 2019-01-08 stsp if (got_pathset_contains(set, path2)) {
119 aaa0878e 2019-01-08 stsp err = got_error(GOT_ERR_BAD_PATH);
120 aaa0878e 2019-01-08 stsp goto done;
121 aaa0878e 2019-01-08 stsp }
122 aaa0878e 2019-01-08 stsp if (got_pathset_get(set, path2) != NULL) {
123 aaa0878e 2019-01-08 stsp err = got_error(GOT_ERR_BAD_PATH);
124 aaa0878e 2019-01-08 stsp goto done;
125 aaa0878e 2019-01-08 stsp }
126 aaa0878e 2019-01-08 stsp
127 8f6d9a6a 2019-01-08 stsp got_pathset_for_each_safe(set, pathset_add_remove_iter_cb, NULL);
128 aaa0878e 2019-01-08 stsp done:
129 aaa0878e 2019-01-08 stsp got_pathset_free(set);
130 aaa0878e 2019-01-08 stsp return (err == NULL);
131 aaa0878e 2019-01-08 stsp }
132 aaa0878e 2019-01-08 stsp
133 aaa0878e 2019-01-08 stsp static const struct got_error *
134 efaf56b7 2019-01-08 stsp pathset_iter_order_cb(const char *path, void *data, void *arg)
135 efaf56b7 2019-01-08 stsp {
136 aaa0878e 2019-01-08 stsp static int i;
137 aaa0878e 2019-01-08 stsp test_printf("%s\n", path);
138 aaa0878e 2019-01-08 stsp if (i == 0 && strcmp(path, "/") != 0)
139 aaa0878e 2019-01-08 stsp abort();
140 aaa0878e 2019-01-08 stsp if (i == 1 && strcmp(path, "/usr.bin") != 0)
141 aaa0878e 2019-01-08 stsp abort();
142 aaa0878e 2019-01-08 stsp if (i == 2 && strcmp(path, "/usr.bin/vi") != 0)
143 aaa0878e 2019-01-08 stsp abort();
144 aaa0878e 2019-01-08 stsp if (i == 3 && strcmp(path, "/usr.sbin") != 0)
145 aaa0878e 2019-01-08 stsp abort();
146 aaa0878e 2019-01-08 stsp if (i == 4 && strcmp(path, "/usr.sbin/unbound") != 0)
147 aaa0878e 2019-01-08 stsp abort();
148 aaa0878e 2019-01-08 stsp if (i == 5 && strcmp(path, "/usr.sbin/zic") != 0)
149 aaa0878e 2019-01-08 stsp abort();
150 aaa0878e 2019-01-08 stsp if (i > 5)
151 aaa0878e 2019-01-08 stsp abort();
152 aaa0878e 2019-01-08 stsp i++;
153 aaa0878e 2019-01-08 stsp return NULL;
154 aaa0878e 2019-01-08 stsp }
155 aaa0878e 2019-01-08 stsp
156 efaf56b7 2019-01-08 stsp static const struct got_error *
157 efaf56b7 2019-01-08 stsp pathset_iter_reverse_order_cb(const char *path, void *data, void *arg)
158 efaf56b7 2019-01-08 stsp {
159 efaf56b7 2019-01-08 stsp static int i;
160 efaf56b7 2019-01-08 stsp test_printf("%s\n", path);
161 efaf56b7 2019-01-08 stsp if (i == 0 && strcmp(path, "/usr.sbin/zic") != 0)
162 efaf56b7 2019-01-08 stsp abort();
163 efaf56b7 2019-01-08 stsp if (i == 1 && strcmp(path, "/usr.sbin/unbound") != 0)
164 efaf56b7 2019-01-08 stsp abort();
165 efaf56b7 2019-01-08 stsp if (i == 2 && strcmp(path, "/usr.sbin") != 0)
166 efaf56b7 2019-01-08 stsp abort();
167 efaf56b7 2019-01-08 stsp if (i == 3 && strcmp(path, "/usr.bin/vi") != 0)
168 efaf56b7 2019-01-08 stsp abort();
169 efaf56b7 2019-01-08 stsp if (i == 4 && strcmp(path, "/usr.bin") != 0)
170 efaf56b7 2019-01-08 stsp abort();
171 efaf56b7 2019-01-08 stsp if (i == 5 && strcmp(path, "/") != 0)
172 efaf56b7 2019-01-08 stsp abort();
173 efaf56b7 2019-01-08 stsp if (i > 5)
174 efaf56b7 2019-01-08 stsp abort();
175 efaf56b7 2019-01-08 stsp i++;
176 efaf56b7 2019-01-08 stsp return NULL;
177 efaf56b7 2019-01-08 stsp }
178 efaf56b7 2019-01-08 stsp
179 aaa0878e 2019-01-08 stsp static int
180 efaf56b7 2019-01-08 stsp pathset_iter_order(void)
181 aaa0878e 2019-01-08 stsp {
182 aaa0878e 2019-01-08 stsp const struct got_error *err = NULL;
183 aaa0878e 2019-01-08 stsp struct got_pathset *set;
184 aaa0878e 2019-01-08 stsp
185 aaa0878e 2019-01-08 stsp set = got_pathset_alloc();
186 aaa0878e 2019-01-08 stsp if (set == NULL) {
187 aaa0878e 2019-01-08 stsp err = got_error_from_errno();
188 aaa0878e 2019-01-08 stsp goto done;
189 aaa0878e 2019-01-08 stsp }
190 aaa0878e 2019-01-08 stsp if (got_pathset_num_elements(set) != 0) {
191 aaa0878e 2019-01-08 stsp err = got_error(GOT_ERR_BAD_PATH);
192 aaa0878e 2019-01-08 stsp goto done;
193 aaa0878e 2019-01-08 stsp }
194 aaa0878e 2019-01-08 stsp
195 aaa0878e 2019-01-08 stsp
196 aaa0878e 2019-01-08 stsp err = got_pathset_add(set, "/usr.bin", (void *)data1);
197 aaa0878e 2019-01-08 stsp if (err)
198 aaa0878e 2019-01-08 stsp goto done;
199 aaa0878e 2019-01-08 stsp err = got_pathset_add(set, "/usr.sbin/unbound", (void *)data1);
200 aaa0878e 2019-01-08 stsp if (err)
201 aaa0878e 2019-01-08 stsp goto done;
202 aaa0878e 2019-01-08 stsp err = got_pathset_add(set, "/usr.bin/vi", (void *)data1);
203 aaa0878e 2019-01-08 stsp if (err)
204 aaa0878e 2019-01-08 stsp goto done;
205 aaa0878e 2019-01-08 stsp err = got_pathset_add(set, "/", (void *)data1);
206 aaa0878e 2019-01-08 stsp if (err)
207 aaa0878e 2019-01-08 stsp goto done;
208 aaa0878e 2019-01-08 stsp err = got_pathset_add(set, "/usr.sbin/zic", (void *)data1);
209 aaa0878e 2019-01-08 stsp if (err)
210 aaa0878e 2019-01-08 stsp goto done;
211 aaa0878e 2019-01-08 stsp err = got_pathset_add(set, "/usr.sbin", (void *)data1);
212 aaa0878e 2019-01-08 stsp if (err)
213 aaa0878e 2019-01-08 stsp goto done;
214 aaa0878e 2019-01-08 stsp
215 efaf56b7 2019-01-08 stsp test_printf("normal order:\n");
216 8f6d9a6a 2019-01-08 stsp got_pathset_for_each_safe(set, pathset_iter_order_cb, NULL);
217 efaf56b7 2019-01-08 stsp test_printf("reverse order:\n");
218 8f6d9a6a 2019-01-08 stsp got_pathset_for_each_reverse_safe(set, pathset_iter_reverse_order_cb,
219 efaf56b7 2019-01-08 stsp NULL);
220 aaa0878e 2019-01-08 stsp done:
221 aaa0878e 2019-01-08 stsp got_pathset_free(set);
222 aaa0878e 2019-01-08 stsp return (err == NULL);
223 aaa0878e 2019-01-08 stsp }
224 aaa0878e 2019-01-08 stsp
225 aaa0878e 2019-01-08 stsp #define RUN_TEST(expr, name) \
226 aaa0878e 2019-01-08 stsp { test_ok = (expr); \
227 aaa0878e 2019-01-08 stsp printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
228 aaa0878e 2019-01-08 stsp failure = (failure || !test_ok); }
229 aaa0878e 2019-01-08 stsp
230 aaa0878e 2019-01-08 stsp void
231 aaa0878e 2019-01-08 stsp usage(void)
232 aaa0878e 2019-01-08 stsp {
233 aaa0878e 2019-01-08 stsp fprintf(stderr, "usage: pathset_test [-v]\n");
234 aaa0878e 2019-01-08 stsp }
235 aaa0878e 2019-01-08 stsp
236 aaa0878e 2019-01-08 stsp int
237 aaa0878e 2019-01-08 stsp main(int argc, char *argv[])
238 aaa0878e 2019-01-08 stsp {
239 aaa0878e 2019-01-08 stsp int test_ok = 0, failure = 0;
240 aaa0878e 2019-01-08 stsp int ch;
241 aaa0878e 2019-01-08 stsp
242 aaa0878e 2019-01-08 stsp #ifndef PROFILE
243 aaa0878e 2019-01-08 stsp if (pledge("stdio", NULL) == -1)
244 aaa0878e 2019-01-08 stsp err(1, "pledge");
245 aaa0878e 2019-01-08 stsp #endif
246 aaa0878e 2019-01-08 stsp
247 aaa0878e 2019-01-08 stsp while ((ch = getopt(argc, argv, "v")) != -1) {
248 aaa0878e 2019-01-08 stsp switch (ch) {
249 aaa0878e 2019-01-08 stsp case 'v':
250 aaa0878e 2019-01-08 stsp verbose = 1;
251 aaa0878e 2019-01-08 stsp break;
252 aaa0878e 2019-01-08 stsp default:
253 aaa0878e 2019-01-08 stsp usage();
254 aaa0878e 2019-01-08 stsp return 1;
255 aaa0878e 2019-01-08 stsp }
256 aaa0878e 2019-01-08 stsp }
257 aaa0878e 2019-01-08 stsp argc -= optind;
258 aaa0878e 2019-01-08 stsp argv += optind;
259 aaa0878e 2019-01-08 stsp
260 aaa0878e 2019-01-08 stsp RUN_TEST(pathset_add_remove_iter(), "pathset_add_remove_iter");
261 efaf56b7 2019-01-08 stsp RUN_TEST(pathset_iter_order(), "pathset_iter_order");
262 aaa0878e 2019-01-08 stsp
263 aaa0878e 2019-01-08 stsp return failure ? 1 : 0;
264 aaa0878e 2019-01-08 stsp }