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 11KB

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