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 * Asm plugin testing. |
|
26 * @test |
|
27 * @summary Test basic functionality. |
|
28 * @author Andrei Eremeev |
|
29 * @modules java.base/jdk.internal.org.objectweb.asm |
|
30 * jdk.jlink/jdk.tools.jlink.internal |
|
31 * jdk.jlink/jdk.tools.jlink.internal.plugins.asm |
|
32 * @build AsmPluginTestBase |
|
33 * @run main NegativeTest |
|
34 */ |
|
35 |
|
36 import java.io.IOException; |
|
37 import java.io.UncheckedIOException; |
|
38 import java.nio.ByteOrder; |
|
39 import java.util.Map; |
|
40 import java.util.Set; |
|
41 import jdk.internal.org.objectweb.asm.ClassReader; |
|
42 import jdk.internal.org.objectweb.asm.ClassVisitor; |
|
43 import jdk.internal.org.objectweb.asm.ClassWriter; |
|
44 import jdk.internal.org.objectweb.asm.Opcodes; |
|
45 import jdk.tools.jlink.plugin.Plugin; |
|
46 import jdk.tools.jlink.internal.ModulePoolImpl; |
|
47 import jdk.tools.jlink.internal.StringTable; |
|
48 import jdk.tools.jlink.internal.plugins.asm.AsmGlobalPool; |
|
49 import jdk.tools.jlink.internal.plugins.asm.AsmModulePool; |
|
50 import jdk.tools.jlink.internal.plugins.asm.AsmPlugin; |
|
51 import jdk.tools.jlink.internal.plugins.asm.AsmPool.ResourceFile; |
|
52 import jdk.tools.jlink.internal.plugins.asm.AsmPools; |
|
53 import jdk.tools.jlink.plugin.PluginException; |
|
54 import jdk.tools.jlink.plugin.ModulePool; |
|
55 |
|
56 public class NegativeTest extends AsmPluginTestBase { |
|
57 public static void main(String[] args) throws Exception { |
|
58 if (!isImageBuild()) { |
|
59 System.err.println("Test not run. Not image build."); |
|
60 return; |
|
61 } |
|
62 new NegativeTest().test(); |
|
63 } |
|
64 |
|
65 @Override |
|
66 public void test() throws Exception { |
|
67 testNull(); |
|
68 testUnknownPackage(); |
|
69 } |
|
70 |
|
71 private void testUnknownPackage() throws Exception { |
|
72 AsmPlugin t = new AsmPlugin() { |
|
73 @Override |
|
74 public void visit(AsmPools pools) { |
|
75 try { |
|
76 AsmGlobalPool globalPool = pools.getGlobalPool(); |
|
77 AsmModulePool javabase = pools.getModulePool("java.base"); |
|
78 ClassReader cr = new ClassReader(NegativeTest.class.getResourceAsStream("NegativeTest.class")); |
|
79 ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES); |
|
80 cr.accept(new RenameClassVisitor(cw), ClassReader.EXPAND_FRAMES); |
|
81 action(() -> globalPool.getTransformedClasses().addClass(cw), |
|
82 "Unknown package", PluginException.class); |
|
83 action(() -> javabase.getTransformedClasses().addClass(cw), |
|
84 "Unknown package", PluginException.class); |
|
85 |
|
86 ResourceFile newResFile = new ResourceFile("java/aaa/file", new byte[0]); |
|
87 action(() -> globalPool.getTransformedResourceFiles().addResourceFile(newResFile), |
|
88 "Unknown package", PluginException.class); |
|
89 action(() -> javabase.getTransformedResourceFiles().addResourceFile(newResFile), |
|
90 "Unknown package", PluginException.class); |
|
91 |
|
92 action(() -> globalPool.getTransformedClasses().forgetClass("java/aaa/file"), |
|
93 "Unknown package", PluginException.class); |
|
94 action(() -> javabase.getTransformedClasses().forgetClass("java/aaa/file"), |
|
95 "Unknown package", PluginException.class); |
|
96 action(() -> globalPool.getTransformedResourceFiles().forgetResourceFile("java/aaa/file"), |
|
97 "Unknown package", PluginException.class); |
|
98 action(() -> javabase.getTransformedResourceFiles().forgetResourceFile("java/aaa/file"), |
|
99 "Unknown package", PluginException.class); |
|
100 } catch (IOException ex) { |
|
101 throw new UncheckedIOException(ex); |
|
102 } |
|
103 } |
|
104 }; |
|
105 ModulePool resources = new ModulePoolImpl(ByteOrder.BIG_ENDIAN, new StringTable() { |
|
106 @Override |
|
107 public int addString(String str) { |
|
108 return -1; |
|
109 } |
|
110 |
|
111 @Override |
|
112 public String getString(int id) { |
|
113 throw new UnsupportedOperationException("Not supported yet."); |
|
114 } |
|
115 }); |
|
116 t.visit(getPool(), resources); |
|
117 } |
|
118 |
|
119 private static class RenameClassVisitor extends ClassVisitor { |
|
120 |
|
121 public RenameClassVisitor(ClassWriter cv) { |
|
122 super(Opcodes.ASM5, cv); |
|
123 } |
|
124 |
|
125 @Override |
|
126 public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { |
|
127 super.visit(version, access, "RENAMED", signature, superName, interfaces); |
|
128 } |
|
129 } |
|
130 |
|
131 private void testNull() throws Exception { |
|
132 AsmPlugin t = new AsmPlugin() { |
|
133 @Override |
|
134 public void visit(AsmPools pools) { |
|
135 action(() -> pools.getModulePool(null), "Module name is null", NullPointerException.class); |
|
136 action(() -> pools.fillOutputResources(null), "Output resource is null", NullPointerException.class); |
|
137 } |
|
138 }; |
|
139 ModulePool resources = new ModulePoolImpl(ByteOrder.BIG_ENDIAN, new StringTable() { |
|
140 @Override |
|
141 public int addString(String str) { |
|
142 return -1; |
|
143 } |
|
144 |
|
145 @Override |
|
146 public String getString(int id) { |
|
147 throw new UnsupportedOperationException("Not supported yet."); |
|
148 } |
|
149 }); |
|
150 action(() -> t.visit(null, resources), "Input resource is null", NullPointerException.class); |
|
151 action(() -> t.visit(resources, null), "Output resource is null", NullPointerException.class); |
|
152 t.visit(resources, resources); |
|
153 } |
|
154 |
|
155 private void action(Action action, String message, Class<? extends Exception> expected) { |
|
156 try { |
|
157 System.err.println("Testing: " + message); |
|
158 action.call(); |
|
159 throw new AssertionError(message + ": should have failed"); |
|
160 } catch (Exception e) { |
|
161 if (!expected.isInstance(e)) { |
|
162 throw new RuntimeException(e); |
|
163 } else { |
|
164 System.err.println("Got exception as expected: " + e); |
|
165 } |
|
166 } |
|
167 } |
|
168 |
|
169 private interface Action { |
|
170 void call() throws Exception; |
|
171 } |
|
172 } |
|