jdk/test/java/security/SecureClassLoader/DefineClassByteBuffer.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4894899
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test various cases of passing java.nio.ByteBuffers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * to defineClass().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @build DefineClassByteBuffer TestClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main DefineClassByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class DefineClassByteBuffer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static void test(ClassLoader cl) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        Class c = Class.forName("TestClass", true, cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        if (!"TestClass".equals(c.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            throw new RuntimeException("Got wrong class: " + c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public static void main(String arg[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        ClassLoader[] cls = new ClassLoader[DummyClassLoader.MAX_TYPE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        for (int i = 0; i < cls.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            cls[i] = new DummyClassLoader(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        /* Create several instances of the class using different classloaders,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
           which are using different types of ByteBuffer. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        for (int i = 0; i < cls.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
          test(cls[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /** Always loads the same class, using various types of ByteBuffers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public static class DummyClassLoader extends SecureClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        public static final String CLASS_NAME = "TestClass";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        public static final int MAPPED_BUFFER = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        public static final int DIRECT_BUFFER = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        public static final int ARRAY_BUFFER = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        public static final int WRAPPED_BUFFER = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        public static final int READ_ONLY_ARRAY_BUFFER = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        public static final int READ_ONLY_DIRECT_BUFFER = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        public static final int DUP_ARRAY_BUFFER = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        public static final int DUP_DIRECT_BUFFER = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        public static final int MAX_TYPE = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        int loaderType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        DummyClassLoader(int loaderType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            this.loaderType = loaderType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        static ByteBuffer[] buffers = new ByteBuffer[MAX_TYPE + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        static ByteBuffer readClassFile(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                File f = new File(System.getProperty("test.classes", "."),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                                  name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                FileInputStream fin = new FileInputStream(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                FileChannel fc = fin.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                return fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            } catch (FileNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                throw new RuntimeException("Can't open file: " + name, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                throw new RuntimeException("Can't open file: " + name, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            /* create a bunch of different ByteBuffers, starting with a mapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
               buffer from a class file, and create various duplicate and wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
               buffers. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            buffers[MAPPED_BUFFER] = readClassFile(CLASS_NAME + ".class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            byte[] array = new byte[buffers[MAPPED_BUFFER].limit()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            buffers[DIRECT_BUFFER] = ByteBuffer.allocateDirect(array.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            buffers[DIRECT_BUFFER].put(array);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            buffers[ARRAY_BUFFER] = ByteBuffer.allocate(array.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            buffers[ARRAY_BUFFER].put(array);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            buffers[WRAPPED_BUFFER] = ByteBuffer.wrap(array);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            buffers[READ_ONLY_ARRAY_BUFFER] = buffers[ARRAY_BUFFER].asReadOnlyBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            buffers[READ_ONLY_DIRECT_BUFFER] = buffers[DIRECT_BUFFER].asReadOnlyBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            buffers[DUP_ARRAY_BUFFER] = buffers[ARRAY_BUFFER].duplicate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            buffers[DUP_DIRECT_BUFFER] = buffers[DIRECT_BUFFER].duplicate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
         public Class findClass(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
             CodeSource cs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
             return defineClass(name, buffers[loaderType], cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    } /* DummyClassLoader */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
} /* DefineClassByteBuffer */