2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-08-30 21:45:37 +00:00

bool type on wire in python messaging part

git-svn-id: svn://bind10.isc.org/svn/bind10/branches/parkinglot@311 e5f2f494-b856-4b98-b285-d166d9295462
This commit is contained in:
Jelte Jansen
2009-11-20 13:31:38 +00:00
parent 5b53b4fd6c
commit bc5d12940d
2 changed files with 37 additions and 0 deletions

View File

@@ -27,6 +27,7 @@ _ITEM_BLOB = 0x01
_ITEM_HASH = 0x02
_ITEM_LIST = 0x03
_ITEM_NULL = 0x04
_ITEM_BOOL = 0x05
_ITEM_UTF8 = 0x08
_ITEM_MASK = 0x0f
@@ -68,6 +69,10 @@ def _pack_blob(item):
"""Pack a blob (binary data) and its type/length prefix."""
return (_encode_length_and_type(item, _ITEM_BLOB))
def _pack_bool(item):
"""Pack a bool and its type/length prefix."""
return (_encode_length_and_type(_encode_bool(item), _ITEM_BOOL))
def _pack_array(item):
"""Pack a list (array) and its type/length prefix."""
return (_encode_length_and_type(_encode_array(item), _ITEM_LIST))
@@ -93,6 +98,8 @@ def _encode_item(item):
"""Encode each item depending on its type"""
if item == None:
return (_pack_nil())
elif type(item) == bool:
return (_pack_bool(item))
elif type(item) == dict:
return (_pack_hash(item))
elif type(item) == list:
@@ -102,6 +109,13 @@ def _encode_item(item):
else:
return (_pack_utf8(str(item)))
def _encode_bool(item):
"""Encode a boolean value into a bytearray of one byte (0=false)"""
if item:
return b'\x01'
else:
return b'\x00'
def _encode_array(item):
"""Encode an array, where each value is encoded recursively"""
ret = bytes()
@@ -170,6 +184,8 @@ def _decode_item(data):
if item_type == _ITEM_BLOB:
value = item
elif item_type == _ITEM_BOOL:
value = _decode_bool(item)
elif item_type == _ITEM_UTF8:
value = str(item, 'utf-8')
elif item_type == _ITEM_HASH:
@@ -183,6 +199,9 @@ def _decode_item(data):
return (value, data)
def _decode_bool(data):
return data == b'0x01'
def _decode_hash(data):
ret = {}
while len(data) > 0:

View File

@@ -76,5 +76,23 @@ class TestCCWireEncoding(unittest.TestCase):
decoded = ISC.CC.Message.from_wire(wire)
self.assertEqual(decoded["せんせい"], "string")
def test_to_wire_of_bool_true(self):
wire = ISC.CC.Message.to_wire({ "bool": True })
self.assertEqual(wire, b'Skan\x04bool%\x01\x01')
def test_to_wire_of_bool_false(self):
wire = ISC.CC.Message.to_wire({ "bool": False })
self.assertEqual(wire, b'Skan\x04bool%\x01\x00')
def test_from_wire_of_bool_true(self):
wire = b'Skan\x04bool%\x01\x01'
decoded = ISC.CC.Message.from_wire(wire)
self.assertEqual(decoded["bool"], True)
def test_from_wire_of_bool_true(self):
wire = b'Skan\x04bool%\x01\x00'
decoded = ISC.CC.Message.from_wire(wire)
self.assertEqual(decoded["bool"], False)
if __name__ == '__main__':
unittest.main()