aboutsummaryrefslogtreecommitdiff
path: root/src/test/model/asn1/BitStringTest.java
blob: b2472fd47782a427d569783b3a029b5f8833fa9a (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
package model.asn1;

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

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

public class BitStringTest {
    @Test
    void testConstructor() {
        assertArrayEquals(new Byte[]{0x2, 0x3},
                new BitString(BitString.TAG, null, 0, new Byte[]{0x2, 0x3})
                        .getVal());
        assertEquals(3,
                new BitString(BitString.TAG, null, 3, new Byte[]{0x2, 0x8})
                        .getUnused());
    }

    @Test
    void testConvert() {
        // 00000010 00001000
        // 00000000 01000001 = 65
        assertArrayEquals(new Byte[]{65},
                new BitString(BitString.TAG, null, 3, new Byte[]{0x2, 0x8})
                        .getConvertedVal());

        assertArrayEquals(new Byte[]{0x2, 0x8},
                new BitString(BitString.TAG, null, 0, new Byte[]{0x2, 0x8})
                        .getConvertedVal());
    }

    @Test
    void testParse() throws ParseException {
        assertArrayEquals(new Byte[]{0x6e, 0x5d, -64},
                new BitString(new BytesReader(new Byte[]{0x03, 0x04, 0x06, 0x6e, 0x5d, -64}), false)
                        .getVal());
        assertEquals(6,
                new BitString(new BytesReader(new Byte[]{0x03, 0x04, 0x06, 0x6e, 0x5d, -64}), false)
                        .getUnused());
        assertArrayEquals(new Byte[]{0x01, -71, 0x77},
                new BitString(new BytesReader(new Byte[]{0x03, 0x04, 0x06, 0x6e, 0x5d, -64}), false)
                        .getConvertedVal());
    }

    @Test
    void testParseFail() {
        assertThrows(ParseException.class, () ->
                new BitString(new BytesReader(new Byte[]{0x03, 0x04}), false));
        // 0b11100000
        assertThrows(ParseException.class, () ->
                new BitString(new BytesReader(new Byte[]{0x03, 0x04, 0x06, 0x6e, 0x5d, -32}), false));
        // 0b11000001
        assertThrows(ParseException.class, () ->
                new BitString(new BytesReader(new Byte[]{0x03, 0x04, 0x06, 0x6e, 0x5d, -63}), false));
        // Unused bits = 8
        assertThrows(ParseException.class, () ->
                new BitString(new BytesReader(new Byte[]{0x03, 0x04, 0x08, 0x6e, 0x5d, -64}), false));
        // Unused bits = 6 -> 7
        assertThrows(ParseException.class, () ->
                new BitString(new BytesReader(new Byte[]{0x03, 0x04, 0x07, 0x6e, 0x5d, -64}), false));
        // Illegal unused bits: 8 and -1
        assertThrows(ParseException.class, () ->
                new BitString(new BytesReader(new Byte[]{0x03, 0x04, 0x08, 0x6e, 0x5d, -64}), false));
        assertThrows(ParseException.class, () ->
                new BitString(new BytesReader(new Byte[]{0x03, 0x04, -1, 0x6e, 0x5d, -64}), false));
    }

    @Test
    void testEncode() throws ParseException {
        assertArrayEquals(new Byte[]{0x03, 0x04, 0x06, 0x6e, 0x5d, -64},
                new BitString(new BytesReader(new Byte[]{0x03, 0x04, 0x06, 0x6e, 0x5d, -64}), false)
                        .encodeDER());
    }
}