Search in sources :

Example 16 with TesterContext

use of org.apache.tomcat.unittest.TesterContext in project tomcat by apache.

the class TestJNDIRealm method buildRealm.

private JNDIRealm buildRealm(String password) throws javax.naming.NamingException, NoSuchFieldException, IllegalAccessException, LifecycleException {
    Context context = new TesterContext();
    JNDIRealm realm = new JNDIRealm();
    realm.setContainer(context);
    realm.setUserSearch("");
    // Usually everything is created in create() but that's not the case here
    Field field = JNDIRealm.class.getDeclaredField("singleConnection");
    field.setAccessible(true);
    Field field2 = JNDIRealm.JNDIConnection.class.getDeclaredField("context");
    field2.setAccessible(true);
    field2.set(field.get(realm), mockDirContext(mockSearchResults(password)));
    realm.start();
    return realm;
}
Also used : DirContext(javax.naming.directory.DirContext) Context(org.apache.catalina.Context) InitialDirContext(javax.naming.directory.InitialDirContext) TesterContext(org.apache.tomcat.unittest.TesterContext) Field(java.lang.reflect.Field) TesterContext(org.apache.tomcat.unittest.TesterContext)

Example 17 with TesterContext

use of org.apache.tomcat.unittest.TesterContext in project tomcat by apache.

the class TestJNDIRealm method testErrorRealm.

@Test
public void testErrorRealm() throws Exception {
    Context context = new TesterContext();
    JNDIRealm realm = new JNDIRealm();
    realm.setContainer(context);
    realm.setUserSearch("");
    // Connect to something that will fail
    realm.setConnectionURL("ldap://127.0.0.1:12345");
    realm.start();
    final CountDownLatch latch = new CountDownLatch(3);
    (new Thread(() -> {
        realm.authenticate("foo", "bar");
        latch.countDown();
    })).start();
    (new Thread(() -> {
        realm.authenticate("foo", "bar");
        latch.countDown();
    })).start();
    (new Thread(() -> {
        realm.authenticate("foo", "bar");
        latch.countDown();
    })).start();
    Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
}
Also used : DirContext(javax.naming.directory.DirContext) Context(org.apache.catalina.Context) InitialDirContext(javax.naming.directory.InitialDirContext) TesterContext(org.apache.tomcat.unittest.TesterContext) CountDownLatch(java.util.concurrent.CountDownLatch) TesterContext(org.apache.tomcat.unittest.TesterContext) Test(org.junit.Test)

Example 18 with TesterContext

use of org.apache.tomcat.unittest.TesterContext in project tomcat by apache.

the class TestRealmBase method doRoleTest.

private void doRoleTest(List<String> userRoles, List<String> constraintOneRoles, List<String> constraintTwoRoles, List<String> applicationRoles, boolean expected) throws IOException {
    TesterMapRealm mapRealm = new TesterMapRealm();
    // Configure the security constraints for the resource
    SecurityConstraint constraintOne = new SecurityConstraint();
    if (constraintOneRoles != null) {
        constraintOne.setAuthConstraint(true);
        for (String constraintRole : constraintOneRoles) {
            constraintOne.addAuthRole(constraintRole);
            if (applicationRoles.contains(SecurityConstraint.ROLE_ALL_AUTHENTICATED_USERS)) {
                constraintOne.treatAllAuthenticatedUsersAsApplicationRole();
            }
        }
    }
    SecurityConstraint constraintTwo = new SecurityConstraint();
    if (constraintTwoRoles != null) {
        constraintTwo.setAuthConstraint(true);
        for (String constraintRole : constraintTwoRoles) {
            constraintTwo.addAuthRole(constraintRole);
            if (applicationRoles.contains(SecurityConstraint.ROLE_ALL_AUTHENTICATED_USERS)) {
                constraintTwo.treatAllAuthenticatedUsersAsApplicationRole();
            }
        }
    }
    SecurityConstraint[] constraints = new SecurityConstraint[] { constraintOne, constraintTwo };
    // Set up the mock request and response
    Request request = new Request(null);
    Response response = new TesterResponse();
    Context context = new TesterContext();
    for (String applicationRole : applicationRoles) {
        context.addSecurityRole(applicationRole);
    }
    request.getMappingData().context = context;
    // Configure the users in the Realm
    if (userRoles != null) {
        GenericPrincipal gp = new GenericPrincipal(USER1, userRoles);
        request.setUserPrincipal(gp);
    }
    // Check if user meets constraints
    boolean result = mapRealm.hasResourcePermission(request, response, constraints, null);
    Assert.assertEquals(Boolean.valueOf(expected), Boolean.valueOf(result));
}
Also used : TesterResponse(org.apache.tomcat.unittest.TesterResponse) Response(org.apache.catalina.connector.Response) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) TesterMapRealm(org.apache.catalina.startup.TesterMapRealm) Request(org.apache.catalina.connector.Request) TesterRequest(org.apache.tomcat.unittest.TesterRequest) TesterResponse(org.apache.tomcat.unittest.TesterResponse) TesterContext(org.apache.tomcat.unittest.TesterContext) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint)

Aggregations

TesterContext (org.apache.tomcat.unittest.TesterContext)18 Context (org.apache.catalina.Context)15 Test (org.junit.Test)8 Request (org.apache.catalina.connector.Request)7 TesterRequest (org.apache.tomcat.unittest.TesterRequest)6 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)4 Host (org.apache.catalina.Host)4 TesterMapRealm (org.apache.catalina.startup.TesterMapRealm)4 TesterHost (org.apache.tomcat.unittest.TesterHost)4 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)3 DirContext (javax.naming.directory.DirContext)3 InitialDirContext (javax.naming.directory.InitialDirContext)3 Response (org.apache.catalina.connector.Response)3 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)3 Field (java.lang.reflect.Field)2 Principal (java.security.Principal)2 HashSet (java.util.HashSet)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Store (org.apache.catalina.Store)2