Beispiel #1
0
    def testReadArcsNormal(self):
        """Asserts that arcs are correctly read from the stream.

        Does not test the use of flags. Validates that arcs are
        created in both blocks and the source/destination are
        correct for each.
        """
        n_blocks = 50
        func = function_summary.FunctionSummary(0, "func", "src.c", 1)
        func.blocks = [
            block_summary.BlockSummary(i, 3 * i) for i in range(n_blocks)
        ]
        src_block_index = 0
        skip = 2
        self.stream = MockStream.concat_int(self.stream, src_block_index)
        for i in range(src_block_index + 1, n_blocks, skip):
            self.stream = MockStream.concat_int(self.stream, i)
            self.stream = MockStream.concat_int(
                self.stream, 0)  #  no flag applied to the arc
        parser = gcno_parser.GCNOParser(self.stream)
        n_arcs = len(range(src_block_index + 1, n_blocks, skip))
        parser.ReadArcs(n_arcs * 2 + 1, func)
        j = 0
        for i in range(src_block_index + 1, n_blocks, skip):
            self.assertEqual(
                func.blocks[src_block_index].exit_arcs[j].src_block.index,
                src_block_index)
            self.assertEqual(
                func.blocks[src_block_index].exit_arcs[j].dst_block.index, i)
            self.assertEqual(func.blocks[i].entry_arcs[0].src_block.index,
                             src_block_index)
            self.assertEqual(func.blocks[i].entry_arcs[0].dst_block.index, i)
            j += 1
Beispiel #2
0
    def testReadLines(self):
        """Asserts that lines are read correctly.

        Blocks must have correct references to the lines contained
        in the block.
        """
        self.stream = MockStream.concat_int(self.stream, 2)  #  block number
        self.stream = MockStream.concat_int(self.stream, 0)  #  dummy
        name = "src.c"
        name_length = int(
            math.ceil(1.0 * len(name) / MockStream.BYTES_PER_WORD)) + 1
        self.stream = MockStream.concat_string(self.stream, name)
        n_arcs = 5
        for i in range(1, n_arcs + 1):
            self.stream = MockStream.concat_int(self.stream, i)

        n_blocks = 5
        func = function_summary.FunctionSummary(0, "func", name, 1)
        func.blocks = [
            block_summary.BlockSummary(i, 3 * i) for i in range(n_blocks)
        ]
        parser = gcno_parser.GCNOParser(self.stream)
        parser.ReadLines(n_arcs + name_length + 3, func)
        self.assertEqual(len(func.blocks[2].lines), 5)
        self.assertEqual(func.blocks[2].lines, range(1, 6))
    def _GetChecksumGcnoDict(self, cov_zip):
        """Generates a dictionary from gcno checksum to GCNOParser object.

        Processes the gcnodir files in the zip file to produce a mapping from gcno
        checksum to the GCNOParser object wrapping the gcno content.

        Args:
            cov_zip: the zip file containing gcnodir files from the device build

        Returns:
            the dictionary of gcno checksums to GCNOParser objects
        """
        checksum_gcno_dict = dict()
        fnames = cov_zip.namelist()
        instrumented_modules = [
            f for f in fnames if f.endswith(COVERAGE_SUFFIX)
        ]
        for instrumented_module in instrumented_modules:
            # Read the gcnodir file
            archive = archive_parser.Archive(
                cov_zip.open(instrumented_module).read())
            try:
                archive.Parse()
            except ValueError:
                logging.error("Archive could not be parsed: %s", name)
                continue

            for gcno_file_path in archive.files:
                file_name_path = gcno_file_path.rsplit(".", 1)[0]
                file_name = os.path.basename(file_name_path)
                gcno_stream = io.BytesIO(archive.files[gcno_file_path])
                gcno_file_parser = gcno_parser.GCNOParser(gcno_stream)
                checksum_gcno_dict[
                    gcno_file_parser.checksum] = gcno_file_parser
        return checksum_gcno_dict
Beispiel #4
0
 def setUpClass(cls):
     dir_path = os.path.dirname(os.path.realpath(__file__))
     gcno_path = os.path.join(dir_path, cls.GOLDEN_GCNO_PATH)
     with open(gcno_path, 'rb') as file:
         gcno_summary = gcno_parser.GCNOParser(file).Parse()
     gcda_path = os.path.join(dir_path, cls.GOLDEN_GCDA_PATH)
     with open(gcda_path, 'rb') as file:
         parser = gcda_parser.GCDAParser(file)
         parser.Parse(gcno_summary)
     cls.gcno_summary = gcno_summary
 def setUpClass(cls):
     root_dir = os.path.join(os.getenv('ANDROID_BUILD_TOP'),
                             'test/vts/utils/python/coverage/testdata/')
     gcno_path = os.path.join(root_dir, 'sample.gcno')
     with open(gcno_path, 'rb') as file:
         gcno_summary = gcno_parser.GCNOParser(file).Parse()
     gcda_path = os.path.join(root_dir, 'sample.gcda')
     with open(gcda_path, 'rb') as file:
         parser = gcda_parser.GCDAParser(file)
         parser.Parse(gcno_summary)
     cls.gcno_summary = gcno_summary
