Skip to content

Commit

Permalink
Reorganize sample directory structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Col-E committed Feb 21, 2022
1 parent 8532130 commit 96e552d
Show file tree
Hide file tree
Showing 48 changed files with 30 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/test/java/me/coley/cafedude/CrasherPatchingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void testPatch(File sub) {
*/
public static List<File> supply() {
List<File> files = new ArrayList<>();
File root = new File("src/test/resources/samples/crasher");
File root = new File("src/test/resources/samples/obfuscated/crasher");
for (File sub : Objects.requireNonNull(root.listFiles())) {
if (sub.getName().endsWith(".class"))
files.add(sub);
Expand Down
65 changes: 29 additions & 36 deletions src/test/java/me/coley/cafedude/EqualityIOTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,53 @@
import me.coley.cafedude.classfile.ClassFile;
import me.coley.cafedude.io.ClassFileReader;
import me.coley.cafedude.io.ClassFileWriter;
import org.junit.jupiter.api.Test;
import me.coley.cafedude.transform.IllegalStrippingTransformer;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Objects;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.*;

/**
* Test that asserts the classes that are read and written back to are identical.
*/
public class EqualityIOTest {
private File lastLoaded;
private int successes;

@Test
public void testSamples() {
@ParameterizedTest
@MethodSource("supply")
public void testEquality(Path sub) {
try {
visitRootDir("samples/annos");
visitRootDir("samples/modules");
visitRootDir("samples/javac");
visitRootDir("samples/misc");
byte[] code = Files.readAllBytes(sub);
ClassFile cf = new ClassFileReader().read(code);
new IllegalStrippingTransformer(cf).transform();
byte[] out = new ClassFileWriter().write(cf);
assertEquals(code.length, out.length, "Class difference for: " + cf.getName());
assertArrayEquals(code, out);
} catch (IOException e) {
System.err.println(lastLoaded.getPath());
fail("Failed to read class, IO error", e);
} catch (InvalidClassException e) {
System.err.println(lastLoaded.getPath());
fail("Failed to read class, invalid class", e);
}
System.out.println("Successes: " + successes);
}

private void assertReadWriteEquality(byte[] code) throws InvalidClassException {
ClassFile cf = new ClassFileReader().read(code);
byte[] out = new ClassFileWriter().write(cf);
assertEquals(code.length, out.length, "Class difference for: " + cf.getName());
assertArrayEquals(code, out);
}

private void visitRootDir(String dir) throws IOException, InvalidClassException {
File root = new File("src/test/resources/" + dir);
visit(root);
}

private void visit(File file) throws IOException, InvalidClassException {
if (file.isDirectory()) {
for (File sub : Objects.requireNonNull(file.listFiles()))
visit(sub);
} else if (file.getName().endsWith(".class")) {
lastLoaded = file;
byte[] code = Files.readAllBytes(file.toPath());
assertReadWriteEquality(code);
successes++;
/**
* @return Test files to check.
*/
public static List<Path> supply() {
try {
BiPredicate<Path, BasicFileAttributes> filter =
(path, attrib) -> attrib.isRegularFile() && path.toString().endsWith(".class");
return Files.find(Paths.get("src/test/resources/samples/normal"), 25, filter)
.collect(Collectors.toList());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
File renamed without changes.
File renamed without changes.
Binary file not shown.
File renamed without changes.

0 comments on commit 96e552d

Please sign in to comment.