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;
35 void
36 test_printf(char *fmt, ...)
37 {
38 va_list ap;
40 if (!verbose)
41 return;
43 va_start(ap, fmt);
44 vprintf(fmt, ap);
45 va_end(ap);
46 }
48 static int
49 path_cmp(void)
50 {
51 struct path_cmp_test {
52 const char *path1;
53 const char *path2;
54 int expected;
55 } test_data[] = {
56 { "", "", 0 },
57 { "/", "/", 0 },
58 { "/a", "/b", -1 },
59 { "x/a", "x.a", -1 },
60 { "x.a", "x/a", 1 },
61 { "//foo", "/bar", 1 },
62 { "/foo", "/bar", 1 },
63 { "foo", "bar", 1 },
64 { "/foo/sub", "/bar", 1 },
65 { "/foo", "/bar/sub", 1 },
66 { "/foo/", "/bar", 1 },
67 { "/foo", "/bar/", 1 },
68 { "/foo/", "/bar/", 1 },
69 { "/bar/", "/bar/", 0 },
70 { "/bar/", "/bar", 0 },
71 { "//bar//", "/bar/", 0 },
72 { "//bar//", "/bar////", 0 },
73 { "/bar/sub", "/bar.", -1 },
74 { "/bar/sub", "/bar/", 1 },
75 { "/bar/sub/", "/bar///", 1 },
76 { "/bar/sub/sub2", "/bar/", 1 },
77 { "/bar/sub/sub2", "/bar", 1 },
78 { "/bar.sub.sub2", "/bar", 1 },
79 { "/bar/sub/sub2", "/bar.c", -1 },
80 };
81 int i;
83 for (i = 0; i < nitems(test_data); i++) {
84 const char *path1 = test_data[i].path1;
85 const char *path2 = test_data[i].path2;
86 int expected = test_data[i].expected;
87 int cmp = got_path_cmp(path1, path2);
89 if (cmp != expected) {
90 test_printf("%d: '%s' vs '%s' == %d; expected %d\n",
91 i, path1, path2, cmp, expected);
92 return 0;
93 }
94 }
96 return 1;
97 }
99 const char *path_list_input[] = {
100 "", "/", "a", "/b", "/bar", "bar/sub", "/bar/sub", "/bar/",
101 "/bar.c", "/bar/sub/sub2", "/bar.sub.sub2", "/foo",
102 "/foo/sub", "/foo/", "/foo/", "x/a",
103 };
104 const char *path_list_expected[] = {
105 "",
106 "a",
107 "/b",
108 "/bar",
109 "bar/sub",
110 "/bar/sub/sub2",
111 "/bar.c",
112 "/bar.sub.sub2",
113 "/foo",
114 "/foo/sub",
115 "x/a",
116 };
118 /* If inserting pathlist_input in reverse the result is slightly different. */
119 const char *path_list_expected_reverse[] = {
120 "/",
121 "a",
122 "/b",
123 "/bar/",
124 "/bar/sub",
125 "/bar/sub/sub2",
126 "/bar.c",
127 "/bar.sub.sub2",
128 "/foo/",
129 "/foo/sub",
130 "x/a",
131 };
134 static int
135 path_list(void)
137 const struct got_error *err = NULL;
138 struct got_pathlist_head paths;
139 struct got_pathlist_entry *pe;
140 int i;
142 TAILQ_INIT(&paths);
143 for (i = 0; i < nitems(path_list_input); i++) {
144 err = got_pathlist_insert(NULL, &paths, path_list_input[i],
145 NULL);
146 if (err) {
147 test_printf("%s\n", __func__, err->msg);
148 return 0;
152 i = 0;
153 TAILQ_FOREACH(pe, &paths, entry) {
154 test_printf("'%s' -- '%s'\n", pe->path, path_list_expected[i]);
155 if (i >= nitems(path_list_expected)) {
156 test_printf("too many elements on list\n");
157 return 0;
159 if (strcmp(pe->path, path_list_expected[i]) != 0) {
160 test_printf("unordered elements on list\n");
161 return 0;
163 i++;
166 got_pathlist_free(&paths);
167 return 1;
170 static int
171 path_list_reverse_input(void)
173 const struct got_error *err = NULL;
174 struct got_pathlist_head paths;
175 struct got_pathlist_entry *pe;
176 int i;
178 TAILQ_INIT(&paths);
179 for (i = nitems(path_list_input) - 1; i >= 0; i--) {
180 err = got_pathlist_insert(NULL, &paths, path_list_input[i],
181 NULL);
182 if (err) {
183 test_printf("%s\n", __func__, err->msg);
184 return 0;
188 i = 0;
189 TAILQ_FOREACH(pe, &paths, entry) {
190 test_printf("'%s' -- '%s'\n", pe->path,
191 path_list_expected_reverse[i]);
192 if (i >= nitems(path_list_expected_reverse)) {
193 test_printf("too many elements on list\n");
194 return 0;
196 if (strcmp(pe->path, path_list_expected_reverse[i]) != 0) {
197 test_printf("unordered elements on list\n");
198 return 0;
200 i++;
203 got_pathlist_free(&paths);
204 return 1;
207 #define RUN_TEST(expr, name) \
208 { test_ok = (expr); \
209 printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
210 failure = (failure || !test_ok); }
212 void
213 usage(void)
215 fprintf(stderr, "usage: path_test [-v]\n");
218 int
219 main(int argc, char *argv[])
221 int test_ok = 0, failure = 0;
222 int ch;
224 #ifndef PROFILE
225 if (pledge("stdio", NULL) == -1)
226 err(1, "pledge");
227 #endif
229 while ((ch = getopt(argc, argv, "v")) != -1) {
230 switch (ch) {
231 case 'v':
232 verbose = 1;
233 break;
234 default:
235 usage();
236 return 1;
239 argc -= optind;
240 argv += optind;
242 RUN_TEST(path_cmp(), "path_cmp");
243 RUN_TEST(path_list(), "path_list");
244 RUN_TEST(path_list_reverse_input(), "path_list_reverse_input");
246 return failure ? 1 : 0;