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"
27 324d37e7 2019-05-11 stsp #include "got_path.h"
29 1c11b35c 2019-01-12 stsp #ifndef nitems
30 1c11b35c 2019-01-12 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
33 1c11b35c 2019-01-12 stsp static int verbose;
34 7fb414ae 2020-08-08 stsp static int quiet;
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 d58ddaf3 2022-03-17 naddy const 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 d572f586 2019-08-02 stsp int cmp = got_path_cmp(path1, path2,
89 d572f586 2019-08-02 stsp strlen(path1), strlen(path2));
91 1c11b35c 2019-01-12 stsp if (cmp != expected) {
92 1c11b35c 2019-01-12 stsp test_printf("%d: '%s' vs '%s' == %d; expected %d\n",
93 1c11b35c 2019-01-12 stsp i, path1, path2, cmp, expected);
101 e08cc72d 2019-02-05 stsp const char *path_list_input[] = {
102 e08cc72d 2019-02-05 stsp "", "/", "a", "/b", "/bar", "bar/sub", "/bar/sub", "/bar/",
103 e08cc72d 2019-02-05 stsp "/bar.c", "/bar/sub/sub2", "/bar.sub.sub2", "/foo",
104 e08cc72d 2019-02-05 stsp "/foo/sub", "/foo/", "/foo/", "x/a",
106 e08cc72d 2019-02-05 stsp const char *path_list_expected[] = {
112 e08cc72d 2019-02-05 stsp "/bar/sub/sub2",
114 e08cc72d 2019-02-05 stsp "/bar.sub.sub2",
116 e08cc72d 2019-02-05 stsp "/foo/sub",
120 e08cc72d 2019-02-05 stsp /* If inserting pathlist_input in reverse the result is slightly different. */
121 e08cc72d 2019-02-05 stsp const char *path_list_expected_reverse[] = {
126 e08cc72d 2019-02-05 stsp "/bar/sub",
127 e08cc72d 2019-02-05 stsp "/bar/sub/sub2",
129 e08cc72d 2019-02-05 stsp "/bar.sub.sub2",
131 e08cc72d 2019-02-05 stsp "/foo/sub",
137 e08cc72d 2019-02-05 stsp path_list(void)
139 e08cc72d 2019-02-05 stsp const struct got_error *err = NULL;
140 e08cc72d 2019-02-05 stsp struct got_pathlist_head paths;
141 e08cc72d 2019-02-05 stsp struct got_pathlist_entry *pe;
144 e08cc72d 2019-02-05 stsp TAILQ_INIT(&paths);
145 e08cc72d 2019-02-05 stsp for (i = 0; i < nitems(path_list_input); i++) {
146 3d8df59c 2019-02-05 stsp err = got_pathlist_insert(NULL, &paths, path_list_input[i],
149 e08cc72d 2019-02-05 stsp test_printf("%s\n", __func__, err->msg);
155 e08cc72d 2019-02-05 stsp TAILQ_FOREACH(pe, &paths, entry) {
156 e08cc72d 2019-02-05 stsp test_printf("'%s' -- '%s'\n", pe->path, path_list_expected[i]);
157 e08cc72d 2019-02-05 stsp if (i >= nitems(path_list_expected)) {
158 e08cc72d 2019-02-05 stsp test_printf("too many elements on list\n");
161 e08cc72d 2019-02-05 stsp if (strcmp(pe->path, path_list_expected[i]) != 0) {
162 e08cc72d 2019-02-05 stsp test_printf("unordered elements on list\n");
168 e08cc72d 2019-02-05 stsp got_pathlist_free(&paths);
173 e08cc72d 2019-02-05 stsp path_list_reverse_input(void)
175 e08cc72d 2019-02-05 stsp const struct got_error *err = NULL;
176 e08cc72d 2019-02-05 stsp struct got_pathlist_head paths;
177 e08cc72d 2019-02-05 stsp struct got_pathlist_entry *pe;
180 e08cc72d 2019-02-05 stsp TAILQ_INIT(&paths);
181 a4153d5b 2020-12-17 stsp for (i = nitems(path_list_input); i > 0;) {
182 a4153d5b 2020-12-17 stsp err = got_pathlist_insert(NULL, &paths, path_list_input[--i],
185 e08cc72d 2019-02-05 stsp test_printf("%s\n", __func__, err->msg);
191 e08cc72d 2019-02-05 stsp TAILQ_FOREACH(pe, &paths, entry) {
192 e08cc72d 2019-02-05 stsp test_printf("'%s' -- '%s'\n", pe->path,
193 e08cc72d 2019-02-05 stsp path_list_expected_reverse[i]);
194 e08cc72d 2019-02-05 stsp if (i >= nitems(path_list_expected_reverse)) {
195 e08cc72d 2019-02-05 stsp test_printf("too many elements on list\n");
198 e08cc72d 2019-02-05 stsp if (strcmp(pe->path, path_list_expected_reverse[i]) != 0) {
199 e08cc72d 2019-02-05 stsp test_printf("unordered elements on list\n");
205 e08cc72d 2019-02-05 stsp got_pathlist_free(&paths);
209 1c11b35c 2019-01-12 stsp #define RUN_TEST(expr, name) \
210 1c11b35c 2019-01-12 stsp { test_ok = (expr); \
211 7fb414ae 2020-08-08 stsp if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
212 1c11b35c 2019-01-12 stsp failure = (failure || !test_ok); }
215 1c11b35c 2019-01-12 stsp usage(void)
217 7fb414ae 2020-08-08 stsp fprintf(stderr, "usage: path_test [-v] [-q]\n");
221 1c11b35c 2019-01-12 stsp main(int argc, char *argv[])
223 1c11b35c 2019-01-12 stsp int test_ok = 0, failure = 0;
226 1c11b35c 2019-01-12 stsp #ifndef PROFILE
227 1c11b35c 2019-01-12 stsp if (pledge("stdio", NULL) == -1)
228 1c11b35c 2019-01-12 stsp err(1, "pledge");
231 7fb414ae 2020-08-08 stsp while ((ch = getopt(argc, argv, "vq")) != -1) {
232 1c11b35c 2019-01-12 stsp switch (ch) {
234 1c11b35c 2019-01-12 stsp verbose = 1;
239 7fb414ae 2020-08-08 stsp verbose = 0;
246 1c11b35c 2019-01-12 stsp argc -= optind;
247 1c11b35c 2019-01-12 stsp argv += optind;
249 1beed999 2019-01-12 stsp RUN_TEST(path_cmp(), "path_cmp");
250 e08cc72d 2019-02-05 stsp RUN_TEST(path_list(), "path_list");
251 e08cc72d 2019-02-05 stsp RUN_TEST(path_list_reverse_input(), "path_list_reverse_input");
253 1c11b35c 2019-01-12 stsp return failure ? 1 : 0;