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()); } }