aboutsummaryrefslogtreecommitdiff
path: root/src/test/model/x501/NameTest.java
blob: c649798e8a09fa5c82c15e769c0735729723fe4e (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package model.x501;

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

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

public class NameTest {
    @Test
    void testConstructor() {
        assertEquals("users",
                TestConstants.NAME_1.getRdnSequence()[1].getArray()[0].getValue().toString());
        assertEquals("CN",
                TestConstants.NAME_2.getRdnSequence()[2].getArray()[0].getValue().toString());
    }

    @Test
    void testParse() throws ParseException {
        assertEquals("CA", new Name(new BytesReader(new Byte[]{
                0x30, 45, // Name

                0x31, 14, // RDN[0]
                0x30, 12, // KV[0]
                0x6, 3, 0x55, 0x04, 0x03, // CN
                0x13, 5, 'y', 'u', 'u', 't', 'a', // yuuta

                0x31, 14, // RDN[1]
                0x30, 12, // KV[0]
                0x6, 3, 0x55, 0x04, 0xB, // OU
                0x13, 5, 'u', 's', 'e', 'r', 's', // users

                0x31, 11, // RDN[2]
                0x30, 9, // KV[0]
                0x6, 3, 0x55, 0x04, 0x6, // C
                0x13, 2, 'C', 'A' // CA
        }), false).getRdnSequence()[2].getArray()[0].getValue().toString());

        assertEquals("SN=Qwq", new Name(new BytesReader(new Byte[]{
                0x30, 38, // Name

                0x31, 12, // RDN[0]
                0x30, 10, // KV[0]
                0x6, 3, 0x55, 0x04, 0x04, // CN
                0x13, 3, 'Q', 'w', 'q', // Qwq

                0x31, 9, // RDN[1]
                0x30, 7, // KV[0]
                0x6, 3, 0x55, 0x04, 0xA, // O
                0x13, 2, 'I', 'T', // IT

                0x31, 11, // RDN[2]
                0x30, 9, // KV[0]
                0x6, 3, 0x55, 0x04, 0x6, // C
                0x13, 2, 'C', 'N' // CN
        }), false).getRdnSequence()[0].toString());
    }

    @Test
    void testParseFail() {
        assertThrows(ParseException.class, () -> new Name(new BytesReader(new Byte[]{
                0x30, 38, // Name

                // Wrong tag here
                0x30, 12, // RDN[0]
                0x30, 10, // KV[0]
                0x6, 3, 0x55, 0x04, 0x04, // CN
                0x13, 3, 'Q', 'w', 'q', // Qwq

                0x31, 9, // RDN[1]
                0x30, 7, // KV[0]
                0x6, 3, 0x55, 0x04, 0xA, // O
                0x13, 2, 'I', 'T', // IT

                0x31, 11, // RDN[2]
                0x30, 9, // KV[0]
                0x6, 3, 0x55, 0x04, 0x6, // C
                0x13, 2, 'C', 'N' // CN
        }), false));
        assertThrows(ParseException.class, () -> new Name(new BytesReader(new Byte[]{
                0x30, 38, // Name

                0x31, 12, // RDN[0]
                0x30, 10, // KV[0]
                0x6, 3, 0x55, 0x04, 0x04, // CN
                0x13, 3, 'Q', 'w', 'q', // Qwq

                0x31, 9, // RDN[1]
                // Wrong tag here
                0x31, 7, // KV[0]
                0x6, 3, 0x55, 0x04, 0xA, // O
                0x13, 2, 'I', 'T', // IT

                0x31, 11, // RDN[2]
                0x30, 9, // KV[0]
                0x6, 3, 0x55, 0x04, 0x6, // C
                0x13, 2, 'C', 'N' // CN
        }), false));
        assertThrows(ParseException.class, () -> new Name(new BytesReader(new Byte[]{
                0x30, 38, // Name

                0x31, 12, // RDN[0]
                0x30, 10, // KV[0]
                0x6, 3, 0x55, 0x04, 0x04, // CN
                0x13, 3, 'Q', 'w', 'q', // Qwq

                0x31, 9, // RDN[1]
                0x30, 7, // KV[0]
                0x6, 3, 0x55, 0x04, 0xA, // O
                0x13, 2, 'I', 'T', // IT

                // Wrong tag here
                0x30, 11, // RDN[2]
                0x30, 9, // KV[0]
                0x6, 3, 0x55, 0x04, 0x6, // C
                0x13, 2, 'C', 'N' // CN
        }), false));
    }

    @Test
    void testEncode() {
        assertArrayEquals(new Byte[]{
                0x30, 45, // Name

                0x31, 14, // RDN[0]
                0x30, 12, // KV[0]
                0x6, 3, 0x55, 0x04, 0x03, // CN
                0x13, 5, 'y', 'u', 'u', 't', 'a', // yuuta

                0x31, 14, // RDN[1]
                0x30, 12, // KV[0]
                0x6, 3, 0x55, 0x04, 0xB, // OU
                0x13, 5, 'u', 's', 'e', 'r', 's', // users

                0x31, 11, // RDN[2]
                0x30, 9, // KV[0]
                0x6, 3, 0x55, 0x04, 0x6, // C
                0x13, 2, 'C', 'A' // CA
        }, TestConstants.NAME_1.encodeDER());

        assertArrayEquals(new Byte[]{
                0x30, 40, // Name

                0x31, 12, // RDN[0]
                0x30, 10, // KV[0]
                0x6, 3, 0x55, 0x04, 0x04, // CN
                0x13, 3, 'Q', 'w', 'q', // Qwq

                0x31, 11, // RDN[1]
                0x30, 9, // KV[0]
                0x6, 3, 0x55, 0x04, 0xA, // O
                0x13, 2, 'I', 'T', // IT

                0x31, 11, // RDN[2]
                0x30, 9, // KV[0]
                0x6, 3, 0x55, 0x04, 0x6, // C
                0x13, 2, 'C', 'N' // CN
        }, TestConstants.NAME_2.encodeDER());
    }

    @Test
    void testToString() {
        assertEquals("CN=yuuta,OU=users,C=CA", TestConstants.NAME_1.toString());
        assertEquals("SN=Qwq,O=IT,C=CN", TestConstants.NAME_2.toString());
    }
}