aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorYuuta Liang <yuutaw@student.cs.ubc.ca>2023-10-26 05:00:12 +0800
committerYuuta Liang <yuutaw@student.cs.ubc.ca>2023-10-26 05:00:12 +0800
commit578b7d1db256d9a582cef45ae5d13d858a977416 (patch)
treeb856cc5af32a0d649321f501f2966d013cade6c0 /src/test
parentf73bca3372a31f360d894dcbe8580cef779af739 (diff)
downloadjca-578b7d1db256d9a582cef45ae5d13d858a977416.tar
jca-578b7d1db256d9a582cef45ae5d13d858a977416.tar.gz
jca-578b7d1db256d9a582cef45ae5d13d858a977416.tar.bz2
jca-578b7d1db256d9a582cef45ae5d13d858a977416.zip
Add persistence
Signed-off-by: Yuuta Liang <yuutaw@student.cs.ubc.ca>
Diffstat (limited to 'src/test')
-rw-r--r--src/test/persistence/DecoderTest.java81
-rw-r--r--src/test/persistence/FSTest.java50
-rw-r--r--src/test/ui/UtilsTest.java6
3 files changed, 137 insertions, 0 deletions
diff --git a/src/test/persistence/DecoderTest.java b/src/test/persistence/DecoderTest.java
new file mode 100644
index 0000000..96f94e7
--- /dev/null
+++ b/src/test/persistence/DecoderTest.java
@@ -0,0 +1,81 @@
+package persistence;
+
+import model.asn1.exceptions.InvalidDBException;
+import model.ca.CertificationAuthority;
+import org.json.JSONObject;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.nio.file.Path;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+public class DecoderTest {
+ private JSONObject invalidKey1;
+ private JSONObject invalidKey2;
+ private JSONObject invalidCert;
+ private JSONObject invalidTemplate1;
+ private JSONObject invalidTemplate2;
+ private JSONObject invalidSigned;
+ private JSONObject invalidRevoked1;
+ private JSONObject invalidRevoked2;
+ private JSONObject invalidLog;
+
+ private JSONObject validMinimal;
+ private JSONObject validFull;
+
+ @BeforeEach
+ void setup() {
+ invalidKey1 = FS.read(Path.of("data", "invalid_key_1.json"));
+ invalidKey2 = FS.read(Path.of("data", "invalid_key_2.json"));
+ invalidCert = FS.read(Path.of("data", "invalid_cert.json"));
+ invalidTemplate1 = FS.read(Path.of("data", "invalid_template_1.json"));
+ invalidTemplate2 = FS.read(Path.of("data", "invalid_template_2.json"));
+ invalidSigned = FS.read(Path.of("data", "invalid_signed.json"));
+ invalidRevoked1 = FS.read(Path.of("data", "invalid_revoked_1.json"));
+ invalidRevoked2 = FS.read(Path.of("data", "invalid_revoked_2.json"));
+ invalidLog = FS.read(Path.of("data", "invalid_log.json"));
+
+ validMinimal = FS.read(Path.of("data", "valid_minimal.json"));
+ validFull = FS.read(Path.of("data", "valid_full.json"));
+ }
+
+ @Test
+ void testDecodeSuccessful() throws Throwable {
+ CertificationAuthority ca = Decoder.decodeCA(validMinimal);
+ assertNull(ca.getPublicKey());
+ assertEquals(CertificationAuthority.SERIAL_DEFAULT, ca.getSerial());
+ assertEquals(0, ca.getTemplates().size());
+ assertEquals(0, ca.getSigned().size());
+ assertEquals(0, ca.getLogs().size());
+ assertEquals(0, ca.getRevoked().size());
+ assertNull(ca.getCertificate());
+
+ ca = Decoder.decodeCA(validFull);
+ assertNotNull(ca.getPublicKey());
+ assertNotNull(ca.getKey());
+ assertNotNull(ca.getCertificate());
+ assertEquals(1, ca.getRevoked().size());
+ assertEquals(2, ca.getSigned().size());
+ assertEquals(7, ca.getLogs().size());
+ }
+
+ @Test
+ void testDecodeFail() throws Throwable {
+ assertThrows(InvalidDBException.class, () -> Decoder.decodeCA(invalidKey1));
+ assertThrows(InvalidDBException.class, () -> Decoder.decodeCA(invalidKey2));
+ assertThrows(InvalidDBException.class, () -> Decoder.decodeCA(invalidCert));
+ assertThrows(InvalidDBException.class, () -> Decoder.decodeCA(invalidSigned));
+ assertThrows(InvalidDBException.class, () -> Decoder.decodeCA(invalidTemplate1));
+ assertThrows(InvalidDBException.class, () -> Decoder.decodeCA(invalidTemplate2));
+ assertThrows(InvalidDBException.class, () -> Decoder.decodeCA(invalidLog));
+ assertThrows(InvalidDBException.class, () -> Decoder.decodeCA(invalidRevoked1));
+ assertThrows(InvalidDBException.class, () -> Decoder.decodeCA(invalidRevoked2));
+ }
+
+ @Test
+ void testEncode() throws Throwable {
+ assertTrue(validFull.similar(Decoder.encodeCA(Decoder.decodeCA(validFull))));
+ assertTrue(validMinimal.similar(Decoder.encodeCA(Decoder.decodeCA(validMinimal))));
+ }
+}
diff --git a/src/test/persistence/FSTest.java b/src/test/persistence/FSTest.java
new file mode 100644
index 0000000..d8b8660
--- /dev/null
+++ b/src/test/persistence/FSTest.java
@@ -0,0 +1,50 @@
+package persistence;
+
+import model.asn1.exceptions.InvalidDBException;
+import org.json.JSONObject;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class FSTest {
+ @Test
+ void testReadFail() {
+ // open(2) - EPERM
+ assertThrows(InvalidDBException.class, () -> FS.read(Path.of("/dev/mem")));
+ // open(2) - EISDIR
+ assertThrows(InvalidDBException.class, () -> FS.read(Path.of("/")));
+ // open(2) - ENOENT
+ assertThrows(InvalidDBException.class,
+ () -> FS.read(Path.of("919123082901382901", "9210388888888190231")));
+ // Cannot parse
+ assertThrows(InvalidDBException.class, () -> FS.read(Path.of("/dev/null")));
+ assertThrows(InvalidDBException.class, () -> FS.read(Path.of("README.md")));
+ }
+
+ @Test
+ void testReadSuccess() {
+ assertTrue(FS.read(Path.of("data", "valid_full.json")).keySet().contains("serial"));
+ }
+
+ @Test
+ void testWriteFail() {
+ // open(2) - EISDIR
+ assertThrows(IOException.class, () -> FS.write(Path.of("/"), new JSONObject()));
+ // open(2) - EPERM
+ assertThrows(IOException.class, () -> FS.write(Path.of("/dev/abc"), new JSONObject()));
+ // open(2) - ENOENT
+ assertThrows(IOException.class, () -> FS.write(Path.of("asdjiasoda", "jisdsaod"), new JSONObject()));
+ }
+
+ @Test
+ void testWriteSuccess() throws IOException {
+ FS.write(Path.of("data", ".tmp"), new JSONObject());
+ assertTrue(Files.exists(Path.of("data")));
+ Files.delete(Path.of("data", ".tmp"));
+ }
+}
diff --git a/src/test/ui/UtilsTest.java b/src/test/ui/UtilsTest.java
index 3a6c885..23d2b3e 100644
--- a/src/test/ui/UtilsTest.java
+++ b/src/test/ui/UtilsTest.java
@@ -50,6 +50,12 @@ public class UtilsTest {
"LALA");
});
assertThrows(ParseException.class, () -> {
+ Utils.parsePEM(Utils.byteToByte(("-----BEGIN BLABLA-----\n"
+ + "91023921083910298*@34908302130890123890\n"
+ + "-----END BLABLA-----").getBytes(StandardCharsets.UTF_8)),
+ "BLABLA");
+ });
+ assertThrows(ParseException.class, () -> {
Utils.parsePEM(Utils.byteToByte(("-----BEGIN BLABLA-----"
+ "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB"
+ "-----END BLABLA-----").getBytes(StandardCharsets.UTF_8)),