aboutsummaryrefslogtreecommitdiff
path: root/src/test/model/csr/AttributesTest.java
blob: 5c40a65c947d098b569a51a51ea5b285811b14e8 (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
package model.csr;

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

import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Stream;

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

public class AttributesTest {
    @Test
    void testConstructor() {
        assertEquals("10.0.19045.2",
                TestConstants.CSR_ATTRS_2.getArray()[1].getValues().getArray()[0].toString());
        assertArrayEquals(ObjectIdentifier.OID_EXTENSION_REQUEST,
                TestConstants.CSR_ATTRS_2.getArray()[0].getType().getInts());
    }

    @Test
    void testParse() throws ParseException {
        final Attributes parsed = new Attributes(new BytesReader(new Byte[]{
                -96, 30,
                0x30, 0x1C,
                0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, -126, 0x37, 0x0D, 0x02, 0x03,
                0x31, 0x0E,
                0x16, 0x0C, 0x31, 0x30, 0x2E, 0x30, 0x2E, 0x31, 0x39, 0x30, 0x34, 0x35, 0x2E, 0x32
        }), false);
        assertEquals(1, parsed.getArray().length);
        assertEquals("10.0.19045.2", parsed.getArray()[0].getValues().getArray()[0].toString());
    }

    @Test
    void testParseFail() {
        // Incorrect length
        assertThrows(ParseException.class, () -> new Attributes(new BytesReader(new Byte[]{
                -96, 31, // Incorrect
                0x30, 0x1C,
                0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, -126, 0x37, 0x0D, 0x02, 0x03,
                0x31, 0x0E,
                0x16, 0x0C, 0x31, 0x30, 0x2E, 0x30, 0x2E, 0x31, 0x39, 0x30, 0x34, 0x35, 0x2E, 0x32
        }), false));
        // Incorrect child item tag
        assertThrows(ParseException.class, () -> new Attributes(new BytesReader(new Byte[]{
                -96, 30,
                0x31, 0x1C, // Incorrect
                0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, -126, 0x37, 0x0D, 0x02, 0x03,
                0x31, 0x0E,
                0x16, 0x0C, 0x31, 0x30, 0x2E, 0x30, 0x2E, 0x31, 0x39, 0x30, 0x34, 0x35, 0x2E, 0x32
        }), false));
    }

    @Test
    void testEncode() {
        Byte[] a2 = TestConstants.CSR_ATTR_2.encodeDER();
        Byte[] a1 = TestConstants.CSR_ATTR_1.encodeDER();
        assertArrayEquals(
                Stream.of(Arrays.asList((byte) 0x31, (byte) (a2.length + a1.length)),
                                Arrays.asList(a2),
                                Arrays.asList(a1))
                        .flatMap(Collection::stream)
                        .toArray(Byte[]::new),
                TestConstants.CSR_ATTRS_2.encodeDER());
    }
}