You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ArchivaDavSessionProviderTest.java 13KB

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