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 IntTest { @Test void testConstructor() { assertEquals(0x2, new Int(Int.TAG, null, 255).getTag().getNumber()); assertEquals(255, new Int(Int.TAG, null, 255).getLong()); } @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()); // 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()); // 2 ^ 63 + 1 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()); // Test no leading ones // Not 0xFF, 0x80 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()); } @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()); // Multiple bytes assertEquals(256, 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()); assertEquals(65536, 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()); assertEquals(16777216, 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()); assertEquals(4294967296L, 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 }), false).getValue().longValue()); // Test no leading zeros // Not 0x00, 0xFF 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()); } @Test void testParseFail() { // Not enough bytes assertThrows(ParseException.class, () -> 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 }), false)); } }