Exemplo n.º 1
0
    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')
Exemplo n.º 2
0
    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')
Exemplo n.º 3
0
    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 get_hcr_repository(self):
        """
        Return a HcrRepository object created based on this output.
        
        The type of this Output object should be 'hcr', otherwise and an exception is raised.
        """
        if self.type != 'hcr':
            raise RuntimeError(
                "get_hcr_repository() called on an Output object with type '%s' (should be 'hcr')"
                % self.type)

        return HcrRepository(self.get_hcr_records())
    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
Exemplo n.º 6
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))
Exemplo n.º 7
0
    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')
Exemplo n.º 8
0
    def parse_repository_from_bindata(self, data):
        """
        @return: HcrRepository object constructed from the given binary data.
        """
        header_data = data[:32]
        header = HcrHeader()
        header.loads(header_data)

        expected_len = 32 + header.nrecords * 20 + header.lsd_size
        if len(data) < expected_len:
            raise InvalidHcrDataSizeError("File size is %d, expected at least %d based on header" \
                                          % (len(data), expected_len))

        expected_lsd_offset = 32 + header.nrecords * 20
        if header.lsd_offset != expected_lsd_offset:
            raise InvalidLsdSectionOffsetError("LSD offset is %d, expected %d" \
                                               % (header.lsd_offset, expected_lsd_offset))

        records = []
        for i in xrange(header.nrecords):
            record_offset = 32 + i * 20
            record_data = data[record_offset:record_offset + 20]
            record, lsd_pos = self.parse_record_from_bindata(record_data)
            if lsd_pos != None:
                if lsd_pos[0] > header.lsd_size or (
                        lsd_pos[0] + lsd_pos[1]) > header.lsd_size:
                    raise InvalidRecordLsdPositionError(
                        "LSD offset of record %d (category=%d, element=%d) is %r, but LSD section size is %d" \
                        % (i, record.category_id, record.element_id, lsd_pos, header.lsd_size))
                lsd_offset = lsd_pos[0] + header.lsd_offset
                lsd_data = data[lsd_offset:lsd_offset + lsd_pos[1]]
                record.value = self.parse_record_value_from_lsd_bindata(
                    record.type, lsd_data)
            records.append(record)

        return HcrRepository(records, header.version, header.flags)
    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_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)
Exemplo n.º 11
0
    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')