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.assertNotNull;
47 import static org.junit.Assert.assertSame;
48 import static org.junit.Assert.fail;
49
50 import java.io.IOException;
51
52 import javax.servlet.http.HttpServletRequest;
53 import javax.servlet.http.HttpServletRequestWrapper;
54
55 import org.eclipse.jetty.server.Request;
56 import org.eclipse.jgit.http.server.resolver.DefaultUploadPackFactory;
57 import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
58 import org.eclipse.jgit.lib.Repository;
59 import org.eclipse.jgit.lib.StoredConfig;
60 import org.eclipse.jgit.transport.UploadPack;
61 import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
62 import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
63 import org.eclipse.jgit.transport.resolver.UploadPackFactory;
64 import org.junit.Before;
65 import org.junit.Test;
66
67 public class DefaultUploadPackFactoryTest extends LocalDiskRepositoryTestCase {
68 private Repository db;
69
70 private UploadPackFactory<HttpServletRequest> factory;
71
72 @Override
73 @Before
74 public void setUp() throws Exception {
75 super.setUp();
76
77 db = createBareRepository();
78 factory = new DefaultUploadPackFactory();
79 }
80
81 @SuppressWarnings("unchecked")
82 @Test
83 public void testDisabledSingleton() throws ServiceNotAuthorizedException {
84 factory = (UploadPackFactory<HttpServletRequest>) UploadPackFactory.DISABLED;
85
86 try {
87 factory.create(new R(null, "localhost"), db);
88 fail("Created session for anonymous user: null");
89 } catch (ServiceNotEnabledException e) {
90
91 }
92
93 try {
94 factory.create(new R("", "localhost"), db);
95 fail("Created session for anonymous user: \"\"");
96 } catch (ServiceNotEnabledException e) {
97
98 }
99
100 try {
101 factory.create(new R("bob", "localhost"), db);
102 fail("Created session for user: \"bob\"");
103 } catch (ServiceNotEnabledException e) {
104
105 }
106 }
107
108 @Test
109 public void testCreate_Default() throws ServiceNotEnabledException,
110 ServiceNotAuthorizedException {
111 UploadPack up;
112
113 up = factory.create(new R(null, "1.2.3.4"), db);
114 assertNotNull("have UploadPack", up);
115 assertSame(db, up.getRepository());
116
117 up = factory.create(new R("bob", "1.2.3.4"), db);
118 assertNotNull("have UploadPack", up);
119 assertSame(db, up.getRepository());
120 }
121
122 @Test
123 public void testCreate_Disabled() throws ServiceNotAuthorizedException,
124 IOException {
125 final StoredConfig cfg = db.getConfig();
126 cfg.setBoolean("http", null, "uploadpack", false);
127 cfg.save();
128
129 try {
130 factory.create(new R(null, "localhost"), db);
131 fail("Created session for anonymous user: null");
132 } catch (ServiceNotEnabledException e) {
133
134 }
135
136 try {
137 factory.create(new R("bob", "localhost"), db);
138 fail("Created session for user: \"bob\"");
139 } catch (ServiceNotEnabledException e) {
140
141 }
142 }
143
144 @Test
145 public void testCreate_Enabled() throws ServiceNotEnabledException,
146 ServiceNotAuthorizedException {
147 db.getConfig().setBoolean("http", null, "uploadpack", true);
148 UploadPack up;
149
150 up = factory.create(new R(null, "1.2.3.4"), db);
151 assertNotNull("have UploadPack", up);
152 assertSame(db, up.getRepository());
153
154 up = factory.create(new R("bob", "1.2.3.4"), db);
155 assertNotNull("have UploadPack", up);
156 assertSame(db, up.getRepository());
157 }
158
159 private static final class R extends HttpServletRequestWrapper {
160 private final String user;
161
162 private final String host;
163
164 R(String user, String host) {
165 super(new Request(null, null) );
166 this.user = user;
167 this.host = host;
168 }
169
170 @Override
171 public String getRemoteHost() {
172 return host;
173 }
174
175 @Override
176 public String getRemoteUser() {
177 return user;
178 }
179 }
180 }