diff options
Diffstat (limited to 'src/test/model/asn1')
-rw-r--r-- | src/test/model/asn1/ASN1LengthTest.java | 39 | ||||
-rw-r--r-- | src/test/model/asn1/ASN1ObjectTest.java | 67 | ||||
-rw-r--r-- | src/test/model/asn1/BitStringTest.java | 42 | ||||
-rw-r--r-- | src/test/model/asn1/BoolTest.java | 20 | ||||
-rw-r--r-- | src/test/model/asn1/GeneralizedTimeTest.java | 14 | ||||
-rw-r--r-- | src/test/model/asn1/IA5StringTest.java | 24 | ||||
-rw-r--r-- | src/test/model/asn1/IntTest.java | 58 | ||||
-rw-r--r-- | src/test/model/asn1/NullTest.java | 10 | ||||
-rw-r--r-- | src/test/model/asn1/ObjectIdentifierTest.java | 60 | ||||
-rw-r--r-- | src/test/model/asn1/OctetStringTest.java | 25 | ||||
-rw-r--r-- | src/test/model/asn1/PrintableStringTest.java | 22 | ||||
-rw-r--r-- | src/test/model/asn1/TagClassTest.java | 2 | ||||
-rw-r--r-- | src/test/model/asn1/TagTest.java | 46 | ||||
-rw-r--r-- | src/test/model/asn1/UTF8StringTest.java | 18 | ||||
-rw-r--r-- | src/test/model/asn1/UtcTimeTest.java | 14 | ||||
-rw-r--r-- | src/test/model/asn1/parsing/BytesReaderTest.java | 36 |
16 files changed, 246 insertions, 251 deletions
diff --git a/src/test/model/asn1/ASN1LengthTest.java b/src/test/model/asn1/ASN1LengthTest.java index 44aed8e..55da5ec 100644 --- a/src/test/model/asn1/ASN1LengthTest.java +++ b/src/test/model/asn1/ASN1LengthTest.java @@ -2,7 +2,6 @@ package model.asn1; import model.asn1.exceptions.ParseException; import model.asn1.parsing.BytesReader; -import model.asn1.parsing.BytesReaderTest; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; @@ -16,31 +15,31 @@ public class ASN1LengthTest { @Test void testParse() throws Exception { // TODO: Exception - BytesReader reader = new BytesReader(new Byte[]{ 0x0 }); + BytesReader reader = new BytesReader(new Byte[]{0x0}); assertEquals(0, new ASN1Length(reader).getLength()); assertEquals(1, reader.getIndex()); - reader = new BytesReader(new Byte[]{ 0x20 }); + reader = new BytesReader(new Byte[]{0x20}); assertEquals(0x20, new ASN1Length(reader).getLength()); assertEquals(1, reader.getIndex()); - reader = new BytesReader(new Byte[]{ -127, 0x30 }); + reader = new BytesReader(new Byte[]{-127, 0x30}); assertEquals(0x30, new ASN1Length(reader).getLength()); assertEquals(2, reader.getIndex()); - reader = new BytesReader(new Byte[]{ -126, -22, 0x60 }); + reader = new BytesReader(new Byte[]{-126, -22, 0x60}); assertEquals(60000, new ASN1Length(reader).getLength()); assertEquals(3, reader.getIndex()); - reader = new BytesReader(new Byte[]{ -127, 127 }); + reader = new BytesReader(new Byte[]{-127, 127}); assertEquals(127, new ASN1Length(reader).getLength()); assertEquals(2, reader.getIndex()); - reader = new BytesReader(new Byte[]{ -124, 1, 1, 1, 1 }); + reader = new BytesReader(new Byte[]{-124, 1, 1, 1, 1}); assertEquals(16843009, new ASN1Length(reader).getLength()); assertEquals(5, reader.getIndex()); - reader = new BytesReader(new Byte[]{ -127, -97 }); + reader = new BytesReader(new Byte[]{-127, -97}); assertEquals(159, new ASN1Length(reader).getLength()); assertEquals(2, reader.getIndex()); } @@ -49,46 +48,46 @@ public class ASN1LengthTest { void testParseFail() throws ParseException { // First byte 0b10000000 assertThrows(ParseException.class, () -> - new ASN1Length(new BytesReader(new Byte[]{ -128 })) + new ASN1Length(new BytesReader(new Byte[]{-128})) ); // First byte 0b11111111 assertThrows(ParseException.class, () -> - new ASN1Length(new BytesReader(new Byte[]{ -1 })) + new ASN1Length(new BytesReader(new Byte[]{-1})) ); // Multibyte, requested 2 bytes but have none. assertThrows(ParseException.class, () -> - new ASN1Length(new BytesReader(new Byte[]{ -126 })) + new ASN1Length(new BytesReader(new Byte[]{-126})) ); // Multibyte, requested 2 bytes but have one. assertThrows(ParseException.class, () -> - new ASN1Length(new BytesReader(new Byte[]{ -126, 0x1 })) + new ASN1Length(new BytesReader(new Byte[]{-126, 0x1})) ); // But this one should work (0b01111111) - new ASN1Length(new BytesReader(new Byte[]{ -127, 127 })); + new ASN1Length(new BytesReader(new Byte[]{-127, 127})); // Multibyte, too long. assertThrows(ParseException.class, () -> - new ASN1Length(new BytesReader(new Byte[]{ -124, -1, -1, -1, -1 })) + new ASN1Length(new BytesReader(new Byte[]{-124, -1, -1, -1, -1})) ); // But this one should work, except for it is too large - new ASN1Length(new BytesReader(new Byte[]{ -125, -1, -1, -1 })); + new ASN1Length(new BytesReader(new Byte[]{-125, -1, -1, -1})); } @Test void testEncode() { // Short form - assertArrayEquals(new Byte[]{ 0x0 }, new ASN1Length(0).encodeDER()); - assertArrayEquals(new Byte[]{ 0x1 }, new ASN1Length(1).encodeDER()); - assertArrayEquals(new Byte[]{ 127 }, new ASN1Length(127).encodeDER()); + assertArrayEquals(new Byte[]{0x0}, new ASN1Length(0).encodeDER()); + assertArrayEquals(new Byte[]{0x1}, new ASN1Length(1).encodeDER()); + assertArrayEquals(new Byte[]{127}, new ASN1Length(127).encodeDER()); // Long form // 0b10000001, 0b10000000 - assertArrayEquals(new Byte[]{ -127, -128 }, new ASN1Length(128).encodeDER()); + assertArrayEquals(new Byte[]{-127, -128}, new ASN1Length(128).encodeDER()); // 0b10000010, 0b11111111, 0b11111111 - assertArrayEquals(new Byte[]{ -126, -1, -1 }, new ASN1Length(65535).encodeDER()); + assertArrayEquals(new Byte[]{-126, -1, -1}, new ASN1Length(65535).encodeDER()); } } diff --git a/src/test/model/asn1/ASN1ObjectTest.java b/src/test/model/asn1/ASN1ObjectTest.java index ea765e6..76615c5 100644 --- a/src/test/model/asn1/ASN1ObjectTest.java +++ b/src/test/model/asn1/ASN1ObjectTest.java @@ -20,17 +20,17 @@ public class ASN1ObjectTest { false).getClass()); assertEquals(BitString.class, ASN1Object.parse(new BytesReader(new BitString(BitString.TAG, - null, 0, new Byte[]{ 1 }).encodeDER()), + null, 0, new Byte[]{1}).encodeDER()), false).getClass()); assertEquals(OctetString.class, - ASN1Object.parse(new BytesReader(new OctetString(OctetString.TAG, null, new Byte[]{ 1 }).encodeDER()), + ASN1Object.parse(new BytesReader(new OctetString(OctetString.TAG, null, new Byte[]{1}).encodeDER()), false).getClass()); assertEquals(Null.class, ASN1Object.parse(new BytesReader(new Null(Null.TAG, null).encodeDER()), false).getClass()); assertEquals(ObjectIdentifier.class, ASN1Object.parse(new BytesReader(new ObjectIdentifier(ObjectIdentifier.TAG, null, - new Integer[]{ 1, 2, 3 }).encodeDER()), + new Integer[]{1, 2, 3}).encodeDER()), false).getClass()); assertEquals(UTF8String.class, ASN1Object.parse(new BytesReader(new UTF8String(UTF8String.TAG, null, @@ -53,7 +53,7 @@ public class ASN1ObjectTest { ZonedDateTime.now(ZoneId.of("UTC"))).encodeDER()), false).getClass()); assertEquals(ASN1Object.class, - ASN1Object.parse(new BytesReader(new Byte[]{ 0x30, 1, 0x0 }), false) + ASN1Object.parse(new BytesReader(new Byte[]{0x30, 1, 0x0}), false) .getClass()); } @@ -68,53 +68,54 @@ public class ASN1ObjectTest { new ASN1Object(new Tag(TagClass.UNIVERSAL, false, 0x5), new Tag(TagClass.UNIVERSAL, false, 0x6)).getParentTag().getNumber()); } + @Test void testParseSuccess() throws ParseException { // No parent tag assertEquals(0x5, - new ASN1Object(new BytesReader(new Byte[]{ 0x5, 0x0 }), false) + new ASN1Object(new BytesReader(new Byte[]{0x5, 0x0}), false) .getTag().getNumber()); assertEquals(TagClass.UNIVERSAL, - new ASN1Object(new BytesReader(new Byte[]{ 0x5, 0x0 }), false) + new ASN1Object(new BytesReader(new Byte[]{0x5, 0x0}), false) .getTag().getCls()); - assertFalse(new ASN1Object(new BytesReader(new Byte[]{ 0x5, 0x0 }), false) + assertFalse(new ASN1Object(new BytesReader(new Byte[]{0x5, 0x0}), false) .getTag().isConstructive()); - assertNull(new ASN1Object(new BytesReader(new Byte[]{ 0x5, 0x0 }), false) + assertNull(new ASN1Object(new BytesReader(new Byte[]{0x5, 0x0}), false) .getParentTag()); - assertEquals(0, new ASN1Object(new BytesReader(new Byte[]{ 0x5, 0x0 }), false) + assertEquals(0, new ASN1Object(new BytesReader(new Byte[]{0x5, 0x0}), false) .encodeValueDER().length); - assertEquals(0, new ASN1Object(new BytesReader(new Byte[]{ 0x5, 0x0 }), false) + assertEquals(0, new ASN1Object(new BytesReader(new Byte[]{0x5, 0x0}), false) .getLength()); // With parent tag // -95 is the 2's complement represent of 0b10100001 assertEquals(0x5, - new ASN1Object(new BytesReader(new Byte[]{ -95, 2, 0x5, 0x0 }), true) + new ASN1Object(new BytesReader(new Byte[]{-95, 2, 0x5, 0x0}), true) .getTag().getNumber()); assertEquals(TagClass.UNIVERSAL, - new ASN1Object(new BytesReader(new Byte[]{ -95, 2, 0x5, 0x0 }), true) + new ASN1Object(new BytesReader(new Byte[]{-95, 2, 0x5, 0x0}), true) .getTag().getCls()); - assertFalse(new ASN1Object(new BytesReader(new Byte[]{ -95, 2, 0x5, 0x0 }), true) + assertFalse(new ASN1Object(new BytesReader(new Byte[]{-95, 2, 0x5, 0x0}), true) .getTag().isConstructive()); assertEquals(0x1, - new ASN1Object(new BytesReader(new Byte[]{ -95, 2, 0x5, 0x0 }), true) + new ASN1Object(new BytesReader(new Byte[]{-95, 2, 0x5, 0x0}), true) .getParentTag().getNumber()); assertEquals(TagClass.CONTEXT_SPECIFIC, - new ASN1Object(new BytesReader(new Byte[]{ -95, 2, 0x5, 0x0 }), true) + new ASN1Object(new BytesReader(new Byte[]{-95, 2, 0x5, 0x0}), true) .getParentTag().getCls()); - assertTrue(new ASN1Object(new BytesReader(new Byte[]{ -95, 2, 0x5, 0x0 }), true) + assertTrue(new ASN1Object(new BytesReader(new Byte[]{-95, 2, 0x5, 0x0}), true) .getParentTag().isConstructive()); // Test index - BytesReader reader = new BytesReader(new Byte[]{ 0xE, 5, 1, 2, 3, 4, 5 }); + BytesReader reader = new BytesReader(new Byte[]{0xE, 5, 1, 2, 3, 4, 5}); ASN1Object obj = new ASN1Object(reader, false); // Contents should not be read. assertEquals(2, reader.getIndex()); // But is copied - assertArrayEquals(new Byte[]{ 1, 2, 3, 4, 5 }, obj.encodeValueDER()); + assertArrayEquals(new Byte[]{1, 2, 3, 4, 5}, obj.encodeValueDER()); // If we parse an unknown type - reader = new BytesReader(new Byte[]{ 0xE, 5, 1, 2, 3, 4, 5 }); + reader = new BytesReader(new Byte[]{0xE, 5, 1, 2, 3, 4, 5}); obj = ASN1Object.parse(reader, false); // Contents should be read now assertEquals(7, reader.getIndex()); @@ -124,50 +125,50 @@ public class ASN1ObjectTest { void testParseFail() { // Value early EOF assertThrows(ParseException.class, () -> - new ASN1Object(new BytesReader(new Byte[]{ 0x5, 0x1 }), false)); + new ASN1Object(new BytesReader(new Byte[]{0x5, 0x1}), false)); // Tag early EOF assertThrows(ParseException.class, () -> - new ASN1Object(new BytesReader(new Byte[]{ -95, 2 }), true)); + new ASN1Object(new BytesReader(new Byte[]{-95, 2}), true)); // Length not found assertThrows(ParseException.class, () -> - new ASN1Object(new BytesReader(new Byte[]{ 0x5 }), false)); + new ASN1Object(new BytesReader(new Byte[]{0x5}), false)); assertThrows(ParseException.class, () -> - new ASN1Object(new BytesReader(new Byte[]{ -95, 2, 0x5 }), true)); + new ASN1Object(new BytesReader(new Byte[]{-95, 2, 0x5}), true)); // Parent tag is not CONTEXT_SPECIFIC // UNIVERSAL assertThrows(ParseException.class, () -> - new ASN1Object(new BytesReader(new Byte[]{ 33, 2, 0x5, 0x0 }), true)); + new ASN1Object(new BytesReader(new Byte[]{33, 2, 0x5, 0x0}), true)); // APPLICATION assertThrows(ParseException.class, () -> - new ASN1Object(new BytesReader(new Byte[]{ 97, 2, 0x5, 0x0 }), true)); + new ASN1Object(new BytesReader(new Byte[]{97, 2, 0x5, 0x0}), true)); // PRIVATE assertThrows(ParseException.class, () -> - new ASN1Object(new BytesReader(new Byte[]{ -31, 2, 0x5, 0x0 }), true)); + new ASN1Object(new BytesReader(new Byte[]{-31, 2, 0x5, 0x0}), true)); // Parent tag is not constructive assertThrows(ParseException.class, () -> - new ASN1Object(new BytesReader(new Byte[]{ -127, 2, 0x5, 0x0 }), true)); + new ASN1Object(new BytesReader(new Byte[]{-127, 2, 0x5, 0x0}), true)); // Parent tag length incorrect assertThrows(ParseException.class, () -> - new ASN1Object(new BytesReader(new Byte[]{ -95, 0, 0x5, 0x0 }), true)); + new ASN1Object(new BytesReader(new Byte[]{-95, 0, 0x5, 0x0}), true)); assertThrows(ParseException.class, () -> - new ASN1Object(new BytesReader(new Byte[]{ -95, 1, 0x5, 0x0 }), true)); + new ASN1Object(new BytesReader(new Byte[]{-95, 1, 0x5, 0x0}), true)); assertThrows(ParseException.class, () -> - new ASN1Object(new BytesReader(new Byte[]{ -95, 3, 0x5, 0x0 }), true)); + new ASN1Object(new BytesReader(new Byte[]{-95, 3, 0x5, 0x0}), true)); } @Test void testEncode() { // No parent tag - assertArrayEquals(new Byte[] { + assertArrayEquals(new Byte[]{ 0x5, 0x0 }, new Null(Null.TAG, null).encodeDER()); // Custom tag - assertArrayEquals(new Byte[] { + assertArrayEquals(new Byte[]{ 0x72, 0x0 }, new Null(new Tag(TagClass.APPLICATION, true, 0x12), null) .encodeDER()); // With parent tag - assertArrayEquals(new Byte[] { + assertArrayEquals(new Byte[]{ -95, 2, 0x72, 0x0 }, new Null(new Tag(TagClass.APPLICATION, true, 0x12), diff --git a/src/test/model/asn1/BitStringTest.java b/src/test/model/asn1/BitStringTest.java index c893b36..b2472fd 100644 --- a/src/test/model/asn1/BitStringTest.java +++ b/src/test/model/asn1/BitStringTest.java @@ -9,11 +9,11 @@ import static org.junit.jupiter.api.Assertions.*; public class BitStringTest { @Test void testConstructor() { - assertArrayEquals(new Byte[]{ 0x2, 0x3 }, - new BitString(BitString.TAG, null, 0, new Byte[]{ 0x2, 0x3 }) + assertArrayEquals(new Byte[]{0x2, 0x3}, + new BitString(BitString.TAG, null, 0, new Byte[]{0x2, 0x3}) .getVal()); assertEquals(3, - new BitString(BitString.TAG, null, 3, new Byte[]{ 0x2, 0x8 }) + new BitString(BitString.TAG, null, 3, new Byte[]{0x2, 0x8}) .getUnused()); } @@ -21,55 +21,55 @@ public class BitStringTest { void testConvert() { // 00000010 00001000 // 00000000 01000001 = 65 - assertArrayEquals(new Byte[]{ 65 }, - new BitString(BitString.TAG, null, 3, new Byte[]{ 0x2, 0x8 }) + assertArrayEquals(new Byte[]{65}, + new BitString(BitString.TAG, null, 3, new Byte[]{0x2, 0x8}) .getConvertedVal()); - assertArrayEquals(new Byte[]{ 0x2, 0x8 }, - new BitString(BitString.TAG, null, 0, new Byte[]{ 0x2, 0x8 }) + assertArrayEquals(new Byte[]{0x2, 0x8}, + new BitString(BitString.TAG, null, 0, new Byte[]{0x2, 0x8}) .getConvertedVal()); } @Test void testParse() throws ParseException { - assertArrayEquals(new Byte[]{ 0x6e, 0x5d, -64 }, - new BitString(new BytesReader(new Byte[]{ 0x03, 0x04, 0x06, 0x6e, 0x5d, -64 }), false) + assertArrayEquals(new Byte[]{0x6e, 0x5d, -64}, + new BitString(new BytesReader(new Byte[]{0x03, 0x04, 0x06, 0x6e, 0x5d, -64}), false) .getVal()); assertEquals(6, - new BitString(new BytesReader(new Byte[]{ 0x03, 0x04, 0x06, 0x6e, 0x5d, -64 }), false) + new BitString(new BytesReader(new Byte[]{0x03, 0x04, 0x06, 0x6e, 0x5d, -64}), false) .getUnused()); - assertArrayEquals(new Byte[]{ 0x01, -71, 0x77 }, - new BitString(new BytesReader(new Byte[]{ 0x03, 0x04, 0x06, 0x6e, 0x5d, -64 }), false) + assertArrayEquals(new Byte[]{0x01, -71, 0x77}, + new BitString(new BytesReader(new Byte[]{0x03, 0x04, 0x06, 0x6e, 0x5d, -64}), false) .getConvertedVal()); } @Test void testParseFail() { assertThrows(ParseException.class, () -> - new BitString(new BytesReader(new Byte[]{ 0x03, 0x04 }), false)); + new BitString(new BytesReader(new Byte[]{0x03, 0x04}), false)); // 0b11100000 assertThrows(ParseException.class, () -> - new BitString(new BytesReader(new Byte[]{ 0x03, 0x04, 0x06, 0x6e, 0x5d, -32 }), false)); + new BitString(new BytesReader(new Byte[]{0x03, 0x04, 0x06, 0x6e, 0x5d, -32}), false)); // 0b11000001 assertThrows(ParseException.class, () -> - new BitString(new BytesReader(new Byte[]{ 0x03, 0x04, 0x06, 0x6e, 0x5d, -63 }), false)); + new BitString(new BytesReader(new Byte[]{0x03, 0x04, 0x06, 0x6e, 0x5d, -63}), false)); // Unused bits = 8 assertThrows(ParseException.class, () -> - new BitString(new BytesReader(new Byte[]{ 0x03, 0x04, 0x08, 0x6e, 0x5d, -64 }), false)); + new BitString(new BytesReader(new Byte[]{0x03, 0x04, 0x08, 0x6e, 0x5d, -64}), false)); // Unused bits = 6 -> 7 assertThrows(ParseException.class, () -> - new BitString(new BytesReader(new Byte[]{ 0x03, 0x04, 0x07, 0x6e, 0x5d, -64 }), false)); + new BitString(new BytesReader(new Byte[]{0x03, 0x04, 0x07, 0x6e, 0x5d, -64}), false)); // Illegal unused bits: 8 and -1 assertThrows(ParseException.class, () -> - new BitString(new BytesReader(new Byte[]{ 0x03, 0x04, 0x08, 0x6e, 0x5d, -64 }), false)); + new BitString(new BytesReader(new Byte[]{0x03, 0x04, 0x08, 0x6e, 0x5d, -64}), false)); assertThrows(ParseException.class, () -> - new BitString(new BytesReader(new Byte[]{ 0x03, 0x04, -1, 0x6e, 0x5d, -64 }), false)); + new BitString(new BytesReader(new Byte[]{0x03, 0x04, -1, 0x6e, 0x5d, -64}), false)); } @Test void testEncode() throws ParseException { - assertArrayEquals(new Byte[]{ 0x03, 0x04, 0x06, 0x6e, 0x5d, -64 }, - new BitString(new BytesReader(new Byte[]{ 0x03, 0x04, 0x06, 0x6e, 0x5d, -64 }), false) + assertArrayEquals(new Byte[]{0x03, 0x04, 0x06, 0x6e, 0x5d, -64}, + new BitString(new BytesReader(new Byte[]{0x03, 0x04, 0x06, 0x6e, 0x5d, -64}), false) .encodeDER()); } } diff --git a/src/test/model/asn1/BoolTest.java b/src/test/model/asn1/BoolTest.java index fed3152..cff1a3e 100644 --- a/src/test/model/asn1/BoolTest.java +++ b/src/test/model/asn1/BoolTest.java @@ -15,31 +15,31 @@ public class BoolTest { @Test void testParse() throws ParseException { - assertFalse(new Bool(new BytesReader(new Byte[]{ 0x1, 1, 0 }), false) - .getValue()); - assertTrue(new Bool(new BytesReader(new Byte[]{ 0x1, 1, -1 }), false) + assertFalse(new Bool(new BytesReader(new Byte[]{0x1, 1, 0}), false) + .getValue()); + assertTrue(new Bool(new BytesReader(new Byte[]{0x1, 1, -1}), false) .getValue()); } @Test void testParseFail() throws ParseException { assertThrows(ParseException.class, () -> - new Bool(new BytesReader(new Byte[]{ 0x1, 0 }), false)); + new Bool(new BytesReader(new Byte[]{0x1, 0}), false)); assertThrows(ParseException.class, () -> - new Bool(new BytesReader(new Byte[]{ 0x1, 1 }), false)); + new Bool(new BytesReader(new Byte[]{0x1, 1}), false)); assertThrows(ParseException.class, () -> - new Bool(new BytesReader(new Byte[]{ 0x1, 1, 1 }), false)); + new Bool(new BytesReader(new Byte[]{0x1, 1, 1}), false)); assertThrows(ParseException.class, () -> - new Bool(new BytesReader(new Byte[]{ 0x1, 1, -2 }), false)); + new Bool(new BytesReader(new Byte[]{0x1, 1, -2}), false)); assertThrows(ParseException.class, () -> - new Bool(new BytesReader(new Byte[]{ 0x1, 2, -1, 2 }), false)); + new Bool(new BytesReader(new Byte[]{0x1, 2, -1, 2}), false)); } @Test void testEncode() { - assertArrayEquals(new Byte[]{ 0 }, + assertArrayEquals(new Byte[]{0}, new Bool(Bool.TAG, null, false).encodeValueDER()); - assertArrayEquals(new Byte[]{ -1 }, + assertArrayEquals(new Byte[]{-1}, new Bool(Bool.TAG, null, true).encodeValueDER()); } } diff --git a/src/test/model/asn1/GeneralizedTimeTest.java b/src/test/model/asn1/GeneralizedTimeTest.java index 4660de7..4ca5b72 100644 --- a/src/test/model/asn1/GeneralizedTimeTest.java +++ b/src/test/model/asn1/GeneralizedTimeTest.java @@ -14,20 +14,20 @@ public class GeneralizedTimeTest { void testConstructor() throws ParseException { final ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC")); assertEquals(now, new GeneralizedTime(GeneralizedTime.TAG, null, now).getTimestamp()); - final ASN1Time parsed = new GeneralizedTime(new BytesReader(new Byte[] { + final ASN1Time parsed = new GeneralizedTime(new BytesReader(new Byte[]{ 0x18, 15, '1', '9', '1', '9', '0', '8', '1', '0', '1', '1', '4', '5', '1', '4', 'Z' }), false); assertEquals("19190810114514Z", parsed.toString()); assertEquals(ZonedDateTime.of(1919, 8, 10, 11, 45, 14, - 0, ZoneId.of("UTC")), + 0, ZoneId.of("UTC")), parsed.getTimestamp()); } @Test void testParse() throws ParseException { - ASN1Time parsed = new GeneralizedTime(new BytesReader(new Byte[] { + ASN1Time parsed = new GeneralizedTime(new BytesReader(new Byte[]{ 0x18, 15, '2', '0', '2', '3', '0', '9', '2', '7', '1', '1', '4', '5', '1', '4', 'Z' }), false); @@ -36,7 +36,7 @@ public class GeneralizedTimeTest { parsed.getTimestamp()); // No seconds - parsed = new GeneralizedTime(new BytesReader(new Byte[] { + parsed = new GeneralizedTime(new BytesReader(new Byte[]{ 0x18, 13, '2', '0', '2', '3', '0', '9', '2', '7', '1', '1', '4', '5', 'Z' }), false); @@ -96,20 +96,20 @@ public class GeneralizedTimeTest { @Test void testEncode() throws ParseException { - assertEquals("20230927114514Z", new GeneralizedTime(new BytesReader(new Byte[] { + assertEquals("20230927114514Z", new GeneralizedTime(new BytesReader(new Byte[]{ -95, 17, 0x18, 15, '2', '0', '2', '3', '0', '9', '2', '7', '1', '1', '4', '5', '1', '4', 'Z' }), true).toString()); // No seconds - assertEquals("202309271145Z", new GeneralizedTime(new BytesReader(new Byte[] { + assertEquals("202309271145Z", new GeneralizedTime(new BytesReader(new Byte[]{ -95, 15, 0x18, 13, '2', '0', '2', '3', '0', '9', '2', '7', '1', '1', '4', '5', 'Z' }), true).toString()); // To byte array - assertArrayEquals(new Byte[] { + assertArrayEquals(new Byte[]{ 0x18, 13, '2', '0', '2', '3', '0', '9', '2', '7', '1', '1', '4', '5', 'Z' }, new GeneralizedTime(GeneralizedTime.TAG, null, ZonedDateTime.of(2023, 9, diff --git a/src/test/model/asn1/IA5StringTest.java b/src/test/model/asn1/IA5StringTest.java index dfaa1aa..8bec6c4 100644 --- a/src/test/model/asn1/IA5StringTest.java +++ b/src/test/model/asn1/IA5StringTest.java @@ -32,25 +32,25 @@ public class IA5StringTest { void testIllegalStrings() { assertThrows(ParseException.class, () -> new IA5String(IA5String.TAG, null, - stringToTest + new String(new byte[]{ -128 }, StandardCharsets.UTF_8))); + stringToTest + new String(new byte[]{-128}, StandardCharsets.UTF_8))); assertThrows(ParseException.class, () -> new IA5String(IA5String.TAG, null, - stringToTest + new String(new byte[]{ -1 }, StandardCharsets.UTF_8))); + stringToTest + new String(new byte[]{-1}, StandardCharsets.UTF_8))); } @Test void testEncode() throws ParseException { assertArrayEquals( - new Byte[] { 0x00, 0x01, 0x02 }, - new IA5String(IA5String.TAG, null, new String(new byte[]{ 0, 1, 2}, StandardCharsets.UTF_8)) + new Byte[]{0x00, 0x01, 0x02}, + new IA5String(IA5String.TAG, null, new String(new byte[]{0, 1, 2}, StandardCharsets.UTF_8)) .encodeValueDER()); assertArrayEquals( - new Byte[] { + new Byte[]{ 0x16, 0x02, // Tag - Length 0x68, 0x69 // Value }, new IA5String(IA5String.TAG, null, "hi").encodeDER()); assertArrayEquals( - new Byte[] { + new Byte[]{ -85, 0x05, // Parent Tag - Length 0x16, 0x03, // Inner Tag - Length 0x68, 0x69, 0x69 // Value @@ -62,18 +62,18 @@ public class IA5StringTest { @Test void testParse() throws ParseException { assertEquals("123", - new IA5String(new BytesReader(new Byte[]{ 0x16, 3, '1', '2', '3' }), false) + new IA5String(new BytesReader(new Byte[]{0x16, 3, '1', '2', '3'}), false) .getString()); assertEquals("", - new IA5String(new BytesReader(new Byte[]{ 0x16, 0 }), false) + new IA5String(new BytesReader(new Byte[]{0x16, 0}), false) .getString()); } @Test void testParseFail() { - assertThrows(ParseException.class, () -> - new IA5String(new BytesReader(new Byte[]{ 0x16, 3, '1', '2' }), false)); - assertThrows(ParseException.class, () -> - new IA5String(new BytesReader(new Byte[]{ 0x16, 2, '1', -128 }), false)); + assertThrows(ParseException.class, () -> + new IA5String(new BytesReader(new Byte[]{0x16, 3, '1', '2'}), false)); + assertThrows(ParseException.class, () -> + new IA5String(new BytesReader(new Byte[]{0x16, 2, '1', -128}), false)); } } diff --git a/src/test/model/asn1/IntTest.java b/src/test/model/asn1/IntTest.java index 23a5e23..d31d132 100644 --- a/src/test/model/asn1/IntTest.java +++ b/src/test/model/asn1/IntTest.java @@ -19,81 +19,81 @@ public class IntTest { @Test void testEncode() { // Single-byte - assertArrayEquals(new Byte[] { 0x0 }, new Int(Int.TAG, null, 0).encodeValueDER()); - assertArrayEquals(new Byte[] { 0x1 }, new Int(Int.TAG, null, 1).encodeValueDER()); - assertArrayEquals(new Byte[] { -1 }, new Int(Int.TAG, null, 255).encodeValueDER()); + assertArrayEquals(new Byte[]{0x0}, new Int(Int.TAG, null, 0).encodeValueDER()); + assertArrayEquals(new Byte[]{0x1}, new Int(Int.TAG, null, 1).encodeValueDER()); + assertArrayEquals(new Byte[]{-1}, new Int(Int.TAG, null, 255).encodeValueDER()); // Multiple bytes - assertArrayEquals(new Byte[] { 0x01, 0x00 }, new Int(Int.TAG, null, 256).encodeValueDER()); - assertArrayEquals(new Byte[] { -1, -1 }, new Int(Int.TAG, null, 65535).encodeValueDER()); - assertArrayEquals(new Byte[] { 0x01, 0x00, 0x00 }, new Int(Int.TAG, null, 65536).encodeValueDER()); - assertArrayEquals(new Byte[] { -1, -1, -1 }, new Int(Int.TAG, null, 16777215).encodeValueDER()); - assertArrayEquals(new Byte[] { 0x01, 0x00, 0x00, 0x00 }, new Int(Int.TAG, null, 16777216).encodeValueDER()); - assertArrayEquals(new Byte[] { -1, -1, -1, -1 }, new Int(Int.TAG, null, 4294967295L).encodeValueDER()); - assertArrayEquals(new Byte[] { -1, -1, -1, -1 }, new Int(Int.TAG, null, 4294967295L).encodeValueDER()); + assertArrayEquals(new Byte[]{0x01, 0x00}, new Int(Int.TAG, null, 256).encodeValueDER()); + assertArrayEquals(new Byte[]{-1, -1}, new Int(Int.TAG, null, 65535).encodeValueDER()); + assertArrayEquals(new Byte[]{0x01, 0x00, 0x00}, new Int(Int.TAG, null, 65536).encodeValueDER()); + assertArrayEquals(new Byte[]{-1, -1, -1}, new Int(Int.TAG, null, 16777215).encodeValueDER()); + assertArrayEquals(new Byte[]{0x01, 0x00, 0x00, 0x00}, new Int(Int.TAG, null, 16777216).encodeValueDER()); + assertArrayEquals(new Byte[]{-1, -1, -1, -1}, new Int(Int.TAG, null, 4294967295L).encodeValueDER()); + assertArrayEquals(new Byte[]{-1, -1, -1, -1}, new Int(Int.TAG, null, 4294967295L).encodeValueDER()); // 2 ^ 63 + 1 - assertArrayEquals(new Byte[] { -128, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }, + assertArrayEquals(new Byte[]{-128, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, new Int(Int.TAG, null, Long.parseUnsignedLong("9223372036854775809")).encodeValueDER()); // Test no leading zeros // Not 0x00, 0xFF - assertArrayEquals(new Byte[] { -1 }, new Int(Int.TAG, null, 255).encodeValueDER()); + assertArrayEquals(new Byte[]{-1}, new Int(Int.TAG, null, 255).encodeValueDER()); // Test no leading ones // Not 0xFF, 0x80 - assertArrayEquals(new Byte[] { -128 }, new Int(Int.TAG, null, -128).encodeValueDER()); + assertArrayEquals(new Byte[]{-128}, new Int(Int.TAG, null, -128).encodeValueDER()); // Encode DER - assertArrayEquals(new Byte[] { 0x02, 2, 0x01, 0x00 }, new Int(Int.TAG, null, 256).encodeDER()); + assertArrayEquals(new Byte[]{0x02, 2, 0x01, 0x00}, new Int(Int.TAG, null, 256).encodeDER()); } @Test void testParse() throws ParseException { // Single-byte - assertEquals(0, new Int(new BytesReader(new Byte[] { 0x2, 1, 0x0 }), false).getLong()); - assertEquals(1, new Int(new BytesReader(new Byte[] { 0x2, 1, 0x1 }), false).getLong()); - assertEquals(-1, new Int(new BytesReader(new Byte[] { 0x2, 1, -1 }), false).getLong()); + assertEquals(0, new Int(new BytesReader(new Byte[]{0x2, 1, 0x0}), false).getLong()); + assertEquals(1, new Int(new BytesReader(new Byte[]{0x2, 1, 0x1}), false).getLong()); + assertEquals(-1, new Int(new BytesReader(new Byte[]{0x2, 1, -1}), false).getLong()); // Multiple bytes assertEquals(256, - new Int(new BytesReader(new Byte[] { 0x2, 2, 0x01, 0x00 }), false).getLong()); + new Int(new BytesReader(new Byte[]{0x2, 2, 0x01, 0x00}), false).getLong()); assertEquals(-1, - new Int(new BytesReader(new Byte[] { 0x2, 2, -1, -1 }), false).getLong()); + new Int(new BytesReader(new Byte[]{0x2, 2, -1, -1}), false).getLong()); assertEquals(65536, - new Int(new BytesReader(new Byte[] { 0x2, 3, 0x01, 0x00, 0x00 }), false).getLong()); + new Int(new BytesReader(new Byte[]{0x2, 3, 0x01, 0x00, 0x00}), false).getLong()); assertEquals(-1, - new Int(new BytesReader(new Byte[] { 0x2, 3, -1, -1, -1 }), false).getLong()); + new Int(new BytesReader(new Byte[]{0x2, 3, -1, -1, -1}), false).getLong()); assertEquals(16777216, - new Int(new BytesReader(new Byte[] { 0x2, 4, 0x01, 0x00, 0x00, 0x00 }), false).getLong()); + new Int(new BytesReader(new Byte[]{0x2, 4, 0x01, 0x00, 0x00, 0x00}), false).getLong()); assertEquals(-1, - new Int(new BytesReader(new Byte[] { 0x2, 4, -1, -1, -1, -1 }), false).getLong()); + new Int(new BytesReader(new Byte[]{0x2, 4, -1, -1, -1, -1}), false).getLong()); assertEquals(4294967296L, - new Int(new BytesReader(new Byte[] { 0x2, 5, 0x01, 0x00, 0x00, 0x00, 0x00 }),false).getLong()); + new Int(new BytesReader(new Byte[]{0x2, 5, 0x01, 0x00, 0x00, 0x00, 0x00}), false).getLong()); // 2 ^ 63 + 1 assertEquals(Long.parseUnsignedLong("9223372036854775809"), - new Int(new BytesReader(new Byte[] { 0x2, 9, 0x00, -128, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }), + new Int(new BytesReader(new Byte[]{0x2, 9, 0x00, -128, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}), false).getValue().longValue()); // Test no leading zeros // Not 0x00, 0xFF - assertEquals(255, new Int(new BytesReader(new Byte[] { 0x02, 0x2, 0x0, -1 }), false).getLong()); + assertEquals(255, new Int(new BytesReader(new Byte[]{0x02, 0x2, 0x0, -1}), false).getLong()); // Test no leading ones // Not 0xFF, 0x80 - assertArrayEquals(new Byte[] { -128 }, new Int(Int.TAG, null, -128).encodeValueDER()); + assertArrayEquals(new Byte[]{-128}, new Int(Int.TAG, null, -128).encodeValueDER()); } @Test void testParseFail() { // Not enough bytes assertThrows(ParseException.class, () -> - new Int(new BytesReader(new Byte[]{ 0x2, 0x7, -1, -1, -1, -1, -1, -1 }), + new Int(new BytesReader(new Byte[]{0x2, 0x7, -1, -1, -1, -1, -1, -1}), false)); // Zero len assertThrows(ParseException.class, () -> - new Int(new BytesReader(new Byte[]{ 0x2, 0x0, -1, -1, -1, -1, -1, -1 }), + new Int(new BytesReader(new Byte[]{0x2, 0x0, -1, -1, -1, -1, -1, -1}), false)); } } diff --git a/src/test/model/asn1/NullTest.java b/src/test/model/asn1/NullTest.java index 4ec2c64..a6512d8 100644 --- a/src/test/model/asn1/NullTest.java +++ b/src/test/model/asn1/NullTest.java @@ -17,22 +17,22 @@ public class NullTest { @Test void testEncode() { assertEquals(0, new Null(Null.TAG, null).encodeValueDER().length); - assertArrayEquals(new Byte[] { + assertArrayEquals(new Byte[]{ 0x5, 0x0 // Tag - Length }, new Null(Null.TAG, null).encodeDER()); } @Test void testParse() throws ParseException { - new Null(new BytesReader(new Byte[]{ 0x5, 0x0 }), false); - new Null(new BytesReader(new Byte[]{ -95, 2, 0x5, 0x0 }), true); + new Null(new BytesReader(new Byte[]{0x5, 0x0}), false); + new Null(new BytesReader(new Byte[]{-95, 2, 0x5, 0x0}), true); } @Test void testParseFail() { assertThrows(ParseException.class, () -> - new Null(new BytesReader(new Byte[]{ 0x5, 0x2 }), false)); + new Null(new BytesReader(new Byte[]{0x5, 0x2}), false)); assertThrows(ParseException.class, () -> - new Null(new BytesReader(new Byte[]{ 0x5, 0x2, 1, 1 }), false)); + new Null(new BytesReader(new Byte[]{0x5, 0x2, 1, 1}), false)); } } diff --git a/src/test/model/asn1/ObjectIdentifierTest.java b/src/test/model/asn1/ObjectIdentifierTest.java index f6f1049..367e349 100644 --- a/src/test/model/asn1/ObjectIdentifierTest.java +++ b/src/test/model/asn1/ObjectIdentifierTest.java @@ -9,18 +9,18 @@ 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()); + 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()); + 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()); + 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", @@ -39,18 +39,18 @@ public class ObjectIdentifierTest { @Test void testEncode() { - assertArrayEquals(new Byte[]{ 0x55, 0x04, 0x0A }, + assertArrayEquals(new Byte[]{0x55, 0x04, 0x0A}, new ObjectIdentifier(ObjectIdentifier.TAG, null, ObjectIdentifier.OID_O).encodeValueDER()); - assertArrayEquals(new Byte[]{ 0x67, -127, 0x0C, 0x01, 0x02, 0x01 }, + 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 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 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 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()); @@ -59,31 +59,31 @@ public class ObjectIdentifierTest { @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 }), + 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 }), + 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 }), + 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 }), + 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()); + 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)); + 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)); + new ObjectIdentifier(new BytesReader(new Byte[]{0x6, 0x9, 0x2A, -122, 0x48, -122, -9, 0x0D, + 0x01, 0x01, -117}), false)); } } diff --git a/src/test/model/asn1/OctetStringTest.java b/src/test/model/asn1/OctetStringTest.java index 9e6e8a9..91825e9 100644 --- a/src/test/model/asn1/OctetStringTest.java +++ b/src/test/model/asn1/OctetStringTest.java @@ -2,36 +2,33 @@ package model.asn1; import model.asn1.exceptions.ParseException; import model.asn1.parsing.BytesReader; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.util.stream.Collectors; -import java.util.stream.IntStream; - -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; public class OctetStringTest { @Test void testConstructor() { - assertArrayEquals(new Byte[]{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }, - new OctetString(OctetString.TAG, null, new Byte[]{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }) + assertArrayEquals(new Byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05}, + new OctetString(OctetString.TAG, null, new Byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05}) .getBytes()); - assertArrayEquals(new Byte[]{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }, - new OctetString(OctetString.TAG, null, new Byte[]{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }) + assertArrayEquals(new Byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05}, + new OctetString(OctetString.TAG, null, new Byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05}) .encodeValueDER()); } @Test void testEncode() throws ParseException { - assertArrayEquals(new Byte[]{ 0x04, 0x06, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }, - new OctetString(OctetString.TAG, null, new Byte[]{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }) + assertArrayEquals(new Byte[]{0x04, 0x06, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05}, + new OctetString(OctetString.TAG, null, new Byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05}) .encodeDER()); } @Test void testParse() throws ParseException { - assertArrayEquals(new Byte[]{ 0x0A, 0x0B, 0x0C }, - new OctetString(new BytesReader(new Byte[]{ 0x4, 3, 0x0A, 0x0B, 0x0C }), false) + assertArrayEquals(new Byte[]{0x0A, 0x0B, 0x0C}, + new OctetString(new BytesReader(new Byte[]{0x4, 3, 0x0A, 0x0B, 0x0C}), false) .getBytes()); } @@ -39,6 +36,6 @@ public class OctetStringTest { void testParseFail() { // EOF assertThrows(ParseException.class, () -> - new OctetString(new BytesReader(new Byte[]{ 0x4, 2, 0x0 }), false)); + new OctetString(new BytesReader(new Byte[]{0x4, 2, 0x0}), false)); } } diff --git a/src/test/model/asn1/PrintableStringTest.java b/src/test/model/asn1/PrintableStringTest.java index f46f400..c6eb7b1 100644 --- a/src/test/model/asn1/PrintableStringTest.java +++ b/src/test/model/asn1/PrintableStringTest.java @@ -21,24 +21,24 @@ public class PrintableStringTest { void testIllegalStrings() { ILLEGAL_CHARS_TO_TEST.chars() .forEach(c -> - assertThrows(ParseException.class, - () -> new PrintableString(PrintableString.TAG, null, Character.toString(c)), - String.format("Expected failing validation by char '%c'.", c)) + assertThrows(ParseException.class, + () -> new PrintableString(PrintableString.TAG, null, Character.toString(c)), + String.format("Expected failing validation by char '%c'.", c)) ); } @Test void testEncode() throws ParseException { assertArrayEquals( - new Byte[] { 0x68, 0x68, 0x68 }, + new Byte[]{0x68, 0x68, 0x68}, new PrintableString(PrintableString.TAG, null, "hhh").encodeValueDER()); assertArrayEquals( - new Byte[] { + new Byte[]{ 0x13, 0x02, // Tag - Length 0x68, 0x69 // Value }, new PrintableString(PrintableString.TAG, null, "hi").encodeDER()); assertArrayEquals( - new Byte[] { + new Byte[]{ -86, 0x05, // Parent Tag - Length 0x13, 0x03, // Inner Tag - Length 0x68, 0x69, 0x69 // Value @@ -50,10 +50,10 @@ public class PrintableStringTest { @Test void testParse() throws ParseException { assertEquals("123", - new PrintableString(new BytesReader(new Byte[]{ 0x13, 3, '1', '2', '3' }), false) + new PrintableString(new BytesReader(new Byte[]{0x13, 3, '1', '2', '3'}), false) .getString()); assertEquals("", - new PrintableString(new BytesReader(new Byte[]{ 0x13, 0, '1', '2', '3' }), false) + new PrintableString(new BytesReader(new Byte[]{0x13, 0, '1', '2', '3'}), false) .getString()); } @@ -61,11 +61,11 @@ public class PrintableStringTest { void testParseFail() { // EOF assertThrows(ParseException.class, () -> - new PrintableString(new BytesReader(new Byte[]{ 0x13, 1 }), false)); + new PrintableString(new BytesReader(new Byte[]{0x13, 1}), false)); // Illegal chars assertThrows(ParseException.class, () -> - new PrintableString(new BytesReader(new Byte[]{ 0x13, 2, '1', '*' }), false)); + new PrintableString(new BytesReader(new Byte[]{0x13, 2, '1', '*'}), false)); assertThrows(ParseException.class, () -> - new PrintableString(new BytesReader(new Byte[]{ 0x13, 2, '1', '@' }), false)); + new PrintableString(new BytesReader(new Byte[]{0x13, 2, '1', '@'}), false)); } } diff --git a/src/test/model/asn1/TagClassTest.java b/src/test/model/asn1/TagClassTest.java index d510618..e7d6531 100644 --- a/src/test/model/asn1/TagClassTest.java +++ b/src/test/model/asn1/TagClassTest.java @@ -2,7 +2,7 @@ package model.asn1; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; public class TagClassTest { @Test diff --git a/src/test/model/asn1/TagTest.java b/src/test/model/asn1/TagTest.java index 02df91f..a5d5d5f 100644 --- a/src/test/model/asn1/TagTest.java +++ b/src/test/model/asn1/TagTest.java @@ -24,35 +24,35 @@ public class TagTest { void testParseSuccess() throws ParseException { // Test basic parsing assertEquals(0x1, - new Tag(new BytesReader(new Byte[]{ 0x1 })).getNumber()); + new Tag(new BytesReader(new Byte[]{0x1})).getNumber()); assertEquals(TagClass.UNIVERSAL, - new Tag(new BytesReader(new Byte[]{ 0x1 })).getCls()); - assertFalse(new Tag(new BytesReader(new Byte[]{ 0x1 })).isConstructive()); + new Tag(new BytesReader(new Byte[]{0x1})).getCls()); + assertFalse(new Tag(new BytesReader(new Byte[]{0x1})).isConstructive()); // Test basic parsing with different class assertEquals(5, - new Tag(new BytesReader(new Byte[]{ 101 })).getNumber()); + new Tag(new BytesReader(new Byte[]{101})).getNumber()); assertEquals(TagClass.APPLICATION, - new Tag(new BytesReader(new Byte[]{ 101 })).getCls()); - assertTrue(new Tag(new BytesReader(new Byte[]{ 101 })).isConstructive()); + new Tag(new BytesReader(new Byte[]{101})).getCls()); + assertTrue(new Tag(new BytesReader(new Byte[]{101})).isConstructive()); // Test different classes assertEquals(TagClass.UNIVERSAL, - new Tag(new BytesReader(new Byte[]{ 1 })).getCls()); // 0b00000001 + new Tag(new BytesReader(new Byte[]{1})).getCls()); // 0b00000001 assertEquals(TagClass.PRIVATE, - new Tag(new BytesReader(new Byte[]{ -63 })).getCls()); // 0b11000001 + new Tag(new BytesReader(new Byte[]{-63})).getCls()); // 0b11000001 assertEquals(TagClass.CONTEXT_SPECIFIC, - new Tag(new BytesReader(new Byte[]{ -127 })).getCls()); // 0b10000001 + new Tag(new BytesReader(new Byte[]{-127})).getCls()); // 0b10000001 assertEquals(TagClass.APPLICATION, - new Tag(new BytesReader(new Byte[]{ 65 })).getCls()); // 0b01000001 + new Tag(new BytesReader(new Byte[]{65})).getCls()); // 0b01000001 // Test different numbers assertEquals(0x10, - new Tag(new BytesReader(new Byte[]{ 0x10 })).getNumber()); + new Tag(new BytesReader(new Byte[]{0x10})).getNumber()); assertEquals(31, - new Tag(new BytesReader(new Byte[]{ 31 })).getNumber()); + new Tag(new BytesReader(new Byte[]{31})).getNumber()); // Test constructive bit - assertFalse(new Tag(new BytesReader(new Byte[]{ 0x1 })).isConstructive()); - assertTrue(new Tag(new BytesReader(new Byte[]{ 33 })).isConstructive()); + assertFalse(new Tag(new BytesReader(new Byte[]{0x1})).isConstructive()); + assertTrue(new Tag(new BytesReader(new Byte[]{33})).isConstructive()); // Test modification - BytesReader reader = new BytesReader(new Byte[]{ 33 }); + BytesReader reader = new BytesReader(new Byte[]{33}); assertEquals(0, reader.getIndex()); new Tag(reader); assertEquals(1, reader.getIndex()); @@ -62,25 +62,25 @@ public class TagTest { void testParseFail() { // No enough bytes assertThrows(ParseException.class, () -> { - BytesReader reader = new BytesReader(new Byte[]{ 33 }); + BytesReader reader = new BytesReader(new Byte[]{33}); reader.require(1, true); new Tag(reader); }); // Number zero - assertThrows(ParseException.class, () -> new Tag(new BytesReader(new Byte[]{ 0 }))); + assertThrows(ParseException.class, () -> new Tag(new BytesReader(new Byte[]{0}))); } @Test void testEncode() { // Basic encoding - assertArrayEquals(new Byte[]{ 1 }, new Tag(TagClass.UNIVERSAL, false, 1).encodeDER()); - assertArrayEquals(new Byte[]{ 31 }, new Tag(TagClass.UNIVERSAL, false, 31).encodeDER()); + assertArrayEquals(new Byte[]{1}, new Tag(TagClass.UNIVERSAL, false, 1).encodeDER()); + assertArrayEquals(new Byte[]{31}, new Tag(TagClass.UNIVERSAL, false, 31).encodeDER()); // With different class - assertArrayEquals(new Byte[]{ -127 }, new Tag(TagClass.CONTEXT_SPECIFIC, false, 1).encodeDER()); - assertArrayEquals(new Byte[]{ -61 }, new Tag(TagClass.PRIVATE, false, 3).encodeDER()); - assertArrayEquals(new Byte[]{ 71 }, new Tag(TagClass.APPLICATION, false, 7).encodeDER()); + assertArrayEquals(new Byte[]{-127}, new Tag(TagClass.CONTEXT_SPECIFIC, false, 1).encodeDER()); + assertArrayEquals(new Byte[]{-61}, new Tag(TagClass.PRIVATE, false, 3).encodeDER()); + assertArrayEquals(new Byte[]{71}, new Tag(TagClass.APPLICATION, false, 7).encodeDER()); // With different constructive bit - assertArrayEquals(new Byte[]{ 63 }, new Tag(TagClass.UNIVERSAL, true, 31).encodeDER()); + assertArrayEquals(new Byte[]{63}, new Tag(TagClass.UNIVERSAL, true, 31).encodeDER()); } @Test diff --git a/src/test/model/asn1/UTF8StringTest.java b/src/test/model/asn1/UTF8StringTest.java index a2518ae..4e879f4 100644 --- a/src/test/model/asn1/UTF8StringTest.java +++ b/src/test/model/asn1/UTF8StringTest.java @@ -5,16 +5,14 @@ import model.asn1.parsing.BytesReader; import org.junit.jupiter.api.Test; import java.nio.charset.StandardCharsets; -import java.util.Arrays; -import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.*; public class UTF8StringTest { - private static final String[] UTF8_CHARS = new String[] { - new String(new byte[]{ -16, -97, -113, -77, -17, -72, -113, -30, - -128, -115, -30, -102, -89, -17, -72, -113 }, StandardCharsets.UTF_8), - new String(new byte[]{ -16, -97, -112, -79 }, StandardCharsets.UTF_8) + private static final String[] UTF8_CHARS = new String[]{ + new String(new byte[]{-16, -97, -113, -77, -17, -72, -113, -30, + -128, -115, -30, -102, -89, -17, -72, -113}, StandardCharsets.UTF_8), + new String(new byte[]{-16, -97, -112, -79}, StandardCharsets.UTF_8) }; @Test @@ -36,7 +34,7 @@ public class UTF8StringTest { }, new UTF8String(UTF8String.TAG, null, UTF8_CHARS[0] + UTF8_CHARS[1]) .encodeValueDER()); assertArrayEquals( - new Byte[] { + new Byte[]{ 0x0C, 6, // Tag - Length 0x68, 0x69, // Value -16, -97, -112, -79 @@ -46,12 +44,12 @@ public class UTF8StringTest { @Test void testParse() throws ParseException { assertEquals(UTF8_CHARS[0], - new UTF8String(new BytesReader(new Byte[]{ 0x0C, 16, + new UTF8String(new BytesReader(new Byte[]{0x0C, 16, -16, -97, -113, -77, -17, -72, -113, -30, -128, -115, -30, -102, -89, -17, -72, -113 }), false).getString()); assertEquals("", - new UTF8String(new BytesReader(new Byte[]{ 0x0C, 0, '1', '2', '3' }), false) + new UTF8String(new BytesReader(new Byte[]{0x0C, 0, '1', '2', '3'}), false) .getString()); } @@ -59,6 +57,6 @@ public class UTF8StringTest { void testParseFail() { // EOF assertThrows(ParseException.class, () -> - new UTF8String(new BytesReader(new Byte[]{ 0x0C, 1 }), false)); + new UTF8String(new BytesReader(new Byte[]{0x0C, 1}), false)); } } diff --git a/src/test/model/asn1/UtcTimeTest.java b/src/test/model/asn1/UtcTimeTest.java index 5ba7e13..41b5a9e 100644 --- a/src/test/model/asn1/UtcTimeTest.java +++ b/src/test/model/asn1/UtcTimeTest.java @@ -15,20 +15,20 @@ public class UtcTimeTest { final ZonedDateTime now = ZonedDateTime.now(); assertEquals(now, new UtcTime(UtcTime.TAG, null, now).getTimestamp()); - final ASN1Time parsed = new UtcTime(new BytesReader(new Byte[] { + final ASN1Time parsed = new UtcTime(new BytesReader(new Byte[]{ 0x17, 13, '1', '9', '0', '8', '1', '0', '1', '1', '4', '5', '1', '4', 'Z' }), false); assertEquals("190810114514Z", parsed.toString()); assertEquals(ZonedDateTime.of(2019, 8, 10, 11, 45, 14, - 0, ZoneId.of("UTC")), + 0, ZoneId.of("UTC")), parsed.getTimestamp()); } @Test void testParse() throws ParseException { - ASN1Time parsed = new UtcTime(new BytesReader(new Byte[] { + ASN1Time parsed = new UtcTime(new BytesReader(new Byte[]{ 0x17, 13, '2', '3', '0', '9', '2', '7', '1', '1', '4', '5', '1', '4', 'Z' }), false); @@ -38,7 +38,7 @@ public class UtcTimeTest { parsed.getTimestamp()); // No seconds - parsed = new UtcTime(new BytesReader(new Byte[] { + parsed = new UtcTime(new BytesReader(new Byte[]{ 0x17, 11, '2', '3', '0', '9', '2', '7', '1', '1', '4', '5', 'Z' }), false); @@ -98,20 +98,20 @@ public class UtcTimeTest { @Test void testEncode() throws ParseException { - assertEquals("230927114514Z", new UtcTime(new BytesReader(new Byte[] { + assertEquals("230927114514Z", new UtcTime(new BytesReader(new Byte[]{ -95, 15, 0x17, 13, '2', '3', '0', '9', '2', '7', '1', '1', '4', '5', '1', '4', 'Z' }), true).toString()); // No seconds - assertEquals("2309271145Z", new UtcTime(new BytesReader(new Byte[] { + assertEquals("2309271145Z", new UtcTime(new BytesReader(new Byte[]{ -95, 13, 0x17, 11, '2', '3', '0', '9', '2', '7', '1', '1', '4', '5', 'Z' }), true).toString()); // To byte array - assertArrayEquals(new Byte[] { + assertArrayEquals(new Byte[]{ 0x17, 11, '2', '3', '0', '9', '2', '7', '1', '1', '4', '5', 'Z' }, new UtcTime(UtcTime.TAG, null, ZonedDateTime.of(2023, 9, diff --git a/src/test/model/asn1/parsing/BytesReaderTest.java b/src/test/model/asn1/parsing/BytesReaderTest.java index 3b63a79..93c2765 100644 --- a/src/test/model/asn1/parsing/BytesReaderTest.java +++ b/src/test/model/asn1/parsing/BytesReaderTest.java @@ -13,14 +13,14 @@ public class BytesReaderTest { @BeforeEach void setup() { - target = new BytesReader(new Byte[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); + target = new BytesReader(new Byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); } @Test void testConstructor() { assertEquals(0, target.getIndex()); assertEquals(10, target.getRawInput().length); - assertArrayEquals(new Byte[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, target.getRawInput()); + assertArrayEquals(new Byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, target.getRawInput()); } @Test @@ -34,19 +34,19 @@ public class BytesReaderTest { void testRead() { assertEquals(10, target.bytesRemaining()); assertEquals(0, target.getIndex()); - assertArrayEquals(new Byte[]{ 1, 2 }, target.read(2, true)); + assertArrayEquals(new Byte[]{1, 2}, target.read(2, true)); assertEquals(8, target.bytesRemaining()); assertEquals(2, target.getIndex()); - assertArrayEquals(new Byte[]{ 3 }, target.read(1, false)); + assertArrayEquals(new Byte[]{3}, target.read(1, false)); assertEquals(8, target.bytesRemaining()); assertEquals(2, target.getIndex()); - assertArrayEquals(new Byte[]{ 3, 4, 5, 6, 7 }, target.read(5, true)); + assertArrayEquals(new Byte[]{3, 4, 5, 6, 7}, target.read(5, true)); assertEquals(3, target.bytesRemaining()); assertEquals(7, target.getIndex()); - assertArrayEquals(new Byte[]{ 8, 9, 10 }, target.read(3, false)); + assertArrayEquals(new Byte[]{8, 9, 10}, target.read(3, false)); assertEquals(3, target.bytesRemaining()); assertEquals(7, target.getIndex()); - assertArrayEquals(new Byte[]{ 8, 9, 10 }, target.read(3, true)); + assertArrayEquals(new Byte[]{8, 9, 10}, target.read(3, true)); assertEquals(0, target.bytesRemaining()); assertEquals(10, target.getIndex()); } @@ -55,11 +55,11 @@ public class BytesReaderTest { void testRequire() throws Exception { // TODO: Exception testing assertEquals(10, target.bytesRemaining()); assertEquals(0, target.getIndex()); - assertArrayEquals(new Byte[]{ 1, 2 }, target.require(2, true)); - assertArrayEquals(new Byte[]{ 3, 4, 5, 6, 7, 8, 9 }, target.require(7, true)); - assertArrayEquals(new Byte[]{ 10 }, target.require(1, false)); + assertArrayEquals(new Byte[]{1, 2}, target.require(2, true)); + assertArrayEquals(new Byte[]{3, 4, 5, 6, 7, 8, 9}, target.require(7, true)); + assertArrayEquals(new Byte[]{10}, target.require(1, false)); assertThrows(ParseException.class, () -> target.require(2, true)); - assertArrayEquals(new Byte[]{ 10 }, target.require(1, true)); + assertArrayEquals(new Byte[]{10}, target.require(1, true)); assertThrows(ParseException.class, () -> target.require(1, true)); } @@ -67,18 +67,18 @@ public class BytesReaderTest { void testValidateSize() throws Exception { // TODO: Exception testing assertEquals(10, target.bytesRemaining()); assertEquals(0, target.getIndex()); - assertArrayEquals(new Byte[]{ 1, 2 }, target.require(2, true)); - assertArrayEquals(new Byte[]{ 3, 4, 5, 6, 7, 8, 9 }, target.require(7, true)); - assertArrayEquals(new Byte[]{ 10 }, target.require(1, false)); + assertArrayEquals(new Byte[]{1, 2}, target.require(2, true)); + assertArrayEquals(new Byte[]{3, 4, 5, 6, 7, 8, 9}, target.require(7, true)); + assertArrayEquals(new Byte[]{10}, target.require(1, false)); target.validateSize(1); assertThrows(ParseException.class, () -> target.validateSize(2)); - assertArrayEquals(new Byte[]{ 10 }, target.require(1, true)); + assertArrayEquals(new Byte[]{10}, target.require(1, true)); assertThrows(ParseException.class, () -> target.validateSize(1)); } @Test void testDetectTag() throws Exception { - final BytesReader reader = new BytesReader(new Byte[]{ -95, 0 }); + final BytesReader reader = new BytesReader(new Byte[]{-95, 0}); assertTrue(reader.detectTag(new Tag(TagClass.CONTEXT_SPECIFIC, true, 1))); assertFalse(reader.detectTag(new Tag(TagClass.CONTEXT_SPECIFIC, true, 0))); assertFalse(reader.detectTag(new Tag(TagClass.UNIVERSAL, true, 0))); @@ -87,9 +87,9 @@ public class BytesReaderTest { @Test void testGetTag() throws Exception { - BytesReader reader = new BytesReader(new Byte[]{ -95, 0 }); + BytesReader reader = new BytesReader(new Byte[]{-95, 0}); assertEquals(1, reader.getTag(false).getNumber()); - reader = new BytesReader(new Byte[]{ -96, 0, -95, 0 }); + reader = new BytesReader(new Byte[]{-96, 0, -95, 0}); assertEquals(1, reader.getTag(true).getNumber()); } } |