This is an automated email from the ASF dual-hosted git repository. remm pushed a commit to branch 10.1.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/10.1.x by this push: new 159a47af3f Expand memory realm to include testing for the lockout realm 159a47af3f is described below commit 159a47af3fc25400021eed61dc26be60867f222f Author: remm <r...@apache.org> AuthorDate: Mon Jun 24 13:58:52 2024 +0200 Expand memory realm to include testing for the lockout realm This should cover a very common Tomcat configuration. --- test/org/apache/catalina/realm/TestJAASRealm.java | 1 + .../org/apache/catalina/realm/TestMemoryRealm.java | 53 +++++++++++++++++++--- .../catalina/users/TestMemoryUserDatabase.java | 11 +---- 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/test/org/apache/catalina/realm/TestJAASRealm.java b/test/org/apache/catalina/realm/TestJAASRealm.java index eeaa89d589..8cee07d7dc 100644 --- a/test/org/apache/catalina/realm/TestJAASRealm.java +++ b/test/org/apache/catalina/realm/TestJAASRealm.java @@ -45,6 +45,7 @@ public class TestJAASRealm extends TomcatBaseTest { try (PrintWriter writer = new PrintWriter(loginConfFile)) { writer.write(CONFIG); } + addDeleteOnTearDown(loginConfFile); JAASRealm jaasRealm = new JAASRealm(); jaasRealm.setAppName("CustomLogin"); diff --git a/test/org/apache/catalina/realm/TestMemoryRealm.java b/test/org/apache/catalina/realm/TestMemoryRealm.java index 567277a97f..9ba71d268f 100644 --- a/test/org/apache/catalina/realm/TestMemoryRealm.java +++ b/test/org/apache/catalina/realm/TestMemoryRealm.java @@ -16,23 +16,64 @@ */ package org.apache.catalina.realm; +import java.io.File; +import java.io.PrintWriter; import java.security.Principal; import org.junit.Assert; import org.junit.Test; -public class TestMemoryRealm { +import org.apache.catalina.Context; +import org.apache.catalina.startup.Tomcat; +import org.apache.catalina.startup.TomcatBaseTest; + +public class TestMemoryRealm extends TomcatBaseTest { + + public static final String CONFIG = "<?xml version=\"1.0\" ?>" + + "<tomcat-users xmlns=\"http://tomcat.apache.org/xml\"" + + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + + " xsi:schemaLocation=\"http://tomcat.apache.org/xml/tomcat-users.xsd\"" + + " version=\"1.0\">" + + "<role rolename=\"testrole\" />" + + "<group groupname=\"testgroup\" />" + + "<user username=\"admin\" password=\"sekr3t\" roles=\"testrole, otherrole\" groups=\"testgroup, othergroup\" />" + + "</tomcat-users>"; - /** - * Unknown user triggers NPE. - */ @Test - public void testBug56246() { + public void testRealmWithLockout() throws Exception { + + File configFile = new File(getTemporaryDirectory(), "tomcat-users-mr.xml"); + try (PrintWriter writer = new PrintWriter(configFile)) { + writer.write(CONFIG); + } + addDeleteOnTearDown(configFile); + MemoryRealm memoryRealm = new MemoryRealm(); memoryRealm.setCredentialHandler(new MessageDigestCredentialHandler()); + memoryRealm.setPathname(configFile.getAbsolutePath()); + LockOutRealm lockout = new LockOutRealm(); + lockout.addRealm(memoryRealm); - Principal p = memoryRealm.authenticate("foo", "bar"); + // LockOutRealm needs full lifecycle + Tomcat tomcat = getTomcatInstance(); + Context context = tomcat.addContext("/realmtest", null); + context.setRealm(lockout); + tomcat.start(); + Principal p = lockout.authenticate("foo", "bar"); Assert.assertNull(p); + p = lockout.authenticate("admin", "sekr3t"); + Assert.assertNotNull(p); + p = lockout.authenticate("admin", "bla"); + Assert.assertNull(p); + p = lockout.authenticate("admin", "bla"); + p = lockout.authenticate("admin", "bla"); + p = lockout.authenticate("admin", "bla"); + p = lockout.authenticate("admin", "bla"); + // Verify that lockout is now in place after 5 failures + p = lockout.authenticate("admin", "sekr3t"); + Assert.assertNull(p); + } + } diff --git a/test/org/apache/catalina/users/TestMemoryUserDatabase.java b/test/org/apache/catalina/users/TestMemoryUserDatabase.java index 900161e5f3..981796cf06 100644 --- a/test/org/apache/catalina/users/TestMemoryUserDatabase.java +++ b/test/org/apache/catalina/users/TestMemoryUserDatabase.java @@ -37,6 +37,7 @@ import org.apache.catalina.Group; import org.apache.catalina.Role; import org.apache.catalina.User; import org.apache.catalina.realm.GenericPrincipal; +import org.apache.catalina.realm.TestMemoryRealm; import org.apache.catalina.realm.UserDatabaseRealm; public class TestMemoryUserDatabase { @@ -49,15 +50,7 @@ public class TestMemoryUserDatabase { throws Exception { try(BufferedWriter out = new BufferedWriter(new FileWriter(TEST_FILE))) { - out.write("<?xml version=\"1.0\" ?>" - + "<tomcat-users xmlns=\"http://tomcat.apache.org/xml\"" - + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" - + " xsi:schemaLocation=\"http://tomcat.apache.org/xml/tomcat-users.xsd\"" - + " version=\"1.0\">" - + "<role rolename=\"testrole\" />" - + "<group groupname=\"testgroup\" />" - + "<user username=\"admin\" password=\"sekr3t\" roles=\"testrole, otherrole\" groups=\"testgroup, othergroup\" />" - + "</tomcat-users>"); + out.write(TestMemoryRealm.CONFIG); } db = new MemoryUserDatabase(); --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org