]> source.dussan.org Git - archiva.git/blob
e882c5ad65ba4fa53478c81d6205024653eda4f6
[archiva.git] /
1 package org.apache.maven.archiva.webdav;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.UnsupportedEncodingException;
25 import java.security.Principal;
26 import java.util.Enumeration;
27 import java.util.Locale;
28 import java.util.Map;
29 import javax.servlet.RequestDispatcher;
30 import javax.servlet.ServletInputStream;
31 import javax.servlet.http.Cookie;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34 import javax.servlet.http.HttpSession;
35 import junit.framework.TestCase;
36 import org.apache.jackrabbit.webdav.DavSessionProvider;
37 import org.apache.jackrabbit.webdav.WebdavRequest;
38 import org.apache.jackrabbit.webdav.WebdavRequestImpl;
39 import org.apache.maven.archiva.security.ServletAuthenticator;
40 import org.codehaus.plexus.redback.authentication.AuthenticationDataSource;
41 import org.codehaus.plexus.redback.authentication.AuthenticationException;
42 import org.codehaus.plexus.redback.authentication.AuthenticationResult;
43 import org.codehaus.plexus.redback.authorization.AuthorizationException;
44 import org.codehaus.plexus.redback.authorization.UnauthorizedException;
45 import org.codehaus.plexus.redback.policy.AccountLockedException;
46 import org.codehaus.plexus.redback.policy.MustChangePasswordException;
47 import org.codehaus.plexus.redback.system.SecuritySession;
48 import org.codehaus.plexus.redback.users.User;
49 import org.codehaus.plexus.redback.xwork.filter.authentication.HttpAuthenticator;
50
51 public class ArchivaDavSessionProviderTest extends TestCase
52 {
53     private DavSessionProvider sessionProvider;
54     
55     private WebdavRequest request;
56
57     @Override
58     protected void setUp()
59         throws Exception
60     {
61         super.setUp();
62         sessionProvider = new ArchivaDavSessionProvider(new ServletAuthenticatorMock(), new HttpAuthenticatorMock(), null);
63         request = new WebdavRequestImpl(new HttpServletRequestMock(), null);
64     }
65     
66     public void testAttachSession()
67         throws Exception
68     {
69         assertNull(request.getDavSession());
70         sessionProvider.attachSession(request);
71         assertNotNull(request.getDavSession());
72     }
73     
74     public void testReleaseSession()
75         throws Exception
76     {
77         assertNull(request.getDavSession());
78         sessionProvider.attachSession(request);
79         assertNotNull(request.getDavSession());
80         
81         sessionProvider.releaseSession(request);
82         assertNull(request.getDavSession());
83     }
84     
85     private class HttpServletRequestMock implements HttpServletRequest
86     {
87         public Object getAttribute(String arg0) {
88             throw new UnsupportedOperationException("Not supported yet.");
89         }
90
91         public Enumeration getAttributeNames() {
92             throw new UnsupportedOperationException("Not supported yet.");
93         }
94
95         public String getCharacterEncoding() {
96             throw new UnsupportedOperationException("Not supported yet.");
97         }
98
99         public int getContentLength() {
100             throw new UnsupportedOperationException("Not supported yet.");
101         }
102
103         public String getContentType() {
104             throw new UnsupportedOperationException("Not supported yet.");
105         }
106
107         public ServletInputStream getInputStream()
108             throws IOException
109         {
110             throw new UnsupportedOperationException("Not supported yet.");
111         }
112
113         public String getLocalAddr() {
114             throw new UnsupportedOperationException("Not supported yet.");
115         }
116
117         public String getLocalName() {
118             throw new UnsupportedOperationException("Not supported yet.");
119         }
120
121         public int getLocalPort() {
122             throw new UnsupportedOperationException("Not supported yet.");
123         }
124
125         public Locale getLocale() {
126             throw new UnsupportedOperationException("Not supported yet.");
127         }
128
129         public Enumeration getLocales()
130         {
131             throw new UnsupportedOperationException("Not supported yet.");
132         }
133
134         public String getParameter(String arg0)
135         {
136             throw new UnsupportedOperationException("Not supported yet.");
137         }
138
139         public Map getParameterMap()
140         {
141             throw new UnsupportedOperationException("Not supported yet.");
142         }
143
144         public Enumeration getParameterNames()
145         {
146             throw new UnsupportedOperationException("Not supported yet.");
147         }
148
149         public String[] getParameterValues(String arg0)
150         {
151             throw new UnsupportedOperationException("Not supported yet.");
152         }
153
154         public String getProtocol() {
155             throw new UnsupportedOperationException("Not supported yet.");
156         }
157
158         public BufferedReader getReader()
159             throws IOException 
160         {
161             throw new UnsupportedOperationException("Not supported yet.");
162         }
163
164         public String getRealPath(String arg0)
165         {
166             throw new UnsupportedOperationException("Not supported yet.");
167         }
168
169         public String getRemoteAddr()
170         {
171             throw new UnsupportedOperationException("Not supported yet.");
172         }
173
174         public String getRemoteHost()
175         {
176             throw new UnsupportedOperationException("Not supported yet.");
177         }
178
179         public int getRemotePort()
180         {
181             throw new UnsupportedOperationException("Not supported yet.");
182         }
183
184         public RequestDispatcher getRequestDispatcher(String arg0)
185         {
186             throw new UnsupportedOperationException("Not supported yet.");
187         }
188
189         public String getScheme()
190         {
191             return "";
192         }
193
194         public String getServerName()
195         {
196             throw new UnsupportedOperationException("Not supported yet.");
197         }
198
199         public int getServerPort()
200         {
201             throw new UnsupportedOperationException("Not supported yet.");
202         }
203
204         public boolean isSecure()
205         {
206             throw new UnsupportedOperationException("Not supported yet.");
207         }
208
209         public void removeAttribute(String arg0)
210         {
211             throw new UnsupportedOperationException("Not supported yet.");
212         }
213
214         public void setAttribute(String arg0, Object arg1)
215         {
216             throw new UnsupportedOperationException("Not supported yet.");
217         }
218
219         public void setCharacterEncoding(String arg0)
220             throws UnsupportedEncodingException
221         {
222             throw new UnsupportedOperationException("Not supported yet.");
223         }
224         
225
226         public String getAuthType()
227         {
228             throw new UnsupportedOperationException("Not supported yet.");
229         }
230
231         public String getContextPath()
232         {
233             return "/";
234         }
235
236         public Cookie[] getCookies()
237         {
238             throw new UnsupportedOperationException("Not supported yet.");
239         }
240
241         public long getDateHeader(String arg0) 
242         {
243             throw new UnsupportedOperationException("Not supported yet.");
244         }
245
246         public String getHeader(String arg0) {
247             return "";
248         }
249
250         public Enumeration getHeaderNames()
251         {
252             throw new UnsupportedOperationException("Not supported yet.");
253         }
254
255         public Enumeration getHeaders(String arg0) 
256         {
257             throw new UnsupportedOperationException("Not supported yet.");
258         }
259
260         public int getIntHeader(String arg0) 
261         {
262             throw new UnsupportedOperationException("Not supported yet.");
263         }
264
265         public String getMethod()
266         {
267             throw new UnsupportedOperationException("Not supported yet.");
268         }
269
270         public String getPathInfo()
271         {
272             throw new UnsupportedOperationException("Not supported yet.");
273         }
274
275         public String getPathTranslated()
276         {
277             throw new UnsupportedOperationException("Not supported yet.");
278         }
279
280         public String getQueryString()
281         {
282             throw new UnsupportedOperationException("Not supported yet.");
283         }
284
285         public String getRemoteUser()
286         {
287             throw new UnsupportedOperationException("Not supported yet.");
288         }
289
290         public String getRequestURI()
291         {
292             return "/";
293         }
294
295         public StringBuffer getRequestURL() 
296         {
297             throw new UnsupportedOperationException("Not supported yet.");
298         }
299
300         public String getRequestedSessionId() 
301         {
302             throw new UnsupportedOperationException("Not supported yet.");
303         }
304
305         public String getServletPath() 
306         {
307             throw new UnsupportedOperationException("Not supported yet.");
308         }
309
310         public HttpSession getSession(boolean arg0) 
311         {
312             throw new UnsupportedOperationException("Not supported yet.");
313         }
314
315         public HttpSession getSession() 
316         {
317             throw new UnsupportedOperationException("Not supported yet.");
318         }
319
320         public Principal getUserPrincipal() 
321         {
322             throw new UnsupportedOperationException("Not supported yet.");
323         }
324
325         public boolean isRequestedSessionIdFromCookie() 
326         {
327             throw new UnsupportedOperationException("Not supported yet.");
328         }
329
330         public boolean isRequestedSessionIdFromURL() 
331         {
332             throw new UnsupportedOperationException("Not supported yet.");
333         }
334
335         public boolean isRequestedSessionIdFromUrl() 
336         {
337             throw new UnsupportedOperationException("Not supported yet.");
338         }
339
340         public boolean isRequestedSessionIdValid() 
341         {
342             throw new UnsupportedOperationException("Not supported yet.");
343         }
344
345         public boolean isUserInRole(String arg0) 
346         {
347             throw new UnsupportedOperationException("Not supported yet.");
348         }
349     }
350     
351     private class ServletAuthenticatorMock implements ServletAuthenticator
352     {
353         public boolean isAuthenticated(HttpServletRequest arg0, AuthenticationResult arg1)
354             throws AuthenticationException, AccountLockedException, MustChangePasswordException
355         {
356             return true;
357         }
358
359         public boolean isAuthorized(HttpServletRequest arg0, SecuritySession arg1, String arg2, boolean arg3)
360             throws AuthorizationException, UnauthorizedException
361         {
362             return true;
363         }
364
365         public boolean isAuthorized(String arg0, String arg1)
366             throws UnauthorizedException
367         {
368             return true;
369         }   
370     }
371     
372     private class HttpAuthenticatorMock extends HttpAuthenticator
373     {
374         @Override
375         public void challenge(HttpServletRequest arg0, HttpServletResponse arg1, String arg2, AuthenticationException arg3)
376             throws IOException
377         {
378             //Do nothing
379         }
380
381         @Override
382         public AuthenticationResult getAuthenticationResult(HttpServletRequest arg0, HttpServletResponse arg1)
383             throws AuthenticationException, AccountLockedException, MustChangePasswordException
384         {
385             return new AuthenticationResult();
386         }
387         
388
389         @Override
390         public AuthenticationResult authenticate(AuthenticationDataSource arg0)
391             throws AuthenticationException, AccountLockedException, MustChangePasswordException
392         {
393             return new AuthenticationResult();
394         }
395
396         @Override
397         public void authenticate(HttpServletRequest arg0, HttpServletResponse arg1)
398             throws AuthenticationException
399         {
400             //Do nothing
401         }
402
403         @Override
404         public Map getContextSession()
405         {
406             return super.getContextSession();
407         }
408
409         @Override
410         public SecuritySession getSecuritySession()
411         {
412             return super.getSecuritySession();
413         }
414
415         @Override
416         public User getSessionUser()
417         {
418             return super.getSessionUser();
419         }
420
421         @Override
422         public boolean isAlreadyAuthenticated()
423         {
424             return super.isAlreadyAuthenticated();
425         }
426
427         @Override
428         public void setSecuritySession(SecuritySession session)
429         {
430             super.setSecuritySession(session);
431         }
432
433         @Override
434         public void setSessionUser(User user) {
435             super.setSessionUser(user);
436         }
437
438         @Override
439         public String storeDefaultUser(String user) {
440             return super.storeDefaultUser(user);
441         }
442         
443     }
444 }