summaryrefslogtreecommitdiff
path: root/tests/checkout/nasty.c
blob: 952a6a1127afbbb676bc55b6fb31595ea83bc8d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include "clar_libgit2.h"
#include "checkout_helpers.h"

#include "git2/checkout.h"
#include "repository.h"
#include "buffer.h"
#include "fileops.h"

static const char *repo_name = "nasty";
static git_repository *repo;
static git_checkout_options checkout_opts;

void test_checkout_nasty__initialize(void)
{
	repo = cl_git_sandbox_init(repo_name);

	GIT_INIT_STRUCTURE(&checkout_opts, GIT_CHECKOUT_OPTIONS_VERSION);
	checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
}

void test_checkout_nasty__cleanup(void)
{
	cl_git_sandbox_cleanup();
}

static void test_checkout_passes(const char *refname, const char *filename)
{
	git_oid commit_id;
	git_commit *commit;
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
	git_buf path = GIT_BUF_INIT;

	cl_git_pass(git_buf_joinpath(&path, repo_name, filename));

	cl_git_pass(git_reference_name_to_id(&commit_id, repo, refname));
	cl_git_pass(git_commit_lookup(&commit, repo, &commit_id));

	opts.checkout_strategy = GIT_CHECKOUT_FORCE |
		GIT_CHECKOUT_DONT_UPDATE_INDEX;

	cl_git_pass(git_checkout_tree(repo, (const git_object *)commit, &opts));
	cl_assert(!git_path_exists(path.ptr));

	git_commit_free(commit);
	git_buf_free(&path);
}

static void test_checkout_fails(const char *refname, const char *filename)
{
	git_oid commit_id;
	git_commit *commit;
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
	git_buf path = GIT_BUF_INIT;

	cl_git_pass(git_buf_joinpath(&path, repo_name, filename));

	cl_git_pass(git_reference_name_to_id(&commit_id, repo, refname));
	cl_git_pass(git_commit_lookup(&commit, repo, &commit_id));

	opts.checkout_strategy = GIT_CHECKOUT_FORCE;

	cl_git_fail(git_checkout_tree(repo, (const git_object *)commit, &opts));
	cl_assert(!git_path_exists(path.ptr));

	git_commit_free(commit);
	git_buf_free(&path);
}

/* A tree that contains ".git" as a tree, with a blob inside
 * (".git/foobar").
 */
void test_checkout_nasty__dotgit_tree(void)
{
	test_checkout_fails("refs/heads/dotgit_tree", ".git/foobar");
}

/* A tree that contains ".GIT" as a tree, with a blob inside
 * (".GIT/foobar").
 */
void test_checkout_nasty__dotcapitalgit_tree(void)
{
	test_checkout_fails("refs/heads/dotcapitalgit_tree", ".GIT/foobar");
}

/* A tree that contains a tree ".", with a blob inside ("./foobar").
 */
void test_checkout_nasty__dot_tree(void)
{
	test_checkout_fails("refs/heads/dot_tree", "foobar");
}

/* A tree that contains a tree ".", with a tree ".git", with a blob
 * inside ("./.git/foobar").
 */
void test_checkout_nasty__dot_dotgit_tree(void)
{
	test_checkout_fails("refs/heads/dot_dotgit_tree", ".git/foobar");
}

/* A tree that contains a tree, with a tree "..", with a tree ".git", with a
 * blob inside ("foo/../.git/foobar").
 */
void test_checkout_nasty__dotdot_dotgit_tree(void)
{
	test_checkout_fails("refs/heads/dotdot_dotgit_tree", ".git/foobar");
}

/* A tree that contains a tree, with a tree "..", with a blob inside
 * ("foo/../foobar").
 */
void test_checkout_nasty__dotdot_tree(void)
{
	test_checkout_fails("refs/heads/dotdot_tree", "foobar");
}

/* A tree that contains a blob with the rogue name ".git/foobar" */
void test_checkout_nasty__dotgit_path(void)
{
	test_checkout_fails("refs/heads/dotgit_path", ".git/foobar");
}

