]> git.proxmox.com Git - libgit2.git/blob - tests/core/filebuf.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / core / filebuf.c
1 #include "clar_libgit2.h"
2 #include "filebuf.h"
3
4 /* make sure git_filebuf_open doesn't delete an existing lock */
5 void test_core_filebuf__0(void)
6 {
7 git_filebuf file = GIT_FILEBUF_INIT;
8 int fd;
9 char test[] = "test", testlock[] = "test.lock";
10
11 fd = p_creat(testlock, 0744); /* -V536 */
12
13 cl_must_pass(fd);
14 cl_must_pass(p_close(fd));
15
16 cl_git_fail(git_filebuf_open(&file, test, 0, 0666));
17 cl_assert(git_fs_path_exists(testlock));
18
19 cl_must_pass(p_unlink(testlock));
20 }
21
22
23 /* make sure GIT_FILEBUF_APPEND works as expected */
24 void test_core_filebuf__1(void)
25 {
26 git_filebuf file = GIT_FILEBUF_INIT;
27 char test[] = "test";
28
29 cl_git_mkfile(test, "libgit2 rocks\n");
30
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));
34
35 cl_assert_equal_file("libgit2 rocks\nlibgit2 rocks\n", 0, test);
36
37 cl_must_pass(p_unlink(test));
38 }
39
40
41 /* make sure git_filebuf_write writes large buffer correctly */
42 void test_core_filebuf__2(void)
43 {
44 git_filebuf file = GIT_FILEBUF_INIT;
45 char test[] = "test";
46 unsigned char buf[4096 * 4]; /* 2 * WRITE_BUFFER_SIZE */
47
48 memset(buf, 0xfe, sizeof(buf));
49
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));
53
54 cl_assert_equal_file((char *)buf, sizeof(buf), test);
55
56 cl_must_pass(p_unlink(test));
57 }
58
59 /* make sure git_filebuf_cleanup clears the buffer */
60 void test_core_filebuf__4(void)
61 {
62 git_filebuf file = GIT_FILEBUF_INIT;
63 char test[] = "test";
64
65 cl_assert(file.buffer == NULL);
66
67 cl_git_pass(git_filebuf_open(&file, test, 0, 0666));
68 cl_assert(file.buffer != NULL);
69
70 git_filebuf_cleanup(&file);
71 cl_assert(file.buffer == NULL);
72 }
73
74
75 /* make sure git_filebuf_commit clears the buffer */
76 void test_core_filebuf__5(void)
77 {
78 git_filebuf file = GIT_FILEBUF_INIT;
79 char test[] = "test";
80
81 cl_assert(file.buffer == NULL);
82
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);
87
88 cl_git_pass(git_filebuf_commit(&file));
89 cl_assert(file.buffer == NULL);
90
91 cl_must_pass(p_unlink(test));
92 }
93
94
95 /* make sure git_filebuf_commit takes umask into account */
96 void test_core_filebuf__umask(void)
97 {
98 git_filebuf file = GIT_FILEBUF_INIT;
99 char test[] = "test";
100 struct stat statbuf;
101 mode_t mask, os_mask;
102
103 #ifdef GIT_WIN32
104 os_mask = 0600;
105 #else
106 os_mask = 0777;
107 #endif
108
109 p_umask(mask = p_umask(0));
110
111 cl_assert(file.buffer == NULL);
112
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);
117
118 cl_git_pass(git_filebuf_commit(&file));
119 cl_assert(file.buffer == NULL);
120
121 cl_must_pass(p_stat("test", &statbuf));
122 cl_assert_equal_i(statbuf.st_mode & os_mask, (0666 & ~mask) & os_mask);
123
124 cl_must_pass(p_unlink(test));
125 }
126
127 void test_core_filebuf__rename_error(void)
128 {
129 git_filebuf file = GIT_FILEBUF_INIT;
130 char *dir = "subdir", *test = "subdir/test", *test_lock = "subdir/test.lock";
131 int fd;
132
133 #ifndef GIT_WIN32
134 cl_skip();
135 #endif
136
137 cl_git_pass(p_mkdir(dir, 0666));
138 cl_git_mkfile(test, "dummy content");
139 fd = p_open(test, O_RDONLY);
140 cl_assert(fd > 0);
141 cl_git_pass(git_filebuf_open(&file, test, 0, 0666));
142
143 cl_git_pass(git_filebuf_printf(&file, "%s\n", "libgit2 rocks"));
144
145 cl_assert_equal_i(true, git_fs_path_exists(test_lock));
146
147 cl_git_fail(git_filebuf_commit(&file));
148 p_close(fd);
149
150 git_filebuf_cleanup(&file);
151
152 cl_assert_equal_i(false, git_fs_path_exists(test_lock));
153 }
154
155 void test_core_filebuf__symlink_follow(void)
156 {
157 git_filebuf file = GIT_FILEBUF_INIT;
158 const char *dir = "linkdir", *source = "linkdir/link";
159
160 if (!git_fs_path_supports_symlinks(clar_sandbox_path()))
161 cl_skip();
162
163 cl_git_pass(p_mkdir(dir, 0777));
164 cl_git_pass(p_symlink("target", source));
165
166 cl_git_pass(git_filebuf_open(&file, source, 0, 0666));
167 cl_git_pass(git_filebuf_printf(&file, "%s\n", "libgit2 rocks"));
168
169 cl_assert_equal_i(true, git_fs_path_exists("linkdir/target.lock"));
170
171 cl_git_pass(git_filebuf_commit(&file));
172 cl_assert_equal_i(true, git_fs_path_exists("linkdir/target"));
173
174 git_filebuf_cleanup(&file);
175
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"));
179
180 cl_assert_equal_i(true, git_fs_path_exists("linkdir/target.lock"));
181
182 cl_git_pass(git_filebuf_commit(&file));
183 cl_assert_equal_i(true, git_fs_path_exists("linkdir/target"));
184
185 git_filebuf_cleanup(&file);
186 cl_git_pass(git_futils_rmdir_r(dir, NULL, GIT_RMDIR_REMOVE_FILES));
187 }
188
189 void test_core_filebuf__symlink_follow_absolute_paths(void)
190 {
191 git_filebuf file = GIT_FILEBUF_INIT;
192 git_str source = GIT_STR_INIT, target = GIT_STR_INIT;
193
194 if (!git_fs_path_supports_symlinks(clar_sandbox_path()))
195 cl_skip();
196
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));
201
202 cl_git_pass(git_filebuf_open(&file, source.ptr, 0, 0666));
203 cl_git_pass(git_filebuf_printf(&file, "%s\n", "libgit2 rocks"));
204
205 cl_assert_equal_i(true, git_fs_path_exists("linkdir/target.lock"));
206
207 cl_git_pass(git_filebuf_commit(&file));
208 cl_assert_equal_i(true, git_fs_path_exists("linkdir/target"));
209
210 git_filebuf_cleanup(&file);
211 git_str_dispose(&source);
212 git_str_dispose(&target);
213
214 cl_git_pass(git_futils_rmdir_r("linkdir", NULL, GIT_RMDIR_REMOVE_FILES));
215 }
216
217 void test_core_filebuf__symlink_depth(void)
218 {
219 git_filebuf file = GIT_FILEBUF_INIT;
220 const char *dir = "linkdir", *source = "linkdir/link";
221
222 if (!git_fs_path_supports_symlinks(clar_sandbox_path()))
223 cl_skip();
224
225 cl_git_pass(p_mkdir(dir, 0777));
226 /* Endless loop */
227 cl_git_pass(p_symlink("link", source));
228
229 cl_git_fail(git_filebuf_open(&file, source, 0, 0666));
230
231 cl_git_pass(git_futils_rmdir_r(dir, NULL, GIT_RMDIR_REMOVE_FILES));
232 }
233
234 void test_core_filebuf__hidden_file(void)
235 {
236 #ifndef GIT_WIN32
237 cl_skip();
238 #else
239 git_filebuf file = GIT_FILEBUF_INIT;
240 char *dir = "hidden", *test = "hidden/test";
241 bool hidden;
242
243 cl_git_pass(p_mkdir(dir, 0666));
244 cl_git_mkfile(test, "dummy content");
245
246 cl_git_pass(git_win32__set_hidden(test, true));
247 cl_git_pass(git_win32__hidden(&hidden, test));
248 cl_assert(hidden);
249
250 cl_git_pass(git_filebuf_open(&file, test, 0, 0666));
251
252 cl_git_pass(git_filebuf_printf(&file, "%s\n", "libgit2 rocks"));
253
254 cl_git_pass(git_filebuf_commit(&file));
255
256 git_filebuf_cleanup(&file);
257 #endif
258 }
259
260 void test_core_filebuf__detects_directory(void)
261 {
262 git_filebuf file = GIT_FILEBUF_INIT;
263
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"));
267 }