t00-core.c 23.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2,
 * as published by the Free Software Foundation.
 *
 * In addition to the permissions in the GNU General Public License,
 * the authors give you unlimited permission to link the compiled
 * version of this file into combinations with other programs,
 * and to distribute those combinations without any restriction
 * coming from the use of this file.  (The General Public License
 * restrictions do apply in other respects; for example, they cover
 * modification of the file, and distribution when not linked into
 * a combined executable.)
 *
 * This file is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
#include "test_lib.h"

#include "vector.h"
#include "fileops.h"

30
BEGIN_TEST(string0, "compare prefixes")
31 32 33 34 35 36 37 38 39 40
	must_be_true(git__prefixcmp("", "") == 0);
	must_be_true(git__prefixcmp("a", "") == 0);
	must_be_true(git__prefixcmp("", "a") < 0);
	must_be_true(git__prefixcmp("a", "b") < 0);
	must_be_true(git__prefixcmp("b", "a") > 0);
	must_be_true(git__prefixcmp("ab", "a") == 0);
	must_be_true(git__prefixcmp("ab", "ac") < 0);
	must_be_true(git__prefixcmp("ab", "aa") > 0);
END_TEST

41
BEGIN_TEST(string1, "compare suffixes")
42 43 44 45 46 47 48 49 50 51
	must_be_true(git__suffixcmp("", "") == 0);
	must_be_true(git__suffixcmp("a", "") == 0);
	must_be_true(git__suffixcmp("", "a") < 0);
	must_be_true(git__suffixcmp("a", "b") < 0);
	must_be_true(git__suffixcmp("b", "a") > 0);
	must_be_true(git__suffixcmp("ba", "a") == 0);
	must_be_true(git__suffixcmp("zaa", "ac") < 0);
	must_be_true(git__suffixcmp("zaz", "ac") > 0);
END_TEST

52 53 54 55

BEGIN_TEST(vector0, "initial size of 1 would cause writing past array bounds")
  git_vector x;
  int i;
56
  git_vector_init(&x, 1, NULL);
57 58 59 60 61 62 63 64 65
  for (i = 0; i < 10; ++i) {
    git_vector_insert(&x, (void*) 0xabc);
  }
  git_vector_free(&x);
END_TEST

BEGIN_TEST(vector1, "don't read past array bounds on remove()")
  git_vector x;
  // make initial capacity exact for our insertions.
66
  git_vector_init(&x, 3, NULL);
67 68 69 70 71 72 73 74 75 76
  git_vector_insert(&x, (void*) 0xabc);
  git_vector_insert(&x, (void*) 0xdef);
  git_vector_insert(&x, (void*) 0x123);

  git_vector_remove(&x, 0);  // used to read past array bounds.
  git_vector_free(&x);
END_TEST


BEGIN_TEST(path0, "get the dirname of a path")
77 78 79 80 81 82 83 84 85
	char dir[64], *dir2;

#define DIRNAME_TEST(A, B) { \
	must_be_true(git__dirname_r(dir, sizeof(dir), A) >= 0); \
	must_be_true(strcmp(dir, B) == 0);				\
	must_be_true((dir2 = git__dirname(A)) != NULL);	\
	must_be_true(strcmp(dir2, B) == 0);				\
	free(dir2);										\
}
86

87 88 89 90 91 92 93
	DIRNAME_TEST(NULL, ".");
	DIRNAME_TEST("", ".");
	DIRNAME_TEST("a", ".");
	DIRNAME_TEST("/", "/");
	DIRNAME_TEST("/usr", "/");
	DIRNAME_TEST("/usr/", "/");
	DIRNAME_TEST("/usr/lib", "/usr");
94 95
	DIRNAME_TEST("/usr/lib/", "/usr");
	DIRNAME_TEST("/usr/lib//", "/usr");
96
	DIRNAME_TEST("usr/lib", "usr");
97 98
	DIRNAME_TEST("usr/lib/", "usr");
	DIRNAME_TEST("usr/lib//", "usr");
99
	DIRNAME_TEST(".git/", ".");
100

101
#undef DIRNAME_TEST
102 103 104

END_TEST

