def test_read_write_arrayint32(self): arr = [] lsd_pos = (1234, 0) record = HcrRecord(HcrRecord.VALTYPE_ARRAY_INT32, arr, 12, 43, 5) rec_data = hex_to_bindata( "0C000000 2B000000 00000400 0500 0000 D2040000") lsd_data = "" self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data) arr = [1234] lsd_pos = (1234, 4) record = HcrRecord(HcrRecord.VALTYPE_ARRAY_INT32, arr, 12, 43, 5) rec_data = hex_to_bindata( "0C000000 2B000000 00000400 0500 0400 D2040000") lsd_data = hex_to_bindata("D2040000") self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data) arr = [-2**31, 0, 1234567890, 2**31 - 1] lsd_pos = (1234, 4 * 3) record = HcrRecord(HcrRecord.VALTYPE_ARRAY_INT32, arr, 12, 43, 5) rec_data = hex_to_bindata( "0C000000 2B000000 00000400 0500 0C00 D2040000") lsd_data = hex_to_bindata("00000080 00000000 D2029649 FFFFFF7F") self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data)
def test_read_write_arrayuint32(self): arr = [] lsd_pos = (1234, 0) record = HcrRecord(HcrRecord.VALTYPE_ARRAY_UINT32, arr, 12, 43, 5) rec_data = hex_to_bindata( "0C000000 2B000000 00000800 0500 0000 D2040000") lsd_data = "" self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data) arr = [1234] lsd_pos = (1234, 4) record = HcrRecord(HcrRecord.VALTYPE_ARRAY_UINT32, arr, 12, 43, 5) rec_data = hex_to_bindata( "0C000000 2B000000 00000800 0500 0400 D2040000") lsd_data = hex_to_bindata("D2040000") self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data) arr = [0, 3123456789, 2**32 - 1] lsd_pos = (1234, 4 * 3) record = HcrRecord(HcrRecord.VALTYPE_ARRAY_UINT32, arr, 12, 43, 5) rec_data = hex_to_bindata( "0C000000 2B000000 00000800 0500 0C00 D2040000") lsd_data = hex_to_bindata("00000000 152B2CBA FFFFFFFF") self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data)
def test_read_write_text8(self): string = '' lsd_pos = (1234, 0) record = HcrRecord(HcrRecord.VALTYPE_TEXT8, string, 12, 43, 5) rec_data = hex_to_bindata( "0C000000 2B000000 00000200 0500 0000 D2040000") lsd_data = "" self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data) string = 'Hello world!!' lsd_pos = (1234, 13) record = HcrRecord(HcrRecord.VALTYPE_TEXT8, string, 12, 43, 5) rec_data = hex_to_bindata( "0C000000 2B000000 00000200 0500 0D00 D2040000") lsd_data = "Hello world!!" self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data) string = u'Cost 100€' lsd_pos = (1234, 11) record = HcrRecord(HcrRecord.VALTYPE_TEXT8, string, 12, 43, 5) rec_data = hex_to_bindata( "0C000000 2B000000 00000200 0500 0B00 D2040000") lsd_data = "Cost 100" + hex_to_bindata("E2 82 AC") self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data)
def test_read_write_bool(self): r = HcrRecord(HcrRecord.VALTYPE_BOOL, False, 12, 43, 5) d = hex_to_bindata("0C000000 2B000000 08000000 0500 0000 00000000") self._run_test_read_write_record_no_lsd(r, d) r = HcrRecord(HcrRecord.VALTYPE_BOOL, True, 0xDEADBEEF, 0xBAADF00D, 0xCAFE) d = hex_to_bindata("EFBEADDE 0DF0ADBA 08000000 FECA 0000 01000000") self._run_test_read_write_record_no_lsd(r, d)
def test_read_write_uint8(self): r = HcrRecord(HcrRecord.VALTYPE_UINT8, 0, 12, 43, 5) d = hex_to_bindata("0C000000 2B000000 40000000 0500 0000 00000000") self._run_test_read_write_record_no_lsd(r, d) r = HcrRecord(HcrRecord.VALTYPE_UINT8, 234, 12, 43, 5) d = hex_to_bindata("0C000000 2B000000 40000000 0500 0000 EA000000") self._run_test_read_write_record_no_lsd(r, d) r = HcrRecord(HcrRecord.VALTYPE_UINT8, 2**8 - 1, 12, 43, 5) d = hex_to_bindata("0C000000 2B000000 40000000 0500 0000 FF000000") self._run_test_read_write_record_no_lsd(r, d)
def test_read_write_int16(self): r = HcrRecord(HcrRecord.VALTYPE_INT16, -2**15, 12, 43, 5) d = hex_to_bindata("0C000000 2B000000 02000000 0500 0000 0080FFFF") self._run_test_read_write_record_no_lsd(r, d) r = HcrRecord(HcrRecord.VALTYPE_INT16, 12345, 12, 43, 5) d = hex_to_bindata("0C000000 2B000000 02000000 0500 0000 39300000") self._run_test_read_write_record_no_lsd(r, d) r = HcrRecord(HcrRecord.VALTYPE_INT16, 2**15 - 1, 12, 43, 5) d = hex_to_bindata("0C000000 2B000000 02000000 0500 0000 FF7F0000") self._run_test_read_write_record_no_lsd(r, d)
def test_read_write_uint16(self): r = HcrRecord(HcrRecord.VALTYPE_UINT16, 0, 12, 43, 5) d = hex_to_bindata("0C000000 2B000000 20000000 0500 0000 00000000") self._run_test_read_write_record_no_lsd(r, d) r = HcrRecord(HcrRecord.VALTYPE_UINT16, 43215, 12, 43, 5) d = hex_to_bindata("0C000000 2B000000 20000000 0500 0000 CFA80000") self._run_test_read_write_record_no_lsd(r, d) r = HcrRecord(HcrRecord.VALTYPE_UINT16, 2**16 - 1, 12, 43, 5) d = hex_to_bindata("0C000000 2B000000 20000000 0500 0000 FFFF0000") self._run_test_read_write_record_no_lsd(r, d)
def test_read_write_int32(self): r = HcrRecord(HcrRecord.VALTYPE_INT32, -2**31, 12, 43, 5) d = hex_to_bindata("0C000000 2B000000 01000000 0500 0000 00000080") self._run_test_read_write_record_no_lsd(r, d) r = HcrRecord(HcrRecord.VALTYPE_INT32, 1234567890, 12, 43, 5) d = hex_to_bindata("0C000000 2B000000 01000000 0500 0000 D2029649") self._run_test_read_write_record_no_lsd(r, d) r = HcrRecord(HcrRecord.VALTYPE_INT32, 2**31 - 1, 12, 43, 5) d = hex_to_bindata("0C000000 2B000000 01000000 0500 0000 FFFFFF7F") self._run_test_read_write_record_no_lsd(r, d)
def test_read_write_uint32(self): r = HcrRecord(HcrRecord.VALTYPE_UINT32, 0, 12, 43, 5) d = hex_to_bindata("0C000000 2B000000 10000000 0500 0000 00000000") self._run_test_read_write_record_no_lsd(r, d) r = HcrRecord(HcrRecord.VALTYPE_UINT32, 3123456789, 12, 43, 5) d = hex_to_bindata("0C000000 2B000000 10000000 0500 0000 152B2CBA") self._run_test_read_write_record_no_lsd(r, d) r = HcrRecord(HcrRecord.VALTYPE_UINT32, 2**32 - 1, 12, 43, 5) d = hex_to_bindata("0C000000 2B000000 10000000 0500 0000 FFFFFFFF") self._run_test_read_write_record_no_lsd(r, d)
def test_read_write_linaddr(self): r = HcrRecord(HcrRecord.VALTYPE_LIN_ADDR, 0, 12, 43, 5) d = hex_to_bindata("0C000000 2B000000 00010000 0500 0000 00000000") self._run_test_read_write_record_no_lsd(r, d) r = HcrRecord(HcrRecord.VALTYPE_LIN_ADDR, 3123456789, 12, 43, 5) d = hex_to_bindata("0C000000 2B000000 00010000 0500 0000 152B2CBA") self._run_test_read_write_record_no_lsd(r, d) r = HcrRecord(HcrRecord.VALTYPE_LIN_ADDR, 2**32 - 1, 12, 43, 5) d = hex_to_bindata("0C000000 2B000000 00010000 0500 0000 FFFFFFFF") self._run_test_read_write_record_no_lsd(r, d)
def test_read_write_int8(self): r = HcrRecord(HcrRecord.VALTYPE_INT8, -2**7, 12, 43, 5) d = hex_to_bindata("0C000000 2B000000 04000000 0500 0000 80FFFFFF") self._run_test_read_write_record_no_lsd(r, d) r = HcrRecord(HcrRecord.VALTYPE_INT8, 122, 12, 43, 5) d = hex_to_bindata("0C000000 2B000000 04000000 0500 0000 7A000000") self._run_test_read_write_record_no_lsd(r, d) r = HcrRecord(HcrRecord.VALTYPE_INT8, 2**7 - 1, 12, 43, 5) d = hex_to_bindata("0C000000 2B000000 04000000 0500 0000 7F000000") self._run_test_read_write_record_no_lsd(r, d)
def test_read_write_repository_without_lsd(self): records = [ HcrRecord(HcrRecord.VALTYPE_BOOL, True, 1, 1, 0), HcrRecord(HcrRecord.VALTYPE_INT8, -123, 1, 2, 0), HcrRecord(HcrRecord.VALTYPE_UINT8, 204, 1, 3, 0), HcrRecord(HcrRecord.VALTYPE_INT16, -13423, 2, 1, 0), HcrRecord(HcrRecord.VALTYPE_UINT16, 54321, 2, 2, 0), HcrRecord(HcrRecord.VALTYPE_INT32, -1000000000, 2, 3, 0), HcrRecord(HcrRecord.VALTYPE_UINT32, 4000000000, 3, 1, 0), HcrRecord(HcrRecord.VALTYPE_LIN_ADDR, 0xAABBCCDD, 3, 2, 0), ] # Shuffle the records to make sure that they are sorted properly when writing random.shuffle(records) repo = HcrRepository(records, version=2, flags=3) h = hex_to_bindata data = [ # Header h("48435266 0200 0300 08000000 C0000000"), h("00000000 000000000000000000000000"), # Record section h("01000000 01000000 08000000 0000 0000 01000000"), # bool h("01000000 02000000 04000000 0000 0000 85FFFFFF"), # int8 h("01000000 03000000 40000000 0000 0000 CC000000"), # uint8 h("02000000 01000000 02000000 0000 0000 91CBFFFF"), # int16 h("02000000 02000000 20000000 0000 0000 31D40000"), # uint16 h("02000000 03000000 01000000 0000 0000 003665C4"), # int32 h("03000000 01000000 10000000 0000 0000 00286BEE"), # uint32 h("03000000 02000000 00010000 0000 0000 DDCCBBAA"), # linaddr ] repo_data = ''.join(data) self._run_test_read_write_repository(repo, repo_data, 'repo_without_lsd')
def test_write_repo_with_duplicate_record(self): records = [ HcrRecord(HcrRecord.VALTYPE_INT8, -123, 1, 1, 0), HcrRecord(HcrRecord.VALTYPE_INT8, 124, 2, 1, 0), HcrRecord(HcrRecord.VALTYPE_INT8, 66, 3, 1, 0), HcrRecord(HcrRecord.VALTYPE_INT8, -72, 1, 1, 0), HcrRecord(HcrRecord.VALTYPE_INT8, 171, 1, 2, 0), ] repo = HcrRepository(records, version=2, flags=3) try: self.writer.get_repository_bindata(repo) except hcr_exceptions.DuplicateRecordError: pass
def test_create_record_with_valid_type(self): r = HcrRecord(HcrRecord.VALTYPE_INT16, 1234, 1, 2, 3) self.assertEquals(r.type, HcrRecord.VALTYPE_INT16) self.assertEquals(r.value, 1234) self.assertEquals(r.category_id, 1) self.assertEquals(r.element_id, 2) self.assertEquals(r.flags, 3)
def test(record_type, value): try: record = HcrRecord(record_type, value, 0, 0, 0) self.writer.get_record_lsd_bindata(record) self.fail("Expected exception not raised!") except hcr_exceptions.TooLargeLsdDataError: pass
def _run_test_write_record_with_invalid_value(self, record_type, record_value): try: record = HcrRecord(record_type, record_value, 0, 0, 0) self.writer.get_record_bindata(record, (0, 0)) self.fail("Expected exception not thrown!") except hcr_exceptions.ValueNotInRangeError, e: pass
def test_record_sorting_by_setting_id(self): records = [ HcrRecord(HcrRecord.VALTYPE_INT8, 10, 3, 1, 0), HcrRecord(HcrRecord.VALTYPE_INT8, 10, 1, 1, 0), HcrRecord(HcrRecord.VALTYPE_INT8, 10, 2, 2, 0), HcrRecord(HcrRecord.VALTYPE_INT8, 10, 2, 1, 0), HcrRecord(HcrRecord.VALTYPE_INT8, 10, 1, 2, 0), ] expected = [ HcrRecord(HcrRecord.VALTYPE_INT8, 10, 1, 1, 0), HcrRecord(HcrRecord.VALTYPE_INT8, 10, 1, 2, 0), HcrRecord(HcrRecord.VALTYPE_INT8, 10, 2, 1, 0), HcrRecord(HcrRecord.VALTYPE_INT8, 10, 2, 2, 0), HcrRecord(HcrRecord.VALTYPE_INT8, 10, 3, 1, 0), ] self.assertEquals( sorted(records, key=self.writer.get_record_setting_id), expected)
def get_hcr_records(self): """ Return a list of HcrRecord objects created based on this category's settings. """ result = [] for setting in self.settings: record = HcrRecord(setting.type, setting.value, self.category_uid, setting.id) flag = setting.flag if flag: record.flags = 0 if flag.Uninitialised == '1': record.flags |= HcrRecord.FLAG_UNINITIALIZED if flag.Modifiable == '1': record.flags |= HcrRecord.FLAG_MODIFIABLE if flag.Persistent == '1': record.flags |= HcrRecord.FLAG_PERSISTENT result.append(record) return result
def test_read_write_bindata(self): data = '' lsd_pos = (1234, 0) record = HcrRecord(HcrRecord.VALTYPE_BIN_DATA, data, 12, 43, 5) rec_data = hex_to_bindata( "0C000000 2B000000 00000100 0500 0000 D2040000") lsd_data = "" self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data) data = hex_to_bindata( '00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF') lsd_pos = (1234, 16) record = HcrRecord(HcrRecord.VALTYPE_BIN_DATA, data, 12, 43, 5) rec_data = hex_to_bindata( "0C000000 2B000000 00000100 0500 1000 D2040000") lsd_data = hex_to_bindata( '00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF') self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data)
def parse_record_from_bindata(self, data): """ @return: Tuple: (record, lsd_pos) where record = HcrRecord object constructed from the given binary data. lsd_pos = The position of the record's data in the LSD section in the form of a tuple (offset, size), or None if the record does not have any data in the LSD section. """ if len(data) != 20: raise HcrReaderError("Invalid record length: %d, expected 20" % len(data)) result = unpack("<IIIHH", data[:16]) category_id = result[0] element_id = result[1] value_type = result[2] flag = result[3] lsd_len = result[4] for key, val in VALUE_TYPE_MAP.iteritems(): if val == value_type: value_type = key break if value_type not in VALUE_TYPE_MAP: raise InvalidRecordValueTypeError("Invalid value type:%X" % value_type) value = None lsd_pos = None if value_type in VALUE_TYPES_WITH_LSD: lsd_offset = unpack("<I", data[16:])[0] lsd_pos = (lsd_offset, lsd_len) else: if value_type in VALUE_TYPES_UNSIGNED_INT: format = "<I" elif value_type == HcrRecord.VALTYPE_INT8: format = "<bxxx" elif value_type == HcrRecord.VALTYPE_INT16: format = "<hxx" else: format = "<i" value = unpack(format, data[16:])[0] if value_type == HcrRecord.VALTYPE_BOOL: value = bool(value) record = HcrRecord(value_type, value, category_id, element_id, flag) return record, lsd_pos
def test_read_write_uint64(self): lsd_pos = (1234, 8) record = HcrRecord(HcrRecord.VALTYPE_UINT64, 0, 12, 43, 5) rec_data = hex_to_bindata( "0C000000 2B000000 00000002 0500 0800 D2040000") lsd_data = hex_to_bindata("0000 0000 0000 0000") self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data) record = HcrRecord(HcrRecord.VALTYPE_UINT64, 10746170304040729876, 12, 43, 5) rec_data = hex_to_bindata( "0C000000 2B000000 00000002 0500 0800 D2040000") lsd_data = hex_to_bindata("14FD 32B4 F410 2295") self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data) record = HcrRecord(HcrRecord.VALTYPE_UINT64, 2**64 - 1, 12, 43, 5) rec_data = hex_to_bindata( "0C000000 2B000000 00000002 0500 0800 D2040000") lsd_data = hex_to_bindata("FFFF FFFF FFFF FFFF") self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data)
def test_read_write_int64(self): lsd_pos = (1234, 8) record = HcrRecord(HcrRecord.VALTYPE_INT64, -2**63, 12, 43, 5) rec_data = hex_to_bindata( "0C000000 2B000000 00000001 0500 0800 D2040000") lsd_data = hex_to_bindata("0000 0000 0000 0080") self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data) record = HcrRecord(HcrRecord.VALTYPE_INT64, 9211026413402420220, 12, 43, 5) rec_data = hex_to_bindata( "0C000000 2B000000 00000001 0500 0800 D2040000") lsd_data = hex_to_bindata("FC73 978B B823 D47F") self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data) record = HcrRecord(HcrRecord.VALTYPE_INT64, 2**63 - 1, 12, 43, 5) rec_data = hex_to_bindata( "0C000000 2B000000 00000001 0500 0800 D2040000") lsd_data = hex_to_bindata("FFFF FFFF FFFF FF7F") self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data)
def test_get_duplicate_records(self): r = HcrRepository([ HcrRecord(HcrRecord.VALTYPE_INT8, 162, 1, 1, 93), HcrRecord(HcrRecord.VALTYPE_INT8, 172, 1, 2, 41), HcrRecord(HcrRecord.VALTYPE_TEXT8, 182, 1, 3, 40), HcrRecord(HcrRecord.VALTYPE_INT8, 192, 2, 1, 41), HcrRecord(HcrRecord.VALTYPE_UINT32, 202, 2, 2, 40) ], version=1, flags=1) self.assertEquals(r.get_duplicate_record_ids(), []) r.records.append(HcrRecord(HcrRecord.VALTYPE_UINT16, 212, 1, 1, 142)) self.assertEquals(r.get_duplicate_record_ids(), [(1, 1)]) r.records.append(HcrRecord(HcrRecord.VALTYPE_UINT64, 105, 1, 1, 142)) self.assertEquals(r.get_duplicate_record_ids(), [(1, 1)]) r.records.append(HcrRecord(HcrRecord.VALTYPE_UINT64, 222, 2, 2, 32)) self.assertEquals(r.get_duplicate_record_ids(), [(1, 1), (2, 2)]) r.records.append(HcrRecord(HcrRecord.VALTYPE_TEXT8, 232, 3, 1, 32)) self.assertEquals(r.get_duplicate_record_ids(), [(1, 1), (2, 2)])
def test_read_write_repository_with_lsd(self): records = [ HcrRecord(HcrRecord.VALTYPE_INT64, 9211026413402420220, 1, 1, 0), HcrRecord(HcrRecord.VALTYPE_UINT64, 10746170304040729876, 1, 2, 0), HcrRecord(HcrRecord.VALTYPE_TEXT8, u'Cost 100€', 1, 3, 0), HcrRecord(HcrRecord.VALTYPE_BIN_DATA, 'test test', 2, 1, 0), HcrRecord(HcrRecord.VALTYPE_ARRAY_INT32, [-2**31, 0, 2**31 - 1], 2, 2, 0), HcrRecord(HcrRecord.VALTYPE_ARRAY_UINT32, [0, 100000, 2**32 - 1], 2, 3, 0), ] # Shuffle the records to make sure that they are sorted properly when writing random.shuffle(records) repo = HcrRepository(records, version=2, flags=3) # Record section size: 6 * 20 = 120 # LSD offset: 32 + 120 = 152 # LSD size: 8 + 8 + 12 + 12 + 12 + 12 = 64 h = hex_to_bindata data = [ # Header h("48435266 0200 0300 06000000 98000000"), h("40000000 000000000000000000000000"), # Record section h("01000000 01000000 00000001 0000 0800 00000000" ), # int64, lsd pos = (0, 8) h("01000000 02000000 00000002 0000 0800 08000000" ), # uint64, lsd pos = (8, 8) h("01000000 03000000 00000200 0000 0B00 10000000" ), # text8, lsd pos = (8 + 8, 11) h("02000000 01000000 00000100 0000 0900 1C000000" ), # bindata, lsd pos = (8 + 8 + 12, 9) h("02000000 02000000 00000400 0000 0C00 28000000" ), # arrayint32, lsd pos = (8 + 8 + 12 + 12, 12) h("02000000 03000000 00000800 0000 0C00 34000000" ), # arrayuint32, lsd pos = (8 + 8 + 12 + 12 + 12, 12) # LSD section h("FC73 978B B823 D47F"), # 8 bytes h("14FD 32B4 F410 2295"), # 8 bytes "Cost 100" + h("E2 82 AC 00"), # 12 bytes "test test" + h("00 00 00"), # 12 bytes h("00000080 00000000 FFFFFF7F"), # 12 bytes h("00000000 A0860100 FFFFFFFF"), # 12 bytes ] repo_data = ''.join(data) self._run_test_read_write_repository(repo, repo_data, 'repo_with_lsd')
def test_repository_equality(self): self.assertTrue(HcrRepository([], 1, 1) == HcrRepository([], 1, 1)) self.assertFalse(HcrRepository([], 1, 1) == HcrRepository([], 2, 1)) self.assertFalse(HcrRepository([], 1, 1) == HcrRepository([], 1, 2)) self.assertTrue(HcrRepository([], 1, 1) != HcrRepository([], 1, 2)) # Records the same, but in different order r1 = HcrRepository([ HcrRecord(HcrRecord.VALTYPE_INT8, 10, 20, 30, 93), HcrRecord(HcrRecord.VALTYPE_INT8, 25, 20, 62, 41), HcrRecord(HcrRecord.VALTYPE_BIN_DATA, 10, 20, 31, 40), HcrRecord(HcrRecord.VALTYPE_INT8, 10, 20, 83, 41), HcrRecord(HcrRecord.VALTYPE_UINT64, 10, 21, 30, 40) ], version=1, flags=1) r2 = HcrRepository([ HcrRecord(HcrRecord.VALTYPE_UINT64, 10, 21, 30, 40), HcrRecord(HcrRecord.VALTYPE_INT8, 10, 20, 30, 93), HcrRecord(HcrRecord.VALTYPE_INT8, 10, 20, 83, 41), HcrRecord(HcrRecord.VALTYPE_BIN_DATA, 10, 20, 31, 40), HcrRecord(HcrRecord.VALTYPE_INT8, 25, 20, 62, 41), ], version=1, flags=1) self.assertTrue(r1 == r2) self.assertEquals(repr(r1), repr(r2)) r1.version = 2 self.assertFalse(r1 == r2) r1.version = 1 self.assertTrue(r1 == r2) r1.flags = 3 self.assertFalse(r1 == r2) r1.flags = 1 self.assertTrue(r1 == r2) r1.records.append(HcrRecord(HcrRecord.VALTYPE_LIN_ADDR, 1, 2, 3, 4)) self.assertFalse(r1 == r2)
def test_read_write_repository_with_all_record_types(self): records = [ HcrRecord(HcrRecord.VALTYPE_BOOL, True, 1, 1, 0), HcrRecord(HcrRecord.VALTYPE_INT8, -123, 1, 2, 0), HcrRecord(HcrRecord.VALTYPE_UINT8, 204, 1, 3, 0), HcrRecord(HcrRecord.VALTYPE_INT16, -13423, 2, 1, 0), HcrRecord(HcrRecord.VALTYPE_UINT16, 54321, 2, 2, 0), HcrRecord(HcrRecord.VALTYPE_INT32, -1000000000, 2, 3, 0), HcrRecord(HcrRecord.VALTYPE_UINT32, 4000000000, 3, 1, 0), HcrRecord(HcrRecord.VALTYPE_LIN_ADDR, 0xAABBCCDD, 3, 2, 0), HcrRecord(HcrRecord.VALTYPE_INT64, 9211026413402420220, 4, 1, 0), HcrRecord(HcrRecord.VALTYPE_UINT64, 10746170304040729876, 4, 2, 0), HcrRecord(HcrRecord.VALTYPE_TEXT8, u'Cost 100€', 4, 3, 0), HcrRecord(HcrRecord.VALTYPE_TEXT8, '', 5, 1, 0), HcrRecord(HcrRecord.VALTYPE_BIN_DATA, 'test test', 5, 2, 0), HcrRecord(HcrRecord.VALTYPE_BIN_DATA, '', 5, 3, 0), HcrRecord(HcrRecord.VALTYPE_ARRAY_INT32, [-2**31, 0, 2**31 - 1], 6, 1, 0), HcrRecord(HcrRecord.VALTYPE_ARRAY_INT32, [], 6, 2, 0), HcrRecord(HcrRecord.VALTYPE_ARRAY_UINT32, [0, 100000, 2**32 - 1], 6, 3, 0), HcrRecord(HcrRecord.VALTYPE_ARRAY_UINT32, [], 7, 1, 0), HcrRecord(HcrRecord.VALTYPE_BOOL, False, 8, 1, 0), HcrRecord(HcrRecord.VALTYPE_TEXT8, u'Hello world!!!', 8, 2, 0), HcrRecord(HcrRecord.VALTYPE_TEXT8, u'unpadded', 8, 3, 0), ] # Shuffle the records to make sure that they are sorted properly when writing random.shuffle(records) repo = HcrRepository(records, version=2, flags=3) # Record section size: 21 * 20 = 420 # LSD offset: 32 + 420 = 452 # LSD size: 8 + 8 + 12 + 12 + 12 + 12 + 16 + 8 = 88 h = hex_to_bindata data = [ # Header h("48435266 0200 0300 15000000 C4010000"), h("58000000 000000000000000000000000"), # Record section h("01000000 01000000 08000000 0000 0000 01000000"), # bool h("01000000 02000000 04000000 0000 0000 85FFFFFF"), # int8 h("01000000 03000000 40000000 0000 0000 CC000000"), # uint8 h("02000000 01000000 02000000 0000 0000 91CBFFFF"), # int16 h("02000000 02000000 20000000 0000 0000 31D40000"), # uint16 h("02000000 03000000 01000000 0000 0000 003665C4"), # int32 h("03000000 01000000 10000000 0000 0000 00286BEE"), # uint32 h("03000000 02000000 00010000 0000 0000 DDCCBBAA"), # linaddr h("04000000 01000000 00000001 0000 0800 00000000" ), # int64, lsd pos = (0, 8) h("04000000 02000000 00000002 0000 0800 08000000" ), # uint64, lsd pos = (8, 8) h("04000000 03000000 00000200 0000 0B00 10000000" ), # text8, lsd pos = (8 + 8, 11) h("05000000 01000000 00000200 0000 0000 1C000000" ), # text8, lsd pos = (8 + 8 + 12, 0) h("05000000 02000000 00000100 0000 0900 1C000000" ), # bindata, lsd pos = (8 + 8 + 12, 9) h("05000000 03000000 00000100 0000 0000 28000000" ), # bindata, lsd pos = (8 + 8 + 12 + 12, 0) h("06000000 01000000 00000400 0000 0C00 28000000" ), # arrayint32, lsd pos = (8 + 8 + 12 + 12, 12) h("06000000 02000000 00000400 0000 0000 34000000" ), # arrayint32, lsd pos = (8 + 8 + 12 + 12 + 12, 0) h("06000000 03000000 00000800 0000 0C00 34000000" ), # arrayuint32, lsd pos = (8 + 8 + 12 + 12 + 12, 12) h("07000000 01000000 00000800 0000 0000 40000000" ), # arrayuint32, lsd pos = (8 + 8 + 12 + 12 + 12 + 12, 0) h("08000000 01000000 08000000 0000 0000 00000000"), # bool h("08000000 02000000 00000200 0000 0E00 40000000" ), # text8, lsd pos = (8 + 8 + 12 + 12 + 12 + 12, 14) h("08000000 03000000 00000200 0000 0800 50000000" ), # text8, lsd pos = (8 + 8 + 12 + 12 + 12 + 12 + 16, 8) # LSD section h("FC73 978B B823 D47F"), # 8 bytes h("14FD 32B4 F410 2295"), # 8 bytes "Cost 100" + h("E2 82 AC 00"), # 12 bytes "", "test test" + h("00 00 00"), # 12 bytes "", h("00000080 00000000 FFFFFF7F"), # 12 bytes "", h("00000000 A0860100 FFFFFFFF"), # 12 bytes "", "Hello world!!!" + h("00 00"), # 16 bytes "unpadded", # 8 bytes ] repo_data = ''.join(data) self._run_test_read_write_repository(repo, repo_data, 'repo_with_all_record_types')
def test_create_record_with_invalid_type(self): try: r = HcrRecord('foobar_type', 0, 0, 0, 0) self.fail("Creating a foobar_type record succeeded!") except ValueError: pass
def test_record_equality(self): self.assertTrue( HcrRecord(HcrRecord.VALTYPE_INT8, 10, 20, 30, 93) == HcrRecord( HcrRecord.VALTYPE_INT8, 10, 20, 30, 93)) self.assertFalse( HcrRecord(HcrRecord.VALTYPE_INT8, 10, 20, 30, 93) == HcrRecord( HcrRecord.VALTYPE_INT16, 10, 20, 30, 93)) self.assertFalse( HcrRecord(HcrRecord.VALTYPE_INT8, 10, 20, 30, 93) == HcrRecord( HcrRecord.VALTYPE_INT8, 2, 20, 30, 93)) self.assertFalse( HcrRecord(HcrRecord.VALTYPE_INT8, 10, 20, 30, 93) == HcrRecord( HcrRecord.VALTYPE_INT8, 10, 2, 30, 93)) self.assertFalse( HcrRecord(HcrRecord.VALTYPE_INT8, 10, 20, 30, 93) == HcrRecord( HcrRecord.VALTYPE_INT8, 10, 20, 2, 93)) self.assertFalse( HcrRecord(HcrRecord.VALTYPE_INT8, 10, 20, 30, 93) == HcrRecord( HcrRecord.VALTYPE_INT8, 10, 20, 30, 2)) self.assertTrue( HcrRecord(HcrRecord.VALTYPE_INT8, 10, 20, 30, 93) != HcrRecord( HcrRecord.VALTYPE_INT8, 10, 20, 30, 2)) r1 = HcrRecord(HcrRecord.VALTYPE_INT8, 10, 20, 30, 93) r2 = HcrRecord(HcrRecord.VALTYPE_INT8, 10, 20, 30, 93) self.assertEquals(repr(r1), repr(r2)) r2.value = 12 self.assertNotEquals(repr(r1), repr(r2))
def test_record_sorting(self): records1 = [ HcrRecord(HcrRecord.VALTYPE_INT8, 10, 20, 30, 93), HcrRecord(HcrRecord.VALTYPE_INT8, 25, 20, 62, 41), HcrRecord(HcrRecord.VALTYPE_INT8, 10, 20, 30, 93), HcrRecord(HcrRecord.VALTYPE_BIN_DATA, 10, 20, 31, 40), HcrRecord(HcrRecord.VALTYPE_INT8, 10, 20, 83, 41), HcrRecord(HcrRecord.VALTYPE_UINT64, 10, 21, 30, 40), ] records2 = [ HcrRecord(HcrRecord.VALTYPE_BIN_DATA, 10, 20, 31, 40), HcrRecord(HcrRecord.VALTYPE_INT8, 25, 20, 62, 41), HcrRecord(HcrRecord.VALTYPE_UINT64, 10, 21, 30, 40), HcrRecord(HcrRecord.VALTYPE_INT8, 10, 20, 83, 41), HcrRecord(HcrRecord.VALTYPE_INT8, 10, 20, 30, 93), HcrRecord(HcrRecord.VALTYPE_INT8, 10, 20, 30, 93), ] self.assertNotEquals(records1, records2) self.assertEquals(sorted(records1), sorted(records2))
def generate_expected_data(): records = [] category = 0x10001234 records.append(HcrRecord(HcrRecord.VALTYPE_INT32, 0, category, 0)) records.append(HcrRecord(HcrRecord.VALTYPE_INT32, 0, category, 1)) records.append(HcrRecord(HcrRecord.VALTYPE_INT32, 0, category, 2)) generate_repository("generated/expected/project/hcr.dat", records) # -------------------------------------------------------------- records = [] category = 0x10001234 records.append(HcrRecord(HcrRecord.VALTYPE_INT8, 125, category, 0)) records.append(HcrRecord(HcrRecord.VALTYPE_UINT32, 4000000000, category, 1)) records.append( HcrRecord(HcrRecord.VALTYPE_ARRAY_INT32, [-1, -20, -300, -4000, -50000], category, 2)) records.append( HcrRecord(HcrRecord.VALTYPE_BIN_DATA, hex_to_bindata('00112233 DEADBEEF CAFE 50'), category, 3)) category = 0x20001234 records.append( HcrRecord(HcrRecord.VALTYPE_LIN_ADDR, 0x10203040, category, 0)) records.append( HcrRecord(HcrRecord.VALTYPE_INT64, 1234567890123456789, category, 1)) records.append( HcrRecord(HcrRecord.VALTYPE_ARRAY_UINT32, [1, 20, 300, 4000, 50000], category, 2)) records.append( HcrRecord(HcrRecord.VALTYPE_TEXT8, u'100\u20ac', category, 3)) category = 0x30001234 records.append(HcrRecord(HcrRecord.VALTYPE_BOOL, False, category, 0)) generate_repository("generated/expected/multifile_project/hcr.dat", records)