setup.h: move declarations for setup.c functions from cache.h
[git.git] / t / helper / test-path-utils.c
blob4f5ac2fadce3d940241c7f8b3fc529bc3318ec71
1 #include "test-tool.h"
2 #include "cache.h"
3 #include "abspath.h"
4 #include "environment.h"
5 #include "setup.h"
6 #include "string-list.h"
7 #include "utf8.h"
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.
14 static int normalize_ceiling_entry(struct string_list_item *item,
15 void *data UNUSED)
17 char *ceil = item->string;
19 if (!*ceil)
20 die("Empty path is not supported");
21 if (!is_absolute_path(ceil))
22 die("Path \"%s\" is not absolute", ceil);
23 if (normalize_path_copy(ceil, ceil) < 0)
24 die("Path \"%s\" could not be normalized", ceil);
25 return 1;
28 static void normalize_argv_string(const char **var, const char *input)
30 if (!strcmp(input, "<null>"))
31 *var = NULL;
32 else if (!strcmp(input, "<empty>"))
33 *var = "";
34 else
35 *var = input;
37 if (*var && (**var == '<' || **var == '('))
38 die("Bad value: %s\n", input);
41 struct test_data {
42 const char *from; /* input: transform from this ... */
43 const char *to; /* output: ... to this. */
44 const char *alternative; /* output: ... or this. */
48 * Compatibility wrappers for OpenBSD, whose basename(3) and dirname(3)
49 * have const parameters.
51 static char *posix_basename(char *path)
53 return basename(path);
56 static char *posix_dirname(char *path)
58 return dirname(path);
61 static int test_function(struct test_data *data, char *(*func)(char *input),
62 const char *funcname)
64 int failed = 0, i;
65 char buffer[1024];
66 char *to;
68 for (i = 0; data[i].to; i++) {
69 if (!data[i].from)
70 to = func(NULL);
71 else {
72 xsnprintf(buffer, sizeof(buffer), "%s", data[i].from);
73 to = func(buffer);
75 if (!strcmp(to, data[i].to))
76 continue;
77 if (!data[i].alternative)
78 error("FAIL: %s(%s) => '%s' != '%s'\n",
79 funcname, data[i].from, to, data[i].to);
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;
88 return failed;
91 static struct test_data basename_data[] = {
92 /* --- POSIX type paths --- */
93 { NULL, "." },
94 { "", "." },
95 { ".", "." },
96 { "..", ".." },
97 { "/", "/" },
98 { "//", "/", "//" },
99 { "///", "/", "//" },
100 { "////", "/", "//" },
101 { "usr", "usr" },
102 { "/usr", "usr" },
103 { "/usr/", "usr" },
104 { "/usr//", "usr" },
105 { "/usr/lib", "lib" },
106 { "usr/lib", "lib" },
107 { "usr/lib///", "lib" },
109 #if defined(__MINGW32__) || defined(_MSC_VER)
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:///", "/" },
128 { "\\", "\\", "/" },
129 { "\\\\", "\\", "/" },
130 { "\\\\\\", "\\", "/" },
131 #endif
132 { NULL, NULL }
135 static struct test_data dirname_data[] = {
136 /* --- POSIX type paths --- */
137 { NULL, "." },
138 { "", "." },
139 { ".", "." },
140 { "..", "." },
141 { "/", "/" },
142 { "//", "/", "//" },
143 { "///", "/", "//" },
144 { "////", "/", "//" },
145 { "usr", "." },
146 { "/usr", "/" },
147 { "/usr/", "/" },
148 { "/usr//", "/" },
149 { "/usr/lib", "/usr" },
150 { "usr/lib", "usr" },
151 { "usr/lib///", "usr" },
153 #if defined(__MINGW32__) || defined(_MSC_VER)
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 { "\\\\\\\\", "\\" },
174 { "C:", "C:.", "." },
175 #endif
176 { NULL, NULL }
179 static int check_dotfile(const char *x, const char **argv,
180 int (*is_hfs)(const char *),
181 int (*is_ntfs)(const char *))
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);
194 return !!res;
197 static int cmp_by_st_size(const void *a, const void *b)
199 intptr_t x = (intptr_t)((struct string_list_item *)a)->util;
200 intptr_t y = (intptr_t)((struct string_list_item *)b)->util;
202 return x > y ? -1 : (x < y ? +1 : 0);
206 * A very simple, reproducible pseudo-random generator. Copied from
207 * `test-genrandom.c`.
209 static uint64_t my_random_value = 1234;
211 static uint64_t my_random(void)
213 my_random_value = my_random_value * 1103515245 + 12345;
214 return my_random_value;
218 * A fast approximation of the square root, without requiring math.h.
220 * It uses Newton's method to approximate the solution of 0 = x^2 - value.
222 static double my_sqrt(double value)
224 const double epsilon = 1e-6;
225 double x = value;
227 if (value == 0)
228 return 0;
230 for (;;) {
231 double delta = (value / x - x) / 2;
232 if (delta < epsilon && delta > -epsilon)
233 return x + delta;
234 x += delta;
238 static int protect_ntfs_hfs_benchmark(int argc, const char **argv)
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;
248 if (argc > 1 && !strcmp(argv[1], "--with-symlink-mode")) {
249 file_mode = 0120000;
250 argc--;
251 argv++;
254 nr = argc > 1 ? strtoul(argv[1], NULL, 0) : 1000000;
255 ALLOC_ARRAY(names, nr);
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");
265 for (i = 0; i < nr; i++) {
266 size_t len = min_len + (my_random() % (max_len + 1 - min_len));
268 names[i] = xmallocz(len);
269 while (len > 0)
270 names[i][--len] = (char)(' ' + (my_random() % ('\x7f' - ' ')));
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);
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);
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]);
295 return 0;
298 int cmd__path_utils(int argc, const char **argv)
300 if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) {
301 char *buf = xmallocz(strlen(argv[2]));
302 int rv = normalize_path_copy(buf, argv[2]);
303 puts(rv ? "++failed++" : buf);
304 free(buf);
305 return 0;
308 if (argc >= 2 && !strcmp(argv[1], "real_path")) {
309 struct strbuf realpath = STRBUF_INIT;
310 while (argc > 2) {
311 strbuf_realpath(&realpath, argv[2], 1);
312 puts(realpath.buf);
313 argc--;
314 argv++;
316 strbuf_release(&realpath);
317 return 0;
320 if (argc >= 2 && !strcmp(argv[1], "absolute_path")) {
321 while (argc > 2) {
322 puts(absolute_path(argv[2]));
323 argc--;
324 argv++;
326 return 0;
329 if (argc == 4 && !strcmp(argv[1], "longest_ancestor_length")) {
330 int len;
331 struct string_list ceiling_dirs = STRING_LIST_INIT_DUP;
332 char *path = xstrdup(argv[2]);
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.
344 if (normalize_path_copy(path, path))
345 die("Path \"%s\" could not be normalized", argv[2]);
346 string_list_split(&ceiling_dirs, argv[3], PATH_SEP, -1);
347 filter_string_list(&ceiling_dirs, 0,
348 normalize_ceiling_entry, NULL);
349 len = longest_ancestor_length(path, &ceiling_dirs);
350 string_list_clear(&ceiling_dirs, 0);
351 free(path);
352 printf("%d\n", len);
353 return 0;
356 if (argc >= 4 && !strcmp(argv[1], "prefix_path")) {
357 const char *prefix = argv[2];
358 int prefix_len = strlen(prefix);
359 int nongit_ok;
360 setup_git_directory_gently(&nongit_ok);
361 while (argc > 3) {
362 char *pfx = prefix_path(prefix, prefix_len, argv[3]);
364 puts(pfx);
365 free(pfx);
366 argc--;
367 argv++;
369 return 0;
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)");
375 free(prefix);
376 return 0;
379 if (argc == 3 && !strcmp(argv[1], "print_path")) {
380 puts(argv[2]);
381 return 0;
384 if (argc == 4 && !strcmp(argv[1], "relative_path")) {
385 struct strbuf sb = STRBUF_INIT;
386 const char *in, *prefix, *rel;
387 normalize_argv_string(&in, argv[2]);
388 normalize_argv_string(&prefix, argv[3]);
389 rel = relative_path(in, prefix, &sb);
390 if (!rel)
391 puts("(null)");
392 else
393 puts(strlen(rel) > 0 ? rel : "(empty)");
394 strbuf_release(&sb);
395 return 0;
398 if (argc == 2 && !strcmp(argv[1], "basename"))
399 return test_function(basename_data, posix_basename, argv[1]);
401 if (argc == 2 && !strcmp(argv[1], "dirname"))
402 return test_function(dirname_data, posix_dirname, argv[1]);
404 if (argc > 2 && !strcmp(argv[1], "is_dotgitmodules")) {
405 return check_dotfile("modules", argv + 2,
406 is_hfs_dotgitmodules,
407 is_ntfs_dotgitmodules);
409 if (argc > 2 && !strcmp(argv[1], "is_dotgitignore")) {
410 return check_dotfile("ignore", argv + 2,
411 is_hfs_dotgitignore,
412 is_ntfs_dotgitignore);
414 if (argc > 2 && !strcmp(argv[1], "is_dotgitattributes")) {
415 return check_dotfile("attributes", argv + 2,
416 is_hfs_dotgitattributes,
417 is_ntfs_dotgitattributes);
419 if (argc > 2 && !strcmp(argv[1], "is_dotmailmap")) {
420 return check_dotfile("mailmap", argv + 2,
421 is_hfs_dotmailmap,
422 is_ntfs_dotmailmap);
425 if (argc > 2 && !strcmp(argv[1], "file-size")) {
426 int res = 0, i;
427 struct stat st;
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;
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];
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");
454 close(fd);
455 return 0;
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;
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);
478 return !!res;
481 if (argc > 1 && !strcmp(argv[1], "protect_ntfs_hfs"))
482 return !!protect_ntfs_hfs_benchmark(argc - 1, argv + 1);
484 if (argc > 1 && !strcmp(argv[1], "is_valid_path")) {
485 int res = 0, expect = 1, i;
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");
498 return !!res;
501 fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
502 argv[1] ? argv[1] : "(there was none)");
503 return 1;