Blame


1 1c11b35c 2019-01-12 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 1c11b35c 2019-01-12 stsp *
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.
7 1c11b35c 2019-01-12 stsp *
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.
15 1c11b35c 2019-01-12 stsp */
16 1c11b35c 2019-01-12 stsp
17 1c11b35c 2019-01-12 stsp #include <stdlib.h>
18 1c11b35c 2019-01-12 stsp #include <stdarg.h>
19 1c11b35c 2019-01-12 stsp #include <stdio.h>
20 1c11b35c 2019-01-12 stsp #include <unistd.h>
21 1c11b35c 2019-01-12 stsp #include <err.h>
22 1c11b35c 2019-01-12 stsp
23 1c11b35c 2019-01-12 stsp #include "got_error.h"
24 1c11b35c 2019-01-12 stsp
25 1c11b35c 2019-01-12 stsp #include "got_lib_path.h"
26 1c11b35c 2019-01-12 stsp
27 1c11b35c 2019-01-12 stsp #ifndef nitems
28 1c11b35c 2019-01-12 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
29 1c11b35c 2019-01-12 stsp #endif
30 1c11b35c 2019-01-12 stsp
31 1c11b35c 2019-01-12 stsp static int verbose;
32 1c11b35c 2019-01-12 stsp
33 1c11b35c 2019-01-12 stsp void
34 1c11b35c 2019-01-12 stsp test_printf(char *fmt, ...)
35 1c11b35c 2019-01-12 stsp {
36 1c11b35c 2019-01-12 stsp va_list ap;
37 1c11b35c 2019-01-12 stsp
38 1c11b35c 2019-01-12 stsp if (!verbose)
39 1c11b35c 2019-01-12 stsp return;
40 1c11b35c 2019-01-12 stsp
41 1c11b35c 2019-01-12 stsp va_start(ap, fmt);
42 1c11b35c 2019-01-12 stsp vprintf(fmt, ap);
43 1c11b35c 2019-01-12 stsp va_end(ap);
44 1c11b35c 2019-01-12 stsp }
45 1c11b35c 2019-01-12 stsp
46 1c11b35c 2019-01-12 stsp static int
47 1beed999 2019-01-12 stsp path_cmp(void)
48 1c11b35c 2019-01-12 stsp {
49 1c11b35c 2019-01-12 stsp struct path_cmp_test {
50 1c11b35c 2019-01-12 stsp const char *path1;
51 1c11b35c 2019-01-12 stsp const char *path2;
52 1c11b35c 2019-01-12 stsp int expected;
53 1c11b35c 2019-01-12 stsp } test_data[] = {
54 1c11b35c 2019-01-12 stsp { "", "", 0 },
55 1c11b35c 2019-01-12 stsp { "/", "/", 0 },
56 1c11b35c 2019-01-12 stsp { "/a", "/b", -1 },
57 1c11b35c 2019-01-12 stsp { "x/a", "x.a", -1 },
58 1c11b35c 2019-01-12 stsp { "x.a", "x/a", 1 },
59 1c11b35c 2019-01-12 stsp { "//foo", "/bar", -1 },
60 1c11b35c 2019-01-12 stsp { "/foo", "/bar", 1 },
61 1c11b35c 2019-01-12 stsp { "/foo/sub", "/bar", 1 },
62 1c11b35c 2019-01-12 stsp { "/foo", "/bar/sub", 1 },
63 1c11b35c 2019-01-12 stsp { "/foo/", "/bar", 1 },
64 1c11b35c 2019-01-12 stsp { "/foo", "/bar/", 1 },
65 1c11b35c 2019-01-12 stsp { "/foo/", "/bar/", 1 },
66 1c11b35c 2019-01-12 stsp { "/bar/", "/bar/", 0 },
67 1c11b35c 2019-01-12 stsp { "/bar/sub", "/bar/", 1 },
68 1c11b35c 2019-01-12 stsp { "/bar/sub/sub2", "/bar/", 1 },
69 1c11b35c 2019-01-12 stsp { "/bar/sub/sub2", "/bar", 1 },
70 1c11b35c 2019-01-12 stsp { "/bar.sub.sub2", "/bar", 1 },
71 1c11b35c 2019-01-12 stsp { "/bar/sub/sub2", "/bar.c", -1 },
72 1c11b35c 2019-01-12 stsp };
73 1c11b35c 2019-01-12 stsp int i;
74 1c11b35c 2019-01-12 stsp
75 1c11b35c 2019-01-12 stsp for (i = 0; i < nitems(test_data); i++) {
76 1c11b35c 2019-01-12 stsp const char *path1 = test_data[i].path1;
77 1c11b35c 2019-01-12 stsp const char *path2 = test_data[i].path2;
78 1c11b35c 2019-01-12 stsp int expected = test_data[i].expected;
79 1beed999 2019-01-12 stsp int cmp = got_path_cmp(path1, path2);
80 1c11b35c 2019-01-12 stsp
81 1c11b35c 2019-01-12 stsp if (cmp != expected) {
82 1c11b35c 2019-01-12 stsp test_printf("%d: '%s' vs '%s' == %d; expected %d\n",
83 1c11b35c 2019-01-12 stsp i, path1, path2, cmp, expected);
84 1c11b35c 2019-01-12 stsp return 0;
85 1c11b35c 2019-01-12 stsp }
86 1c11b35c 2019-01-12 stsp }
87 1c11b35c 2019-01-12 stsp
88 1c11b35c 2019-01-12 stsp return 1;
89 1c11b35c 2019-01-12 stsp }
90 1c11b35c 2019-01-12 stsp
91 1c11b35c 2019-01-12 stsp #define RUN_TEST(expr, name) \
92 1c11b35c 2019-01-12 stsp { test_ok = (expr); \
93 1c11b35c 2019-01-12 stsp printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
94 1c11b35c 2019-01-12 stsp failure = (failure || !test_ok); }
95 1c11b35c 2019-01-12 stsp
96 1c11b35c 2019-01-12 stsp void
97 1c11b35c 2019-01-12 stsp usage(void)
98 1c11b35c 2019-01-12 stsp {
99 1c11b35c 2019-01-12 stsp fprintf(stderr, "usage: path_test [-v]\n");
100 1c11b35c 2019-01-12 stsp }
101 1c11b35c 2019-01-12 stsp
102 1c11b35c 2019-01-12 stsp int
103 1c11b35c 2019-01-12 stsp main(int argc, char *argv[])
104 1c11b35c 2019-01-12 stsp {
105 1c11b35c 2019-01-12 stsp int test_ok = 0, failure = 0;
106 1c11b35c 2019-01-12 stsp int ch;
107 1c11b35c 2019-01-12 stsp
108 1c11b35c 2019-01-12 stsp #ifndef PROFILE
109 1c11b35c 2019-01-12 stsp if (pledge("stdio", NULL) == -1)
110 1c11b35c 2019-01-12 stsp err(1, "pledge");
111 1c11b35c 2019-01-12 stsp #endif
112 1c11b35c 2019-01-12 stsp
113 1c11b35c 2019-01-12 stsp while ((ch = getopt(argc, argv, "v")) != -1) {
114 1c11b35c 2019-01-12 stsp switch (ch) {
115 1c11b35c 2019-01-12 stsp case 'v':
116 1c11b35c 2019-01-12 stsp verbose = 1;
117 1c11b35c 2019-01-12 stsp break;
118 1c11b35c 2019-01-12 stsp default:
119 1c11b35c 2019-01-12 stsp usage();
120 1c11b35c 2019-01-12 stsp return 1;
121 1c11b35c 2019-01-12 stsp }
122 1c11b35c 2019-01-12 stsp }
123 1c11b35c 2019-01-12 stsp argc -= optind;
124 1c11b35c 2019-01-12 stsp argv += optind;
125 1c11b35c 2019-01-12 stsp
126 1beed999 2019-01-12 stsp RUN_TEST(path_cmp(), "path_cmp");
127 1c11b35c 2019-01-12 stsp
128 1c11b35c 2019-01-12 stsp return failure ? 1 : 0;
129 1c11b35c 2019-01-12 stsp }