Example #1
0
def store(test_conf, run_name, file_content):
    """
    Store a new analysis run with multiple reports.
    """

    check_host, check_port = get_check_server_host_port(test_conf)

    with CCReportHelper(check_host, check_port) as store_server:

        run_id = store_server.addCheckerRun('command',
                                            run_name,
                                            'version',
                                            False)

        store_reports(run_id, file_content, test_conf)

        store_server.finishCheckerRun(run_id)

    return run_id
Example #2
0
 def setUp(self):
     self._report = CCReportHelper("localhost", int(os.environ["CC_TEST_SERVER_PORT"]), "/")
Example #3
0
class HashClash(unittest.TestCase):
    """Unit test for testing hash clash handling."""

    def _create_run(self, name):
        """Creates a new run using an unique id."""

        return self._report.addCheckerRun(name, name + str(uuid4()), "v", False)

    def _create_file(self, run_id, name, cols=10, lines=10):
        """Creates a new file with random content."""

        path = name + "_" + str(uuid4())
        need = self._report.needFileContent(run_id, path)
        self.assertTrue(need.needed)

        content = _generate_content(cols, lines)
        success = self._report.addFileContent(need.fileId, content)
        self.assertTrue(success)

        return need.fileId, path

    def _create_build_action(self, run_id, name, analyzer_type, source_file):
        """Creates a new build action."""

        return self._report.addBuildAction(run_id, name, name, analyzer_type, source_file)

    def _create_simple_report(self, file_id, build_action_id, bug_hash, position):
        """Creates a new report with one bug path position and event."""

        bug_path = [BugPathPos(position[0][0], position[0][1], position[1][0], position[1][1], file_id)]
        bug_evt = [BugPathEvent(position[0][0], position[0][1], position[1][0], position[1][1], "evt", file_id)]
        return self._report.addReport(
            build_action_id,
            file_id,
            bug_hash,
            "test",
            bug_path,
            bug_evt,
            "test_id",
            "test_cat",
            "test_type",
            Severity.UNSPECIFIED,
            False,
        )

    @contextmanager
    def _init_new_test(self, name):
        """Creates a new run, file, and build action.

        Use it in a 'with' statement. At the end of the 'with' statement it
        calls the finish methods for the run and the build action.

        Example:
            with self._init_new_test('test') as ids:
                # do stuff
        """

        run_id = self._create_run(name)
        file_id, source_file = self._create_file(run_id, name)

        # analyzer type needs to match with the supported analyzer types
        # clangsa is used for testing
        analyzer_type = "clangsa"
        build_action_id = self._create_build_action(run_id, name, analyzer_type, source_file)
        yield (run_id, file_id, build_action_id, source_file)
        self._report.finishBuildAction(build_action_id, "OK")
        self._report.finishCheckerRun(run_id)

    def setUp(self):
        self._report = CCReportHelper("localhost", int(os.environ["CC_TEST_SERVER_PORT"]), "/")

    def test(self):
        """Runs the following tests:

        - Duplicates.
        - Hash clash in same file.
        - Hash clash in different files.
        - Hash clash in different build actions.
        """

        with self._init_new_test("test1") as ids1, self._init_new_test("test2") as ids2:
            _, file_id1, build_action_id1, source_file1 = ids1
            run_id2, file_id2, build_action_id2, source_file2 = ids2
            rep_id1 = self._create_simple_report(file_id1, build_action_id1, "XXX", ((1, 1), (1, 2)))
            rep_id2 = self._create_simple_report(file_id1, build_action_id1, "XXX", ((1, 1), (1, 2)))
            # Same file, same hash and position
            self.assertEqual(rep_id1, rep_id2)

            rep_id3 = self._create_simple_report(file_id1, build_action_id1, "XXX", ((2, 1), (2, 2)))
            # Same file, same hash and different position
            self.assertNotEqual(rep_id1, rep_id3)

            rep_id4 = self._create_simple_report(file_id2, build_action_id2, "XXX", ((1, 1), (1, 2)))
            rep_id5 = self._create_simple_report(file_id2, build_action_id2, "YYY", ((1, 1), (1, 2)))
            # Different file, same hash, and position
            self.assertNotEqual(rep_id1, rep_id4)
            # Different file, same hash and different position
            self.assertNotEqual(rep_id3, rep_id4)
            # Same file and position, different hash
            self.assertNotEqual(rep_id4, rep_id5)

            # analyzer type needs to match with the supported analyzer types
            # clangsa is used for testing
            analyzer_type = "clangsa"
            build_action_id2_2 = self._create_build_action(run_id2, "test2_2", analyzer_type, source_file2)
            try:
                rep_id6 = self._create_simple_report(file_id2, build_action_id2_2, "XXX", ((1, 1), (1, 2)))
                # Same run, file, hash, and position, but
                # different build action.
                self.assertEqual(rep_id4, rep_id6)
            finally:
                self._report.finishBuildAction(build_action_id2_2, "OK")

            rep_id7 = self._create_simple_report(file_id1, build_action_id2, "XXX", ((1, 1), (1, 2)))
            # build_action_id1 and build_action_id2 is in different runs
            self.assertNotEqual(rep_id1, rep_id7)
