1 /*
   2  * Copyright (c) 2015, 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  * @library /testlibrary
  27  * @run main DefineClass
  28  */
  29 
  30 import java.security.ProtectionDomain;
  31 import java.io.InputStream;
  32 import com.oracle.java.testlibrary.*;
  33 import sun.misc.Unsafe;
  34 import static com.oracle.java.testlibrary.Asserts.*;
  35 
  36 public class DefineClass {
  37         public static void main(String args[]) throws Exception {
  38         Unsafe unsafe = Utils.getUnsafe();
  39         TestClassLoader classloader = new TestClassLoader();
  40         ProtectionDomain pd = new ProtectionDomain(null, null);
  41 
  42         byte klassbuf[] = InMemoryJavaCompiler.compile("TestClass", "class TestClass { }");
  43 
  44         // Invalid class data
  45         try {
  46             unsafe.defineClass(null, klassbuf, 4, klassbuf.length - 4, classloader, pd);
  47             throw new RuntimeException("defineClass did not throw expected ClassFormatError");
  48         } catch (ClassFormatError e) {
  49             // Expected
  50         }
  51 
  52         // Negative offset
  53         try {
  54             unsafe.defineClass(null, klassbuf, -1, klassbuf.length, classloader, pd);
  55             throw new RuntimeException("defineClass did not throw expected IndexOutOfBoundsException");
  56         } catch (IndexOutOfBoundsException e) {
  57             // Expected
  58         }
  59 
  60         // Negative length
  61         try {
  62             unsafe.defineClass(null, klassbuf, 0, -1, classloader, pd);
  63             throw new RuntimeException("defineClass did not throw expected IndexOutOfBoundsException");
  64         } catch (IndexOutOfBoundsException e) {
  65             // Expected
  66         }
  67 
  68         // Offset greater than klassbuf.length
  69         try {
  70             unsafe.defineClass(null, klassbuf, klassbuf.length + 1, klassbuf.length, classloader, pd);
  71             throw new RuntimeException("defineClass did not throw expected IndexOutOfBoundsException");
  72         } catch (IndexOutOfBoundsException e) {
  73             // Expected
  74         }
  75 
  76         // Length greater than klassbuf.length
  77         try {
  78             unsafe.defineClass(null, klassbuf, 0, klassbuf.length + 1, classloader, pd);
  79             throw new RuntimeException("defineClass did not throw expected IndexOutOfBoundsException");
  80         } catch (IndexOutOfBoundsException e) {
  81             // Expected
  82         }
  83 
  84         Class klass = unsafe.defineClass(null, klassbuf, 0, klassbuf.length, classloader, pd);
  85         assertEquals(klass.getClassLoader(), classloader);
  86         assertEquals(klass.getProtectionDomain(), pd);
  87     }
  88 
  89     private static class TestClassLoader extends ClassLoader {
  90         public TestClassLoader(ClassLoader parent) {
  91             super(parent);
  92         }
  93 
  94         public TestClassLoader() {
  95             super();
  96         }
  97     }
  98 }