View Javadoc
1   /*
2    * Copyright (C) 2019, Google LLC.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 	 * @return true if a commit reachable from a visible tip (but not directly
110 	 *         the tip) is valid
111 	 */
112 	protected abstract boolean isReachableCommitValid();
113 
114 	/** @return true if a commit not reachable from any tip is valid */
115 	protected abstract boolean isUnreachableCommitValid();
116 
117 	/**
118 	 * @return true if the commit directly pointed by an advertised ref is valid
119 	 */
120 	protected abstract boolean isAdvertisedTipValid();
121 
122 	/**
123 	 * @return true if the object directly pointed by a non-advertised ref is
124 	 *         valid
125 	 */
126 	protected abstract boolean isUnadvertisedTipCommitValid();
127 
128 	// UploadPack doesn't allow to ask for blobs when there is no
129 	// bitmap. Test both cases separately.
130 	/**
131 	 * @return true if a reachable blob is valid (and the repo has bitmaps)
132 	 */
133 	protected abstract boolean isReachableBlobValid_withBitmaps();
134 
135 	/**
136 	 * @return true if a reachable blob is valid (and the repo does NOT have
137 	 *         bitmaps)
138 	 */
139 	protected abstract boolean isReachableBlobValid_withoutBitmaps();
140 
141 	/**
142 	 * @return true if a blob unreachable from any tip is valid
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 }