From bc5d12940d1ef150570b0d9d23aef5b534b6f11a Mon Sep 17 00:00:00 2001 From: Jelte Jansen Date: Fri, 20 Nov 2009 13:31:38 +0000 Subject: [PATCH] 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 --- src/lib/cc/python/ISC/CC/Message.py | 19 +++++++++++++++++++ src/lib/cc/python/test.py | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/lib/cc/python/ISC/CC/Message.py b/src/lib/cc/python/ISC/CC/Message.py index 246c7c6650..6fdea674be 100644 --- a/src/lib/cc/python/ISC/CC/Message.py +++ b/src/lib/cc/python/ISC/CC/Message.py @@ -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: diff --git a/src/lib/cc/python/test.py b/src/lib/cc/python/test.py index 881abb09fc..635f1cf189 100644 --- a/src/lib/cc/python/test.py +++ b/src/lib/cc/python/test.py @@ -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()