def test_byte_length_no_data(self): 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'), ]) test_data_no_data = { 'length_in_objects': 0, 'vardata': [], 'length_in_bytes': 0, 'bytesdata': [], } made = TestStruct.make(test_data_no_data) assert made.length_in_objects == 0 assert made.vardata == [] assert made.length_in_bytes == 0 assert made.bytesdata == [] packed = TestStruct.pack(test_data_no_data) assert packed == \ struct.pack('H', 0) + \ struct.pack('H', 0)
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_pack_big_endian(self): """Test pack 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 packed_msg = test_msg.pack(**self.testvalues[idx]) self.assertEqual(self.testbytes['big'][idx], packed_msg)
def test_single_element_2(self): TestStruct = Message('TestStruct', [ ('length_in_objects', 'H', 'vardata'), ('vardata', self.VarTest, 'length_in_objects'), ]) CRCedMessage = Message('CRCedMessage', [ ('data', TestStruct), ('function_data', 'I', crc32, [b'data']), ]) test_data = { 'data': { 'length_in_objects': 2, 'vardata': [ {'x': 1, 'y': 2}, {'x': 3, 'y': 4}, ], }, } made = CRCedMessage.make(test_data) # assert len(made) == 5 assert len(made.data.vardata) == 2 assert made.data.vardata[0].x == 1 assert made.data.vardata[0].y == 2 assert made.function_data == crc32(TestStruct.pack(test_data['data']))
def test_one_element(self): def crc32_wrapper(*args): return crc32(b''.join(args)) CompareMessage = Message('CompareMessage', [ ('length_in_objects', 'H', 'vardata'), ('vardata', self.VarTest, 'length_in_objects'), ]) CRCedMessage = Message('TestStruct', [ ('length_in_objects', 'H', 'vardata'), ('vardata', self.VarTest, 'length_in_objects'), ('function_data', 'I', crc32_wrapper, [b'length_in_objects', b'vardata']), ]) test_data = { 'length_in_objects': 2, 'vardata': [ {'x': 1, 'y': 2}, {'x': 3, 'y': 4}, ], } made = CRCedMessage.make(test_data) # assert len(made) == 5 assert len(made.vardata) == 2 assert made.vardata[0].x == 1 assert made.vardata[0].y == 2 assert made.function_data == crc32(CompareMessage.pack(test_data))
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_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_not_all_fixed_data(self): num_repeats = 5 TestStruct = Message('TestStruct', [ ('length', 'H', 'vardata'), ('vardata', self.VarTest, 'length'), ('repeated_data', self.Repeated, num_repeats), ]) test_data = { 'length': 1, 'vardata': [ {'x': 255, 'y': 127}, ], 'repeated_data': [ {'x': 6, 'z': 12}, {'x': 1, 'z': 26}, {'x': 5, 'z': 10}, ], } made = TestStruct.make(test_data) assert made.length == 1 assert len(made.vardata) == 1 assert len(made.repeated_data) == 3 packed = TestStruct.pack(test_data) assert packed == struct.pack('H', 1) + \ struct.pack('BB', 255, 127) + \ (struct.pack('B', 6) + struct.pack('H', 12)) + \ (struct.pack('B', 1) + struct.pack('H', 26)) + \ (struct.pack('B', 5) + struct.pack('H', 10)) + \ (struct.pack('B', 0) + struct.pack('H', 0)) * 2
def test_some_data(self): num_repeats = 3 TestStruct = Message('TestStruct', [ ('length', 'H', 'vardata'), ('vardata', self.VarTest, 'length'), ('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 len(made.vardata) == 2 assert len(made.repeated_data) == 3 packed = TestStruct.pack(test_data) assert packed == struct.pack('H', 2) + \ struct.pack('BB', 1, 2) + \ struct.pack('BB', 3, 4) + \ (struct.pack('B', 7) + struct.pack('H', 13)) + \ (struct.pack('B', 2) + struct.pack('H', 27)) + \ (struct.pack('B', 6) + struct.pack('H', 11))
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_byte_length_more_data(self): 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'), ]) test_data_no_data = { 'length_in_objects': 1, 'vardata': [ {'x': 255, 'y': 127}, ], 'length_in_bytes': 10, 'bytesdata': [ {'x': 254, 'y': 126}, {'x': 25, 'y': 16}, {'x': 24, 'y': 26}, {'x': 54, 'y': 17}, {'x': 25, 'y': 12}, ], } made = TestStruct.make(test_data_no_data) assert made.length_in_objects == 1 assert made.vardata == [ self.VarTest.make( {'x': 255, 'y': 127} )] assert made.length_in_bytes == 10 assert made.bytesdata == [ self.VarTest.make( {'x': 254, 'y': 126} ), self.VarTest.make( {'x': 25, 'y': 16}, ), self.VarTest.make( {'x': 24, 'y': 26}, ), self.VarTest.make( {'x': 54, 'y': 17}, ), self.VarTest.make( {'x': 25, 'y': 12}, ), ] packed = TestStruct.pack(test_data_no_data) assert packed == \ 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)
def test_alignment(self): """Test field formats that are valid ElementString elements.""" TestStruct = Message("TestStruct", [("a", "c"), ("b", "2c")]) # single character # 2 char string test_data = {"a": "a", "b": "no"} TestStruct.update(alignment=4) packed = TestStruct.pack(test_data) assert packed == b"a\x00\x00\x00no\x00\x00"
def test_self_pack(self): my_message = Message('MyMessage', self.teststruct) my_instance_1 = my_message.make(self.testvalues[0]) my_instance_2 = my_message.make(self.testvalues[1]) my_bytes_1 = my_message.pack(self.testvalues[0]) my_bytes_2 = my_message.pack(self.testvalues[1]) assert my_instance_1.pack() == my_bytes_1 assert my_instance_2.pack() == my_bytes_2
def test_some_data(self): num_repeats = 3 TestStruct = Message('TestStruct', [ ('length', 'H', 'vardata'), ('vardata', self.VarTest, 'length'), ('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 len(made.vardata) == 2 assert len(made.repeated_data) == 3 packed = TestStruct.pack(test_data) assert packed == struct.pack('H', 2) + \ struct.pack('BB', 1, 2) + \ struct.pack('BB', 3, 4) + \ (struct.pack('B', 7) + struct.pack('H', 13)) + \ (struct.pack('B', 2) + struct.pack('H', 27)) + \ (struct.pack('B', 6) + struct.pack('H', 11))
def test_self_pack(self): my_message = Message('MyMessage', self.teststruct) my_instance_1 = my_message.make(self.testvalues[0]) my_instance_2 = my_message.make(self.testvalues[1]) my_bytes_1 = my_message.pack(self.testvalues[0]) my_bytes_2 = my_message.pack(self.testvalues[1]) assert my_instance_1.pack() == my_bytes_1 assert my_instance_2.pack() == my_bytes_2 assert my_message._name == 'MyMessage' assert my_instance_1._name == 'MyMessage'
def test_not_all_fixed_data(self): num_repeats = 5 TestStruct = Message('TestStruct', [ ('length', 'H', 'vardata'), ('vardata', self.VarTest, 'length'), ('repeated_data', self.Repeated, num_repeats), ]) test_data = { 'length': 1, 'vardata': [ { 'x': 255, 'y': 127 }, ], 'repeated_data': [ { 'x': 6, 'z': 12 }, { 'x': 1, 'z': 26 }, { 'x': 5, 'z': 10 }, ], } made = TestStruct.make(test_data) assert made.length == 1 assert len(made.vardata) == 1 assert len(made.repeated_data) == 3 packed = TestStruct.pack(test_data) assert packed == struct.pack('H', 1) + \ struct.pack('BB', 255, 127) + \ (struct.pack('B', 6) + struct.pack('H', 12)) + \ (struct.pack('B', 1) + struct.pack('H', 26)) + \ (struct.pack('B', 5) + struct.pack('H', 10)) + \ (struct.pack('B', 0) + struct.pack('H', 0)) * 2
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_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_no_data(self): num_repeats = 4 TestStruct = Message('TestStruct', [ ('length', 'H', 'vardata'), ('vardata', self.VarTest, 'length'), ('repeated_data', self.Repeated, num_repeats), ]) test_data_no_data = { 'length': 0, 'vardata': [], 'repeated_data': [], } made = TestStruct.make(test_data_no_data) assert made.length == 0 assert made.vardata == [] assert made.repeated_data == [] packed = TestStruct.pack(test_data_no_data) assert packed == struct.pack( 'H', 0) + (struct.pack('B', 0) + struct.pack('H', 0)) * num_repeats
def test_no_data(self): num_repeats = 4 TestStruct = Message('TestStruct', [ ('length', 'H', 'vardata'), ('vardata', self.VarTest, 'length'), ('repeated_data', self.Repeated, num_repeats), ]) test_data_no_data = { 'length': 0, 'vardata': [], 'repeated_data': [ ], } made = TestStruct.make(test_data_no_data) assert made.length == 0 assert made.vardata == [] assert made.repeated_data == [] packed = TestStruct.pack(test_data_no_data) assert packed == struct.pack('H', 0) + (struct.pack('B', 0) + struct.pack('H', 0)) * num_repeats
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_byte_length_some_data(self): 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'), ]) test_data_no_data = { 'length_in_objects': 1, 'vardata': [ { 'x': 255, 'y': 127 }, ], 'length_in_bytes': 2, 'bytesdata': [ { 'x': 254, 'y': 126 }, ], } made = TestStruct.make(test_data_no_data) assert made.length_in_objects == 1 assert made.vardata == [self.VarTest.make({'x': 255, 'y': 127})] assert made.length_in_bytes == 2 assert made.bytesdata == [self.VarTest.make({'x': 254, 'y': 126})] packed = TestStruct.pack(test_data_no_data) assert packed == \ struct.pack('H', 1) + \ struct.pack('BB', 255, 127) + \ struct.pack('H', 2) + \ struct.pack('BB', 254, 126)
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