]> git.ipfire.org Git - thirdparty/git.git/blame - t/helper/test-path-utils.c
setup.h: move declarations for setup.c functions from cache.h
[thirdparty/git.git] / t / helper / test-path-utils.c
CommitLineData
b8d5cf4f 1#include "test-tool.h"
ae299be0 2#include "cache.h"
0b027f6c 3#include "abspath.h"
32a8f510 4#include "environment.h"
e38da487 5#include "setup.h"
31171d9e 6#include "string-list.h"
dc2d9ba3 7#include "utf8.h"
ae299be0 8
9e2326c7
MH
9/*
10 * A "string_list_each_func_t" function that normalizes an entry from
11 * GIT_CEILING_DIRECTORIES. If the path is unusable for some reason,
12 * die with an explanation.
13 */
1ee34710
JK
14static int normalize_ceiling_entry(struct string_list_item *item,
15 void *data UNUSED)
9e2326c7 16{
62f17513 17 char *ceil = item->string;
9e2326c7 18
62f17513 19 if (!*ceil)
9e2326c7 20 die("Empty path is not supported");
9e2326c7
MH
21 if (!is_absolute_path(ceil))
22 die("Path \"%s\" is not absolute", ceil);
62f17513 23 if (normalize_path_copy(ceil, ceil) < 0)
9e2326c7 24 die("Path \"%s\" could not be normalized", ceil);
9e2326c7
MH
25 return 1;
26}
27
203439b2
JX
28static void normalize_argv_string(const char **var, const char *input)
29{
30 if (!strcmp(input, "<null>"))
31 *var = NULL;
32 else if (!strcmp(input, "<empty>"))
33 *var = "";
34 else
35 *var = input;
36
37 if (*var && (**var == '<' || **var == '('))
38 die("Bad value: %s\n", input);
39}
40
7d1aaa68
JS
41struct test_data {
42 const char *from; /* input: transform from this ... */
43 const char *to; /* output: ... to this. */
371471ce 44 const char *alternative; /* output: ... or this. */
7d1aaa68
JS
45};
46
29c2eda8
RS
47/*
48 * Compatibility wrappers for OpenBSD, whose basename(3) and dirname(3)
49 * have const parameters.
50 */
51static char *posix_basename(char *path)
52{
53 return basename(path);
54}
55
56static char *posix_dirname(char *path)
57{
58 return dirname(path);
59}
60
7d1aaa68
JS
61static int test_function(struct test_data *data, char *(*func)(char *input),
62 const char *funcname)
63{
64 int failed = 0, i;
65 char buffer[1024];
66 char *to;
67
68 for (i = 0; data[i].to; i++) {
69 if (!data[i].from)
70 to = func(NULL);
71 else {
7b11a18a 72 xsnprintf(buffer, sizeof(buffer), "%s", data[i].from);
7d1aaa68
JS
73 to = func(buffer);
74 }
371471ce
JS
75 if (!strcmp(to, data[i].to))
76 continue;
77 if (!data[i].alternative)
7d1aaa68
JS
78 error("FAIL: %s(%s) => '%s' != '%s'\n",
79 funcname, data[i].from, to, data[i].to);
371471ce
JS
80 else if (!strcmp(to, data[i].alternative))
81 continue;
82 else
83 error("FAIL: %s(%s) => '%s' != '%s', '%s'\n",
84 funcname, data[i].from, to, data[i].to,
85 data[i].alternative);
86 failed = 1;
7d1aaa68
JS
87 }
88 return failed;
89}
90
91static struct test_data basename_data[] = {
92 /* --- POSIX type paths --- */
93 { NULL, "." },
94 { "", "." },
95 { ".", "." },
96 { "..", ".." },
97 { "/", "/" },
371471ce
JS
98 { "//", "/", "//" },
99 { "///", "/", "//" },
100 { "////", "/", "//" },
7d1aaa68
JS
101 { "usr", "usr" },
102 { "/usr", "usr" },
103 { "/usr/", "usr" },
104 { "/usr//", "usr" },
105 { "/usr/lib", "lib" },
106 { "usr/lib", "lib" },
107 { "usr/lib///", "lib" },
108
109#if defined(__MINGW32__) || defined(_MSC_VER)
7d1aaa68
JS
110 /* --- win32 type paths --- */
111 { "\\usr", "usr" },
112 { "\\usr\\", "usr" },
113 { "\\usr\\\\", "usr" },
114 { "\\usr\\lib", "lib" },
115 { "usr\\lib", "lib" },
116 { "usr\\lib\\\\\\", "lib" },
117 { "C:/usr", "usr" },
118 { "C:/usr", "usr" },
119 { "C:/usr/", "usr" },
120 { "C:/usr//", "usr" },
121 { "C:/usr/lib", "lib" },
122 { "C:usr/lib", "lib" },
123 { "C:usr/lib///", "lib" },
124 { "C:", "." },
125 { "C:a", "a" },
126 { "C:/", "/" },
127 { "C:///", "/" },
371471ce
JS
128 { "\\", "\\", "/" },
129 { "\\\\", "\\", "/" },
130 { "\\\\\\", "\\", "/" },
7d1aaa68
JS
131#endif
132 { NULL, NULL }
133};
134
135static struct test_data dirname_data[] = {
136 /* --- POSIX type paths --- */
137 { NULL, "." },
138 { "", "." },
139 { ".", "." },
140 { "..", "." },
141 { "/", "/" },
371471ce
JS
142 { "//", "/", "//" },
143 { "///", "/", "//" },
144 { "////", "/", "//" },
7d1aaa68
JS
145 { "usr", "." },
146 { "/usr", "/" },
147 { "/usr/", "/" },
148 { "/usr//", "/" },
149 { "/usr/lib", "/usr" },
150 { "usr/lib", "usr" },
151 { "usr/lib///", "usr" },
152
153#if defined(__MINGW32__) || defined(_MSC_VER)
7d1aaa68
JS
154 /* --- win32 type paths --- */
155 { "\\", "\\" },
156 { "\\\\", "\\\\" },
157 { "\\usr", "\\" },
158 { "\\usr\\", "\\" },
159 { "\\usr\\\\", "\\" },
160 { "\\usr\\lib", "\\usr" },
161 { "usr\\lib", "usr" },
162 { "usr\\lib\\\\\\", "usr" },
163 { "C:a", "C:." },
164 { "C:/", "C:/" },
165 { "C:///", "C:/" },
166 { "C:/usr", "C:/" },
167 { "C:/usr/", "C:/" },
168 { "C:/usr//", "C:/" },
169 { "C:/usr/lib", "C:/usr" },
170 { "C:usr/lib", "C:usr" },
171 { "C:usr/lib///", "C:usr" },
172 { "\\\\\\", "\\" },
173 { "\\\\\\\\", "\\" },
371471ce 174 { "C:", "C:.", "." },
7d1aaa68
JS
175#endif
176 { NULL, NULL }
177};
178
801ed010
JK
179static int check_dotfile(const char *x, const char **argv,
180 int (*is_hfs)(const char *),
181 int (*is_ntfs)(const char *))
dc2d9ba3 182{
801ed010
JK
183 int res = 0, expect = 1;
184 for (; *argv; argv++) {
185 if (!strcmp("--not", *argv))
186 expect = !expect;
187 else if (expect != (is_hfs(*argv) || is_ntfs(*argv)))
188 res = error("'%s' is %s.git%s", *argv,
189 expect ? "not " : "", x);
190 else
191 fprintf(stderr, "ok: '%s' is %s.git%s\n",
192 *argv, expect ? "" : "not ", x);
193 }
194 return !!res;
dc2d9ba3
JS
195}
196
b819f1d2
JS
197static int cmp_by_st_size(const void *a, const void *b)
198{
199 intptr_t x = (intptr_t)((struct string_list_item *)a)->util;
200 intptr_t y = (intptr_t)((struct string_list_item *)b)->util;
201
202 return x > y ? -1 : (x < y ? +1 : 0);
203}
204
a62f9d1a
GS
205/*
206 * A very simple, reproducible pseudo-random generator. Copied from
207 * `test-genrandom.c`.
208 */
209static uint64_t my_random_value = 1234;
210
211static uint64_t my_random(void)
212{
213 my_random_value = my_random_value * 1103515245 + 12345;
214 return my_random_value;
215}
216
217/*
218 * A fast approximation of the square root, without requiring math.h.
219 *
220 * It uses Newton's method to approximate the solution of 0 = x^2 - value.
221 */
222static double my_sqrt(double value)
223{
224 const double epsilon = 1e-6;
225 double x = value;
226
227 if (value == 0)
228 return 0;
229
230 for (;;) {
231 double delta = (value / x - x) / 2;
232 if (delta < epsilon && delta > -epsilon)
233 return x + delta;
234 x += delta;
235 }
236}
237
238static int protect_ntfs_hfs_benchmark(int argc, const char **argv)
239{
240 size_t i, j, nr, min_len = 3, max_len = 20;
241 char **names;
242 int repetitions = 15, file_mode = 0100644;
243 uint64_t begin, end;
244 double m[3][2], v[3][2];
245 uint64_t cumul;
246 double cumul2;
247
248 if (argc > 1 && !strcmp(argv[1], "--with-symlink-mode")) {
249 file_mode = 0120000;
250 argc--;
251 argv++;
252 }
253
254 nr = argc > 1 ? strtoul(argv[1], NULL, 0) : 1000000;
255 ALLOC_ARRAY(names, nr);
256
257 if (argc > 2) {
258 min_len = strtoul(argv[2], NULL, 0);
259 if (argc > 3)
260 max_len = strtoul(argv[3], NULL, 0);
261 if (min_len > max_len)
262 die("min_len > max_len");
263 }
264
265 for (i = 0; i < nr; i++) {
266 size_t len = min_len + (my_random() % (max_len + 1 - min_len));
267
268 names[i] = xmallocz(len);
269 while (len > 0)
270 names[i][--len] = (char)(' ' + (my_random() % ('\x7f' - ' ')));
271 }
272
273 for (protect_ntfs = 0; protect_ntfs < 2; protect_ntfs++)
274 for (protect_hfs = 0; protect_hfs < 2; protect_hfs++) {
275 cumul = 0;
276 cumul2 = 0;
277 for (i = 0; i < repetitions; i++) {
278 begin = getnanotime();
279 for (j = 0; j < nr; j++)
280 verify_path(names[j], file_mode);
281 end = getnanotime();
282 printf("protect_ntfs = %d, protect_hfs = %d: %lfms\n", protect_ntfs, protect_hfs, (end-begin) / (double)1e6);
283 cumul += end - begin;
284 cumul2 += (end - begin) * (end - begin);
285 }
286 m[protect_ntfs][protect_hfs] = cumul / (double)repetitions;
287 v[protect_ntfs][protect_hfs] = my_sqrt(cumul2 / (double)repetitions - m[protect_ntfs][protect_hfs] * m[protect_ntfs][protect_hfs]);
288 printf("mean: %lfms, stddev: %lfms\n", m[protect_ntfs][protect_hfs] / (double)1e6, v[protect_ntfs][protect_hfs] / (double)1e6);
289 }
290
291 for (protect_ntfs = 0; protect_ntfs < 2; protect_ntfs++)
292 for (protect_hfs = 0; protect_hfs < 2; protect_hfs++)
293 printf("ntfs=%d/hfs=%d: %lf%% slower\n", protect_ntfs, protect_hfs, (m[protect_ntfs][protect_hfs] - m[0][0]) * 100 / m[0][0]);
294
295 return 0;
296}
297
b8d5cf4f 298int cmd__path_utils(int argc, const char **argv)
ae299be0 299{
f42302b4 300 if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) {
62f17513 301 char *buf = xmallocz(strlen(argv[2]));
f42302b4 302 int rv = normalize_path_copy(buf, argv[2]);
e287a5b0
ÆAB
303 puts(rv ? "++failed++" : buf);
304 free(buf);
2cd85c40 305 return 0;
ae299be0
DR
306 }
307
e2a57aac 308 if (argc >= 2 && !strcmp(argv[1], "real_path")) {
3d7747e3 309 struct strbuf realpath = STRBUF_INIT;
d553e737 310 while (argc > 2) {
3d7747e3
AM
311 strbuf_realpath(&realpath, argv[2], 1);
312 puts(realpath.buf);
d553e737
DR
313 argc--;
314 argv++;
315 }
3d7747e3 316 strbuf_release(&realpath);
2cd85c40 317 return 0;
d553e737
DR
318 }
319
87a246e1
MH
320 if (argc >= 2 && !strcmp(argv[1], "absolute_path")) {
321 while (argc > 2) {
322 puts(absolute_path(argv[2]));
323 argc--;
324 argv++;
325 }
326 return 0;
327 }
328
0454dd93 329 if (argc == 4 && !strcmp(argv[1], "longest_ancestor_length")) {
31171d9e
MH
330 int len;
331 struct string_list ceiling_dirs = STRING_LIST_INIT_DUP;
9e2326c7 332 char *path = xstrdup(argv[2]);
31171d9e 333
9e2326c7
MH
334 /*
335 * We have to normalize the arguments because under
336 * Windows, bash mangles arguments that look like
337 * absolute POSIX paths or colon-separate lists of
338 * absolute POSIX paths into DOS paths (e.g.,
339 * "/foo:/foo/bar" might be converted to
340 * "D:\Src\msysgit\foo;D:\Src\msysgit\foo\bar"),
341 * whereas longest_ancestor_length() requires paths
342 * that use forward slashes.
343 */
344 if (normalize_path_copy(path, path))
345 die("Path \"%s\" could not be normalized", argv[2]);
31171d9e 346 string_list_split(&ceiling_dirs, argv[3], PATH_SEP, -1);
9e2326c7
MH
347 filter_string_list(&ceiling_dirs, 0,
348 normalize_ceiling_entry, NULL);
349 len = longest_ancestor_length(path, &ceiling_dirs);
31171d9e 350 string_list_clear(&ceiling_dirs, 0);
9e2326c7 351 free(path);
0454dd93 352 printf("%d\n", len);
2cd85c40 353 return 0;
0454dd93
DR
354 }
355
9e813723 356 if (argc >= 4 && !strcmp(argv[1], "prefix_path")) {
3f2e2297 357 const char *prefix = argv[2];
9e813723
MH
358 int prefix_len = strlen(prefix);
359 int nongit_ok;
360 setup_git_directory_gently(&nongit_ok);
361 while (argc > 3) {
e287a5b0
ÆAB
362 char *pfx = prefix_path(prefix, prefix_len, argv[3]);
363
364 puts(pfx);
365 free(pfx);
9e813723
MH
366 argc--;
367 argv++;
368 }
369 return 0;
370 }
371
4fcc86b0
JS
372 if (argc == 4 && !strcmp(argv[1], "strip_path_suffix")) {
373 char *prefix = strip_path_suffix(argv[2], argv[3]);
374 printf("%s\n", prefix ? prefix : "(null)");
e287a5b0 375 free(prefix);
4fcc86b0
JS
376 return 0;
377 }
378
7ffd18fc 379 if (argc == 3 && !strcmp(argv[1], "print_path")) {
abd4284b
JX
380 puts(argv[2]);
381 return 0;
382 }
383
203439b2 384 if (argc == 4 && !strcmp(argv[1], "relative_path")) {
e02ca72f 385 struct strbuf sb = STRBUF_INIT;
203439b2
JX
386 const char *in, *prefix, *rel;
387 normalize_argv_string(&in, argv[2]);
388 normalize_argv_string(&prefix, argv[3]);
e02ca72f 389 rel = relative_path(in, prefix, &sb);
203439b2
JX
390 if (!rel)
391 puts("(null)");
392 else
393 puts(strlen(rel) > 0 ? rel : "(empty)");
e02ca72f 394 strbuf_release(&sb);
203439b2
JX
395 return 0;
396 }
397
7d1aaa68 398 if (argc == 2 && !strcmp(argv[1], "basename"))
29c2eda8 399 return test_function(basename_data, posix_basename, argv[1]);
7d1aaa68
JS
400
401 if (argc == 2 && !strcmp(argv[1], "dirname"))
29c2eda8 402 return test_function(dirname_data, posix_dirname, argv[1]);
7d1aaa68 403
dc2d9ba3 404 if (argc > 2 && !strcmp(argv[1], "is_dotgitmodules")) {
801ed010
JK
405 return check_dotfile("modules", argv + 2,
406 is_hfs_dotgitmodules,
407 is_ntfs_dotgitmodules);
408 }
409 if (argc > 2 && !strcmp(argv[1], "is_dotgitignore")) {
410 return check_dotfile("ignore", argv + 2,
411 is_hfs_dotgitignore,
412 is_ntfs_dotgitignore);
413 }
414 if (argc > 2 && !strcmp(argv[1], "is_dotgitattributes")) {
415 return check_dotfile("attributes", argv + 2,
416 is_hfs_dotgitattributes,
417 is_ntfs_dotgitattributes);
418 }
419 if (argc > 2 && !strcmp(argv[1], "is_dotmailmap")) {
420 return check_dotfile("mailmap", argv + 2,
421 is_hfs_dotmailmap,
422 is_ntfs_dotmailmap);
dc2d9ba3
JS
423 }
424
5868bd86
JS
425 if (argc > 2 && !strcmp(argv[1], "file-size")) {
426 int res = 0, i;
427 struct stat st;
428
429 for (i = 2; i < argc; i++)
430 if (stat(argv[i], &st))
431 res = error_errno("Cannot stat '%s'", argv[i]);
432 else
433 printf("%"PRIuMAX"\n", (uintmax_t)st.st_size);
434 return !!res;
435 }
436
af9912ef
JS
437 if (argc == 4 && !strcmp(argv[1], "skip-n-bytes")) {
438 int fd = open(argv[2], O_RDONLY), offset = atoi(argv[3]);
439 char buffer[65536];
440
441 if (fd < 0)
442 die_errno("could not open '%s'", argv[2]);
443 if (lseek(fd, offset, SEEK_SET) < 0)
444 die_errno("could not skip %d bytes", offset);
445 for (;;) {
446 ssize_t count = read(fd, buffer, sizeof(buffer));
447 if (count < 0)
448 die_errno("could not read '%s'", argv[2]);
449 if (!count)
450 break;
451 if (write(1, buffer, count) < 0)
452 die_errno("could not write to stdout");
453 }
454 close(fd);
455 return 0;
456 }
457
b819f1d2
JS
458 if (argc > 5 && !strcmp(argv[1], "slice-tests")) {
459 int res = 0;
460 long offset, stride, i;
461 struct string_list list = STRING_LIST_INIT_NODUP;
462 struct stat st;
463
464 offset = strtol(argv[2], NULL, 10);
465 stride = strtol(argv[3], NULL, 10);
466 if (stride < 1)
467 stride = 1;
468 for (i = 4; i < argc; i++)
469 if (stat(argv[i], &st))
470 res = error_errno("Cannot stat '%s'", argv[i]);
471 else
472 string_list_append(&list, argv[i])->util =
473 (void *)(intptr_t)st.st_size;
474 QSORT(list.items, list.nr, cmp_by_st_size);
475 for (i = offset; i < list.nr; i+= stride)
476 printf("%s\n", list.items[i].string);
477
478 return !!res;
479 }
480
a62f9d1a
GS
481 if (argc > 1 && !strcmp(argv[1], "protect_ntfs_hfs"))
482 return !!protect_ntfs_hfs_benchmark(argc - 1, argv + 1);
483
d2c84dad
JS
484 if (argc > 1 && !strcmp(argv[1], "is_valid_path")) {
485 int res = 0, expect = 1, i;
486
487 for (i = 2; i < argc; i++)
488 if (!strcmp("--not", argv[i]))
489 expect = 0;
490 else if (expect != is_valid_path(argv[i]))
491 res = error("'%s' is%s a valid path",
492 argv[i], expect ? " not" : "");
493 else
494 fprintf(stderr,
495 "'%s' is%s a valid path\n",
496 argv[i], expect ? "" : " not");
497
498 return !!res;
499 }
500
2cd85c40
JS
501 fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
502 argv[1] ? argv[1] : "(there was none)");
503 return 1;
ae299be0 504}