]> source.dussan.org Git - archiva.git/blob
20a8543f95728c5e02906142ab26726b525f9153
[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
30 import javax.servlet.RequestDispatcher;
31 import javax.servlet.ServletInputStream;
32 import javax.servlet.http.Cookie;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35 import javax.servlet.http.HttpSession;
36
37 import junit.framework.TestCase;
38
39 import org.apache.jackrabbit.webdav.DavSessionProvider;
40 import org.apache.jackrabbit.webdav.WebdavRequest;
41 import org.apache.jackrabbit.webdav.WebdavRequestImpl;
42 import org.apache.maven.archiva.security.ServletAuthenticator;
43 import org.codehaus.plexus.redback.authentication.AuthenticationDataSource;
44 import org.codehaus.plexus.redback.authentication.AuthenticationException;
45 import org.codehaus.plexus.redback.authentication.AuthenticationResult;
46 import org.codehaus.plexus.redback.authorization.AuthorizationException;
47 import org.codehaus.plexus.redback.authorization.UnauthorizedException;
48 import org.codehaus.plexus.redback.policy.AccountLockedException;
49 import org.codehaus.plexus.redback.policy.MustChangePasswordException;
50 import org.codehaus.plexus.redback.system.SecuritySession;
51 import org.codehaus.plexus.redback.users.User;
52 import org.codehaus.redback.integration.filter.authentication.HttpAuthenticator;
53
54 public class ArchivaDavSessionProviderTest extends TestCase
55 {
56     private DavSessionProvider sessionProvider;
57     
58     private WebdavRequest request;
59
60     @Override
61     protected void setUp()
62         throws Exception
63     {
64         super.setUp();
65         sessionProvider = new ArchivaDavSessionProvider(new ServletAuthenticatorMock(), new HttpAuthenticatorMock(), null);
66         request = new WebdavRequestImpl(new HttpServletRequestMock(), null);
67     }
68     
69     public void testAttachSession()
70         throws Exception
71     {
72         assertNull(request.getDavSession());
73         sessionProvider.attachSession(request);
74         assertNotNull(request.getDavSession());
75     }
76     
77     public void testReleaseSession()
78         throws Exception
79     {
80         assertNull(request.getDavSession());
81         sessionProvider.attachSession(request);
82         assertNotNull(request.getDavSession());
83         
84         sessionProvider.releaseSession(request);
85         assertNull(request.getDavSession());
86     }
87     
88     private class HttpServletRequestMock implements HttpServletRequest
89     {
90         public Object getAttribute(String arg0) {
91             throw new UnsupportedOperationException("Not supported yet.");
92         }
93
94         public Enumeration getAttributeNames() {
95             throw new UnsupportedOperationException("Not supported yet.");
96         }
97
98         public String getCharacterEncoding() {
99             throw new UnsupportedOperationException("Not supported yet.");
100         }
101
102         public int getContentLength() {
103             throw new UnsupportedOperationException("Not supported yet.");
104         }
105
106         public String getContentType() {
107             throw new UnsupportedOperationException("Not supported yet.");
108         }
109
110         public ServletInputStream getInputStream()
111             throws IOException
112         {
113             throw new UnsupportedOperationException("Not supported yet.");
114         }
115
116         public String getLocalAddr() {
117             throw new UnsupportedOperationException("Not supported yet.");
118         }
119
120         public String getLocalName() {
121             throw new UnsupportedOperationException("Not supported yet.");
122         }
123
124         public int getLocalPort() {
125             throw new UnsupportedOperationException("Not supported yet.");
126         }
127
128         public Locale getLocale() {
129             throw new UnsupportedOperationException("Not supported yet.");
130         }
131
132         public Enumeration getLocales()
133         {
134             throw new UnsupportedOperationException("Not supported yet.");
135         }
136
137         public String getParameter(String arg0)
138         {
139             throw new UnsupportedOperationException("Not supported yet.");
140         }
141
142         public Map getParameterMap()
143         {
144             throw new UnsupportedOperationException("Not supported yet.");
145         }
146
147         public Enumeration getParameterNames()
148         {
149             throw new UnsupportedOperationException("Not supported yet.");
150         }
151
152         public String[] getParameterValues(String arg0)
153         {
154             throw new UnsupportedOperationException("Not supported yet.");
155         }
156
157         public String getProtocol() {
158             throw new UnsupportedOperationException("Not supported yet.");
159         }
160
161         public BufferedReader getReader()
162             throws IOException 
163         {
164             throw new UnsupportedOperationException("Not supported yet.");
165         }
166
167         public String getRealPath(String arg0)
168         {
169             throw new UnsupportedOperationException("Not supported yet.");
170         }
171
172         public String getRemoteAddr()
173         {
174             throw new UnsupportedOperationException("Not supported yet.");
175         }
176
177         public String getRemoteHost()
178         {
179             throw new UnsupportedOperationException("Not supported yet.");
180         }
181
182         public int getRemotePort()
183         {
184             throw new UnsupportedOperationException("Not supported yet.");
185         }
186
187         public RequestDispatcher getRequestDispatcher(String arg0)
188         {
189             throw new UnsupportedOperationException("Not supported yet.");
190         }
191
192         public String getScheme()
193         {
194             return "";
195         }
196
197         public String getServerName()
198         {
199             throw new UnsupportedOperationException("Not supported yet.");
200         }
201
202         public int getServerPort()
203         {
204             throw new UnsupportedOperationException("Not supported yet.");
205         }
206
207         public boolean isSecure()
208         {
209             throw new UnsupportedOperationException("Not supported yet.");
210         }
211
212         public void removeAttribute(String arg0)
213         {
214             throw new UnsupportedOperationException("Not supported yet.");
215         }
216
217         public void setAttribute(String arg0, Object arg1)
218         {
219             throw new UnsupportedOperationException("Not supported yet.");
220         }
221
222         public void setCharacterEncoding(String arg0)
223             throws UnsupportedEncodingException
224         {
225             throw new UnsupportedOperationException("Not supported yet.");
226         }
227         
228
229         public String getAuthType()
230         {
231             throw new UnsupportedOperationException("Not supported yet.");
232         }
233
234         public String getContextPath()
235         {
236             return "/";
237         }
238
239         public Cookie[] getCookies()
240         {
241             throw new UnsupportedOperationException("Not supported yet.");
242         }
243
244         public long getDateHeader(String arg0) 
245         {
246             throw new UnsupportedOperationException("Not supported yet.");
247         }
248
249         public String getHeader(String arg0) {
250             return "";
251         }
252
253         public Enumeration getHeaderNames()
254         {
255             throw new UnsupportedOperationException("Not supported yet.");
256         }
257
258         public Enumeration getHeaders(String arg0) 
259         {
260             throw new UnsupportedOperationException("Not supported yet.");
261         }
262
263         public int getIntHeader(String arg0) 
264         {
265             throw new UnsupportedOperationException("Not supported yet.");
266         }
267
268         public String getMethod()
269         {
270             throw new UnsupportedOperationException("Not supported yet.");
271         }
272
273         public String getPathInfo()
274         {
275             throw new UnsupportedOperationException("Not supported yet.");
276         }
277
278         public String getPathTranslated()
279         {
280             throw new UnsupportedOperationException("Not supported yet.");
281         }
282
283         public String getQueryString()
284         {
285             throw new UnsupportedOperationException("Not supported yet.");
286         }
287
288         public String getRemoteUser()
289         {
290             throw new UnsupportedOperationException("Not supported yet.");
291         }
292
293         public String getRequestURI()
294         {
295             return "/";
296         }
297
298         public StringBuffer getRequestURL() 
299         {
300             throw new UnsupportedOperationException("Not supported yet.");
301         }
302
303         public String getRequestedSessionId() 
304         {
305             throw new UnsupportedOperationException("Not supported yet.");
306         }
307
308         public String getServletPath() 
309         {
310             throw new UnsupportedOperationException("Not supported yet.");
311         }
312
313         public HttpSession getSession(boolean arg0) 
314         {
315             throw new UnsupportedOperationException("Not supported yet.");
316         }
317
318         public HttpSession getSession() 
319         {
320             throw new UnsupportedOperationException("Not supported yet.");
321         }
322
323         public Principal getUserPrincipal() 
324         {
325             throw new UnsupportedOperationException("Not supported yet.");
326         }
327
328         public boolean isRequestedSessionIdFromCookie() 
329         {
330             throw new UnsupportedOperationException("Not supported yet.");
331         }
332
333         public boolean isRequestedSessionIdFromURL() 
334         {
335             throw new UnsupportedOperationException("Not supported yet.");
336         }
337
338         public boolean isRequestedSessionIdFromUrl() 
339         {
340             throw new UnsupportedOperationException("Not supported yet.");
341         }
342
343         public boolean isRequestedSessionIdValid() 
344         {
345             throw new UnsupportedOperationException("Not supported yet.");
346         }
347
348         public boolean isUserInRole(String arg0) 
349         {
350             throw new UnsupportedOperationException("Not supported yet.");
351         }
352     }
353     
354     private class ServletAuthenticatorMock implements ServletAuthenticator
355     {
356         public boolean isAuthenticated(HttpServletRequest arg0, AuthenticationResult arg1)
357             throws AuthenticationException, AccountLockedException, MustChangePasswordException
358         {
359             return true;
360         }
361
362         public boolean isAuthorized(HttpServletRequest arg0, SecuritySession arg1, String arg2, boolean arg3)
363             throws AuthorizationException, UnauthorizedException
364         {
365             return true;
366         }
367
368         public boolean isAuthorized(String arg0, String arg1, boolean isWriteRequest)
369             throws UnauthorizedException
370         {
371             return true;
372         }   
373     }
374     
375     private class HttpAuthenticatorMock extends HttpAuthenticator
376     {
377         @Override
378         public void challenge(HttpServletRequest arg0, HttpServletResponse arg1, String arg2, AuthenticationException arg3)
379             throws IOException
380         {
381             //Do nothing
382         }
383
384         @Override
385         public AuthenticationResult getAuthenticationResult(HttpServletRequest arg0, HttpServletResponse arg1)
386             throws AuthenticationException, AccountLockedException, MustChangePasswordException
387         {
388             return new AuthenticationResult();
389         }
390         
391
392         @Override
393         public AuthenticationResult authenticate(AuthenticationDataSource arg0, HttpSession httpSession)
394             throws AuthenticationException, AccountLockedException, MustChangePasswordException
395         {
396             return new AuthenticationResult();
397         }
398
399         @Override
400         public void authenticate(HttpServletRequest arg0, HttpServletResponse arg1)
401             throws AuthenticationException
402         {
403             //Do nothing
404         }
405
406         @Override
407         public SecuritySession getSecuritySession(HttpSession httpSession)
408         {
409             return super.getSecuritySession(httpSession);
410         }
411
412         @Override
413         public User getSessionUser(HttpSession httpSession)
414         {
415             return super.getSessionUser(httpSession);
416         }
417
418         @Override
419         public boolean isAlreadyAuthenticated(HttpSession httpSession)
420         {
421             return super.isAlreadyAuthenticated(httpSession);
422         }        
423     }
424 }