Blame tests/core/dirent.c

Packit Service 20376f
#include "clar_libgit2.h"
Packit Service 20376f
#include "fileops.h"
Packit Service 20376f
Packit Service 20376f
typedef struct name_data {
Packit Service 20376f
	int count; /* return count */
Packit Service 20376f
	char *name; /* filename		*/
Packit Service 20376f
} name_data;
Packit Service 20376f
Packit Service 20376f
typedef struct walk_data {
Packit Service 20376f
	char *sub;		/* sub-directory name */
Packit Service 20376f
	name_data *names; /* name state data	*/
Packit Service 20376f
	git_buf path;
Packit Service 20376f
} walk_data;
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
static char *top_dir = "dir-walk";
Packit Service 20376f
static walk_data *state_loc;
Packit Service 20376f
Packit Service 20376f
static void setup(walk_data *d)
Packit Service 20376f
{
Packit Service 20376f
	name_data *n;
Packit Service 20376f
Packit Service 20376f
	cl_must_pass(p_mkdir(top_dir, 0777));
Packit Service 20376f
Packit Service 20376f
	cl_must_pass(p_chdir(top_dir));
Packit Service 20376f
Packit Service 20376f
	if (strcmp(d->sub, ".") != 0)
Packit Service 20376f
		cl_must_pass(p_mkdir(d->sub, 0777));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_sets(&d->path, d->sub));
Packit Service 20376f
Packit Service 20376f
	state_loc = d;
