コード例 #1
0
def generate_repository(file_path, records):
    dir = os.path.dirname(file_path)
    if dir != '' and not os.path.exists(dir):
        os.makedirs(dir)

    writer = HcrWriter()
    repo = HcrRepository(records)
    f = open(file_path, "wb")
    try:
        f.write(writer.get_repository_bindata(repo))
    finally:
        f.close()

    print "Generated '%s' with %d records" % (file_path, len(records))
class TestHcrWriter(unittest.TestCase):
    def setUp(self):
        self.writer = HcrWriter()

    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_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 _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
コード例 #3
0
 def generate(self, context=None):
     """
     Generate the given implementation. 
     @return: 
     """
     outputfile = self.__get_output_filename()
     # For output type 'hcr', write the binary repository file
     if self.output_obj.type == 'hcr':
         self.logger.info("Generating binary repository to '%s'" %
                          outputfile)
         writer = HcrWriter()
         repo = self.output_obj.get_hcr_repository()
         data = writer.get_repository_bindata(repo)
         f = context.create_file(outputfile, mode='wb')
         #f = open(outputfile,'wb')
         try:
             f.write(data)
         finally:
             f.close()
     elif self.output_obj.type == 'header':
         self.logger.info("Generating header file to '%s'" % outputfile)
         writer = HeaderWriter(outputfile, self.output_obj)
         writer.write(context)
     elif self.output_obj.type == None:
         # The HCRML file contains no <output> element, so no output should
         # be generated
         pass
 def setUp(self):
     self.writer = HcrWriter()
     self.reader = HcrReader()
class TestReadWriteHcrRecords(unittest.TestCase):
    def setUp(self):
        self.writer = HcrWriter()
        self.reader = HcrReader()

    def _run_test_read_write_record_no_lsd(self, record, record_bindata):
        self.assertEquals(self.writer.get_record_bindata(record, None),
                          record_bindata)
        self.assertEquals(self.writer.get_record_lsd_bindata(record), None)

        parsed_record, parsed_lsd_pos = self.reader.parse_record_from_bindata(
            record_bindata)
        self.assertEquals(parsed_record.type, record.type)
        self.assertEquals(parsed_record.value, record.value)
        self.assertEquals(parsed_record.category_id, record.category_id)
        self.assertEquals(parsed_record.element_id, record.element_id)
        self.assertEquals(parsed_record.flags, record.flags)
        self.assertEquals(parsed_lsd_pos, None)

        self.assertEquals(
            self.reader.parse_record_value_from_lsd_bindata(
                parsed_record.type, None), None)

    def _run_test_read_write_record_with_lsd(self, record, record_bindata,
                                             lsd_pos, lsd_bindata):
        self.assertEquals(self.writer.get_record_bindata(record, lsd_pos),
                          record_bindata)
        self.assertEquals(self.writer.get_record_lsd_bindata(record),
                          lsd_bindata)

        parsed_record, parsed_lsd_pos = self.reader.parse_record_from_bindata(
            record_bindata)
        self.assertEquals(parsed_record.type, record.type)
        self.assertEquals(parsed_record.category_id, record.category_id)
        self.assertEquals(parsed_record.element_id, record.element_id)
        self.assertEquals(parsed_record.flags, record.flags)
        self.assertEquals(parsed_lsd_pos, lsd_pos)

        self.assertEquals(
            self.reader.parse_record_value_from_lsd_bindata(
                record.type, lsd_bindata), record.value)

    # -------------------------------------------------------------------------
    #
    # -------------------------------------------------------------------------

    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_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_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_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_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_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_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 setUp(self):
     self.writer = HcrWriter()
コード例 #7
0
class TestReadWriteHcrRepository(unittest.TestCase):
    def setUp(self):
        self.writer = HcrWriter()
        self.reader = HcrReader()

    def write_repo_assertion_failure_files(self, actual_data, expected_data,
                                           failure_report_dir, filename):
        dir = os.path.join(ROOT_PATH, 'temp/repo_assertion_failure')
        dir = os.path.normpath(os.path.join(dir, failure_report_dir))
        if not os.path.exists(dir):
            os.makedirs(dir)

        f = open(os.path.join(dir, "actual_" + filename), "wb")
        try:
            f.write(actual_data)
        finally:
            f.close()

        f = open(os.path.join(dir, "expected_" + filename), "wb")
        try:
            f.write(expected_data)
        finally:
            f.close()

        return dir

    def _run_test_read_write_repository(self, repo, repo_data,
                                        failure_report_dir):
        """
        Test reading and writing the repository using the given data.
        
        In case of a failure, the actual and expected data are written
        to the given directory, so that they can be compared using e.g.
        Beyond Compare.
        
        @param repo: The repository object to test.
        @param repo_data: The expected binary data corresponding to the repository object.
        @param failure_report_dir: The directory where files are written in case of
            a failure. Note that this should be just e.g. 'empty_repo', the parent dirs
            are appended automatically.
        """
        # Test writing
        expected_data = repo_data
        actual_data = self.writer.get_repository_bindata(repo)
        if actual_data != expected_data:
            dir = self.write_repo_assertion_failure_files(
                actual_data, expected_data, failure_report_dir, 'repo.dat')
            self.fail("Actual and expected repository data are not equal!\n"+\
                "See the files in '%s'" % dir)

        # Test reading
        expected_repo = repo
        actual_repo = self.reader.parse_repository_from_bindata(repo_data)
        if actual_repo != expected_repo:
            dir = self.write_repo_assertion_failure_files(
                repr(actual_repo), repr(expected_repo), failure_report_dir,
                'repo.txt')
            self.fail("Actual and expected repository objects are not equal!\n"+\
                "See the files in '%s'" % dir)

    # -------------------------------------------------------------------------
    #
    # -------------------------------------------------------------------------

    def test_read_write_empty_repository_1(self):
        repo = HcrRepository([], version=2, flags=3)
        repo_data = hex_to_bindata(
            "48435266 0200 0300 00000000 20000000"+\
            "00000000 000000000000000000000000")

        self._run_test_read_write_repository(repo, repo_data, 'empty_repo_1')

    def test_read_write_empty_repository_2(self):
        repo = HcrRepository([], version=0xBEEF, flags=0xCAFE)
        repo_data = hex_to_bindata(
            "48435266 EFBE FECA 00000000 20000000"+\
            "00000000 000000000000000000000000")

        self._run_test_read_write_repository(repo, repo_data, 'empty_repo_2')

    # -------------------------------------------------------------------------
    #
    # -------------------------------------------------------------------------

    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_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_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')