Example #4
0
 def setUp(self):
     self._report = CCReportHelper('localhost',
                                   int(os.environ['CC_TEST_SERVER_PORT']),
                                   '/')
Example #5
0
 def setUp(self):
     self._report = CCReportHelper('localhost',
                                   int(os.environ['CC_TEST_SERVER_PORT']),
                                   '/')
Example #6
0
class HashClash(unittest.TestCase):
    """Unit test for testing hash clash handling."""
    def _create_run(self, name):
        """Creates a new run using an unique id."""

        return self._report.addCheckerRun(name, name + str(uuid4()), 'v',
                                          False)

    def _create_file(self, run_id, name, cols=10, lines=10):
        """Creates a new file with random content."""

        path = name + "_" + str(uuid4())
        need = self._report.needFileContent(run_id, path)
        self.assertTrue(need.needed)

        content = _generate_content(cols, lines)
        success = self._report.addFileContent(need.fileId, content)
        self.assertTrue(success)

        return need.fileId, path

    def _create_build_action(self, run_id, name, analyzer_type, source_file):
        """Creates a new build action."""

        return self._report.addBuildAction(run_id, name, name, analyzer_type,
                                           source_file)

    def _create_simple_report(self, file_id, build_action_id, bug_hash,
                              position):
        """Creates a new report with one bug path position and event."""

        bug_path = [
            BugPathPos(position[0][0], position[0][1], position[1][0],
                       position[1][1], file_id)
        ]
        bug_evt = [
            BugPathEvent(position[0][0], position[0][1], position[1][0],
                         position[1][1], 'evt', file_id)
        ]
        return self._report.addReport(build_action_id, file_id, bug_hash,
                                      'test', bug_path, bug_evt, 'test_id',
                                      'test_cat', 'test_type',
                                      Severity.UNSPECIFIED, False)

    @contextmanager
    def _init_new_test(self, name):
        """Creates a new run, file, and build action.

        Use it in a 'with' statement. At the end of the 'with' statement it
        calls the finish methods for the run and the build action.

        Example:
            with self._init_new_test('test') as ids:
                # do stuff
        """

        run_id = self._create_run(name)
        file_id, source_file = self._create_file(run_id, name)

        # analyzer type needs to match with the supported analyzer types
        # clangsa is used for testing
        analyzer_type = 'clangsa'
        build_action_id = self._create_build_action(run_id, name,
                                                    analyzer_type, source_file)
        yield (run_id, file_id, build_action_id, source_file)
        self._report.finishBuildAction(build_action_id, 'OK')
        self._report.finishCheckerRun(run_id)

    def setUp(self):
        self._report = CCReportHelper('localhost',
                                      int(os.environ['CC_TEST_SERVER_PORT']),
                                      '/')

    def test(self):
        """Runs the following tests:

        - Duplicates.
        - Hash clash in same file.
        - Hash clash in different files.
        - Hash clash in different build actions.
        """

        with self._init_new_test('test1') as ids1, \
             self._init_new_test('test2') as ids2:
            _, file_id1, build_action_id1, source_file1 = ids1
            run_id2, file_id2, build_action_id2, source_file2 = ids2
            rep_id1 = self._create_simple_report(file_id1, build_action_id1,
                                                 'XXX', ((1, 1), (1, 2)))
            rep_id2 = self._create_simple_report(file_id1, build_action_id1,
                                                 'XXX', ((1, 1), (1, 2)))
            # Same file, same hash and position
            self.assertEqual(rep_id1, rep_id2)

            rep_id3 = self._create_simple_report(file_id1, build_action_id1,
                                                 'XXX', ((2, 1), (2, 2)))
            # Same file, same hash and different position
            self.assertNotEqual(rep_id1, rep_id3)

            rep_id4 = self._create_simple_report(file_id2, build_action_id2,
                                                 'XXX', ((1, 1), (1, 2)))
            rep_id5 = self._create_simple_report(file_id2, build_action_id2,
                                                 'YYY', ((1, 1), (1, 2)))
            # Different file, same hash, and position
            self.assertNotEqual(rep_id1, rep_id4)
            # Different file, same hash and different position
            self.assertNotEqual(rep_id3, rep_id4)
            # Same file and position, different hash
            self.assertNotEqual(rep_id4, rep_id5)

            # analyzer type needs to match with the supported analyzer types
            # clangsa is used for testing
            analyzer_type = 'clangsa'
            build_action_id2_2 = self._create_build_action(
                run_id2, 'test2_2', analyzer_type, source_file2)
            try:
                rep_id6 = self._create_simple_report(file_id2,
                                                     build_action_id2_2, 'XXX',
                                                     ((1, 1), (1, 2)))
                # Same run, file, hash, and position, but different build action
                self.assertEqual(rep_id4, rep_id6)
            finally:
                self._report.finishBuildAction(build_action_id2_2, 'OK')

            rep_id7 = self._create_simple_report(file_id1, build_action_id2,
                                                 'XXX', ((1, 1), (1, 2)))
            # build_action_id1 and build_action_id2 is in different runs
            self.assertNotEqual(rep_id1, rep_id7)
