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 package org.eclipse.jgit.transport;
44
45 import java.io.IOException;
46 import java.util.Arrays;
47 import java.util.HashMap;
48 import java.util.Map;
49
50 import org.eclipse.jgit.errors.PackProtocolException;
51 import org.eclipse.jgit.errors.TransportException;
52 import org.eclipse.jgit.internal.storage.dfs.DfsGarbageCollector;
53 import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
54 import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
55 import org.eclipse.jgit.junit.TestRepository;
56 import org.eclipse.jgit.lib.Ref;
57 import org.eclipse.jgit.lib.Repository;
58 import org.eclipse.jgit.revwalk.RevBlob;
59 import org.eclipse.jgit.revwalk.RevCommit;
60 import org.eclipse.jgit.transport.UploadPack.RequestValidator;
61 import org.hamcrest.Matchers;
62 import org.junit.Before;
63 import org.junit.Rule;
64 import org.junit.Test;
65 import org.junit.rules.ExpectedException;
66
67 public abstract class RequestValidatorTestCase {
68
69 @Rule
70 public ExpectedException thrown = ExpectedException.none();
71
72 private RevCommit reachableCommit;
73
74 private RevCommit tipAdvertisedCommit;
75
76 private RevCommit tipUnadvertisedCommit;
77
78 private RevCommit unreachableCommit;
79
80 private RevBlob reachableBlob;
81
82 private RevBlob unreachableBlob;
83
84 private InMemoryRepository repo;
85
86 protected abstract RequestValidator createValidator();
87
88 @Before
89 public void setUp() throws Exception {
90 repo = new InMemoryRepository(new DfsRepositoryDescription());
91 try (TestRepository<InMemoryRepository> git = new TestRepository<>(
92 repo)) {
93 reachableBlob = git.blob("foo");
94 reachableCommit = git
95 .commit(git.tree(git.file("foo", reachableBlob)));
96 tipAdvertisedCommit = git.commit(reachableCommit);
97 git.update("advertised", tipAdvertisedCommit);
98
99 tipUnadvertisedCommit = git.commit(reachableCommit);
100 git.update("unadvertised", tipUnadvertisedCommit);
101
102 unreachableBlob = git.blob("unreachableFoo");
103 unreachableCommit = git
104 .commit(git.tree(git.file("foo", unreachableBlob)));
105 }
106 }
107
108
109
110
111
112 protected abstract boolean isReachableCommitValid();
113
114
115 protected abstract boolean isUnreachableCommitValid();
116
117
118
119
120 protected abstract boolean isAdvertisedTipValid();
121
122
123
124
125
126 protected abstract boolean isUnadvertisedTipCommitValid();
127
128
129
130
131
132
133 protected abstract boolean isReachableBlobValid_withBitmaps();
134
135
136
137
138
139 protected abstract boolean isReachableBlobValid_withoutBitmaps();
140
141
142
143
144 protected abstract boolean isUnreachableBlobValid();
145
146 @Test
147 public void validateReachableCommitWithBitmaps()
148 throws PackProtocolException, IOException {
149 if (!isReachableCommitValid()) {
150 thrown.expect(TransportException.class);
151 thrown.expectMessage(Matchers
152 .containsString(
153 "want " + reachableCommit.name() + " not valid"));
154
155 }
156 createValidator().checkWants(getUploadPack(getRepoWithBitmaps()),
157 Arrays.asList(reachableCommit));
158 }
159
160 @Test
161 public void validateReachableCommitWithoutBitmaps()
162 throws PackProtocolException, IOException {
163 if (!isReachableCommitValid()) {
164 thrown.expect(TransportException.class);
165 thrown.expectMessage(Matchers.containsString(
166 "want " + reachableCommit.name() + " not valid"));
167
168 }
169 createValidator().checkWants(getUploadPack(getRepoWithoutBitmaps()),
170 Arrays.asList(reachableCommit));
171 }
172
173 @Test
174 public void validateAdvertisedTipWithBitmaps()
175 throws PackProtocolException, IOException {
176 if (!isAdvertisedTipValid()) {
177 thrown.expect(TransportException.class);
178 thrown.expectMessage(Matchers.containsString(
179 "want " + tipAdvertisedCommit.name() + " not valid"));
180
181 }
182 createValidator().checkWants(getUploadPack(getRepoWithBitmaps()),
183 Arrays.asList(tipAdvertisedCommit));
184 }
185
186 @Test
187 public void validateAdvertisedTipWithoutBitmaps()
188 throws PackProtocolException, IOException {
189 if (!isAdvertisedTipValid()) {
190 thrown.expect(TransportException.class);
191 thrown.expectMessage(Matchers.containsString(
192 "want " + tipAdvertisedCommit.name() + " not valid"));
193
194 }
195 createValidator().checkWants(getUploadPack(getRepoWithoutBitmaps()),
196 Arrays.asList(tipAdvertisedCommit));
197 }
198
199 @Test
200 public void validateUnadvertisedTipWithBitmaps()
201 throws PackProtocolException, IOException {
202 if (!isUnadvertisedTipCommitValid()) {
203 thrown.expect(TransportException.class);
204 thrown.expectMessage(Matchers.containsString(
205 "want " + tipUnadvertisedCommit.name() + " not valid"));
206
207 }
208 createValidator().checkWants(getUploadPack(getRepoWithBitmaps()),
209 Arrays.asList(tipUnadvertisedCommit));
210 }
211
212 @Test
213 public void validateUnadvertisedTipWithoutBitmaps()
214 throws PackProtocolException, IOException {
215 if (!isUnadvertisedTipCommitValid()) {
216 thrown.expect(TransportException.class);
217 thrown.expectMessage(Matchers.containsString(
218 "want " + tipUnadvertisedCommit.name() + " not valid"));
219
220 }
221 createValidator().checkWants(getUploadPack(getRepoWithoutBitmaps()),
222 Arrays.asList(tipUnadvertisedCommit));
223 }
224
225 @Test
226 public void validateUnreachableCommitWithBitmaps()
227 throws PackProtocolException, IOException {
228 if (!isUnreachableCommitValid()) {
229 thrown.expect(TransportException.class);
230 thrown.expectMessage(Matchers.containsString(
231 "want " + unreachableCommit.name() + " not valid"));
232
233 }
234 createValidator().checkWants(getUploadPack(getRepoWithBitmaps()),
235 Arrays.asList(unreachableCommit));
236 }
237
238 @Test
239 public void validateUnreachableCommitWithoutBitmaps()
240 throws PackProtocolException, IOException {
241 if (!isUnreachableCommitValid()) {
242 thrown.expect(TransportException.class);
243 thrown.expectMessage(Matchers.containsString(
244 "want " + unreachableCommit.name() + " not valid"));
245
246 }
247 createValidator().checkWants(getUploadPack(getRepoWithoutBitmaps()),
248 Arrays.asList(unreachableCommit));
249 }
250
251 @Test
252 public void validateReachableBlobWithBitmaps()
253 throws PackProtocolException, IOException {
254 if (!isReachableBlobValid_withBitmaps()) {
255 thrown.expect(TransportException.class);
256 thrown.expectMessage(Matchers.containsString(
257 "want " + reachableBlob.name() + " not valid"));
258 }
259 createValidator().checkWants(getUploadPack(getRepoWithBitmaps()),
260 Arrays.asList(reachableBlob));
261 }
262
263 @Test
264 public void validateReachableBlobWithoutBitmaps()
265 throws PackProtocolException, IOException {
266 if (!isReachableBlobValid_withoutBitmaps()) {
267 thrown.expect(TransportException.class);
268 thrown.expectMessage(Matchers.containsString(
269 "want " + reachableBlob.name() + " not valid"));
270 }
271 createValidator().checkWants(getUploadPack(getRepoWithoutBitmaps()),
272 Arrays.asList(reachableBlob));
273 }
274
275 @Test
276 public void validateUnreachableBlobWithBitmaps()
277 throws PackProtocolException, IOException {
278 if (!isUnreachableBlobValid()) {
279 thrown.expect(TransportException.class);
280 thrown.expectMessage(Matchers.containsString(
281 "want " + unreachableBlob.name() + " not valid"));
282 }
283 createValidator().checkWants(getUploadPack(getRepoWithBitmaps()),
284 Arrays.asList(unreachableBlob));
285 }
286
287 @Test
288 public void validateUnreachableBlobWithoutBitmaps()
289 throws PackProtocolException, IOException {
290 if (!isUnreachableBlobValid()) {
291 thrown.expect(TransportException.class);
292 thrown.expectMessage(Matchers.containsString(
293 "want " + unreachableBlob.name() + " not valid"));
294 }
295 createValidator().checkWants(getUploadPack(getRepoWithoutBitmaps()),
296 Arrays.asList(unreachableBlob));
297 }
298
299 private UploadPack getUploadPack(Repository repository) throws IOException {
300 UploadPack uploadPack = new UploadPack(repository);
301
302 Ref advertisedRef = repo.getRefDatabase().findRef("advertised");
303 Map<String, Ref> advertisedRefs = new HashMap<>();
304 advertisedRefs.put(advertisedRef.getName(), advertisedRef);
305
306 uploadPack.setAdvertisedRefs(advertisedRefs);
307 return uploadPack;
308 }
309
310 private Repository getRepoWithBitmaps() throws IOException {
311 new DfsGarbageCollector(repo).pack(null);
312 repo.scanForRepoChanges();
313 return repo;
314 }
315
316 private Repository getRepoWithoutBitmaps() {
317 return repo;
318 }
319 }