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 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. package org.apache.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 junit.framework.TestCase;
  21. import org.apache.archiva.redback.authentication.AuthenticationDataSource;
  22. import org.apache.archiva.redback.authentication.AuthenticationException;
  23. import org.apache.archiva.redback.authentication.AuthenticationResult;
  24. import org.apache.archiva.redback.authorization.AuthorizationException;
  25. import org.apache.archiva.redback.authorization.UnauthorizedException;
  26. import org.apache.archiva.redback.integration.filter.authentication.HttpAuthenticator;
  27. import org.apache.archiva.redback.policy.AccountLockedException;
  28. import org.apache.archiva.redback.policy.MustChangePasswordException;
  29. import org.apache.archiva.redback.system.SecuritySession;
  30. import org.apache.archiva.redback.users.User;
  31. import org.apache.archiva.security.ServletAuthenticator;
  32. import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
  33. import org.apache.jackrabbit.webdav.DavSessionProvider;
  34. import org.apache.jackrabbit.webdav.WebdavRequest;
  35. import org.apache.jackrabbit.webdav.WebdavRequestImpl;
  36. import org.junit.Before;
  37. import org.junit.Test;
  38. import org.junit.runner.RunWith;
  39. import org.springframework.mock.web.MockHttpServletRequest;
  40. import javax.servlet.http.HttpServletRequest;
  41. import javax.servlet.http.HttpServletResponse;
  42. import javax.servlet.http.HttpSession;
  43. import java.io.IOException;
  44. @RunWith( ArchivaBlockJUnit4ClassRunner.class )
  45. public class ArchivaDavSessionProviderTest
  46. extends TestCase
  47. {
  48. private DavSessionProvider sessionProvider;
  49. private WebdavRequest request;
  50. @Override
  51. @Before
  52. public void setUp()
  53. throws Exception
  54. {
  55. super.setUp();
  56. sessionProvider = new ArchivaDavSessionProvider( new ServletAuthenticatorMock(), new HttpAuthenticatorMock() );
  57. request = new WebdavRequestImpl( new MockHttpServletRequest(), null );
  58. }
  59. @Test
  60. public void testAttachSession()
  61. throws Exception
  62. {
  63. assertNull( request.getDavSession() );
  64. sessionProvider.attachSession( request );
  65. assertNotNull( request.getDavSession() );
  66. }
  67. @Test
  68. public void testReleaseSession()
  69. throws Exception
  70. {
  71. assertNull( request.getDavSession() );
  72. sessionProvider.attachSession( request );
  73. assertNotNull( request.getDavSession() );
  74. sessionProvider.releaseSession( request );
  75. assertNull( request.getDavSession() );
  76. }
  77. @SuppressWarnings( "unchecked" )
  78. /*
  79. private class HttpServletRequestMock
  80. implements HttpServletRequest
  81. {
  82. @Override
  83. public long getContentLengthLong()
  84. {
  85. throw new UnsupportedOperationException( "Not supported yet." );
  86. }
  87. @Override
  88. public String changeSessionId()
  89. {
  90. throw new UnsupportedOperationException( "Not supported yet." );
  91. }
  92. @Override
  93. public <T extends HttpUpgradeHandler> T upgrade( Class<T> handlerClass )
  94. throws IOException, ServletException
  95. {
  96. throw new UnsupportedOperationException( "Not supported yet." );
  97. }
  98. @Override
  99. public boolean authenticate( HttpServletResponse httpServletResponse )
  100. throws IOException, ServletException
  101. {
  102. throw new UnsupportedOperationException( "Not supported yet." );
  103. }
  104. @Override
  105. public void login( String s, String s1 )
  106. throws ServletException
  107. {
  108. throw new UnsupportedOperationException( "Not supported yet." );
  109. }
  110. @Override
  111. public void logout()
  112. throws ServletException
  113. {
  114. throw new UnsupportedOperationException( "Not supported yet." );
  115. }
  116. @Override
  117. public Collection<Part> getParts()
  118. throws IOException, ServletException
  119. {
  120. throw new UnsupportedOperationException( "Not supported yet." );
  121. }
  122. @Override
  123. public Part getPart( String s )
  124. throws IOException, ServletException
  125. {
  126. throw new UnsupportedOperationException( "Not supported yet." );
  127. }
  128. @Override
  129. public ServletContext getServletContext()
  130. {
  131. throw new UnsupportedOperationException( "Not supported yet." );
  132. }
  133. @Override
  134. public AsyncContext startAsync()
  135. throws IllegalStateException
  136. {
  137. throw new UnsupportedOperationException( "Not supported yet." );
  138. }
  139. @Override
  140. public AsyncContext startAsync( ServletRequest servletRequest, ServletResponse servletResponse )
  141. throws IllegalStateException
  142. {
  143. throw new UnsupportedOperationException( "Not supported yet." );
  144. }
  145. @Override
  146. public boolean isAsyncStarted()
  147. {
  148. throw new UnsupportedOperationException( "Not supported yet." );
  149. }
  150. @Override
  151. public boolean isAsyncSupported()
  152. {
  153. throw new UnsupportedOperationException( "Not supported yet." );
  154. }
  155. @Override
  156. public AsyncContext getAsyncContext()
  157. {
  158. throw new UnsupportedOperationException( "Not supported yet." );
  159. }
  160. @Override
  161. public DispatcherType getDispatcherType()
  162. {
  163. throw new UnsupportedOperationException( "Not supported yet." );
  164. }
  165. @Override
  166. public Object getAttribute( String arg0 )
  167. {
  168. throw new UnsupportedOperationException( "Not supported yet." );
  169. }
  170. @Override
  171. public Enumeration getAttributeNames()
  172. {
  173. throw new UnsupportedOperationException( "Not supported yet." );
  174. }
  175. @Override
  176. public String getCharacterEncoding()
  177. {
  178. throw new UnsupportedOperationException( "Not supported yet." );
  179. }
  180. @Override
  181. public int getContentLength()
  182. {
  183. throw new UnsupportedOperationException( "Not supported yet." );
  184. }
  185. @Override
  186. public String getContentType()
  187. {
  188. throw new UnsupportedOperationException( "Not supported yet." );
  189. }
  190. @Override
  191. public ServletInputStream getInputStream()
  192. throws IOException
  193. {
  194. throw new UnsupportedOperationException( "Not supported yet." );
  195. }
  196. @Override
  197. public String getLocalAddr()
  198. {
  199. throw new UnsupportedOperationException( "Not supported yet." );
  200. }
  201. @Override
  202. public String getLocalName()
  203. {
  204. throw new UnsupportedOperationException( "Not supported yet." );
  205. }
  206. @Override
  207. public int getLocalPort()
  208. {
  209. throw new UnsupportedOperationException( "Not supported yet." );
  210. }
  211. @Override
  212. public Locale getLocale()
  213. {
  214. throw new UnsupportedOperationException( "Not supported yet." );
  215. }
  216. @Override
  217. public Enumeration getLocales()
  218. {
  219. throw new UnsupportedOperationException( "Not supported yet." );
  220. }
  221. @Override
  222. public String getParameter( String arg0 )
  223. {
  224. throw new UnsupportedOperationException( "Not supported yet." );
  225. }
  226. @Override
  227. public Map getParameterMap()
  228. {
  229. throw new UnsupportedOperationException( "Not supported yet." );
  230. }
  231. @Override
  232. public Enumeration getParameterNames()
  233. {
  234. throw new UnsupportedOperationException( "Not supported yet." );
  235. }
  236. @Override
  237. public String[] getParameterValues( String arg0 )
  238. {
  239. throw new UnsupportedOperationException( "Not supported yet." );
  240. }
  241. @Override
  242. public String getProtocol()
  243. {
  244. throw new UnsupportedOperationException( "Not supported yet." );
  245. }
  246. @Override
  247. public BufferedReader getReader()
  248. throws IOException
  249. {
  250. throw new UnsupportedOperationException( "Not supported yet." );
  251. }
  252. @Override
  253. public String getRealPath( String arg0 )
  254. {
  255. throw new UnsupportedOperationException( "Not supported yet." );
  256. }
  257. @Override
  258. public String getRemoteAddr()
  259. {
  260. throw new UnsupportedOperationException( "Not supported yet." );
  261. }
  262. @Override
  263. public String getRemoteHost()
  264. {
  265. throw new UnsupportedOperationException( "Not supported yet." );
  266. }
  267. @Override
  268. public int getRemotePort()
  269. {
  270. throw new UnsupportedOperationException( "Not supported yet." );
  271. }
  272. @Override
  273. public RequestDispatcher getRequestDispatcher( String arg0 )
  274. {
  275. throw new UnsupportedOperationException( "Not supported yet." );
  276. }
  277. @Override
  278. public String getScheme()
  279. {
  280. return "";
  281. }
  282. @Override
  283. public String getServerName()
  284. {
  285. throw new UnsupportedOperationException( "Not supported yet." );
  286. }
  287. @Override
  288. public int getServerPort()
  289. {
  290. throw new UnsupportedOperationException( "Not supported yet." );
  291. }
  292. @Override
  293. public boolean isSecure()
  294. {
  295. throw new UnsupportedOperationException( "Not supported yet." );
  296. }
  297. @Override
  298. public void removeAttribute( String arg0 )
  299. {
  300. throw new UnsupportedOperationException( "Not supported yet." );
  301. }
  302. @Override
  303. public void setAttribute( String arg0, Object arg1 )
  304. {
  305. throw new UnsupportedOperationException( "Not supported yet." );
  306. }
  307. @Override
  308. public void setCharacterEncoding( String arg0 )
  309. throws UnsupportedEncodingException
  310. {
  311. throw new UnsupportedOperationException( "Not supported yet." );
  312. }
  313. @Override
  314. public String getAuthType()
  315. {
  316. throw new UnsupportedOperationException( "Not supported yet." );
  317. }
  318. @Override
  319. public String getContextPath()
  320. {
  321. return "/";
  322. }
  323. @Override
  324. public Cookie[] getCookies()
  325. {
  326. throw new UnsupportedOperationException( "Not supported yet." );
  327. }
  328. @Override
  329. public long getDateHeader( String arg0 )
  330. {
  331. throw new UnsupportedOperationException( "Not supported yet." );
  332. }
  333. @Override
  334. public String getHeader( String arg0 )
  335. {
  336. return "";
  337. }
  338. @Override
  339. public Enumeration getHeaderNames()
  340. {
  341. throw new UnsupportedOperationException( "Not supported yet." );
  342. }
  343. @Override
  344. public Enumeration getHeaders( String arg0 )
  345. {
  346. throw new UnsupportedOperationException( "Not supported yet." );
  347. }
  348. @Override
  349. public int getIntHeader( String arg0 )
  350. {
  351. throw new UnsupportedOperationException( "Not supported yet." );
  352. }
  353. @Override
  354. public String getMethod()
  355. {
  356. throw new UnsupportedOperationException( "Not supported yet." );
  357. }
  358. @Override
  359. public String getPathInfo()
  360. {
  361. throw new UnsupportedOperationException( "Not supported yet." );
  362. }
  363. @Override
  364. public String getPathTranslated()
  365. {
  366. throw new UnsupportedOperationException( "Not supported yet." );
  367. }
  368. @Override
  369. public String getQueryString()
  370. {
  371. throw new UnsupportedOperationException( "Not supported yet." );
  372. }
  373. @Override
  374. public String getRemoteUser()
  375. {
  376. throw new UnsupportedOperationException( "Not supported yet." );
  377. }
  378. @Override
  379. public String getRequestURI()
  380. {
  381. return "/";
  382. }
  383. @Override
  384. public StringBuffer getRequestURL()
  385. {
  386. throw new UnsupportedOperationException( "Not supported yet." );
  387. }
  388. @Override
  389. public String getRequestedSessionId()
  390. {
  391. throw new UnsupportedOperationException( "Not supported yet." );
  392. }
  393. @Override
  394. public String getServletPath()
  395. {
  396. throw new UnsupportedOperationException( "Not supported yet." );
  397. }
  398. @Override
  399. public HttpSession getSession( boolean arg0 )
  400. {
  401. throw new UnsupportedOperationException( "Not supported yet." );
  402. }
  403. @Override
  404. public HttpSession getSession()
  405. {
  406. throw new UnsupportedOperationException( "Not supported yet." );
  407. }
  408. @Override
  409. public Principal getUserPrincipal()
  410. {
  411. throw new UnsupportedOperationException( "Not supported yet." );
  412. }
  413. @Override
  414. public boolean isRequestedSessionIdFromCookie()
  415. {
  416. throw new UnsupportedOperationException( "Not supported yet." );
  417. }
  418. @Override
  419. public boolean isRequestedSessionIdFromURL()
  420. {
  421. throw new UnsupportedOperationException( "Not supported yet." );
  422. }
  423. @Override
  424. public boolean isRequestedSessionIdFromUrl()
  425. {
  426. throw new UnsupportedOperationException( "Not supported yet." );
  427. }
  428. @Override
  429. public boolean isRequestedSessionIdValid()
  430. {
  431. throw new UnsupportedOperationException( "Not supported yet." );
  432. }
  433. @Override
  434. public boolean isUserInRole( String arg0 )
  435. {
  436. throw new UnsupportedOperationException( "Not supported yet." );
  437. }
  438. }
  439. */
  440. private class ServletAuthenticatorMock
  441. implements ServletAuthenticator
  442. {
  443. @Override
  444. public boolean isAuthenticated( HttpServletRequest arg0, AuthenticationResult arg1 )
  445. throws AuthenticationException, AccountLockedException, MustChangePasswordException
  446. {
  447. return true;
  448. }
  449. @Override
  450. public boolean isAuthorized( HttpServletRequest request, SecuritySession securitySession, String repositoryId,
  451. String permission )
  452. throws AuthorizationException, UnauthorizedException
  453. {
  454. return true;
  455. }
  456. @Override
  457. public boolean isAuthorized( String principal, String repoId, String permission )
  458. throws UnauthorizedException
  459. {
  460. return true;
  461. }
  462. }
  463. private class HttpAuthenticatorMock
  464. extends HttpAuthenticator
  465. {
  466. @Override
  467. public void challenge( HttpServletRequest arg0, HttpServletResponse arg1, String arg2,
  468. AuthenticationException arg3 )
  469. throws IOException
  470. {
  471. //Do nothing
  472. }
  473. @Override
  474. public AuthenticationResult getAuthenticationResult( HttpServletRequest arg0, HttpServletResponse arg1 )
  475. throws AuthenticationException, AccountLockedException, MustChangePasswordException
  476. {
  477. return new AuthenticationResult();
  478. }
  479. @Override
  480. public AuthenticationResult authenticate( AuthenticationDataSource arg0, HttpSession httpSession )
  481. throws AuthenticationException, AccountLockedException, MustChangePasswordException
  482. {
  483. return new AuthenticationResult();
  484. }
  485. @Override
  486. public void authenticate( HttpServletRequest arg0, HttpServletResponse arg1 )
  487. throws AuthenticationException
  488. {
  489. //Do nothing
  490. }
  491. @Override
  492. public SecuritySession getSecuritySession( HttpSession httpSession )
  493. {
  494. return super.getSecuritySession( httpSession );
  495. }
  496. @Override
  497. public User getSessionUser( HttpSession httpSession )
  498. {
  499. return super.getSessionUser( httpSession );
  500. }
  501. @Override
  502. public boolean isAlreadyAuthenticated( HttpSession httpSession )
  503. {
  504. return super.isAlreadyAuthenticated( httpSession );
  505. }
  506. }
  507. }