Example #7
0
def main():

    # handle argument parsing
    parseArgs()

    #--- main part -------------------------------------------------------------
    reportTimeList = []
    getTimeList = []

    with CCReportHelper(args.address, args.check_port) as ccReporter, \
         CCViewerHelper(args.address, args.view_port, '/') as ccViewer, \
         open(args.output, 'r+') as outFile:

        outFile.truncate()
        for runCount in range(runNumber):
            before = datetime.datetime.now()
            run_id = ccReporter.addCheckerRun('command', 'name_' + str(runCount) + '_' + str(uuid4()),
                                         'version', False)
            report_ids = []
            for fileCount in range(fileNumber):
                print('\nrun: ' + str(runCount + 1) + '/' + str(runNumber) + '\nfile: ' + str(fileCount + 1) + '/' + str(fileNumber))

                file_id = ccReporter.needFileContent(run_id, 'file_' + str(fileCount)).fileId
                ccReporter.addFileContent(file_id, fileContent)

                build_action_id = ccReporter.addBuildAction(run_id,
                                                            'build_cmd_' + str(fileCount),
                                                            'check_cmd_' + str(fileCount),
                                                            'target_' + str(fileCount))

                ccReporter.finishBuildAction(build_action_id, '')
                for bugCount in range(bugPerFile):
                    bug_pathes = []
                    bug_events = []
                    for bugElementCount in range(bugLength):
                        line = bugCount * bugLength + bugElementCount + 1
                        bug_pathes.append(BugPathPos(line, 1, line, 10, file_id))
                        bug_events.append(BugPathEvent(line, 1, line, 10, 'event_msg', file_id))

                    report_id = ccReporter.addReport(build_action_id,
                                                     file_id,
                                                     'hash_' + str(run_id) + '_'  + str(fileCount) + '_' + str(bugCount),
                                                     1,
                                                     'checker_message',
                                                     bug_pathes,
                                                     bug_events,
                                                     'checker_name',
                                                     'checker_cat',
                                                     'bug_type',
                                                     1)
                    report_ids.append(report_id)

            #ccReporter.moduleToReport(run_id, 'module_id', report_ids)
            ccReporter.finishCheckerRun(run_id)

            after = datetime.datetime.now()

            time = (after - before).total_seconds()
            reportTimeList.append(time)

            before = datetime.datetime.now()
            runIDs = [rundata.runId for rundata in ccViewer.getRunData()]
            after = datetime.datetime.now()
            time = (after - before).total_seconds()
            getTimeList.append(time)

            before = datetime.datetime.now()
            res = ccViewer.getAllRunResults(runIDs[-1])
            after = datetime.datetime.now()
            time = (after - before).total_seconds()
            getTimeList.append(time)

            before = datetime.datetime.now()
            ccViewer.getReportDetails(res[-1].reportId)
            after = datetime.datetime.now()
            time = (after - before).total_seconds()
            getTimeList.append(time)

            s = str(runCount) + ';' + str(reportTimeList[-1]) + ';' + str(getTimeList[-3]) + ';' + str(getTimeList[-2]) + ';' + str(getTimeList[-1])
            print(s)
            outFile.write(s + '\n')
Example #8
0
def store_reports(run_id, file_content, test_conf):
    """
    Generate and store build actions, reports, file content.
    """

    number_of_files = test_conf.get("number_of_files", 1)
    bug_per_file = test_conf.get("bug_per_file", 1)
    bug_length = test_conf.get("bug_length", 1)

    check_host, check_port = get_check_server_host_port(test_conf)

    with CCReportHelper(check_host, check_port) as store_server:

        for file_count in range(number_of_files):

            ba_id = store_server.addBuildAction(run_id,
                                                'build_cmd_' +
                                                str(file_count),
                                                'check_cmd_' +
                                                str(file_count),
                                                "clangsa",
                                                'target_' +
                                                str(file_count)
                                                )

            file_id = store_server.needFileContent(run_id,
                                                   'file_' +
                                                   str(file_count)
                                                   ).fileId

            store_server.addFileContent(file_id, file_content)

            store_server.finishBuildAction(ba_id, '')
            for bug_count in range(bug_per_file):
                bug_paths = []
                bug_events = []
                for bug_element_count in range(bug_length):
                    line = bug_count * bug_length + bug_element_count + 1

                    bug_paths.append(BugPathPos(line,  # start line
                                                1,  # start col
                                                line,  # end line
                                                10,  # end col
                                                file_id)  # file id
                                     )

                    bug_events.append(BugPathEvent(line,  # start line
                                                   1,  # start col
                                                   line,  # end line
                                                   10,  # end col
                                                   'event_msg',  # checker msg
                                                   file_id)  # file id
                                      )

                bug_hash = 'hash_' + str(run_id) + '_' + str(file_count) + \
                    '_' + str(bug_count)

                r_id = store_server.addReport(ba_id,
                                              file_id,
                                              bug_hash,
                                              'checker_message',
                                              bug_paths,
                                              bug_events,
                                              'checker_name',
                                              'checker_cat',
                                              'bug_type',
                                              Severity.STYLE,
                                              False)