You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by mi...@apache.org on 2019/08/12 14:08:38 UTC
[tomcat] 04/04: Add proper tests in TestStandardWrapper
This is an automated email from the ASF dual-hosted git repository.
michaelo pushed a commit to branch BZ-63636/tomcat-9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit 01e323350c17e850b9d6e297e7fdd480c65e02cf
Author: Michael Osipov <mi...@apache.org>
AuthorDate: Mon Aug 12 15:58:47 2019 +0200
Add proper tests in TestStandardWrapper
---
.../apache/catalina/core/TestStandardWrapper.java | 66 ++++++++++++++++++++++
test/org/apache/catalina/realm/TestRealmBase.java | 41 --------------
2 files changed, 66 insertions(+), 41 deletions(-)
diff --git a/test/org/apache/catalina/core/TestStandardWrapper.java b/test/org/apache/catalina/core/TestStandardWrapper.java
index 30f24c1..179cc98 100644
--- a/test/org/apache/catalina/core/TestStandardWrapper.java
+++ b/test/org/apache/catalina/core/TestStandardWrapper.java
@@ -18,6 +18,7 @@ package org.apache.catalina.core;
import java.io.File;
import java.io.IOException;
+import java.security.Principal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
@@ -48,6 +49,7 @@ import org.junit.Test;
import org.apache.catalina.Context;
import org.apache.catalina.Wrapper;
import org.apache.catalina.authenticator.BasicAuthenticator;
+import org.apache.catalina.realm.MessageDigestCredentialHandler;
import org.apache.catalina.startup.TesterMapRealm;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
@@ -235,6 +237,70 @@ public class TestStandardWrapper extends TomcatBaseTest {
Assert.assertTrue(bc.toString().contains("00-OK"));
}
+ @Test
+ public void testRoleMappingInEngine() throws Exception {
+ doTestRoleMapping("engine");
+ }
+
+ @Test
+ public void testRoleMappingInHost() throws Exception {
+ doTestRoleMapping("host");
+ }
+
+ @Test
+ public void testRoleMappingInContext() throws Exception {
+ doTestRoleMapping("context");
+ }
+
+ private void doTestRoleMapping(String realmContainer)
+ throws Exception {
+ // Setup Tomcat instance
+ Tomcat tomcat = getTomcatInstance();
+
+ // No file system docBase required
+ Context ctx = tomcat.addContext("", null);
+ ctx.addRoleMapping("testRole2", "very-complex-role-name");
+ // We won't map testRole3 to "another-very-complex-role-name" to make it fail
+ // intentionally
+
+ Wrapper wrapper = Tomcat.addServlet(ctx, "servlet", TestServlet.class.getName());
+ ctx.addServletMappingDecoded("/", "servlet");
+
+ TesterMapRealm realm = new TesterMapRealm();
+ MessageDigestCredentialHandler ch = new MessageDigestCredentialHandler();
+ ch.setAlgorithm("SHA");
+ realm.setCredentialHandler(ch);
+
+ /* Attach the realm to the appropriate container, but role mapping must succeed always
+ * because it is evaluated at context level later.
+ */
+ if (realmContainer.equals("engine")) {
+ tomcat.getEngine().setRealm(realm);
+ } else if (realmContainer.equals("host")) {
+ tomcat.getHost().setRealm(realm);
+ } else if (realmContainer.equals("context")) {
+ ctx.setRealm(realm);
+ } else {
+ throw new IllegalArgumentException("realmContainer is invalid");
+ }
+
+ realm.addUser("testUser", ch.mutate("testPwd"));
+ realm.addUserRole("testUser", "testRole1");
+ realm.addUserRole("testUser", "very-complex-role-name");
+ realm.addUserRole("testUser", "another-very-complex-role-name");
+
+ tomcat.start();
+
+ Principal p = realm.authenticate("testUser", "testPwd");
+
+ Assert.assertNotNull(p);
+ Assert.assertEquals("testUser", p.getName());
+ Assert.assertTrue(realm.hasRole(wrapper, p, "testRole1"));
+ Assert.assertTrue(realm.hasRole(wrapper, p, "testRole2"));
+ Assert.assertTrue(realm.hasRole(wrapper, p, "very-complex-role-name"));
+ Assert.assertFalse(realm.hasRole(wrapper, p, "testRole3"));
+ }
+
private void doTestSecurityAnnotationsAddServlet(boolean useCreateServlet)
throws Exception {
diff --git a/test/org/apache/catalina/realm/TestRealmBase.java b/test/org/apache/catalina/realm/TestRealmBase.java
index b4d35fb..a2c013d 100644
--- a/test/org/apache/catalina/realm/TestRealmBase.java
+++ b/test/org/apache/catalina/realm/TestRealmBase.java
@@ -791,45 +791,4 @@ public class TestRealmBase {
Assert.assertFalse(mapRealm.hasResourcePermission(
request, response, constraintsDelete, null));
}
-
- @Test
- public void testRoleMapping() throws Exception {
- Context context = new TesterContext() {
- private Map<String, String> roleMapping = new HashMap<>();
-
- public void addRoleMapping(String role, String link) {
- roleMapping.put(role, link);
- }
-
- @Override
- public String findRoleMapping(String role) {
- return roleMapping.get(role);
- }
- };
-
- context.addRoleMapping(ROLE2, "very-complex-role-name");
- // We won't map ROLE3 to "another-very-complex-role-name" to make it fail
- // intentionally
-
- TesterMapRealm realm = new TesterMapRealm();
- MessageDigestCredentialHandler ch = new MessageDigestCredentialHandler();
- ch.setAlgorithm("SHA");
- realm.setCredentialHandler(ch);
- realm.setContainer(context);
- realm.start();
-
- realm.addUser(USER1, PWD_SHA);
- realm.addUserRole(USER1, ROLE1);
- realm.addUserRole(USER1, "very-complex-role-name");
- realm.addUserRole(USER1, "another-very-complex-role-name");
-
- Principal p = realm.authenticate(USER1, PWD);
-
- Assert.assertNotNull(p);
- Assert.assertEquals(USER1, p.getName());
- Assert.assertTrue(realm.hasRole(null, p, ROLE1));
- Assert.assertTrue(realm.hasRole(null, p, ROLE2));
- Assert.assertTrue(realm.hasRole(null, p, "very-complex-role-name"));
- Assert.assertFalse(realm.hasRole(null, p, ROLE3));
- }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org