Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/queue.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <err.h>
26 #include "got_error.h"
27 #include "got_path.h"
29 #ifndef nitems
30 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
31 #endif
33 static int verbose;
34 static int quiet;
36 static void
37 test_printf(const char *fmt, ...)
38 {
39 va_list ap;
41 if (!verbose)
42 return;
44 va_start(ap, fmt);
45 vprintf(fmt, ap);
46 va_end(ap);
47 }
49 static int
50 path_cmp(void)
51 {
52 const struct path_cmp_test {
53 const char *path1;
54 const char *path2;
55 int expected;
56 } test_data[] = {
57 { "", "", 0 },
58 { "/", "/", 0 },
59 { "/a", "/b", -1 },
60 { "x/a", "x.a", -1 },
61 { "x.a", "x/a", 1 },
62 { "//foo", "/bar", 1 },
63 { "/foo", "/bar", 1 },
64 { "foo", "bar", 1 },
65 { "/foo/sub", "/bar", 1 },
66 { "/foo", "/bar/sub", 1 },
67 { "/foo/", "/bar", 1 },
68 { "/foo", "/bar/", 1 },
69 { "/foo/", "/bar/", 1 },
70 { "/bar/", "/bar/", 0 },
71 { "/bar/", "/bar", 0 },
72 { "//bar//", "/bar/", 0 },
73 { "//bar//", "/bar////", 0 },
74 { "/bar/sub", "/bar.", -1 },
75 { "/bar/sub", "/bar/", 1 },
76 { "/bar/sub/", "/bar///", 1 },
77 { "/bar/sub/sub2", "/bar/", 1 },
78 { "/bar/sub/sub2", "/bar", 1 },
79 { "/bar.sub.sub2", "/bar", 1 },
80 { "/bar/sub/sub2", "/bar.c", -1 },
81 };
82 size_t i;
84 for (i = 0; i < nitems(test_data); i++) {
85 const char *path1 = test_data[i].path1;
86 const char *path2 = test_data[i].path2;
87 int expected = test_data[i].expected;
88 int cmp = got_path_cmp(path1, path2,
89 strlen(path1), strlen(path2));
91 if (cmp != expected) {
92 test_printf("%d: '%s' vs '%s' == %d; expected %d\n",
93 i, path1, path2, cmp, expected);
94 return 0;
95 }
96 }
98 return 1;
99 }
101 const char *path_list_input[] = {
102 "", "/", "a", "/b", "/bar", "bar/sub", "/bar/sub", "/bar/",
103 "/bar.c", "/bar/sub/sub2", "/bar.sub.sub2", "/foo",
104 "/foo/sub", "/foo/", "/foo/", "x/a",
105 };
106 const char *path_list_expected[] = {
107 "",
108 "a",
109 "/b",
110 "/bar",
111 "bar/sub",
112 "/bar/sub/sub2",
113 "/bar.c",
114 "/bar.sub.sub2",
115 "/foo",
116 "/foo/sub",
117 "x/a",
118 };
120 /* If inserting pathlist_input in reverse the result is slightly different. */
121 const char *path_list_expected_reverse[] = {
122 "/",
123 "a",
124 "/b",
125 "/bar/",
126 "/bar/sub",
127 "/bar/sub/sub2",
128 "/bar.c",
129 "/bar.sub.sub2",
130 "/foo/",
131 "/foo/sub",
132 "x/a",
133 };
136 static int
137 path_list(void)
139 const struct got_error *err = NULL;
140 struct got_pathlist_head paths;
141 struct got_pathlist_entry *pe;
142 size_t i;
144 TAILQ_INIT(&paths);
145 for (i = 0; i < nitems(path_list_input); i++) {
146 err = got_pathlist_insert(NULL, &paths, path_list_input[i],
147 NULL);
148 if (err) {
149 test_printf("%s\n", __func__, err->msg);
150 return 0;
154 i = 0;
155 TAILQ_FOREACH(pe, &paths, entry) {
156 test_printf("'%s' -- '%s'\n", pe->path, path_list_expected[i]);
157 if (i >= nitems(path_list_expected)) {
158 test_printf("too many elements on list\n");
159 return 0;
161 if (strcmp(pe->path, path_list_expected[i]) != 0) {
162 test_printf("unordered elements on list\n");
163 return 0;
165 i++;
168 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
169 return 1;
172 static int
173 path_list_reverse_input(void)
175 const struct got_error *err = NULL;
176 struct got_pathlist_head paths;
177 struct got_pathlist_entry *pe;
178 size_t i;
180 TAILQ_INIT(&paths);
181 for (i = nitems(path_list_input); i > 0;) {
182 err = got_pathlist_insert(NULL, &paths, path_list_input[--i],
183 NULL);
184 if (err) {
185 test_printf("%s\n", __func__, err->msg);
186 return 0;
190 i = 0;
191 TAILQ_FOREACH(pe, &paths, entry) {
192 test_printf("'%s' -- '%s'\n", pe->path,
193 path_list_expected_reverse[i]);
194 if (i >= nitems(path_list_expected_reverse)) {
195 test_printf("too many elements on list\n");
196 return 0;
198 if (strcmp(pe->path, path_list_expected_reverse[i]) != 0) {
199 test_printf("unordered elements on list\n");
200 return 0;
202 i++;
205 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
206 return 1;
209 #define RUN_TEST(expr, name) \
210 { test_ok = (expr); \
211 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
212 failure = (failure || !test_ok); }
214 static void
215 usage(void)
217 fprintf(stderr, "usage: path_test [-v] [-q]\n");
220 int
221 main(int argc, char *argv[])
223 int test_ok = 0, failure = 0;
224 int ch;
226 #ifndef PROFILE
227 if (pledge("stdio", NULL) == -1)
228 err(1, "pledge");
229 #endif
231 while ((ch = getopt(argc, argv, "qv")) != -1) {
232 switch (ch) {
233 case 'q':
234 quiet = 1;
235 verbose = 0;
236 break;
237 case 'v':
238 verbose = 1;
239 quiet = 0;
240 break;
241 default:
242 usage();
243 return 1;
246 argc -= optind;
247 argv += optind;
249 RUN_TEST(path_cmp(), "path_cmp");
250 RUN_TEST(path_list(), "path_list");
251 RUN_TEST(path_list_reverse_input(), "path_list_reverse_input");
253 return failure ? 1 : 0;