Path: blob/master/test/jdk/java/security/SecureClassLoader/DefineClassByteBuffer.java
41149 views
/*1* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4894899 705442826* @summary Test various cases of passing java.nio.ByteBuffers27* to defineClass().28*29* @build DefineClassByteBuffer TestClass30* @run main DefineClassByteBuffer31*/3233import java.security.*;34import java.nio.*;35import java.nio.channels.*;36import java.io.*;3738public class DefineClassByteBuffer {3940static void test(ClassLoader cl) throws Exception {41Class c = Class.forName("TestClass", true, cl);42if (!"TestClass".equals(c.getName())) {43throw new RuntimeException("Got wrong class: " + c);44}45}4647public static void main(String arg[]) throws Exception {4849// Rename the compiled TestClass.class file to something else,50// otherwise it would be loaded by the parent class loader and51// DummyClassLoader will never be used, especially in /othervm mode.5253File oldFile = new File(System.getProperty("test.classes", "."),54"TestClass.class");55File newFile = new File(System.getProperty("test.classes", "."),56"CLAZZ");57oldFile.renameTo(newFile);5859ClassLoader[] cls = new ClassLoader[DummyClassLoader.MAX_TYPE];60for (int i = 0; i < cls.length; i++) {61cls[i] = new DummyClassLoader(i);62}6364/* Create several instances of the class using different classloaders,65which are using different types of ByteBuffer. */66for (int i = 0; i < cls.length; i++) {67test(cls[i]);68}6970if (DummyClassLoader.count != cls.length) {71throw new Exception("DummyClassLoader not always used");72}73}7475/** Always loads the same class, using various types of ByteBuffers */76public static class DummyClassLoader extends SecureClassLoader {7778public static final String CLASS_NAME = "TestClass";7980public static final int MAPPED_BUFFER = 0;81public static final int DIRECT_BUFFER = 1;82public static final int ARRAY_BUFFER = 2;83public static final int WRAPPED_BUFFER = 3;84public static final int READ_ONLY_ARRAY_BUFFER = 4;85public static final int READ_ONLY_DIRECT_BUFFER = 5;86public static final int DUP_ARRAY_BUFFER = 6;87public static final int DUP_DIRECT_BUFFER = 7;88public static final int MAX_TYPE = 7;8990int loaderType;9192static int count = 0;9394DummyClassLoader(int loaderType) {95this.loaderType = loaderType;96}9798static ByteBuffer[] buffers = new ByteBuffer[MAX_TYPE + 1];99100static ByteBuffer readClassFile(String name) {101try {102File f = new File(System.getProperty("test.classes", "."),103"CLAZZ");104try (FileInputStream fin = new FileInputStream(f);105FileChannel fc = fin.getChannel()) {106return fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());107}108} catch (FileNotFoundException e) {109throw new RuntimeException("Can't open file: " + name, e);110} catch (IOException e) {111throw new RuntimeException("Can't open file: " + name, e);112}113}114115static {116/* create a bunch of different ByteBuffers, starting with a mapped117buffer from a class file, and create various duplicate and wrapped118buffers. */119buffers[MAPPED_BUFFER] = readClassFile(CLASS_NAME + ".class");120byte[] array = new byte[buffers[MAPPED_BUFFER].limit()];121buffers[MAPPED_BUFFER].get(array);122buffers[MAPPED_BUFFER].flip();123124buffers[DIRECT_BUFFER] = ByteBuffer.allocateDirect(array.length);125buffers[DIRECT_BUFFER].put(array);126buffers[DIRECT_BUFFER].flip();127128buffers[ARRAY_BUFFER] = ByteBuffer.allocate(array.length);129buffers[ARRAY_BUFFER].put(array);130buffers[ARRAY_BUFFER].flip();131132buffers[WRAPPED_BUFFER] = ByteBuffer.wrap(array);133134buffers[READ_ONLY_ARRAY_BUFFER] = buffers[ARRAY_BUFFER].asReadOnlyBuffer();135136buffers[READ_ONLY_DIRECT_BUFFER] = buffers[DIRECT_BUFFER].asReadOnlyBuffer();137138buffers[DUP_ARRAY_BUFFER] = buffers[ARRAY_BUFFER].duplicate();139140buffers[DUP_DIRECT_BUFFER] = buffers[DIRECT_BUFFER].duplicate();141}142143public Class findClass(String name) {144CodeSource cs = null;145count++;146return defineClass(name, buffers[loaderType], cs);147}148} /* DummyClassLoader */149150} /* DefineClassByteBuffer */151152153