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