2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 e08cc72d 2019-02-05 stsp #include <sys/queue.h>
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>
26 1c11b35c 2019-01-12 stsp #include "got_error.h"
28 1c11b35c 2019-01-12 stsp #include "got_lib_path.h"
30 1c11b35c 2019-01-12 stsp #ifndef nitems
31 1c11b35c 2019-01-12 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
34 1c11b35c 2019-01-12 stsp static int verbose;
37 1c11b35c 2019-01-12 stsp test_printf(char *fmt, ...)
41 1c11b35c 2019-01-12 stsp if (!verbose)
44 1c11b35c 2019-01-12 stsp va_start(ap, fmt);
45 1c11b35c 2019-01-12 stsp vprintf(fmt, ap);
50 1beed999 2019-01-12 stsp path_cmp(void)
52 1c11b35c 2019-01-12 stsp struct path_cmp_test {
53 1c11b35c 2019-01-12 stsp const char *path1;
54 1c11b35c 2019-01-12 stsp const char *path2;
55 1c11b35c 2019-01-12 stsp int expected;
56 1c11b35c 2019-01-12 stsp } test_data[] = {
57 1c11b35c 2019-01-12 stsp { "", "", 0 },
58 1c11b35c 2019-01-12 stsp { "/", "/", 0 },
59 1c11b35c 2019-01-12 stsp { "/a", "/b", -1 },
60 1c11b35c 2019-01-12 stsp { "x/a", "x.a", -1 },
61 1c11b35c 2019-01-12 stsp { "x.a", "x/a", 1 },
62 e08cc72d 2019-02-05 stsp { "//foo", "/bar", 1 },
63 1c11b35c 2019-01-12 stsp { "/foo", "/bar", 1 },
64 e08cc72d 2019-02-05 stsp { "foo", "bar", 1 },
65 1c11b35c 2019-01-12 stsp { "/foo/sub", "/bar", 1 },
66 1c11b35c 2019-01-12 stsp { "/foo", "/bar/sub", 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 { "/foo/", "/bar/", 1 },
70 1c11b35c 2019-01-12 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//", "/bar////", 0 },
74 e08cc72d 2019-02-05 stsp { "/bar/sub", "/bar.", -1 },
75 1c11b35c 2019-01-12 stsp { "/bar/sub", "/bar/", 1 },
76 e08cc72d 2019-02-05 stsp { "/bar/sub/", "/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", 1 },
80 1c11b35c 2019-01-12 stsp { "/bar/sub/sub2", "/bar.c", -1 },
84 1c11b35c 2019-01-12 stsp for (i = 0; i < nitems(test_data); i++) {
85 1c11b35c 2019-01-12 stsp const char *path1 = test_data[i].path1;
86 1c11b35c 2019-01-12 stsp const char *path2 = test_data[i].path2;
87 1c11b35c 2019-01-12 stsp int expected = test_data[i].expected;
88 1beed999 2019-01-12 stsp int cmp = got_path_cmp(path1, path2);
90 1c11b35c 2019-01-12 stsp if (cmp != expected) {
91 1c11b35c 2019-01-12 stsp test_printf("%d: '%s' vs '%s' == %d; expected %d\n",
92 1c11b35c 2019-01-12 stsp i, path1, path2, cmp, expected);
100 e08cc72d 2019-02-05 stsp const char *path_list_input[] = {
101 e08cc72d 2019-02-05 stsp "", "/", "a", "/b", "/bar", "bar/sub", "/bar/sub", "/bar/",
102 e08cc72d 2019-02-05 stsp "/bar.c", "/bar/sub/sub2", "/bar.sub.sub2", "/foo",
103 e08cc72d 2019-02-05 stsp "/foo/sub", "/foo/", "/foo/", "x/a",
105 e08cc72d 2019-02-05 stsp const char *path_list_expected[] = {
111 e08cc72d 2019-02-05 stsp "/bar/sub/sub2",
113 e08cc72d 2019-02-05 stsp "/bar.sub.sub2",
115 e08cc72d 2019-02-05 stsp "/foo/sub",
119 e08cc72d 2019-02-05 stsp /* If inserting pathlist_input in reverse the result is slightly different. */
120 e08cc72d 2019-02-05 stsp const char *path_list_expected_reverse[] = {
125 e08cc72d 2019-02-05 stsp "/bar/sub",
126 e08cc72d 2019-02-05 stsp "/bar/sub/sub2",
128 e08cc72d 2019-02-05 stsp "/bar.sub.sub2",
130 e08cc72d 2019-02-05 stsp "/foo/sub",
136 e08cc72d 2019-02-05 stsp path_list(void)
138 e08cc72d 2019-02-05 stsp const struct got_error *err = NULL;
139 e08cc72d 2019-02-05 stsp struct got_pathlist_head paths;
140 e08cc72d 2019-02-05 stsp struct got_pathlist_entry *pe;
143 e08cc72d 2019-02-05 stsp TAILQ_INIT(&paths);
144 e08cc72d 2019-02-05 stsp for (i = 0; i < nitems(path_list_input); i++) {
145 3d8df59c 2019-02-05 stsp err = got_pathlist_insert(NULL, &paths, path_list_input[i],
148 e08cc72d 2019-02-05 stsp test_printf("%s\n", __func__, err->msg);
154 e08cc72d 2019-02-05 stsp TAILQ_FOREACH(pe, &paths, entry) {
155 e08cc72d 2019-02-05 stsp test_printf("'%s' -- '%s'\n", pe->path, path_list_expected[i]);
156 e08cc72d 2019-02-05 stsp if (i >= nitems(path_list_expected)) {
157 e08cc72d 2019-02-05 stsp test_printf("too many elements on list\n");
160 e08cc72d 2019-02-05 stsp if (strcmp(pe->path, path_list_expected[i]) != 0) {
161 e08cc72d 2019-02-05 stsp test_printf("unordered elements on list\n");
167 e08cc72d 2019-02-05 stsp got_pathlist_free(&paths);
172 e08cc72d 2019-02-05 stsp path_list_reverse_input(void)
174 e08cc72d 2019-02-05 stsp const struct got_error *err = NULL;
175 e08cc72d 2019-02-05 stsp struct got_pathlist_head paths;
176 e08cc72d 2019-02-05 stsp struct got_pathlist_entry *pe;
179 e08cc72d 2019-02-05 stsp TAILQ_INIT(&paths);
180 e08cc72d 2019-02-05 stsp for (i = nitems(path_list_input) - 1; i >= 0; i--) {
181 3d8df59c 2019-02-05 stsp err = got_pathlist_insert(NULL, &paths, path_list_input[i],
184 e08cc72d 2019-02-05 stsp test_printf("%s\n", __func__, err->msg);
190 e08cc72d 2019-02-05 stsp TAILQ_FOREACH(pe, &paths, entry) {
191 e08cc72d 2019-02-05 stsp test_printf("'%s' -- '%s'\n", pe->path,
192 e08cc72d 2019-02-05 stsp path_list_expected_reverse[i]);
193 e08cc72d 2019-02-05 stsp if (i >= nitems(path_list_expected_reverse)) {
194 e08cc72d 2019-02-05 stsp test_printf("too many elements on list\n");
197 e08cc72d 2019-02-05 stsp if (strcmp(pe->path, path_list_expected_reverse[i]) != 0) {
198 e08cc72d 2019-02-05 stsp test_printf("unordered elements on list\n");
204 e08cc72d 2019-02-05 stsp got_pathlist_free(&paths);
208 1c11b35c 2019-01-12 stsp #define RUN_TEST(expr, name) \
209 1c11b35c 2019-01-12 stsp { test_ok = (expr); \
210 1c11b35c 2019-01-12 stsp printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
211 1c11b35c 2019-01-12 stsp failure = (failure || !test_ok); }
214 1c11b35c 2019-01-12 stsp usage(void)
216 1c11b35c 2019-01-12 stsp fprintf(stderr, "usage: path_test [-v]\n");
220 1c11b35c 2019-01-12 stsp main(int argc, char *argv[])
222 1c11b35c 2019-01-12 stsp int test_ok = 0, failure = 0;
225 1c11b35c 2019-01-12 stsp #ifndef PROFILE
226 1c11b35c 2019-01-12 stsp if (pledge("stdio", NULL) == -1)
227 1c11b35c 2019-01-12 stsp err(1, "pledge");
230 1c11b35c 2019-01-12 stsp while ((ch = getopt(argc, argv, "v")) != -1) {
231 1c11b35c 2019-01-12 stsp switch (ch) {
233 1c11b35c 2019-01-12 stsp verbose = 1;
240 1c11b35c 2019-01-12 stsp argc -= optind;
241 1c11b35c 2019-01-12 stsp argv += optind;
243 1beed999 2019-01-12 stsp RUN_TEST(path_cmp(), "path_cmp");
244 e08cc72d 2019-02-05 stsp RUN_TEST(path_list(), "path_list");
245 e08cc72d 2019-02-05 stsp RUN_TEST(path_list_reverse_input(), "path_list_reverse_input");
247 1c11b35c 2019-01-12 stsp return failure ? 1 : 0;