filebuf.c 2.93 KB
Newer Older
1
#include "clar_libgit2.h"
Vicent Marti committed
2 3 4 5 6
#include "filebuf.h"

/* make sure git_filebuf_open doesn't delete an existing lock */
void test_core_filebuf__0(void)
{
7
	git_filebuf file = GIT_FILEBUF_INIT;
Vicent Marti committed
8 9 10
	int fd;
	char test[] = "test", testlock[] = "test.lock";

11
	fd = p_creat(testlock, 0744); //-V536
Vicent Marti committed
12 13 14 15

	cl_must_pass(fd);
	cl_must_pass(p_close(fd));

16
	cl_git_fail(git_filebuf_open(&file, test, 0, 0666));
17
	cl_assert(git_path_exists(testlock));
Vicent Marti committed
18 19 20 21 22 23 24 25

	cl_must_pass(p_unlink(testlock));
}


/* make sure GIT_FILEBUF_APPEND works as expected */
void test_core_filebuf__1(void)
{
26
	git_filebuf file = GIT_FILEBUF_INIT;
Vicent Marti committed
27 28
	char test[] = "test";

29
	cl_git_mkfile(test, "libgit2 rocks\n");
Vicent Marti committed
30

31
	cl_git_pass(git_filebuf_open(&file, test, GIT_FILEBUF_APPEND, 0666));
Vicent Marti committed
32
	cl_git_pass(git_filebuf_printf(&file, "%s\n", "libgit2 rocks"));
33
	cl_git_pass(git_filebuf_commit(&file));
Vicent Marti committed
34

35 36
	cl_assert_equal_file("libgit2 rocks\nlibgit2 rocks\n", 0, test);

Vicent Marti committed
37 38 39 40 41 42 43
	cl_must_pass(p_unlink(test));
}


/* make sure git_filebuf_write writes large buffer correctly */
void test_core_filebuf__2(void)
{
44
	git_filebuf file = GIT_FILEBUF_INIT;
Vicent Marti committed
45 46 47 48 49
	char test[] = "test";
	unsigned char buf[4096 * 4]; /* 2 * WRITE_BUFFER_SIZE */

	memset(buf, 0xfe, sizeof(buf));

50
	cl_git_pass(git_filebuf_open(&file, test, 0, 0666));
Vicent Marti committed
51
	cl_git_pass(git_filebuf_write(&file, buf, sizeof(buf)));
52
	cl_git_pass(git_filebuf_commit(&file));
Vicent Marti committed
53

54 55
	cl_assert_equal_file((char *)buf, sizeof(buf), test);

Vicent Marti committed
56 57 58
	cl_must_pass(p_unlink(test));
}

59 60 61 62 63 64 65 66
/* make sure git_filebuf_cleanup clears the buffer */
void test_core_filebuf__4(void)
{
	git_filebuf file = GIT_FILEBUF_INIT;
	char test[] = "test";

	cl_assert(file.buffer == NULL);

67
	cl_git_pass(git_filebuf_open(&file, test, 0, 0666));
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	cl_assert(file.buffer != NULL);

	git_filebuf_cleanup(&file);
	cl_assert(file.buffer == NULL);
}


/* make sure git_filebuf_commit clears the buffer */
void test_core_filebuf__5(void)
{
	git_filebuf file = GIT_FILEBUF_INIT;
	char test[] = "test";

	cl_assert(file.buffer == NULL);

83
	cl_git_pass(git_filebuf_open(&file, test, 0, 0666));
84 85 86 87
	cl_assert(file.buffer != NULL);
	cl_git_pass(git_filebuf_printf(&file, "%s\n", "libgit2 rocks"));
	cl_assert(file.buffer != NULL);

88
	cl_git_pass(git_filebuf_commit(&file));
89 90 91 92
	cl_assert(file.buffer == NULL);

	cl_must_pass(p_unlink(test));
}
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112


/* make sure git_filebuf_commit takes umask into account */
void test_core_filebuf__umask(void)
{
	git_filebuf file = GIT_FILEBUF_INIT;
	char test[] = "test";
	struct stat statbuf;
	mode_t mask, os_mask;

#ifdef GIT_WIN32
	os_mask = 0600;
#else
	os_mask = 0777;
#endif

	p_umask(mask = p_umask(0));

	cl_assert(file.buffer == NULL);

113
	cl_git_pass(git_filebuf_open(&file, test, 0, 0666));
114 115 116 117
	cl_assert(file.buffer != NULL);
	cl_git_pass(git_filebuf_printf(&file, "%s\n", "libgit2 rocks"));
	cl_assert(file.buffer != NULL);

118
	cl_git_pass(git_filebuf_commit(&file));
119 120 121 122 123 124 125 126
	cl_assert(file.buffer == NULL);

	cl_must_pass(p_stat("test", &statbuf));
	cl_assert_equal_i(statbuf.st_mode & os_mask, (0666 & ~mask) & os_mask);

	cl_must_pass(p_unlink(test));
}