105
BEGIN_TEST(path1, "get the base name of a path")
106 107 108 109 110 111 112 113 114
	char base[64], *base2;

#define BASENAME_TEST(A, B) { \
	must_be_true(git__basename_r(base, sizeof(base), A) >= 0); \
	must_be_true(strcmp(base, B) == 0);					\
	must_be_true((base2 = git__basename(A)) != NULL);	\
	must_be_true(strcmp(base2, B) == 0);				\
	free(base2);										\
}
115

116 117 118 119 120 121 122
	BASENAME_TEST(NULL, ".");
	BASENAME_TEST("", ".");
	BASENAME_TEST("a", "a");
	BASENAME_TEST("/", "/");
	BASENAME_TEST("/usr", "usr");
	BASENAME_TEST("/usr/", "usr");
	BASENAME_TEST("/usr/lib", "lib");
123
	BASENAME_TEST("/usr/lib//", "lib");
124
	BASENAME_TEST("usr/lib", "lib");
125

126
#undef BASENAME_TEST
127

128
END_TEST
129

130
BEGIN_TEST(path2, "get the latest component in a path")
131
	const char *dir;
132

133 134 135 136
#define TOPDIR_TEST(A, B) { \
	must_be_true((dir = git__topdir(A)) != NULL);	\
	must_be_true(strcmp(dir, B) == 0);				\
}
137

138 139 140 141 142 143 144
	TOPDIR_TEST(".git/", ".git/");
	TOPDIR_TEST("/.git/", ".git/");
	TOPDIR_TEST("usr/local/.git/", ".git/");
	TOPDIR_TEST("./.git/", ".git/");
	TOPDIR_TEST("/usr/.git/", ".git/");
	TOPDIR_TEST("/", "/");
	TOPDIR_TEST("a/", "a/");
145

146 147 148 149
	must_be_true(git__topdir("/usr/.git") == NULL);
	must_be_true(git__topdir(".") == NULL);
	must_be_true(git__topdir("") == NULL);
	must_be_true(git__topdir("a") == NULL);
150

151
#undef TOPDIR_TEST
152 153
END_TEST

154
typedef int (normalize_path)(char *, size_t, const char *);
155

156 157 158 159 160 161
/* Assert flags */
#define CWD_AS_PREFIX 1
#define PATH_AS_SUFFIX 2
#define ROOTED_PATH 4

static int ensure_normalized(const char *input_path, const char *expected_path, normalize_path normalizer, int assert_flags)
162 163 164
{
	int error = GIT_SUCCESS;
	char buffer_out[GIT_PATH_MAX];
165 166 167 168 169
	char current_workdir[GIT_PATH_MAX];

	error = gitfo_getcwd(current_workdir, sizeof(current_workdir));
	if (error < GIT_SUCCESS)
		return error;
170

171
	error = normalizer(buffer_out, sizeof(buffer_out), input_path);
172 173 174 175 176 177
	if (error < GIT_SUCCESS)
		return error;

	if (expected_path == NULL)
		return error;

178 179 180 181 182 183 184 185 186 187 188
	if ((assert_flags & PATH_AS_SUFFIX) != 0)
		if (git__suffixcmp(buffer_out, expected_path))
			return GIT_ERROR;

	if ((assert_flags & CWD_AS_PREFIX) != 0)
		if (git__prefixcmp(buffer_out, current_workdir))
			return GIT_ERROR;

	if ((assert_flags & ROOTED_PATH) != 0) {
		error = strcmp(expected_path, buffer_out);
	}
189 190 191 192

	return error;
}

193
static int ensure_dir_path_normalized(const char *input_path, const char *expected_path, int assert_flags)
194
{
195
	return ensure_normalized(input_path, expected_path, gitfo_prettify_dir_path, assert_flags);
196 197
}

198
static int ensure_file_path_normalized(const char *input_path, const char *expected_path, int assert_flags)
199
{
200
	return ensure_normalized(input_path, expected_path, gitfo_prettify_file_path, assert_flags);
201 202
}

