package model.asn1; import model.asn1.exceptions.ParseException; import model.asn1.parsing.BytesReader; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class ObjectIdentifierTest { @Test void testConstructor() { assertArrayEquals(new Integer[]{1, 3, 6, 1, 4}, new ObjectIdentifier(ObjectIdentifier.TAG, null, new Integer[]{1, 3, 6, 1, 4}).getInts()); assertArrayEquals(new Integer[]{1, 2, 3, 4, 5}, new ObjectIdentifier(ObjectIdentifier.TAG, null, new Integer[]{1, 2, 3, 4, 5}).getInts()); } @Test void testToString() { assertEquals("1.3.6.1.4", new ObjectIdentifier(ObjectIdentifier.TAG, null, new Integer[]{1, 3, 6, 1, 4}).toString()); assertEquals("1.2.3.4.5.6", new ObjectIdentifier(ObjectIdentifier.TAG, null, new Integer[]{1, 2, 3, 4, 5, 6}).toString()); assertEquals("CN", new ObjectIdentifier(ObjectIdentifier.TAG, null, ObjectIdentifier.OID_CN).toString()); assertEquals("SN", new ObjectIdentifier(ObjectIdentifier.TAG, null, ObjectIdentifier.OID_SN).toString()); assertEquals("C", new ObjectIdentifier(ObjectIdentifier.TAG, null, ObjectIdentifier.OID_C).toString()); assertEquals("L", new ObjectIdentifier(ObjectIdentifier.TAG, null, ObjectIdentifier.OID_L).toString()); assertEquals("O", new ObjectIdentifier(ObjectIdentifier.TAG, null, ObjectIdentifier.OID_O).toString()); assertEquals("OU", new ObjectIdentifier(ObjectIdentifier.TAG, null, ObjectIdentifier.OID_OU).toString()); assertEquals("DC", new ObjectIdentifier(ObjectIdentifier.TAG, null, ObjectIdentifier.OID_DC).toString()); } @Test void testEncode() { assertArrayEquals(new Byte[]{0x55, 0x04, 0x0A}, new ObjectIdentifier(ObjectIdentifier.TAG, null, ObjectIdentifier.OID_O).encodeValueDER()); assertArrayEquals(new Byte[]{0x67, -127, 0x0C, 0x01, 0x02, 0x01}, new ObjectIdentifier(ObjectIdentifier.TAG, null, new Integer[]{2, 23, 140, 1, 2, 1}).encodeValueDER()); assertArrayEquals(new Byte[]{0x2B, 0x06, 0x01, 0x04, 0x01, -126, -33, 0x13, 0x01, 0x01, 0x01}, new ObjectIdentifier(ObjectIdentifier.TAG, null, new Integer[]{1, 3, 6, 1, 4, 1, 44947, 1, 1, 1}).encodeValueDER()); assertArrayEquals(new Byte[]{0x2A, -122, 0x48, -50, 0x3D, 0x02, 0x01}, new ObjectIdentifier(ObjectIdentifier.TAG, null, new Integer[]{1, 2, 840, 10045, 2, 1}).encodeValueDER()); assertArrayEquals(new Byte[]{0x2A, -122, 0x48, -122, -9, 0x0D, 0x01, 0x01, 0x0B}, new ObjectIdentifier(ObjectIdentifier.TAG, null, ObjectIdentifier.OID_SHA256_WITH_RSA_ENCRYPTION).encodeValueDER()); } @Test void testParse() throws ParseException { assertArrayEquals(ObjectIdentifier.OID_SHA256_WITH_RSA_ENCRYPTION, new ObjectIdentifier(new BytesReader(new Byte[]{0x6, 0x9, 0x2A, -122, 0x48, -122, -9, 0x0D, 0x01, 0x01, 0x0B}), false).getInts()); assertArrayEquals(new Integer[]{1, 2, 840, 10045, 2, 1}, new ObjectIdentifier(new BytesReader(new Byte[]{0x6, 7, 0x2A, -122, 0x48, -50, 0x3D, 0x02, 0x01}), false).getInts()); assertArrayEquals(new Integer[]{0, 2, 840, 10045, 2, 1}, new ObjectIdentifier(new BytesReader(new Byte[]{0x6, 7, 2, -122, 0x48, -50, 0x3D, 0x02, 0x01}), false).getInts()); assertArrayEquals(new Integer[]{2, 2, 840, 10045, 2, 1}, new ObjectIdentifier(new BytesReader(new Byte[]{0x6, 7, 82, -122, 0x48, -50, 0x3D, 0x02, 0x01}), false).getInts()); assertArrayEquals(new Integer[]{2, 42, 840, 10045, 2, 1}, new ObjectIdentifier(new BytesReader(new Byte[]{0x6, 7, 122, -122, 0x48, -50, 0x3D, 0x02, 0x01}), false).getInts()); assertArrayEquals(new Integer[]{1, 2, 840, 113549, 1, 9, 14}, new ObjectIdentifier(new BytesReader(new Byte[]{0x06, 0x09, 0x2A, -122, 0x48, -122, -9, 0x0D, 0x01, 0x09, 0x0E}), false).getInts()); } @Test void testParseFail() { assertThrows(ParseException.class, () -> new ObjectIdentifier(new BytesReader(new Byte[]{0x6, 0x0}), false)); assertThrows(ParseException.class, () -> new ObjectIdentifier(new BytesReader(new Byte[]{0x6, 0x9, 0x2A, -122, 0x48, -122, -9, 0x0D, 0x01, 0x01, -117}), false)); } @Test void testGetKnown() throws ParseException { assertNotNull(ObjectIdentifier.getKnown("C")); assertNotNull(ObjectIdentifier.getKnown("CN")); assertNotNull(ObjectIdentifier.getKnown("O")); assertNotNull(ObjectIdentifier.getKnown("OU")); assertNotNull(ObjectIdentifier.getKnown("DC")); assertNotNull(ObjectIdentifier.getKnown("L")); assertNotNull(ObjectIdentifier.getKnown("oU")); assertNotNull(ObjectIdentifier.getKnown("Dc")); assertNotNull(ObjectIdentifier.getKnown("cn")); assertThrows(ParseException.class, () -> ObjectIdentifier.getKnown("114514")); assertThrows(ParseException.class, () -> ObjectIdentifier.getKnown("SN")); } }