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 09e05ed5 2023-03-08 thomas #include "got_compat.h"
19 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
21 e08cc72d 2019-02-05 stsp #include <string.h>
22 1c11b35c 2019-01-12 stsp #include <stdlib.h>
23 1c11b35c 2019-01-12 stsp #include <stdarg.h>
24 1c11b35c 2019-01-12 stsp #include <stdio.h>
25 1c11b35c 2019-01-12 stsp #include <unistd.h>
26 1c11b35c 2019-01-12 stsp #include <err.h>
28 1c11b35c 2019-01-12 stsp #include "got_error.h"
29 324d37e7 2019-05-11 stsp #include "got_path.h"
31 1c11b35c 2019-01-12 stsp #ifndef nitems
32 1c11b35c 2019-01-12 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
35 1c11b35c 2019-01-12 stsp static int verbose;
36 7fb414ae 2020-08-08 stsp static int quiet;
38 ef20f542 2022-06-26 thomas static void
39 0c6f49ba 2022-07-01 thomas test_printf(const char *fmt, ...)
43 1c11b35c 2019-01-12 stsp if (!verbose)
46 1c11b35c 2019-01-12 stsp va_start(ap, fmt);
47 1c11b35c 2019-01-12 stsp vprintf(fmt, ap);
52 1beed999 2019-01-12 stsp path_cmp(void)
54 fa8129f7 2022-03-22 thomas const struct path_cmp_test {
55 1c11b35c 2019-01-12 stsp const char *path1;
56 1c11b35c 2019-01-12 stsp const char *path2;
57 1c11b35c 2019-01-12 stsp int expected;
58 1c11b35c 2019-01-12 stsp } test_data[] = {
59 1c11b35c 2019-01-12 stsp { "", "", 0 },
60 1c11b35c 2019-01-12 stsp { "/", "/", 0 },
61 1c11b35c 2019-01-12 stsp { "/a", "/b", -1 },
62 1c11b35c 2019-01-12 stsp { "x/a", "x.a", -1 },
63 1c11b35c 2019-01-12 stsp { "x.a", "x/a", 1 },
64 e08cc72d 2019-02-05 stsp { "//foo", "/bar", 1 },
65 1c11b35c 2019-01-12 stsp { "/foo", "/bar", 1 },
66 e08cc72d 2019-02-05 stsp { "foo", "bar", 1 },
67 1c11b35c 2019-01-12 stsp { "/foo/sub", "/bar", 1 },
68 1c11b35c 2019-01-12 stsp { "/foo", "/bar/sub", 1 },
69 1c11b35c 2019-01-12 stsp { "/foo/", "/bar", 1 },
70 1c11b35c 2019-01-12 stsp { "/foo", "/bar/", 1 },
71 1c11b35c 2019-01-12 stsp { "/foo/", "/bar/", 1 },
72 1c11b35c 2019-01-12 stsp { "/bar/", "/bar/", 0 },
73 e08cc72d 2019-02-05 stsp { "/bar/", "/bar", 0 },
74 e08cc72d 2019-02-05 stsp { "//bar//", "/bar/", 0 },
75 e08cc72d 2019-02-05 stsp { "//bar//", "/bar////", 0 },
76 e08cc72d 2019-02-05 stsp { "/bar/sub", "/bar.", -1 },
77 1c11b35c 2019-01-12 stsp { "/bar/sub", "/bar/", 1 },
78 e08cc72d 2019-02-05 stsp { "/bar/sub/", "/bar///", 1 },
79 1c11b35c 2019-01-12 stsp { "/bar/sub/sub2", "/bar/", 1 },
80 1c11b35c 2019-01-12 stsp { "/bar/sub/sub2", "/bar", 1 },
81 1c11b35c 2019-01-12 stsp { "/bar.sub.sub2", "/bar", 1 },
82 1c11b35c 2019-01-12 stsp { "/bar/sub/sub2", "/bar.c", -1 },
86 1c11b35c 2019-01-12 stsp for (i = 0; i < nitems(test_data); i++) {
87 1c11b35c 2019-01-12 stsp const char *path1 = test_data[i].path1;
88 1c11b35c 2019-01-12 stsp const char *path2 = test_data[i].path2;
89 1c11b35c 2019-01-12 stsp int expected = test_data[i].expected;
90 d572f586 2019-08-02 stsp int cmp = got_path_cmp(path1, path2,
91 d572f586 2019-08-02 stsp strlen(path1), strlen(path2));
93 1c11b35c 2019-01-12 stsp if (cmp != expected) {
94 1c11b35c 2019-01-12 stsp test_printf("%d: '%s' vs '%s' == %d; expected %d\n",
95 1c11b35c 2019-01-12 stsp i, path1, path2, cmp, expected);
103 e08cc72d 2019-02-05 stsp const char *path_list_input[] = {
104 e08cc72d 2019-02-05 stsp "", "/", "a", "/b", "/bar", "bar/sub", "/bar/sub", "/bar/",
105 e08cc72d 2019-02-05 stsp "/bar.c", "/bar/sub/sub2", "/bar.sub.sub2", "/foo",
106 e08cc72d 2019-02-05 stsp "/foo/sub", "/foo/", "/foo/", "x/a",
108 e08cc72d 2019-02-05 stsp const char *path_list_expected[] = {
114 e08cc72d 2019-02-05 stsp "/bar/sub/sub2",
116 e08cc72d 2019-02-05 stsp "/bar.sub.sub2",
118 e08cc72d 2019-02-05 stsp "/foo/sub",
122 e08cc72d 2019-02-05 stsp /* If inserting pathlist_input in reverse the result is slightly different. */
123 e08cc72d 2019-02-05 stsp const char *path_list_expected_reverse[] = {
128 e08cc72d 2019-02-05 stsp "/bar/sub",
129 e08cc72d 2019-02-05 stsp "/bar/sub/sub2",
131 e08cc72d 2019-02-05 stsp "/bar.sub.sub2",
133 e08cc72d 2019-02-05 stsp "/foo/sub",
139 e08cc72d 2019-02-05 stsp path_list(void)
141 e08cc72d 2019-02-05 stsp const struct got_error *err = NULL;
142 e08cc72d 2019-02-05 stsp struct got_pathlist_head paths;
143 e08cc72d 2019-02-05 stsp struct got_pathlist_entry *pe;
146 e08cc72d 2019-02-05 stsp TAILQ_INIT(&paths);
147 e08cc72d 2019-02-05 stsp for (i = 0; i < nitems(path_list_input); i++) {
148 3d8df59c 2019-02-05 stsp err = got_pathlist_insert(NULL, &paths, path_list_input[i],
151 e08cc72d 2019-02-05 stsp test_printf("%s\n", __func__, err->msg);
157 e08cc72d 2019-02-05 stsp TAILQ_FOREACH(pe, &paths, entry) {
158 e08cc72d 2019-02-05 stsp test_printf("'%s' -- '%s'\n", pe->path, path_list_expected[i]);
159 e08cc72d 2019-02-05 stsp if (i >= nitems(path_list_expected)) {
160 e08cc72d 2019-02-05 stsp test_printf("too many elements on list\n");
163 e08cc72d 2019-02-05 stsp if (strcmp(pe->path, path_list_expected[i]) != 0) {
164 e08cc72d 2019-02-05 stsp test_printf("unordered elements on list\n");
170 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
175 e08cc72d 2019-02-05 stsp path_list_reverse_input(void)
177 e08cc72d 2019-02-05 stsp const struct got_error *err = NULL;
178 e08cc72d 2019-02-05 stsp struct got_pathlist_head paths;
179 e08cc72d 2019-02-05 stsp struct got_pathlist_entry *pe;
182 e08cc72d 2019-02-05 stsp TAILQ_INIT(&paths);
183 a4153d5b 2020-12-17 stsp for (i = nitems(path_list_input); i > 0;) {
184 a4153d5b 2020-12-17 stsp err = got_pathlist_insert(NULL, &paths, path_list_input[--i],
187 e08cc72d 2019-02-05 stsp test_printf("%s\n", __func__, err->msg);
193 e08cc72d 2019-02-05 stsp TAILQ_FOREACH(pe, &paths, entry) {
194 e08cc72d 2019-02-05 stsp test_printf("'%s' -- '%s'\n", pe->path,
195 e08cc72d 2019-02-05 stsp path_list_expected_reverse[i]);
196 e08cc72d 2019-02-05 stsp if (i >= nitems(path_list_expected_reverse)) {
197 e08cc72d 2019-02-05 stsp test_printf("too many elements on list\n");
200 e08cc72d 2019-02-05 stsp if (strcmp(pe->path, path_list_expected_reverse[i]) != 0) {
201 e08cc72d 2019-02-05 stsp test_printf("unordered elements on list\n");
207 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
211 1c11b35c 2019-01-12 stsp #define RUN_TEST(expr, name) \
212 1c11b35c 2019-01-12 stsp { test_ok = (expr); \
213 7fb414ae 2020-08-08 stsp if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
214 1c11b35c 2019-01-12 stsp failure = (failure || !test_ok); }
216 ef20f542 2022-06-26 thomas static void
217 1c11b35c 2019-01-12 stsp usage(void)
219 7fb414ae 2020-08-08 stsp fprintf(stderr, "usage: path_test [-v] [-q]\n");
223 1c11b35c 2019-01-12 stsp main(int argc, char *argv[])
225 1c11b35c 2019-01-12 stsp int test_ok = 0, failure = 0;
228 1c11b35c 2019-01-12 stsp #ifndef PROFILE
229 1c11b35c 2019-01-12 stsp if (pledge("stdio", NULL) == -1)
230 1c11b35c 2019-01-12 stsp err(1, "pledge");
233 f7065961 2022-10-27 thomas while ((ch = getopt(argc, argv, "qv")) != -1) {
234 1c11b35c 2019-01-12 stsp switch (ch) {
237 7fb414ae 2020-08-08 stsp verbose = 0;
239 f7065961 2022-10-27 thomas case 'v':
240 f7065961 2022-10-27 thomas verbose = 1;
241 f7065961 2022-10-27 thomas quiet = 0;
248 1c11b35c 2019-01-12 stsp argc -= optind;
249 1c11b35c 2019-01-12 stsp argv += optind;
251 1beed999 2019-01-12 stsp RUN_TEST(path_cmp(), "path_cmp");
252 e08cc72d 2019-02-05 stsp RUN_TEST(path_list(), "path_list");
253 e08cc72d 2019-02-05 stsp RUN_TEST(path_list_reverse_input(), "path_list_reverse_input");
255 1c11b35c 2019-01-12 stsp return failure ? 1 : 0;