SHA256
58 lines
3.9 KiB
Java
58 lines
3.9 KiB
Java
package server.archive;
|
|
|
|
import blockchain.*;
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.util.*;
|
|
|
|
/** Offline tests with real signed fixtures and fake upload/download callbacks (no network). */
|
|
public final class M4ArchiveBlockPairCheck {
|
|
private static int checks;
|
|
private static final HexFormat HEX = HexFormat.of();
|
|
@FunctionalInterface interface Action { void run() throws Exception; }
|
|
private static void ok(boolean value) { checks++; if (!value) throw new AssertionError("check " + checks); }
|
|
private static void reject(Class<? extends Exception> type, Action action) throws Exception {
|
|
try { action.run(); } catch (Exception e) { ok(type.isInstance(e)); return; }
|
|
throw new AssertionError("Expected " + type);
|
|
}
|
|
public static void main(String[] args) throws Exception {
|
|
var lines = Files.readAllLines(Path.of(args[0]));
|
|
for (String line : lines.subList(1, lines.size())) {
|
|
String[] v = line.split("\t");
|
|
byte[] core = HEX.parseHex(v[6]), content = HEX.parseHex(v[8]);
|
|
byte[] coreId = new Ans104DataItem(core).id32(), contentId = new Ans104DataItem(content).id32();
|
|
byte[] pair = CoreContentContainer.pack(new Ans104DataItem(core), new Ans104DataItem(content));
|
|
List<byte[]> sent = new ArrayList<>();
|
|
var ack = ArchiveBlockPair.publish(pair, coreId, (raw, id) -> {
|
|
ok(Arrays.equals(new Ans104DataItem(raw).id32(), id)); sent.add(raw); return HEX.formatHex(id);
|
|
});
|
|
ok(sent.size() == 2); ok(Arrays.equals(sent.get(0), content)); ok(Arrays.equals(sent.get(1), core));
|
|
ok(ack.equals(HEX.formatHex(coreId)));
|
|
sent.clear();
|
|
reject(IOException.class, () -> ArchiveBlockPair.publish(pair, coreId, (raw, id) -> {
|
|
sent.add(raw); throw new IOException("CONTENT not accepted");
|
|
}));
|
|
ok(sent.size() == 1); ok(Arrays.equals(sent.get(0), content));
|
|
sent.clear();
|
|
reject(IOException.class, () -> ArchiveBlockPair.publish(pair, coreId, (raw, id) -> {
|
|
sent.add(raw); if (Arrays.equals(id, coreId)) throw new IOException("CORE not accepted"); return null;
|
|
}));
|
|
ok(sent.size() == 2);
|
|
ArchiveBlockPair.publish(pair, coreId, (raw, id) -> { sent.add(raw); return null; });
|
|
ok(sent.size() == 4); ok(Arrays.equals(sent.get(0), sent.get(2))); ok(Arrays.equals(sent.get(1), sent.get(3)));
|
|
byte[] restored = ArchiveBlockPair.download(core, coreId, id -> { ok(Arrays.equals(id, contentId)); return content; });
|
|
ok(Arrays.equals(restored, pair));
|
|
reject(IOException.class, () -> ArchiveBlockPair.download(core, coreId, id -> { throw new IOException("CONTENT not visible yet"); }));
|
|
reject(IllegalArgumentException.class, () -> ArchiveBlockPair.download(core, coreId, id -> core));
|
|
byte[] damaged = content.clone(); damaged[damaged.length - 1] ^= 1;
|
|
reject(IllegalArgumentException.class, () -> ArchiveBlockPair.download(core, coreId, id -> damaged));
|
|
reject(IllegalArgumentException.class, () -> ArchiveBlockPair.download(core, contentId, id -> { throw new AssertionError("Unverified CORE requested CONTENT"); }));
|
|
reject(IllegalArgumentException.class, () -> ArchiveBlockPair.publish(pair, contentId, (raw,id) -> { throw new AssertionError("Wrong CORE published"); }));
|
|
byte[] badPair = CoreContentContainer.pack(new Ans104DataItem(core), new Ans104DataItem(damaged));
|
|
reject(IllegalArgumentException.class, () -> ArchiveBlockPair.publish(badPair, coreId, (raw,id) -> { throw new AssertionError("Invalid CONTENT published"); }));
|
|
}
|
|
System.out.println("PASS: " + checks + " archive pair checks (order, exact retry, missing CONTENT, wrong ID, signature/hash)");
|
|
}
|
|
}
|