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)
49 test_printf("%s\n", path);
50 if ((strcmp(path, path1) == 0 && data == (void *)data1) ||
51 (strcmp(path, path3) == 0 && data == (void *)data3))
54 return NULL; /* not reached */
58 pathset_add_remove_iter(void)
60 const struct got_error *err = NULL;
61 struct got_pathset *set;
63 set = got_pathset_alloc();
65 err = got_error_from_errno();
68 if (got_pathset_num_elements(set) != 0) {
69 err = got_error(GOT_ERR_BAD_PATH);
74 err = got_pathset_add(set, path1, (void *)data1);
77 if (got_pathset_num_elements(set) != 1) {
78 err = got_error(GOT_ERR_BAD_PATH);
82 if (!got_pathset_contains(set, path1)) {
83 err = got_error(GOT_ERR_BAD_PATH);
87 err = got_pathset_add(set, path2, (void *)data2);
90 if (!got_pathset_contains(set, path2)) {
91 err = got_error(GOT_ERR_BAD_PATH);
94 if (got_pathset_num_elements(set) != 2) {
95 err = got_error(GOT_ERR_BAD_PATH);
99 err = got_pathset_add(set, path3, (void *)data3);
102 if (got_pathset_get(set, path3) != (void *)data3) {
103 err = got_error(GOT_ERR_BAD_PATH);
106 if (got_pathset_num_elements(set) != 3) {
107 err = got_error(GOT_ERR_BAD_PATH);
111 err = got_pathset_remove(NULL, set, path2);
114 if (got_pathset_num_elements(set) != 2) {
115 err = got_error(GOT_ERR_BAD_PATH);
118 if (got_pathset_contains(set, path2)) {
119 err = got_error(GOT_ERR_BAD_PATH);
122 if (got_pathset_get(set, path2) != NULL) {
123 err = got_error(GOT_ERR_BAD_PATH);
127 got_pathset_for_each_safe(set, pathset_add_remove_iter_cb, NULL);
129 got_pathset_free(set);
130 return (err == NULL);
133 static const struct got_error *
134 pathset_iter_order_cb(const char *path, void *data, void *arg)
137 test_printf("%s\n", path);
138 if (i == 0 && strcmp(path, "/") != 0)
140 if (i == 1 && strcmp(path, "/usr.bin") != 0)
142 if (i == 2 && strcmp(path, "/usr.bin/vi") != 0)
144 if (i == 3 && strcmp(path, "/usr.sbin") != 0)
146 if (i == 4 && strcmp(path, "/usr.sbin/unbound") != 0)
148 if (i == 5 && strcmp(path, "/usr.sbin/zic") != 0)
156 static const struct got_error *
157 pathset_iter_reverse_order_cb(const char *path, void *data, void *arg)
160 test_printf("%s\n", path);
161 if (i == 0 && strcmp(path, "/usr.sbin/zic") != 0)
163 if (i == 1 && strcmp(path, "/usr.sbin/unbound") != 0)
165 if (i == 2 && strcmp(path, "/usr.sbin") != 0)
167 if (i == 3 && strcmp(path, "/usr.bin/vi") != 0)
169 if (i == 4 && strcmp(path, "/usr.bin") != 0)
171 if (i == 5 && strcmp(path, "/") != 0)
180 pathset_iter_order(void)
182 const struct got_error *err = NULL;
183 struct got_pathset *set;
185 set = got_pathset_alloc();
187 err = got_error_from_errno();
190 if (got_pathset_num_elements(set) != 0) {
191 err = got_error(GOT_ERR_BAD_PATH);
196 err = got_pathset_add(set, "/usr.bin", (void *)data1);
199 err = got_pathset_add(set, "/usr.sbin/unbound", (void *)data1);
202 err = got_pathset_add(set, "/usr.bin/vi", (void *)data1);
205 err = got_pathset_add(set, "/", (void *)data1);
208 err = got_pathset_add(set, "/usr.sbin/zic", (void *)data1);
211 err = got_pathset_add(set, "/usr.sbin", (void *)data1);
215 test_printf("normal order:\n");
216 got_pathset_for_each_safe(set, pathset_iter_order_cb, NULL);
217 test_printf("reverse order:\n");
218 got_pathset_for_each_reverse_safe(set, pathset_iter_reverse_order_cb,
221 got_pathset_free(set);
222 return (err == NULL);
225 #define RUN_TEST(expr, name) \
226 { test_ok = (expr); \
227 printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
228 failure = (failure || !test_ok); }
233 fprintf(stderr, "usage: pathset_test [-v]\n");
237 main(int argc, char *argv[])
239 int test_ok = 0, failure = 0;
243 if (pledge("stdio", NULL) == -1)
247 while ((ch = getopt(argc, argv, "v")) != -1) {
260 RUN_TEST(pathset_add_remove_iter(), "pathset_add_remove_iter");
261 RUN_TEST(pathset_iter_order(), "pathset_iter_order");
263 return failure ? 1 : 0;