You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by py...@apache.org on 2007/04/11 13:52:22 UTC
svn commit: r527458 - in /harmony/enhanced/classlib/trunk/modules:
awt/src/test/api/java/common/org/apache/harmony/awt/AWTPermissionTest.java
luni/src/test/java/org/apache/harmony/luni/tests/java/lang/SecurityManagerTest.java
Author: pyang
Date: Wed Apr 11 04:52:20 2007
New Revision: 527458
URL: http://svn.apache.org/viewvc?view=rev&rev=527458
Log:
Apply patch for HARMONY-3594([classlib][luni] Add test cases for java.lang.SecurityManager)
Added:
harmony/enhanced/classlib/trunk/modules/awt/src/test/api/java/common/org/apache/harmony/awt/AWTPermissionTest.java
Modified:
harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/SecurityManagerTest.java
Added: harmony/enhanced/classlib/trunk/modules/awt/src/test/api/java/common/org/apache/harmony/awt/AWTPermissionTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/awt/src/test/api/java/common/org/apache/harmony/awt/AWTPermissionTest.java?view=auto&rev=527458
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/awt/src/test/api/java/common/org/apache/harmony/awt/AWTPermissionTest.java (added)
+++ harmony/enhanced/classlib/trunk/modules/awt/src/test/api/java/common/org/apache/harmony/awt/AWTPermissionTest.java Wed Apr 11 04:52:20 2007
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.harmony.awt;
+
+import java.awt.AWTPermission;
+import java.awt.Frame;
+import java.awt.Window;
+import java.security.Permission;
+
+import junit.framework.TestCase;
+
+/**
+ * Test case for java.awt.AWTPermission
+ */
+public class AWTPermissionTest extends TestCase {
+
+ SecurityManager originalSM = null;
+
+ MockSecurityManager sm = null;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ originalSM = System.getSecurityManager();
+ sm = new MockSecurityManager();
+ System.setSecurityManager(sm);
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ System.setSecurityManager(originalSM);
+ }
+
+ /**
+ * @tests java.awt.AWTPermission#AWTPermission((String) test case for
+ * "accessEventQueue"
+ */
+ @SuppressWarnings("nls")
+ public void test_checkAwtEventQueueAccess() {
+ try {
+ sm.checkAwtEventQueueAccess();
+ fail("Should throw a SecurityException.");
+ } catch (SecurityException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests java.awt.AWTPermission#AWTPermission((String) test case for
+ * "accessClipboard"
+ */
+ @SuppressWarnings("nls")
+ public void test_checkSystemClipboardAccess() {
+ try {
+ sm.checkSystemClipboardAccess();
+ fail("Should throw a SecurityException.");
+ } catch (SecurityException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests java.awt.AWTPermission#AWTPermission((String) test case for
+ * "showWindowWithoutWarningBanner"
+ */
+ public void test_checkTopLevelWindowLjava_lang_Object() {
+ assertFalse(sm.checkTopLevelWindow(new Window(new Frame())));
+ }
+
+ /**
+ * Sub-SecurityManager to test some actions of AWTPermission.
+ */
+ class MockSecurityManager extends SecurityManager {
+ /**
+ * @see java.lang.SecurityManager#checkPermission(java.security.Permission)
+ */
+ @Override
+ @SuppressWarnings("nls")
+ public void checkPermission(Permission permission) {
+ Permission[] denied = new Permission[] {
+ new AWTPermission("accessEventQueue"),
+ new AWTPermission("accessClipboard"),
+ new AWTPermission("showWindowWithoutWarningBanner") };
+ for (Permission per : denied) {
+ if (null != per && per.implies(permission)) {
+ throw new SecurityException("Denied " + permission);
+ }
+ }
+ }
+ }
+}
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/SecurityManagerTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/SecurityManagerTest.java?view=diff&rev=527458&r1=527457&r2=527458
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/SecurityManagerTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/SecurityManagerTest.java Wed Apr 11 04:52:20 2007
@@ -17,15 +17,30 @@
package org.apache.harmony.luni.tests.java.lang;
import java.io.File;
-import java.security.Security;
-import java.security.SecurityPermission;
+import java.io.FileDescriptor;
+import java.io.FilePermission;
+import java.net.InetAddress;
+import java.net.SocketPermission;
+import java.net.UnknownHostException;
+import java.security.AccessControlContext;
import java.security.AllPermission;
-import tests.support.Support_Exec;
+import java.security.Permission;
+import java.security.ProtectionDomain;
+import java.security.Security;
import junit.framework.TestCase;
+import tests.support.Support_Exec;
+/**
+ * Test case for java.lang.SecurityManager
+ */
public class SecurityManagerTest extends TestCase {
-
+ MutableSecurityManager mutableSM = null;
+
+ MockSecurityManager mockSM = null;
+
+ SecurityManager originalSM = null;
+
/**
* @tests java.lang.SecurityManager#checkPackageAccess(String)
*/
@@ -33,41 +48,44 @@
final String old = Security.getProperty("package.access");
Security.setProperty("package.access", "a.,bbb, c.d.");
- MutableSecurityManager sm = new MutableSecurityManager();
- sm.denyPermission(new RuntimePermission("accessClassInPackage.*"));
-
+ mutableSM
+ .denyPermission(new RuntimePermission("accessClassInPackage.*"));
+
try {
- sm.checkPackageAccess("z.z.z");
- sm.checkPackageAccess("aa");
- sm.checkPackageAccess("bb");
- sm.checkPackageAccess("c");
+ mutableSM.checkPackageAccess("z.z.z");
+ mutableSM.checkPackageAccess("aa");
+ mutableSM.checkPackageAccess("bb");
+ mutableSM.checkPackageAccess("c");
try {
- sm.checkPackageAccess("a");
+ mutableSM.checkPackageAccess("a");
fail("This should throw a SecurityException.");
- } catch (SecurityException ok) {}
-
+ } catch (SecurityException ok) {
+ }
+
try {
- sm.checkPackageAccess("bbb");
+ mutableSM.checkPackageAccess("bbb");
fail("This should throw a SecurityException.");
- } catch (SecurityException ok) {}
+ } catch (SecurityException ok) {
+ }
try {
- sm.checkPackageAccess("c.d.e");
+ mutableSM.checkPackageAccess("c.d.e");
fail("This should throw a SecurityException.");
- } catch (SecurityException ok) {}
-
+ } catch (SecurityException ok) {
+ }
+
Security.setProperty("package.access", "QWERTY");
- sm.checkPackageAccess("a");
- sm.checkPackageAccess("qwerty");
+ mutableSM.checkPackageAccess("a");
+ mutableSM.checkPackageAccess("qwerty");
try {
- sm.checkPackageAccess("QWERTY");
+ mutableSM.checkPackageAccess("QWERTY");
fail("This should throw a SecurityException.");
- } catch (SecurityException ok) {}
+ } catch (SecurityException ok) {
+ }
} finally {
- Security.setProperty("package.access",
- old == null ? "" : old);
+ Security.setProperty("package.access", old == null ? "" : old);
}
}
@@ -78,41 +96,44 @@
final String old = Security.getProperty("package.definition");
Security.setProperty("package.definition", "a.,bbb, c.d.");
- MutableSecurityManager sm = new MutableSecurityManager();
- sm.denyPermission(new RuntimePermission("defineClassInPackage.*"));
-
+ mutableSM
+ .denyPermission(new RuntimePermission("defineClassInPackage.*"));
+
try {
- sm.checkPackageDefinition("z.z.z");
- sm.checkPackageDefinition("aa");
- sm.checkPackageDefinition("bb");
- sm.checkPackageDefinition("c");
+ mutableSM.checkPackageDefinition("z.z.z");
+ mutableSM.checkPackageDefinition("aa");
+ mutableSM.checkPackageDefinition("bb");
+ mutableSM.checkPackageDefinition("c");
try {
- sm.checkPackageDefinition("a");
+ mutableSM.checkPackageDefinition("a");
fail("This should throw a SecurityException.");
- } catch (SecurityException ok) {}
-
+ } catch (SecurityException ok) {
+ }
+
try {
- sm.checkPackageDefinition("bbb");
+ mutableSM.checkPackageDefinition("bbb");
fail("This should throw a SecurityException.");
- } catch (SecurityException ok) {}
+ } catch (SecurityException ok) {
+ }
try {
- sm.checkPackageDefinition("c.d.e");
+ mutableSM.checkPackageDefinition("c.d.e");
fail("This should throw a SecurityException.");
- } catch (SecurityException ok) {}
+ } catch (SecurityException ok) {
+ }
Security.setProperty("package.definition", "QWERTY");
- sm.checkPackageDefinition("a");
- sm.checkPackageDefinition("qwerty");
+ mutableSM.checkPackageDefinition("a");
+ mutableSM.checkPackageDefinition("qwerty");
try {
- sm.checkPackageDefinition("QWERTY");
+ mutableSM.checkPackageDefinition("QWERTY");
fail("This should throw a SecurityException.");
- } catch (SecurityException ok) {}
+ } catch (SecurityException ok) {
+ }
} finally {
- Security.setProperty("package.definition",
- old == null ? "" : old);
+ Security.setProperty("package.definition", old == null ? "" : old);
}
}
@@ -120,11 +141,11 @@
* @tests java.lang.SecurityManager#checkMemberAccess(java.lang.Class, int)
*/
public void test_checkMemberAccessLjava_lang_ClassI() {
- MutableSecurityManager sm = new MutableSecurityManager();
- // enable all but access check
- sm.addPermission(new AllPermission());
- sm.denyPermission(new RuntimePermission("accessDeclaredMembers"));
- System.setSecurityManager(sm);
+ // enable all but one check
+ mutableSM.addPermission(new AllPermission());
+ mutableSM
+ .denyPermission(new RuntimePermission("accessDeclaredMembers"));
+ System.setSecurityManager(mutableSM);
try {
getClass().getDeclaredFields();
@@ -194,5 +215,390 @@
t.start();
t.join();
new SecurityManager().checkAccess(t);
+ }
+
+ /**
+ * @tests {@link java.lang.SecurityManager#checkAccept(String, int)}
+ */
+ @SuppressWarnings("nls")
+ public void test_checkAcceptLjava_lang_String_int() {
+ // enable all but one check
+ mutableSM.addPermission(new AllPermission());
+ mutableSM.denyPermission(new SocketPermission("localhost:1024-",
+ "accept, connect, listen"));
+ System.setSecurityManager(mutableSM);
+ try {
+ mutableSM.checkAccept("localhost", 1024);
+ fail("This should throw a SecurityException.");
+ } catch (SecurityException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.lang.SecurityManager#checkConnect(String, int, Object)}
+ */
+ @SuppressWarnings("nls")
+ public void test_checkConnectLjava_lang_String_int_Ljava_lang_Object() {
+ // enable all but one check
+ mutableSM.addPermission(new AllPermission());
+ mutableSM.denyPermission(new SocketPermission("localhost:1024-",
+ "accept, connect, listen"));
+ System.setSecurityManager(mutableSM);
+ ProtectionDomain pDomain = this.getClass().getProtectionDomain();
+ ProtectionDomain[] pd = { pDomain };
+ AccessControlContext acc = new AccessControlContext(pd);
+ try {
+ mutableSM.checkConnect("localhost", 1024, acc);
+ fail("This should throw a SecurityException.");
+ } catch (SecurityException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.lang.SecurityManager#checkExec(String)}
+ */
+ @SuppressWarnings("nls")
+ public void test_checkExecLjava_lang_String() {
+ // enable all but one check
+ mutableSM.addPermission(new AllPermission());
+ mutableSM
+ .denyPermission(new FilePermission("<<ALL FILES>>", "execute"));
+ System.setSecurityManager(mutableSM);
+ try {
+ mutableSM.checkExec("java");
+ fail("This should throw a SecurityException.");
+ } catch (SecurityException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.lang.SecurityManager#checkExit(int)}
+ */
+ @SuppressWarnings("nls")
+ public void test_checkExit_int() {
+ // enable all but one check
+ mutableSM.addPermission(new AllPermission());
+ mutableSM.denyPermission(new RuntimePermission("exitVM"));
+ System.setSecurityManager(mutableSM);
+ try {
+ mutableSM.checkExit(0);
+ fail("This should throw a SecurityException.");
+ } catch (SecurityException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.lang.SecurityManager#checkLink(String)}
+ */
+ @SuppressWarnings("nls")
+ public void test_checkLinkLjava_lang_String() {
+ // enable all but one check
+ mutableSM.addPermission(new AllPermission());
+ mutableSM.denyPermission(new RuntimePermission("loadLibrary.harmony"));
+ System.setSecurityManager(mutableSM);
+ try {
+ mutableSM.checkLink("harmony");
+ fail("This should throw a SecurityException.");
+ } catch (SecurityException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.lang.SecurityManager#checkListen(int)}
+ */
+ @SuppressWarnings("nls")
+ public void test_checkListen_int() {
+ // enable all but one check
+ mutableSM.addPermission(new AllPermission());
+ mutableSM
+ .denyPermission(new SocketPermission("localhost:80", "listen"));
+ System.setSecurityManager(mutableSM);
+
+ try {
+ mutableSM.checkListen(80);
+ fail("This should throw a SecurityException.");
+ } catch (SecurityException e) {
+ // expected
+ }
+ mutableSM.addPermission(new AllPermission());
+ mutableSM.denyPermission(new SocketPermission("localhost:1024-",
+ "listen"));
+ System.setSecurityManager(mutableSM);
+ try {
+ mutableSM.checkListen(0);
+ fail("This should throw a SecurityException.");
+ } catch (SecurityException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @throws UnknownHostException
+ * @tests {@link java.lang.SecurityManager#checkMulticast(java.net.InetAddress)}
+ */
+ @SuppressWarnings("nls")
+ public void test_checkMulticastLjava_net_InetAddress()
+ throws UnknownHostException {
+ // enable all but one check
+ mutableSM.addPermission(new AllPermission());
+ mutableSM.denyPermission(new SocketPermission(InetAddress.getByName(
+ "localhost").getHostAddress(), "accept,connect"));
+ System.setSecurityManager(mutableSM);
+ try {
+ mutableSM.checkMulticast(InetAddress.getByName("localhost"));
+ fail("This should throw a SecurityException.");
+ } catch (SecurityException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @throws UnknownHostException
+ * @tests {@link java.lang.SecurityManager#checkMulticast(java.net.InetAddress,byte)}
+ */
+ @SuppressWarnings( { "nls", "deprecation" })
+ public void test_checkMulticastLjava_net_InetAddress_int()
+ throws UnknownHostException {
+ // enable all but one check
+ mutableSM.addPermission(new AllPermission());
+ mutableSM.denyPermission(new SocketPermission(InetAddress.getByName(
+ "localhost").getHostAddress(), "accept,connect"));
+ System.setSecurityManager(mutableSM);
+ try {
+ // the second parameter is the TTL(time to live)
+ mutableSM.checkMulticast(InetAddress.getByName("localhost"),
+ (byte) 0);
+ fail("This should throw a SecurityException.");
+ } catch (SecurityException e) {
+ // expected
+ }
+ }
+
+ /**
+ *
+ * @tests {@link java.lang.SecurityManager#checkPermission(Permission, Object)}
+ */
+ @SuppressWarnings("nls")
+ public void test_checkPermissionLjava_security_PermissionLjava_lang_Object() {
+ // enable all but one check
+ mutableSM.addPermission(new AllPermission());
+ Permission denyp = new SocketPermission("localhost:1024-",
+ "accept, connect, listen");
+ mutableSM.denyPermission(denyp);
+ System.setSecurityManager(mutableSM);
+ ProtectionDomain pDomain = this.getClass().getProtectionDomain();
+ ProtectionDomain[] pd = { pDomain };
+ AccessControlContext acc = new AccessControlContext(pd);
+ try {
+ mutableSM.checkPermission(denyp, acc);
+ fail("This should throw a SecurityException.");
+ } catch (SecurityException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.lang.SecurityManager#checkPrintJobAccess()}
+ */
+ @SuppressWarnings("nls")
+ public void test_checkPrintJobAccess() {
+ // enable all but one check
+ mutableSM.addPermission(new AllPermission());
+ mutableSM.denyPermission(new RuntimePermission("queuePrintJob"));
+ System.setSecurityManager(mutableSM);
+ try {
+ mutableSM.checkPrintJobAccess();
+ fail("This should throw a SecurityException.");
+ } catch (SecurityException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.lang.SecurityManager#checkRead(FileDescriptor)}
+ */
+ @SuppressWarnings("nls")
+ public void test_checkReadLjava_io_FileDescriptor() {
+ // enable all but one check
+ mutableSM.addPermission(new AllPermission());
+ mutableSM.denyPermission(new RuntimePermission("readFileDescriptor"));
+ System.setSecurityManager(mutableSM);
+ try {
+ mutableSM.checkRead(new FileDescriptor());
+ fail("This should throw a SecurityException.");
+ } catch (SecurityException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.lang.SecurityManager#checkRead(String,Object)}
+ */
+ @SuppressWarnings("nls")
+ public void test_checkReadLjava_lang_StringLjava_lang_Object() {
+ // enable all but one check
+ mutableSM.addPermission(new AllPermission());
+ mutableSM.denyPermission(new FilePermission("<<ALL FILES>>", "read"));
+ ProtectionDomain pDomain = this.getClass().getProtectionDomain();
+ ProtectionDomain[] pd = { pDomain };
+ AccessControlContext acc = new AccessControlContext(pd);
+ System.setSecurityManager(mutableSM);
+ try {
+ mutableSM.checkRead("aa", acc);
+ fail("This should throw a SecurityException.");
+ } catch (SecurityException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.lang.SecurityManager#checkSetFactory()}
+ */
+ @SuppressWarnings("nls")
+ public void test_checkSetFactory() {
+ // enable all but one check
+ mutableSM.addPermission(new AllPermission());
+ mutableSM.denyPermission(new RuntimePermission("setFactory"));
+ System.setSecurityManager(mutableSM);
+ try {
+ mutableSM.checkSetFactory();
+ fail("This should throw a SecurityException.");
+ } catch (SecurityException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.lang.SecurityManager#getInCheck()}
+ */
+ public void test_getIncheck() {
+ mockSM.setInCheck(false);
+ assertFalse(mockSM.getInCheck());
+ mockSM.setInCheck(true);
+ assertTrue(mockSM.getInCheck());
+ }
+
+ /**
+ * @tests {@link java.lang.SecurityManager#getSecurityContext()}
+ */
+ @SuppressWarnings("nls")
+ public void test_getSecurityContext() {
+ // enable all but one check
+ mutableSM.addPermission(new AllPermission());
+ mutableSM.denyPermission(new FilePermission("<<ALL FILES>>", "read"));
+ System.setSecurityManager(mutableSM);
+ try {
+ mutableSM.checkRead("aa", mutableSM.getSecurityContext());
+ fail("This should throw a SecurityException.");
+ } catch (SecurityException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.lang.SecurityManager#classDepth(String)}
+ */
+ @SuppressWarnings("nls")
+ public void test_classDepthLjava_lang_String() {
+ assertEquals(-1, mockSM.classDepth("nothing"));
+ }
+
+ /**
+ * @tests {@link java.lang.SecurityManager#classLoaderDepth()}
+ */
+ public void test_classLoaderDepth() {
+ assertEquals(-1, mockSM.classLoaderDepth());
+ }
+
+ /**
+ * @tests {@link java.lang.SecurityManager#currentClassLoader()}
+ */
+ public void test_currentClassLoader() {
+ assertNull(mockSM.currentClassLoader());
+ }
+
+ /**
+ * @tests {@link java.lang.SecurityManager#currentLoadedClass()}
+ */
+ public void test_currentLoadedClass() {
+ assertNull(mockSM.currentLoadedClass());
+ }
+
+ /**
+ * @tests {@link java.lang.SecurityManager#inClass(String)}
+ */
+ @SuppressWarnings("nls")
+ public void test_inClassLjava_lang_String() {
+ assertFalse(mockSM.inClass("nothing"));
+ assertTrue(mockSM.inClass(MockSecurityManager.class.getName()));
+ }
+
+ /**
+ * @tests {@link java.lang.SecurityManager#inClassLoader()}
+ */
+ public void test_inClassLoader() {
+ assertFalse(mockSM.inClassLoader());
+ }
+
+ // set some protected method to public for testing
+ class MockSecurityManager extends SecurityManager {
+
+ public void setInCheck(boolean inCheck) {
+ super.inCheck = inCheck;
+ }
+
+ @Override
+ public int classDepth(String name) {
+ return super.classDepth(name);
+ }
+
+ @Override
+ public int classLoaderDepth() {
+ return super.classLoaderDepth();
+ }
+
+ @Override
+ public ClassLoader currentClassLoader() {
+ return super.currentClassLoader();
+ }
+
+ @Override
+ public Class<?> currentLoadedClass() {
+ return super.currentLoadedClass();
+ }
+
+ @Override
+ public Class[] getClassContext() {
+ return super.getClassContext();
+ }
+
+ @Override
+ public boolean inClass(String name) {
+ return super.inClass(name);
+ }
+
+ @Override
+ public boolean inClassLoader() {
+ return super.inClassLoader();
+ }
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ mutableSM = new MutableSecurityManager();
+ mockSM = new MockSecurityManager();
+ originalSM = System.getSecurityManager();
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ System.setSecurityManager(originalSM);
}
}