Avoid duplicate archive scans and repeated sync marker writes
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
package online.astradev.packs;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
|
||||
class ContentSyncTest {
|
||||
@TempDir Path root;
|
||||
|
||||
Path contents() {
|
||||
return root.resolve("ItemsAdder/contents");
|
||||
}
|
||||
|
||||
Path state() {
|
||||
return root.resolve("AstraPacks");
|
||||
}
|
||||
|
||||
ContentSync sync() {
|
||||
return new ContentSync(contents(), state());
|
||||
}
|
||||
|
||||
void bundle(String filename, String owner, String folder, Map<String, String> files)
|
||||
throws Exception {
|
||||
try (var zip = new ZipOutputStream(Files.newOutputStream(root.resolve(filename)))) {
|
||||
entry(
|
||||
zip,
|
||||
"astra-pack.properties",
|
||||
"schema=1\nplugin=" + owner + "\nfolder=" + folder + "\n");
|
||||
for (var file : files.entrySet())
|
||||
entry(zip, ContentSync.PREFIX + folder + "/" + file.getKey(), file.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
void pack(String fingerprint) throws Exception {
|
||||
Path path = root.resolve("ItemsAdder/output/generated.zip");
|
||||
Files.createDirectories(path.getParent());
|
||||
try (var zip = new ZipOutputStream(Files.newOutputStream(path))) {
|
||||
entry(zip, "pack.mcmeta", "{}");
|
||||
entry(zip, ContentSync.MARKER, "{\"astrapacks.build\":\"" + fingerprint + "\"}");
|
||||
}
|
||||
}
|
||||
|
||||
static void entry(ZipOutputStream zip, String name, String text) throws Exception {
|
||||
zip.putNextEntry(new ZipEntry(name));
|
||||
zip.write(text.getBytes(StandardCharsets.UTF_8));
|
||||
zip.closeEntry();
|
||||
}
|
||||
|
||||
@Test
|
||||
void installRestartUpdateAndRemovalPreserveUnmanagedContent() throws Exception {
|
||||
bundle(
|
||||
"First.jar",
|
||||
"First",
|
||||
"first",
|
||||
Map.of("resourcepack/assets/first/items/a.json", "v1", "configs/old.yml", "old"));
|
||||
bundle("Second.jar", "Second", "second", Map.of("configs/items.yml", "items"));
|
||||
var first = sync().sync(root);
|
||||
assertEquals(2, first.modules());
|
||||
assertTrue(first.buildRequired());
|
||||
pack(first.fingerprint());
|
||||
sync().confirmed(first.fingerprint());
|
||||
var unchanged = sync().sync(root);
|
||||
assertEquals(0, unchanged.written());
|
||||
assertFalse(unchanged.buildRequired());
|
||||
Files.writeString(
|
||||
contents().resolve("first/resourcepack/assets/first/items/a.json"),
|
||||
"optimized by ItemsAdder");
|
||||
assertFalse(
|
||||
sync().sync(root).buildRequired(),
|
||||
"Do not undo ItemsAdder source optimization on every restart");
|
||||
Path custom = contents().resolve("first/configs/local.yml");
|
||||
Files.writeString(custom, "mine");
|
||||
Files.writeString(contents().resolve("first/configs/old.yml"), "edited by owner");
|
||||
bundle(
|
||||
"First.jar",
|
||||
"First",
|
||||
"first",
|
||||
Map.of("resourcepack/assets/first/items/a.json", "v2"));
|
||||
Files.delete(root.resolve("Second.jar"));
|
||||
var updated = sync().sync(root);
|
||||
assertTrue(updated.buildRequired());
|
||||
assertEquals(2, updated.removed());
|
||||
assertEquals(
|
||||
"v2",
|
||||
Files.readString(
|
||||
contents().resolve("first/resourcepack/assets/first/items/a.json")));
|
||||
assertEquals("mine", Files.readString(custom));
|
||||
try (var paths = Files.walk(state().resolve("backups"))) {
|
||||
assertTrue(
|
||||
paths.filter(Files::isRegularFile)
|
||||
.anyMatch(
|
||||
p -> {
|
||||
try {
|
||||
return Files.readString(p).equals("edited by owner");
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void interruptedBuildRetriesAndMissingOutputIsRebuilt() throws Exception {
|
||||
bundle("One.jar", "One", "one", Map.of("configs/items.yml", "content"));
|
||||
var result = sync().sync(root);
|
||||
assertTrue(sync().sync(root).buildRequired());
|
||||
pack(result.fingerprint());
|
||||
assertTrue(
|
||||
sync().sync(root).buildRequired(), "ZIP alone must not clear an unconfirmed build");
|
||||
sync().confirmed(result.fingerprint());
|
||||
assertFalse(sync().sync(root).buildRequired());
|
||||
Files.delete(root.resolve("ItemsAdder/output/generated.zip"));
|
||||
assertTrue(sync().sync(root).buildRequired());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsTraversalAndDuplicateAssetsBeforeWriting() throws Exception {
|
||||
bundle("Unsafe.jar", "Unsafe", "unsafe", Map.of("../escape", "bad"));
|
||||
assertThrows(IOException.class, () -> sync().sync(root));
|
||||
assertFalse(Files.exists(contents()));
|
||||
Files.delete(root.resolve("Unsafe.jar"));
|
||||
bundle("One.jar", "One", "one", Map.of("resourcepack/assets/shared/items/a.json", "one"));
|
||||
bundle("Two.jar", "Two", "two", Map.of("resourcepack/assets/shared/items/a.json", "two"));
|
||||
assertThrows(IOException.class, () -> sync().sync(root));
|
||||
assertFalse(Files.exists(contents()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void duplicatePluginVersionsFailBeforeReplacingFiles() throws Exception {
|
||||
bundle("One-1.jar", "One", "one", Map.of("configs/items.yml", "1"));
|
||||
bundle("One-2.jar", "One", "one", Map.of("configs/items.yml", "2"));
|
||||
assertThrows(IOException.class, () -> sync().sync(root));
|
||||
assertFalse(Files.exists(contents()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void itemsAdderUpgradeRebuildsUnchangedBundles() throws Exception {
|
||||
bundle("One.jar", "One", "one", Map.of("configs/items.yml", "content"));
|
||||
Path jar = root.resolve("ItemsAdder.jar");
|
||||
try (var zip = new ZipOutputStream(Files.newOutputStream(jar))) {
|
||||
entry(zip, "plugin.yml", "name: ItemsAdder\nversion: 1\n");
|
||||
}
|
||||
var installed = sync().sync(root);
|
||||
pack(installed.fingerprint());
|
||||
sync().confirmed(installed.fingerprint());
|
||||
assertFalse(sync().sync(root).buildRequired());
|
||||
|
||||
try (var zip = new ZipOutputStream(Files.newOutputStream(jar))) {
|
||||
entry(zip, "plugin.yml", "name: ItemsAdder\nversion: 2\n");
|
||||
}
|
||||
var upgraded = sync().sync(root);
|
||||
assertNotEquals(installed.fingerprint(), upgraded.fingerprint());
|
||||
assertTrue(upgraded.buildRequired());
|
||||
assertEquals("content", Files.readString(contents().resolve("one/configs/items.yml")));
|
||||
assertTrue(Files.exists(state().resolve("pending")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user