xref: /freebsd/crypto/krb5/src/util/support/t_path.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* util/support/t_path.c - Path manipulation tests */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert  * Copyright (C) 2011 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert  * All rights reserved.
6*7f2fe78bSCy Schubert  *
7*7f2fe78bSCy Schubert  * Export of this software from the United States of America may
8*7f2fe78bSCy Schubert  *   require a specific license from the United States Government.
9*7f2fe78bSCy Schubert  *   It is the responsibility of any person or organization contemplating
10*7f2fe78bSCy Schubert  *   export to obtain such a license before exporting.
11*7f2fe78bSCy Schubert  *
12*7f2fe78bSCy Schubert  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13*7f2fe78bSCy Schubert  * distribute this software and its documentation for any purpose and
14*7f2fe78bSCy Schubert  * without fee is hereby granted, provided that the above copyright
15*7f2fe78bSCy Schubert  * notice appear in all copies and that both that copyright notice and
16*7f2fe78bSCy Schubert  * this permission notice appear in supporting documentation, and that
17*7f2fe78bSCy Schubert  * the name of M.I.T. not be used in advertising or publicity pertaining
18*7f2fe78bSCy Schubert  * to distribution of the software without specific, written prior
19*7f2fe78bSCy Schubert  * permission.  Furthermore if you modify this software you must label
20*7f2fe78bSCy Schubert  * your software as modified software and not distribute it in such a
21*7f2fe78bSCy Schubert  * fashion that it might be confused with the original M.I.T. software.
22*7f2fe78bSCy Schubert  * M.I.T. makes no representations about the suitability of
23*7f2fe78bSCy Schubert  * this software for any purpose.  It is provided "as is" without express
24*7f2fe78bSCy Schubert  * or implied warranty.
25*7f2fe78bSCy Schubert  */
26*7f2fe78bSCy Schubert 
27*7f2fe78bSCy Schubert #include <k5-platform.h>
28*7f2fe78bSCy Schubert 
29*7f2fe78bSCy Schubert /* For testing purposes, use a different symbol for Windows path semantics. */
30*7f2fe78bSCy Schubert #ifdef _WIN32
31*7f2fe78bSCy Schubert #define WINDOWS_PATHS
32*7f2fe78bSCy Schubert #endif
33*7f2fe78bSCy Schubert 
34*7f2fe78bSCy Schubert /*
35*7f2fe78bSCy Schubert  * The ultimate arbiter of these tests is the dirname, basename, and isabs
36*7f2fe78bSCy Schubert  * methods of the Python posixpath and ntpath modules.
37*7f2fe78bSCy Schubert  */
38*7f2fe78bSCy Schubert 
39*7f2fe78bSCy Schubert struct {
40*7f2fe78bSCy Schubert     const char *path;
41*7f2fe78bSCy Schubert     const char *posix_dirname;
42*7f2fe78bSCy Schubert     const char *posix_basename;
43*7f2fe78bSCy Schubert     const char *win_dirname;
44*7f2fe78bSCy Schubert     const char *win_basename;
45*7f2fe78bSCy Schubert } split_tests[] = {
46*7f2fe78bSCy Schubert     { "",          "",      "",          "",       ""  },
47*7f2fe78bSCy Schubert     { "a/b/c",     "a/b",   "c",         "a/b",    "c" },
48*7f2fe78bSCy Schubert     { "a/b/",      "a/b",   "",          "a/b",    ""  },
49*7f2fe78bSCy Schubert     { "a\\b\\c",   "",      "a\\b\\c",   "a\\b",   "c" },
50*7f2fe78bSCy Schubert     { "a\\b\\",    "",      "a\\b\\",    "a\\b",   ""  },
51*7f2fe78bSCy Schubert     { "a/b\\c",    "a",     "b\\c",      "a/b",    "c" },
52*7f2fe78bSCy Schubert     { "a//b",      "a",     "b",         "a",      "b" },
53*7f2fe78bSCy Schubert     { "a/\\/b",    "a/\\",  "b",         "a",      "b" },
54*7f2fe78bSCy Schubert     { "a//b/c",    "a//b",  "c",         "a//b",   "c" },
55*7f2fe78bSCy Schubert 
56*7f2fe78bSCy Schubert     { "/",         "/",     "",          "/",      ""  },
57*7f2fe78bSCy Schubert     { "\\",        "",      "\\",        "\\",     ""  },
58*7f2fe78bSCy Schubert     { "/a/b/c",    "/a/b",  "c",         "/a/b",   "c" },
59*7f2fe78bSCy Schubert     { "\\a/b/c",   "\\a/b", "c",         "\\a/b",  "c" },
60*7f2fe78bSCy Schubert     { "/a",        "/",     "a",         "/",      "a" },
61*7f2fe78bSCy Schubert     { "//a",       "//",    "a",         "//",     "a" },
62*7f2fe78bSCy Schubert     { "\\//\\a",   "\\",    "\\a",       "\\//\\", "a" },
63*7f2fe78bSCy Schubert 
64*7f2fe78bSCy Schubert     { "/:",        "/",     ":",         "/:",     ""  },
65*7f2fe78bSCy Schubert     { "c:\\",      "",      "c:\\",      "c:\\",   ""  },
66*7f2fe78bSCy Schubert     { "c:/",       "c:",    "",          "c:/",    ""  },
67*7f2fe78bSCy Schubert     { "c:/\\a",    "c:",    "\\a",       "c:/\\",  "a" },
68*7f2fe78bSCy Schubert     { "c:a",       "",      "c:a",       "c:",     "a" },
69*7f2fe78bSCy Schubert };
70*7f2fe78bSCy Schubert 
71*7f2fe78bSCy Schubert struct {
72*7f2fe78bSCy Schubert     const char *path1;
73*7f2fe78bSCy Schubert     const char *path2;
74*7f2fe78bSCy Schubert     const char *posix_result;
75*7f2fe78bSCy Schubert     const char *win_result;
76*7f2fe78bSCy Schubert } join_tests[] = {
77*7f2fe78bSCy Schubert     { "",     "",     "",         ""      },
78*7f2fe78bSCy Schubert     { "",     "a",    "a",        "a"     },
79*7f2fe78bSCy Schubert     { "",     "/a",   "/a",       "/a"    },
80*7f2fe78bSCy Schubert     { "",     "c:",   "c:",       "c:"    },
81*7f2fe78bSCy Schubert 
82*7f2fe78bSCy Schubert     { "a",    "",     "a/",       "a\\"   },
83*7f2fe78bSCy Schubert     { "a/",   "",     "a/",       "a/"    },
84*7f2fe78bSCy Schubert     { "a\\",  "",     "a\\/",     "a\\"   },
85*7f2fe78bSCy Schubert     { "a/\\", "",     "a/\\/",    "a/\\"  },
86*7f2fe78bSCy Schubert 
87*7f2fe78bSCy Schubert     { "a",    "b",    "a/b",      "a\\b"  },
88*7f2fe78bSCy Schubert     { "a",    "/b",   "/b",       "/b"    },
89*7f2fe78bSCy Schubert     { "a",    "c:",   "a/c:",     "a\\c:" },
90*7f2fe78bSCy Schubert     { "a",    "c:/",  "a/c:/",    "c:/"   },
91*7f2fe78bSCy Schubert     { "a",    "c:/a", "a/c:/a",   "c:/a"  },
92*7f2fe78bSCy Schubert     { "a",    "/:",   "/:",       "a/:"   },
93*7f2fe78bSCy Schubert     { "a/",   "b",    "a/b",      "a/b"   },
94*7f2fe78bSCy Schubert     { "a/",   "",     "a/",       "a/"    },
95*7f2fe78bSCy Schubert     { "a\\",  "b",    "a\\/b",    "a\\b"  },
96*7f2fe78bSCy Schubert 
97*7f2fe78bSCy Schubert     { "a//",  "b",    "a//b",     "a//b"  },
98*7f2fe78bSCy Schubert     { "a/\\", "b",    "a/\\/b",   "a/\\b" },
99*7f2fe78bSCy Schubert };
100*7f2fe78bSCy Schubert 
101*7f2fe78bSCy Schubert struct {
102*7f2fe78bSCy Schubert     const char *path;
103*7f2fe78bSCy Schubert     int posix_result;
104*7f2fe78bSCy Schubert     int win_result;
105*7f2fe78bSCy Schubert } isabs_tests[] = {
106*7f2fe78bSCy Schubert     { "",      0, 0 },
107*7f2fe78bSCy Schubert     { "/",     1, 1 },
108*7f2fe78bSCy Schubert     { "/a",    1, 1 },
109*7f2fe78bSCy Schubert     { "a/b",   0, 0 },
110*7f2fe78bSCy Schubert     { "\\",    0, 1 },
111*7f2fe78bSCy Schubert     { "\\a",   0, 1 },
112*7f2fe78bSCy Schubert     { "c:",    0, 0 },
113*7f2fe78bSCy Schubert     { "/:",    1, 0 },
114*7f2fe78bSCy Schubert     { "\\:",   0, 0 },
115*7f2fe78bSCy Schubert     { "c:/a",  0, 1 },
116*7f2fe78bSCy Schubert     { "c:\\a", 0, 1 },
117*7f2fe78bSCy Schubert     { "c:a",   0, 0 },
118*7f2fe78bSCy Schubert     { "c:a/b", 0, 0 },
119*7f2fe78bSCy Schubert     { "/:a/b", 1, 0 },
120*7f2fe78bSCy Schubert };
121*7f2fe78bSCy Schubert 
122*7f2fe78bSCy Schubert int
main(void)123*7f2fe78bSCy Schubert main(void)
124*7f2fe78bSCy Schubert {
125*7f2fe78bSCy Schubert     char *dirname, *basename, *joined;
126*7f2fe78bSCy Schubert     const char *edirname, *ebasename, *ejoined, *ipath, *path1, *path2;
127*7f2fe78bSCy Schubert     int result, eresult, status = 0;
128*7f2fe78bSCy Schubert     size_t i;
129*7f2fe78bSCy Schubert 
130*7f2fe78bSCy Schubert     for (i = 0; i < sizeof(split_tests) / sizeof(*split_tests); i++) {
131*7f2fe78bSCy Schubert         ipath = split_tests[i].path;
132*7f2fe78bSCy Schubert #ifdef WINDOWS_PATHS
133*7f2fe78bSCy Schubert         edirname = split_tests[i].win_dirname;
134*7f2fe78bSCy Schubert         ebasename = split_tests[i].win_basename;
135*7f2fe78bSCy Schubert #else
136*7f2fe78bSCy Schubert         edirname = split_tests[i].posix_dirname;
137*7f2fe78bSCy Schubert         ebasename = split_tests[i].posix_basename;
138*7f2fe78bSCy Schubert #endif
139*7f2fe78bSCy Schubert         if (k5_path_split(ipath, NULL, NULL) != 0)
140*7f2fe78bSCy Schubert             abort();
141*7f2fe78bSCy Schubert         if (k5_path_split(ipath, &dirname, NULL) != 0)
142*7f2fe78bSCy Schubert             abort();
143*7f2fe78bSCy Schubert         free(dirname);
144*7f2fe78bSCy Schubert         if (k5_path_split(ipath, NULL, &basename) != 0)
145*7f2fe78bSCy Schubert             abort();
146*7f2fe78bSCy Schubert         free(basename);
147*7f2fe78bSCy Schubert         if (k5_path_split(ipath, &dirname, &basename) != 0)
148*7f2fe78bSCy Schubert             abort();
149*7f2fe78bSCy Schubert         if (strcmp(dirname, edirname) != 0) {
150*7f2fe78bSCy Schubert             fprintf(stderr, "Split test %d: dirname %s != expected %s\n",
151*7f2fe78bSCy Schubert                     (int)i, dirname, edirname);
152*7f2fe78bSCy Schubert             status = 1;
153*7f2fe78bSCy Schubert         }
154*7f2fe78bSCy Schubert         if (strcmp(basename, ebasename) != 0) {
155*7f2fe78bSCy Schubert             fprintf(stderr, "Split test %d: basename %s != expected %s\n",
156*7f2fe78bSCy Schubert                     (int)i, basename, ebasename);
157*7f2fe78bSCy Schubert             status = 1;
158*7f2fe78bSCy Schubert         }
159*7f2fe78bSCy Schubert         free(dirname);
160*7f2fe78bSCy Schubert         free(basename);
161*7f2fe78bSCy Schubert     }
162*7f2fe78bSCy Schubert 
163*7f2fe78bSCy Schubert     for (i = 0; i < sizeof(join_tests) / sizeof(*join_tests); i++) {
164*7f2fe78bSCy Schubert         path1 = join_tests[i].path1;
165*7f2fe78bSCy Schubert         path2 = join_tests[i].path2;
166*7f2fe78bSCy Schubert #ifdef WINDOWS_PATHS
167*7f2fe78bSCy Schubert         ejoined = join_tests[i].win_result;
168*7f2fe78bSCy Schubert #else
169*7f2fe78bSCy Schubert         ejoined = join_tests[i].posix_result;
170*7f2fe78bSCy Schubert #endif
171*7f2fe78bSCy Schubert         if (k5_path_join(path1, path2, &joined) != 0)
172*7f2fe78bSCy Schubert             abort();
173*7f2fe78bSCy Schubert         if (strcmp(joined, ejoined) != 0) {
174*7f2fe78bSCy Schubert             fprintf(stderr, "Join test %d: %s != expected %s\n",
175*7f2fe78bSCy Schubert                     (int)i, joined, ejoined);
176*7f2fe78bSCy Schubert             status = 1;
177*7f2fe78bSCy Schubert         }
178*7f2fe78bSCy Schubert         free(joined);
179*7f2fe78bSCy Schubert     }
180*7f2fe78bSCy Schubert 
181*7f2fe78bSCy Schubert     for (i = 0; i < sizeof(isabs_tests) / sizeof(*isabs_tests); i++) {
182*7f2fe78bSCy Schubert #ifdef WINDOWS_PATHS
183*7f2fe78bSCy Schubert         eresult = isabs_tests[i].win_result;
184*7f2fe78bSCy Schubert #else
185*7f2fe78bSCy Schubert         eresult = isabs_tests[i].posix_result;
186*7f2fe78bSCy Schubert #endif
187*7f2fe78bSCy Schubert         result = k5_path_isabs(isabs_tests[i].path);
188*7f2fe78bSCy Schubert         if (result != eresult) {
189*7f2fe78bSCy Schubert             fprintf(stderr, "isabs test %d: %d != expected %d\n",
190*7f2fe78bSCy Schubert                     (int)i, result, eresult);
191*7f2fe78bSCy Schubert             status = 1;
192*7f2fe78bSCy Schubert         }
193*7f2fe78bSCy Schubert     }
194*7f2fe78bSCy Schubert 
195*7f2fe78bSCy Schubert     return status;
196*7f2fe78bSCy Schubert }
197