You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UploadPackReachabilityTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. import static org.eclipse.jgit.lib.MoreAsserts.assertThrows;
  45. import static org.hamcrest.Matchers.containsString;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertThat;
  48. import static org.junit.Assert.assertTrue;
  49. import java.util.Collections;
  50. import org.eclipse.jgit.errors.TransportException;
  51. import org.eclipse.jgit.internal.storage.dfs.DfsGarbageCollector;
  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.lib.NullProgressMonitor;
  56. import org.eclipse.jgit.lib.Repository;
  57. import org.eclipse.jgit.revwalk.RevBlob;
  58. import org.eclipse.jgit.revwalk.RevCommit;
  59. import org.eclipse.jgit.transport.UploadPack.RequestPolicy;
  60. import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
  61. import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
  62. import org.eclipse.jgit.transport.resolver.UploadPackFactory;
  63. import org.junit.After;
  64. import org.junit.Before;
  65. import org.junit.Rule;
  66. import org.junit.Test;
  67. import org.junit.rules.ExpectedException;
  68. /**
  69. * Test combinations of:
  70. * <ul>
  71. * <li>Fetch a blob or a commit</li>
  72. * <li>Fetched object is reachable or not</li>
  73. * <li>With and without bitmaps</li>
  74. * </ul>
  75. */
  76. public class UploadPackReachabilityTest {
  77. @Rule
  78. public ExpectedException thrown = ExpectedException.none();
  79. private URIish uri;
  80. private TestProtocol<Object> testProtocol;
  81. private Object ctx = new Object();
  82. private InMemoryRepository server;
  83. private InMemoryRepository client;
  84. private TestRepository<InMemoryRepository> remote;
  85. @Before
  86. public void setUp() throws Exception {
  87. server = newRepo("server");
  88. client = newRepo("client");
  89. remote = new TestRepository<>(server);
  90. }
  91. @After
  92. public void tearDown() {
  93. Transport.unregister(testProtocol);
  94. }
  95. @Test
  96. public void testFetchUnreachableBlobWithBitmap() throws Exception {
  97. RevBlob blob = remote.blob("foo");
  98. remote.commit(remote.tree(remote.file("foo", blob)));
  99. generateBitmaps(server);
  100. testProtocol = generateReachableCommitUploadPackProtocol();
  101. uri = testProtocol.register(ctx, server);
  102. assertFalse(client.getObjectDatabase().has(blob.toObjectId()));
  103. try (Transport tn = testProtocol.open(uri, client, "server")) {
  104. TransportException e = assertThrows(TransportException.class,
  105. () -> tn.fetch(NullProgressMonitor.INSTANCE, Collections
  106. .singletonList(new RefSpec(blob.name()))));
  107. assertThat(e.getMessage(),
  108. containsString("want " + blob.name() + " not valid"));
  109. }
  110. }
  111. @Test
  112. public void testFetchReachableBlobWithoutBitmap() throws Exception {
  113. RevBlob blob = remote.blob("foo");
  114. RevCommit commit = remote.commit(remote.tree(remote.file("foo", blob)));
  115. remote.update("master", commit);
  116. testProtocol = generateReachableCommitUploadPackProtocol();
  117. uri = testProtocol.register(ctx, server);
  118. assertFalse(client.getObjectDatabase().has(blob.toObjectId()));
  119. try (Transport tn = testProtocol.open(uri, client, "server")) {
  120. TransportException e = assertThrows(TransportException.class,
  121. () -> tn.fetch(NullProgressMonitor.INSTANCE, Collections
  122. .singletonList(new RefSpec(blob.name()))));
  123. assertThat(e.getMessage(),
  124. containsString(
  125. "want " + blob.name() + " not valid"));
  126. }
  127. }
  128. @Test
  129. public void testFetchReachableBlobWithoutBitmapButFilterAllowed() throws Exception {
  130. InMemoryRepository server2 = newRepo("server2");
  131. try (TestRepository<InMemoryRepository> remote2 = new TestRepository<>(
  132. server2)) {
  133. RevBlob blob = remote2.blob("foo");
  134. RevCommit commit = remote2.commit(remote2.tree(remote2.file("foo", blob)));
  135. remote2.update("master", commit);
  136. server2.getConfig().setBoolean("uploadpack", null, "allowfilter",
  137. true);
  138. testProtocol = new TestProtocol<>((Object req, Repository db) -> {
  139. UploadPack up = new UploadPack(db);
  140. up.setRequestPolicy(RequestPolicy.REACHABLE_COMMIT);
  141. return up;
  142. }, null);
  143. uri = testProtocol.register(ctx, server2);
  144. assertFalse(client.getObjectDatabase().has(blob.toObjectId()));
  145. try (Transport tn = testProtocol.open(uri, client, "server2")) {
  146. tn.fetch(NullProgressMonitor.INSTANCE,
  147. Collections.singletonList(new RefSpec(blob.name())));
  148. assertTrue(client.getObjectDatabase().has(blob.toObjectId()));
  149. }
  150. }
  151. }
  152. @Test
  153. public void testFetchUnreachableBlobWithoutBitmap() throws Exception {
  154. RevBlob blob = remote.blob("foo");
  155. remote.commit(remote.tree(remote.file("foo", blob)));
  156. testProtocol = generateReachableCommitUploadPackProtocol();
  157. uri = testProtocol.register(ctx, server);
  158. assertFalse(client.getObjectDatabase().has(blob.toObjectId()));
  159. try (Transport tn = testProtocol.open(uri, client, "server")) {
  160. TransportException e = assertThrows(TransportException.class, () ->
  161. tn.fetch(NullProgressMonitor.INSTANCE,
  162. Collections.singletonList(new RefSpec(blob.name()))));
  163. assertThat(e.getMessage(),
  164. containsString("want " + blob.name() + " not valid"));
  165. }
  166. }
  167. @Test
  168. public void testFetchReachableBlobWithBitmap() throws Exception {
  169. RevBlob blob = remote.blob("foo");
  170. RevCommit commit = remote.commit(remote.tree(remote.file("foo", blob)));
  171. remote.update("master", commit);
  172. generateBitmaps(server);
  173. testProtocol = generateReachableCommitUploadPackProtocol();
  174. uri = testProtocol.register(ctx, server);
  175. assertFalse(client.getObjectDatabase().has(blob.toObjectId()));
  176. try (Transport tn = testProtocol.open(uri, client, "server")) {
  177. tn.fetch(NullProgressMonitor.INSTANCE,
  178. Collections.singletonList(new RefSpec(blob.name())));
  179. assertTrue(client.getObjectDatabase().has(blob.toObjectId()));
  180. }
  181. }
  182. @Test
  183. public void testFetchReachableCommitWithBitmap() throws Exception {
  184. RevCommit commit = remote
  185. .commit(remote.tree(remote.file("foo", remote.blob("foo"))));
  186. remote.update("master", commit);
  187. generateBitmaps(server);
  188. testProtocol = generateReachableCommitUploadPackProtocol();
  189. uri = testProtocol.register(ctx, server);
  190. assertFalse(client.getObjectDatabase().has(commit.toObjectId()));
  191. try (Transport tn = testProtocol.open(uri, client, "server")) {
  192. tn.fetch(NullProgressMonitor.INSTANCE,
  193. Collections.singletonList(new RefSpec(commit.name())));
  194. assertTrue(client.getObjectDatabase().has(commit.toObjectId()));
  195. }
  196. }
  197. @Test
  198. public void testFetchReachableCommitWithoutBitmap() throws Exception {
  199. RevCommit commit = remote
  200. .commit(remote.tree(remote.file("foo", remote.blob("foo"))));
  201. remote.update("master", commit);
  202. generateBitmaps(server);
  203. testProtocol = generateReachableCommitUploadPackProtocol();
  204. uri = testProtocol.register(ctx, server);
  205. assertFalse(client.getObjectDatabase().has(commit.toObjectId()));
  206. try (Transport tn = testProtocol.open(uri, client, "server")) {
  207. tn.fetch(NullProgressMonitor.INSTANCE,
  208. Collections.singletonList(new RefSpec(commit.name())));
  209. assertTrue(client.getObjectDatabase().has(commit.toObjectId()));
  210. }
  211. }
  212. @Test
  213. public void testFetchUnreachableCommitWithBitmap() throws Exception {
  214. RevCommit commit = remote
  215. .commit(remote.tree(remote.file("foo", remote.blob("foo"))));
  216. generateBitmaps(server);
  217. testProtocol = generateReachableCommitUploadPackProtocol();
  218. uri = testProtocol.register(ctx, server);
  219. assertFalse(client.getObjectDatabase().has(commit.toObjectId()));
  220. try (Transport tn = testProtocol.open(uri, client, "server")) {
  221. TransportException e = assertThrows(TransportException.class,
  222. () -> tn.fetch(NullProgressMonitor.INSTANCE,
  223. Collections.singletonList(new RefSpec(commit.name()))));
  224. assertThat(e.getMessage(),
  225. containsString("want " + commit.name() + " not valid"));
  226. }
  227. }
  228. @Test
  229. public void testFetchUnreachableCommitWithoutBitmap() throws Exception {
  230. RevCommit commit = remote
  231. .commit(remote.tree(remote.file("foo", remote.blob("foo"))));
  232. testProtocol = generateReachableCommitUploadPackProtocol();
  233. uri = testProtocol.register(ctx, server);
  234. assertFalse(client.getObjectDatabase().has(commit.toObjectId()));
  235. try (Transport tn = testProtocol.open(uri, client, "server")) {
  236. TransportException e = assertThrows(TransportException.class,
  237. () -> tn.fetch(NullProgressMonitor.INSTANCE, Collections
  238. .singletonList(new RefSpec(commit.name()))));
  239. assertThat(e.getMessage(),
  240. containsString("want " + commit.name() + " not valid"));
  241. }
  242. }
  243. private static InMemoryRepository newRepo(String name) {
  244. return new InMemoryRepository(new DfsRepositoryDescription(name));
  245. }
  246. private void generateBitmaps(InMemoryRepository repo) throws Exception {
  247. new DfsGarbageCollector(repo).pack(null);
  248. repo.scanForRepoChanges();
  249. }
  250. private static TestProtocol<Object> generateReachableCommitUploadPackProtocol() {
  251. return new TestProtocol<>(new UploadPackFactory<Object>() {
  252. @Override
  253. public UploadPack create(Object req, Repository db)
  254. throws ServiceNotEnabledException,
  255. ServiceNotAuthorizedException {
  256. UploadPack up = new UploadPack(db);
  257. up.setRequestPolicy(RequestPolicy.REACHABLE_COMMIT);
  258. return up;
  259. }
  260. }, null);
  261. }
  262. }