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/PrintableStringTest.java | 71 ++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/test/model/asn1/PrintableStringTest.java (limited to 'src/test/model/asn1/PrintableStringTest.java') diff --git a/src/test/model/asn1/PrintableStringTest.java b/src/test/model/asn1/PrintableStringTest.java new file mode 100644 index 0000000..f46f400 --- /dev/null +++ b/src/test/model/asn1/PrintableStringTest.java @@ -0,0 +1,71 @@ +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 PrintableStringTest { + private final String ILLEGAL_CHARS_TO_TEST = + "<>\\[]{}@!*&^%$#_|`~"; + + @Test + void testAcceptedString() throws ParseException { + final PrintableString string = + new PrintableString(PrintableString.TAG, null, "0123456789abCdExYz '()+,-./:=?"); + assertEquals("0123456789abCdExYz '()+,-./:=?", string.getString()); + } + + @Test + 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)) + ); + } + + @Test + void testEncode() throws ParseException { + assertArrayEquals( + new Byte[] { 0x68, 0x68, 0x68 }, + new PrintableString(PrintableString.TAG, null, "hhh").encodeValueDER()); + assertArrayEquals( + new Byte[] { + 0x13, 0x02, // Tag - Length + 0x68, 0x69 // Value + }, new PrintableString(PrintableString.TAG, null, "hi").encodeDER()); + assertArrayEquals( + new Byte[] { + -86, 0x05, // Parent Tag - Length + 0x13, 0x03, // Inner Tag - Length + 0x68, 0x69, 0x69 // Value + }, new PrintableString(PrintableString.TAG, + new Tag(TagClass.CONTEXT_SPECIFIC, true, 10), + "hii").encodeDER()); + } + + @Test + void testParse() throws ParseException { + assertEquals("123", + 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) + .getString()); + } + + @Test + void testParseFail() { + // EOF + assertThrows(ParseException.class, () -> + new PrintableString(new BytesReader(new Byte[]{ 0x13, 1 }), false)); + // Illegal chars + assertThrows(ParseException.class, () -> + new PrintableString(new BytesReader(new Byte[]{ 0x13, 2, '1', '*' }), false)); + assertThrows(ParseException.class, () -> + new PrintableString(new BytesReader(new Byte[]{ 0x13, 2, '1', '@' }), false)); + } +} -- cgit v1.2.3