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 */
18 #include <string.h>
19 #include <stdlib.h>
20 #include <stdarg.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <err.h>
25 #include "got_error.h"
26 #include "got_path.h"
28 #ifndef nitems
29 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
30 #endif
32 static int verbose;
33 static int quiet;
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 size_t 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,
88 strlen(path1), strlen(path2));
90 if (cmp != expected) {
91 test_printf("%d: '%s' vs '%s' == %d; expected %d\n",
92 i, path1, path2, cmp, expected);
93 return 0;
94 }
95 }
97 return 1;
98 }
100 const char *path_list_input[] = {
101 "", "/", "a", "/b", "/bar", "bar/sub", "/bar/sub", "/bar/",
102 "/bar.c", "/bar/sub/sub2", "/bar.sub.sub2", "/foo",
103 "/foo/sub", "/foo/", "/foo/", "x/a",
104 };
105 const char *path_list_expected[] = {
106 "",
107 "a",
108 "/b",
109 "/bar",
110 "bar/sub",
111 "/bar/sub/sub2",
112 "/bar.c",
113 "/bar.sub.sub2",
114 "/foo",
115 "/foo/sub",
116 "x/a",
117 };
119 /* If inserting pathlist_input in reverse the result is slightly different. */
120 const char *path_list_expected_reverse[] = {
121 "/",
122 "a",
123 "/b",
124 "/bar/",
125 "/bar/sub",
126 "/bar/sub/sub2",
127 "/bar.c",
128 "/bar.sub.sub2",
129 "/foo/",
130 "/foo/sub",
131 "x/a",
132 };
135 static int
136 path_list(void)
138 const struct got_error *err = NULL;
139 struct got_pathlist_head paths;
140 struct got_pathlist_entry *pe;
141 size_t i;
143 TAILQ_INIT(&paths);
144 for (i = 0; i < nitems(path_list_input); i++) {
145 err = got_pathlist_insert(NULL, &paths, path_list_input[i],
146 NULL);
147 if (err) {
148 test_printf("%s\n", __func__, err->msg);
149 return 0;
153 i = 0;
154 TAILQ_FOREACH(pe, &paths, entry) {
155 test_printf("'%s' -- '%s'\n", pe->path, path_list_expected[i]);
156 if (i >= nitems(path_list_expected)) {
157 test_printf("too many elements on list\n");
158 return 0;
160 if (strcmp(pe->path, path_list_expected[i]) != 0) {
161 test_printf("unordered elements on list\n");
162 return 0;
164 i++;
167 got_pathlist_free(&paths);
168 return 1;
171 static int
172 path_list_reverse_input(void)
174 const struct got_error *err = NULL;
175 struct got_pathlist_head paths;
176 struct got_pathlist_entry *pe;
177 size_t i;
179 TAILQ_INIT(&paths);
180 for (i = nitems(path_list_input); i > 0;) {
181 err = got_pathlist_insert(NULL, &paths, path_list_input[--i],
182 NULL);
183 if (err) {
184 test_printf("%s\n", __func__, err->msg);
185 return 0;
189 i = 0;
190 TAILQ_FOREACH(pe, &paths, entry) {
191 test_printf("'%s' -- '%s'\n", pe->path,
192 path_list_expected_reverse[i]);
193 if (i >= nitems(path_list_expected_reverse)) {
194 test_printf("too many elements on list\n");
195 return 0;
197 if (strcmp(pe->path, path_list_expected_reverse[i]) != 0) {
198 test_printf("unordered elements on list\n");
199 return 0;
201 i++;
204 got_pathlist_free(&paths);
205 return 1;
208 #define RUN_TEST(expr, name) \
209 { test_ok = (expr); \
210 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
211 failure = (failure || !test_ok); }
213 void
214 usage(void)
216 fprintf(stderr, "usage: path_test [-v] [-q]\n");
219 int
220 main(int argc, char *argv[])
222 int test_ok = 0, failure = 0;
223 int ch;
225 #ifndef PROFILE
226 if (pledge("stdio", NULL) == -1)
227 err(1, "pledge");
228 #endif
230 while ((ch = getopt(argc, argv, "vq")) != -1) {
231 switch (ch) {
232 case 'v':
233 verbose = 1;
234 quiet = 0;
235 break;
236 case 'q':
237 quiet = 1;
238 verbose = 0;
239 break;
240 default:
241 usage();
242 return 1;
245 argc -= optind;
246 argv += optind;
248 RUN_TEST(path_cmp(), "path_cmp");
249 RUN_TEST(path_list(), "path_list");
250 RUN_TEST(path_list_reverse_input(), "path_list_reverse_input");
252 return failure ? 1 : 0;