/* A tree that contains a blob with the rogue name ".GIT/foobar" */
void test_checkout_nasty__dotcapitalgit_path(void)
{
	test_checkout_fails("refs/heads/dotcapitalgit_path", ".GIT/foobar");
}

/* A tree that contains a blob with the rogue name "./.git/foobar" */
void test_checkout_nasty__dot_dotgit_path(void)
{
	test_checkout_fails("refs/heads/dot_dotgit_path", ".git/foobar");
}

/* A tree that contains a blob with the rogue name "./.GIT/foobar" */
void test_checkout_nasty__dot_dotcapitalgit_path(void)
{
	test_checkout_fails("refs/heads/dot_dotcapitalgit_path", ".GIT/foobar");
}

/* A tree that contains a blob with the rogue name "foo/../.git/foobar" */
void test_checkout_nasty__dotdot_dotgit_path(void)
{
	test_checkout_fails("refs/heads/dotdot_dotgit_path", ".git/foobar");
}

/* A tree that contains a blob with the rogue name "foo/../.GIT/foobar" */
void test_checkout_nasty__dotdot_dotcapitalgit_path(void)
{
	test_checkout_fails("refs/heads/dotdot_dotcapitalgit_path", ".GIT/foobar");
}

/* A tree that contains a blob with the rogue name "foo/." */
void test_checkout_nasty__dot_path(void)
{
	test_checkout_fails("refs/heads/dot_path", "./foobar");
}

/* A tree that contains a blob with the rogue name "foo/." */
void test_checkout_nasty__dot_path_two(void)
{
	test_checkout_fails("refs/heads/dot_path_two", "foo/.");
}

/* A tree that contains a blob with the rogue name "foo/../foobar" */
void test_checkout_nasty__dotdot_path(void)
{
	test_checkout_fails("refs/heads/dotdot_path", "foobar");
}

/* A tree that contains an entry with a backslash ".git\foobar"  */
void test_checkout_nasty__dotgit_backslash_path(void)
{
#ifdef GIT_WIN32
	test_checkout_fails("refs/heads/dotgit_backslash_path", ".git/foobar");
#endif
}

/* A tree that contains an entry with a backslash ".GIT\foobar"  */
void test_checkout_nasty__dotcapitalgit_backslash_path(void)
{
#ifdef GIT_WIN32
	test_checkout_fails("refs/heads/dotcapitalgit_backslash_path", ".GIT/foobar");
#endif
}

/* A tree that contains an entry with a backslash ".\.GIT\foobar"  */
void test_checkout_nasty__dot_backslash_dotcapitalgit_path(void)
{
#ifdef GIT_WIN32
	test_checkout_fails("refs/heads/dot_backslash_dotcapitalgit_path", ".GIT/foobar");
#endif
}

/* A tree that contains an entry ".git.", because Win32 APIs will drop the
 * trailing slash.
 */
void test_checkout_nasty__dot_git_dot(void)
{
#ifdef GIT_WIN32
	test_checkout_fails("refs/heads/dot_git_dot", ".git/foobar");
#endif
}

/* A tree that contains an entry "git~1", because that is typically the
 * short name for ".git".
 */
void test_checkout_nasty__git_tilde1(void)
{
#ifdef GIT_WIN32
	test_checkout_fails("refs/heads/git_tilde1", ".git/foobar");
#endif
}

/* A tree that contains an entry "git~2", when we have forced the short
 * name for ".git" into "GIT~2".
 */
void test_checkout_nasty__git_custom_shortname(void)
{
#ifdef GIT_WIN32
	if (!cl_sandbox_supports_8dot3())
		clar__skip();

	cl_must_pass(p_rename("nasty/.git", "nasty/_temp"));
	cl_git_write2file("nasty/git~1", "", 0, O_RDWR|O_CREAT, 0666);
	cl_must_pass(p_rename("nasty/_temp", "nasty/.git"));
	test_checkout_fails("refs/heads/git_tilde2", ".git/foobar");
#endif
}

/* A tree that contains an entry "git~3", which should be allowed, since
 * it is not the typical short name ("GIT~1") or the actual short name
 * ("GIT~2") for ".git".
 */
