Skip to content

Commit

Permalink
Add tests of new JACC APIs, jakartaee/authorization#113
Browse files Browse the repository at this point in the history
Signed-off-by: Scott M Stark <starksm64@gmail.com>
  • Loading branch information
starksm64 committed Apr 21, 2022
1 parent fb06e30 commit 9246eb8
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 32 deletions.
71 changes: 47 additions & 24 deletions src/com/sun/ts/tests/jacc/provider/TSPolicy.java
Expand Up @@ -36,6 +36,8 @@

import jakarta.security.jacc.EJBMethodPermission;
import jakarta.security.jacc.EJBRoleRefPermission;
import jakarta.security.jacc.PolicyConfiguration;
import jakarta.security.jacc.PolicyConfigurationFactory;
import jakarta.security.jacc.PolicyContext;
import jakarta.security.jacc.WebResourcePermission;
import jakarta.security.jacc.WebRoleRefPermission;
Expand Down Expand Up @@ -187,6 +189,31 @@ public boolean implies(ProtectionDomain domain, Permission permission) {
}
}

// If there is a PolicyContext.getContextID, verify new getPolicyConfiguration() methods work
String contextId = PolicyContext.getContextID();
if(contextId != null) {
try {
// Should be non-null PolicyConfiguration
PolicyConfigurationFactory pcf = PolicyConfigurationFactory.getPolicyConfigurationFactory();
PolicyConfiguration pc = pcf.getPolicyConfiguration();
if(pc != null) {
logger.log(Level.INFO, "PolicyConfigurationFactory.getPolicyConfiguration() : PASSED");
} else {
logger.log(Level.INFO, "PolicyConfigurationFactory.getPolicyConfiguration() : FAILED");
}
// Should be non-null PolicyConfiguration and match no-arg getPolicyConfiguration()
PolicyConfiguration pc2 = pcf.getPolicyConfiguration(contextId);
if(pc2 == null || !pc.equals(pc2)) {
logger.log(Level.INFO, "PolicyConfigurationFactory.getPolicyConfiguration(String) : FAILED");
} else {
logger.log(Level.INFO, "PolicyConfigurationFactory.getPolicyConfiguration(String) : PASSED");
}

} catch (Exception e) {
logger.log(Level.INFO, "PolicyConfigurationFactory.getPolicyConfiguration() : FAILED");
}
}

return policy.implies(domain, permission);
}

