You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by as...@apache.org on 2017/02/24 12:52:44 UTC

svn commit: r1784273 - /sling/trunk/bundles/auth/core/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorTest.java

Author: asanso
Date: Fri Feb 24 12:52:44 2017
New Revision: 1784273

URL: http://svn.apache.org/viewvc?rev=1784273&view=rev
Log:
SLING-6053 - SlingAuthenticator identifies wrong sibling node with AuthenticationInfo 

* applied patch from Miklos Csere . Thanks!!

Modified:
    sling/trunk/bundles/auth/core/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorTest.java

Modified: sling/trunk/bundles/auth/core/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/auth/core/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorTest.java?rev=1784273&r1=1784272&r2=1784273&view=diff
==============================================================================
--- sling/trunk/bundles/auth/core/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorTest.java (original)
+++ sling/trunk/bundles/auth/core/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorTest.java Fri Feb 24 12:52:44 2017
@@ -18,26 +18,30 @@
  */
 package org.apache.sling.auth.core.impl;
 
+import java.io.IOException;
 import java.io.UnsupportedEncodingException;
-
 import javax.servlet.http.HttpServletRequest;
-
+import javax.servlet.http.HttpServletResponse;
+import org.apache.sling.auth.core.spi.AuthenticationFeedbackHandler;
+import org.apache.sling.auth.core.spi.AuthenticationInfo;
 import org.jmock.Expectations;
 import org.jmock.Mockery;
 import org.jmock.integration.junit4.JUnit4Mockery;
-
-import junit.framework.TestCase;
+import org.junit.Assert;
+import org.junit.Ignore;
+import org.junit.Test;
 import junitx.util.PrivateAccessor;
 
-public class SlingAuthenticatorTest extends TestCase {
+public class SlingAuthenticatorTest {
 
     private final Mockery context = new JUnit4Mockery();
 
+    @Test
     public void test_quoteCookieValue() throws UnsupportedEncodingException {
 
         try {
             SlingAuthenticator.quoteCookieValue(null);
-            fail("Expected IllegalArgumentExcepion on null value");
+            Assert.fail("Expected IllegalArgumentExcepion on null value");
         } catch (IllegalArgumentException iae) {
             // expected
         }
@@ -53,16 +57,17 @@ public class SlingAuthenticatorTest exte
 
         try {
             SlingAuthenticator.quoteCookieValue("string\rCR");
-            fail("Expected IllegalArgumentExcepion on value containing CR");
+            Assert.fail("Expected IllegalArgumentExcepion on value containing CR");
         } catch (IllegalArgumentException iae) {
             // expected
         }
     }
 
+    @Test
     public void test_unquoteCookieValue() {
 
-        assertNull(SlingAuthenticator.unquoteCookieValue(null));
-        assertEquals("", SlingAuthenticator.unquoteCookieValue(""));
+        Assert.assertNull(SlingAuthenticator.unquoteCookieValue(null));
+        Assert.assertEquals("", SlingAuthenticator.unquoteCookieValue(""));
 
         checkUnQuote("unquoted", "unquoted");
         checkUnQuote("unquoted\"", "unquoted\"");
@@ -76,17 +81,8 @@ public class SlingAuthenticatorTest exte
         checkUnQuote("\"string\ttab\"", "string\ttab");
     }
 
-    private void checkQuote(final String value, final String expected) throws UnsupportedEncodingException {
-        final String actual = SlingAuthenticator.quoteCookieValue(value);
-        assertEquals(expected, actual);
-    }
-
-    private void checkUnQuote(final String value, final String expected) {
-        final String actual = SlingAuthenticator.unquoteCookieValue(value);
-        assertEquals(expected, actual);
-    }
-
     //SLING-4864
+    @Test
     public void  test_isAnonAllowed() throws Throwable {
         SlingAuthenticator slingAuthenticator = new SlingAuthenticator();
 
@@ -112,7 +108,150 @@ public class SlingAuthenticatorTest exte
         });
 
         Boolean allowed = (Boolean) PrivateAccessor.invoke(slingAuthenticator,"isAnonAllowed",  new Class[]{HttpServletRequest.class},new Object[]{request});
-        assertTrue(allowed);
+        Assert.assertTrue(allowed);
+    }
+
+
+    /**
+     * Test is OK for child node;
+     * @throws Throwable
+     */
+    @Test
+    public void test_childNodeShouldHaveAuthenticationInfo() throws Throwable {
+        final String AUTH_TYPE = "AUTH_TYPE_TEST";
+        final String PROTECTED_PATH = "/content/en/test";
+        final String REQUEST_CHILD_NODE = "/content/en/test/childnodetest";
+
+        SlingAuthenticator slingAuthenticator = new SlingAuthenticator();
+
+        PathBasedHolderCache<AbstractAuthenticationHandlerHolder> authRequiredCache = new PathBasedHolderCache<AbstractAuthenticationHandlerHolder>();
+        authRequiredCache.addHolder(buildAuthHolderForAuthTypeAndPath(AUTH_TYPE, PROTECTED_PATH));
+
+        PrivateAccessor.setField(slingAuthenticator, "authHandlerCache", authRequiredCache);
+        final HttpServletRequest request = context.mock(HttpServletRequest.class);
+        buildExpectationsForRequestPathAndAuthPath(request, REQUEST_CHILD_NODE, PROTECTED_PATH);
+
+        AuthenticationInfo authInfo = (AuthenticationInfo) PrivateAccessor.invoke(slingAuthenticator, "getAuthenticationInfo",
+                new Class[]{HttpServletRequest.class, HttpServletResponse.class}, new Object[]{request, context.mock(HttpServletResponse.class)});
+        /**
+         * The AUTH TYPE defined aboved should  be used for the path /test and his children: eg /test/childnode.
+         */
+        Assert.assertTrue(AUTH_TYPE.equals(authInfo.getAuthType()));
+    }
+
+
+    /**
+     * JIRA: SLING-6053
+     * Issue can be reproduced with the following steps:
+     *
+     * Create node "/page"
+     * Create sibling node "/page1"
+     * Define an auth handler for node: "/page"
+     *
+     * Expected: "/page" has AuthenticationInfo
+     *           "/page1" does not have AuthenticationInfo (has anonymous)
+     *
+     * Actual:  "/page" & "page1" are both having AuthenticationInfo
+     *
+     *
+     * @throws Throwable
+     */
+    @Test
+    @Ignore
+    public void test_siblingNodeShouldNotHaveAuthenticationInfo() throws Throwable {
+        final String AUTH_TYPE = "AUTH_TYPE_TEST";
+        final String PROTECTED_PATH = "/content/en/test";
+        final String REQUEST_NOT_PROTECTED_PATH = "/content/en/test2";
+
+        SlingAuthenticator slingAuthenticator = new SlingAuthenticator();
+
+        PathBasedHolderCache<AbstractAuthenticationHandlerHolder> authRequiredCache = new PathBasedHolderCache<AbstractAuthenticationHandlerHolder>();
+        authRequiredCache.addHolder(buildAuthHolderForAuthTypeAndPath(AUTH_TYPE, PROTECTED_PATH));
+
+        PrivateAccessor.setField(slingAuthenticator, "authHandlerCache", authRequiredCache);
+        final HttpServletRequest request = context.mock(HttpServletRequest.class);
+        buildExpectationsForRequestPathAndAuthPath(request, REQUEST_NOT_PROTECTED_PATH, PROTECTED_PATH);
+
+        AuthenticationInfo authInfo = (AuthenticationInfo) PrivateAccessor.invoke(slingAuthenticator, "getAuthenticationInfo",
+                new Class[]{HttpServletRequest.class, HttpServletResponse.class}, new Object[]{request, context.mock(HttpServletResponse.class)});
+        /**
+         * The AUTH TYPE defined aboved should not be used for the path /test2.
+         */
+        Assert.assertFalse(AUTH_TYPE.equals(authInfo.getAuthType()));
     }
 