Beispiel #6
0
    def testReadBlocks(self):
        """Asserts that blocks are correctly read from the stream.

        Tests correct values for flag and index.
        """
        n_blocks = 10
        func = function_summary.FunctionSummary(0, "func", "src.c", 1)
        for i in range(n_blocks):
            self.stream = MockStream.concat_int(self.stream, 3 * i)
        parser = gcno_parser.GCNOParser(self.stream)
        parser.ReadBlocks(n_blocks, func)
        self.assertEqual(len(func.blocks), n_blocks)
        for i in range(n_blocks):
            self.assertEqual(func.blocks[i].flag, 3 * i)
            self.assertEqual(func.blocks[i].index, i)
Beispiel #7
0
    def testReadArcFlags(self):
        """Asserts that arc flags are correctly interpreted.
        """
        n_blocks = 5
        func = function_summary.FunctionSummary(0, "func", "src.c", 1)
        func.blocks = [
            block_summary.BlockSummary(i, 3 * i) for i in range(n_blocks)
        ]
        self.stream = MockStream.concat_int(self.stream,
                                            0)  #  source block index

        self.stream = MockStream.concat_int(self.stream, 1)  #  normal arc
        self.stream = MockStream.concat_int(self.stream, 0)

        self.stream = MockStream.concat_int(self.stream, 2)  #  on-tree arc
        self.stream = MockStream.concat_int(
            self.stream, arc_summary.ArcSummary.GCOV_ARC_ON_TREE)

        self.stream = MockStream.concat_int(self.stream, 3)  #  fake arc
        self.stream = MockStream.concat_int(
            self.stream, arc_summary.ArcSummary.GCOV_ARC_FAKE)

        self.stream = MockStream.concat_int(self.stream, 4)  #  fallthrough arc
        self.stream = MockStream.concat_int(
            self.stream, arc_summary.ArcSummary.GCOV_ARC_FALLTHROUGH)

        parser = gcno_parser.GCNOParser(self.stream)
        parser.ReadArcs(4 * 2 + 1, func)

        self.assertFalse(func.blocks[0].exit_arcs[0].on_tree)
        self.assertFalse(func.blocks[0].exit_arcs[0].fake)
        self.assertFalse(func.blocks[0].exit_arcs[0].fallthrough)

        self.assertTrue(func.blocks[0].exit_arcs[1].on_tree)
        self.assertFalse(func.blocks[0].exit_arcs[1].fake)
        self.assertFalse(func.blocks[0].exit_arcs[1].fallthrough)

        self.assertFalse(func.blocks[0].exit_arcs[2].on_tree)
        self.assertTrue(func.blocks[0].exit_arcs[2].fake)
        self.assertFalse(func.blocks[0].exit_arcs[2].fallthrough)

        self.assertFalse(func.blocks[0].exit_arcs[3].on_tree)
        self.assertFalse(func.blocks[0].exit_arcs[3].fake)
        self.assertTrue(func.blocks[0].exit_arcs[3].fallthrough)
Beispiel #8
0
    def _GetChecksumGcnoDict(self, cov_zip):
        """Generates a dictionary from gcno checksum to GCNOParser object.

        Processes the gcnodir files in the zip file to produce a mapping from gcno
        checksum to the GCNOParser object wrapping the gcno content.
        Note there might be multiple gcno files corresponds to the same checksum.

        Args:
            cov_zip: the zip file containing gcnodir files from the device build

        Returns:
            the dictionary of gcno checksums to GCNOParser objects
        """
        checksum_gcno_dict = dict()
        fnames = cov_zip.namelist()
        instrumented_modules = [
            f for f in fnames if f.endswith(COVERAGE_SUFFIX)
        ]
        for instrumented_module in instrumented_modules:
            # Read the gcnodir file
            archive = archive_parser.Archive(
                cov_zip.open(instrumented_module).read())
            try:
                archive.Parse()
            except ValueError:
                logging.error("Archive could not be parsed: %s", name)
                continue

            for gcno_file_path in archive.files:
                gcno_stream = io.BytesIO(archive.files[gcno_file_path])
                gcno_file_parser = gcno_parser.GCNOParser(gcno_stream)
                if gcno_file_parser.checksum in checksum_gcno_dict:
                    checksum_gcno_dict[gcno_file_parser.checksum].append(
                        gcno_file_parser)
                else:
                    checksum_gcno_dict[gcno_file_parser.checksum] = [
                        gcno_file_parser
                    ]
        return checksum_gcno_dict
Beispiel #9
0
    def testReadFunction(self):
        """Asserts that the function is read correctly.

        Verifies that ident, name, source file name,
        and first line number are all read correctly.
        """
        ident = 102010
        self.stream = MockStream.concat_int(self.stream, ident)
        self.stream = MockStream.concat_int(self.stream, 0)
        self.stream = MockStream.concat_int(self.stream, 0)
        name = "TestFunction"
        src_file_name = "TestSouceFile.c"
        first_line_number = 102
        self.stream = MockStream.concat_string(self.stream, name)
        self.stream = MockStream.concat_string(self.stream, src_file_name)
        self.stream = MockStream.concat_int(self.stream, first_line_number)
        parser = gcno_parser.GCNOParser(self.stream)
        summary = parser.ReadFunction()
        self.assertEqual(name, summary.name)
        self.assertEqual(ident, summary.ident)
        self.assertEqual(src_file_name, summary.src_file_name)
        self.assertEqual(first_line_number, summary.first_line_number)