aboutsummaryrefslogtreecommitdiff
path: root/src/test/model/asn1/IntTest.java
blob: 6e92eb0ec6aebc8bcae3370df3340595ce00ee9d (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
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 IntTest {
    @Test
    void testConstructor() {
        assertEquals(0x2, new Int(Int.TAG, null, 255).getTag().getNumber());
        assertEquals(255, new Int(Int.TAG, null, 255).getLong());
    }

    @Test
    void testEncode() {
        // Single-byte
        assertArrayEquals(new Byte[] { 0x0 }, new Int(Int.TAG, null, 0).encodeValueDER());
        assertArrayEquals(new Byte[] { 0x1 }, new Int(Int.TAG, null, 1).encodeValueDER());
        assertArrayEquals(new Byte[] { -1 }, new Int(Int.TAG, null, 255).encodeValueDER());

        // Multiple bytes
        assertArrayEquals(new Byte[] { 0x01, 0x00 }, new Int(Int.TAG, null, 256).encodeValueDER());
        assertArrayEquals(new Byte[] { -1, -1 }, new Int(Int.TAG, null, 65535).encodeValueDER());
        assertArrayEquals(new Byte[] { 0x01, 0x00, 0x00 }, new Int(Int.TAG, null, 65536).encodeValueDER());
        assertArrayEquals(new Byte[] { -1, -1, -1 }, new Int(Int.TAG, null, 16777215).encodeValueDER());
        assertArrayEquals(new Byte[] { 0x01, 0x00, 0x00, 0x00 }, new Int(Int.TAG, null, 16777216).encodeValueDER());
        assertArrayEquals(new Byte[] { -1, -1, -1, -1 }, new Int(Int.TAG, null, 4294967295L).encodeValueDER());
        assertArrayEquals(new Byte[] { -1, -1, -1, -1 }, new Int(Int.TAG, null, 4294967295L).encodeValueDER());

        // 2 ^ 63 + 1
        assertArrayEquals(new Byte[] { -128, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
                new Int(Int.TAG, null, Long.parseUnsignedLong("9223372036854775809")).encodeValueDER());

        // Test no leading zeros
        // Not 0x00, 0xFF
        assertArrayEquals(new Byte[] { -1 }, new Int(Int.TAG, null, 255).encodeValueDER());

        // Test no leading ones
        // Not 0xFF, 0x80
        assertArrayEquals(new Byte[] { -128 }, new Int(Int.TAG, null, -128).encodeValueDER());

        // Encode DER
        assertArrayEquals(new Byte[] { 0x02, 2, 0x01, 0x00 }, new Int(Int.TAG, null, 256).encodeDER());
    }

    @Test
    void testParse() throws ParseException {
        // Single-byte
        assertEquals(0, new Int(new BytesReader(new Byte[] { 0x2, 1, 0x0 }), false).getLong());
        assertEquals(1, new Int(new BytesReader(new Byte[] { 0x2, 1, 0x1 }), false).getLong());
        assertEquals(-1, new Int(new BytesReader(new Byte[] { 0x2, 1, -1 }), false).getLong());

        // Multiple bytes
        assertEquals(256,
                new Int(new BytesReader(new Byte[] { 0x2, 2, 0x01, 0x00 }), false).getLong());
        assertEquals(-1,
                new Int(new BytesReader(new Byte[] { 0x2, 2, -1, -1 }), false).getLong());
        assertEquals(65536,
                new Int(new BytesReader(new Byte[] { 0x2, 3, 0x01, 0x00, 0x00 }), false).getLong());
        assertEquals(-1,
                new Int(new BytesReader(new Byte[] { 0x2, 3, -1, -1, -1 }), false).getLong());
        assertEquals(16777216,
                new Int(new BytesReader(new Byte[] { 0x2, 4, 0x01, 0x00, 0x00, 0x00 }), false).getLong());
        assertEquals(-1,
                new Int(new BytesReader(new Byte[] { 0x2, 4, -1, -1, -1, -1 }), false).getLong());
        assertEquals(4294967296L,
                new Int(new BytesReader(new Byte[] { 0x2, 5, 0x01, 0x00, 0x00, 0x00, 0x00 }),false).getLong());

        // 2 ^ 63 + 1
        assertEquals(Long.parseUnsignedLong("9223372036854775809"),
                new Int(new BytesReader(new Byte[] { 0x2, 9, 0x00, -128, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }),
                        false).getValue().longValue());

        // Test no leading zeros
        // Not 0x00, 0xFF
        assertEquals(255, new Int(new BytesReader(new Byte[] { 0x02, 0x2, 0x0, -1 }), false).getLong());

        // Test no leading ones
        // Not 0xFF, 0x80
        assertArrayEquals(new Byte[] { -128 }, new Int(Int.TAG, null, -128).encodeValueDER());
    }

    @Test
    void testParseFail() {
        // Not enough bytes
        assertThrows(ParseException.class, () ->
                new Int(new BytesReader(new Byte[]{ 0x2, 0x7, -1, -1, -1, -1, -1, -1 }),
                        false));
        // Zero len
        assertThrows(ParseException.class, () ->
                new Int(new BytesReader(new Byte[]{ 0x2, 0x0, -1, -1, -1, -1, -1, -1 }),
                        false));
    }
}