]> git.proxmox.com Git - libgit2.git/blob - tests/attr/repo.c
Some further sandboxing cleanups to tests
[libgit2.git] / tests / attr / repo.c
1 #include "clar_libgit2.h"
2 #include "fileops.h"
3 #include "git2/attr.h"
4 #include "attr.h"
5
6 #include "attr_expect.h"
7
8 static git_repository *g_repo = NULL;
9
10 void test_attr_repo__initialize(void)
11 {
12 g_repo = cl_git_sandbox_init("attr");
13 }
14
15 void test_attr_repo__cleanup(void)
16 {
17 cl_git_sandbox_cleanup();
18 g_repo = NULL;
19 }
20
21 static struct attr_expected get_one_test_cases[] = {
22 { "root_test1", "repoattr", EXPECT_TRUE, NULL },
23 { "root_test1", "rootattr", EXPECT_TRUE, NULL },
24 { "root_test1", "missingattr", EXPECT_UNDEFINED, NULL },
25 { "root_test1", "subattr", EXPECT_UNDEFINED, NULL },
26 { "root_test1", "negattr", EXPECT_UNDEFINED, NULL },
27 { "root_test2", "repoattr", EXPECT_TRUE, NULL },
28 { "root_test2", "rootattr", EXPECT_FALSE, NULL },
29 { "root_test2", "missingattr", EXPECT_UNDEFINED, NULL },
30 { "root_test2", "multiattr", EXPECT_FALSE, NULL },
31 { "root_test3", "repoattr", EXPECT_TRUE, NULL },
32 { "root_test3", "rootattr", EXPECT_UNDEFINED, NULL },
33 { "root_test3", "multiattr", EXPECT_STRING, "3" },
34 { "root_test3", "multi2", EXPECT_UNDEFINED, NULL },
35 { "sub/subdir_test1", "repoattr", EXPECT_TRUE, NULL },
36 { "sub/subdir_test1", "rootattr", EXPECT_TRUE, NULL },
37 { "sub/subdir_test1", "missingattr", EXPECT_UNDEFINED, NULL },
38 { "sub/subdir_test1", "subattr", EXPECT_STRING, "yes" },
39 { "sub/subdir_test1", "negattr", EXPECT_FALSE, NULL },
40 { "sub/subdir_test1", "another", EXPECT_UNDEFINED, NULL },
41 { "sub/subdir_test2.txt", "repoattr", EXPECT_TRUE, NULL },
42 { "sub/subdir_test2.txt", "rootattr", EXPECT_TRUE, NULL },
43 { "sub/subdir_test2.txt", "missingattr", EXPECT_UNDEFINED, NULL },
44 { "sub/subdir_test2.txt", "subattr", EXPECT_STRING, "yes" },
45 { "sub/subdir_test2.txt", "negattr", EXPECT_FALSE, NULL },
46 { "sub/subdir_test2.txt", "another", EXPECT_STRING, "zero" },
47 { "sub/subdir_test2.txt", "reposub", EXPECT_TRUE, NULL },
48 { "sub/sub/subdir.txt", "another", EXPECT_STRING, "one" },
49 { "sub/sub/subdir.txt", "reposubsub", EXPECT_TRUE, NULL },
50 { "sub/sub/subdir.txt", "reposub", EXPECT_UNDEFINED, NULL },
51 { "does-not-exist", "foo", EXPECT_STRING, "yes" },
52 { "sub/deep/file", "deepdeep", EXPECT_TRUE, NULL },
53 { "sub/sub/d/no", "test", EXPECT_STRING, "a/b/d/*" },
54 { "sub/sub/d/yes", "test", EXPECT_UNDEFINED, NULL },
55 };
56
57 void test_attr_repo__get_one(void)
58 {
59 int i;
60
61 for (i = 0; i < (int)ARRAY_SIZE(get_one_test_cases); ++i) {
62 struct attr_expected *scan = &get_one_test_cases[i];
63 const char *value;
64
65 cl_git_pass(git_attr_get(&value, g_repo, 0, scan->path, scan->attr));
66 attr_check_expected(
67 scan->expected, scan->expected_str, scan->attr, value);
68 }
69
70 cl_assert(git_attr_cache__is_cached(
71 g_repo, GIT_ATTR_FILE__FROM_FILE, ".git/info/attributes"));
72 cl_assert(git_attr_cache__is_cached(
73 g_repo, GIT_ATTR_FILE__FROM_FILE, ".gitattributes"));
74 cl_assert(git_attr_cache__is_cached(
75 g_repo, GIT_ATTR_FILE__FROM_FILE, "sub/.gitattributes"));
76 }
77
78 void test_attr_repo__get_one_start_deep(void)
79 {
80 int i;
81
82 for (i = (int)ARRAY_SIZE(get_one_test_cases) - 1; i >= 0; --i) {
83 struct attr_expected *scan = &get_one_test_cases[i];
84 const char *value;
85
86 cl_git_pass(git_attr_get(&value, g_repo, 0, scan->path, scan->attr));
87 attr_check_expected(
88 scan->expected, scan->expected_str, scan->attr, value);
89 }
90
91 cl_assert(git_attr_cache__is_cached(
92 g_repo, GIT_ATTR_FILE__FROM_FILE, ".git/info/attributes"));
93 cl_assert(git_attr_cache__is_cached(
94 g_repo, GIT_ATTR_FILE__FROM_FILE, ".gitattributes"));
95 cl_assert(git_attr_cache__is_cached(
96 g_repo, GIT_ATTR_FILE__FROM_FILE, "sub/.gitattributes"));
97 }
98
99 void test_attr_repo__get_many(void)
100 {
101 const char *names[4] = { "repoattr", "rootattr", "missingattr", "subattr" };
102 const char *values[4];
103
104 cl_git_pass(git_attr_get_many(values, g_repo, 0, "root_test1", 4, names));
105
106 cl_assert(GIT_ATTR_TRUE(values[0]));
107 cl_assert(GIT_ATTR_TRUE(values[1]));
108 cl_assert(GIT_ATTR_UNSPECIFIED(values[2]));
109 cl_assert(GIT_ATTR_UNSPECIFIED(values[3]));
110
111 cl_git_pass(git_attr_get_many(values, g_repo, 0, "root_test2", 4, names));
112
113 cl_assert(GIT_ATTR_TRUE(values[0]));
114 cl_assert(GIT_ATTR_FALSE(values[1]));
115 cl_assert(GIT_ATTR_UNSPECIFIED(values[2]));
116 cl_assert(GIT_ATTR_UNSPECIFIED(values[3]));
117
118 cl_git_pass(git_attr_get_many(values, g_repo, 0, "sub/subdir_test1", 4, names));
119
120 cl_assert(GIT_ATTR_TRUE(values[0]));
121 cl_assert(GIT_ATTR_TRUE(values[1]));
122 cl_assert(GIT_ATTR_UNSPECIFIED(values[2]));
123 cl_assert_equal_s("yes", values[3]);
124 }
125
126 void test_attr_repo__get_many_in_place(void)
127 {
128 const char *vals[4] = { "repoattr", "rootattr", "missingattr", "subattr" };
129
130 /* it should be legal to look up values into the same array that has
131 * the attribute names, overwriting each name as the value is found.
132 */
133
134 cl_git_pass(git_attr_get_many(vals, g_repo, 0, "sub/subdir_test1", 4, vals));
135
136 cl_assert(GIT_ATTR_TRUE(vals[0]));
137 cl_assert(GIT_ATTR_TRUE(vals[1]));
138 cl_assert(GIT_ATTR_UNSPECIFIED(vals[2]));
139 cl_assert_equal_s("yes", vals[3]);
140 }
141
142 static int count_attrs(
143 const char *name,
144 const char *value,
145 void *payload)
146 {
147 GIT_UNUSED(name);
148 GIT_UNUSED(value);
149
150 *((int *)payload) += 1;
151
152 return 0;
153 }
154
155 #define CANCEL_VALUE 12345
156
157 static int cancel_iteration(
158 const char *name,
159 const char *value,
160 void *payload)
161 {
162 GIT_UNUSED(name);
163 GIT_UNUSED(value);
164
165 *((int *)payload) -= 1;
166
167 if (*((int *)payload) < 0)
168 return CANCEL_VALUE;
169
170 return 0;
171 }
172
173 void test_attr_repo__foreach(void)
174 {
175 int count;
176
177 count = 0;
178 cl_git_pass(git_attr_foreach(
179 g_repo, 0, "root_test1", &count_attrs, &count));
180 cl_assert(count == 2);
181
182 count = 0;
183 cl_git_pass(git_attr_foreach(g_repo, 0, "sub/subdir_test1",
184 &count_attrs, &count));
185 cl_assert(count == 4); /* repoattr, rootattr, subattr, negattr */
186
187 count = 0;
188 cl_git_pass(git_attr_foreach(g_repo, 0, "sub/subdir_test2.txt",
189 &count_attrs, &count));
190 cl_assert(count == 6); /* repoattr, rootattr, subattr, reposub, negattr, another */
191
192 count = 2;
193 cl_assert_equal_i(
194 CANCEL_VALUE, git_attr_foreach(
195 g_repo, 0, "sub/subdir_test1", &cancel_iteration, &count)
196 );
197 }
198
199 void test_attr_repo__manpage_example(void)
200 {
201 const char *value;
202
203 cl_git_pass(git_attr_get(&value, g_repo, 0, "sub/abc", "foo"));
204 cl_assert(GIT_ATTR_TRUE(value));
205
206 cl_git_pass(git_attr_get(&value, g_repo, 0, "sub/abc", "bar"));
207 cl_assert(GIT_ATTR_UNSPECIFIED(value));
208
209 cl_git_pass(git_attr_get(&value, g_repo, 0, "sub/abc", "baz"));
210 cl_assert(GIT_ATTR_FALSE(value));
211
212 cl_git_pass(git_attr_get(&value, g_repo, 0, "sub/abc", "merge"));
213 cl_assert_equal_s("filfre", value);
214
215 cl_git_pass(git_attr_get(&value, g_repo, 0, "sub/abc", "frotz"));
216 cl_assert(GIT_ATTR_UNSPECIFIED(value));
217 }
218
219 void test_attr_repo__macros(void)
220 {
221 const char *names[5] = { "rootattr", "binary", "diff", "crlf", "frotz" };
222 const char *names2[5] = { "mymacro", "positive", "negative", "rootattr", "another" };
223 const char *names3[3] = { "macro2", "multi2", "multi3" };
224 const char *values[5];
225
226 cl_git_pass(git_attr_get_many(values, g_repo, 0, "binfile", 5, names));
227
228 cl_assert(GIT_ATTR_TRUE(values[0]));
229 cl_assert(GIT_ATTR_TRUE(values[1]));
230 cl_assert(GIT_ATTR_FALSE(values[2]));
231 cl_assert(GIT_ATTR_FALSE(values[3]));
232 cl_assert(GIT_ATTR_UNSPECIFIED(values[4]));
233
234 cl_git_pass(git_attr_get_many(values, g_repo, 0, "macro_test", 5, names2));
235
236 cl_assert(GIT_ATTR_TRUE(values[0]));
237 cl_assert(GIT_ATTR_TRUE(values[1]));
238 cl_assert(GIT_ATTR_FALSE(values[2]));
239 cl_assert(GIT_ATTR_UNSPECIFIED(values[3]));
240 cl_assert_equal_s("77", values[4]);
241
242 cl_git_pass(git_attr_get_many(values, g_repo, 0, "macro_test", 3, names3));
243
244 cl_assert(GIT_ATTR_TRUE(values[0]));
245 cl_assert(GIT_ATTR_FALSE(values[1]));
246 cl_assert_equal_s("answer", values[2]);
247 }
248
249 void test_attr_repo__bad_macros(void)
250 {
251 const char *names[6] = { "rootattr", "positive", "negative",
252 "firstmacro", "secondmacro", "thirdmacro" };
253 const char *values[6];
254
255 cl_git_pass(git_attr_get_many(values, g_repo, 0, "macro_bad", 6, names));
256
257 /* these three just confirm that the "mymacro" rule ran */
258 cl_assert(GIT_ATTR_UNSPECIFIED(values[0]));
259 cl_assert(GIT_ATTR_TRUE(values[1]));
260 cl_assert(GIT_ATTR_FALSE(values[2]));
261
262 /* file contains:
263 * # let's try some malicious macro defs
264 * [attr]firstmacro -thirdmacro -secondmacro
265 * [attr]secondmacro firstmacro -firstmacro
266 * [attr]thirdmacro secondmacro=hahaha -firstmacro
267 * macro_bad firstmacro secondmacro thirdmacro
268 *
269 * firstmacro assignment list ends up with:
270 * -thirdmacro -secondmacro
271 * secondmacro assignment list expands "firstmacro" and ends up with:
272 * -thirdmacro -secondmacro -firstmacro
273 * thirdmacro assignment don't expand so list ends up with:
274 * secondmacro="hahaha"
275 *
276 * macro_bad assignment list ends up with:
277 * -thirdmacro -secondmacro firstmacro &&
278 * -thirdmacro -secondmacro -firstmacro secondmacro &&
279 * secondmacro="hahaha" thirdmacro
280 *
281 * so summary results should be:
282 * -firstmacro secondmacro="hahaha" thirdmacro
283 */
284 cl_assert(GIT_ATTR_FALSE(values[3]));
285 cl_assert_equal_s("hahaha", values[4]);
286 cl_assert(GIT_ATTR_TRUE(values[5]));
287 }
288
289 #define CONTENT "I'm going to be dynamically processed\r\n" \
290 "And my line endings...\r\n" \
291 "...are going to be\n" \
292 "normalized!\r\n"
293
294 #define GITATTR "* text=auto\n" \
295 "*.txt text\n" \
296 "*.data binary\n"
297
298 static void add_to_workdir(const char *filename, const char *content)
299 {
300 git_buf buf = GIT_BUF_INIT;
301
302 cl_git_pass(git_buf_joinpath(&buf, "attr", filename));
303 cl_git_rewritefile(git_buf_cstr(&buf), content);
304
305 git_buf_free(&buf);
306 }
307
308 static void assert_proper_normalization(git_index *index, const char *filename, const char *expected_sha)
309 {
310 size_t index_pos;
311 const git_index_entry *entry;
312
313 add_to_workdir(filename, CONTENT);
314 cl_git_pass(git_index_add_bypath(index, filename));
315
316 cl_assert(!git_index_find(&index_pos, index, filename));
317
318 entry = git_index_get_byindex(index, index_pos);
319 cl_assert_equal_i(0, git_oid_streq(&entry->id, expected_sha));
320 }
321
322 void test_attr_repo__staging_properly_normalizes_line_endings_according_to_gitattributes_directives(void)
323 {
324 git_index* index;
325
326 cl_git_pass(git_repository_index(&index, g_repo));
327
328 add_to_workdir(".gitattributes", GITATTR);
329
330 assert_proper_normalization(index, "text.txt", "22c74203bace3c2e950278c7ab08da0fca9f4e9b");
331 assert_proper_normalization(index, "huh.dunno", "22c74203bace3c2e950278c7ab08da0fca9f4e9b");
332 assert_proper_normalization(index, "binary.data", "66eeff1fcbacf589e6d70aa70edd3fce5be2b37c");
333
334 git_index_free(index);
335 }