2 * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 #include "got_error.h"
26 #include "got_lib_pathset.h"
31 test_printf(char *fmt, ...)
43 static const char *path1 = "/", *path2 = "/usr", *path3 = "/usr/bin";
44 static const char *data1 = "data1", *data2 = "data2", *data3 = "data3";
46 static const struct got_error *
47 pathset_add_remove_iter_cb(const char *path, void *data, void *arg) {
48 test_printf("%s\n", path);
49 if ((strcmp(path, path1) == 0 && data == (void *)data1) ||
50 (strcmp(path, path3) == 0 && data == (void *)data3))
53 return NULL; /* not reached */
57 pathset_add_remove_iter(void)
59 const struct got_error *err = NULL;
60 struct got_pathset *set;
62 set = got_pathset_alloc();
64 err = got_error_from_errno();
67 if (got_pathset_num_elements(set) != 0) {
68 err = got_error(GOT_ERR_BAD_PATH);
73 err = got_pathset_add(set, path1, (void *)data1);
76 if (got_pathset_num_elements(set) != 1) {
77 err = got_error(GOT_ERR_BAD_PATH);
81 if (!got_pathset_contains(set, path1)) {
82 err = got_error(GOT_ERR_BAD_PATH);
86 err = got_pathset_add(set, path2, (void *)data2);
89 if (!got_pathset_contains(set, path2)) {
90 err = got_error(GOT_ERR_BAD_PATH);
93 if (got_pathset_num_elements(set) != 2) {
94 err = got_error(GOT_ERR_BAD_PATH);
98 err = got_pathset_add(set, path3, (void *)data3);
101 if (got_pathset_get(set, path3) != (void *)data3) {
102 err = got_error(GOT_ERR_BAD_PATH);
105 if (got_pathset_num_elements(set) != 3) {
106 err = got_error(GOT_ERR_BAD_PATH);
110 err = got_pathset_remove(NULL, set, path2);
113 if (got_pathset_num_elements(set) != 2) {
114 err = got_error(GOT_ERR_BAD_PATH);
117 if (got_pathset_contains(set, path2)) {
118 err = got_error(GOT_ERR_BAD_PATH);
121 if (got_pathset_get(set, path2) != NULL) {
122 err = got_error(GOT_ERR_BAD_PATH);
126 got_pathset_for_each(set, pathset_add_remove_iter_cb, NULL);
128 got_pathset_free(set);
129 return (err == NULL);
132 static const struct got_error *
133 pathset_iter_order_cb(const char *path, void *data, void *arg)
136 test_printf("%s\n", path);
137 if (i == 0 && strcmp(path, "/") != 0)
139 if (i == 1 && strcmp(path, "/usr.bin") != 0)
141 if (i == 2 && strcmp(path, "/usr.bin/vi") != 0)
143 if (i == 3 && strcmp(path, "/usr.sbin") != 0)
145 if (i == 4 && strcmp(path, "/usr.sbin/unbound") != 0)
147 if (i == 5 && strcmp(path, "/usr.sbin/zic") != 0)
155 static const struct got_error *
156 pathset_iter_reverse_order_cb(const char *path, void *data, void *arg)
159 test_printf("%s\n", path);
160 if (i == 0 && strcmp(path, "/usr.sbin/zic") != 0)
162 if (i == 1 && strcmp(path, "/usr.sbin/unbound") != 0)
164 if (i == 2 && strcmp(path, "/usr.sbin") != 0)
166 if (i == 3 && strcmp(path, "/usr.bin/vi") != 0)
168 if (i == 4 && strcmp(path, "/usr.bin") != 0)
170 if (i == 5 && strcmp(path, "/") != 0)
179 pathset_iter_order(void)
181 const struct got_error *err = NULL;
182 struct got_pathset *set;
184 set = got_pathset_alloc();
186 err = got_error_from_errno();
189 if (got_pathset_num_elements(set) != 0) {
190 err = got_error(GOT_ERR_BAD_PATH);
195 err = got_pathset_add(set, "/usr.bin", (void *)data1);
198 err = got_pathset_add(set, "/usr.sbin/unbound", (void *)data1);
201 err = got_pathset_add(set, "/usr.bin/vi", (void *)data1);
204 err = got_pathset_add(set, "/", (void *)data1);
207 err = got_pathset_add(set, "/usr.sbin/zic", (void *)data1);
210 err = got_pathset_add(set, "/usr.sbin", (void *)data1);
214 test_printf("normal order:\n");
215 got_pathset_for_each(set, pathset_iter_order_cb, NULL);
216 test_printf("reverse order:\n");
217 got_pathset_for_each_reverse(set, pathset_iter_reverse_order_cb,
220 got_pathset_free(set);
221 return (err == NULL);
224 #define RUN_TEST(expr, name) \
225 { test_ok = (expr); \
226 printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
227 failure = (failure || !test_ok); }
232 fprintf(stderr, "usage: pathset_test [-v]\n");
236 main(int argc, char *argv[])
238 int test_ok = 0, failure = 0;
242 if (pledge("stdio", NULL) == -1)
246 while ((ch = getopt(argc, argv, "v")) != -1) {
259 RUN_TEST(pathset_add_remove_iter(), "pathset_add_remove_iter");
260 RUN_TEST(pathset_iter_order(), "pathset_iter_order");
262 return failure ? 1 : 0;