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
|
/*
* Copyright (C) 2019, Google LLC. and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.transport;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import java.util.Collections;
import org.eclipse.jgit.errors.TransportException;
import org.eclipse.jgit.internal.storage.dfs.DfsGarbageCollector;
import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.NullProgressMonitor;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevBlob;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.transport.UploadPack.RequestPolicy;
import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
import org.eclipse.jgit.transport.resolver.UploadPackFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* Test combinations of:
* <ul>
* <li>Fetch a blob or a commit</li>
* <li>Fetched object is reachable or not</li>
* <li>With and without bitmaps</li>
* </ul>
*/
public class UploadPackReachabilityTest {
private URIish uri;
private TestProtocol<Object> testProtocol;
private Object ctx = new Object();
private InMemoryRepository server;
private InMemoryRepository client;
private TestRepository<InMemoryRepository> remote;
@Before
public void setUp() throws Exception {
server = newRepo("server");
client = newRepo("client");
remote = new TestRepository<>(server);
}
@After
public void tearDown() {
Transport.unregister(testProtocol);
}
@Test
public void testFetchUnreachableBlobWithBitmap() throws Exception {
RevBlob blob = remote.blob("foo");
remote.commit(remote.tree(remote.file("foo", blob)));
generateBitmaps(server);
testProtocol = generateReachableCommitUploadPackProtocol();
uri = testProtocol.register(ctx, server);
assertFalse(client.getObjectDatabase().has(blob.toObjectId()));
try (Transport tn = testProtocol.open(uri, client, "server")) {
TransportException e = assertThrows(TransportException.class,
() -> tn.fetch(NullProgressMonitor.INSTANCE, Collections
.singletonList(new RefSpec(blob.name()))));
assertThat(e.getMessage(),
containsString("want " + blob.name() + " not valid"));
}
}
@Test
public void testFetchReachableBlobWithoutBitmap() throws Exception {
RevBlob blob = remote.blob("foo");
RevCommit commit = remote.commit(remote.tree(remote.file("foo", blob)));
remote.update("master", commit);
testProtocol = generateReachableCommitUploadPackProtocol();
uri = testProtocol.register(ctx, server);
assertFalse(client.getObjectDatabase().has(blob.toObjectId()));
try (Transport tn = testProtocol.open(uri, client, "server")) {
TransportException e = assertThrows(TransportException.class,
() -> tn.fetch(NullProgressMonitor.INSTANCE, Collections
.singletonList(new RefSpec(blob.name()))));
assertThat(e.getMessage(),
containsString(
"want " + blob.name() + " not valid"));
}
}
@Test
public void testFetchReachableBlobWithoutBitmapButFilterAllowed() throws Exception {
InMemoryRepository server2 = newRepo("server2");
try (TestRepository<InMemoryRepository> remote2 = new TestRepository<>(
server2)) {
RevBlob blob = remote2.blob("foo");
RevCommit commit = remote2.commit(remote2.tree(remote2.file("foo", blob)));
remote2.update("master", commit);
server2.getConfig().setBoolean("uploadpack", null, "allowfilter",
true);
testProtocol = new TestProtocol<>((Object req, Repository db) -> {
UploadPack up = new UploadPack(db);
up.setRequestPolicy(RequestPolicy.REACHABLE_COMMIT);
return up;
}, null);
uri = testProtocol.register(ctx, server2);
assertFalse(client.getObjectDatabase().has(blob.toObjectId()));
try (Transport tn = testProtocol.open(uri, client, "server2")) {
tn.fetch(NullProgressMonitor.INSTANCE,
Collections.singletonList(new RefSpec(blob.name())));
assertTrue(client.getObjectDatabase().has(blob.toObjectId()));
}
}
}
@Test
public void testFetchUnreachableBlobWithoutBitmap() throws Exception {
RevBlob blob = remote.blob("foo");
remote.commit(remote.tree(remote.file("foo", blob)));
testProtocol = generateReachableCommitUploadPackProtocol();
uri = testProtocol.register(ctx, server);
assertFalse(client.getObjectDatabase().has(blob.toObjectId()));
try (Transport tn = testProtocol.open(uri, client, "server")) {
TransportException e = assertThrows(TransportException.class, () ->
tn.fetch(NullProgressMonitor.INSTANCE,
Collections.singletonList(new RefSpec(blob.name()))));
assertThat(e.getMessage(),
containsString("want " + blob.name() + " not valid"));
}
}
@Test
public void testFetchReachableBlobWithBitmap() throws Exception {
RevBlob blob = remote.blob("foo");
RevCommit commit = remote.commit(remote.tree(remote.file("foo", blob)));
remote.update("master", commit);
generateBitmaps(server);
testProtocol = generateReachableCommitUploadPackProtocol();
uri = testProtocol.register(ctx, server);
assertFalse(client.getObjectDatabase().has(blob.toObjectId()));
try (Transport tn = testProtocol.open(uri, client, "server")) {
tn.fetch(NullProgressMonitor.INSTANCE,
Collections.singletonList(new RefSpec(blob.name())));
assertTrue(client.getObjectDatabase().has(blob.toObjectId()));
}
}
@Test
public void testFetchReachableCommitWithBitmap() throws Exception {
RevCommit commit = remote
.commit(remote.tree(remote.file("foo", remote.blob("foo"))));
remote.update("master", commit);
generateBitmaps(server);
testProtocol = generateReachableCommitUploadPackProtocol();
uri = testProtocol.register(ctx, server);
assertFalse(client.getObjectDatabase().has(commit.toObjectId()));
try (Transport tn = testProtocol.open(uri, client, "server")) {
tn.fetch(NullProgressMonitor.INSTANCE,
Collections.singletonList(new RefSpec(commit.name())));
assertTrue(client.getObjectDatabase().has(commit.toObjectId()));
}
}
@Test
public void testFetchReachableCommitWithoutBitmap() throws Exception {
RevCommit commit = remote
.commit(remote.tree(remote.file("foo", remote.blob("foo"))));
remote.update("master", commit);
generateBitmaps(server);
testProtocol = generateReachableCommitUploadPackProtocol();
uri = testProtocol.register(ctx, server);
assertFalse(client.getObjectDatabase().has(commit.toObjectId()));
try (Transport tn = testProtocol.open(uri, client, "server")) {
tn.fetch(NullProgressMonitor.INSTANCE,
Collections.singletonList(new RefSpec(commit.name())));
assertTrue(client.getObjectDatabase().has(commit.toObjectId()));
}
}
@Test
public void testFetchUnreachableCommitWithBitmap() throws Exception {
RevCommit commit = remote
.commit(remote.tree(remote.file("foo", remote.blob("foo"))));
generateBitmaps(server);
testProtocol = generateReachableCommitUploadPackProtocol();
uri = testProtocol.register(ctx, server);
assertFalse(client.getObjectDatabase().has(commit.toObjectId()));
try (Transport tn = testProtocol.open(uri, client, "server")) {
TransportException e = assertThrows(TransportException.class,
() -> tn.fetch(NullProgressMonitor.INSTANCE,
Collections.singletonList(new RefSpec(commit.name()))));
assertThat(e.getMessage(),
containsString("want " + commit.name() + " not valid"));
}
}
@Test
public void testFetchUnreachableCommitWithoutBitmap() throws Exception {
RevCommit commit = remote
.commit(remote.tree(remote.file("foo", remote.blob("foo"))));
testProtocol = generateReachableCommitUploadPackProtocol();
uri = testProtocol.register(ctx, server);
assertFalse(client.getObjectDatabase().has(commit.toObjectId()));
try (Transport tn = testProtocol.open(uri, client, "server")) {
TransportException e = assertThrows(TransportException.class,
() -> tn.fetch(NullProgressMonitor.INSTANCE, Collections
.singletonList(new RefSpec(commit.name()))));
assertThat(e.getMessage(),
containsString("want " + commit.name() + " not valid"));
}
}
private static InMemoryRepository newRepo(String name) {
return new InMemoryRepository(new DfsRepositoryDescription(name));
}
private void generateBitmaps(InMemoryRepository repo) throws Exception {
new DfsGarbageCollector(repo).pack(null);
repo.scanForRepoChanges();
}
private static TestProtocol<Object> generateReachableCommitUploadPackProtocol() {
return new TestProtocol<>(new UploadPackFactory<Object>() {
@Override
public UploadPack create(Object req, Repository db)
throws ServiceNotEnabledException,
ServiceNotAuthorizedException {
UploadPack up = new UploadPack(db);
up.setRequestPolicy(RequestPolicy.REACHABLE_COMMIT);
return up;
}
}, null);
}
}
|