aboutsummaryrefslogtreecommitdiff
path: root/src/test/model/asn1/UtcTimeTest.java
blob: 5ba7e13263d5b0104d1818651fcaba8d57adad6b (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package model.asn1;

import model.asn1.exceptions.ParseException;
import model.asn1.parsing.BytesReader;
import org.junit.jupiter.api.Test;

import java.time.ZoneId;
import java.time.ZonedDateTime;

import static org.junit.jupiter.api.Assertions.*;

public class UtcTimeTest {
    @Test
    void testConstructor() throws ParseException {
        final ZonedDateTime now = ZonedDateTime.now();
        assertEquals(now, new UtcTime(UtcTime.TAG, null, now).getTimestamp());

        final ASN1Time parsed = new UtcTime(new BytesReader(new Byte[] {
                0x17, 13,
                '1', '9', '0', '8', '1', '0', '1', '1', '4', '5', '1', '4', 'Z'
        }), false);
        assertEquals("190810114514Z",
                parsed.toString());
        assertEquals(ZonedDateTime.of(2019, 8, 10, 11, 45, 14,
                0, ZoneId.of("UTC")),
                parsed.getTimestamp());
    }

    @Test
    void testParse() throws ParseException {
        ASN1Time parsed = new UtcTime(new BytesReader(new Byte[] {
                0x17, 13,
                '2', '3', '0', '9', '2', '7', '1', '1', '4', '5', '1', '4', 'Z'
        }), false);

        assertEquals(ZonedDateTime.of(2023, 9, 27, 11, 45, 14,
                        0, ZoneId.of("UTC")),
                parsed.getTimestamp());

        // No seconds
        parsed = new UtcTime(new BytesReader(new Byte[] {
                0x17, 11,
                '2', '3', '0', '9', '2', '7', '1', '1', '4', '5', 'Z'
        }), false);
        assertEquals(ZonedDateTime.of(2023, 9, 27, 11, 45, 0,
                        0, ZoneId.of("UTC")),
                parsed.getTimestamp());

        // Length 0
        assertThrows(ParseException.class, () ->
                new UtcTime(new BytesReader(new Byte[]{
                        0x17, 0
                }), false));
        // Early EOF
        assertThrows(ParseException.class, () ->
                new UtcTime(new BytesReader(new Byte[]{
                        0x17, 13
                }), false));
        // No tailing Z
        assertThrows(ParseException.class, () ->
                new UtcTime(new BytesReader(new Byte[]{
                        0x17, 12,
                        '2', '3', '0', '9', '2', '7', '1', '1', '4', '5', '1', '4'
                }), false));
        // Custom timezone
        assertThrows(ParseException.class, () ->
                new UtcTime(new BytesReader(new Byte[]{
                        0x17, 18,
                        '2', '0', '2', '3', '0', '9', '2', '7', '1', '1', '4', '5', '1', '4', 'Z', '-', '0', '8'
                }), false));
        // Invalid month / day / hour / minute / second
        assertThrows(ParseException.class, () ->
                new UtcTime(new BytesReader(new Byte[]{
                        0x17, 13,
                        '2', '3', '1', '3', '2', '7', '1', '1', '4', '5', '1', '4', 'Z'
                }), false));
        assertThrows(ParseException.class, () ->
                new UtcTime(new BytesReader(new Byte[]{
                        0x17, 13,
                        '2', '3', '1', '0', '3', '2', '1', '1', '4', '5', '1', '4', 'Z'
                }), false));
        assertThrows(ParseException.class, () ->
                new UtcTime(new BytesReader(new Byte[]{
                        0x17, 13,
                        '2', '3', '1', '0', '3', '0', '2', '5', '4', '5', '1', '4', 'Z'
                }), false));
        assertThrows(ParseException.class, () ->
                new UtcTime(new BytesReader(new Byte[]{
                        0x17, 13,
                        '2', '3', '1', '0', '3', '0', '2', '4', '6', '1', '1', '4', 'Z'
                }), false));
        assertThrows(ParseException.class, () ->
                new UtcTime(new BytesReader(new Byte[]{
                        0x17, 13,
                        '2', '3', '1', '0', '3', '0', '2', '4', '6', '0', '6', '1', 'Z'
                }), false));
    }

    @Test
    void testEncode() throws ParseException {
        assertEquals("230927114514Z", new UtcTime(new BytesReader(new Byte[] {
                -95, 15,
                0x17, 13,
                '2', '3', '0', '9', '2', '7', '1', '1', '4', '5', '1', '4', 'Z'
        }), true).toString());
        // No seconds
        assertEquals("2309271145Z", new UtcTime(new BytesReader(new Byte[] {
                -95, 13,
                0x17, 11,
                '2', '3', '0', '9', '2', '7', '1', '1', '4', '5', 'Z'
        }), true).toString());

        // To byte array
        assertArrayEquals(new Byte[] {
                0x17, 11,
                '2', '3', '0', '9', '2', '7', '1', '1', '4', '5', 'Z'
        }, new UtcTime(UtcTime.TAG, null, ZonedDateTime.of(2023, 9,
                27, 11, 45, 0, 0, ZoneId.of("UTC")))
                .encodeDER());
    }
}