1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.lfs.lib;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertNull;
17 import static org.junit.Assert.assertTrue;
18
19 import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
20 import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
21 import org.eclipse.jgit.junit.TestRepository;
22 import org.eclipse.jgit.revwalk.ObjectWalk;
23 import org.eclipse.jgit.revwalk.RevCommit;
24 import org.eclipse.jgit.revwalk.RevTree;
25 import org.eclipse.jgit.treewalk.TreeWalk;
26 import org.junit.Test;
27
28 public class LfsPointerFilterTest {
29
30 private static final int SIZE = 12345;
31
32 private static final String OID = "4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393";
33
34 private static final String[] NOT_VALID_LFS_FILES = { "",
35
36 "package org.eclipse.jgit;",
37
38 "version https://hawser.github.com/spec/v1\n",
39
40 "version https://hawser.github.com/spec/v1\n"
41 + "oid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\n",
42
43 "version https://hawser.github.com/spec/v1\n" + "size 12345\n",
44
45 "version https://hawser.github.com/spec/v1\n" + "size 12345\n"
46 + "oid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\n" };
47
48 private static final String[] LFS_VERSION_DOMAINS = {
49 "hawser", "git-lfs"
50 };
51
52 private static final String[] VALID_LFS_FILES = {
53
54 "version https://%s.github.com/spec/v1\n"
55 + "oid sha256:" + OID + "\n"
56 + "size " + SIZE + "\n",
57
58 "version https://%s.github.com/spec/v1\n"
59 + "custom key with value\n"
60 + "oid sha256:" + OID + "\n"
61 + "size " + SIZE + "\n",
62
63 "version https://%s.github.com/spec/v1\n"
64 + "oid sha256:" + OID + "\n"
65 + "r.key key with .\n"
66 + "size " + SIZE + "\n",
67
68 "version https://%s.github.com/spec/v1\n"
69 + "oid sha256:" + OID + "\n"
70 + "size " + SIZE + "\n"
71 + "valid-name another valid key\n" };
72
73 @Test
74 public void testRegularFilesInRepositoryRoot() throws Exception {
75 for (String file : NOT_VALID_LFS_FILES) {
76 assertLfs("file.bin", file).withRecursive(false).shouldBe(false);
77 }
78 }
79
80 @Test
81 public void testNestedRegularFiles() throws Exception {
82 for (String file : NOT_VALID_LFS_FILES) {
83 assertLfs("a/file.bin", file).withRecursive(true).shouldBe(false);
84 }
85 }
86
87 @Test
88 public void testValidPointersInRepositoryRoot() throws Exception {
89 for (String domain : LFS_VERSION_DOMAINS) {
90 for (String file : VALID_LFS_FILES) {
91 assertLfs("file.bin", String.format(file, domain))
92 .withRecursive(true).shouldBe(true)
93 .check();
94 }
95 }
96 }
97
98 @Test
99 public void testValidNestedPointers() throws Exception {
100 for (String domain : LFS_VERSION_DOMAINS) {
101 for (String file : VALID_LFS_FILES) {
102 assertLfs("a/file.bin", String.format(file, domain))
103 .withRecursive(true).shouldBe(true).check();
104 }
105 }
106 }
107
108 @Test
109 public void testValidNestedPointersWithoutRecurrence() throws Exception {
110 for (String domain : LFS_VERSION_DOMAINS) {
111 for (String file : VALID_LFS_FILES) {
112 assertLfs("file.bin", String.format(file, domain))
113 .withRecursive(false).shouldBe(true).check();
114 assertLfs("a/file.bin", String.format(file, domain))
115 .withRecursive(false).shouldBe(false).check();
116 }
117 }
118 }
119
120 private static LfsTreeWalk assertLfs(String path, String content) {
121 return new LfsTreeWalk(path, content);
122 }
123
124 private static class LfsTreeWalk {
125 private final String path;
126
127 private final String content;
128
129 private boolean state;
130
131 private boolean recursive;
132
133 private TestRepository<InMemoryRepository> tr;
134
135 LfsTreeWalk(String path, String content) {
136 this.path = path;
137 this.content = content;
138 }
139
140 LfsTreeWalk withRecursive(boolean shouldBeRecursive) {
141 this.recursive = shouldBeRecursive;
142 return this;
143 }
144
145 LfsTreeWalk shouldBe(boolean shouldBeValid) {
146 this.state = shouldBeValid;
147 return this;
148 }
149
150 void check() throws Exception {
151 tr = new TestRepository<>(new InMemoryRepository(
152 new DfsRepositoryDescription("test")));
153 RevCommit commit = tr.branch("master").commit().add(path, content)
154 .message("initial commit").create();
155 RevTree tree = parseCommit(commit);
156 LfsPointerFilter filter = new LfsPointerFilter();
157 try (TreeWalk treeWalk = new TreeWalk(tr.getRepository())) {
158 treeWalk.addTree(tree);
159 treeWalk.setRecursive(recursive);
160 treeWalk.setFilter(filter);
161
162 if (state) {
163 assertTrue(treeWalk.next());
164 assertEquals(path, treeWalk.getPathString());
165 assertNotNull(filter.getPointer());
166 assertEquals(SIZE, filter.getPointer().getSize());
167 assertEquals(OID, filter.getPointer().getOid().name());
168 } else {
169 assertFalse(treeWalk.next());
170 assertNull(filter.getPointer());
171 }
172 }
173 }
174
175 private RevTree parseCommit(RevCommit commit) throws Exception {
176 try (ObjectWalk ow = new ObjectWalk(tr.getRepository())) {
177 return ow.parseCommit(commit).getTree();
178 }
179 }
180 }
181 }