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/UTF8StringTest.java | 64 +++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/test/model/asn1/UTF8StringTest.java (limited to 'src/test/model/asn1/UTF8StringTest.java') diff --git a/src/test/model/asn1/UTF8StringTest.java b/src/test/model/asn1/UTF8StringTest.java new file mode 100644 index 0000000..a2518ae --- /dev/null +++ b/src/test/model/asn1/UTF8StringTest.java @@ -0,0 +1,64 @@ +package model.asn1; + +import model.asn1.exceptions.ParseException; +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) + }; + + @Test + void testAcceptedString() throws ParseException { + final ASN1String string = + new UTF8String(PrintableString.TAG, null, "0123456789abCdExYz '()+,-./:=?*@" + + String.join("", UTF8_CHARS)); + assertEquals("0123456789abCdExYz '()+,-./:=?*@" + String.join("", UTF8_CHARS), + string.getString()); + } + + @Test + void testEncode() throws ParseException { + assertArrayEquals( + new Byte[]{ + -16, -97, -113, -77, -17, -72, -113, -30, + -128, -115, -30, -102, -89, -17, -72, -113, + -16, -97, -112, -79 + }, new UTF8String(UTF8String.TAG, null, UTF8_CHARS[0] + UTF8_CHARS[1]) + .encodeValueDER()); + assertArrayEquals( + new Byte[] { + 0x0C, 6, // Tag - Length + 0x68, 0x69, // Value + -16, -97, -112, -79 + }, new UTF8String(UTF8String.TAG, null, "hi" + UTF8_CHARS[1]).encodeDER()); + } + + @Test + void testParse() throws ParseException { + assertEquals(UTF8_CHARS[0], + 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) + .getString()); + } + + @Test + void testParseFail() { + // EOF + assertThrows(ParseException.class, () -> + new UTF8String(new BytesReader(new Byte[]{ 0x0C, 1 }), false)); + } +} -- cgit v1.2.3