Skip to content

Commit

Permalink
Move path suffix/prefix test to correct test class
Browse files Browse the repository at this point in the history
  • Loading branch information
Col-E committed Jul 4, 2024
1 parent 23effe1 commit 1f44669
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,17 @@ private void mapInto(@Nonnull Map<String, byte[]> map, @Nonnull WorkspaceResourc
// Place classes into map
resource.jvmClassBundleStream().forEach(bundle -> {
for (JvmClassInfo classInfo : bundle) {
String pathPrefix = PathPrefixProperty.get(classInfo);
String pathSuffix = Objects.requireNonNullElse(PathSuffixProperty.get(classInfo), ".class");
String key = classInfo.getName() + pathSuffix;
if (pathPrefix != null)
key = pathPrefix + key;
String key;
String originalName = PathOriginalNameProperty.get(classInfo);
if (originalName == null) {
String pathPrefix = PathPrefixProperty.get(classInfo);
String pathSuffix = Objects.requireNonNullElse(PathSuffixProperty.get(classInfo), ".class");
key = classInfo.getName() + pathSuffix;
if (pathPrefix != null)
key = pathPrefix + key;
} else {
key = originalName;
}
map.put(key, classInfo.getBytecode());
updateProperties(key, classInfo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,39 +451,4 @@ void testZipProperties() throws IOException {
assertEquals(timeModify, ZipModificationTimeProperty.get(fileInfo), "Missing modification time");
assertEquals(timeAccess, ZipAccessTimeProperty.get(fileInfo), "Missing access time");
}

/**
* There's a lombok fabric mod which bundles some classes with tampered names. The file contents are normal classes.
* When we re-export the workspace we need to ensure the classes are written back to where they originally came from.
*/
@Test
void testLombokOddBehavior() throws IOException {
byte[] inputZipBytes = Files.readAllBytes(Paths.get("src/testFixtures/resources/lombok-sample.jar"));
WorkspaceResource resource = importer.importResource(ByteSources.wrap(inputZipBytes));
BasicWorkspace workspace = new BasicWorkspace(resource);


// Export the workspace
Set<String> paths = new TreeSet<>();
new WorkspaceExportOptions(WorkspaceOutputType.DIRECTORY, new WorkspaceExportConsumer() {
@Override
public void write(@Nonnull byte[] bytes) {
throw new RuntimeException("Should not be invoked in directory output type");
}

@Override
public void writeRelative(@Nonnull String relative, @Nonnull byte[] bytes) {
paths.add(relative);
}

@Override
public void commit() {
// no-op
}
}).create().export(workspace);

// Verify the paths match the input names
assertTrue(paths.contains("SCL.lombok/org/objectweb/asm/Constants.SCL.lombok"),
"Lombok's bundled ASM classes not written to expected path");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package software.coley.recaf.workspace.io;

import jakarta.annotation.Nonnull;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import software.coley.recaf.info.JarFileInfo;
Expand All @@ -16,9 +17,11 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
import java.util.TreeSet;

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

/**
* Tests for {@link WorkspaceExporter}
Expand Down Expand Up @@ -101,4 +104,69 @@ private static void test(WorkspaceOutputType outputType) throws IOException {
assertEquals(targetResource.getFileBundle(), importedResource.getFileBundle());
assertEquals(targetResource.getEmbeddedResources(), importedResource.getEmbeddedResources());
}

/**
* There's a lombok fabric mod which bundles some classes with tampered names. The file contents are normal classes.
* When we re-export the workspace we need to ensure the classes are written back to where they originally came from.
*/
@Test
void testLombokClassPrefixSuffixExport() throws IOException {
byte[] inputZipBytes = Files.readAllBytes(Paths.get("src/testFixtures/resources/name-prefix-suffix.jar"));
WorkspaceResource resource = importer.importResource(ByteSources.wrap(inputZipBytes));
assertNotNull(resource.getJvmClassBundle().get("org/objectweb/asm/Constants"), "Missing ASM classes");
BasicWorkspace workspace = new BasicWorkspace(resource);

// Export the workspace
Set<String> paths = new TreeSet<>();
new WorkspaceExportOptions(WorkspaceOutputType.DIRECTORY, new WorkspaceExportConsumer() {
@Override
public void write(@Nonnull byte[] bytes) {
throw new RuntimeException("Should not be invoked in directory output type");
}

@Override
public void writeRelative(@Nonnull String relative, @Nonnull byte[] bytes) {
paths.add(relative);
}

@Override
public void commit() {
// no-op
}
}).create().export(workspace);

// Verify the paths match the input names
assertTrue(paths.contains("SCL.lombok/org/objectweb/asm/Constants.SCL.lombok"),
"Lombok's bundled ASM classes not written to expected path");
}

@Test
void testNameDifferenceExport() throws IOException {
byte[] inputZipBytes = Files.readAllBytes(Paths.get("src/testFixtures/resources/name-difference.zip"));
WorkspaceResource resource = importer.importResource(ByteSources.wrap(inputZipBytes));
assertNotNull(resource.getJvmClassBundle().get("org/objectweb/asm/Constants"), "Missing ASM classes");
BasicWorkspace workspace = new BasicWorkspace(resource);

// Export the workspace
Set<String> paths = new TreeSet<>();
new WorkspaceExportOptions(WorkspaceOutputType.DIRECTORY, new WorkspaceExportConsumer() {
@Override
public void write(@Nonnull byte[] bytes) {
throw new RuntimeException("Should not be invoked in directory output type");
}

@Override
public void writeRelative(@Nonnull String relative, @Nonnull byte[] bytes) {
paths.add(relative);
}

@Override
public void commit() {
// no-op
}
}).create().export(workspace);

// Verify the paths match the input names
assertTrue(paths.contains("SomethingElse.bin"), "Class was not written back to expected name");
}
}
Binary file not shown.

0 comments on commit 1f44669

Please sign in to comment.