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.fail;
47
48 import java.io.IOException;
49
50 import javax.servlet.http.HttpServletRequestWrapper;
51
52 import org.eclipse.jetty.server.Request;
53 import org.eclipse.jgit.http.server.resolver.AsIsFileService;
54 import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
55 import org.eclipse.jgit.lib.Repository;
56 import org.eclipse.jgit.lib.StoredConfig;
57 import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
58 import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
59 import org.junit.Before;
60 import org.junit.Test;
61
62 public class AsIsServiceTest extends LocalDiskRepositoryTestCase {
63 private Repository db;
64
65 private AsIsFileService service;
66
67 @Before
68 public void setUp() throws Exception {
69 super.setUp();
70
71 db = createBareRepository();
72 service = new AsIsFileService();
73 }
74
75 @Test
76 public void testDisabledSingleton() throws ServiceNotAuthorizedException {
77 service = AsIsFileService.DISABLED;
78 try {
79 service.access(new R(null, "1.2.3.4"), db);
80 fail("Created session for anonymous user: null");
81 } catch (ServiceNotEnabledException e) {
82
83 }
84
85 try {
86 service.access(new R("bob", "1.2.3.4"), db);
87 fail("Created session for user: \"bob\"");
88 } catch (ServiceNotEnabledException e) {
89
90 }
91 }
92
93 @Test
94 public void testCreate_Default() throws ServiceNotEnabledException,
95 ServiceNotAuthorizedException {
96 service.access(new R(null, "1.2.3.4"), db);
97 service.access(new R("bob", "1.2.3.4"), db);
98 }
99
100 @Test
101 public void testCreate_Disabled() throws ServiceNotAuthorizedException,
102 IOException {
103 final StoredConfig cfg = db.getConfig();
104 cfg.setBoolean("http", null, "getanyfile", false);
105 cfg.save();
106
107 try {
108 service.access(new R(null, "1.2.3.4"), db);
109 fail("Created session for anonymous user: null");
110 } catch (ServiceNotEnabledException e) {
111
112 }
113
114 try {
115 service.access(new R("bob", "1.2.3.4"), db);
116 fail("Created session for user: \"bob\"");
117 } catch (ServiceNotEnabledException e) {
118
119 }
120 }
121
122 @Test
123 public void testCreate_Enabled() throws ServiceNotEnabledException,
124 ServiceNotAuthorizedException {
125 db.getConfig().setBoolean("http", null, "getanyfile", true);
126 service.access(new R(null, "1.2.3.4"), db);
127 service.access(new R("bob", "1.2.3.4"), db);
128 }
129
130 private static final class R extends HttpServletRequestWrapper {
131 private final String user;
132
133 private final String host;
134
135 R(final String user, final String host) {
136 super(new Request(null, null) );
137 this.user = user;
138 this.host = host;
139 }
140
141 @Override
142 public String getRemoteHost() {
143 return host;
144 }
145
146 @Override
147 public String getRemoteUser() {
148 return user;
149 }
150 }
151 }