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/AttributesTest.java | 69 ++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/test/model/csr/AttributesTest.java (limited to 'src/test/model/csr/AttributesTest.java') diff --git a/src/test/model/csr/AttributesTest.java b/src/test/model/csr/AttributesTest.java new file mode 100644 index 0000000..86a0112 --- /dev/null +++ b/src/test/model/csr/AttributesTest.java @@ -0,0 +1,69 @@ +package model.csr; + +import model.TestConstants; +import model.asn1.ObjectIdentifier; +import model.asn1.exceptions.ParseException; +import model.asn1.parsing.BytesReader; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Collection; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.*; + +public class AttributesTest { + @Test + void testConstructor() { + assertEquals("10.0.19045.2", + TestConstants.CSR_ATTRS_2.getArray()[1].getValues().getArray()[0].toString()); + assertArrayEquals(ObjectIdentifier.OID_EXTENSION_REQUEST, + TestConstants.CSR_ATTRS_2.getArray()[0].getType().getInts()); + } + + @Test + void testParse() throws ParseException { + final Attributes parsed = new Attributes(new BytesReader(new Byte[]{ + -96, 30, + 0x30, 0x1C, + 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, -126, 0x37, 0x0D, 0x02, 0x03, + 0x31, 0x0E, + 0x16, 0x0C, 0x31, 0x30, 0x2E, 0x30, 0x2E, 0x31, 0x39, 0x30, 0x34, 0x35, 0x2E, 0x32 + }), false); + assertEquals(1, parsed.getArray().length); + assertEquals("10.0.19045.2", parsed.getArray()[0].getValues().getArray()[0].toString()); + } + + @Test + void testParseFail() { + // Incorrect length + assertThrows(ParseException.class, () -> new Attributes(new BytesReader(new Byte[]{ + -96, 31, // Incorrect + 0x30, 0x1C, + 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, -126, 0x37, 0x0D, 0x02, 0x03, + 0x31, 0x0E, + 0x16, 0x0C, 0x31, 0x30, 0x2E, 0x30, 0x2E, 0x31, 0x39, 0x30, 0x34, 0x35, 0x2E, 0x32 + }), false)); + // Incorrect child item tag + assertThrows(ParseException.class, () -> new Attributes(new BytesReader(new Byte[]{ + -96, 30, + 0x31, 0x1C, // Incorrect + 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, -126, 0x37, 0x0D, 0x02, 0x03, + 0x31, 0x0E, + 0x16, 0x0C, 0x31, 0x30, 0x2E, 0x30, 0x2E, 0x31, 0x39, 0x30, 0x34, 0x35, 0x2E, 0x32 + }), false)); + } + + @Test + void testEncode() { + Byte[] a2 = TestConstants.CSR_ATTR_2.encodeDER(); + Byte[] a1 = TestConstants.CSR_ATTR_1.encodeDER(); + assertArrayEquals( + Stream.of(Arrays.asList(new Byte[]{ 0x31, (byte)(a2.length + a1.length) }), + Arrays.asList(a2), + Arrays.asList(a1)) + .flatMap(Collection::stream) + .toArray(Byte[]::new), + TestConstants.CSR_ATTRS_2.encodeDER()); + } +} -- cgit v1.2.3