Skip to content

Commit f37ee3d

Browse files
committed
refactoring tests
1 parent ea78c3a commit f37ee3d

3 files changed

Lines changed: 115 additions & 95 deletions

File tree

src/test/java/jadx/plugins/stringdecoder/JadxStringDecoderPluginTest.java renamed to src/test/java/jadx/plugins/stringdecoder/B64DeobfuscatePassTest.java

Lines changed: 1 addition & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
package jadx.plugins.stringdecoder;
22

3-
import jadx.api.JadxArgs;
4-
import jadx.api.JadxDecompiler;
5-
import jadx.api.JavaClass;
63
import org.junit.jupiter.api.Test;
74

8-
import java.io.File;
9-
import java.net.URISyntaxException;
10-
import java.net.URL;
115
import java.util.Map;
126

137
import static org.assertj.core.api.Assertions.assertThat;
148

15-
class JadxStringDecoderPluginTest {
9+
class B64DeobfuscatePassTest extends PluginTestBase {
1610

1711
@Test
1812
public void integrationTest() throws Exception {
@@ -202,49 +196,6 @@ public void minDecodedLengthAllowsLongDecodeTest() throws Exception {
202196
assertThat(code).contains("b64: Hello, World!");
203197
}
204198

205-
@Test
206-
public void byteArrayStringTest() throws Exception {
207-
// byte[] field whose bytes are ASCII "Hello, World!" — plugin should add a bytes: comment
208-
String code = decompileSmali("bytes/byte_array_string.smali");
209-
System.out.println(code);
210-
assertThat(code).contains("bytes: \"Hello, World!\"");
211-
}
212-
213-
@Test
214-
public void intArrayUtf8Test() throws Exception {
215-
// int[] storing "café" as unsigned UTF-8 bytes {99, 97, 102, 195, 169}.
216-
// Values 195 and 169 exceed signed byte range; byte[] would need negative literals, int[] avoids that.
217-
String code = decompileSmali("bytes/int_array_utf8.smali");
218-
System.out.println(code);
219-
assertThat(code).contains("bytes: \"café\"");
220-
}
221-
222-
@Test
223-
public void twoByteArraysInClinitTest() throws Exception {
224-
// Two byte[] fields initialised sequentially in <clinit> via new-array + fill-array-data.
225-
// Mirrors the real-world Base64$Encoder pattern — both fields should get bytes: comments.
226-
String code = decompileSmali("bytes/two_byte_arrays.smali");
227-
System.out.println(code);
228-
assertThat(code).contains("bytes: \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\"");
229-
assertThat(code).contains("bytes: \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_\"");
230-
}
231-
232-
@Test
233-
public void byteArrayB64AlphabetTest() throws Exception {
234-
// byte[] alphabet field — standard Base64 (+/)
235-
String code = decompileSmali("bytes/b64_alphabet_byte_array.smali");
236-
System.out.println(code);
237-
assertThat(code).contains("bytes: \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\"");
238-
}
239-
240-
@Test
241-
public void intArrayB64AlphabetTest() throws Exception {
242-
// int[] alphabet field — URL-safe Base64 (-_); verifies int[] is treated identically to byte[]
243-
String code = decompileSmali("bytes/b64_alphabet_byte_array.smali");
244-
System.out.println(code);
245-
assertThat(code).contains("bytes: \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_\"");
246-
}
247-
248199
@Test
249200
public void multilineFieldB64Test() throws Exception {
250201
// PEM-style line-wrapped Base64 string passed to Base64.decode — MIME decoder required
@@ -430,16 +381,6 @@ public void multiB64InvokeChainTest() throws Exception {
430381
assertThat(code).contains("b64[1]: getPackageManager");
431382
}
432383

433-
@Test
434-
public void base64EncoderInnerClassTest() throws Exception {
435-
// Synthetic inner class with two byte[] fields (standard and URL-safe Base64 alphabets)
436-
// initialised sequentially in <clinit>. Verifies ByteArrayStringPass recurses into inner classes.
437-
String code = decompileSmali("bytes/base64_encoder_inner_class.smali");
438-
System.out.println(code);
439-
assertThat(code).contains("bytes: \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\"");
440-
assertThat(code).contains("bytes: \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_\"");
441-
}
442-
443384
@Test
444385
public void b64ReflectTryCatchTest() throws Exception {
445386
// Multiple Base64.decode strings inside try-catch blocks with conditional branching.
@@ -455,7 +396,6 @@ public void b64ReflectTryCatchTest() throws Exception {
455396
assertThat(code).contains("b64: getPackageInfo");
456397
}
457398

458-
459399
@Test
460400
public void b64CachedReflectionTest() throws Exception {
461401
// Three Base64.decode strings in a cached-reflection pattern with try-catch + conditionals.
@@ -479,38 +419,4 @@ public void b64InTryCatchTest() throws Exception {
479419
assertThat(code).contains("b64: getPackageInfo");
480420
}
481421

482-
@Test
483-
public void byteArrayStringPassDisabledTest() throws Exception {
484-
// with enableByteArrayStringPass=false the bytes: comment must not appear
485-
String code = decompileSmali("bytes/byte_array_string.smali",
486-
Map.of(opt("enableByteArrayStringPass"), "false"));
487-
System.out.println(code);
488-
assertThat(code).doesNotContain("bytes:");
489-
}
490-
491-
private String decompileSmali(String fileName) throws Exception {
492-
return decompileSmali(fileName, Map.of());
493-
}
494-
495-
private static String opt(String key) {
496-
return JadxStringDecoderPlugin.PLUGIN_ID + "." + key;
497-
}
498-
499-
private String decompileSmali(String fileName, Map<String, String> pluginOptions) throws Exception {
500-
JadxArgs args = new JadxArgs();
501-
args.isShowInconsistentCode();
502-
args.getInputFiles().add(getSampleFile(fileName));
503-
args.setPluginOptions(pluginOptions);
504-
try (JadxDecompiler jadx = new JadxDecompiler(args)) {
505-
jadx.load();
506-
JavaClass cls = jadx.getClasses().get(0);
507-
return cls.getCode();
508-
}
509-
}
510-
511-
private File getSampleFile(String fileName) throws URISyntaxException {
512-
URL file = getClass().getClassLoader().getResource("samples/" + fileName);
513-
assertThat(file).isNotNull();
514-
return new File(file.toURI());
515-
}
516422
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package jadx.plugins.stringdecoder;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Map;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
class ByteArrayStringPassTest extends PluginTestBase {
10+
11+
@Test
12+
public void byteArrayStringTest() throws Exception {
13+
// byte[] field whose bytes are ASCII "Hello, World!" — plugin should add a bytes: comment
14+
String code = decompileSmali("bytes/byte_array_string.smali");
15+
System.out.println(code);
16+
assertThat(code).contains("bytes: \"Hello, World!\"");
17+
}
18+
19+
@Test
20+
public void intArrayUtf8Test() throws Exception {
21+
// int[] storing "café" as unsigned UTF-8 bytes {99, 97, 102, 195, 169}.
22+
// Values 195 and 169 exceed signed byte range; byte[] would need negative literals, int[] avoids that.
23+
String code = decompileSmali("bytes/int_array_utf8.smali");
24+
System.out.println(code);
25+
assertThat(code).contains("bytes: \"café\"");
26+
}
27+
28+
@Test
29+
public void twoByteArraysInClinitTest() throws Exception {
30+
// Two byte[] fields initialised sequentially in <clinit> via new-array + fill-array-data.
31+
// Mirrors the real-world Base64$Encoder pattern — both fields should get bytes: comments.
32+
String code = decompileSmali("bytes/two_byte_arrays.smali");
33+
System.out.println(code);
34+
assertThat(code).contains("bytes: \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\"");
35+
assertThat(code).contains("bytes: \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_\"");
36+
}
37+
38+
@Test
39+
public void byteArrayB64AlphabetTest() throws Exception {
40+
// byte[] alphabet field — standard Base64 (+/)
41+
String code = decompileSmali("bytes/b64_alphabet_byte_array.smali");
42+
System.out.println(code);
43+
assertThat(code).contains("bytes: \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\"");
44+
}
45+
46+
@Test
47+
public void intArrayB64AlphabetTest() throws Exception {
48+
// int[] alphabet field — URL-safe Base64 (-_); verifies int[] is treated identically to byte[]
49+
String code = decompileSmali("bytes/b64_alphabet_byte_array.smali");
50+
System.out.println(code);
51+
assertThat(code).contains("bytes: \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_\"");
52+
}
53+
54+
@Test
55+
public void base64EncoderInnerClassTest() throws Exception {
56+
// Synthetic inner class with two byte[] fields (standard and URL-safe Base64 alphabets)
57+
// initialised sequentially in <clinit>. Verifies ByteArrayStringPass recurses into inner classes.
58+
String code = decompileSmali("bytes/base64_encoder_inner_class.smali");
59+
System.out.println(code);
60+
assertThat(code).contains("bytes: \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\"");
61+
assertThat(code).contains("bytes: \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_\"");
62+
}
63+
64+
@Test
65+
public void byteArrayStringPassDisabledTest() throws Exception {
66+
// with enableByteArrayStringPass=false the bytes: comment must not appear
67+
String code = decompileSmali("bytes/byte_array_string.smali",
68+
Map.of(opt("enableByteArrayStringPass"), "false"));
69+
System.out.println(code);
70+
assertThat(code).doesNotContain("bytes:");
71+
}
72+
73+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package jadx.plugins.stringdecoder;
2+
3+
import jadx.api.JadxArgs;
4+
import jadx.api.JadxDecompiler;
5+
import jadx.api.JavaClass;
6+
7+
import java.io.File;
8+
import java.net.URISyntaxException;
9+
import java.net.URL;
10+
import java.util.Map;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
14+
abstract class PluginTestBase {
15+
16+
protected String decompileSmali(String fileName) throws Exception {
17+
return decompileSmali(fileName, Map.of());
18+
}
19+
20+
protected String decompileSmali(String fileName, Map<String, String> pluginOptions) throws Exception {
21+
JadxArgs args = new JadxArgs();
22+
args.isShowInconsistentCode();
23+
args.getInputFiles().add(getSampleFile(fileName));
24+
args.setPluginOptions(pluginOptions);
25+
try (JadxDecompiler jadx = new JadxDecompiler(args)) {
26+
jadx.load();
27+
JavaClass cls = jadx.getClasses().get(0);
28+
return cls.getCode();
29+
}
30+
}
31+
32+
protected static String opt(String key) {
33+
return JadxStringDecoderPlugin.PLUGIN_ID + "." + key;
34+
}
35+
36+
private File getSampleFile(String fileName) throws URISyntaxException {
37+
URL file = getClass().getClassLoader().getResource("samples/" + fileName);
38+
assertThat(file).isNotNull();
39+
return new File(file.toURI());
40+
}
41+
}

0 commit comments

Comments
 (0)