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/IA5StringTest.java | 79 ++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/test/model/asn1/IA5StringTest.java (limited to 'src/test/model/asn1/IA5StringTest.java') diff --git a/src/test/model/asn1/IA5StringTest.java b/src/test/model/asn1/IA5StringTest.java new file mode 100644 index 0000000..dfaa1aa --- /dev/null +++ b/src/test/model/asn1/IA5StringTest.java @@ -0,0 +1,79 @@ +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.nio.charset.StandardCharsets; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import static org.junit.jupiter.api.Assertions.*; + +public class IA5StringTest { + private String stringToTest = ""; + + @BeforeEach + void setup() { + stringToTest = IntStream.range(0, 127) + .mapToObj(Character::toString) + .collect(Collectors.joining()); + } + + @Test + void testAcceptedString() throws ParseException { + assertEquals(stringToTest, + new IA5String(IA5String.TAG, null, stringToTest) + .getString()); + } + + @Test + void testIllegalStrings() { + assertThrows(ParseException.class, + () -> new IA5String(IA5String.TAG, null, + 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))); + } + + @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)) + .encodeValueDER()); + assertArrayEquals( + new Byte[] { + 0x16, 0x02, // Tag - Length + 0x68, 0x69 // Value + }, new IA5String(IA5String.TAG, null, "hi").encodeDER()); + assertArrayEquals( + new Byte[] { + -85, 0x05, // Parent Tag - Length + 0x16, 0x03, // Inner Tag - Length + 0x68, 0x69, 0x69 // Value + }, new IA5String(IA5String.TAG, + new Tag(TagClass.CONTEXT_SPECIFIC, true, 11), + "hii").encodeDER()); + } + + @Test + void testParse() throws ParseException { + assertEquals("123", + new IA5String(new BytesReader(new Byte[]{ 0x16, 3, '1', '2', '3' }), false) + .getString()); + assertEquals("", + 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)); + } +} -- cgit v1.2.3