Path: blob/master/test/jdk/tools/jlink/plugins/ExcludeVMPluginTest.java
41149 views
/*1* Copyright (c) 2015, 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* @summary Test exclude VM plugin26* @author Jean-Francois Denise27* @modules jdk.jlink/jdk.tools.jlink.internal28* jdk.jlink/jdk.tools.jlink.internal.plugins29* jdk.jlink/jdk.tools.jlink.plugin30* @run main ExcludeVMPluginTest31*/32import java.io.ByteArrayInputStream;33import java.net.URI;34import java.nio.file.Path;35import java.nio.file.Paths;36import java.util.HashMap;37import java.util.Map;38import jdk.tools.jlink.internal.ResourcePoolManager;3940import jdk.tools.jlink.internal.plugins.ExcludeVMPlugin;41import jdk.tools.jlink.plugin.Plugin;42import jdk.tools.jlink.plugin.ResourcePool;43import jdk.tools.jlink.plugin.ResourcePoolEntry;4445public class ExcludeVMPluginTest {4647private static final String TAG = "# orig in test\n";4849private static final String[] ARCHITECTURES = {"/", "/amd64/", "/i386/", "/arm/",50"/aarch64/", "/toto/"};5152private static final String[] CLIENT = {"client/" + jvmlib(),};53private static final String[] SERVER = {"server/" + jvmlib()};54private static final String[] MINIMAL = {"minimal/" + jvmlib()};55private static final String[] ALL = {CLIENT[0], SERVER[0], MINIMAL[0]};56private static final String JVM_CFG_ALL = TAG + "-server KNOWN\n-client KNOWN\n-minimal KNOWN\n";57private static final String JVM_CFG_CLIENT = TAG + "-client KNOWN\n";58private static final String JVM_CFG_SERVER = TAG + "-server KNOWN\n";59private static final String JVM_CFG_SERVER_ALIAS_OTHERS = TAG + "-server KNOWN\n-client ALIASED_TO -server\n-minimal ALIASED_TO -server\n";60private static final String JVM_CFG_CLIENT_ALIAS_OTHERS = TAG + "-client KNOWN\n-server ALIASED_TO -client\n-minimal ALIASED_TO -client\n";61private static final String JVM_CFG_MINIMAL_ALIAS_OTHERS = TAG + "-minimal KNOWN\n-server ALIASED_TO -minimal\n-client ALIASED_TO -minimal\n";62private static final String JVM_CFG_MINIMAL = TAG + "-minimal KNOWN\n";6364public static void main(String[] args) throws Exception {65new ExcludeVMPluginTest().test();66}6768public void test() throws Exception {69boolean failed = false;7071try {72checkVM("toto", ALL, JVM_CFG_ALL, ALL, JVM_CFG_ALL);73failed = true;74throw new Exception("Should have failed");75} catch (Exception ex) {76if (failed) {77throw ex;78}79}8081checkVM("all", ALL, JVM_CFG_ALL, ALL, JVM_CFG_ALL);82checkVM("all", CLIENT, JVM_CFG_CLIENT, CLIENT, JVM_CFG_CLIENT);83checkVM("all", SERVER, JVM_CFG_SERVER, SERVER, JVM_CFG_SERVER);84checkVM("all", MINIMAL, JVM_CFG_MINIMAL, MINIMAL, JVM_CFG_MINIMAL);8586checkVM("server", ALL, JVM_CFG_ALL, SERVER, JVM_CFG_SERVER_ALIAS_OTHERS);87checkVM("server", SERVER, JVM_CFG_SERVER, SERVER, JVM_CFG_SERVER);88try {89checkVM("server", CLIENT, JVM_CFG_CLIENT, SERVER, JVM_CFG_SERVER);90failed = true;91throw new Exception("Should have failed");92} catch (Exception ex) {93if (failed) {94throw ex;95}96}97try {98checkVM("server", MINIMAL, JVM_CFG_MINIMAL, SERVER, JVM_CFG_SERVER);99failed = true;100throw new Exception("Should have failed");101} catch (Exception ex) {102if (failed) {103throw ex;104}105}106107checkVM("client", ALL, JVM_CFG_ALL, CLIENT, JVM_CFG_CLIENT_ALIAS_OTHERS);108checkVM("client", CLIENT, JVM_CFG_CLIENT, CLIENT, JVM_CFG_CLIENT);109try {110checkVM("client", SERVER, JVM_CFG_SERVER, CLIENT, JVM_CFG_CLIENT);111failed = true;112throw new Exception("Should have failed");113} catch (Exception ex) {114if (failed) {115throw ex;116}117}118try {119checkVM("client", MINIMAL, JVM_CFG_MINIMAL, CLIENT, JVM_CFG_CLIENT);120failed = true;121throw new Exception("Should have failed");122} catch (Exception ex) {123if (failed) {124throw ex;125}126}127128checkVM("minimal", ALL, JVM_CFG_ALL, MINIMAL, JVM_CFG_MINIMAL_ALIAS_OTHERS);129checkVM("minimal", MINIMAL, JVM_CFG_MINIMAL, MINIMAL, JVM_CFG_MINIMAL);130try {131checkVM("minimal", SERVER, JVM_CFG_SERVER, MINIMAL, JVM_CFG_MINIMAL);132failed = true;133throw new Exception("Should have failed");134} catch (Exception ex) {135if (failed) {136throw ex;137}138}139try {140checkVM("minimal", CLIENT, JVM_CFG_CLIENT, MINIMAL, JVM_CFG_MINIMAL);141failed = true;142throw new Exception("Should have failed");143} catch (Exception ex) {144if (failed) {145throw ex;146}147}148149}150151public void checkVM(String vm, String[] input, String jvmcfg, String[] expectedOutput, String expectdJvmCfg) throws Exception {152153for (String arch : ARCHITECTURES) {154String[] winput = new String[input.length];155String[] woutput = new String[expectedOutput.length];156for (int i = 0; i < input.length; i++) {157winput[i] = "/java.base/lib" + arch + input[i];158}159for (int i = 0; i < expectedOutput.length; i++) {160woutput[i] = "/java.base/lib" + arch + expectedOutput[i];161}162doCheckVM(vm, winput, jvmcfg, woutput, expectdJvmCfg);163}164}165166private void doCheckVM(String vm, String[] input, String jvmcfg, String[] expectedOutput, String expectdJvmCfg) throws Exception {167// Create a pool with jvm.cfg and the input paths.168byte[] jvmcfgContent = jvmcfg.getBytes();169ResourcePoolManager poolMgr = new ResourcePoolManager();170poolMgr.add(171ResourcePoolEntry.create("/java.base/lib/jvm.cfg",172ResourcePoolEntry.Type.NATIVE_LIB, jvmcfgContent));173174// java.base/module-info.class is used by exclude vm plugin175// to get current osName(). We read it from jrt-fs and add a176// ResourcePoolEntry177poolMgr.add(178ResourcePoolEntry.create("/java.base/module-info.class",179ResourcePoolEntry.Type.CLASS_OR_RESOURCE, getJavaBaseModuleInfo()));180for (String in : input) {181poolMgr.add(ResourcePoolEntry.create(in,182ResourcePoolEntry.Type.NATIVE_LIB, new byte[0]));183}184ResourcePoolManager outMgr = new ResourcePoolManager();185186Plugin p = new ExcludeVMPlugin();187Map<String, String> config = new HashMap<>();188if (vm != null) {189config.put(p.getName(), vm);190}191p.configure(config);192ResourcePool out = p.transform(poolMgr.resourcePool(), outMgr.resourcePoolBuilder());193194String newContent = new String(out.findEntry("/java.base/lib/jvm.cfg").get().contentBytes());195196if (!expectdJvmCfg.equals(newContent)) {197throw new Exception("Got content " + newContent + " expected " + expectdJvmCfg);198}199200// Apart from native resources, we should find jvm.cfg and201// java.base/module-info.class. So, we add 2 here to the202// expected count!203if (out.entryCount() != (expectedOutput.length + 2)) {204out.entries().forEach(m -> {205System.err.println(m.path());206});207throw new Exception("Invalid output size " + out.entryCount() + " expected " + (expectedOutput.length + 2));208}209210out.entries().forEach(md -> {211if (md.path().equals("/java.base/lib/jvm.cfg") ||212md.path().equals("/java.base/module-info.class")) {213return;214}215boolean contained = false;216for (String o : expectedOutput) {217if (md.path().equals(o)) {218contained = true;219break;220}221}222if (!contained) {223throw new RuntimeException(md.path() + " not expected");224}225});226}227228// read java.base/module-info.class from jrt-fs229private static Path getJavaBaseModuleInfo() {230return Paths.get(URI.create("jrt:/java.base/module-info.class"));231}232233private static boolean isWindows() {234return System.getProperty("os.name").startsWith("Windows");235}236237private static boolean isMac() {238return System.getProperty("os.name").startsWith("Mac OS");239}240241private static String jvmlib() {242String lib = "libjvm.so";243if (isWindows()) {244lib = "jvm.dll";245} else if (isMac()) {246lib = "libjvm.dylib";247}248return lib;249}250}251252253