aboutsummaryrefslogtreecommitdiff
path: root/src/test/model/csr/AttributeTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/model/csr/AttributeTest.java')
-rw-r--r--src/test/model/csr/AttributeTest.java53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/test/model/csr/AttributeTest.java b/src/test/model/csr/AttributeTest.java
new file mode 100644
index 0000000..f4daa4c
--- /dev/null
+++ b/src/test/model/csr/AttributeTest.java
@@ -0,0 +1,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());
+ }
+}