void test_checkout_nasty__only_looks_like_a_git_shortname(void)
{
#ifdef GIT_WIN32
	git_oid commit_id;
	git_commit *commit;
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;

	cl_must_pass(p_rename("nasty/.git", "nasty/_temp"));
	cl_git_write2file("nasty/git~1", "", 0, O_RDWR|O_CREAT, 0666);
	cl_must_pass(p_rename("nasty/_temp", "nasty/.git"));

	cl_git_pass(git_reference_name_to_id(&commit_id, repo, "refs/heads/git_tilde3"));
	cl_git_pass(git_commit_lookup(&commit, repo, &commit_id));

	opts.checkout_strategy = GIT_CHECKOUT_FORCE;

	cl_git_pass(git_checkout_tree(repo, (const git_object *)commit, &opts));
	cl_assert(git_path_exists("nasty/git~3/foobar"));

	git_commit_free(commit);
#endif
}

/* A tree that contains an entry "git:", because Win32 APIs will reject
 * that as looking too similar to a drive letter.
 */
void test_checkout_nasty__dot_git_colon(void)
{
#ifdef GIT_WIN32
	test_checkout_fails("refs/heads/dot_git_colon", ".git/foobar");
#endif
}

/* A tree that contains an entry "git:foo", because Win32 APIs will turn
 * that into ".git".
 */
void test_checkout_nasty__dot_git_colon_stuff(void)
{
#ifdef GIT_WIN32
	test_checkout_fails("refs/heads/dot_git_colon_stuff", ".git/foobar");
#endif
}

/* Trees that contains entries with a tree ".git" that contain
 * byte sequences:
 * { 0xe2, 0x80, 0x8c }
 * { 0xe2, 0x80, 0x8d }
 * { 0xe2, 0x80, 0x8e }
 * { 0xe2, 0x80, 0x8f }
 * { 0xe2, 0x80, 0xaa }
 * { 0xe2, 0x80, 0xab }
 * { 0xe2, 0x80, 0xac }
 * { 0xe2, 0x80, 0xad }
 * { 0xe2, 0x81, 0xae }
 * { 0xe2, 0x81, 0xaa }
 * { 0xe2, 0x81, 0xab }
 * { 0xe2, 0x81, 0xac }
 * { 0xe2, 0x81, 0xad }
 * { 0xe2, 0x81, 0xae }
 * { 0xe2, 0x81, 0xaf }
 * { 0xef, 0xbb, 0xbf }
 * Because these map to characters that HFS filesystems "ignore".  Thus
 * ".git<U+200C>" will map to ".git".
 */
void test_checkout_nasty__dot_git_hfs_ignorable(void)
{
#ifdef __APPLE__
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_1", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_2", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_3", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_4", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_5", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_6", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_7", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_8", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_9", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_10", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_11", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_12", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_13", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_14", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_15", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_16", ".git/foobar");
#endif
}

void test_checkout_nasty__honors_core_protecthfs(void)
{
	cl_repo_set_bool(repo, "core.protectHFS", true);

	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_1", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_2", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_3", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_4", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_5", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_6", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_7", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_8", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_9", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_10", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_11", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_12", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_13", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_14", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_15", ".git/foobar");
	test_checkout_fails("refs/heads/dotgit_hfs_ignorable_16", ".git/foobar");
}

void test_checkout_nasty__honors_core_protectntfs(void)
{
	cl_repo_set_bool(repo, "core.protectNTFS", true);

	test_checkout_fails("refs/heads/dotgit_backslash_path", ".git/foobar");
	test_checkout_fails("refs/heads/dotcapitalgit_backslash_path", ".GIT/foobar");
	test_checkout_fails("refs/heads/dot_git_dot", ".git/foobar");
	test_checkout_fails("refs/heads/git_tilde1", ".git/foobar");
}

void test_checkout_nasty__symlink1(void)
{
	test_checkout_passes("refs/heads/symlink1", ".git/foobar");
}

void test_checkout_nasty__symlink2(void)
{
	test_checkout_passes("refs/heads/symlink2", ".git/foobar");
}

void test_checkout_nasty__symlink3(void)
{
	test_checkout_passes("refs/heads/symlink3", ".git/foobar");
}