Raw
1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
3
4 #include "test-tool.h"
5 #include "abspath.h"
6 #include "environment.h"
7 #include "path.h"
8 #include "read-cache-ll.h"
9 #include "setup.h"
10 #include "string-list.h"
11 #include "trace.h"
12 #include "utf8.h"
13 #include "copy.h"
14
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 */
20 static int normalize_ceiling_entry(struct string_list_item *item,
21 void *data UNUSED)
22 {
23 char *ceil = item->string;
24
25 if (!*ceil)
26 die("Empty path is not supported");
27 if (!is_absolute_path(ceil))
28 die("Path \"%s\" is not absolute", ceil);
29 if (normalize_path_copy(ceil, ceil) < 0)
30 die("Path \"%s\" could not be normalized", ceil);
31 return 1;
32 }
33
34 static 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 == '('))
44 die("Bad value: %s", input);
45 }
46
47 struct test_data {
48 const char *from; /* input: transform from this ... */
49 const char *to; /* output: ... to this. */
50 const char *alternative; /* output: ... or this. */
51 };
52
53 /*
54 * Compatibility wrappers for OpenBSD, whose basename(3) and dirname(3)
55 * have const parameters.
56 */
57 static char *posix_basename(char *path)
58 {
59 return basename(path);
60 }
61
62 static char *posix_dirname(char *path)
63 {
64 return dirname(path);
65 }
66
67 static 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 {
78 xsnprintf(buffer, sizeof(buffer), "%s", data[i].from);
79 to = func(buffer);
80 }
81 if (!strcmp(to, data[i].to))
82 continue;
83 if (!data[i].alternative)
84 error("FAIL: %s(%s) => '%s' != '%s'",
85 funcname, data[i].from, to, data[i].to);
86 else if (!strcmp(to, data[i].alternative))
87 continue;
88 else
89 error("FAIL: %s(%s) => '%s' != '%s', '%s'",
90 funcname, data[i].from, to, data[i].to,
91 data[i].alternative);
92 failed = 1;
93 }
94 return failed;
95 }
96
97 static struct test_data basename_data[] = {
98 /* --- POSIX type paths --- */
99 { NULL, "." },
100 { "", "." },
101 { ".", "." },
102 { "..", ".." },
103 { "/", "/" },
104 { "//", "/", "//" },
105 { "///", "/", "//" },
106 { "////", "/", "//" },
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)
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:///", "/" },
134 { "\\", "\\", "/" },
135 { "\\\\", "\\", "/" },
136 { "\\\\\\", "\\", "/" },
137 #endif
138 { NULL, NULL }
139 };
140
141 static struct test_data dirname_data[] = {
142 /* --- POSIX type paths --- */
143 { NULL, "." },
144 { "", "." },
145 { ".", "." },
146 { "..", "." },
147 { "/", "/" },
148 { "//", "/", "//" },
149 { "///", "/", "//" },
150 { "////", "/", "//" },
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)
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 { "\\\\\\\\", "\\" },
180 { "C:", "C:.", "." },
181 #endif
182 { NULL, NULL }
183 };
184
185 static int check_dotfile(const char *x, const char **argv,
186 int (*is_hfs)(const char *),
187 int (*is_ntfs)(const char *))
188 {
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;
201 }
202
203 static 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
211 /*
212 * A very simple, reproducible pseudo-random generator. Copied from
213 * `test-genrandom.c`.
214 */
215 static uint64_t my_random_value = 1234;
216
217 static 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 */
228 static 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
244 static 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 int ntfs, hfs;
254
255 if (argc > 1 && !strcmp(argv[1], "--with-symlink-mode")) {
256 file_mode = 0120000;
257 argc--;
258 argv++;
259 }
260
261 nr = argc > 1 ? strtoul(argv[1], NULL, 0) : 1000000;
262 ALLOC_ARRAY(names, nr);
263
264 if (argc > 2) {
265 min_len = strtoul(argv[2], NULL, 0);
266 if (argc > 3)
267 max_len = strtoul(argv[3], NULL, 0);
268 if (min_len > max_len)
269 die("min_len > max_len");
270 }
271
272 for (i = 0; i < nr; i++) {
273 size_t len = min_len + (my_random() % (max_len + 1 - min_len));
274
275 names[i] = xmallocz(len);
276 while (len > 0)
277 names[i][--len] = (char)(' ' + (my_random() % ('\x7f' - ' ')));
278 }
279
280 if (!the_repository->gitdir)
281 the_repository->gitdir = xstrdup(".git");
282
283 for (ntfs = 0; ntfs < 2; ntfs++)
284 for (hfs = 0; hfs < 2; hfs++) {
285 repo_config_values(the_repository)->protect_ntfs = ntfs;
286 repo_config_values(the_repository)->protect_hfs = hfs;
287 cumul = 0;
288 cumul2 = 0;
289 for (i = 0; i < repetitions; i++) {
290 begin = getnanotime();
291 for (j = 0; j < nr; j++)
292 verify_path(names[j], file_mode);
293 end = getnanotime();
294 printf("protect_ntfs = %d, protect_hfs = %d: %lfms\n", ntfs, hfs, (end-begin) / (double)1e6);
295 cumul += end - begin;
296 cumul2 += (end - begin) * (end - begin);
297 }
298 m[ntfs][hfs] = cumul / (double)repetitions;
299 v[ntfs][hfs] = my_sqrt(cumul2 / (double)repetitions - m[ntfs][hfs] * m[ntfs][hfs]);
300 printf("mean: %lfms, stddev: %lfms\n", m[ntfs][hfs] / (double)1e6, v[ntfs][hfs] / (double)1e6);
301 }
302
303 for (ntfs = 0; ntfs < 2; ntfs++)
304 for (hfs = 0; hfs < 2; hfs++)
305 printf("ntfs=%d/hfs=%d: %lf%% slower\n", ntfs, hfs, (m[ntfs][hfs] - m[0][0]) * 100 / m[0][0]);
306
307 return 0;
308 }
309
310 int cmd__path_utils(int argc, const char **argv)
311 {
312 if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) {
313 char *buf = xmallocz(strlen(argv[2]));
314 int rv = normalize_path_copy(buf, argv[2]);
315 puts(rv ? "++failed++" : buf);
316 free(buf);
317 return 0;
318 }
319
320 if (argc >= 2 && !strcmp(argv[1], "real_path")) {
321 struct strbuf realpath = STRBUF_INIT;
322 while (argc > 2) {
323 strbuf_realpath(&realpath, argv[2], 1);
324 puts(realpath.buf);
325 argc--;
326 argv++;
327 }
328 strbuf_release(&realpath);
329 return 0;
330 }
331
332 if (argc >= 2 && !strcmp(argv[1], "readlink")) {
333 struct strbuf target = STRBUF_INIT;
334 while (argc > 2) {
335 if (strbuf_readlink(&target, argv[2], 0) < 0)
336 die_errno("cannot read link at '%s'", argv[2]);
337 puts(target.buf);
338 argc--;
339 argv++;
340 }
341 strbuf_release(&target);
342 return 0;
343 }
344
345 if (argc >= 2 && !strcmp(argv[1], "absolute_path")) {
346 while (argc > 2) {
347 puts(absolute_path(argv[2]));
348 argc--;
349 argv++;
350 }
351 return 0;
352 }
353
354 if (argc == 4 && !strcmp(argv[1], "longest_ancestor_length")) {
355 int len;
356 struct string_list ceiling_dirs = STRING_LIST_INIT_DUP;
357 const char path_sep[] = { PATH_SEP, '\0' };
358 char *path = xstrdup(argv[2]);
359
360 /*
361 * We have to normalize the arguments because under
362 * Windows, bash mangles arguments that look like
363 * absolute POSIX paths or colon-separate lists of
364 * absolute POSIX paths into DOS paths (e.g.,
365 * "/foo:/foo/bar" might be converted to
366 * "D:\Src\msysgit\foo;D:\Src\msysgit\foo\bar"),
367 * whereas longest_ancestor_length() requires paths
368 * that use forward slashes.
369 */
370 if (normalize_path_copy(path, path))
371 die("Path \"%s\" could not be normalized", argv[2]);
372 string_list_split(&ceiling_dirs, argv[3], path_sep, -1);
373 filter_string_list(&ceiling_dirs, 0,
374 normalize_ceiling_entry, NULL);
375 len = longest_ancestor_length(path, &ceiling_dirs);
376 string_list_clear(&ceiling_dirs, 0);
377 free(path);
378 printf("%d\n", len);
379 return 0;
380 }
381
382 if (argc >= 4 && !strcmp(argv[1], "prefix_path")) {
383 const char *prefix = argv[2];
384 int prefix_len = strlen(prefix);
385 int nongit_ok;
386 setup_git_directory_gently(the_repository, &nongit_ok);
387 while (argc > 3) {
388 char *pfx = prefix_path(the_repository, prefix, prefix_len, argv[3]);
389
390 puts(pfx);
391 free(pfx);
392 argc--;
393 argv++;
394 }
395 return 0;
396 }
397
398 if (argc == 4 && !strcmp(argv[1], "strip_path_suffix")) {
399 char *prefix = strip_path_suffix(argv[2], argv[3]);
400 printf("%s\n", prefix ? prefix : "(null)");
401 free(prefix);
402 return 0;
403 }
404
405 if (argc == 3 && !strcmp(argv[1], "print_path")) {
406 puts(argv[2]);
407 return 0;
408 }
409
410 if (argc == 4 && !strcmp(argv[1], "relative_path")) {
411 struct strbuf sb = STRBUF_INIT;
412 const char *in, *prefix, *rel;
413 normalize_argv_string(&in, argv[2]);
414 normalize_argv_string(&prefix, argv[3]);
415 rel = relative_path(in, prefix, &sb);
416 if (!rel)
417 puts("(null)");
418 else
419 puts(strlen(rel) > 0 ? rel : "(empty)");
420 strbuf_release(&sb);
421 return 0;
422 }
423
424 if (argc == 2 && !strcmp(argv[1], "basename"))
425 return test_function(basename_data, posix_basename, argv[1]);
426
427 if (argc == 2 && !strcmp(argv[1], "dirname"))
428 return test_function(dirname_data, posix_dirname, argv[1]);
429
430 if (argc > 2 && !strcmp(argv[1], "is_dotgitmodules")) {
431 return check_dotfile("modules", argv + 2,
432 is_hfs_dotgitmodules,
433 is_ntfs_dotgitmodules);
434 }
435 if (argc > 2 && !strcmp(argv[1], "is_dotgitignore")) {
436 return check_dotfile("ignore", argv + 2,
437 is_hfs_dotgitignore,
438 is_ntfs_dotgitignore);
439 }
440 if (argc > 2 && !strcmp(argv[1], "is_dotgitattributes")) {
441 return check_dotfile("attributes", argv + 2,
442 is_hfs_dotgitattributes,
443 is_ntfs_dotgitattributes);
444 }
445 if (argc > 2 && !strcmp(argv[1], "is_dotmailmap")) {
446 return check_dotfile("mailmap", argv + 2,
447 is_hfs_dotmailmap,
448 is_ntfs_dotmailmap);
449 }
450
451 if (argc > 2 && !strcmp(argv[1], "file-size")) {
452 int res = 0, i;
453 struct stat st;
454
455 for (i = 2; i < argc; i++)
456 if (stat(argv[i], &st))
457 res = error_errno("Cannot stat '%s'", argv[i]);
458 else
459 printf("%"PRIuMAX"\n", (uintmax_t)st.st_size);
460 return !!res;
461 }
462
463 if (argc == 4 && !strcmp(argv[1], "skip-n-bytes")) {
464 int fd = open(argv[2], O_RDONLY), offset = atoi(argv[3]);
465 char buffer[65536];
466
467 if (fd < 0)
468 die_errno("could not open '%s'", argv[2]);
469 if (lseek(fd, offset, SEEK_SET) < 0)
470 die_errno("could not skip %d bytes", offset);
471 for (;;) {
472 ssize_t count = read(fd, buffer, sizeof(buffer));
473 if (count < 0)
474 die_errno("could not read '%s'", argv[2]);
475 if (!count)
476 break;
477 if (write(1, buffer, count) < 0)
478 die_errno("could not write to stdout");
479 }
480 close(fd);
481 return 0;
482 }
483
484 if (argc > 5 && !strcmp(argv[1], "slice-tests")) {
485 int res = 0;
486 long slice, slices_total, i;
487 struct string_list list = STRING_LIST_INIT_NODUP;
488 struct stat st;
489
490 slices_total = strtol(argv[3], NULL, 10);
491 if (slices_total < 1)
492 die("there must be at least one slice, got '%s'",
493 argv[3]);
494
495 slice = strtol(argv[2], NULL, 10);
496 if (1 > slice || slice > slices_total)
497 die("slice must be in the range 1 <= slice <= %ld, got '%s'",
498 slices_total, argv[2]);
499
500 for (i = 4; i < argc; i++)
501 if (stat(argv[i], &st))
502 res = error_errno("Cannot stat '%s'", argv[i]);
503 else
504 string_list_append(&list, argv[i])->util =
505 (void *)(intptr_t)st.st_size;
506 QSORT(list.items, list.nr, cmp_by_st_size);
507 for (i = slice - 1; i < list.nr; i+= slices_total)
508 printf("%s\n", list.items[i].string);
509
510 return !!res;
511 }
512
513 if (argc > 1 && !strcmp(argv[1], "protect_ntfs_hfs"))
514 return !!protect_ntfs_hfs_benchmark(argc - 1, argv + 1);
515
516 if (argc > 1 && !strcmp(argv[1], "is_valid_path")) {
517 int res = 0, expect = 1, i;
518
519 for (i = 2; i < argc; i++)
520 if (!strcmp("--not", argv[i]))
521 expect = 0;
522 else if (expect != is_valid_path(argv[i]))
523 res = error("'%s' is%s a valid path",
524 argv[i], expect ? " not" : "");
525 else
526 fprintf(stderr,
527 "'%s' is%s a valid path\n",
528 argv[i], expect ? "" : " not");
529
530 return !!res;
531 }
532
533 if (argc > 1 && !strcmp(argv[1], "is_path_owned_by_current_user")) {
534 int res = 0;
535
536 for (int i = 2; i < argc; i++) {
537 struct strbuf buf = STRBUF_INIT;
538
539 if (is_path_owned_by_current_user(argv[i], &buf))
540 printf("'%s' is owned by current SID\n", argv[i]);
541 else {
542 printf("'%s' is not owned by current SID: %s\n", argv[i], buf.buf);
543 res = 1;
544 }
545
546 strbuf_release(&buf);
547 }
548
549 return res;
550 }
551
552 fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
553 argv[1] ? argv[1] : "(there was none)");
554 return 1;
555 }