def test_valid_make(self): """Test full packing.""" my_message = Message('my_msg', [ ('my_fixed', 'F', 'i', 8), ('not_specified_fixed', 'F', 'i', 8), ('other_fixed', 'F', 'i', 8, 3), ('just_a_num', 'i'), ('a_string', '32s'), ('this_fixed', 'F', 'i', 5), ], Mode.Big) data = { 'my_fixed': 1.1 + 2.2, 'other_fixed': Decimal('1.1') + Decimal('2.2'), 'not_specified_fixed': Decimal('1.1') + Decimal('2.2'), 'just_a_num': 16, 'a_string': '=====================', 'this_fixed': '1.9375', } packed = my_message.pack(data) unpacked = my_message.unpack(packed) assert unpacked.a_string == data['a_string'] assert unpacked.just_a_num == data['just_a_num'] assert unpacked.other_fixed == Decimal('3.3') assert unpacked.my_fixed == Decimal('3.296875') assert unpacked.not_specified_fixed == Decimal('3.296875') assert unpacked.this_fixed == Decimal(data['this_fixed'])
def test_length_after_item(self): num_repeats = 3 TestStruct = Message('TestStruct', [ ('vardata', self.VarTest, 'length'), ('length', 'H', 'vardata'), ('repeated_data', self.Repeated, num_repeats), ]) test_data = { 'length': 2, 'vardata': [ {'x': 1, 'y': 2}, {'x': 3, 'y': 4}, ], 'repeated_data': [ {'x': 7, 'z': 13}, {'x': 2, 'z': 27}, {'x': 6, 'z': 11}, ], } made = TestStruct.make(test_data) assert made.length == 2 assert made.vardata[0].x == 1 assert made.vardata[0].y == 2 packed = TestStruct.pack(test_data) unpacked = TestStruct.unpack(packed) assert unpacked
def test_unpacking_of_too_many_bytes(self): packed_element = \ struct.pack('H', 1) + \ struct.pack('BB', 255, 127) + \ struct.pack('H', 10) + \ struct.pack('BB', 254, 126) + \ struct.pack('BB', 25, 16) + \ struct.pack('BB', 24, 26) + \ struct.pack('BB', 24, 26) + \ struct.pack('BB', 24, 26) + \ struct.pack('BB', 24, 26) + \ struct.pack('BB', 24, 26) + \ struct.pack('BB', 24, 26) + \ struct.pack('BB', 54, 17) TestStruct = Message('TestStruct', [ ('length_in_objects', 'H', 'vardata'), ('vardata', self.VarTest, 'length_in_objects'), (b'length_in_bytes', 'H', 'bytesdata'), ('bytesdata', self.VarTest, b'length_in_bytes'), ]) with pytest.raises(ValueError): unpacked = TestStruct.unpack(packed_element) assert unpacked
def test_unpack_in_the_middle(self): SomeMessage = Message('SomeMessage', [ ('regular', 'B'), ('irregular', 'B'), ('confused', 'B'), ]) TestStruct = Message('TestStruct', [ ('regular', 'B'), ('middle_constant', 'II', (0xAB, 0xBA)), ('a_variable_length', 'H', 'msg'), ('msg', SomeMessage, 'a_variable_length') ], Mode.Little) test_data = { 'regular': 8, 'a_variable_length': 2, 'msg': [ {'regular': 4, 'irregular': 0, 'confused': 6}, {'regular': 5, 'irregular': 2, 'confused': 4}, ], } made = TestStruct.make(**test_data) assert made.regular == 8 assert made.middle_constant == (0xAB, 0xBA) packed = TestStruct.pack(test_data) assert packed == b'\x08\xab\x00\x00\x00\xba\x00\x00\x00\x02\x00\x04\x00\x06\x05\x02\x04' unpacked = TestStruct.unpack(packed) assert unpacked.regular == 8 assert unpacked.middle_constant == (0xAB, 0xBA)
def test_make_andk_pack(self): """Test field formats that are valid ElementString elements.""" TestStruct = Message( "TestStruct", [ ("a", "c"), # single character ("b", "2c"), # 2 char string ("c", "10s"), # 10 char string (variable) ("d", "9p"), # 9 ( - 1 ) char string (fixed) ("e", "5c"), ], ) test_data = {"a": "i", "b": "hi", "c": "short", "d": "long", "e": ["l", "i", "s", "t"]} made = TestStruct.make(test_data) assert made.a == ["i"] assert made.b == ["h", "i"] assert made.c == "short" assert made.d == "long\x00\x00\x00\x00" assert made.e == ["l", "i", "s", "t", "\x00"] packed = TestStruct.pack(test_data) unpacked = TestStruct.unpack(packed) assert made == unpacked
def test_unpack_big_endian(self): """Test unpack the test formats.""" test_msg = Message('test', self.teststruct, Mode.Big) for idx in range(len(self.testvalues)): with self.subTest(idx): # pylint: disable=no-member (unpacked_partial_msg, unused) = test_msg.unpack_partial(self.testbytes['big'][idx] + b'\xde\xad') self.assertEqual(unused, b'\xde\xad') unpacked_msg = test_msg.unpack(self.testbytes['big'][idx]) expected_tuple = test_msg.make(**self.testvalues[idx]) # pylint: disable=protected-access self.assertEqual(unpacked_msg, unpacked_partial_msg) self.assertEqual(unpacked_msg, expected_tuple)
def test_unpack_little_endian(self): """Test unpack the test formats.""" test_msg = Message('test', self.teststruct, Mode.Little) assert test_msg.mode.to_byteorder() == 'little' for idx in range(len(self.testvalues)): with self.subTest(idx): # pylint: disable=no-member (unpacked_partial_msg, unused) = test_msg.unpack_partial(self.testbytes['little'][idx] + b'\xde\xad') self.assertEqual(unused, b'\xde\xad') unpacked_msg = test_msg.unpack(self.testbytes['little'][idx]) expected_tuple = test_msg.make(**self.testvalues[idx]) # pylint: disable=protected-access self.assertEqual(unpacked_msg, unpacked_partial_msg) self.assertEqual(unpacked_msg, expected_tuple)
def test_unpack(self): TestStruct = Message('TestStruct', [ ('regular', 'B'), # Two regular messages ('fill_in_later', 'H'), ('ending_sequence', 'II', (0xAB, 0xBA)), # An ending sequence to a message ], Mode.Little) test_data = { 'regular': 8, 'fill_in_later': 7, } test_bytes = b'\x08\x07\x00\xab\x00\x00\x00\xba\x00\x00\x00' assert test_bytes == TestStruct.pack(**test_data) unpacked = TestStruct.unpack(test_bytes) assert unpacked.regular == 8 assert unpacked.fill_in_later == 7 assert unpacked.ending_sequence == (0xAB, 0xBA)
def test_unpack_in_the_middle(self): SomeMessage = Message('SomeMessage', [ ('regular', 'B'), ('irregular', 'B'), ('confused', 'B'), ]) TestStruct = Message('TestStruct', [('regular', 'B'), ('middle_constant', 'II', (0xAB, 0xBA)), ('a_variable_length', 'H', 'msg'), ('msg', SomeMessage, 'a_variable_length')], Mode.Little) test_data = { 'regular': 8, 'a_variable_length': 2, 'msg': [ { 'regular': 4, 'irregular': 0, 'confused': 6 }, { 'regular': 5, 'irregular': 2, 'confused': 4 }, ], } made = TestStruct.make(**test_data) assert made.regular == 8 assert made.middle_constant == (0xAB, 0xBA) packed = TestStruct.pack(test_data) assert packed == b'\x08\xab\x00\x00\x00\xba\x00\x00\x00\x02\x00\x04\x00\x06\x05\x02\x04' unpacked = TestStruct.unpack(packed) assert unpacked.regular == 8 assert unpacked.middle_constant == (0xAB, 0xBA)
def test_unpacking_of_too_little_bytes(self): # Only pack four elements, instead of the five packed_element = \ struct.pack('H', 1) + \ struct.pack('BB', 255, 127) + \ struct.pack('H', 10) + \ struct.pack('BB', 254, 126) + \ struct.pack('BB', 25, 16) + \ struct.pack('BB', 24, 26) + \ struct.pack('BB', 54, 17) TestStruct = Message('TestStruct', [ ('length_in_objects', 'H', 'vardata'), ('vardata', self.VarTest, 'length_in_objects'), (b'length_in_bytes', 'H', 'bytesdata'), ('bytesdata', self.VarTest, b'length_in_bytes'), ]) with pytest.raises(struct.error): unpacked = TestStruct.unpack(packed_element) assert unpacked
def test_unpack(self): TestStruct = Message( 'TestStruct', [ ('regular', 'B'), # Two regular messages ('fill_in_later', 'H'), ('ending_sequence', 'II', (0xAB, 0xBA)), # An ending sequence to a message ], Mode.Little) test_data = { 'regular': 8, 'fill_in_later': 7, } test_bytes = b'\x08\x07\x00\xab\x00\x00\x00\xba\x00\x00\x00' assert test_bytes == TestStruct.pack(**test_data) unpacked = TestStruct.unpack(test_bytes) assert unpacked.regular == 8 assert unpacked.fill_in_later == 7 assert unpacked.ending_sequence == (0xAB, 0xBA)
def test_unpacking_of_correct_size(self): packed_element = \ struct.pack('H', 1) + \ struct.pack('BB', 255, 127) + \ struct.pack('H', 10) + \ struct.pack('BB', 254, 126) + \ struct.pack('BB', 25, 16) + \ struct.pack('BB', 24, 26) + \ struct.pack('BB', 54, 17) + \ struct.pack('BB', 25, 12) TestStruct = Message('TestStruct', [ ('length_in_objects', 'H', 'vardata'), ('vardata', self.VarTest, 'length_in_objects'), (b'length_in_bytes', 'H', 'bytesdata'), ('bytesdata', self.VarTest, b'length_in_bytes'), ]) unpacked = TestStruct.unpack(packed_element) assert unpacked assert unpacked.length_in_objects == 1 assert unpacked.length_in_bytes == 10
def test_adding_element_list(self): def adder(*args): return sum(args) AdderMessage = Message('AdderMessage', [ ('item_a', 'H'), ('item_b', 'B'), ('item_c', 'B'), ('item_d', 'B'), ('item_e', 'B'), # Note, there is no item 'e' in the list of arguments ('function_data', 'I', adder, ['item_a', 'item_b', 'item_c', 'item_d']), ]) # Test getting the correct result test_data = { 'item_a': 2, 'item_b': 5, 'item_c': 7, 'item_d': 4, 'item_e': 6, } made = AdderMessage.make(test_data) assert made.item_a == 2 assert made.item_b == 5 assert made.function_data == 2 + 5 + 7 + 4 # Check packing and unpacking packed = AdderMessage.pack(test_data) assert packed == b'\x02\x00\x05\x07\x04\x06\x12\x00\x00\x00' assert packed == made.pack() unpacked = AdderMessage.unpack(packed) assert made == unpacked # Test with correct result test_data_2 = { 'item_a': 2, 'item_b': 5, 'item_c': 7, 'item_d': 4, 'item_e': 6, 'function_data': 2 + 5 + 7 + 4, } made = AdderMessage.make(test_data_2) assert made.item_a == 2 assert made.item_b == 5 assert made.function_data == 2 + 5 + 7 + 4 # Test with incorrect result test_data_2 = { 'item_a': 2, 'item_b': 5, 'item_c': 7, 'item_d': 4, 'item_e': 6, 'function_data': -1, } with pytest.raises(ValueError): made = AdderMessage.make(test_data_2)
def test_verifying_unpack(self): def adder(*args): return sum(args) AdderMessage = Message('AdderMessage', [ ('item_a', 'H'), ('item_b', 'B'), ('item_c', 'B'), ('item_d', 'B'), ('item_e', 'B'), # Note, there is no item 'e' in the list of arguments ('function_data', 'I', adder, ['item_a', 'item_b', 'item_c', 'item_d']), ]) # Test getting the correct result test_data = { 'item_a': 2, 'item_b': 5, 'item_c': 7, 'item_d': 4, 'item_e': 6, } made = AdderMessage.make(test_data) assert made.item_a == 2 assert made.item_b == 5 assert made.function_data == 2 + 5 + 7 + 4 # Check packing and unpacking packed = AdderMessage.pack(test_data) assert packed == b'\x02\x00\x05\x07\x04\x06\x12\x00\x00\x00' assert packed == made.pack() unpacked = AdderMessage.unpack(packed) assert made == unpacked # Now we modify the data we are going to unpack, and we should get an error modified_packed = b'\x02\x00\x05\x07\x04\x06\x11\x11\x11\x11' with pytest.raises(ValueError): unpacked = AdderMessage.unpack(modified_packed) AdderMessageFalse = Message('AdderMessageFalse', [ ('item_a', 'H'), ('item_b', 'B'), ('item_c', 'B'), ('item_d', 'B'), ('item_e', 'B'), # Note, there is no item 'e' in the list of arguments ('function_data', 'I', adder, ['item_a', 'item_b', 'item_c', 'item_d'], False), ]) # Test getting the correct result test_data = { 'item_a': 2, 'item_b': 5, 'item_c': 7, 'item_d': 4, 'item_e': 6, } made = AdderMessageFalse.make(test_data) assert made.item_a == 2 assert made.item_b == 5 assert made.function_data == 2 + 5 + 7 + 4 # Check packing and unpacking packed = AdderMessageFalse.pack(test_data) assert packed == b'\x02\x00\x05\x07\x04\x06\x12\x00\x00\x00' assert packed == made.pack() unpacked = AdderMessageFalse.unpack(packed) assert made == unpacked # Now we modify the data we are going to unpack, and we should get an error modified_packed = b'\x02\x00\x05\x07\x04\x06\x11\x11\x11\x11' # This time it won't fail because we set False for this message unpacked = AdderMessageFalse.unpack(modified_packed) assert unpacked.item_a == 2