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