aboutsummaryrefslogtreecommitdiff
path: root/src/test/persistence/FSTest.java
blob: d8b86609a8f837ce4d4336cef638e93c60e62653 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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"));
    }
}