Files

36 lines
1.6 KiB
Java

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
/** Tiny helper shared by the demos: read a fixture, print a section header, assert. */
final class Fx {
static final Path DIR = Path.of(System.getProperty("fixtures", "fixtures"));
static String read(String name) {
try { return Files.readString(DIR.resolve(name)); }
catch (IOException e) { throw new RuntimeException(e); }
}
static void h(String s) { System.out.println("== " + s); }
static void check(boolean ok, String what) {
if (!ok) throw new AssertionError(what);
System.out.println("ok: " + what);
}
/** Run, and print the exception class and message instead of a stack trace. */
static void tryIt(String label, java.util.concurrent.Callable<Object> c) {
try {
Object o = c.call();
System.out.println(label + " -> " + describe(o));
} catch (Throwable t) {
System.out.println(label + " !! " + t);
}
}
static String describe(Object o) {
if (o instanceof java.security.PEM p) return "PEM(type=" + p.type() + ") - NOT decoded";
if (o instanceof java.security.KeyPair) return "KeyPair";
if (o instanceof java.security.PrivateKey k) return "PrivateKey " + k.getAlgorithm();
if (o instanceof java.security.PublicKey k) return "PublicKey " + k.getAlgorithm();
if (o instanceof java.security.cert.X509Certificate c) return "X509Certificate " + c.getSubjectX500Principal();
if (o instanceof javax.crypto.EncryptedPrivateKeyInfo e) return "EncryptedPrivateKeyInfo " + e.getAlgName();
return String.valueOf(o);
}
}