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