SecureClassLoader2Test.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package org.apache.harmony.security.tests.java.security;
19
20import java.io.File;
21import java.io.IOException;
22import java.io.InputStream;
23import java.security.CodeSource;
24import java.security.PermissionCollection;
25import java.security.ProtectionDomain;
26import java.security.SecureClassLoader;
27import java.security.cert.Certificate;
28import java.util.Enumeration;
29import java.util.jar.JarFile;
30
31import org.apache.harmony.luni.util.InputStreamHelper;
32
33import tests.support.Support_GetLocal;
34
35public class SecureClassLoader2Test extends junit.framework.TestCase {
36
37	/**
38	 * @tests java.security.SecureClassLoader#getPermissions(java.security.CodeSource)
39	 */
40	public void test_getPermissionsLjava_security_CodeSource() throws IOException {
41		class MyClassLoader extends SecureClassLoader {
42			public PermissionCollection getPerms() {
43				return super.getPermissions(new CodeSource(null,
44						(Certificate[]) null));
45			}
46
47			public Class define(String name, byte[] bytes) {
48				return defineClass(name, bytes, 0, bytes.length,
49						(ProtectionDomain) null);
50			}
51		}
52
53		MyClassLoader myloader = new MyClassLoader();
54		PermissionCollection pc = myloader.getPerms();
55		Enumeration e1 = pc.elements();
56		int count = 0;
57		while (e1.hasMoreElements()) {
58			e1.nextElement();
59			count++;
60		}
61		assertEquals("expected no permissions", 0, count);
62
63        File file = Support_GetLocal.getLocalFile("hyts_security.jar");
64        JarFile jar = new JarFile(file);
65        InputStream in = jar.getInputStream(jar.getEntry("packA/SecurityTest.class"));
66        byte[] bytes = InputStreamHelper.readFullyAndClose(in);
67        Class c = myloader.define("packA.SecurityTest", bytes);
68		ProtectionDomain pd = c.getProtectionDomain();
69		assertNotNull("Expected dynamic policy", pd.getClassLoader());
70		assertNull("Expected null permissions", pd.getPermissions());
71	}
72}