package model.x501; import model.asn1.*; import model.asn1.exceptions.ParseException; import model.asn1.parsing.BytesReader; import model.TestConstants; import org.junit.jupiter.api.Test; import java.util.Arrays; import static model.asn1.ASN1Object.TAG_SET; import static model.asn1.ObjectIdentifier.OID_C; import static model.asn1.ObjectIdentifier.OID_OU; import static org.junit.jupiter.api.Assertions.*; public class RelativeDistinguishedNameTest { @Test void testConstructor() throws ParseException { assertArrayEquals(OID_OU, new RelativeDistinguishedName(TAG_SET, null, new AttributeTypeAndValue[]{ new AttributeTypeAndValue( ASN1Object.TAG_SEQUENCE, null, new ObjectIdentifier(ObjectIdentifier.TAG, null, OID_OU), new Null(Null.TAG, null)) }).getArray()[0].getType().getInts()); assertEquals("123", new RelativeDistinguishedName(TAG_SET, null, new AttributeTypeAndValue[]{ new AttributeTypeAndValue( ASN1Object.TAG_SEQUENCE, null, new ObjectIdentifier(ObjectIdentifier.TAG, null, OID_OU), new PrintableString(PrintableString.TAG, null, "123")) }).getArray()[0].getValue().toString()); } @Test void testParse() throws ParseException { assertEquals(1, TestConstants.L_MILANO.getArray().length); assertEquals("Milano", TestConstants.L_MILANO.getArray()[0].getValue().toString()); assertEquals(2, TestConstants.L_MILANO_CN_TEST_ED25519.getArray().length); assertEquals("Test ed25519", TestConstants.L_MILANO_CN_TEST_ED25519.getArray()[0].getValue().toString()); } @Test void testParseFail() { // Invalid child tag assertThrows(ParseException.class, () -> new RelativeDistinguishedName(new BytesReader(new Byte[] { 0x31, 0x0F, 0x31, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x06, 0x4D, 0x69, 0x6C, 0x61, 0x6E, 0x6F }), false)); assertThrows(ParseException.class, () -> new RelativeDistinguishedName(new BytesReader(new Byte[] { 0x31, 0x23, // CN 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x54, 0x65, 0x73, 0x74, 0x20, 0x65, 0x64, 0x32, 0x35, 0x35, 0x31, 0x39, // L 0x31, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x06, 0x4D, 0x69, 0x6C, 0x61, 0x6E, 0x6F }), false)); } @Test void testEncode() throws ParseException { assertArrayEquals(TestConstants.combine((byte) 0x31, TestConstants.CN_TEST_ED25519_DER, TestConstants.L_MILANO_DER), TestConstants.L_MILANO_CN_TEST_ED25519.encodeDER()); assertArrayEquals(new Byte[]{ 0x31, 15, 0x30, 13, 0x06, 3, 0x55, 0x04, 0x06, 0x13, 6, '1', '2', '3', '1', '2', '3' }, new RelativeDistinguishedName(TAG_SET, null, new AttributeTypeAndValue[]{ new AttributeTypeAndValue( ASN1Object.TAG_SEQUENCE, null, new ObjectIdentifier(ObjectIdentifier.TAG, null, OID_C), new PrintableString(PrintableString.TAG, null, "123123"))}) .encodeDER()); } @Test void testToString() { assertEquals("L=Milano", TestConstants.L_MILANO.toString()); assertEquals("CN=Test ed25519+L=Milano", TestConstants.L_MILANO_CN_TEST_ED25519.toString()); } }