Path: blob/master/test/jdk/tools/jlink/plugins/DefaultStripDebugPluginTest.java
41152 views
/*1* Copyright (c) 2019, Red Hat, Inc.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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425import java.util.Map;2627import jdk.tools.jlink.internal.ResourcePoolManager;28import jdk.tools.jlink.internal.plugins.DefaultStripDebugPlugin;29import jdk.tools.jlink.internal.plugins.DefaultStripDebugPlugin.NativePluginFactory;30import jdk.tools.jlink.plugin.Plugin;31import jdk.tools.jlink.plugin.ResourcePool;32import jdk.tools.jlink.plugin.ResourcePoolBuilder;33import jdk.tools.jlink.plugin.ResourcePoolEntry;34import jdk.tools.jlink.plugin.ResourcePoolEntry.Type;3536/*37* @test38* @summary Test for combination of java debug attributes stripping and39* native debug symbols stripping.40* @modules jdk.jlink/jdk.tools.jlink.internal41* jdk.jlink/jdk.tools.jlink.internal.plugins42* jdk.jlink/jdk.tools.jlink.plugin43* @run main/othervm DefaultStripDebugPluginTest44*/45public class DefaultStripDebugPluginTest {4647public void testWithNativeStripPresent() {48MockStripPlugin javaPlugin = new MockStripPlugin(false);49MockStripPlugin nativePlugin = new MockStripPlugin(true);50TestNativeStripPluginFactory nativeFactory =51new TestNativeStripPluginFactory(nativePlugin);52DefaultStripDebugPlugin plugin = new DefaultStripDebugPlugin(javaPlugin,53nativeFactory);54ResourcePoolManager inManager = new ResourcePoolManager();55ResourcePool pool = plugin.transform(inManager.resourcePool(),56inManager.resourcePoolBuilder());57if (!pool.findEntry(MockStripPlugin.JAVA_PATH).isPresent() ||58!pool.findEntry(MockStripPlugin.NATIVE_PATH).isPresent()) {59throw new AssertionError("Expected both native and java to get called");60}61}6263public void testNoNativeStripPluginPresent() {64MockStripPlugin javaPlugin = new MockStripPlugin(false);65TestNativeStripPluginFactory nativeFactory =66new TestNativeStripPluginFactory(null);67DefaultStripDebugPlugin plugin = new DefaultStripDebugPlugin(javaPlugin,68nativeFactory);69ResourcePoolManager inManager = new ResourcePoolManager();70ResourcePool pool = plugin.transform(inManager.resourcePool(),71inManager.resourcePoolBuilder());72if (!pool.findEntry(MockStripPlugin.JAVA_PATH).isPresent()) {73throw new AssertionError("Expected java strip plugin to get called");74}75}7677public static void main(String[] args) {78DefaultStripDebugPluginTest test = new DefaultStripDebugPluginTest();79test.testNoNativeStripPluginPresent();80test.testWithNativeStripPresent();81}8283public static class MockStripPlugin implements Plugin {8485private static final String NATIVE_PATH = "/foo/lib/test.so.debug";86private static final String JAVA_PATH = "/foo/TestClass.class";87private static final String STRIP_NATIVE_NAME = "strip-native-debug-symbols";88private static final String OMIT_ARG = "exclude-debuginfo-files";89private final boolean isNative;9091MockStripPlugin(boolean isNative) {92this.isNative = isNative;93}9495@Override96public void configure(Map<String, String> config) {97if (isNative) {98if (config.get(STRIP_NATIVE_NAME) == null ||99!config.get(STRIP_NATIVE_NAME).equals(OMIT_ARG)) {100throw new AssertionError("Test failed!, Expected native " +101"plugin to be properly configured.");102} else {103System.out.println("DEBUG: native plugin properly configured with: " +104STRIP_NATIVE_NAME + "=" + config.get(STRIP_NATIVE_NAME));105}106}107}108109@Override110public ResourcePool transform(ResourcePool in,111ResourcePoolBuilder out) {112in.transformAndCopy((r) -> {return r; }, out); // identity113String resPath = JAVA_PATH;114ResourcePoolEntry.Type type = Type.CLASS_OR_RESOURCE;115if (isNative) {116resPath = NATIVE_PATH;117type = Type.NATIVE_LIB;118}119ResourcePoolEntry entry = createMockEntry(resPath, type);120out.add(entry);121return out.build();122}123124private ResourcePoolEntry createMockEntry(String path,125ResourcePoolEntry.Type type) {126byte[] mockContent = new byte[] { 0, 1, 2, 3 };127ResourcePoolEntry entry = ResourcePoolEntry.create(path,128type,129mockContent);130return entry;131}132133}134135public static class TestNativeStripPluginFactory implements NativePluginFactory {136137private final MockStripPlugin plugin;138139TestNativeStripPluginFactory(MockStripPlugin plugin) {140this.plugin = plugin;141}142143@Override144public Plugin create() {145return plugin;146}147148}149}150151152