1/*
2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @summary Verifies the behaviour of Unsafe.defineClass
27 * @library /test/lib
28 * @modules java.base/jdk.internal.misc
29 *          java.compiler
30 *          java.management
31 * @run main DefineClass
32 */
33
34import java.security.ProtectionDomain;
35
36import jdk.test.lib.compiler.InMemoryJavaCompiler;
37import jdk.internal.misc.Unsafe;
38import static jdk.test.lib.Asserts.*;
39
40public class DefineClass {
41    public static void main(String args[]) throws Exception {
42        Unsafe unsafe = Unsafe.getUnsafe();
43        TestClassLoader classloader = new TestClassLoader();
44        ProtectionDomain pd = new ProtectionDomain(null, null);
45
46        byte klassbuf[] = InMemoryJavaCompiler.compile("TestClass", "class TestClass { }");
47
48        // Invalid class data
49        try {
50            unsafe.defineClass(null, klassbuf, 4, klassbuf.length - 4, classloader, pd);
51            throw new RuntimeException("defineClass did not throw expected ClassFormatError");
52        } catch (ClassFormatError e) {
53            // Expected
54        }
55
56        // Negative offset
57        try {
58            unsafe.defineClass(null, klassbuf, -1, klassbuf.length, classloader, pd);
59            throw new RuntimeException("defineClass did not throw expected IndexOutOfBoundsException");
60        } catch (IndexOutOfBoundsException e) {
61            // Expected
62        }
63
64        // Negative length
65        try {
66            unsafe.defineClass(null, klassbuf, 0, -1, classloader, pd);
67            throw new RuntimeException("defineClass did not throw expected IndexOutOfBoundsException");
68        } catch (IndexOutOfBoundsException e) {
69            // Expected
70        }
71
72        // Offset greater than klassbuf.length
73        try {
74            unsafe.defineClass(null, klassbuf, klassbuf.length + 1, klassbuf.length, classloader, pd);
75            throw new RuntimeException("defineClass did not throw expected IndexOutOfBoundsException");
76        } catch (IndexOutOfBoundsException e) {
77            // Expected
78        }
79
80        // Length greater than klassbuf.length
81        try {
82            unsafe.defineClass(null, klassbuf, 0, klassbuf.length + 1, classloader, pd);
83            throw new RuntimeException("defineClass did not throw expected IndexOutOfBoundsException");
84        } catch (IndexOutOfBoundsException e) {
85            // Expected
86        }
87
88        Class klass = unsafe.defineClass(null, klassbuf, 0, klassbuf.length, classloader, pd);
89        assertEquals(klass.getClassLoader(), classloader);
90        assertEquals(klass.getProtectionDomain(), pd);
91    }
92
93    private static class TestClassLoader extends ClassLoader {
94        public TestClassLoader(ClassLoader parent) {
95            super(parent);
96        }
97
98        public TestClassLoader() {
99            super();
100        }
101    }
102}
103