jdk/test/java/security/SecureClassLoader/DefineClassByteBuffer.java
author mfang
Wed, 17 Aug 2011 14:18:26 -0700
changeset 10294 8fcdae2a7ec7
parent 10055 f5220ab72a1d
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10055
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
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
10055
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
    26
 * @bug 4894899 7054428
2
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 {
10055
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
    49
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
    50
        // Rename the compiled TestClass.class file to something else,
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
    51
        // otherwise it would be loaded by the parent class loader and
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
    52
        // DummyClassLoader will never be used, especially in /othervm mode.
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
    53
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
    54
        File oldFile = new File(System.getProperty("test.classes", "."),
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
    55
                                  "TestClass.class");
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
    56
        File newFile = new File(System.getProperty("test.classes", "."),
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
    57
                                  "CLAZZ");
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
    58
        oldFile.renameTo(newFile);
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
    59
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        ClassLoader[] cls = new ClassLoader[DummyClassLoader.MAX_TYPE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        for (int i = 0; i < cls.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            cls[i] = new DummyClassLoader(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        /* Create several instances of the class using different classloaders,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
           which are using different types of ByteBuffer. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        for (int i = 0; i < cls.length; i++) {
10055
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
    68
            test(cls[i]);
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
    69
        }
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
    70
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
    71
        if (DummyClassLoader.count != cls.length) {
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
    72
             throw new Exception("DummyClassLoader not always used");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /** Always loads the same class, using various types of ByteBuffers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public static class DummyClassLoader extends SecureClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        public static final String CLASS_NAME = "TestClass";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        public static final int MAPPED_BUFFER = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        public static final int DIRECT_BUFFER = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        public static final int ARRAY_BUFFER = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        public static final int WRAPPED_BUFFER = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        public static final int READ_ONLY_ARRAY_BUFFER = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        public static final int READ_ONLY_DIRECT_BUFFER = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        public static final int DUP_ARRAY_BUFFER = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        public static final int DUP_DIRECT_BUFFER = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        public static final int MAX_TYPE = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        int loaderType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
10055
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
    93
        static int count = 0;
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
    94
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        DummyClassLoader(int loaderType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            this.loaderType = loaderType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        static ByteBuffer[] buffers = new ByteBuffer[MAX_TYPE + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        static ByteBuffer readClassFile(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                File f = new File(System.getProperty("test.classes", "."),
10055
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
   104
                                  "CLAZZ");
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
   105
                try (FileInputStream fin = new FileInputStream(f);
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
   106
                        FileChannel fc = fin.getChannel()) {
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
   107
                    return fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
   108
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            } catch (FileNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                throw new RuntimeException("Can't open file: " + name, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                throw new RuntimeException("Can't open file: " + name, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            /* create a bunch of different ByteBuffers, starting with a mapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
               buffer from a class file, and create various duplicate and wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
               buffers. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            buffers[MAPPED_BUFFER] = readClassFile(CLASS_NAME + ".class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            byte[] array = new byte[buffers[MAPPED_BUFFER].limit()];
10055
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
   122
            buffers[MAPPED_BUFFER].get(array);
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
   123
            buffers[MAPPED_BUFFER].flip();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            buffers[DIRECT_BUFFER] = ByteBuffer.allocateDirect(array.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            buffers[DIRECT_BUFFER].put(array);
10055
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
   127
            buffers[DIRECT_BUFFER].flip();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            buffers[ARRAY_BUFFER] = ByteBuffer.allocate(array.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            buffers[ARRAY_BUFFER].put(array);
10055
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
   131
            buffers[ARRAY_BUFFER].flip();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            buffers[WRAPPED_BUFFER] = ByteBuffer.wrap(array);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            buffers[READ_ONLY_ARRAY_BUFFER] = buffers[ARRAY_BUFFER].asReadOnlyBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            buffers[READ_ONLY_DIRECT_BUFFER] = buffers[DIRECT_BUFFER].asReadOnlyBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            buffers[DUP_ARRAY_BUFFER] = buffers[ARRAY_BUFFER].duplicate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            buffers[DUP_DIRECT_BUFFER] = buffers[DIRECT_BUFFER].duplicate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
         public Class findClass(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
             CodeSource cs = null;
10055
f5220ab72a1d 7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents: 5506
diff changeset
   146
             count++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
             return defineClass(name, buffers[loaderType], cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    } /* DummyClassLoader */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
} /* DefineClassByteBuffer */