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
|
package model.csr;
import model.TestConstants;
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 AttributeTest {
@Test
void testConstructor() throws ParseException {
assertArrayEquals(OID_EXTENSION_REQUEST, TestConstants.CSR_ATTR_2.getType().getInts());
assertEquals(0x21, TestConstants.CSR_ATTR_2.getValues().getArray()[1].getLength());
}
@Test
void testParse() throws ParseException {
final Attribute parsed = new Attribute(new BytesReader(TestConstants.CSR_ATTR_VALUES_2_DER), false);
assertArrayEquals(OID_EXTENSION_REQUEST, parsed.getType().getInts());
assertEquals(2, parsed.getValues().getArray().length);
}
@Test
void testParseFail() {
// No type
assertThrows(ParseException.class, () -> new Attribute(new BytesReader(new Byte[]{
0x30, 0x0
}), false));
// No value
assertThrows(ParseException.class, () -> new Attribute(new BytesReader(new Byte[]{
0x30, 0x5, 0x6, 0x3, 0x55, 0x4, 0x6
}), false));
// Incorrect type tag (should be OID)
assertThrows(ParseException.class, () -> new Attribute(new BytesReader(new Byte[]{
0x30, 0x9, 0x7, 0x3, 0x55, 0x4, 0x6, 0x13, 0x2, 0x49, 0x54
}), false));
// Incorrect value tag (should be SET)
assertThrows(ParseException.class, () -> new Attribute(new BytesReader(new Byte[]{
0x30, 13,
0x06, 0x09, 0x1A, -122, 0x48, -122, -9, 0x0D, 0x01, 0x09, 0x0E,
0x30, 0
}), false));
}
@Test
void testEncode() {
assertArrayEquals(TestConstants.CSR_ATTR_1_DER, TestConstants.CSR_ATTR_1.encodeDER());
assertArrayEquals(TestConstants.CSR_ATTR_VALUES_2_DER, TestConstants.CSR_ATTR_2.encodeDER());
}
}
|