aboutsummaryrefslogtreecommitdiff
path: root/src/test/model/asn1/PrintableStringTest.java
blob: f46f4006bc70f67147afdfc44600c9419001340c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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));
    }
}