From d342a45d98c4795b3a3fe1aaef5236ad4a782b55 Mon Sep 17 00:00:00 2001 From: Yuuta Liang Date: Thu, 12 Oct 2023 12:10:33 +0800 Subject: Implement data structures from X.680, X.501, X.509, and PKCS#10, with X.690 encoding / decoding support The implementation took four days, and it is still a little bit rough. Updated version should arrive soon. Signed-off-by: Yuuta Liang --- src/test/model/asn1/BitStringTest.java | 75 ++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/test/model/asn1/BitStringTest.java (limited to 'src/test/model/asn1/BitStringTest.java') diff --git a/src/test/model/asn1/BitStringTest.java b/src/test/model/asn1/BitStringTest.java new file mode 100644 index 0000000..c893b36 --- /dev/null +++ b/src/test/model/asn1/BitStringTest.java @@ -0,0 +1,75 @@ +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 BitStringTest { + @Test + void testConstructor() { + 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 }) + .getUnused()); + } + + @Test + void testConvert() { + // 00000010 00001000 + // 00000000 01000001 = 65 + 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 }) + .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) + .getVal()); + assertEquals(6, + 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) + .getConvertedVal()); + } + + @Test + void testParseFail() { + assertThrows(ParseException.class, () -> + 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)); + // 0b11000001 + assertThrows(ParseException.class, () -> + 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)); + // Unused bits = 6 -> 7 + assertThrows(ParseException.class, () -> + 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)); + assertThrows(ParseException.class, () -> + 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) + .encodeDER()); + } +} -- cgit v1.2.3