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.

RequestValidatorTestCase.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright (C) 2019, Google LLC. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.transport;
  11. import static org.assertj.core.api.Assertions.assertThat;
  12. import static org.assertj.core.api.Assertions.catchThrowableOfType;
  13. import java.io.IOException;
  14. import java.util.Arrays;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
  18. import org.eclipse.jgit.errors.TransportException;
  19. import org.eclipse.jgit.internal.storage.dfs.DfsGarbageCollector;
  20. import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
  21. import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
  22. import org.eclipse.jgit.junit.TestRepository;
  23. import org.eclipse.jgit.lib.Ref;
  24. import org.eclipse.jgit.lib.Repository;
  25. import org.eclipse.jgit.revwalk.RevBlob;
  26. import org.eclipse.jgit.revwalk.RevCommit;
  27. import org.eclipse.jgit.transport.UploadPack.RequestValidator;
  28. import org.junit.Before;
  29. import org.junit.Test;
  30. public abstract class RequestValidatorTestCase {
  31. private RevCommit reachableCommit;
  32. private RevCommit tipAdvertisedCommit;
  33. private RevCommit tipUnadvertisedCommit;
  34. private RevCommit unreachableCommit;
  35. private RevBlob reachableBlob;
  36. private RevBlob unreachableBlob;
  37. private InMemoryRepository repo;
  38. protected abstract RequestValidator createValidator();
  39. @Before
  40. public void setUp() throws Exception {
  41. repo = new InMemoryRepository(new DfsRepositoryDescription());
  42. try (TestRepository<InMemoryRepository> git = new TestRepository<>(
  43. repo)) {
  44. reachableBlob = git.blob("foo");
  45. reachableCommit = git
  46. .commit(git.tree(git.file("foo", reachableBlob)));
  47. tipAdvertisedCommit = git.commit(reachableCommit);
  48. git.update("advertised", tipAdvertisedCommit);
  49. tipUnadvertisedCommit = git.commit(reachableCommit);
  50. git.update("unadvertised", tipUnadvertisedCommit);
  51. unreachableBlob = git.blob("unreachableFoo");
  52. unreachableCommit = git
  53. .commit(git.tree(git.file("foo", unreachableBlob)));
  54. }
  55. }
  56. /**
  57. * @return true if a commit reachable from a visible tip (but not directly
  58. * the tip) is valid
  59. */
  60. protected abstract boolean isReachableCommitValid();
  61. /** @return true if a commit not reachable from any tip is valid */
  62. protected abstract boolean isUnreachableCommitValid();
  63. /**
  64. * @return true if the commit directly pointed by an advertised ref is valid
  65. */
  66. protected abstract boolean isAdvertisedTipValid();
  67. /**
  68. * @return true if the object directly pointed by a non-advertised ref is
  69. * valid
  70. */
  71. protected abstract boolean isUnadvertisedTipCommitValid();
  72. // UploadPack doesn't allow to ask for blobs when there is no
  73. // bitmap. Test both cases separately.
  74. /**
  75. * @return true if a reachable blob is valid (and the repo has bitmaps)
  76. */
  77. protected abstract boolean isReachableBlobValid_withBitmaps();
  78. /**
  79. * @return true if a reachable blob is valid (and the repo does NOT have
  80. * bitmaps)
  81. */
  82. protected abstract boolean isReachableBlobValid_withoutBitmaps();
  83. /**
  84. * @return true if a blob unreachable from any tip is valid
  85. */
  86. protected abstract boolean isUnreachableBlobValid();
  87. @Test
  88. public void validateReachableCommitWithBitmaps() throws Throwable {
  89. ThrowingCallable c = () -> createValidator().checkWants(
  90. getUploadPack(getRepoWithBitmaps()),
  91. Arrays.asList(reachableCommit));
  92. if (!isReachableCommitValid()) {
  93. assertTransportException(c,
  94. "want " + reachableCommit.name() + " not valid");
  95. return;
  96. }
  97. c.call();
  98. }
  99. @Test
  100. public void validateReachableCommitWithoutBitmaps() throws Throwable {
  101. ThrowingCallable c = () -> createValidator().checkWants(
  102. getUploadPack(getRepoWithoutBitmaps()),
  103. Arrays.asList(reachableCommit));
  104. if (!isReachableCommitValid()) {
  105. assertTransportException(c,
  106. "want " + reachableCommit.name() + " not valid");
  107. return;
  108. }
  109. c.call();
  110. }
  111. @Test
  112. public void validateAdvertisedTipWithBitmaps() throws Throwable {
  113. ThrowingCallable c = () -> createValidator().checkWants(
  114. getUploadPack(getRepoWithBitmaps()),
  115. Arrays.asList(tipAdvertisedCommit));
  116. if (!isAdvertisedTipValid()) {
  117. assertTransportException(c,
  118. "want " + tipAdvertisedCommit.name() + " not valid");
  119. return;
  120. }
  121. c.call();
  122. }
  123. @Test
  124. public void validateAdvertisedTipWithoutBitmaps() throws Throwable {
  125. ThrowingCallable c = () -> createValidator().checkWants(
  126. getUploadPack(getRepoWithoutBitmaps()),
  127. Arrays.asList(tipAdvertisedCommit));
  128. if (!isAdvertisedTipValid()) {
  129. assertTransportException(c,
  130. "want " + tipAdvertisedCommit.name() + " not valid");
  131. return;
  132. }
  133. c.call();
  134. }
  135. @Test
  136. public void validateUnadvertisedTipWithBitmaps() throws Throwable {
  137. ThrowingCallable c = () -> createValidator().checkWants(
  138. getUploadPack(getRepoWithBitmaps()),
  139. Arrays.asList(tipUnadvertisedCommit));
  140. if (!isUnadvertisedTipCommitValid()) {
  141. assertTransportException(c,
  142. "want " + tipUnadvertisedCommit.name() + " not valid");
  143. return;
  144. }
  145. c.call();
  146. }
  147. @Test
  148. public void validateUnadvertisedTipWithoutBitmaps() throws Throwable {
  149. ThrowingCallable c = () -> createValidator().checkWants(
  150. getUploadPack(getRepoWithoutBitmaps()),
  151. Arrays.asList(tipUnadvertisedCommit));
  152. if (!isUnadvertisedTipCommitValid()) {
  153. assertTransportException(c,
  154. "want " + tipUnadvertisedCommit.name() + " not valid");
  155. return;
  156. }
  157. c.call();
  158. }
  159. @Test
  160. public void validateUnreachableCommitWithBitmaps() throws Throwable {
  161. ThrowingCallable c = () -> createValidator().checkWants(
  162. getUploadPack(getRepoWithBitmaps()),
  163. Arrays.asList(unreachableCommit));
  164. if (!isUnreachableCommitValid()) {
  165. assertTransportException(c,
  166. "want " + unreachableCommit.name() + " not valid");
  167. return;
  168. }
  169. c.call();
  170. }
  171. @Test
  172. public void validateUnreachableCommitWithoutBitmaps() throws Throwable {
  173. ThrowingCallable c = () -> createValidator().checkWants(
  174. getUploadPack(getRepoWithoutBitmaps()),
  175. Arrays.asList(unreachableCommit));
  176. if (!isUnreachableCommitValid()) {
  177. assertTransportException(c,
  178. "want " + unreachableCommit.name() + " not valid");
  179. return;
  180. }
  181. c.call();
  182. }
  183. @Test
  184. public void validateReachableBlobWithBitmaps() throws Throwable {
  185. ThrowingCallable c = () -> createValidator().checkWants(
  186. getUploadPack(getRepoWithBitmaps()),
  187. Arrays.asList(reachableBlob));
  188. if (!isReachableBlobValid_withBitmaps()) {
  189. assertTransportException(c,
  190. "want " + reachableBlob.name() + " not valid");
  191. return;
  192. }
  193. c.call();
  194. }
  195. @Test
  196. public void validateReachableBlobWithoutBitmaps() throws Throwable {
  197. ThrowingCallable c = () -> createValidator().checkWants(
  198. getUploadPack(getRepoWithoutBitmaps()),
  199. Arrays.asList(reachableBlob));
  200. if (!isReachableBlobValid_withoutBitmaps()) {
  201. assertTransportException(c,
  202. "want " + reachableBlob.name() + " not valid");
  203. return;
  204. }
  205. c.call();
  206. }
  207. @Test
  208. public void validateUnreachableBlobWithBitmaps() throws Throwable {
  209. ThrowingCallable c = () -> createValidator().checkWants(
  210. getUploadPack(getRepoWithBitmaps()),
  211. Arrays.asList(unreachableBlob));
  212. if (!isUnreachableBlobValid()) {
  213. assertTransportException(c,
  214. "want " + unreachableBlob.name() + " not valid");
  215. return;
  216. }
  217. c.call();
  218. }
  219. @Test
  220. public void validateUnreachableBlobWithoutBitmaps() throws Throwable {
  221. ThrowingCallable c = () -> createValidator().checkWants(
  222. getUploadPack(getRepoWithoutBitmaps()),
  223. Arrays.asList(unreachableBlob));
  224. if (!isUnreachableBlobValid()) {
  225. assertTransportException(c,
  226. "want " + unreachableBlob.name() + " not valid");
  227. return;
  228. }
  229. c.call();
  230. }
  231. private void assertTransportException(ThrowingCallable c,
  232. String messageContent) throws AssertionError {
  233. assertThat(catchThrowableOfType(c, TransportException.class))
  234. .hasMessageContaining(messageContent);
  235. }
  236. private UploadPack getUploadPack(Repository repository) throws IOException {
  237. UploadPack uploadPack = new UploadPack(repository);
  238. Ref advertisedRef = repo.getRefDatabase().findRef("advertised");
  239. Map<String, Ref> advertisedRefs = new HashMap<>();
  240. advertisedRefs.put(advertisedRef.getName(), advertisedRef);
  241. uploadPack.setAdvertisedRefs(advertisedRefs);
  242. return uploadPack;
  243. }
  244. private Repository getRepoWithBitmaps() throws IOException {
  245. new DfsGarbageCollector(repo).pack(null);
  246. repo.scanForRepoChanges();
  247. return repo;
  248. }
  249. private Repository getRepoWithoutBitmaps() {
  250. return repo;
  251. }
  252. }