Guest User

Untitled

a guest
Sep 13th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.63 KB | None | 0 0
  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  *   http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19. package org.apache.sling.auth.core.impl;
  20.  
  21. import java.io.IOException;
  22. import java.io.UnsupportedEncodingException;
  23.  
  24. import javax.servlet.http.HttpServletRequest;
  25. import javax.servlet.http.HttpServletResponse;
  26.  
  27. import org.apache.sling.auth.core.spi.AuthenticationFeedbackHandler;
  28. import org.apache.sling.auth.core.spi.AuthenticationInfo;
  29. import org.jmock.Expectations;
  30. import org.jmock.Mockery;
  31. import org.jmock.integration.junit4.JUnit4Mockery;
  32.  
  33. import junit.framework.TestCase;
  34. import junitx.util.PrivateAccessor;
  35.  
  36. public class SlingAuthenticatorTest extends TestCase {
  37.  
  38.     private final Mockery context = new JUnit4Mockery();
  39.  
  40.     public void test_quoteCookieValue() throws UnsupportedEncodingException {
  41.  
  42.         try {
  43.             SlingAuthenticator.quoteCookieValue(null);
  44.             fail("Expected IllegalArgumentExcepion on null value");
  45.         } catch (IllegalArgumentException iae) {
  46.             // expected
  47.         }
  48.  
  49.         checkQuote("\"", "\"\\\"\"");
  50.         checkQuote("simplevalue", "\"simplevalue\"");
  51.         checkQuote("simple value", "\"simple+value\"");
  52.         checkQuote("[email protected]", "\"email%40address.com\"");
  53.  
  54.         checkQuote("string\ttab", "\"string%09tab\"");
  55.         checkQuote("test中文", "\"test%E4%B8%AD%E6%96%87\"");
  56.  
  57.  
  58.         try {
  59.             SlingAuthenticator.quoteCookieValue("string\rCR");
  60.             fail("Expected IllegalArgumentExcepion on value containing CR");
  61.         } catch (IllegalArgumentException iae) {
  62.             // expected
  63.         }
  64.     }
  65.  
  66.     public void test_unquoteCookieValue() {
  67.  
  68.         assertNull(SlingAuthenticator.unquoteCookieValue(null));
  69.         assertEquals("", SlingAuthenticator.unquoteCookieValue(""));
  70.  
  71.         checkUnQuote("unquoted", "unquoted");
  72.         checkUnQuote("unquoted\"", "unquoted\"");
  73.         checkUnQuote("un\"quoted", "un\"quoted");
  74.  
  75.         checkUnQuote("\"\\\"\"", "\"");
  76.         checkUnQuote("\"simplevalue\"", "simplevalue");
  77.         checkUnQuote("\"simple value\"", "simple value");
  78.         checkUnQuote("\"[email protected]\"", "[email protected]");
  79.  
  80.         checkUnQuote("\"string\ttab\"", "string\ttab");
  81.     }
  82.  
  83.     private void checkQuote(final String value, final String expected) throws UnsupportedEncodingException {
  84.         final String actual = SlingAuthenticator.quoteCookieValue(value);
  85.         assertEquals(expected, actual);
  86.     }
  87.  
  88.     private void checkUnQuote(final String value, final String expected) {
  89.         final String actual = SlingAuthenticator.unquoteCookieValue(value);
  90.         assertEquals(expected, actual);
  91.     }
  92.  
  93.     //SLING-4864
  94.     public void  test_isAnonAllowed() throws Throwable {
  95.         SlingAuthenticator slingAuthenticator = new SlingAuthenticator();
  96.  
  97.         PathBasedHolderCache<AuthenticationRequirementHolder> authRequiredCache = new PathBasedHolderCache<AuthenticationRequirementHolder>();
  98.  
  99.         authRequiredCache.addHolder(new AuthenticationRequirementHolder("/", false, null));
  100.  
  101.         PrivateAccessor.setField(slingAuthenticator, "authRequiredCache", authRequiredCache);
  102.         final HttpServletRequest request = context.mock(HttpServletRequest.class);
  103.         context.checking(new Expectations() {
  104.             {
  105.                 allowing(request).getServletPath();
  106.                 will(returnValue(null));
  107.                 allowing(request).getPathInfo();
  108.                 will(returnValue(null));
  109.                 allowing(request).getServerName();
  110.                 will(returnValue("localhost"));
  111.                 allowing(request).getServerPort();
  112.                 will(returnValue(80));
  113.                 allowing(request).getScheme();
  114.                 will(returnValue("http"));
  115.             }
  116.         });
  117.  
  118.         Boolean allowed = (Boolean) PrivateAccessor.invoke(slingAuthenticator,"isAnonAllowed",  new Class[]{HttpServletRequest.class},new Object[]{request});
  119.         assertTrue(allowed);
  120.     }
  121.  
  122.     /**
  123.      * Test is KO, not working.
  124.      *
  125.      * Issue can be reproduced with the following steps:
  126.      *
  127.      * Create node "/page"
  128.      * Create sibling node "/page1"
  129.      * Define a protection handler for node: "/page"
  130.      *
  131.      * Expected: "/page" has AuthenticationInfo
  132.      *           "/page1" does not have AuthenticationInfo (has anonymous)
  133.      *
  134.      * Actual:  "/page" & "page1" are both having AuthenticationInfo
  135.      *
  136.      * Reason: SlingAuthenticator.java line 726:  if (path.startsWith(holder.path))
  137.      * Warning: The same check is used in 4 more places in code with similar behaviour.
  138.      *
  139.      * @throws Throwable
  140.      */
  141.     public void test_siblingNodeShouldNotHaveAuthenticationInfo() throws Throwable {
  142.         final String AUTH_TYPE = "AUTH_TYPE_TEST";
  143.         final String PROTECTED_PATH = "/test";
  144.         final String REQUEST_NOT_PROTECTED_PATH = "/test2";
  145.  
  146.         SlingAuthenticator slingAuthenticator = new SlingAuthenticator();
  147.  
  148.         PathBasedHolderCache<AbstractAuthenticationHandlerHolder> authRequiredCache = new PathBasedHolderCache<AbstractAuthenticationHandlerHolder>();
  149.         authRequiredCache.addHolder(buildAuthHolderForAuthTypeAndPath(AUTH_TYPE, PROTECTED_PATH));
  150.  
  151.         PrivateAccessor.setField(slingAuthenticator, "authHandlerCache", authRequiredCache);
  152.         final HttpServletRequest request = context.mock(HttpServletRequest.class);
  153.         buildExpectationsForRequestPathAndAuthPath(request, REQUEST_NOT_PROTECTED_PATH, PROTECTED_PATH);
  154.  
  155.         AuthenticationInfo authInfo = (AuthenticationInfo) PrivateAccessor.invoke(slingAuthenticator, "getAuthenticationInfo",
  156.                 new Class[]{HttpServletRequest.class, HttpServletResponse.class}, new Object[]{request, context.mock(HttpServletResponse.class)});
  157.         /**
  158.          * The AUTH TYPE defined aboved should not be used for the path /test2.
  159.          */
  160.         assertFalse(AUTH_TYPE.equals(authInfo.getAuthType()));
  161.     }
  162.  
  163.     /**
  164.      * Test is OK for child node;
  165.      * @throws Throwable
  166.      */
  167.     public void test_childNodeShouldHaveAuthenticationInfo() throws Throwable {
  168.         final String AUTH_TYPE = "AUTH_TYPE_TEST";
  169.         final String PROTECTED_PATH = "/test";
  170.         final String REQUEST_CHILD_NODE = "/test/childnodetest";
  171.  
  172.         SlingAuthenticator slingAuthenticator = new SlingAuthenticator();
  173.  
  174.         PathBasedHolderCache<AbstractAuthenticationHandlerHolder> authRequiredCache = new PathBasedHolderCache<AbstractAuthenticationHandlerHolder>();
  175.         authRequiredCache.addHolder(buildAuthHolderForAuthTypeAndPath(AUTH_TYPE, PROTECTED_PATH));
  176.  
  177.         PrivateAccessor.setField(slingAuthenticator, "authHandlerCache", authRequiredCache);
  178.         final HttpServletRequest request = context.mock(HttpServletRequest.class);
  179.         buildExpectationsForRequestPathAndAuthPath(request, REQUEST_CHILD_NODE, PROTECTED_PATH);
  180.  
  181.         AuthenticationInfo authInfo = (AuthenticationInfo) PrivateAccessor.invoke(slingAuthenticator, "getAuthenticationInfo",
  182.                 new Class[]{HttpServletRequest.class, HttpServletResponse.class}, new Object[]{request, context.mock(HttpServletResponse.class)});
  183.         /**
  184.          * The AUTH TYPE defined aboved should  be used for the path /test and his children: eg /test/childnode.
  185.          */
  186.         assertTrue(AUTH_TYPE.equals(authInfo.getAuthType()));
  187.     }
  188.  
  189.     /**
  190.      * Mocks the request to accept method calls on path;
  191.      *
  192.      * @param request              http request
  193.      * @param requestPath          path in the http request
  194.      * @param authProtectedPath    path protected by the auth handler
  195.      */
  196.     private void buildExpectationsForRequestPathAndAuthPath(final HttpServletRequest request,
  197.                                                             final String requestPath,
  198.                                                             final String authProtectedPath) {
  199.         {
  200.             context.checking(new Expectations() {
  201.                 {
  202.                     allowing(request).getServletPath();
  203.                     will(returnValue(requestPath));
  204.                     allowing(request).getPathInfo();
  205.                     will(returnValue(null));
  206.                     allowing(request).getServerName();
  207.                     will(returnValue("localhost"));
  208.                     allowing(request).getServerPort();
  209.                     will(returnValue(80));
  210.                     allowing(request).getScheme();
  211.                     will(returnValue("http"));
  212.                     allowing(request).getAttribute("path");
  213.                     will(returnValue(requestPath));
  214.                     allowing(request).setAttribute("path", requestPath);
  215.                     allowing(request).setAttribute("path", authProtectedPath);
  216.                 }
  217.             });
  218.         }
  219.     }
  220.  
  221.  
  222.     /**
  223.      * Builds an auth handler for a specific path;
  224.      * @param authType             name of the auth for this path
  225.      * @param authProtectedPath    path protected by the auth handler
  226.      * @return
  227.      */
  228.     private AbstractAuthenticationHandlerHolder buildAuthHolderForAuthTypeAndPath(final String authType, final String authProtectedPath) {
  229.         return new AbstractAuthenticationHandlerHolder(authProtectedPath, null) {
  230.  
  231.             @Override
  232.             protected AuthenticationFeedbackHandler getFeedbackHandler() {
  233.                 return null;
  234.             }
  235.  
  236.             @Override
  237.             protected AuthenticationInfo doExtractCredentials(HttpServletRequest request, HttpServletResponse response) {
  238.                 return new AuthenticationInfo(authType);
  239.             }
  240.  
  241.             @Override
  242.             protected boolean doRequestCredentials(HttpServletRequest request, HttpServletResponse response) throws IOException {
  243.                 return false;
  244.             }
  245.  
  246.             @Override
  247.             protected void doDropCredentials(HttpServletRequest request, HttpServletResponse response) throws IOException {
  248.  
  249.             }
  250.         };
  251.     }
  252.  
  253. }
Advertisement
Add Comment
Please, Sign In to add comment