java2sectest.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 546 行 · 第 1/2 页
JAVA
546 行
System.setSecurityManager(null);
if (System.getSecurityManager() == null) {
System.out.println("Security Manager is successfully disabled.");
} else {
System.out.println("Security Manager is still enabled");
}
}
System.out.println("\ntestDoPrivilegedFailure() ends\n\n");
}
}
/**
* testAccessControlContextFailure
*/
public void testAccessControlContextFailure() throws Exception {
Java2SecTest.testResult = "testAccessControlContextFailure failed.";
SecurityManager oldSM = null;
String expectedString = "This line is from private.txt.";
System.out.println("\ntestAccessControlContextFailure() begins");
// Check whether the security is enable or not.
// If it is not enabled, turn it on
oldSM = System.getSecurityManager();
if (oldSM != null) {
System.out.println("\nSecurity Manager is enabled.");
} else {
System.out.println("\nSecurity Manager is disabled.");
System.out.println("Enabling the default Java Security Manager");
System.setSecurityManager(new SecurityManager());
}
// Run test with AccessController.doPrivilege
Action dp = new Action("private/private.txt");
MorePermissionAccessControlContext mp = new MorePermissionAccessControlContext(dp, false);
LessPermissionAccessControlContext lp = new LessPermissionAccessControlContext(mp, true);
try {
lp.takeAction();
} catch (Exception e) {
// Verify the test result
assertTrue("It is not the security exception.",
(e instanceof java.security.AccessControlException));
} finally {
// Disable security manager if it is enabled by this testcsae
if (System.getSecurityManager() != null && oldSM == null) {
System.setSecurityManager(null);
if (System.getSecurityManager() == null) {
System.out.println("Security Manager is successfully disabled.");
} else {
System.out.println("Security Manager is still enabled");
}
}
System.out.println("\ntestAccessControlContextFailure() ends\n\n");
}
}
// 2 begins
/**
* testPrivilegedExceptionActionSuccessed
*/
public void testPrivilegedExceptionSuccessed() throws Exception {
Java2SecTest.testResult = "testPrivielgedExceptionSuccessed failed";
SecurityManager oldSM = null;
String expectedString = "This line is from private.txt.";
System.out.println("\ntestPrivilegedExceptionActionSuccessed() begins");
// Check whether the security is enable or not.
// If it is not enabled, turn it on
oldSM = System.getSecurityManager();
if (oldSM != null) {
System.out.println("\nSecurity Manager is enabled.");
} else {
System.out.println("\nSecurity Manager is disabled.");
System.out.println("Enabling the default Java Security Manager");
System.setSecurityManager(new SecurityManager());
}
// Run test with AccessController.doPrivilege
Action dp = new Action("private/private.txt");
MorePermissionPrivilegedExceptionAction mp =
new MorePermissionPrivilegedExceptionAction(dp, true);
LessPermissionPrivilegedExceptionAction lp =
new LessPermissionPrivilegedExceptionAction(mp, false);
lp.takeAction();
// Disable security manager if it is enabled by this testcsae
if (System.getSecurityManager() != null && oldSM == null) {
System.setSecurityManager(null);
if (System.getSecurityManager() == null) {
System.out.println("Security Manager is successfully disabled.");
} else {
System.out.println("Security Manager is still enabled");
}
}
// Remove extra characters within the result string
testResult = testResult.replaceAll("\\r", "");
testResult = testResult.replaceAll("\\n", "");
System.out.println("testDoPrivilege's result string is " + testResult);
// Verify the test result by comparing the test result with expected string
assertTrue("The string contents do not match.",
expectedString.equalsIgnoreCase(testResult));
System.out.println("\ntestDoPrivilegeSuccessed() ends\n\n");
}
/**
* testPrivilegedExceptionActionFailure
*/
public void testPrivilegedExceptionActionFailure() throws Exception {
Java2SecTest.testResult = "testPrivilegedExceptionActionFailure failed.";
SecurityManager oldSM = null;
String expectedString = "This line is from private.txt.";
System.out.println("\ntestPrivilegedExceptionActionFailure() begins");
// Check whether the security is enable or not.
// If it is not enabled, turn it on
oldSM = System.getSecurityManager();
if (oldSM != null) {
System.out.println("\nSecurity Manager is enabled.");
} else {
System.out.println("\nSecurity Manager is disabled.");
System.out.println("Enabling the default Java Security Manager");
System.setSecurityManager(new SecurityManager());
}
// Run test with AccessController.doPrivilege
Action dp = new Action("private/private.txt");
MorePermissionPrivilegedExceptionAction mp =
new MorePermissionPrivilegedExceptionAction(dp, false);
LessPermissionPrivilegedExceptionAction lp =
new LessPermissionPrivilegedExceptionAction(mp, true);
try {
mp.takeAction();
} catch (Exception e) {
// Verify the test result
assertTrue("It is not the security exception.",
(e instanceof java.security.PrivilegedActionException));
} finally {
// Disable security manager if it is enabled by this testcsae
if (System.getSecurityManager() != null && oldSM == null) {
System.setSecurityManager(null);
if (System.getSecurityManager() == null) {
System.out.println("Security Manager is successfully disabled.");
} else {
System.out.println("Security Manager is still enabled");
}
}
System.out.println("\ntestPrivilegedExceptionActionFailure() ends\n\n");
}
}
/**
* testCheckPermissionAllowed
*/
public void testCheckPermissionAllowed() throws Exception {
Java2SecTest.testResult = "testCheckPermissionAllowed failed.";
SecurityManager oldSM = null;
System.out.println("\ntestCheckPermissionAllowed() begins.\n");
boolean allowed = false;
String fileName = "public/public.txt";
oldSM = System.getSecurityManager();
if (oldSM != null) {
System.out.println("\nSecurity Manager is enabled.");
} else {
System.out.println("\nSecurity Manager is disabled.");
System.out.println("Enabling the default Java Security Manager");
System.setSecurityManager(new SecurityManager());
}
try {
// Print out maven's base,build, and test direcotories
String baseDir = AbstractTestCase.basedir;
System.out.println("basedir => " + baseDir);
// Convert the \ (back slash) to / (forward slash)
String baseDirM = baseDir.replace('\\', '/');
System.out.println("baseDirM => " + baseDirM);
String fs = "/";
// Build the file URL
String fileURL = baseDirM + fs + "test-resources" + fs + "java2sec" + fs + fileName;
Permission perm = new java.io.FilePermission(fileURL, "read");
AccessController.checkPermission(perm);
allowed = true;
} catch (Exception e) {
if (e instanceof AccessControlException) {
e.printStackTrace(System.out);
}
} finally {
assertTrue("Accessing to public.txt file is denied; Test failed.", allowed);
// Disable security manager if it is enabled by this testcsae
if (System.getSecurityManager() != null && oldSM == null) {
System.setSecurityManager(null);
if (System.getSecurityManager() == null) {
System.out.println("Security Manager is successfully disabled.");
} else {
System.out.println("Security Manager is still enabled");
}
}
System.out.println("\ntestCheckPermissionAllowed() ends.\n");
}
}
/**
* testCheckPermissionDenied
*/
public void testCheckPermissionDenied() throws Exception {
Java2SecTest.testResult = "testCheckPermissionDenied failed";
SecurityManager oldSM = null;
System.out.println("\ntestCheckPermissionDenied() begins.\n");
boolean denied = true;
String fileName = "private/private.txt";
oldSM = System.getSecurityManager();
if (oldSM != null) {
System.out.println("\nSecurity Manager is enabled.");
} else {
System.out.println("\nSecurity Manager is disabled.");
System.out.println("Enabling the default Java Security Manager");
System.setSecurityManager(new SecurityManager());
}
try {
// Print out maven's base,build, and test direcotories
String baseDir = AbstractTestCase.basedir;
System.out.println("basedir => " + baseDir);
// Convert the \ (back slash) to / (forward slash)
String baseDirM = baseDir.replace('\\', '/');
System.out.println("baseDirM => " + baseDirM);
String fs = "/";
// Build the file URL
String fileURL = baseDirM + fs + "test-resources" + fs + "java2sec" + fs + fileName;
Permission perm = new java.io.FilePermission(fileURL, "read");
AccessController.checkPermission(perm);
denied = false;
} catch (Exception e) {
if (!(e instanceof AccessControlException)) {
denied = false;
}
e.printStackTrace(System.out);
} finally {
assertTrue("Accessing to private.txt file is allowed; Test failed.", denied);
// Disable security manager if it is enabled by this testcsae
if (System.getSecurityManager() != null && oldSM == null) {
System.setSecurityManager(null);
if (System.getSecurityManager() == null) {
System.out.println("Security Manager is successfully disabled.");
} else {
System.out.println("Security Manager is still enabled");
}
}
System.out.println("\ntestCheckPermissionDenied() ends.\n");
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?