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 package org.eclipse.jgit.lfs.lib;
45
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertFalse;
48 import static org.junit.Assert.assertNotNull;
49 import static org.junit.Assert.assertNull;
50 import static org.junit.Assert.assertTrue;
51
52 import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
53 import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
54 import org.eclipse.jgit.junit.TestRepository;
55 import org.eclipse.jgit.revwalk.ObjectWalk;
56 import org.eclipse.jgit.revwalk.RevCommit;
57 import org.eclipse.jgit.revwalk.RevTree;
58 import org.eclipse.jgit.treewalk.TreeWalk;
59 import org.junit.Test;
60
61 public class LfsPointerFilterTest {
62
63 private static final int SIZE = 12345;
64
65 private static final String OID = "4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393";
66
67 private static final String[] NOT_VALID_LFS_FILES = { "",
68
69 "package org.eclipse.jgit;",
70
71 "version https://hawser.github.com/spec/v1\n",
72
73 "version https://hawser.github.com/spec/v1\n"
74 + "oid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\n",
75
76 "version https://hawser.github.com/spec/v1\n" + "size 12345\n",
77
78 "version https://hawser.github.com/spec/v1\n" + "size 12345\n"
79 + "oid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\n" };
80
81 private static final String[] LFS_VERSION_DOMAINS = {
82 "hawser", "git-lfs"
83 };
84
85 private static final String[] VALID_LFS_FILES = {
86
87 "version https://%s.github.com/spec/v1\n"
88 + "oid sha256:" + OID + "\n"
89 + "size " + SIZE + "\n",
90
91 "version https://%s.github.com/spec/v1\n"
92 + "custom key with value\n"
93 + "oid sha256:" + OID + "\n"
94 + "size " + SIZE + "\n",
95
96 "version https://%s.github.com/spec/v1\n"
97 + "oid sha256:" + OID + "\n"
98 + "r.key key with .\n"
99 + "size " + SIZE + "\n",
100
101 "version https://%s.github.com/spec/v1\n"
102 + "oid sha256:" + OID + "\n"
103 + "size " + SIZE + "\n"
104 + "valid-name another valid key\n" };
105
106 @Test
107 public void testRegularFilesInRepositoryRoot() throws Exception {
108 for (String file : NOT_VALID_LFS_FILES) {
109 assertLfs("file.bin", file).withRecursive(false).shouldBe(false);
110 }
111 }
112
113 @Test
114 public void testNestedRegularFiles() throws Exception {
115 for (String file : NOT_VALID_LFS_FILES) {
116 assertLfs("a/file.bin", file).withRecursive(true).shouldBe(false);
117 }
118 }
119
120 @Test
121 public void testValidPointersInRepositoryRoot() throws Exception {
122 for (String domain : LFS_VERSION_DOMAINS) {
123 for (String file : VALID_LFS_FILES) {
124 assertLfs("file.bin", String.format(file, domain))
125 .withRecursive(true).shouldBe(true)
126 .check();
127 }
128 }
129 }
130
131 @Test
132 public void testValidNestedPointers() throws Exception {
133 for (String domain : LFS_VERSION_DOMAINS) {
134 for (String file : VALID_LFS_FILES) {
135 assertLfs("a/file.bin", String.format(file, domain))
136 .withRecursive(true).shouldBe(true).check();
137 }
138 }
139 }
140
141 @Test
142 public void testValidNestedPointersWithoutRecurrence() throws Exception {
143 for (String domain : LFS_VERSION_DOMAINS) {
144 for (String file : VALID_LFS_FILES) {
145 assertLfs("file.bin", String.format(file, domain))
146 .withRecursive(false).shouldBe(true).check();
147 assertLfs("a/file.bin", String.format(file, domain))
148 .withRecursive(false).shouldBe(false).check();
149 }
150 }
151 }
152
153 private static LfsTreeWalk assertLfs(String path, String content) {
154 return new LfsTreeWalk(path, content);
155 }
156
157 private static class LfsTreeWalk {
158 private final String path;
159
160 private final String content;
161
162 private boolean state;
163
164 private boolean recursive;
165
166 private TestRepository<InMemoryRepository> tr;
167
168 LfsTreeWalk(String path, String content) {
169 this.path = path;
170 this.content = content;
171 }
172
173 LfsTreeWalk withRecursive(boolean shouldBeRecursive) {
174 this.recursive = shouldBeRecursive;
175 return this;
176 }
177
178 LfsTreeWalk shouldBe(boolean shouldBeValid) {
179 this.state = shouldBeValid;
180 return this;
181 }
182
183 void check() throws Exception {
184 tr = new TestRepository<>(new InMemoryRepository(
185 new DfsRepositoryDescription("test")));
186 RevCommit commit = tr.branch("master").commit().add(path, content)
187 .message("initial commit").create();
188 RevTree tree = parseCommit(commit);
189 LfsPointerFilter filter = new LfsPointerFilter();
190 try (TreeWalk treeWalk = new TreeWalk(tr.getRepository())) {
191 treeWalk.addTree(tree);
192 treeWalk.setRecursive(recursive);
193 treeWalk.setFilter(filter);
194
195 if (state) {
196 assertTrue(treeWalk.next());
197 assertEquals(path, treeWalk.getPathString());
198 assertNotNull(filter.getPointer());
199 assertEquals(SIZE, filter.getPointer().getSize());
200 assertEquals(OID, filter.getPointer().getOid().name());
201 } else {
202 assertFalse(treeWalk.next());
203 assertNull(filter.getPointer());
204 }
205 }
206 }
207
208 private RevTree parseCommit(RevCommit commit) throws Exception {
209 try (ObjectWalk ow = new ObjectWalk(tr.getRepository())) {
210 return ow.parseCommit(commit).getTree();
211 }
212 }
213 }
214 }