|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, Jamie Mansfield <https://jamiemansfield.me/> |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * Redistributions of source code must retain the above copyright notice, this |
| 9 | + * list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * Neither the name of the copyright holder nor the names of its |
| 16 | + * contributors may be used to endorse or promote products derived from |
| 17 | + * this software without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 23 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 25 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 26 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 27 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | + |
| 31 | +package org.cadixdev.bombe.asm.test; |
| 32 | + |
| 33 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 35 | + |
| 36 | +import org.cadixdev.bombe.asm.jar.JarEntryRemappingTransformer; |
| 37 | +import org.cadixdev.bombe.jar.JarClassEntry; |
| 38 | +import org.cadixdev.bombe.jar.JarManifestEntry; |
| 39 | +import org.cadixdev.bombe.jar.JarServiceProviderConfigurationEntry; |
| 40 | +import org.cadixdev.bombe.jar.ServiceProviderConfiguration; |
| 41 | +import org.junit.jupiter.api.Test; |
| 42 | +import org.objectweb.asm.ClassReader; |
| 43 | +import org.objectweb.asm.ClassWriter; |
| 44 | +import org.objectweb.asm.Opcodes; |
| 45 | +import org.objectweb.asm.commons.Remapper; |
| 46 | +import org.objectweb.asm.tree.ClassNode; |
| 47 | + |
| 48 | +import java.io.IOException; |
| 49 | +import java.util.Collections; |
| 50 | +import java.util.jar.Manifest; |
| 51 | + |
| 52 | +/** |
| 53 | + * Unit tests pertaining to {@link JarEntryRemappingTransformer}. |
| 54 | + */ |
| 55 | +public final class JarEntryRemappingTransformerTests { |
| 56 | + |
| 57 | + private static final Remapper REMAPPER = new Remapper() { |
| 58 | + @Override |
| 59 | + public String map(final String internalName) { |
| 60 | + if ("a".equals(internalName)) return "pkg/Demo"; |
| 61 | + if ("b".equals(internalName)) return "pkg/DemoTwo"; |
| 62 | + return internalName; |
| 63 | + } |
| 64 | + }; |
| 65 | + private static final JarEntryRemappingTransformer TRANSFORMER = |
| 66 | + new JarEntryRemappingTransformer(REMAPPER); |
| 67 | + |
| 68 | + @Test |
| 69 | + public void remapsClasses() { |
| 70 | + // Create a test class |
| 71 | + final ClassWriter obf = new ClassWriter(0); |
| 72 | + obf.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "a", null, "java/lang/Object", null); |
| 73 | + |
| 74 | + // Run it through the transformer |
| 75 | + final JarClassEntry entry = TRANSFORMER.transform(new JarClassEntry("a.class", 0, obf.toByteArray())); |
| 76 | + assertEquals("pkg/Demo.class", entry.getName()); |
| 77 | + |
| 78 | + // Verify the contents |
| 79 | + final ClassNode node = new ClassNode(); |
| 80 | + final ClassReader reader = new ClassReader(entry.getContents()); |
| 81 | + reader.accept(node, 0); |
| 82 | + assertEquals("pkg/Demo", node.name); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void remapsMainClass() { |
| 87 | + final Manifest obfManifest = new Manifest(); |
| 88 | + { |
| 89 | + obfManifest.getMainAttributes().putValue("Manifest-Version", "1.0"); |
| 90 | + obfManifest.getMainAttributes().putValue("Main-Class", "a"); |
| 91 | + } |
| 92 | + |
| 93 | + final JarManifestEntry manifestEntry = TRANSFORMER.transform(new JarManifestEntry(0, obfManifest)); |
| 94 | + final Manifest deobfManifest = manifestEntry.getManifest(); |
| 95 | + assertEquals("pkg.Demo", deobfManifest.getMainAttributes().getValue("Main-Class")); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void remapsSimpleManifest() { |
| 100 | + final Manifest obfManifest = new Manifest(); |
| 101 | + { |
| 102 | + obfManifest.getMainAttributes().putValue("Manifest-Version", "1.0"); |
| 103 | + } |
| 104 | + |
| 105 | + TRANSFORMER.transform(new JarManifestEntry(0, obfManifest)); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void remapsConfig() { |
| 110 | + final ServiceProviderConfiguration obfConfig = new ServiceProviderConfiguration( |
| 111 | + "a", |
| 112 | + Collections.singletonList("b") |
| 113 | + ); |
| 114 | + final ServiceProviderConfiguration deobfConfig = |
| 115 | + TRANSFORMER.transform(new JarServiceProviderConfigurationEntry(0, obfConfig)).getConfig(); |
| 116 | + assertEquals("pkg.Demo", deobfConfig.getService()); |
| 117 | + assertEquals(1, deobfConfig.getProviders().size()); |
| 118 | + assertTrue(deobfConfig.getProviders().contains("pkg.DemoTwo"), "Provider not present!"); |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments