From d342a45d98c4795b3a3fe1aaef5236ad4a782b55 Mon Sep 17 00:00:00 2001 From: Yuuta Liang Date: Thu, 12 Oct 2023 12:10:33 +0800 Subject: Implement data structures from X.680, X.501, X.509, and PKCS#10, with X.690 encoding / decoding support The implementation took four days, and it is still a little bit rough. Updated version should arrive soon. Signed-off-by: Yuuta Liang --- src/test/model/csr/AttributeTest.java | 53 +++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/test/model/csr/AttributeTest.java (limited to 'src/test/model/csr/AttributeTest.java') 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()); + } +} -- cgit v1.2.3