aboutsummaryrefslogtreecommitdiff
path: root/src/test/persistence/FSTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/persistence/FSTest.java')
-rw-r--r--src/test/persistence/FSTest.java50
1 files changed, 50 insertions, 0 deletions
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"));
+ }
+}