aboutsummaryrefslogtreecommitdiff
path: root/src/test/model/x501/AttributeTypeAndValueTest.java
blob: ea9c17eb0efe70e7b0a2feb7ffbde28a31d2230b (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
85
86
87
88
89
90
package model.x501;

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

import static model.asn1.ObjectIdentifier.*;
import static org.junit.jupiter.api.Assertions.*;

public class AttributeTypeAndValueTest {
    @Test
    void testConstructor() throws ParseException {
        assertArrayEquals(OID_OU,
                new AttributeTypeAndValue(
                        ASN1Object.TAG_SEQUENCE, null,
                        new ObjectIdentifier(ObjectIdentifier.TAG, null, OID_OU),
                        new Null(Null.TAG, null))
                        .getType().getInts());
        assertEquals("123",
                new AttributeTypeAndValue(
                        ASN1Object.TAG_SEQUENCE, null,
                        new ObjectIdentifier(ObjectIdentifier.TAG, null, OID_OU),
                        new PrintableString(PrintableString.TAG, null, "123"))
                        .getValue().toString());
    }

    @Test
    void testParse() throws ParseException {
        // C = IT
        assertArrayEquals(OID_C, new AttributeTypeAndValue(new BytesReader(new Byte[]{
                0x30, 0x9, 0x6, 0x3, 0x55, 0x4, 0x6, 0x13, 0x2, 0x49, 0x54
        }), false).getType().getInts());
        assertEquals("IT", ((PrintableString) new AttributeTypeAndValue(new BytesReader(new Byte[]{
                        0x30, 0x9, 0x6, 0x3, 0x55, 0x4, 0x6, 0x13, 0x2, 0x49, 0x54
                }), false).getValue()).getString());

        // CN = Test ed25519
        assertArrayEquals(OID_CN, new AttributeTypeAndValue(new BytesReader(new Byte[]{
                0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C,
                0x0C, 0x54, 0x65, 0x73, 0x74, 0x20, 0x65, 0x64,
                0x32, 0x35, 0x35, 0x31, 0x39
        }), false).getType().getInts());
        assertEquals("Test ed25519", ((ASN1String) new AttributeTypeAndValue(new BytesReader(new Byte[]{
                0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C,
                0x0C, 0x54, 0x65, 0x73, 0x74, 0x20, 0x65, 0x64,
                0x32, 0x35, 0x35, 0x31, 0x39
        }), false).getValue()).getString());
    }

    @Test
    void testParseFail() {
        // No type
        assertThrows(ParseException.class, () -> new AttributeTypeAndValue(new BytesReader(new Byte[]{
                0x30, 0x0
        }), false));
        // No value
        assertThrows(ParseException.class, () -> new AttributeTypeAndValue(new BytesReader(new Byte[]{
                0x30, 0x5, 0x6, 0x3, 0x55, 0x4, 0x6
        }), false));
        // Incorrect type tag (should be OID)
        assertThrows(ParseException.class, () -> new AttributeTypeAndValue(new BytesReader(new Byte[]{
                0x30, 0x9, 0x7, 0x3, 0x55, 0x4, 0x6, 0x13, 0x2, 0x49, 0x54
        }), false));
    }

    @Test
    void testEncode() throws ParseException {
        assertArrayEquals(new Byte[]{
                0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C,
                0x0C, 0x54, 0x65, 0x73, 0x74, 0x20, 0x65, 0x64,
                0x32, 0x35, 0x35, 0x31, 0x39
        }, new AttributeTypeAndValue(ASN1Object.TAG_SEQUENCE, null,
                new ObjectIdentifier(ObjectIdentifier.TAG, null, OID_CN),
                new UTF8String(UTF8String.TAG, null, "Test ed25519"))
                .encodeDER());
    }

    @Test
    void testToString() throws ParseException {
        assertEquals("CN=Test ed25519", new AttributeTypeAndValue(new BytesReader(new Byte[]{
                0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C,
                0x0C, 0x54, 0x65, 0x73, 0x74, 0x20, 0x65, 0x64,
                0x32, 0x35, 0x35, 0x31, 0x39
        }), false).toString());
        assertEquals("C=IT", new AttributeTypeAndValue(new BytesReader(new Byte[]{
                0x30, 0x9, 0x6, 0x3, 0x55, 0x4, 0x6, 0x13, 0x2, 0x49, 0x54
        }), false).toString());
    }
}