Packit Service 20376f
Packit Service 20376f
	for (n = d->names; n->name; n++) {
Packit Service 20376f
		git_file fd = p_creat(n->name, 0666);
Packit Service 20376f
		cl_assert(fd >= 0);
Packit Service 20376f
		p_close(fd);
Packit Service 20376f
		n->count = 0;
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void dirent_cleanup__cb(void *_d)
Packit Service 20376f
{
Packit Service 20376f
	walk_data *d = _d;
Packit Service 20376f
	name_data *n;
Packit Service 20376f
Packit Service 20376f
	for (n = d->names; n->name; n++) {
Packit Service 20376f
		cl_must_pass(p_unlink(n->name));
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (strcmp(d->sub, ".") != 0)
Packit Service 20376f
		cl_must_pass(p_rmdir(d->sub));
Packit Service 20376f
Packit Service 20376f
	cl_must_pass(p_chdir(".."));
Packit Service 20376f
Packit Service 20376f
	cl_must_pass(p_rmdir(top_dir));
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&d->path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void check_counts(walk_data *d)
Packit Service 20376f
{
Packit Service 20376f
	name_data *n;
Packit Service 20376f
Packit Service 20376f
	for (n = d->names; n->name; n++) {
Packit Service 20376f
		cl_assert(n->count == 1);
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int update_count(name_data *data, const char *name)
Packit Service 20376f
{
Packit Service 20376f
	name_data *n;
Packit Service 20376f
Packit Service 20376f
	for (n = data; n->name; n++) {
Packit Service 20376f
		if (!strcmp(n->name, name)) {
Packit Service 20376f
			n->count++;
Packit Service 20376f
			return 0;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return GIT_ERROR;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int one_entry(void *state, git_buf *path)
Packit Service 20376f
{
Packit Service 20376f
	walk_data *d = (walk_data *) state;
Packit Service 20376f
Packit Service 20376f
	if (state != state_loc)
Packit Service 20376f
		return GIT_ERROR;
Packit Service 20376f
Packit Service 20376f
	if (path != &d->path)
Packit Service 20376f
		return GIT_ERROR;
Packit Service 20376f
Packit Service 20376f
	return update_count(d->names, path->ptr);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
static name_data dot_names[] = {
Packit Service 20376f
	{ 0, "./a" },
Packit Service 20376f
	{ 0, "./asdf" },
Packit Service 20376f
	{ 0, "./pack-foo.pack" },
Packit Service 20376f
	{ 0, NULL }
Packit Service 20376f
};
Packit Service 20376f
static walk_data dot = {
Packit Service 20376f
	".",
Packit Service 20376f
	dot_names,
Packit Service 20376f
	GIT_BUF_INIT
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
/* make sure that the '.' folder is not traversed */
Packit Service 20376f
void test_core_dirent__dont_traverse_dot(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_set_cleanup(&dirent_cleanup__cb, &dot);
Packit Service 20376f
	setup(&dot);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_path_direach(&dot.path, 0, one_entry, &dot);;
Packit Service 20376f
Packit Service 20376f
	check_counts(&dot);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
static name_data sub_names[] = {
Packit Service 20376f
	{ 0, "sub/a" },
Packit Service 20376f
	{ 0, "sub/asdf" },
Packit Service 20376f
	{ 0, "sub/pack-foo.pack" },
Packit Service 20376f
	{ 0, NULL }
Packit Service 20376f
};
Packit Service 20376f
static walk_data sub = {
Packit Service 20376f
	"sub",
Packit Service 20376f
	sub_names,
Packit Service 20376f
	GIT_BUF_INIT
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
/* traverse a subfolder */
Packit Service 20376f
void test_core_dirent__traverse_subfolder(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_set_cleanup(&dirent_cleanup__cb, &sub);
Packit Service 20376f
	setup(&sub);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_path_direach(&sub.path, 0, one_entry, &sub);;
Packit Service 20376f
Packit Service 20376f
	check_counts(&sub);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
static walk_data sub_slash = {
Packit Service 20376f
	"sub/",
Packit Service 20376f
	sub_names,
Packit Service 20376f
	GIT_BUF_INIT
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
/* traverse a slash-terminated subfolder */
Packit Service 20376f
void test_core_dirent__traverse_slash_terminated_folder(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_set_cleanup(&dirent_cleanup__cb, &sub_slash);
Packit Service 20376f
	setup(&sub_slash);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_path_direach(&sub_slash.path, 0, one_entry, &sub_slash));
Packit Service 20376f
Packit Service 20376f
	check_counts(&sub_slash);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
static name_data empty_names[] = {
Packit Service 20376f
	{ 0, NULL }
Packit Service 20376f
};
Packit Service 20376f
static walk_data empty = {
Packit Service 20376f
	"empty",
Packit Service 20376f
	empty_names,
Packit Service 20376f
	GIT_BUF_INIT
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
/* make sure that empty folders are not traversed */
Packit Service 20376f
void test_core_dirent__dont_traverse_empty_folders(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_set_cleanup(&dirent_cleanup__cb, &empty);
Packit Service 20376f
	setup(&empty);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_path_direach(&empty.path, 0, one_entry, &empty);;
Packit Service 20376f
Packit Service 20376f
	check_counts(&empty);
Packit Service 20376f
Packit Service 20376f
	/* make sure callback not called */
Packit Service 20376f
	cl_assert(git_path_is_empty_dir(empty.path.ptr));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static name_data odd_names[] = {
Packit Service 20376f
	{ 0, "odd/.a" },
Packit Service 20376f
	{ 0, "odd/..c" },
Packit Service 20376f
	/* the following don't work on cygwin/win32 */
Packit Service 20376f
	/* { 0, "odd/.b." }, */
Packit Service 20376f
	/* { 0, "odd/..d.." }, */
Packit Service 20376f
	{ 0, NULL }
Packit Service 20376f
};
Packit Service 20376f
static walk_data odd = {
Packit Service 20376f
	"odd",
Packit Service 20376f
	odd_names,
Packit Service 20376f
	GIT_BUF_INIT
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
/* make sure that strange looking filenames ('..c') are traversed */
Packit Service 20376f
void test_core_dirent__traverse_weird_filenames(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_set_cleanup(&dirent_cleanup__cb, &odd;;
Packit Service 20376f
	setup(&odd;;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_path_direach(&odd.path, 0, one_entry, &odd));
Packit Service 20376f
Packit Service 20376f
	check_counts(&odd;;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* test filename length limits */
Packit Service 20376f
void test_core_dirent__length_limits(void)
Packit Service 20376f
{
Packit Service 20376f
	char *big_filename = (char *)git__malloc(FILENAME_MAX + 1);
Packit Service 20376f
	memset(big_filename, 'a', FILENAME_MAX + 1);
Packit Service 20376f
	big_filename[FILENAME_MAX] = 0;
Packit Service 20376f
Packit Service 20376f
	cl_must_fail(p_creat(big_filename, 0666));
Packit Service 20376f
Packit Service 20376f
	git__free(big_filename);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_core_dirent__empty_dir(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_must_pass(p_mkdir("empty_dir", 0777));
Packit Service 20376f
	cl_assert(git_path_is_empty_dir("empty_dir"));
Packit Service 20376f
Packit Service 20376f
	cl_git_mkfile("empty_dir/content", "whatever\n");
Packit Service 20376f
	cl_assert(!git_path_is_empty_dir("empty_dir"));
Packit Service 20376f
	cl_assert(!git_path_is_empty_dir("empty_dir/content"));
Packit Service 20376f
Packit Service 20376f
	cl_must_pass(p_unlink("empty_dir/content"));
Packit Service 20376f
Packit Service 20376f
	cl_must_pass(p_mkdir("empty_dir/content", 0777));
Packit Service 20376f
	cl_assert(!git_path_is_empty_dir("empty_dir"));
Packit Service 20376f
	cl_assert(git_path_is_empty_dir("empty_dir/content"));
Packit Service 20376f
Packit Service 20376f
	cl_must_pass(p_rmdir("empty_dir/content"));
Packit Service 20376f
Packit Service 20376f
	cl_must_pass(p_rmdir("empty_dir"));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void handle_next(git_path_diriter *diriter, walk_data *walk)
Packit Service 20376f
{
Packit Service 20376f
	const char *fullpath, *filename;
Packit Service 20376f
	size_t fullpath_len, filename_len;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_path_diriter_fullpath(&fullpath, &fullpath_len, diriter));
Packit Service 20376f
	cl_git_pass(git_path_diriter_filename(&filename, &filename_len, diriter));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_strn(fullpath, "sub/", 4);
Packit Service 20376f
	cl_assert_equal_s(fullpath+4, filename);
Packit Service 20376f
Packit Service 20376f
	update_count(walk->names, fullpath);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* test directory iterator */
Packit Service 20376f
void test_core_dirent__diriter_with_fullname(void)
Packit Service 20376f
{
Packit Service 20376f
	git_path_diriter diriter = GIT_PATH_DIRITER_INIT;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	cl_set_cleanup(&dirent_cleanup__cb, &sub);
Packit Service 20376f
	setup(&sub);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_path_diriter_init(&diriter, sub.path.ptr, 0));
Packit Service 20376f
Packit Service 20376f
	while ((error = git_path_diriter_next(&diriter)) == 0)
Packit Service 20376f
		handle_next(&diriter, &sub);
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(error, GIT_ITEROVER);
Packit Service 20376f
Packit Service 20376f
	git_path_diriter_free(&diriter);
Packit Service 20376f
Packit Service 20376f
	check_counts(&sub);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_core_dirent__diriter_at_directory_root(void)
Packit Service 20376f
{
Packit Service 20376f
	git_path_diriter diriter = GIT_PATH_DIRITER_INIT;
Packit Service 20376f
	const char *sandbox_path, *path;
Packit Service 20376f
	char *root_path;
Packit Service 20376f
	size_t path_len;
Packit Service 20376f
	int root_offset, error;
Packit Service 20376f
Packit Service 20376f
	sandbox_path = clar_sandbox_path();
Packit Service 20376f
	cl_assert((root_offset = git_path_root(sandbox_path)) >= 0);
Packit Service 20376f
Packit Service 20376f
	cl_assert(root_path = git__calloc(1, root_offset + 2));
Packit Service 20376f
	strncpy(root_path, sandbox_path, root_offset + 1);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_path_diriter_init(&diriter, root_path, 0));
Packit Service 20376f
Packit Service 20376f
	while ((error = git_path_diriter_next(&diriter)) == 0) {
Packit Service 20376f
		cl_git_pass(git_path_diriter_fullpath(&path, &path_len, &diriter));
Packit Service 20376f
Packit Service 20376f
		cl_assert(path_len > (size_t)(root_offset + 1));
Packit Service 20376f
		cl_assert(path[root_offset+1] != '/');
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(error, GIT_ITEROVER);
Packit Service 20376f
Packit Service 20376f
	git_path_diriter_free(&diriter);
Packit Service 20376f
	git__free(root_path);
Packit Service 20376f
}