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.

TestProtocolTest.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright (C) 2015, Google Inc.
  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.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertNull;
  46. import static org.junit.Assert.assertTrue;
  47. import static org.junit.Assert.fail;
  48. import java.util.ArrayList;
  49. import java.util.List;
  50. import java.util.concurrent.atomic.AtomicInteger;
  51. import org.eclipse.jgit.api.Git;
  52. import org.eclipse.jgit.api.errors.InvalidRemoteException;
  53. import org.eclipse.jgit.api.errors.TransportException;
  54. import org.eclipse.jgit.internal.JGitText;
  55. import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
  56. import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
  57. import org.eclipse.jgit.junit.TestRepository;
  58. import org.eclipse.jgit.lib.ObjectId;
  59. import org.eclipse.jgit.lib.Repository;
  60. import org.eclipse.jgit.revwalk.RevCommit;
  61. import org.eclipse.jgit.storage.pack.PackStatistics;
  62. import org.eclipse.jgit.transport.BasePackFetchConnection.FetchConfig;
  63. import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
  64. import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
  65. import org.eclipse.jgit.transport.resolver.UploadPackFactory;
  66. import org.junit.After;
  67. import org.junit.Before;
  68. import org.junit.Test;
  69. public class TestProtocolTest {
  70. private static final RefSpec HEADS = new RefSpec("+refs/heads/*:refs/heads/*");
  71. private static final RefSpec MASTER = new RefSpec(
  72. "+refs/heads/master:refs/heads/master");
  73. private static final int HAVES_PER_ROUND = 32;
  74. private static final int MAX_HAVES = 256;
  75. private static class User {
  76. private final String name;
  77. private User(String name) {
  78. this.name = name;
  79. }
  80. }
  81. private static class DefaultUpload implements UploadPackFactory<User> {
  82. @Override
  83. public UploadPack create(User req, Repository db) {
  84. UploadPack up = new UploadPack(db);
  85. up.setPostUploadHook((PackStatistics stats) -> {
  86. havesCount = stats.getHaves();
  87. });
  88. return up;
  89. }
  90. }
  91. private static class DefaultReceive implements ReceivePackFactory<User> {
  92. @Override
  93. public ReceivePack create(User req, Repository db) {
  94. return new ReceivePack(db);
  95. }
  96. }
  97. private static long havesCount;
  98. private List<TransportProtocol> protos;
  99. private TestRepository<InMemoryRepository> local;
  100. private TestRepository<InMemoryRepository> remote;
  101. @Before
  102. public void setUp() throws Exception {
  103. protos = new ArrayList<>();
  104. local = new TestRepository<>(
  105. new InMemoryRepository(new DfsRepositoryDescription("local")));
  106. remote = new TestRepository<>(
  107. new InMemoryRepository(new DfsRepositoryDescription("remote")));
  108. }
  109. @After
  110. public void tearDown() {
  111. for (TransportProtocol proto : protos) {
  112. Transport.unregister(proto);
  113. }
  114. }
  115. @Test
  116. public void testFetch() throws Exception {
  117. ObjectId master = remote.branch("master").commit().create();
  118. TestProtocol<User> proto = registerDefault();
  119. URIish uri = proto.register(new User("user"), remote.getRepository());
  120. try (Git git = new Git(local.getRepository())) {
  121. git.fetch()
  122. .setRemote(uri.toString())
  123. .setRefSpecs(HEADS)
  124. .call();
  125. assertEquals(master,
  126. local.getRepository().exactRef("refs/heads/master").getObjectId());
  127. }
  128. }
  129. @Test
  130. public void testPush() throws Exception {
  131. ObjectId master = local.branch("master").commit().create();
  132. TestProtocol<User> proto = registerDefault();
  133. URIish uri = proto.register(new User("user"), remote.getRepository());
  134. try (Git git = new Git(local.getRepository())) {
  135. git.push()
  136. .setRemote(uri.toString())
  137. .setRefSpecs(HEADS)
  138. .call();
  139. assertEquals(master,
  140. remote.getRepository().exactRef("refs/heads/master").getObjectId());
  141. }
  142. }
  143. @Test
  144. public void testFullNegotiation() throws Exception {
  145. TestProtocol<User> proto = registerDefault();
  146. URIish uri = proto.register(new User("user"), remote.getRepository());
  147. // Enough local branches to cause 10 rounds of negotiation,
  148. // and a unique remote master branch commit with a later timestamp.
  149. for (int i = 0; i < 10 * HAVES_PER_ROUND; i++) {
  150. local.branch("local-branch-" + i).commit().create();
  151. }
  152. remote.tick(11 * HAVES_PER_ROUND);
  153. RevCommit master = remote.branch("master").commit()
  154. .add("readme.txt", "unique commit").create();
  155. try (Git git = new Git(local.getRepository())) {
  156. assertNull(local.getRepository().exactRef("refs/heads/master"));
  157. git.fetch().setRemote(uri.toString()).setRefSpecs(MASTER).call();
  158. assertEquals(master, local.getRepository()
  159. .exactRef("refs/heads/master").getObjectId());
  160. assertEquals(10 * HAVES_PER_ROUND, havesCount);
  161. }
  162. }
  163. @Test
  164. public void testMaxHaves() throws Exception {
  165. TestProtocol<User> proto = registerDefault();
  166. URIish uri = proto.register(new User("user"), remote.getRepository());
  167. // Enough local branches to cause 10 rounds of negotiation,
  168. // and a unique remote master branch commit with a later timestamp.
  169. for (int i = 0; i < 10 * HAVES_PER_ROUND; i++) {
  170. local.branch("local-branch-" + i).commit().create();
  171. }
  172. remote.tick(11 * HAVES_PER_ROUND);
  173. RevCommit master = remote.branch("master").commit()
  174. .add("readme.txt", "unique commit").create();
  175. TestProtocol.setFetchConfig(new FetchConfig(true, MAX_HAVES));
  176. try (Git git = new Git(local.getRepository())) {
  177. assertNull(local.getRepository().exactRef("refs/heads/master"));
  178. git.fetch().setRemote(uri.toString()).setRefSpecs(MASTER).call();
  179. assertEquals(master, local.getRepository()
  180. .exactRef("refs/heads/master").getObjectId());
  181. assertTrue(havesCount <= MAX_HAVES);
  182. }
  183. }
  184. @Test
  185. public void testUploadPackFactory() throws Exception {
  186. ObjectId master = remote.branch("master").commit().create();
  187. final AtomicInteger rejected = new AtomicInteger();
  188. TestProtocol<User> proto = registerProto((User req, Repository db) -> {
  189. if (!"user2".equals(req.name)) {
  190. rejected.incrementAndGet();
  191. throw new ServiceNotAuthorizedException();
  192. }
  193. return new UploadPack(db);
  194. }, new DefaultReceive());
  195. // Same repository, different users.
  196. URIish user1Uri = proto.register(new User("user1"), remote.getRepository());
  197. URIish user2Uri = proto.register(new User("user2"), remote.getRepository());
  198. try (Git git = new Git(local.getRepository())) {
  199. try {
  200. git.fetch()
  201. .setRemote(user1Uri.toString())
  202. .setRefSpecs(MASTER)
  203. .call();
  204. fail("accepted not permitted fetch");
  205. } catch (InvalidRemoteException expected) {
  206. // Expected.
  207. }
  208. assertEquals(1, rejected.get());
  209. assertNull(local.getRepository().exactRef("refs/heads/master"));
  210. git.fetch()
  211. .setRemote(user2Uri.toString())
  212. .setRefSpecs(MASTER)
  213. .call();
  214. assertEquals(1, rejected.get());
  215. assertEquals(master,
  216. local.getRepository().exactRef("refs/heads/master").getObjectId());
  217. }
  218. }
  219. @Test
  220. public void testReceivePackFactory() throws Exception {
  221. ObjectId master = local.branch("master").commit().create();
  222. final AtomicInteger rejected = new AtomicInteger();
  223. TestProtocol<User> proto = registerProto(new DefaultUpload(),
  224. (User req, Repository db) -> {
  225. if (!"user2".equals(req.name)) {
  226. rejected.incrementAndGet();
  227. throw new ServiceNotAuthorizedException();
  228. }
  229. return new ReceivePack(db);
  230. });
  231. // Same repository, different users.
  232. URIish user1Uri = proto.register(new User("user1"), remote.getRepository());
  233. URIish user2Uri = proto.register(new User("user2"), remote.getRepository());
  234. try (Git git = new Git(local.getRepository())) {
  235. try {
  236. git.push()
  237. .setRemote(user1Uri.toString())
  238. .setRefSpecs(HEADS)
  239. .call();
  240. fail("accepted not permitted push");
  241. } catch (TransportException expected) {
  242. assertTrue(expected.getMessage().contains(
  243. JGitText.get().pushNotPermitted));
  244. }
  245. assertEquals(1, rejected.get());
  246. assertNull(remote.getRepository().exactRef("refs/heads/master"));
  247. git.push()
  248. .setRemote(user2Uri.toString())
  249. .setRefSpecs(HEADS)
  250. .call();
  251. assertEquals(1, rejected.get());
  252. assertEquals(master,
  253. remote.getRepository().exactRef("refs/heads/master").getObjectId());
  254. }
  255. }
  256. private TestProtocol<User> registerDefault() {
  257. return registerProto(new DefaultUpload(), new DefaultReceive());
  258. }
  259. private TestProtocol<User> registerProto(UploadPackFactory<User> upf,
  260. ReceivePackFactory<User> rpf) {
  261. TestProtocol<User> proto = new TestProtocol<>(upf, rpf);
  262. protos.add(proto);
  263. Transport.register(proto);
  264. return proto;
  265. }
  266. }