Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4be2aa5

Browse files
author
Edward Thomson
committed
win32: tests around handling forbidden paths
Introduce a repository that contains some paths that were illegal on PC-DOS circa 1981 (like `aux`, `con`, `com1`) and that in a bizarre fit of retrocomputing, remain illegal on some "modern" computers, despite being "new technology". Introduce some aspirational tests that suggest that we should be able to cope with trees and indexes that contain paths that would be illegal on the filesystem, so that we can at least diff them. Further ensure that checkout will not write a repository with forbidden paths.
1 parent a066b4c commit 4be2aa5

13 files changed

+206
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = true
4+
bare = false
5+
logallrefupdates = true
6+
ignorecase = true
7+
precomposeunicode = true
577 Bytes
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# git ls-files --others --exclude-from=.git/info/exclude
2+
# Lines that start with '#' are comments.
3+
# For a project mostly in C, the following would be a good set of
4+
# exclude patterns (uncomment them if you want to use them):
5+
# *.[oa]
6+
# *~

tests/resources/win32-forbidden/.gitted/objects/34/96991d72d500af36edef68bbfcccd1661d88db

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
x���
2+
!�[�w����hӢ}/�8�B*8N�~�@gu��՜S� ͡7�a�
3+
�f�2��"se.�%��Q ��ؽ��຾m[�k�j���m�G�q_N��3LBJŔFG:B���� �����VRO� ͿҖj!��=�
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
x�A
2+
� �֝�/k�.� ]`����h޾�"=� ˕�e*� ��U��+��M�%���O4�c�˱���

tests/resources/win32-forbidden/.gitted/objects/ea/c7621a652e5261ef1c1d3e7ae31b0d84fcbaba

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
x��M
2+
B!F��;BoW}BD��ۀ��
3+
>_m?�Bg�q���VJ �=F�����dJT�qD�d�B��N�'��6R�p��S+k�p�����G����rAR*Tz!�� �vVG���n5�l_��;��U�>H
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3496991d72d500af36edef68bbfcccd1661d88db

tests/win32/forbidden.c

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
#include "clar_libgit2.h"
2+
3+
#include "repository.h"
4+
#include "buffer.h"
5+
#include "submodule.h"
6+
7+
static const char *repo_name = "win32-forbidden";
8+
static git_repository *repo;
9+
10+
void test_win32_forbidden__initialize(void)
11+
{
12+
repo = cl_git_sandbox_init(repo_name);
13+
}
14+
15+
void test_win32_forbidden__cleanup(void)
16+
{
17+
cl_git_sandbox_cleanup();
18+
}
19+
20+
void test_win32_forbidden__can_open_index(void)
21+
{
22+
git_index *index;
23+
cl_git_pass(git_repository_index(&index, repo));
24+
cl_assert_equal_i(7, git_index_entrycount(index));
25+
26+
/* ensure we can even write the unmodified index */
27+
cl_git_pass(git_index_write(index));
28+
29+
git_index_free(index);
30+
}
31+
32+
void test_win32_forbidden__can_add_forbidden_filename_with_entry(void)
33+
{
34+
git_index *index;
35+
git_index_entry entry = {{0}};
36+
37+
cl_git_pass(git_repository_index(&index, repo));
38+
39+
entry.path = "aux";
40+
entry.mode = GIT_FILEMODE_BLOB;
41+
git_oid_fromstr(&entry.id, "da623abd956bb2fd8052c708c7ed43f05d192d37");
42+
43+
cl_git_pass(git_index_add(index, &entry));
44+
45+
git_index_free(index);
46+
}
47+
48+
void test_win32_forbidden__cannot_add_dot_git_even_with_entry(void)
49+
{
50+
git_index *index;
51+
git_index_entry entry = {{0}};
52+
53+
cl_git_pass(git_repository_index(&index, repo));
54+
55+
entry.path = "foo/.git";
56+
entry.mode = GIT_FILEMODE_BLOB;
57+
git_oid_fromstr(&entry.id, "da623abd956bb2fd8052c708c7ed43f05d192d37");
58+
59+
cl_git_fail(git_index_add(index, &entry));
60+
61+
git_index_free(index);
62+
}
63+
64+
void test_win32_forbidden__cannot_add_forbidden_filename_from_filesystem(void)
65+
{
66+
git_index *index;
67+
68+
/* since our function calls are very low-level, we can create `aux.`,
69+
* but we should not be able to add it to the index
70+
*/
71+
cl_git_pass(git_repository_index(&index, repo));
72+
cl_git_write2file("win32-forbidden/aux.", "foo\n", 4, O_RDWR | O_CREAT, 0666);
73+
74+
#ifdef GIT_WIN32
75+
cl_git_fail(git_index_add_bypath(index, "aux."));
76+
#else
77+
cl_git_pass(git_index_add_bypath(index, "aux."));
78+
#endif
79+
80+
cl_must_pass(p_unlink("win32-forbidden/aux."));
81+
git_index_free(index);
82+
}
83+
84+
static int dummy_submodule_cb(
85+
git_submodule *sm, const char *name, void *payload)
86+
{
87+
GIT_UNUSED(sm);
88+
GIT_UNUSED(name);
89+
GIT_UNUSED(payload);
90+
return 0;
91+
}
92+
93+
void test_win32_forbidden__can_diff_tree_to_index(void)
94+
{
95+
git_diff *diff;
96+
git_tree *tree;
97+
98+
cl_git_pass(git_repository_head_tree(&tree, repo));
99+
cl_git_pass(git_diff_tree_to_index(&diff, repo, tree, NULL, NULL));
100+
cl_assert_equal_i(0, git_diff_num_deltas(diff));
101+
git_diff_free(diff);
102+
git_tree_free(tree);
103+
}
104+
105+
void test_win32_forbidden__can_diff_tree_to_tree(void)
106+
{
107+
git_diff *diff;
108+
git_tree *tree;
109+
110+
cl_git_pass(git_repository_head_tree(&tree, repo));
111+
cl_git_pass(git_diff_tree_to_tree(&diff, repo, tree, tree, NULL));
112+
cl_assert_equal_i(0, git_diff_num_deltas(diff));
113+
git_diff_free(diff);
114+
git_tree_free(tree);
115+
}
116+
117+
void test_win32_forbidden__can_diff_index_to_workdir(void)
118+
{
119+
git_index *index;
120+
git_diff *diff;
121+
const git_diff_delta *delta;
122+
git_tree *tree;
123+
size_t i;
124+
125+
cl_git_pass(git_repository_index(&index, repo));
126+
cl_git_pass(git_repository_head_tree(&tree, repo));
127+
cl_git_pass(git_diff_index_to_workdir(&diff, repo, index, NULL));
128+
129+
for (i = 0; i < git_diff_num_deltas(diff); i++) {
130+
delta = git_diff_get_delta(diff, i);
131+
cl_assert_equal_i(GIT_DELTA_DELETED, delta->status);
132+
}
133+
134+
git_diff_free(diff);
135+
git_tree_free(tree);
136+
git_index_free(index);
137+
}
138+
139+
void test_win32_forbidden__checking_out_forbidden_index_fails(void)
140+
{
141+
#ifdef GIT_WIN32
142+
git_index *index;
143+
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
144+
git_diff *diff;
145+
const git_diff_delta *delta;
146+
git_tree *tree;
147+
size_t num_deltas, i;
148+
149+
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
150+
151+
cl_git_pass(git_repository_index(&index, repo));
152+
cl_git_fail(git_checkout_index(repo, index, &opts));
153+
154+
cl_git_pass(git_repository_head_tree(&tree, repo));
155+
cl_git_pass(git_diff_index_to_workdir(&diff, repo, index, NULL));
156+
157+
num_deltas = git_diff_num_deltas(diff);
158+
159+
cl_assert(num_deltas > 0);
160+
161+
for (i = 0; i < num_deltas; i++) {
162+
delta = git_diff_get_delta(diff, i);
163+
cl_assert_equal_i(GIT_DELTA_DELETED, delta->status);
164+
}
165+
166+
git_diff_free(diff);
167+
git_tree_free(tree);
168+
git_index_free(index);
169+
#endif
170+
}
171+
172+
void test_win32_forbidden__can_query_submodules(void)
173+
{
174+
cl_git_pass(git_submodule_foreach(repo, dummy_submodule_cb, NULL));
175+
}
176+
177+
void test_win32_forbidden__can_blame_file(void)
178+
{
179+
git_blame *blame;
180+
181+
cl_git_pass(git_blame_file(&blame, repo, "aux", NULL));
182+
git_blame_free(blame);
183+
}

0 commit comments

Comments
 (0)