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