+    //---------------------------- PRIVATE METHODS -----------------------------
+
+    /**
+     * Mocks the request to accept method calls on path;
+     *
+     * @param request              http request
+     * @param requestPath          path in the http request
+     * @param authProtectedPath    path protected by the auth handler
+     */
+    private void buildExpectationsForRequestPathAndAuthPath(final HttpServletRequest request,
+            final String requestPath,
+            final String authProtectedPath) {
+        {
+            context.checking(new Expectations() {
+                {
+                    allowing(request).getServletPath();
+                    will(returnValue(requestPath));
+                    allowing(request).getPathInfo();
+                    will(returnValue(null));
+                    allowing(request).getServerName();
+                    will(returnValue("localhost"));
+                    allowing(request).getServerPort();
+                    will(returnValue(80));
+                    allowing(request).getScheme();
+                    will(returnValue("http"));
+                    allowing(request).getAttribute("path");
+                    will(returnValue(requestPath));
+                    allowing(request).setAttribute("path", requestPath);
+                    allowing(request).setAttribute("path", authProtectedPath);
+                }
+            });
+        }
+    }
+
+    /**
+     * Builds an auth handler for a specific path;
+     * @param authType             name of the auth for this path
+     * @param authProtectedPath    path protected by the auth handler
+     * @return AbstractAuthenticationHandlerHolder with only an AuthenticationInfo
+     */
+    private AbstractAuthenticationHandlerHolder buildAuthHolderForAuthTypeAndPath(final String authType, final String authProtectedPath) {
+        return new AbstractAuthenticationHandlerHolder(authProtectedPath, null) {
+
+            @Override
+            protected AuthenticationFeedbackHandler getFeedbackHandler() {
+                return null;
+            }
+
+            @Override
+            protected AuthenticationInfo doExtractCredentials(HttpServletRequest request, HttpServletResponse response) {
+                return new AuthenticationInfo(authType);
+            }
+
+            @Override
+            protected boolean doRequestCredentials(HttpServletRequest request, HttpServletResponse response) throws IOException {
+                return false;
+            }
+
+            @Override
+            protected void doDropCredentials(HttpServletRequest request, HttpServletResponse response) throws IOException {
+
+            }
+        };
+    }
+
+    private void checkQuote(final String value, final String expected) throws UnsupportedEncodingException {
+        final String actual = SlingAuthenticator.quoteCookieValue(value);
+        Assert.assertEquals(expected, actual);
+    }
+
+    private void checkUnQuote(final String value, final String expected) {
+        final String actual = SlingAuthenticator.unquoteCookieValue(value);
+        Assert.assertEquals(expected, actual);
+    }
 }