aboutsummaryrefslogtreecommitdiff
path: root/src/test/persistence/DecoderTest.java
blob: 418087b9c6da40056e8ac0c2d7a33cf8ac1ececc (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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() {
        // Dr. Paul: You can make dummy constructor calls to make the coverage happy since they are util classes.
        new FS();
        new Decoder();
        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))));
    }
}