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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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.transport.resolver.ReceivePackFactory;
  60. import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
  61. import org.eclipse.jgit.transport.resolver.UploadPackFactory;
  62. import org.junit.After;
  63. import org.junit.Before;
  64. import org.junit.Test;
  65. public class TestProtocolTest {
  66. private static final RefSpec HEADS = new RefSpec("+refs/heads/*:refs/heads/*");
  67. private static class User {
  68. private final String name;
  69. private User(String name) {
  70. this.name = name;
  71. }
  72. }
  73. private static class DefaultUpload implements UploadPackFactory<User> {
  74. @Override
  75. public UploadPack create(User req, Repository db) {
  76. return new UploadPack(db);
  77. }
  78. }
  79. private static class DefaultReceive implements ReceivePackFactory<User> {
  80. @Override
  81. public ReceivePack create(User req, Repository db) {
  82. return new ReceivePack(db);
  83. }
  84. }
  85. private List<TransportProtocol> protos;
  86. private TestRepository<InMemoryRepository> local;
  87. private TestRepository<InMemoryRepository> remote;
  88. @Before
  89. public void setUp() throws Exception {
  90. protos = new ArrayList<TransportProtocol>();
  91. local = new TestRepository<InMemoryRepository>(
  92. new InMemoryRepository(new DfsRepositoryDescription("local")));
  93. remote = new TestRepository<InMemoryRepository>(
  94. new InMemoryRepository(new DfsRepositoryDescription("remote")));
  95. }
  96. @After
  97. public void tearDown() {
  98. for (TransportProtocol proto : protos) {
  99. Transport.unregister(proto);
  100. }
  101. }
  102. @Test
  103. public void testFetch() throws Exception {
  104. ObjectId master = remote.branch("master").commit().create();
  105. TestProtocol<User> proto = registerDefault();
  106. URIish uri = proto.register(new User("user"), remote.getRepository());
  107. try (Git git = new Git(local.getRepository())) {
  108. git.fetch()
  109. .setRemote(uri.toString())
  110. .setRefSpecs(HEADS)
  111. .call();
  112. assertEquals(master,
  113. local.getRepository().getRef("master").getObjectId());
  114. }
  115. }
  116. @Test
  117. public void testPush() throws Exception {
  118. ObjectId master = local.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.push()
  123. .setRemote(uri.toString())
  124. .setRefSpecs(HEADS)
  125. .call();
  126. assertEquals(master,
  127. remote.getRepository().getRef("master").getObjectId());
  128. }
  129. }
  130. @Test
  131. public void testUploadPackFactory() throws Exception {
  132. ObjectId master = remote.branch("master").commit().create();
  133. final AtomicInteger rejected = new AtomicInteger();
  134. TestProtocol<User> proto = registerProto(new UploadPackFactory<User>() {
  135. @Override
  136. public UploadPack create(User req, Repository db)
  137. throws ServiceNotAuthorizedException {
  138. if (!"user2".equals(req.name)) {
  139. rejected.incrementAndGet();
  140. throw new ServiceNotAuthorizedException();
  141. }
  142. return new UploadPack(db);
  143. }
  144. }, new DefaultReceive());
  145. // Same repository, different users.
  146. URIish user1Uri = proto.register(new User("user1"), remote.getRepository());
  147. URIish user2Uri = proto.register(new User("user2"), remote.getRepository());
  148. try (Git git = new Git(local.getRepository())) {
  149. try {
  150. git.fetch()
  151. .setRemote(user1Uri.toString())
  152. .setRefSpecs(HEADS)
  153. .call();
  154. } catch (InvalidRemoteException expected) {
  155. // Expected.
  156. }
  157. assertEquals(1, rejected.get());
  158. assertNull(local.getRepository().getRef("master"));
  159. git.fetch()
  160. .setRemote(user2Uri.toString())
  161. .setRefSpecs(HEADS)
  162. .call();
  163. assertEquals(1, rejected.get());
  164. assertEquals(master,
  165. local.getRepository().getRef("master").getObjectId());
  166. }
  167. }
  168. @Test
  169. public void testReceivePackFactory() throws Exception {
  170. ObjectId master = local.branch("master").commit().create();
  171. final AtomicInteger rejected = new AtomicInteger();
  172. TestProtocol<User> proto = registerProto(new DefaultUpload(),
  173. new ReceivePackFactory<User>() {
  174. @Override
  175. public ReceivePack create(User req, Repository db)
  176. throws ServiceNotAuthorizedException {
  177. if (!"user2".equals(req.name)) {
  178. rejected.incrementAndGet();
  179. throw new ServiceNotAuthorizedException();
  180. }
  181. return new ReceivePack(db);
  182. }
  183. });
  184. // Same repository, different users.
  185. URIish user1Uri = proto.register(new User("user1"), remote.getRepository());
  186. URIish user2Uri = proto.register(new User("user2"), remote.getRepository());
  187. try (Git git = new Git(local.getRepository())) {
  188. try {
  189. git.push()
  190. .setRemote(user1Uri.toString())
  191. .setRefSpecs(HEADS)
  192. .call();
  193. } catch (TransportException expected) {
  194. assertTrue(expected.getMessage().contains(
  195. JGitText.get().pushNotPermitted));
  196. }
  197. assertEquals(1, rejected.get());
  198. assertNull(remote.getRepository().getRef("master"));
  199. git.push()
  200. .setRemote(user2Uri.toString())
  201. .setRefSpecs(HEADS)
  202. .call();
  203. assertEquals(1, rejected.get());
  204. assertEquals(master,
  205. remote.getRepository().getRef("master").getObjectId());
  206. }
  207. }
  208. private TestProtocol<User> registerDefault() {
  209. return registerProto(new DefaultUpload(), new DefaultReceive());
  210. }
  211. private TestProtocol<User> registerProto(UploadPackFactory<User> upf,
  212. ReceivePackFactory<User> rpf) {
  213. TestProtocol<User> proto = new TestProtocol<User>(upf, rpf);
  214. protos.add(proto);
  215. Transport.register(proto);
  216. return proto;
  217. }
  218. }