Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
radioegor146 committed Feb 7, 2024
1 parent 358d4ab commit 926e4c7
Show file tree
Hide file tree
Showing 44 changed files with 762 additions and 15 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ to the directory of the .jar file that this tool will print in `stdout` (by defa

---

### Tests
You need to have [Krakatau](https://github.com/Storyyeller/Krakatau) installed to your PATH, because test suite is using `krak2` for some tests

1. Run `gradlew build` to assemble and run full test suite

This tool uses tests from [huzpsb/JavaObfuscatorTest](https://github.com/huzpsb/JavaObfuscatorTest)

---

In case of any problems, feel free to open an issue or contact me at [re146.dev](https://re146.dev)

### Stargazers over time
Expand Down
21 changes: 15 additions & 6 deletions obfuscator/src/test/java/by/radioegor146/ClassicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void execute() throws Throwable {
Optional<String> mainClassOptional = javaFiles.stream()
.filter(uncheckedPredicate(p -> Files.lines(p).collect(Collectors.joining("\n"))
.matches("(?s).*public(\\s+static)?\\s+void\\s+main.*")))
.map(p -> p.getFileName().toString())
.map(p -> testData.relativize(p).toString().replace('\\', '/'))
.map(f -> f.substring(0, f.lastIndexOf('.')))
.findAny();

Expand All @@ -83,10 +83,13 @@ public void execute() throws Throwable {
throw new RuntimeException("Main class not found");
}

javaFiles.forEach(uncheckedConsumer(p -> Files.copy(p, tempSource.resolve(p.getFileName()))));
krakatauFiles.forEach(uncheckedConsumer(p -> Files.copy(p, tempKrakatauSource.resolve(p.getFileName()))));
resourceFiles.forEach(uncheckedConsumer(p -> {
Path target = temp.resolve(testData.relativize(p));
javaFiles.forEach(uncheckedConsumer(p -> {
Path target = tempSource.resolve(testData.relativize(p));
Files.createDirectories(target.getParent());
Files.copy(p, target);
}));
krakatauFiles.forEach(uncheckedConsumer(p -> {
Path target = tempKrakatauSource.resolve(testData.relativize(p));
Files.createDirectories(target.getParent());
Files.copy(p, target);
}));
Expand All @@ -110,10 +113,16 @@ public void execute() throws Throwable {
}));
}

System.out.println("Copying resources");
resourceFiles.forEach(uncheckedConsumer(p -> {
Path target = tempClasses.resolve(testData.relativize(p));
Files.createDirectories(target.getParent());
Files.copy(p, target);
}));

List<String> jarParameters = new ArrayList<>(Arrays.asList(
"jar", "cvfe", idealJar.toString(), mainClassOptional.get(),
"-C", tempClasses + File.separator, "."));
resourceFiles.stream().map(Path::toString).forEach(jarParameters::add);
ProcessHelper.run(temp, 10_000,
jarParameters)
.check("Jar command");
Expand Down
28 changes: 19 additions & 9 deletions obfuscator/src/test/java/by/radioegor146/TestsGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class TestsGenerator {

private final static List<String> ALLOWED_TESTS = null; /*
// private final static List<String> ALLOWED_TESTS = null; /*
private final static List<String> ALLOWED_TESTS = Arrays.asList(
"StringTokenizer"
"JavaObfuscatorTest"
); // */

private static boolean testAllowed(Path testPath) {
Expand All @@ -38,8 +39,21 @@ private static boolean testAllowed(Path testPath) {
return false;
}

public List<DynamicTest> generateTests(Path testDir, boolean useKrakatau) throws IOException {
List<DynamicTest> result = new ArrayList<>();
for (Path path : Files.list(testDir).filter(Files::isDirectory).collect(Collectors.toList())) {
if (hasJavaFiles(path) && testAllowed(path)) {
result.add(DynamicTest.dynamicTest(testDir.relativize(path).toString(),
new ClassicTest(path, testDir.relativize(path).toString(), useKrakatau)));
continue;
}
result.addAll(generateTests(path, useKrakatau));
}
return result;
}

@TestFactory
public Stream<DynamicTest> generateTests() throws URISyntaxException, IOException {
public Collection<DynamicTest> generateTests() throws URISyntaxException, IOException {
URL tests = TestsGenerator.class.getClassLoader().getResource("tests");
Objects.requireNonNull(tests, "No tests dir in resources");

Expand All @@ -57,13 +71,9 @@ public Stream<DynamicTest> generateTests() throws URISyntaxException, IOExceptio
useKrakatau = false;
}

boolean finalUseKrakatau = useKrakatau;

Path testDir = Paths.get(tests.toURI());
return Files.walk(testDir, FileVisitOption.FOLLOW_LINKS).filter(Files::isDirectory)
.filter(TestsGenerator::hasJavaFiles).filter(TestsGenerator::testAllowed)
.map(p -> DynamicTest.dynamicTest(testDir.relativize(p).toString(),
new ClassicTest(p, testDir.relativize(p).toString(), finalUseKrakatau)));

return generateTests(testDir, useKrakatau);
}

private static boolean hasJavaFiles(Path path) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# java-obfuscator-test

Slightly modified tests from [huzpsb/JavaObfuscatorTest](https://github.com/huzpsb/JavaObfuscatorTest)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public class Test {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package pack;

public class Clazz {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package pack;

import pack.tests.basics.accu.Digi;
import pack.tests.basics.cross.Top;
import pack.tests.basics.ctrl.Ctrl;
import pack.tests.basics.inner.Test;
import pack.tests.basics.overwirte.Sub;
import pack.tests.basics.runable.Task;
import pack.tests.basics.sub.Solver;
import pack.tests.bench.Calc;
import pack.tests.reflects.annot.annot;
import pack.tests.reflects.counter.Count;
import pack.tests.reflects.field.FTest;
import pack.tests.reflects.loader.LRun;
import pack.tests.reflects.res.Accesor;
import pack.tests.reflects.retrace.Tracer;
import pack.tests.security.SecTest;

import java.io.File;

public class Main {
public static void main(String[] args) throws Exception {
System.out.println("Obfuscator Test Program");
System.out.println("Author: huzpsb");
System.out.println("Version: 1.0r");
System.out.println("Link: https://github.com/huzpsb/JavaObfuscatorTest");
System.out.println("-------------Test #1: Basics-------------");
System.out.print("Test 1.1: Inheritance ");
try {
new Sub().run();
} catch (Throwable t) {
System.out.println("ERROR");
}
System.out.print("Test 1.2: Cross ");
try {
new Top().run();
} catch (Throwable t) {
System.out.println("ERROR");
}
System.out.print("Test 1.3: Throw ");
try {
new Ctrl().run();
} catch (Throwable t) {
System.out.println("ERROR");
}
System.out.print("Test 1.4: Accuracy ");
try {
new Digi().run();
} catch (Throwable t) {
System.out.println("ERROR");
}
System.out.print("Test 1.5: SubClass ");
try {
new Solver();
} catch (Throwable t) {
System.out.println("ERROR");
}
System.out.print("Test 1.6: Pool ");
try {
new Task().run();
} catch (Throwable t) {
System.out.println("ERROR");
}
System.out.print("Test 1.7: InnerClass ");
try {
new Test().run();
} catch (Throwable t) {
System.out.println("ERROR");
}
System.out.println("-------------Test #2: Reflects-------------");
System.out.print("Test 2.1: Counter ");
try {
new Count().run();
} catch (Throwable t) {
System.out.println("ERROR");
}
System.out.print("Test 2.2: Chinese 通过LMAO\b\b\b\b \n");
System.out.print("Test 2.3: Resource ");
try {
new Accesor().run();
} catch (Throwable t) {
System.out.println("ERROR");
}
System.out.print("Test 2.4: Field ");
try {
new FTest().run();
} catch (Throwable t) {
System.out.println("ERROR");
}
System.out.print("Test 2.5: Loader ");
try {
new LRun().run();
} catch (Throwable t) {
System.out.println("ERROR");
}
System.out.print("Test 2.6: ReTrace ");
try {
new Tracer().run();
} catch (Throwable t) {
System.out.println("ERROR");
}
System.out.print("Test 2.7: Annotation ");
try {
new annot().run();
} catch (Throwable t) {
System.out.println("ERROR");
}
System.out.print("Test 2.8: Sec ");
try {
new SecTest().run();
} catch (Throwable t) {
System.out.println("ERROR");
}
System.out.println("-------------Test #3: Efficiency-------------");
Calc.runAll();
System.out.println("-------------Tests r Finished-------------");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package pack.tests.basics.accu;

public class Digi {
public void run() {
double fl = 0d;
int co = 0;
float fx = 1.1f;
while (true) {
fl += 0.000000000000000001d;
co++;
if (co > 100 || (float) fl == 0.00000000000000002f)
break;
}
if (co == 20) {
fx += 1.3f;
if (fx == 2.4f) {
System.out.println("PASS");
return;
}
System.out.println("FAIL");
} else {
System.out.println("FAIL");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package pack.tests.basics.cross;

public abstract class Abst1 {
public abstract int add(int a, int b);

public int mul(int a, int b) {
return a * b;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package pack.tests.basics.cross;

public interface Inte {
public int mul(int a, int b);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package pack.tests.basics.cross;

public class Top extends Abst1 implements Inte {

public void run() {
if (add(1, 2) == 3) {
if (mul(2, 3) == 6) {
System.out.println("PASS");
return;
}
}
System.out.println("FAIL");
}

@Override
public int add(int a, int b) {
return a + b;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package pack.tests.basics.ctrl;

public class Ctrl {
private String ret = "FAIL";

public void runt() {
if ("a".equals("b")) {
} else {
throw new UnsupportedOperationException();
}
}

public void runf() {
try {
runt();
} catch (RuntimeException e) {
ret = "PASS";
}
try {
runt();
ret = "FAIL";
} catch (Exception e) {

}
}

public void run() {
runf();
System.out.println(ret);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package pack.tests.basics.inner;

public class Exec {
public int fuss = 1;

public void addF() {
fuss += 2;
}

public class Inner {
int i;

public Inner(int p) {
i = p;
}

public void doAdd() {
addF();
fuss += i;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package pack.tests.basics.inner;

public class Test {
public void run() {
Exec exec = new Exec();
Exec.Inner inner1 = exec.new Inner(3);
inner1.doAdd();
Exec.Inner inner2 = exec.new Inner(100);
inner2.doAdd();
if (exec.fuss == 108)
System.out.println("PASS");
else
System.out.println("ERROR");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package pack.tests.basics.overwirte;

public interface Face {
String face(int i);
}
Loading

0 comments on commit 926e4c7

Please sign in to comment.