]>
git.proxmox.com Git - libgit2.git/blob - tests/core/filebuf.c
1 #include "clar_libgit2.h"
4 /* make sure git_filebuf_open doesn't delete an existing lock */
5 void test_core_filebuf__0(void)
7 git_filebuf file
= GIT_FILEBUF_INIT
;
9 char test
[] = "test", testlock
[] = "test.lock";
11 fd
= p_creat(testlock
, 0744); /* -V536 */
14 cl_must_pass(p_close(fd
));
16 cl_git_fail(git_filebuf_open(&file
, test
, 0, 0666));
17 cl_assert(git_fs_path_exists(testlock
));
19 cl_must_pass(p_unlink(testlock
));
23 /* make sure GIT_FILEBUF_APPEND works as expected */
24 void test_core_filebuf__1(void)
26 git_filebuf file
= GIT_FILEBUF_INIT
;
29 cl_git_mkfile(test
, "libgit2 rocks\n");
31 cl_git_pass(git_filebuf_open(&file
, test
, GIT_FILEBUF_APPEND
, 0666));
32 cl_git_pass(git_filebuf_printf(&file
, "%s\n", "libgit2 rocks"));
33 cl_git_pass(git_filebuf_commit(&file
));
35 cl_assert_equal_file("libgit2 rocks\nlibgit2 rocks\n", 0, test
);
37 cl_must_pass(p_unlink(test
));
41 /* make sure git_filebuf_write writes large buffer correctly */
42 void test_core_filebuf__2(void)
44 git_filebuf file
= GIT_FILEBUF_INIT
;
46 unsigned char buf
[4096 * 4]; /* 2 * WRITE_BUFFER_SIZE */
48 memset(buf
, 0xfe, sizeof(buf
));
50 cl_git_pass(git_filebuf_open(&file
, test
, 0, 0666));
51 cl_git_pass(git_filebuf_write(&file
, buf
, sizeof(buf
)));
52 cl_git_pass(git_filebuf_commit(&file
));
54 cl_assert_equal_file((char *)buf
, sizeof(buf
), test
);
56 cl_must_pass(p_unlink(test
));
59 /* make sure git_filebuf_cleanup clears the buffer */
60 void test_core_filebuf__4(void)
62 git_filebuf file
= GIT_FILEBUF_INIT
;
65 cl_assert(file
.buffer
== NULL
);
67 cl_git_pass(git_filebuf_open(&file
, test
, 0, 0666));
68 cl_assert(file
.buffer
!= NULL
);
70 git_filebuf_cleanup(&file
);
71 cl_assert(file
.buffer
== NULL
);
75 /* make sure git_filebuf_commit clears the buffer */
76 void test_core_filebuf__5(void)
78 git_filebuf file
= GIT_FILEBUF_INIT
;
81 cl_assert(file
.buffer
== NULL
);
83 cl_git_pass(git_filebuf_open(&file
, test
, 0, 0666));
84 cl_assert(file
.buffer
!= NULL
);
85 cl_git_pass(git_filebuf_printf(&file
, "%s\n", "libgit2 rocks"));
86 cl_assert(file
.buffer
!= NULL
);
88 cl_git_pass(git_filebuf_commit(&file
));
89 cl_assert(file
.buffer
== NULL
);
91 cl_must_pass(p_unlink(test
));
95 /* make sure git_filebuf_commit takes umask into account */
96 void test_core_filebuf__umask(void)
98 git_filebuf file
= GIT_FILEBUF_INIT
;
101 mode_t mask
, os_mask
;
109 p_umask(mask
= p_umask(0));
111 cl_assert(file
.buffer
== NULL
);
113 cl_git_pass(git_filebuf_open(&file
, test
, 0, 0666));
114 cl_assert(file
.buffer
!= NULL
);
115 cl_git_pass(git_filebuf_printf(&file
, "%s\n", "libgit2 rocks"));
116 cl_assert(file
.buffer
!= NULL
);
118 cl_git_pass(git_filebuf_commit(&file
));
119 cl_assert(file
.buffer
== NULL
);
121 cl_must_pass(p_stat("test", &statbuf
));
122 cl_assert_equal_i(statbuf
.st_mode
& os_mask
, (0666 & ~mask
) & os_mask
);
124 cl_must_pass(p_unlink(test
));
127 void test_core_filebuf__rename_error(void)
129 git_filebuf file
= GIT_FILEBUF_INIT
;
130 char *dir
= "subdir", *test
= "subdir/test", *test_lock
= "subdir/test.lock";
137 cl_git_pass(p_mkdir(dir
, 0666));
138 cl_git_mkfile(test
, "dummy content");
139 fd
= p_open(test
, O_RDONLY
);
141 cl_git_pass(git_filebuf_open(&file
, test
, 0, 0666));
143 cl_git_pass(git_filebuf_printf(&file
, "%s\n", "libgit2 rocks"));
145 cl_assert_equal_i(true, git_fs_path_exists(test_lock
));
147 cl_git_fail(git_filebuf_commit(&file
));
150 git_filebuf_cleanup(&file
);
152 cl_assert_equal_i(false, git_fs_path_exists(test_lock
));
155 void test_core_filebuf__symlink_follow(void)
157 git_filebuf file
= GIT_FILEBUF_INIT
;
158 const char *dir
= "linkdir", *source
= "linkdir/link";
160 if (!git_fs_path_supports_symlinks(clar_sandbox_path()))
163 cl_git_pass(p_mkdir(dir
, 0777));
164 cl_git_pass(p_symlink("target", source
));
166 cl_git_pass(git_filebuf_open(&file
, source
, 0, 0666));
167 cl_git_pass(git_filebuf_printf(&file
, "%s\n", "libgit2 rocks"));
169 cl_assert_equal_i(true, git_fs_path_exists("linkdir/target.lock"));
171 cl_git_pass(git_filebuf_commit(&file
));
172 cl_assert_equal_i(true, git_fs_path_exists("linkdir/target"));
174 git_filebuf_cleanup(&file
);
176 /* The second time around, the target file does exist */
177 cl_git_pass(git_filebuf_open(&file
, source
, 0, 0666));
178 cl_git_pass(git_filebuf_printf(&file
, "%s\n", "libgit2 rocks"));
180 cl_assert_equal_i(true, git_fs_path_exists("linkdir/target.lock"));
182 cl_git_pass(git_filebuf_commit(&file
));
183 cl_assert_equal_i(true, git_fs_path_exists("linkdir/target"));
185 git_filebuf_cleanup(&file
);
186 cl_git_pass(git_futils_rmdir_r(dir
, NULL
, GIT_RMDIR_REMOVE_FILES
));
189 void test_core_filebuf__symlink_follow_absolute_paths(void)
191 git_filebuf file
= GIT_FILEBUF_INIT
;
192 git_str source
= GIT_STR_INIT
, target
= GIT_STR_INIT
;
194 if (!git_fs_path_supports_symlinks(clar_sandbox_path()))
197 cl_git_pass(git_str_joinpath(&source
, clar_sandbox_path(), "linkdir/link"));
198 cl_git_pass(git_str_joinpath(&target
, clar_sandbox_path(), "linkdir/target"));
199 cl_git_pass(p_mkdir("linkdir", 0777));
200 cl_git_pass(p_symlink(target
.ptr
, source
.ptr
));
202 cl_git_pass(git_filebuf_open(&file
, source
.ptr
, 0, 0666));
203 cl_git_pass(git_filebuf_printf(&file
, "%s\n", "libgit2 rocks"));
205 cl_assert_equal_i(true, git_fs_path_exists("linkdir/target.lock"));
207 cl_git_pass(git_filebuf_commit(&file
));
208 cl_assert_equal_i(true, git_fs_path_exists("linkdir/target"));
210 git_filebuf_cleanup(&file
);
211 git_str_dispose(&source
);
212 git_str_dispose(&target
);
214 cl_git_pass(git_futils_rmdir_r("linkdir", NULL
, GIT_RMDIR_REMOVE_FILES
));
217 void test_core_filebuf__symlink_depth(void)
219 git_filebuf file
= GIT_FILEBUF_INIT
;
220 const char *dir
= "linkdir", *source
= "linkdir/link";
222 if (!git_fs_path_supports_symlinks(clar_sandbox_path()))
225 cl_git_pass(p_mkdir(dir
, 0777));
227 cl_git_pass(p_symlink("link", source
));
229 cl_git_fail(git_filebuf_open(&file
, source
, 0, 0666));
231 cl_git_pass(git_futils_rmdir_r(dir
, NULL
, GIT_RMDIR_REMOVE_FILES
));
234 void test_core_filebuf__hidden_file(void)
239 git_filebuf file
= GIT_FILEBUF_INIT
;
240 char *dir
= "hidden", *test
= "hidden/test";
243 cl_git_pass(p_mkdir(dir
, 0666));
244 cl_git_mkfile(test
, "dummy content");
246 cl_git_pass(git_win32__set_hidden(test
, true));
247 cl_git_pass(git_win32__hidden(&hidden
, test
));
250 cl_git_pass(git_filebuf_open(&file
, test
, 0, 0666));
252 cl_git_pass(git_filebuf_printf(&file
, "%s\n", "libgit2 rocks"));
254 cl_git_pass(git_filebuf_commit(&file
));
256 git_filebuf_cleanup(&file
);
260 void test_core_filebuf__detects_directory(void)
262 git_filebuf file
= GIT_FILEBUF_INIT
;
264 cl_must_pass(p_mkdir("foo", 0777));
265 cl_git_fail_with(GIT_EDIRECTORY
, git_filebuf_open(&file
, "foo", 0, 0666));
266 cl_must_pass(p_rmdir("foo"));