Skip to content

Commit 709d4c6

Browse files
committed
Merge branch 'hotfix-0.4.1'
2 parents cb6c448 + 70e5e5b commit 709d4c6

File tree

6 files changed

+161
-8
lines changed

6 files changed

+161
-8
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
}

bombe-jar/src/main/java/org/cadixdev/bombe/jar/asm/JarEntryRemappingTransformer.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
import java.util.List;
4545
import java.util.function.BiFunction;
46+
import java.util.jar.Attributes;
4647
import java.util.stream.Collectors;
4748

4849
/**
@@ -84,14 +85,17 @@ public JarClassEntry transform(final JarClassEntry entry) {
8485

8586
@Override
8687
public JarManifestEntry transform(final JarManifestEntry entry) {
87-
// Remap the Main-Class attribute
88-
final String mainClassObf = entry.getManifest().getMainAttributes().getValue("Main-Class")
89-
.replace('.', '/');
90-
final String mainClassDeobf = this.remapper.map(mainClassObf)
91-
.replace('/', '.');
88+
// Remap the Main-Class attribute, if present
89+
if (entry.getManifest().getMainAttributes().containsKey(new Attributes.Name("Main-Class"))) {
90+
final String mainClassObf = entry.getManifest().getMainAttributes().getValue("Main-Class")
91+
.replace('.', '/');
92+
final String mainClassDeobf = this.remapper.map(mainClassObf)
93+
.replace('/', '.');
94+
95+
// Since Manifest is mutable, we need'nt create a new entry \o/
96+
entry.getManifest().getMainAttributes().putValue("Main-Class", mainClassDeobf);
97+
}
9298

93-
// Since Manifest is mutable, we need'nt create a new entry \o/
94-
entry.getManifest().getMainAttributes().putValue("Main-Class", mainClassDeobf);
9599
return entry;
96100
}
97101

bombe-jar/src/test/groovy/org/cadixdev/bombe/jar/test/asm/JarEntryRemappingTransformerSpec.groovy

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@ class JarEntryRemappingTransformerSpec extends Specification {
9696
deobf.mainAttributes[name('Main-Class')] == 'pkg.Demo'
9797
}
9898

99+
def "remaps simple manifest"() {
100+
given:
101+
// Create a test Manifest
102+
def obf = new Manifest()
103+
obf.with {
104+
mainAttributes[name('Manifest-Version')] = '1.0'
105+
}
106+
107+
// Run it through the transformer
108+
TRANSFORMER.transform(new JarManifestEntry(0, obf)).manifest
109+
}
110+
99111
def "remaps service config"() {
100112
given:
101113
// Create a test service config

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ subprojects {
1616

1717
group = 'org.cadixdev'
1818
archivesBaseName = project.name.toLowerCase()
19-
version = '0.4.0'
19+
version = '0.4.1'
2020

2121
repositories {
2222
mavenCentral()

changelogs/0.3.2.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Bombe 0.3.2
2+
===========
3+
4+
Bombe 0.3.2 resolves a critical bug with `JarEntryRemappingTransformer` where it
5+
would crash given a manifest with no `Main-Class`.
6+
7+
As Bombe 0.3.x is still in use by the latest version of Lorenz and Atlas, and
8+
used in software running today - this is why a further release to 0.3 is being
9+
made.

changelogs/0.4.1.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Bombe 0.4.1
2+
===========
3+
4+
Bombe 0.4.1 resolves a critical bug with `JarEntryRemappingTransformer` where it
5+
would crash given a manifest with no `Main-Class`.
6+
7+
An equivalent change was made in Bombe 0.3.2.

0 commit comments

Comments
 (0)