aboutsummaryrefslogtreecommitdiff
path: root/src/test/ui/UtilsTest.java
blob: 0b222862082294fcd3ff743a36155764729343db (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
package ui;

import model.asn1.exceptions.ParseException;
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;
import java.util.stream.IntStream;

import static org.junit.jupiter.api.Assertions.*;

public class UtilsTest {
    @Test
    void testByteToByte() {
        final byte[] primitive = new byte[]{1, 1, 4, 5, 1, 4};
        final Byte[] boxed = new Byte[]{1, 1, 4, 5, 1, 4};
        assertArrayEquals(primitive, Utils.byteToByte(boxed));
        assertArrayEquals(boxed, Utils.byteToByte(primitive));
    }

    @Test
    void testBytesToInt() throws ParseException {
        assertEquals(1, Utils.bytesToInt(new Byte[]{1}));
        assertEquals(257, Utils.bytesToInt(new Byte[]{1, 1}));
        assertThrows(ParseException.class, () -> Utils.bytesToInt(new Byte[]{1, -1, -1, -1, -1}));
    }

    @Test
    void testValToByte() {
        assertArrayEquals(new Byte[]{0},
                Utils.valToByte(0));
        assertArrayEquals(new Byte[]{1},
                Utils.valToByte(1));
        assertArrayEquals(new Byte[]{1, 1},
                Utils.valToByte(257));
        assertArrayEquals(new Byte[]{-1},
                Utils.valToByte(-1));
    }

    @Test
    void testParsePEM() throws ParseException {
        assertArrayEquals(IntStream.range(0, 48).mapToObj(i -> (byte) 1).toArray(Byte[]::new),
                Utils.parsePEM(Utils.byteToByte(("-----BEGIN BLABLA-----\n" +
                                "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB\n" +
                                "-----END BLABLA-----").getBytes(StandardCharsets.UTF_8)),
                        "BLABLA"));
        assertThrows(ParseException.class, () -> {
            Utils.parsePEM(Utils.byteToByte(("-----BEGIN BLABLA-----\n" +
                            "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB\n" +
                            "-----END BLABLA-----").getBytes(StandardCharsets.UTF_8)),
                    "LALA");
        });
        assertThrows(ParseException.class, () -> {
            Utils.parsePEM(Utils.byteToByte(("-----BEGIN BLABLA-----" +
                            "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB" +
                            "-----END BLABLA-----").getBytes(StandardCharsets.UTF_8)),
                    "BLABLA");
        });
    }

    @Test
    void testToPEM() {
        assertEquals("-----BEGIN ABC-----\n" +
                        "AQ==\n" +
                        "-----END ABC-----",
                Utils.toPEM(new Byte[]{0x1}, "ABC"));
    }
}