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