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.http.test;
45
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertNotNull;
48 import static org.junit.Assert.assertSame;
49 import static org.junit.Assert.fail;
50
51 import java.io.IOException;
52
53 import javax.servlet.http.HttpServletRequest;
54 import javax.servlet.http.HttpServletRequestWrapper;
55
56 import org.eclipse.jetty.server.Request;
57 import org.eclipse.jgit.http.server.resolver.DefaultReceivePackFactory;
58 import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
59 import org.eclipse.jgit.lib.PersonIdent;
60 import org.eclipse.jgit.lib.Repository;
61 import org.eclipse.jgit.lib.StoredConfig;
62 import org.eclipse.jgit.transport.ReceivePack;
63 import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
64 import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
65 import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
66 import org.junit.Before;
67 import org.junit.Test;
68
69 public class DefaultReceivePackFactoryTest extends LocalDiskRepositoryTestCase {
70 private Repository db;
71
72 private ReceivePackFactory<HttpServletRequest> factory;
73
74 @Override
75 @Before
76 public void setUp() throws Exception {
77 super.setUp();
78
79 db = createBareRepository();
80 factory = new DefaultReceivePackFactory();
81 }
82
83 @SuppressWarnings("unchecked")
84 @Test
85 public void testDisabledSingleton() throws ServiceNotAuthorizedException {
86 factory = (ReceivePackFactory<HttpServletRequest>) ReceivePackFactory.DISABLED;
87
88 try {
89 factory.create(new R(null, "localhost"), db);
90 fail("Created session for anonymous user: null");
91 } catch (ServiceNotEnabledException e) {
92
93 }
94
95 try {
96 factory.create(new R("", "localhost"), db);
97 fail("Created session for anonymous user: \"\"");
98 } catch (ServiceNotEnabledException e) {
99
100 }
101
102 try {
103 factory.create(new R("bob", "localhost"), db);
104 fail("Created session for user: \"bob\"");
105 } catch (ServiceNotEnabledException e) {
106
107 }
108 }
109
110 @Test
111 public void testCreate_NullUser() throws ServiceNotEnabledException {
112 try {
113 factory.create(new R(null, "localhost"), db);
114 fail("Created session for anonymous user: null");
115 } catch (ServiceNotAuthorizedException e) {
116
117 }
118 }
119
120 @Test
121 public void testCreate_EmptyStringUser() throws ServiceNotEnabledException {
122 try {
123 factory.create(new R("", "localhost"), db);
124 fail("Created session for anonymous user: \"\"");
125 } catch (ServiceNotAuthorizedException e) {
126
127 }
128 }
129
130 @Test
131 public void testCreate_AuthUser() throws ServiceNotEnabledException,
132 ServiceNotAuthorizedException {
133 ReceivePack rp;
134 rp = factory.create(new R("bob", "1.2.3.4"), db);
135 assertNotNull("have ReceivePack", rp);
136 assertSame(db, rp.getRepository());
137
138 PersonIdent id = rp.getRefLogIdent();
139 assertNotNull(id);
140 assertEquals("bob", id.getName());
141 assertEquals("bob@1.2.3.4", id.getEmailAddress());
142
143
144 assertEquals(author.getTimeZoneOffset(), id.getTimeZoneOffset());
145 assertEquals(author.getWhen(), id.getWhen());
146 }
147
148 @Test
149 public void testCreate_Disabled() throws ServiceNotAuthorizedException,
150 IOException {
151 final StoredConfig cfg = db.getConfig();
152 cfg.setBoolean("http", null, "receivepack", false);
153 cfg.save();
154
155 try {
156 factory.create(new R(null, "localhost"), db);
157 fail("Created session for anonymous user: null");
158 } catch (ServiceNotEnabledException e) {
159
160 }
161
162 try {
163 factory.create(new R("", "localhost"), db);
164 fail("Created session for anonymous user: \"\"");
165 } catch (ServiceNotEnabledException e) {
166
167 }
168
169 try {
170 factory.create(new R("bob", "localhost"), db);
171 fail("Created session for user: \"bob\"");
172 } catch (ServiceNotEnabledException e) {
173
174 }
175 }
176
177 @Test
178 public void testCreate_Enabled() throws ServiceNotEnabledException,
179 ServiceNotAuthorizedException, IOException {
180 final StoredConfig cfg = db.getConfig();
181 cfg.setBoolean("http", null, "receivepack", true);
182 cfg.save();
183
184 ReceivePack rp;
185
186 rp = factory.create(new R(null, "1.2.3.4"), db);
187 assertNotNull("have ReceivePack", rp);
188 assertSame(db, rp.getRepository());
189
190 PersonIdent id = rp.getRefLogIdent();
191 assertNotNull(id);
192 assertEquals("anonymous", id.getName());
193 assertEquals("anonymous@1.2.3.4", id.getEmailAddress());
194
195
196 assertEquals(author.getTimeZoneOffset(), id.getTimeZoneOffset());
197 assertEquals(author.getWhen(), id.getWhen());
198
199 rp = factory.create(new R("bob", "1.2.3.4"), db);
200 assertNotNull("have ReceivePack", rp);
201 }
202
203 private static final class R extends HttpServletRequestWrapper {
204 private final String user;
205
206 private final String host;
207
208 R(final String user, final String host) {
209 super(new Request(null, null) );
210 this.user = user;
211 this.host = host;
212 }
213
214 @Override
215 public String getRemoteHost() {
216 return host;
217 }
218
219 @Override
220 public String getRemoteUser() {
221 return user;
222 }
223 }
224 }