203
BEGIN_TEST(path3, "prettify and validate a path to a file")
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	must_pass(ensure_file_path_normalized("a", "a", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_file_path_normalized("./testrepo.git", "testrepo.git", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_file_path_normalized("./.git", ".git", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_file_path_normalized("./git.", "git.", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_fail(ensure_file_path_normalized("git./", NULL, 0));
	must_fail(ensure_file_path_normalized("", NULL, 0));
	must_fail(ensure_file_path_normalized(".", NULL, 0));
	must_fail(ensure_file_path_normalized("./", NULL, 0));
	must_fail(ensure_file_path_normalized("./.", NULL, 0));
	must_fail(ensure_file_path_normalized("./..", NULL, 0));
	must_fail(ensure_file_path_normalized("../.", NULL, 0));
	must_fail(ensure_file_path_normalized("./.././/", NULL, 0));
	must_fail(ensure_file_path_normalized("dir/..", NULL, 0));
	must_fail(ensure_file_path_normalized("dir/sub/../..", NULL, 0));
	must_fail(ensure_file_path_normalized("dir/sub/..///..", NULL, 0));
	must_fail(ensure_file_path_normalized("dir/sub///../..", NULL, 0));
	must_fail(ensure_file_path_normalized("dir/sub///..///..", NULL, 0));
	must_fail(ensure_file_path_normalized("dir/sub/../../..", NULL, 0));
	must_pass(ensure_file_path_normalized("dir", "dir", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_fail(ensure_file_path_normalized("dir//", NULL, 0));
	must_pass(ensure_file_path_normalized("./dir", "dir", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_fail(ensure_file_path_normalized("dir/.", NULL, 0));
	must_fail(ensure_file_path_normalized("dir///./", NULL, 0));
	must_fail(ensure_file_path_normalized("dir/sub/..", NULL, 0));
	must_fail(ensure_file_path_normalized("dir//sub/..",NULL, 0));
	must_fail(ensure_file_path_normalized("dir//sub/../", NULL, 0));
	must_fail(ensure_file_path_normalized("dir/sub/../", NULL, 0));
	must_fail(ensure_file_path_normalized("dir/sub/../.", NULL, 0));
	must_fail(ensure_file_path_normalized("dir/s1/../s2/", NULL, 0));
	must_fail(ensure_file_path_normalized("d1/s1///s2/..//../s3/", NULL, 0));
	must_pass(ensure_file_path_normalized("d1/s1//../s2/../../d2", "d2", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_fail(ensure_file_path_normalized("dir/sub/../", NULL, 0));
	must_pass(ensure_file_path_normalized("../../a/../../b/c/d/../../e", "b/e", PATH_AS_SUFFIX));
	must_fail(ensure_file_path_normalized("....", NULL, 0));
	must_fail(ensure_file_path_normalized("...", NULL, 0));
	must_fail(ensure_file_path_normalized("./...", NULL, 0));
	must_fail(ensure_file_path_normalized("d1/...", NULL, 0));
	must_fail(ensure_file_path_normalized("d1/.../", NULL, 0));
	must_fail(ensure_file_path_normalized("d1/.../d2", NULL, 0));
243
	
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
	must_pass(ensure_file_path_normalized("/a", "/a", ROOTED_PATH));
	must_pass(ensure_file_path_normalized("/./testrepo.git", "/testrepo.git", ROOTED_PATH));
	must_pass(ensure_file_path_normalized("/./.git", "/.git", ROOTED_PATH));
	must_pass(ensure_file_path_normalized("/./git.", "/git.", ROOTED_PATH));
	must_fail(ensure_file_path_normalized("/git./", NULL, 0));
	must_fail(ensure_file_path_normalized("/", NULL, 0));
	must_fail(ensure_file_path_normalized("/.", NULL, 0));
	must_fail(ensure_file_path_normalized("/./", NULL, 0));
	must_fail(ensure_file_path_normalized("/./.", NULL, 0));
	must_fail(ensure_file_path_normalized("/./..", NULL, 0));
	must_fail(ensure_file_path_normalized("/../.", NULL, 0));
	must_fail(ensure_file_path_normalized("/./.././/", NULL, 0));
	must_fail(ensure_file_path_normalized("/dir/..", NULL, 0));
	must_fail(ensure_file_path_normalized("/dir/sub/../..", NULL, 0));
	must_fail(ensure_file_path_normalized("/dir/sub/..///..", NULL, 0));
	must_fail(ensure_file_path_normalized("/dir/sub///../..", NULL, 0));
	must_fail(ensure_file_path_normalized("/dir/sub///..///..", NULL, 0));
	must_fail(ensure_file_path_normalized("/dir/sub/../../..", NULL, 0));
	must_pass(ensure_file_path_normalized("/dir", "/dir", 0));
	must_fail(ensure_file_path_normalized("/dir//", NULL, 0));
	must_pass(ensure_file_path_normalized("/./dir", "/dir", 0));
	must_fail(ensure_file_path_normalized("/dir/.", NULL, 0));
	must_fail(ensure_file_path_normalized("/dir///./", NULL, 0));
	must_fail(ensure_file_path_normalized("/dir/sub/..", NULL, 0));
	must_fail(ensure_file_path_normalized("/dir//sub/..",NULL, 0));
	must_fail(ensure_file_path_normalized("/dir//sub/../", NULL, 0));
	must_fail(ensure_file_path_normalized("/dir/sub/../", NULL, 0));
	must_fail(ensure_file_path_normalized("/dir/sub/../.", NULL, 0));
	must_fail(ensure_file_path_normalized("/dir/s1/../s2/", NULL, 0));
	must_fail(ensure_file_path_normalized("/d1/s1///s2/..//../s3/", NULL, 0));
	must_pass(ensure_file_path_normalized("/d1/s1//../s2/../../d2", "/d2", 0));
	must_fail(ensure_file_path_normalized("/dir/sub/../", NULL, 0));
	must_fail(ensure_file_path_normalized("/....", NULL, 0));
	must_fail(ensure_file_path_normalized("/...", NULL, 0));
	must_fail(ensure_file_path_normalized("/./...", NULL, 0));
	must_fail(ensure_file_path_normalized("/d1/...", NULL, 0));
	must_fail(ensure_file_path_normalized("/d1/.../", NULL, 0));
	must_fail(ensure_file_path_normalized("/d1/.../d2", NULL, 0));
282 283
END_TEST

284
BEGIN_TEST(path4, "validate and prettify a path to a folder")
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
	must_pass(ensure_dir_path_normalized("./testrepo.git", "testrepo.git/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("./.git", ".git/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("./git.", "git./", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("git./", "git./", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("", "", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized(".", "", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("./", "", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("./.", "", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("dir/..", "", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("dir/sub/../..", "", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("dir/sub/..///..", "", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("dir/sub///../..", "", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("dir/sub///..///..", "", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("dir", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("dir//", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("./dir", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("dir/.", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("dir///./", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("dir/sub/..", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("dir//sub/..", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("dir//sub/../", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("dir/sub/../", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("dir/sub/../.", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("dir/s1/../s2/", "dir/s2/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("d1/s1///s2/..//../s3/", "d1/s3/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("d1/s1//../s2/../../d2", "d2/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("dir/sub/../", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
	must_pass(ensure_dir_path_normalized("../../a/../../b/c/d/../../e", "b/e/", PATH_AS_SUFFIX));
	must_fail(ensure_dir_path_normalized("....", NULL, 0));
	must_fail(ensure_dir_path_normalized("...", NULL, 0));
	must_fail(ensure_dir_path_normalized("./...", NULL, 0));
	must_fail(ensure_dir_path_normalized("d1/...", NULL, 0));
	must_fail(ensure_dir_path_normalized("d1/.../", NULL, 0));
	must_fail(ensure_dir_path_normalized("d1/.../d2", NULL, 0));

	must_pass(ensure_dir_path_normalized("/./testrepo.git", "/testrepo.git/", ROOTED_PATH));
	must_pass(ensure_dir_path_normalized("/./.git", "/.git/", ROOTED_PATH));
	must_pass(ensure_dir_path_normalized("/./git.", "/git./", ROOTED_PATH));
	must_pass(ensure_dir_path_normalized("/git./", "/git./", ROOTED_PATH));
	must_pass(ensure_dir_path_normalized("/", "/", ROOTED_PATH));
	must_pass(ensure_dir_path_normalized("//", "/", ROOTED_PATH));
	must_pass(ensure_dir_path_normalized("///", "/", ROOTED_PATH));
	must_pass(ensure_dir_path_normalized("/.", "/", ROOTED_PATH));
	must_pass(ensure_dir_path_normalized("/./", "/", ROOTED_PATH));
	must_fail(ensure_dir_path_normalized("/./..", NULL, 0));
	must_fail(ensure_dir_path_normalized("/../.", NULL, 0));
	must_fail(ensure_dir_path_normalized("/./.././/", NULL, 0));
	must_pass(ensure_dir_path_normalized("/dir/..", "/", 0));
	must_pass(ensure_dir_path_normalized("/dir/sub/../..", "/", 0));
	must_fail(ensure_dir_path_normalized("/dir/sub/../../..", NULL, 0));
	must_pass(ensure_dir_path_normalized("/dir", "/dir/", ROOTED_PATH));
	must_pass(ensure_dir_path_normalized("/dir//", "/dir/", ROOTED_PATH));
	must_pass(ensure_dir_path_normalized("/./dir", "/dir/", ROOTED_PATH));
	must_pass(ensure_dir_path_normalized("/dir/.", "/dir/", ROOTED_PATH));
	must_pass(ensure_dir_path_normalized("/dir///./", "/dir/", ROOTED_PATH));
	must_pass(ensure_dir_path_normalized("/dir//sub/..", "/dir/", ROOTED_PATH));
	must_pass(ensure_dir_path_normalized("/dir/sub/../", "/dir/", ROOTED_PATH));
	must_pass(ensure_dir_path_normalized("//dir/sub/../.", "/dir/", ROOTED_PATH));
	must_pass(ensure_dir_path_normalized("/dir/s1/../s2/", "/dir/s2/", ROOTED_PATH));
	must_pass(ensure_dir_path_normalized("/d1/s1///s2/..//../s3/", "/d1/s3/", ROOTED_PATH));
	must_pass(ensure_dir_path_normalized("/d1/s1//../s2/../../d2", "/d2/", ROOTED_PATH));
	must_fail(ensure_dir_path_normalized("/....", NULL, 0));
	must_fail(ensure_dir_path_normalized("/...", NULL, 0));
	must_fail(ensure_dir_path_normalized("/./...", NULL, 0));
	must_fail(ensure_dir_path_normalized("/d1/...", NULL, 0));
	must_fail(ensure_dir_path_normalized("/d1/.../", NULL, 0));
	must_fail(ensure_dir_path_normalized("/d1/.../d2", NULL, 0));
352 353
END_TEST

354 355
static int ensure_joinpath(const char *path_a, const char *path_b, const char *expected_path)
{
356 357 358
	char joined_path[GIT_PATH_MAX];
	git__joinpath(joined_path, path_a, path_b);
	return strcmp(joined_path, expected_path) == 0 ? GIT_SUCCESS : GIT_ERROR;
359 360
}

361
BEGIN_TEST(path5, "properly join path components")
362 363 364
	must_pass(ensure_joinpath("", "", ""));
	must_pass(ensure_joinpath("", "a", "a"));
	must_pass(ensure_joinpath("", "/a", "/a"));
365
	must_pass(ensure_joinpath("a", "", "a/"));
366
	must_pass(ensure_joinpath("a", "/", "a/"));
367 368 369 370 371 372 373 374 375
	must_pass(ensure_joinpath("a", "b", "a/b"));
	must_pass(ensure_joinpath("/", "a", "/a"));
	must_pass(ensure_joinpath("/", "", "/"));
	must_pass(ensure_joinpath("/a", "/b", "/a/b"));
	must_pass(ensure_joinpath("/a", "/b/", "/a/b/"));
	must_pass(ensure_joinpath("/a/", "b/", "/a/b/"));
	must_pass(ensure_joinpath("/a/", "/b/", "/a/b/"));
END_TEST

376 377 378 379 380 381 382
static int ensure_joinpath_n(const char *path_a, const char *path_b, const char *path_c, const char *path_d, const char *expected_path)
{
	char joined_path[GIT_PATH_MAX];
	git__joinpath_n(joined_path, 4, path_a, path_b, path_c, path_d);
	return strcmp(joined_path, expected_path) == 0 ? GIT_SUCCESS : GIT_ERROR;
}

383
BEGIN_TEST(path6, "properly join path components for more than one path")
384 385 386 387 388 389 390 391
	must_pass(ensure_joinpath_n("", "", "", "", ""));
	must_pass(ensure_joinpath_n("", "a", "", "", "a/"));
	must_pass(ensure_joinpath_n("a", "", "", "", "a/"));
	must_pass(ensure_joinpath_n("", "", "", "a", "a"));
	must_pass(ensure_joinpath_n("a", "b", "", "/c/d/", "a/b/c/d/"));
	must_pass(ensure_joinpath_n("a", "b", "", "/c/d", "a/b/c/d"));
END_TEST

392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
static int count_number_of_path_segments(const char *path)
{
	int number = 0;
	char *current = (char *)path;

	while (*current)
	{
		if (*current++ == '/')
			number++;
	}

	assert (number > 0);

	return --number;
}

BEGIN_TEST(path7, "prevent a path which escapes the root directory from being prettified")
	char current_workdir[GIT_PATH_MAX];
	char prettified[GIT_PATH_MAX];
	int i = 0, number_to_escape;

	must_pass(gitfo_getcwd(current_workdir, sizeof(current_workdir)));

	number_to_escape = count_number_of_path_segments(current_workdir);

	for (i = 0; i < number_to_escape + 1; i++)
		git__joinpath(current_workdir, current_workdir, "../");

	must_fail(gitfo_prettify_dir_path(prettified, sizeof(prettified), current_workdir));
END_TEST

423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
typedef struct name_data {
	int  count;  /* return count */
	char *name;  /* filename     */
} name_data;

typedef struct walk_data {
	char *sub;        /* sub-directory name */
	name_data *names; /* name state data    */
} walk_data;


static char path_buffer[GIT_PATH_MAX];
static char *top_dir = "dir-walk";
static walk_data *state_loc;

static int error(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);
	fprintf(stderr, "\n");
	return -1;
}

static int setup(walk_data *d)
{
	name_data *n;

	if (gitfo_mkdir(top_dir, 0755) < 0)
		return error("can't mkdir(\"%s\")", top_dir);

	if (gitfo_chdir(top_dir) < 0)
		return error("can't chdir(\"%s\")", top_dir);

	if (strcmp(d->sub, ".") != 0)
		if (gitfo_mkdir(d->sub, 0755) < 0)
			return error("can't mkdir(\"%s\")", d->sub);

	strcpy(path_buffer, d->sub);
	state_loc = d;

	for (n = d->names; n->name; n++) {
		git_file fd = gitfo_creat(n->name, 0600);
		if (fd < 0)
			return GIT_ERROR;
		gitfo_close(fd);
		n->count = 0;
	}

	return 0;
}

static int knockdown(walk_data *d)
{
	name_data *n;

	for (n = d->names; n->name; n++) {
		if (gitfo_unlink(n->name) < 0)
			return error("can't unlink(\"%s\")", n->name);
	}

	if (strcmp(d->sub, ".") != 0)
		if (gitfo_rmdir(d->sub) < 0)
			return error("can't rmdir(\"%s\")", d->sub);

	if (gitfo_chdir("..") < 0)
		return error("can't chdir(\"..\")");

	if (gitfo_rmdir(top_dir) < 0)
		return error("can't rmdir(\"%s\")", top_dir);

	return 0;
}

static int check_counts(walk_data *d)
{
	int ret = 0;
	name_data *n;

	for (n = d->names; n->name; n++) {
		if (n->count != 1)
			ret = error("count (%d, %s)", n->count, n->name);
	}
	return ret;
}

static int one_entry(void *state, char *path)
{
	walk_data *d = (walk_data *) state;
	name_data *n;

	if (state != state_loc)
		return GIT_ERROR;

	if (path != path_buffer)
		return GIT_ERROR;

	for (n = d->names; n->name; n++) {
		if (!strcmp(n->name, path)) {
			n->count++;
			return 0;
		}
	}

	return GIT_ERROR;
}


static name_data dot_names[] = {
	{ 0, "./a" },
	{ 0, "./asdf" },
	{ 0, "./pack-foo.pack" },
	{ 0, NULL }
};
static walk_data dot = {
	".",
	dot_names
};

544
BEGIN_TEST(dirent0, "make sure that the '.' folder is not traversed")
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568

	must_pass(setup(&dot));

	must_pass(gitfo_dirent(path_buffer,
			       sizeof(path_buffer),
			       one_entry,
			       &dot));

	must_pass(check_counts(&dot));

	must_pass(knockdown(&dot));
END_TEST

static name_data sub_names[] = {
	{ 0, "sub/a" },
	{ 0, "sub/asdf" },
	{ 0, "sub/pack-foo.pack" },
	{ 0, NULL }
};
static walk_data sub = {
	"sub",
	sub_names
};

569
BEGIN_TEST(dirent1, "traverse a subfolder")
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587

	must_pass(setup(&sub));

	must_pass(gitfo_dirent(path_buffer,
			       sizeof(path_buffer),
			       one_entry,
			       &sub));

	must_pass(check_counts(&sub));

	must_pass(knockdown(&sub));
END_TEST

static walk_data sub_slash = {
	"sub/",
	sub_names
};

588
BEGIN_TEST(dirent2, "traverse a slash-terminated subfolder")
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616

	must_pass(setup(&sub_slash));

	must_pass(gitfo_dirent(path_buffer,
			       sizeof(path_buffer),
			       one_entry,
			       &sub_slash));

	must_pass(check_counts(&sub_slash));

	must_pass(knockdown(&sub_slash));
END_TEST

static name_data empty_names[] = {
	{ 0, NULL }
};
static walk_data empty = {
	"empty",
	empty_names
};

static int dont_call_me(void *GIT_UNUSED(state), char *GIT_UNUSED(path))
{
	GIT_UNUSED_ARG(state)
	GIT_UNUSED_ARG(path)
	return GIT_ERROR;
}

617
BEGIN_TEST(dirent3, "make sure that empty folders are not traversed")
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649

	must_pass(setup(&empty));

	must_pass(gitfo_dirent(path_buffer,
			       sizeof(path_buffer),
			       one_entry,
			       &empty));

	must_pass(check_counts(&empty));

	/* make sure callback not called */
	must_pass(gitfo_dirent(path_buffer,
			       sizeof(path_buffer),
			       dont_call_me,
			       &empty));

	must_pass(knockdown(&empty));
END_TEST

static name_data odd_names[] = {
	{ 0, "odd/.a" },
	{ 0, "odd/..c" },
	/* the following don't work on cygwin/win32 */
	/* { 0, "odd/.b." }, */
	/* { 0, "odd/..d.." },  */
	{ 0, NULL }
};
static walk_data odd = {
	"odd",
	odd_names
};

650
BEGIN_TEST(dirent4, "make sure that strange looking filenames ('..c') are traversed")
651 652 653 654 655 656 657 658 659 660 661 662 663 664

	must_pass(setup(&odd));

	must_pass(gitfo_dirent(path_buffer,
			       sizeof(path_buffer),
			       one_entry,
			       &odd));

	must_pass(check_counts(&odd));

	must_pass(knockdown(&odd));
END_TEST


665 666 667
BEGIN_SUITE(core)
	ADD_TEST(string0);
	ADD_TEST(string1);
668

669 670
	ADD_TEST(vector0);
	ADD_TEST(vector1);
671

672 673 674 675 676 677 678
	ADD_TEST(path0);
	ADD_TEST(path1);
	ADD_TEST(path2);
	ADD_TEST(path3);
	ADD_TEST(path4);
	ADD_TEST(path5);
	ADD_TEST(path6);
679
	ADD_TEST(path7);
680

681 682 683 684 685 686
	ADD_TEST(dirent0);
	ADD_TEST(dirent1);
	ADD_TEST(dirent2);
	ADD_TEST(dirent3);
	ADD_TEST(dirent4);
END_SUITE