1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
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
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
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 }