def test_run_map_length_multibyte(): # Map length on 1 byte. data = CBORStream(BytesIO(bytes([ 0b10111000, 0b1 ]))) stack = MapInfo().run(data, ignore_handler) assert len(stack) == 1 assert type(stack[0]) == MapLen assert stack[0] == MapLen(1) stack2 = stack[0].run(data, ignore_handler) assert len(stack2) == 2 assert type(stack2[0]) == MapReadValue assert stack2[0] == MapReadValue(1) assert type(stack2[1]) == MajorType # Map length on 2 bytes. data = CBORStream(BytesIO(bytes([ 0b10111001, 0b1, 0b0 ]))) stack = MapInfo().run(data, ignore_handler) assert len(stack) == 1 assert type(stack[0]) == MapLen assert stack[0] == MapLen(2) stack2 = stack[0].run(data, ignore_handler) assert len(stack2) == 2 assert type(stack2[0]) == MapReadValue assert stack2[0] == MapReadValue(1 << 8) assert type(stack2[1]) == MajorType # Map length on 4 bytes. data = CBORStream(BytesIO(bytes([ 0b10111010, 0b1, 0b0, 0b0, 0b0 ]))) stack = MapInfo().run(data, ignore_handler) assert len(stack) == 1 assert type(stack[0]) == MapLen assert stack[0] == MapLen(4) stack2 = stack[0].run(data, ignore_handler) assert len(stack2) == 2 assert type(stack2[0]) == MapReadValue assert stack2[0] == MapReadValue(1 << 24) assert type(stack2[1]) == MajorType # Map length on 8 bytes. data = CBORStream(BytesIO(bytes([ 0b10111011, 0b1, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0 ]))) stack = MapInfo().run(data, ignore_handler) assert len(stack) == 1 assert type(stack[0]) == MapLen assert stack[0] == MapLen(8) stack2 = stack[0].run(data, ignore_handler) assert len(stack2) == 2 assert type(stack2[0]) == MapReadValue assert stack2[0] == MapReadValue(1 << 56) assert type(stack2[1]) == MajorType
def test_run_uint(): mt = MajorType() data = CBORStream(BytesIO(bytes([0b00010101]))) assert type(mt.run(data, None)) is UIntInfo data = CBORStream(BytesIO(bytes([0b00000000]))) assert type(mt.run(data, None)) is UIntInfo data = CBORStream(BytesIO(bytes([0b00010111]))) assert type(mt.run(data, None)) is UIntInfo data = CBORStream(BytesIO(bytes([0b00011111]))) assert type(mt.run(data, None)) is UIntInfo
def test_run_text_string_read_ietf(): handler = MockHandler() encoded_data = bytearray.fromhex('6449455446') data = CBORStream(BytesIO(encoded_data)) stack = TextString().run(data, handler.handler) assert len(stack) == 0 handler.assert_data('\"IETF\"')
def test_run_uint_same_byte(): handler = MockHandler() # UInt length lower than 24. data = CBORStream(BytesIO(bytes([0b00010111]))) stack = UIntInfo().run(data, handler.handler) handler.assert_data('23')
def test_run_byte_string_read_value(): handler = MockHandler() encoded_data = bytearray.fromhex('4401020304') data = CBORStream(BytesIO(encoded_data)) stack = ByteString().run(data, handler.handler) assert len(stack) == 0 handler.assert_data('\"01020304\"')
def test_FloatRead(): handler = MockHandler() data = CBORStream( BytesIO(bytes([0b01000001, 0b11001011, 0b01100000, 0b01000010]))) stack = FloatRead(4).run(data, handler.handler) assert len(stack) == 0 handler.assert_data('25.422000885009766')
def test_run_map_inf_key(): # Map with infinite length. data = CBORStream(BytesIO(bytes([]))) stack = MapInfKey().run(data, ignore_handler) assert len(stack) == 2 assert type(stack[0]) == MapInfValue assert type(stack[1]) == MajorType
def run(self, stream: CBORStream, handler): current = stream.peek(1) t = (ord(current) & MAJOR_TYPE_MASK) >> (8 - MAJOR_TYPE_SIZE) if t == 0: # should return the proper major type instance return UIntInfo() elif t == 1: # should return the proper major type instance return NegIntInfo() elif t == 2: return ByteString() elif t == 3: return TextString() elif t == 4: return ArrayInfo() elif t == 5: return MapInfo() elif t == 6: return TagInfo() elif t == 7: return Type7Info() return
def test_run_array_read(): handler = MockHandler() # Empty array. data = CBORStream(BytesIO(bytes([0b10000000]))) stack = ArrayInfo().run(data, handler.handler) assert len(stack) == 0 handler.assert_data('[]')
def test_run_array_length(): # Array length lower than 24. data = CBORStream(BytesIO(bytes([0b10000011]))) stack = ArrayInfo().run(data, ignore_handler) assert len(stack) == 2 assert type(stack[0]) == ArrayRead assert stack[0] == ArrayRead(3) assert type(stack[1]) == MajorType
def test_run_map_length(): # Map length lower than 24. data = CBORStream(BytesIO(bytes([0b10100011]))) stack = MapInfo().run(data, ignore_handler) assert len(stack) == 2 assert type(stack[0]) == MapReadValue assert stack[0] == MapReadValue(3) assert type(stack[1]) == MajorType
def test_run_map_info_empty(): handler = MockHandler() # Empty array. data = CBORStream(BytesIO(bytes([0b10100000]))) stack = MapInfo().run(data, handler.handler) assert len(stack) == 0 handler.assert_data('{}')
def test_run_tag_read(): handler = MockHandler() # No output expected. data = CBORStream(BytesIO(bytes([0b11000000]))) stack = TagInfo().run(data, handler.handler) assert len(stack) == 1 assert type(stack[0]) == MajorType handler.assert_data('')
def test_run_uint_multibyte(): handler = MockHandler() # UInt length on 1 byte. data = CBORStream(BytesIO(bytes([0b00011000, 0b11100101]))) stack = UIntInfo().run(data, ignore_handler) assert len(stack) == 1 stack2 = stack[0].run(data, handler.handler) assert len(stack2) == 0 handler.assert_data('229') # UInt length on 2 byte. data = CBORStream(BytesIO(bytes([0b00011001, 0b11100101, 0b10101010]))) stack = UIntInfo().run(data, ignore_handler) assert len(stack) == 1 stack2 = stack[0].run(data, handler.handler) assert len(stack2) == 0 handler.assert_data('58794') # UInt length on 4 byte. data = CBORStream( BytesIO( bytes([0b00011010, 0b11100101, 0b00100101, 0b11101101, 0b00000101]))) stack = UIntInfo().run(data, ignore_handler) assert len(stack) == 1 stack2 = stack[0].run(data, handler.handler) assert len(stack2) == 0 handler.assert_data('3844467973') # UInt length on 8 byte. data = CBORStream( BytesIO( bytes([ 0b00011011, 0b11100101, 0b00100101, 0b11101101, 0b00000101, 0b11100101, 0b00100101, 0b11101101, 0b00000101 ]))) stack = UIntInfo().run(data, ignore_handler) assert len(stack) == 1 stack2 = stack[0].run(data, handler.handler) assert len(stack2) == 0 handler.assert_data('16511864218398878981')
def test_run_tag_length_multibyte(): # Tag length on 1 byte. data = CBORStream(BytesIO(bytes([0b11011000, 0b1]))) stack = TagInfo().run(data, ignore_handler) assert len(stack) == 1 assert type(stack[0]) == TagRead assert stack[0] == TagRead(1) stack2 = stack[0].run(data, ignore_handler) assert len(stack2) == 1 assert type(stack2[0]) == MajorType # Tag length on 2 bytes. data = CBORStream(BytesIO(bytes([0b11011001, 0b1, 0b0]))) stack = TagInfo().run(data, ignore_handler) assert len(stack) == 1 assert type(stack[0]) == TagRead assert stack[0] == TagRead(2) stack2 = stack[0].run(data, ignore_handler) assert len(stack2) == 1 assert type(stack2[0]) == MajorType # Tag length on 4 bytes. data = CBORStream(BytesIO(bytes([0b11011010, 0b1, 0b0, 0b0, 0b0]))) stack = TagInfo().run(data, ignore_handler) assert len(stack) == 1 assert type(stack[0]) == TagRead assert stack[0] == TagRead(4) stack2 = stack[0].run(data, ignore_handler) assert len(stack2) == 1 assert type(stack2[0]) == MajorType # Tag length on 8 bytes. data = CBORStream( BytesIO(bytes([0b11011011, 0b1, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0]))) stack = TagInfo().run(data, ignore_handler) assert len(stack) == 1 assert type(stack[0]) == TagRead assert stack[0] == TagRead(8) stack2 = stack[0].run(data, ignore_handler) assert len(stack2) == 1 assert type(stack2[0]) == MajorType
def test_run_map_probe(): data = CBORStream(BytesIO(bytes([0b10100001]))) assert type(MajorType().run(data, None)) == MapInfo
def test_Type7Read_pass(): data = CBORStream(BytesIO(bytes([0b00000110]))) stack = Type7Read().run(data, ignore_handler) assert len(stack) == 0
def test_Type7Info_inf_end(): data = CBORStream(BytesIO(bytes([0b11111111]))) stack = Type7Info().run(data, ignore_handler) assert len(stack) == 1 assert stack[0] == 'break'
def test_Type7Info_float_8byte_next(): data = CBORStream(BytesIO(bytes([0b11111011]))) stack = Type7Info().run(data, ignore_handler) assert len(stack) == 1 assert type(stack[0]) == FloatRead assert stack[0] == FloatRead(8)
def test_Type7Info_simple_value_next(): data = CBORStream(BytesIO(bytes([0b11111000]))) stack = Type7Info().run(data, ignore_handler) assert len(stack) == 1 assert type(stack[0]) == Type7Read
def test_Type7Info_Null(): handler = MockHandler() data = CBORStream(BytesIO(bytes([0b00010110]))) stack = Type7Info().run(data, handler.handler) assert len(stack) == 0 handler.assert_data('null')
def test_run_type7_probe(): data = CBORStream(BytesIO(bytes([0b11100001]))) assert type(MajorType().run(data, None)) == Type7Info
def decode_stream(self, stream: RawIOBase, handler): decode_stream = CBORStream(stream) return self.__decode(decode_stream, handler)
def test_run_array_probe(): data = CBORStream(BytesIO(bytes([0b10000001]))) assert type(MajorType().run(data, None)) == ArrayInfo
def test_run_tag_length(): # Tag length lower than 24. data = CBORStream(BytesIO(bytes([0b11000011]))) stack = TagInfo().run(data, ignore_handler) assert len(stack) == 1 assert type(stack[0]) == MajorType
def test_run_uint_probe(): data = CBORStream(BytesIO(bytes([0b00000001]))) assert type(MajorType().run(data, None)) == UIntInfo
def test_run_byte_string_probe(): data = CBORStream(BytesIO(bytes([0b01000001]))) assert type(MajorType().run(data, None)) == ByteString
def decode_array(self, array: bytes, handler): decode_stream = CBORStream(BytesIO(array)) return self.__decode(decode_stream, handler)