Skip to content

Commit

Permalink
Add type unsigned integer
Browse files Browse the repository at this point in the history
This allows to force unsigned integer values e.g. for network ports.
  • Loading branch information
jlusiardi authored Mar 14, 2020
1 parent a4eaa78 commit 06bd2ea
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 118 deletions.
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@

# Changes

## Version 0.7.0

New Features:

- Add data type `tlv8.DataType.UNSIGNED_INTEGER` to make a distinction between the default of
signed integers and unsigned integer. This is important for read network ports from TLV
structures

## Version 0.6.0

New Features:
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
setuptools.setup(
name='tlv8',
packages=setuptools.find_packages(exclude=['tests']),
version='0.6.0',
version='0.7.0',
description='Python module to handle type-length-value (TLV) encoded data 8-bit type, 8-bit length, and N-byte '
'value as described within the Apple HomeKit Accessory Protocol Specification Non-Commercial Version '
'Release R2.',
author='Joachim Lusiardi',
author_email='[email protected]',
url='https://github.com/jlusiardi/tlv8_python',
download_url='https://github.com/jlusiardi/tlv8_python/archive/0.6.0.tar.gz',
download_url='https://github.com/jlusiardi/tlv8_python/archive/0.7.0.tar.gz',
keywords=['TLV', 'Type-Length-Value', 'tlv8'],
classifiers=[
'License :: OSI Approved :: Apache Software License',
Expand Down
4 changes: 3 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
#

__all__ = [
'TestTLV8', 'TestTLV8Decode', 'TestTLV8Entry', 'TestTLV8Enum', 'TestTLV8EntryList', 'TestTLV8DeepDecode'
'TestTLV8', 'TestTLV8Decode', 'TestTLV8Entry', 'TestTLV8Enum', 'TestTLV8EntryList', 'TestTLV8DeepDecode',
'TestTLV8DecodeInteger'
]

from tests.tlv8_encode_tests import TestTLV8
Expand All @@ -24,3 +25,4 @@
from tests.tlv8_entry_tests import TestTLV8Entry
from tests.tlv8_enum_test import TestTLV8Enum
from tests.tlv8_entrylist_tests import TestTLV8EntryList
from tests.tlv8_decode_integer_tests import TestTLV8DecodeInteger
175 changes: 175 additions & 0 deletions tests/tlv8_decode_integer_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)
53 changes: 0 additions & 53 deletions tests/tlv8_decode_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,59 +86,6 @@ def test_decode_float(self):
])
self.assertEqual(expected, result)

def test_decode_int1(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_int2(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_int3(self):
input_data = b'\x01\x03' + pack('<i', 12345)
structure = {
1: tlv8.DataType.INTEGER,
}
self.assertRaises(ValueError, tlv8.decode, input_data, structure)

def test_decode_int4(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(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_example_2(self):
input_data = b'\x01\x01\x00'
structure = {
Expand Down
Loading

0 comments on commit 06bd2ea

Please sign in to comment.