Expand Down Expand Up @@ -311,18 +338,16 @@ public static void setTSLogger(TSLogger lgr) {
private void policyContextKey1() {
try {
// Get HttpServletRequest object
Object o = PolicyContext
.getContext("jakarta.servlet.http.HttpServletRequest");
if (o instanceof jakarta.servlet.http.HttpServletRequest) {
logger.log(Level.INFO, "PolicyContext.getContext() " + "test passed for"
+ "jakarta.servlet.http.HttpServletRequest");
logger.log(Level.INFO, "PolicyContextKey1: PASSED");
} else {
logger.log(Level.INFO,
"PolicyContext.getContext()" + "returned incorrect value for key "
+ "jakarta.servlet.http.HttpServletRequest");
logger.log(Level.INFO, "PolicyContextKey1: FAILED");
}
jakarta.servlet.http.HttpServletRequest ctx = PolicyContext
.getContext("jakarta.servlet.http.HttpServletRequest");
logger.log(Level.INFO, "PolicyContext.getContext() " + "test passed for"
+ "jakarta.servlet.http.HttpServletRequest");
logger.log(Level.INFO, "PolicyContextKey1: PASSED");
} catch (ClassCastException e) {
logger.log(Level.INFO,
"PolicyContext.getContext()" + "returned incorrect value for key "
+ "jakarta.servlet.http.HttpServletRequest");
logger.log(Level.SEVERE, "PolicyContextKey1: FAILED");
} catch (Exception e) {
logger.log(Level.SEVERE, "PolicyContextKey1: FAILED");
}
Expand All @@ -342,18 +367,16 @@ private void policyContextKey1() {
private void policyContextKey3() {
try {
// Get Subject
Object o = PolicyContext
.getContext("javax.security.auth.Subject.container");
if (o instanceof javax.security.auth.Subject) {
logger.log(Level.INFO, "PolicyContext.getContext() " + "test passed for"
+ "javax.security.auth.Subject.container");
logger.log(Level.INFO, "PolicyContextKey3: PASSED");
} else {
logger.log(Level.INFO,
"PolicyContext.getContext()" + "returned incorrect value for key "
+ "javax.security.auth.Subject.container");
logger.log(Level.INFO, "PolicyContextKey3: FAILED");
}
javax.security.auth.Subject subject = PolicyContext
.getContext("javax.security.auth.Subject.container");
logger.log(Level.INFO, "PolicyContext.getContext() " + "test passed for"
+ "javax.security.auth.Subject.container");
logger.log(Level.INFO, "PolicyContextKey3: PASSED");
} catch (ClassCastException e) {
logger.log(Level.INFO,
"PolicyContext.getContext()" + "returned incorrect value for key "
+ "javax.security.auth.Subject.container");
logger.log(Level.INFO, "PolicyContextKey3: FAILED");
} catch (Exception e) {
logger.log(Level.SEVERE, "PolicyContextKey3: FAILED");
}
Expand Down
Expand Up @@ -27,6 +27,7 @@

import jakarta.security.jacc.PolicyConfiguration;
import jakarta.security.jacc.PolicyConfigurationFactory;
import jakarta.security.jacc.PolicyContext;
import jakarta.security.jacc.PolicyContextException;

/**
Expand Down Expand Up @@ -84,7 +85,7 @@ public TSPolicyConfigurationFactoryImpl() throws PolicyContextException {
* be thread safe.
* <P>
*
* @param contextID
* @param contextId
* A String identifying the policy context whose PolicyConfiguration
* interface is to be returned. The value passed to this parameter
* must not be null.
Expand Down Expand Up @@ -187,7 +188,7 @@ public static PolicyConfigurationFactory getPolicyConfigurationFactory()
* "inService" in the Policy provider associated with the factory.
* <P>
*
* @param contextID
* @param contextId
* A string identifying a policy context
*
* @return true if the identified policy context exists within the provider
Expand Down Expand Up @@ -222,13 +223,31 @@ public boolean inService(String contextId) throws PolicyContextException {
}

public PolicyConfiguration getPolicyConfiguration(String contextID){
return null;
if (lgr.isLoggable(Level.FINER)) {
lgr.entering("PolicyConfigurationFactoryImpl", "getPolicyConfiguration(String)");
}
PolicyConfiguration polConf = pcFactory.getPolicyConfiguration(contextID);
lgr.log(Level.INFO,
"PolicyConfigurationFactory.getPolicyConfiguration(String) invoked");
return polConf;
}

public PolicyConfiguration getPolicyConfiguration(){
return null;
}
String contextId = PolicyContext.getContextID();
PolicyConfiguration polConf = null;
// check if we can invoke method
if (lgr.isLoggable(Level.FINER)) {
lgr.entering("PolicyConfigurationFactoryImpl", "getPolicyConfiguration()");
}
polConf = pcFactory.getPolicyConfiguration(contextId);
lgr.log(Level.INFO,
"PolicyConfigurationFactory.getPolicyConfiguration(String) invoked");
lgr.log(Level.FINER,
"Getting PolicyConfiguration object with id = " + contextId);
policyConfiguration = (TSPolicyConfigurationImpl) polConf;

return polConf;
}

private static void getTSLogger() {
if (lgr != null)
Expand Down
Expand Up @@ -940,15 +940,15 @@ public static String stuffData(String inputStr) {
}

public PermissionCollection getExcludedPermissions(){
return null;
return policyConfiguration.getExcludedPermissions();
}

public PermissionCollection getUncheckedPermissions(){
return null;
return policyConfiguration.getUncheckedPermissions();
}

public Map<String, PermissionCollection> getPerRolePermissions(){
return null;
return policyConfiguration.getPerRolePermissions();
}

private void assertIsInserviceState(String callingMethod) {
Expand Down

0 comments on commit 9246eb8

Please sign in to comment.