2 aaa0878e 2019-01-08 stsp * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
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>
24 aaa0878e 2019-01-08 stsp #include "got_error.h"
26 aaa0878e 2019-01-08 stsp #include "got_lib_pathset.h"
28 aaa0878e 2019-01-08 stsp static int verbose;
31 aaa0878e 2019-01-08 stsp test_printf(char *fmt, ...)
35 aaa0878e 2019-01-08 stsp if (!verbose)
38 aaa0878e 2019-01-08 stsp va_start(ap, fmt);
39 aaa0878e 2019-01-08 stsp vprintf(fmt, ap);
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";
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)
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;
54 aaa0878e 2019-01-08 stsp return NULL; /* not reached */
58 aaa0878e 2019-01-08 stsp pathset_add_remove_iter(void)
60 aaa0878e 2019-01-08 stsp const struct got_error *err = NULL;
61 aaa0878e 2019-01-08 stsp struct got_pathset *set;
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();
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);
74 aaa0878e 2019-01-08 stsp err = got_pathset_add(set, path1, (void *)data1);
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);
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);
87 aaa0878e 2019-01-08 stsp err = got_pathset_add(set, path2, (void *)data2);
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);
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);
99 aaa0878e 2019-01-08 stsp err = got_pathset_add(set, path3, (void *)data3);
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);
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);
111 aaa0878e 2019-01-08 stsp err = got_pathset_remove(NULL, set, path2);
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);
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);
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);
127 8f6d9a6a 2019-01-08 stsp got_pathset_for_each_safe(set, pathset_add_remove_iter_cb, NULL);
129 aaa0878e 2019-01-08 stsp got_pathset_free(set);
130 aaa0878e 2019-01-08 stsp return (err == NULL);
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)
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)
140 aaa0878e 2019-01-08 stsp if (i == 1 && strcmp(path, "/usr.bin") != 0)
142 aaa0878e 2019-01-08 stsp if (i == 2 && strcmp(path, "/usr.bin/vi") != 0)
144 aaa0878e 2019-01-08 stsp if (i == 3 && strcmp(path, "/usr.sbin") != 0)
146 aaa0878e 2019-01-08 stsp if (i == 4 && strcmp(path, "/usr.sbin/unbound") != 0)
148 aaa0878e 2019-01-08 stsp if (i == 5 && strcmp(path, "/usr.sbin/zic") != 0)
153 aaa0878e 2019-01-08 stsp return NULL;
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)
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)
163 efaf56b7 2019-01-08 stsp if (i == 1 && strcmp(path, "/usr.sbin/unbound") != 0)
165 efaf56b7 2019-01-08 stsp if (i == 2 && strcmp(path, "/usr.sbin") != 0)
167 efaf56b7 2019-01-08 stsp if (i == 3 && strcmp(path, "/usr.bin/vi") != 0)
169 efaf56b7 2019-01-08 stsp if (i == 4 && strcmp(path, "/usr.bin") != 0)
171 efaf56b7 2019-01-08 stsp if (i == 5 && strcmp(path, "/") != 0)
176 efaf56b7 2019-01-08 stsp return NULL;
180 efaf56b7 2019-01-08 stsp pathset_iter_order(void)
182 aaa0878e 2019-01-08 stsp const struct got_error *err = NULL;
183 aaa0878e 2019-01-08 stsp struct got_pathset *set;
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();
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);
196 aaa0878e 2019-01-08 stsp err = got_pathset_add(set, "/usr.bin", (void *)data1);
199 aaa0878e 2019-01-08 stsp err = got_pathset_add(set, "/usr.sbin/unbound", (void *)data1);
202 aaa0878e 2019-01-08 stsp err = got_pathset_add(set, "/usr.bin/vi", (void *)data1);
205 aaa0878e 2019-01-08 stsp err = got_pathset_add(set, "/", (void *)data1);
208 aaa0878e 2019-01-08 stsp err = got_pathset_add(set, "/usr.sbin/zic", (void *)data1);
211 aaa0878e 2019-01-08 stsp err = got_pathset_add(set, "/usr.sbin", (void *)data1);
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,
221 aaa0878e 2019-01-08 stsp got_pathset_free(set);
222 aaa0878e 2019-01-08 stsp return (err == NULL);
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); }
231 aaa0878e 2019-01-08 stsp usage(void)
233 aaa0878e 2019-01-08 stsp fprintf(stderr, "usage: pathset_test [-v]\n");
237 aaa0878e 2019-01-08 stsp main(int argc, char *argv[])
239 aaa0878e 2019-01-08 stsp int test_ok = 0, failure = 0;
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");
247 aaa0878e 2019-01-08 stsp while ((ch = getopt(argc, argv, "v")) != -1) {
248 aaa0878e 2019-01-08 stsp switch (ch) {
250 aaa0878e 2019-01-08 stsp verbose = 1;
257 aaa0878e 2019-01-08 stsp argc -= optind;
258 aaa0878e 2019-01-08 stsp argv += optind;
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");
263 aaa0878e 2019-01-08 stsp return failure ? 1 : 0;