author | mfang |
Wed, 17 Aug 2011 14:18:26 -0700 | |
changeset 10294 | 8fcdae2a7ec7 |
parent 10055 | f5220ab72a1d |
permissions | -rw-r--r-- |
2 | 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 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
10055
f5220ab72a1d
7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents:
5506
diff
changeset
|
26 |
* @bug 4894899 7054428 |
2 | 27 |
* @summary Test various cases of passing java.nio.ByteBuffers |
28 |
* to defineClass(). |
|
29 |
* |
|
30 |
* @build DefineClassByteBuffer TestClass |
|
31 |
* @run main DefineClassByteBuffer |
|
32 |
*/ |
|
33 |
||
34 |
import java.security.*; |
|
35 |
import java.nio.*; |
|
36 |
import java.nio.channels.*; |
|
37 |
import java.io.*; |
|
38 |
||
39 |
public class DefineClassByteBuffer { |
|
40 |
||
41 |
static void test(ClassLoader cl) throws Exception { |
|
42 |
Class c = Class.forName("TestClass", true, cl); |
|
43 |
if (!"TestClass".equals(c.getName())) { |
|
44 |
throw new RuntimeException("Got wrong class: " + c); |
|
45 |
} |
|
46 |
} |
|
47 |
||
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 | 60 |
ClassLoader[] cls = new ClassLoader[DummyClassLoader.MAX_TYPE]; |
61 |
for (int i = 0; i < cls.length; i++) { |
|
62 |
cls[i] = new DummyClassLoader(i); |
|
63 |
} |
|
64 |
||
65 |
/* Create several instances of the class using different classloaders, |
|
66 |
which are using different types of ByteBuffer. */ |
|
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 | 73 |
} |
74 |
} |
|
75 |
||
76 |
/** Always loads the same class, using various types of ByteBuffers */ |
|
77 |
public static class DummyClassLoader extends SecureClassLoader { |
|
78 |
||
79 |
public static final String CLASS_NAME = "TestClass"; |
|
80 |
||
81 |
public static final int MAPPED_BUFFER = 0; |
|
82 |
public static final int DIRECT_BUFFER = 1; |
|
83 |
public static final int ARRAY_BUFFER = 2; |
|
84 |
public static final int WRAPPED_BUFFER = 3; |
|
85 |
public static final int READ_ONLY_ARRAY_BUFFER = 4; |
|
86 |
public static final int READ_ONLY_DIRECT_BUFFER = 5; |
|
87 |
public static final int DUP_ARRAY_BUFFER = 6; |
|
88 |
public static final int DUP_DIRECT_BUFFER = 7; |
|
89 |
public static final int MAX_TYPE = 7; |
|
90 |
||
91 |
int loaderType; |
|
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 | 95 |
DummyClassLoader(int loaderType) { |
96 |
this.loaderType = loaderType; |
|
97 |
} |
|
98 |
||
99 |
static ByteBuffer[] buffers = new ByteBuffer[MAX_TYPE + 1]; |
|
100 |
||
101 |
static ByteBuffer readClassFile(String name) { |
|
102 |
try { |
|
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 | 109 |
} catch (FileNotFoundException e) { |
110 |
throw new RuntimeException("Can't open file: " + name, e); |
|
111 |
} catch (IOException e) { |
|
112 |
throw new RuntimeException("Can't open file: " + name, e); |
|
113 |
} |
|
114 |
} |
|
115 |
||
116 |
static { |
|
117 |
/* create a bunch of different ByteBuffers, starting with a mapped |
|
118 |
buffer from a class file, and create various duplicate and wrapped |
|
119 |
buffers. */ |
|
120 |
buffers[MAPPED_BUFFER] = readClassFile(CLASS_NAME + ".class"); |
|
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 | 124 |
|
125 |
buffers[DIRECT_BUFFER] = ByteBuffer.allocateDirect(array.length); |
|
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 | 128 |
|
129 |
buffers[ARRAY_BUFFER] = ByteBuffer.allocate(array.length); |
|
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 | 132 |
|
133 |
buffers[WRAPPED_BUFFER] = ByteBuffer.wrap(array); |
|
134 |
||
135 |
buffers[READ_ONLY_ARRAY_BUFFER] = buffers[ARRAY_BUFFER].asReadOnlyBuffer(); |
|
136 |
||
137 |
buffers[READ_ONLY_DIRECT_BUFFER] = buffers[DIRECT_BUFFER].asReadOnlyBuffer(); |
|
138 |
||
139 |
buffers[DUP_ARRAY_BUFFER] = buffers[ARRAY_BUFFER].duplicate(); |
|
140 |
||
141 |
buffers[DUP_DIRECT_BUFFER] = buffers[DIRECT_BUFFER].duplicate(); |
|
142 |
} |
|
143 |
||
144 |
public Class findClass(String name) { |
|
145 |
CodeSource cs = null; |
|
10055
f5220ab72a1d
7054428: test/java/security/SecureClassLoader/DefineClassByteBuffer.java error
weijun
parents:
5506
diff
changeset
|
146 |
count++; |
2 | 147 |
return defineClass(name, buffers[loaderType], cs); |
148 |
} |
|
149 |
} /* DummyClassLoader */ |
|
150 |
||
151 |
} /* DefineClassByteBuffer */ |