File: tlv8_decode_integer_tests.py

package info (click to toggle)
tlv8-python 0.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 216 kB
  • sloc: python: 1,518; sh: 7; makefile: 3
file content (175 lines) | stat: -rw-r--r-- 5,382 bytes parent folder | download
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#
# Copyright 2020 Joachim Lusiardi
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import unittest
from struct import pack

import tlv8


class TestTLV8DecodeInteger(unittest.TestCase):
    def test_decode_int1_neg(self):
        input_data = b'\x01\x01' + pack('<b', -123)
        structure = {
            1: tlv8.DataType.INTEGER,
        }
        result = tlv8.decode(input_data, structure)
        expected = tlv8.EntryList([
            tlv8.Entry(1, -123),
        ])
        self.assertEqual(expected, result)

    def test_decode_int1_pos(self):
        input_data = b'\x01\x01' + pack('<b', 123)
        structure = {
            1: tlv8.DataType.INTEGER,
        }
        result = tlv8.decode(input_data, structure)
        expected = tlv8.EntryList([
            tlv8.Entry(1, 123),
        ])
        self.assertEqual(expected, result)

    def test_decode_int1_un(self):
        input_data = b'\x01\x01' + pack('<B', 123)
        structure = {
            1: tlv8.DataType.UNSIGNED_INTEGER,
        }
        result = tlv8.decode(input_data, structure)
        expected = tlv8.EntryList([
            tlv8.Entry(1, 123),
        ])
        self.assertEqual(expected, result)

    def test_decode_int2_neg(self):
        input_data = b'\x01\x02' + pack('<h', -12345)
        structure = {
            1: tlv8.DataType.INTEGER,
        }
        result = tlv8.decode(input_data, structure)
        expected = tlv8.EntryList([
            tlv8.Entry(1, -12345),
        ])
        self.assertEqual(expected, result)

    def test_decode_int2_pos(self):
        input_data = b'\x01\x02' + pack('<h', 12345)
        structure = {
            1: tlv8.DataType.INTEGER,
        }
        result = tlv8.decode(input_data, structure)
        expected = tlv8.EntryList([
            tlv8.Entry(1, 12345),
        ])
        self.assertEqual(expected, result)

    def test_decode_int2_un(self):
        input_data = b'\x01\x02' + pack('<H', 12345)
        structure = {
            1: tlv8.DataType.UNSIGNED_INTEGER,
        }
        result = tlv8.decode(input_data, structure)
        expected = tlv8.EntryList([
            tlv8.Entry(1, 12345),
        ])
        self.assertEqual(expected, result)

    def test_decode_int3_signed(self):
        input_data = b'\x01\x03\x01\x02\x03\x02\x02\x02\x03'
        structure = {
            1: tlv8.DataType.INTEGER,
        }
        self.assertRaises(ValueError, tlv8.decode, input_data, structure)

    def test_decode_int3_unsigned(self):
        input_data = b'\x01\x03\x01\x02\x03\x02\x02\x02\x03'
        structure = {
            1: tlv8.DataType.UNSIGNED_INTEGER,
        }
        self.assertRaises(ValueError, tlv8.decode, input_data, structure)
        # tlv8.decode(input_data, structure)

    def test_decode_int4_neg(self):
        input_data = b'\x01\x04' + pack('<i', -12345)
        structure = {
            1: tlv8.DataType.INTEGER,
        }
        result = tlv8.decode(input_data, structure)

        expected = tlv8.EntryList([
            tlv8.Entry(1, -12345),
        ])
        self.assertEqual(expected, result)

    def test_decode_int4_pos(self):
        input_data = b'\x01\x04' + pack('<i', 12345)
        structure = {
            1: tlv8.DataType.INTEGER,
        }
        result = tlv8.decode(input_data, structure)

        expected = tlv8.EntryList([
            tlv8.Entry(1, 12345),
        ])
        self.assertEqual(expected, result)

    def test_decode_int4_un(self):
        input_data = b'\x01\x04' + pack('<I', 12345)
        structure = {
            1: tlv8.DataType.UNSIGNED_INTEGER,
        }
        result = tlv8.decode(input_data, structure)

        expected = tlv8.EntryList([
            tlv8.Entry(1, 12345),
        ])
        self.assertEqual(expected, result)

    def test_decode_int8_neg(self):
        input_data = b'\x01\x08' + pack('<q', -4611686018427387904)
        structure = {
            1: tlv8.DataType.INTEGER,
        }
        result = tlv8.decode(input_data, structure)

        expected = tlv8.EntryList([
            tlv8.Entry(1, -4611686018427387904),
        ])
        self.assertEqual(expected, result)

    def test_decode_int8_pos(self):
        input_data = b'\x01\x08' + pack('<q', 4611686018427387904)
        structure = {
            1: tlv8.DataType.INTEGER,
        }
        result = tlv8.decode(input_data, structure)

        expected = tlv8.EntryList([
            tlv8.Entry(1, 4611686018427387904),
        ])
        self.assertEqual(expected, result)

    def test_decode_int8_un(self):
        input_data = b'\x01\x08' + pack('<q', 4611686018427387904)
        structure = {
            1: tlv8.DataType.UNSIGNED_INTEGER,
        }
        result = tlv8.decode(input_data, structure)

        expected = tlv8.EntryList([
            tlv8.Entry(1, 4611686018427387904),
        ])
        self.assertEqual(expected, result)