Example #1
0
    def test_get_diff_res_count_unresolved(self):
        """
        Count the unresolved results with no filter.
        """
        base_run_id = self._base_runid
        new_run_id = self._new_runid

        base_count = self._cc_client.getRunResultCount([base_run_id],
                                                       None,
                                                       None)
        logging.debug("Base run id: %d", base_run_id)
        logging.debug("Base count: %d", base_count)

        base_run_res = get_all_run_results(self._cc_client, base_run_id)

        print_run_results(base_run_res)

        new_count = self._cc_client.getRunResultCount([new_run_id],
                                                      None,
                                                      None)
        logging.debug("New run id: %d", new_run_id)
        logging.debug("New count: %d", new_count)

        new_run_res = get_all_run_results(self._cc_client, new_run_id)

        print_run_results(new_run_res)

        cmp_data = CompareData(runIds=[new_run_id],
                               diffType=DiffType.UNRESOLVED)

        diff_res = self._cc_client.getRunResultCount([base_run_id],
                                                     None,
                                                     cmp_data)

        self.assertEqual(diff_res, 26)
Example #2
0
    def test_same_bug_hash(self):
        """
        Test that different report ID's referring the same bug hash can
        query each other's comments.
        """

        # Get run results for the first run.
        runid_base = self._test_runs[0].runId
        logging.debug('Get all run results from the db for runid: ' +
                      str(runid_base))

        run_results_base = get_all_run_results(self._cc_client, runid_base)
        self.assertIsNotNone(run_results_base)
        self.assertNotEqual(len(run_results_base), 0)

        bug_base = run_results_base[0]

        # Get run results for the second run.
        runid_new = self._test_runs[1].runId
        logging.debug('Get all run results from the db for runid: ' +
                      str(runid_new))

        run_results_new = get_all_run_results(self._cc_client, runid_new)
        self.assertIsNotNone(run_results_new)
        self.assertNotEqual(len(run_results_new), 0)

        bug_new = run_results_new[0]

        # Both bug have the same bug hash.
        self.assertEqual(bug_base.bugHash, bug_new.bugHash)

        # There are no comments available for the bug.
        comments = self._cc_client.getComments(bug_base.reportId)
        self.assertEqual(len(comments), 0)

        comments = self._cc_client.getComments(bug_new.reportId)
        self.assertEqual(len(comments), 0)

        # Try to add a new comment for the first bug
        comment = CommentData(author='Anonymous', message='First msg')
        success = self._cc_client.addComment(bug_base.reportId, comment)
        self.assertTrue(success)
        logging.debug('Bug commented successfully')

        comments = self._cc_client.getComments(bug_base.reportId)
        self.assertEqual(len(comments), 1)

        comments = self._cc_client.getComments(bug_new.reportId)
        self.assertEqual(len(comments), 1)

        # Remove the comment for the bug.
        success = self._cc_client.removeComment(comments[0].id)
        self.assertTrue(success)
        logging.debug('Comment removed successfully')

        comments = self._cc_client.getComments(bug_base.reportId)
        self.assertEqual(len(comments), 0)

        comments = self._cc_client.getComments(bug_new.reportId)
        self.assertEqual(len(comments), 0)
Example #3
0
    def test_same_bug_hash(self):
        """
        Test that different report ID's referring the same bug hash can
        query each other's comments.
        """

        # Get run results for the first run.
        runid_base = self._test_runs[0].runId
        logging.debug('Get all run results from the db for runid: ' +
                      str(runid_base))

        run_results_base = get_all_run_results(self._cc_client, runid_base)
        self.assertIsNotNone(run_results_base)
        self.assertNotEqual(len(run_results_base), 0)

        bug_base = run_results_base[0]

        # Get run results for the second run.
        runid_new = self._test_runs[1].runId
        logging.debug('Get all run results from the db for runid: ' +
                      str(runid_new))

        run_results_new = get_all_run_results(self._cc_client, runid_new)
        self.assertIsNotNone(run_results_new)
        self.assertNotEqual(len(run_results_new), 0)

        bug_new = run_results_new[0]

        # Both bug have the same bug hash.
        self.assertEqual(bug_base.bugHash, bug_new.bugHash)

        # There are no comments available for the bug.
        comments = self._cc_client.getComments(bug_base.reportId)
        self.assertEqual(len(comments), 0)

        comments = self._cc_client.getComments(bug_new.reportId)
        self.assertEqual(len(comments), 0)

        # Try to add a new comment for the first bug
        comment = CommentData(author='Anonymous', message='First msg')
        success = self._cc_client.addComment(bug_base.reportId, comment)
        self.assertTrue(success)
        logging.debug('Bug commented successfully')

        comments = self._cc_client.getComments(bug_base.reportId)
        self.assertEqual(len(comments), 1)

        comments = self._cc_client.getComments(bug_new.reportId)
        self.assertEqual(len(comments), 1)

        # Remove the comment for the bug.
        success = self._cc_client.removeComment(comments[0].id)
        self.assertTrue(success)
        logging.debug('Comment removed successfully')

        comments = self._cc_client.getComments(bug_base.reportId)
        self.assertEqual(len(comments), 0)

        comments = self._cc_client.getComments(bug_new.reportId)
        self.assertEqual(len(comments), 0)
Example #4
0
    def test_get_diff_res_count_unresolved(self):
        """
        Count the unresolved results with no filter.
        """
        base_run_id = self._base_runid
        new_run_id = self._new_runid

        base_count = self._cc_client.getRunResultCount(base_run_id, [])
        logging.debug("Base run id: %d", base_run_id)
        logging.debug("Base count: %d", base_count)

        base_run_res = get_all_run_results(self._cc_client, base_run_id)

        print_run_results(base_run_res)

        new_count = self._cc_client.getRunResultCount(new_run_id, [])
        logging.debug("New run id: %d", new_run_id)
        logging.debug("New count: %d", new_count)

        new_run_res = get_all_run_results(self._cc_client, new_run_id)

        print_run_results(new_run_res)

        diff_res = self._cc_client.getDiffResultCount(base_run_id, new_run_id,
                                                      DiffType.UNRESOLVED, [])
        # Nothing is resolved.
        self.assertEqual(diff_res, base_count)
Example #5
0
    def test_get_diff_res_count_unresolved(self):
        """
        Count the unresolved results with no filter.
        """
        base_run_id = self._base_runid
        new_run_id = self._new_runid

        base_count = self._cc_client.getRunResultCount([base_run_id],
                                                       None,
                                                       None)
        print("Base run id: %d", base_run_id)
        print("Base count: %d", base_count)

        base_run_res = get_all_run_results(self._cc_client, base_run_id)

        print_run_results(base_run_res)

        new_count = self._cc_client.getRunResultCount([new_run_id],
                                                      None,
                                                      None)
        print("New run id: %d", new_run_id)
        print("New count: %d", new_count)

        new_run_res = get_all_run_results(self._cc_client, new_run_id)

        print_run_results(new_run_res)

        cmp_data = CompareData(runIds=[new_run_id],
                               diffType=DiffType.UNRESOLVED)

        diff_res = self._cc_client.getRunResultCount([base_run_id],
                                                     None,
                                                     cmp_data)

        self.assertEqual(diff_res, 25)
Example #6
0
    def test_double_suppress(self):
        """
        Suppressing the same bug for the second time should be successfull.
        The second suppress should not overwrite the
        already stored suppress comment.
        """

        runid = self._runid
        logging.debug('Get all run results from the db for runid: ' +
                      str(runid))

        run_results = get_all_run_results(self._cc_client, runid)
        self.assertIsNotNone(run_results)
        self.assertNotEqual(len(run_results), 0)

        suppress_comment = r'First suppress msg'
        bug = run_results[0]
        success = self._cc_client.suppressBug([runid],
                                              bug.reportId,
                                              suppress_comment)
        self.assertTrue(success)
        logging.debug('Bug suppressed successfully')

        # Try to suppress the same bug again.
        second_suppress_msg = r'Second suppress msg'
        success = self._cc_client.suppressBug([runid],
                                              bug.reportId,
                                              second_suppress_msg)
        self.assertTrue(success)
        logging.debug('Same bug suppressed successfully for the second time')

        simple_filters = [ReportFilter(suppressed=True)]
        run_results_suppressed = get_all_run_results(self._cc_client,
                                                     runid,
                                                     filters=simple_filters)

        self.assertIsNotNone(run_results_suppressed)
        self.assertEqual(len(run_results_suppressed), 1)

        bug_to_suppress = bug
        bug_to_suppress.suppressed = True
        bug_to_suppress.suppressComment = suppress_comment

        # The only one suppressed bug should be returned.
        self.assertEqual(bug_to_suppress, run_results_suppressed[0])

        success = self._cc_client.unSuppressBug([runid],
                                                bug_to_suppress.reportId)
        self.assertTrue(success)
        logging.debug('Bug unsuppressed successfully')

        simple_filters = [ReportFilter(suppressed=False)]
        run_results_unsuppressed = get_all_run_results(self._cc_client,
                                                       runid,
                                                       filters=simple_filters)

        self.assertIsNotNone(run_results_unsuppressed)
        self.assertEqual(len(run_results), len(run_results_unsuppressed))
Example #7
0
    def test_disable_checker(self):
        """
        The test depends on a run which was configured for update mode.
        Compared to the original test analysis in this run
        the deadcode.Deadstores checker was disabled.
        """

        run_results = get_all_run_results(self._cc_client, self._runid)

        print_run_results(run_results)

        # Get check command for the first storage.
        original_check_command = \
            self._cc_client.getCheckCommand(None, self._runid)

        # Run the anaysis again with different setup.
        test_project_path = self._testproject_data['project_path']
        ret = project.clean(test_project_path)
        if ret:
            sys.exit(ret)

        initial_codechecker_cfg = env.import_test_cfg(
            self._test_workspace)['codechecker_cfg']

        # Disable some checkers for the analysis.
        deadcode = 'deadcode.DeadStores'
        initial_codechecker_cfg['checkers'] = ['-d', deadcode]

        initial_test_project_name = self._run_name

        ret = codechecker.check_and_store(initial_codechecker_cfg,
                                          initial_test_project_name,
                                          test_project_path)
        if ret:
            sys.exit(1)

        # Get the results to compare.
        updated_results = get_all_run_results(self._cc_client, self._runid)

        all_bugs = self._testproject_data[self._clang_to_test]['bugs']
        deadcode_bugs = \
            [bug['hash'] for bug in all_bugs if bug['checker'] == deadcode]

        self.assertEquals(len(updated_results), len(all_bugs))
        self.assertTrue(all(map(
            lambda b: b.detectionStatus == 'unresolved',
            filter(lambda x: x in deadcode_bugs, updated_results))))

        # Get check command for the updated storage.
        updated_check_command = \
            self._cc_client.getCheckCommand(None, self._runid)

        # Check that the check command is changed.
        self.assertNotEqual(original_check_command, updated_check_command)
        self.assertTrue(deadcode not in original_check_command)
        self.assertTrue(deadcode in updated_check_command)
    def test_suppress_comment_in_db(self):
        """
        Exported source suppress comment stored as a review status in the db.
        """
        runid = self._runid
        logging.debug('Get all run results from the db for runid: ' +
                      str(runid))

        run_results = get_all_run_results(self._cc_client, runid)
        self.assertIsNotNone(run_results)
        self.assertNotEqual(len(run_results), 0)

        bug = run_results[0]

        report = self._cc_client.getReport(bug.reportId)

        # Check the stored suppress comment
        status = ReviewStatus.FALSE_POSITIVE
        self.assertEqual(report.reviewData.comment, 'deliberate segfault!')
        self.assertEqual(report.reviewData.status, status)

        # Change review status to confirmed bug.
        review_comment = 'This is really a bug'
        status = ReviewStatus.CONFIRMED
        success = self._cc_client.changeReviewStatus(bug.reportId, status,
                                                     review_comment)

        self.assertTrue(success)
        logging.debug('Bug review status changed successfully')

        # Check the same project again.
        codechecker_cfg = env.import_test_cfg(
            self._test_workspace)['codechecker_cfg']

        initial_test_project_name = self._run_name

        ret = codechecker.check(codechecker_cfg, initial_test_project_name,
                                self._test_project_path)
        if ret:
            sys.exit(1)

        # Get the results to compare.
        updated_results = get_all_run_results(self._cc_client, self._runid)
        self.assertIsNotNone(updated_results)
        self.assertNotEqual(len(updated_results), 0)

        bug = updated_results[0]

        report = self._cc_client.getReport(bug.reportId)

        # The stored suppress comment for the same bughash is the same.
        status = ReviewStatus.FALSE_POSITIVE
        self.assertEqual(report.reviewData.comment, 'deliberate segfault!')
        self.assertEqual(report.reviewData.status, status)
Example #9
0
    def test_get_run_results_severity_sort(self):
        """ Get the run results and sort them by severity and filename ASC. """
        runid = self._runid
        logging.debug('Get all run results from the db for runid: ' +
                      str(runid))
        sort_mode1 = SortMode(SortType.SEVERITY, Order.ASC)
        sort_mode2 = SortMode(SortType.FILENAME, Order.ASC)
        sort_types = [sort_mode1, sort_mode2]

        run_result_count = self._cc_client.getRunResultCount(runid, [])
        self.assertTrue(run_result_count)

        run_results = get_all_run_results(self._cc_client, runid, sort_types,
                                          [])
        self.assertIsNotNone(run_results)

        for i in range(run_result_count - 1):
            bug1 = run_results[i]
            bug2 = run_results[i + 1]
            self.assertTrue(bug1.severity <= bug2.severity)
            self.assertTrue((bug1.severity != bug2.severity)
                            or (bug1.checkedFile <= bug2.checkedFile))

        print_run_results(run_results)

        self.assertEqual(run_result_count, len(run_results))
Example #10
0
    def test_get_run_results_sorted2(self):
        """ Get the run results and sort them by file name and
            checker name ASC. """
        runid = self._runid
        logging.debug('Get all run results from the db for runid: ' +
                      str(runid))
        sortMode1 = SortMode(SortType.FILENAME, Order.ASC)
        sortMode2 = SortMode(SortType.CHECKER_NAME, Order.ASC)
        sort_types = [sortMode1, sortMode2]

        run_result_count = self._cc_client.getRunResultCount(runid, [])
        self.assertTrue(run_result_count)

        run_results = get_all_run_results(self._cc_client, runid, sort_types,
                                          [])
        self.assertIsNotNone(run_results)

        print_run_results(run_results)

        self.assertEqual(run_result_count, len(run_results))

        for i in range(run_result_count - 1):
            bug1 = run_results[i]
            bug2 = run_results[i + 1]
            self.assertTrue(bug1.checkedFile <= bug2.checkedFile)
            self.assertTrue((bug1.checkedFile != bug2.checkedFile)
                            or (bug1.lastBugPosition.startLine <=
                                bug2.lastBugPosition.startLine)
                            or (bug1.checkerId <= bug2.checkerId))
Example #11
0
    def test_get_run_results_checker_id_and_file_path(self):
        """ Test if all the bugs are found based
            on the test project configuration. """
        runid = self._runid
        logging.debug('Get all run results from the db for runid: ' +
                      str(runid))

        run_result_count = self._cc_client.getRunResultCount(runid, [])
        self.assertTrue(run_result_count)

        run_results = get_all_run_results(self._cc_client, runid)
        self.assertIsNotNone(run_results)
        self.assertEqual(run_result_count, len(run_results))

        test_project_results = self._testproject_data[
            self._clang_to_test]['bugs']
        for r in test_project_results:
            print(r)

        not_found = find_all(run_results, test_project_results)

        print_run_results(run_results)

        if not_found:
            print("===================")
            print('Not found bugs:')
            for bug in not_found:
                print(bug)
            print("===================")

        self.assertEqual(len(not_found), 0)
    def test_get_run_results_checker_id_and_file_path(self):
        """ Test if all the bugs are found based
            on the test project configuration. """
        runid = self._runid
        logging.debug('Get all run results from the db for runid: ' +
                      str(runid))

        run_result_count = self._cc_client.getRunResultCount([runid],
                                                             None,
                                                             None)
        self.assertTrue(run_result_count)

        run_results = get_all_run_results(self._cc_client, runid)
        self.assertIsNotNone(run_results)
        self.assertEqual(run_result_count, len(run_results))

        test_project_results = self._testproject_data[
            self._clang_to_test]['bugs']
        for r in test_project_results:
            print(r)

        not_found = find_all(run_results, test_project_results)

        print_run_results(run_results)

        if not_found:
            print("===================")
            print('Not found bugs:')
            for bug in not_found:
                print(bug)
            print("===================")

        self.assertEqual(len(not_found), 0)
    def test_get_run_results_sorted2(self):
        """ Get the run results and sort them by file name and
            checker name ASC. """
        runid = self._runid
        logging.debug('Get all run results from the db for runid: ' +
                      str(runid))
        sortMode1 = SortMode(SortType.FILENAME, Order.ASC)
        sortMode2 = SortMode(SortType.CHECKER_NAME, Order.ASC)
        sort_types = [sortMode1, sortMode2]

        run_result_count = self._cc_client.getRunResultCount([runid],
                                                             None,
                                                             None)
        self.assertTrue(run_result_count)

        run_results = get_all_run_results(self._cc_client,
                                          runid,
                                          sort_types,
                                          None)
        self.assertIsNotNone(run_results)

        print_run_results(run_results)

        self.assertEqual(run_result_count, len(run_results))

        for i in range(run_result_count - 1):
            bug1 = run_results[i]
            bug2 = run_results[i + 1]
            self.assertTrue(bug1.checkedFile <= bug2.checkedFile)
            self.assertTrue((bug1.checkedFile != bug2.checkedFile) or
                            (bug1.line <=
                             bug2.line) or
                            (bug1.checkerId <= bug2.checkerId))
Example #14
0
def measure(test_conf, performance_data, store_done, proc_done_counter,
            proc_counter_lock, keep):
    """
    Fill up a run with the configured values.
    """

    try:

        log.debug("Generating and storing results ...")

        number_of_runs = test_conf.get("number_of_runs", 1)

        # TODO: simulate append by using the same run name in multiple threads
        run_perf = {}
        for run_count in range(number_of_runs):
            run_name = 'name_' + str(run_count) + '_' + str(uuid4())
            file_line_size = test_conf.get("file_line_size")
            file_content = zlib.compress(
                '\n'.join(['A' * 80] * file_line_size),
                zlib.Z_BEST_COMPRESSION)

            with Timer('store', run_perf):
                run_id = store(test_conf, run_name, file_content)

            log.debug("Storing results for run " + str(run_id) + " done.")

            with proc_counter_lock:
                proc_done_counter.value -= 1

            # wait here for other processes to finish storing results
            # before measuiring queries
            store_done.wait()

            view_host, view_port = get_viewer_host_port(test_conf)

            with CCViewerHelper(view_host, view_port, '/') as viewer_client:

                with Timer('getRunData', run_perf):
                    viewer_client.getRunData()

                with Timer('getAllRunResulst', run_perf):
                    res = get_all_run_results(viewer_client, run_id)

                with Timer('getReportDetails', run_perf):
                    viewer_client.getReportDetails(res[-1].reportId)

                add_measurement_data(run_id, performance_data, run_perf)

                clean_after_fill = test_conf.get("clean_after_fill", True)
                if clean_after_fill and not keep:
                    delete_results(view_host, view_port, run_id,
                                   performance_data)

    except Exception as ex:
        log.error(ex)
        with proc_counter_lock:
            proc_done_counter.value -= 1
        sys.exit(1)
    def test_sort_review_status_rules(self):
        """ Test sorting review status rules. """
        run_results = get_all_run_results(self._cc_client, self._runid)
        self.assertIsNotNone(run_results)
        self.assertNotEqual(len(run_results), 0)

        bug1 = run_results[0]
        bug2 = next(r for r in run_results if r.bugHash != bug1.bugHash)

        success = self._cc_client.addReviewStatusRule(bug1.bugHash,
                                                      ReviewStatus.CONFIRMED,
                                                      'bug1')
        self.assertTrue(success)

        success = self._cc_client.addReviewStatusRule(
            bug2.bugHash, ReviewStatus.FALSE_POSITIVE, 'bug2')
        self.assertTrue(success)

        success = self._cc_client.addReviewStatusRule('invalid',
                                                      ReviewStatus.INTENTIONAL,
                                                      'bug3')
        self.assertTrue(success)

        # By default the rules are sorted by the date field in descending
        # order.
        rule_filter = None
        sort_mode = None
        limit = None
        offset = 0
        rules = self._cc_client.getReviewStatusRules(rule_filter, sort_mode,
                                                     limit, offset)
        self.assertEqual(len(rules), 3)

        self.__check_value_order([r.reviewData.date for r in rules],
                                 Order.DESC)

        # Test ordering rules by date.
        self.__check_rule_order(lambda r: r.reviewData.date,
                                ReviewStatusRuleSortType.DATE)

        # Test ordering rules by report hash.
        self.__check_rule_order(lambda r: r.reportHash,
                                ReviewStatusRuleSortType.REPORT_HASH)

        # Test ordering rules by author.
        self.__check_rule_order(lambda r: r.reviewData.author,
                                ReviewStatusRuleSortType.AUTHOR)

        # Test ordering rules by review status.
        self.__check_rule_order(lambda r: r.reviewData.status,
                                ReviewStatusRuleSortType.STATUS)

        # Test ordering rules by associated report count.
        self.__check_rule_order(
            lambda r: r.associatedReportCount,
            ReviewStatusRuleSortType.ASSOCIATED_REPORTS_COUNT)
Example #16
0
    def test_disable_checker(self):
        """
        The test depends on a run which was configured for update mode.
        Compared to the original test analysis in this run
        the deadcode.Deadstores checker was disabled.
        """

        run_results = get_all_run_results(self._cc_client, self._runid)

        print_run_results(run_results)

        # Run the anaysis again with different setup.
        test_project_path = self._testproject_data['project_path']
        ret = project.clean(test_project_path)
        if ret:
            sys.exit(ret)

        initial_codechecker_cfg = env.import_test_cfg(
            self._test_workspace)['codechecker_cfg']

        # Disable some checkers for the analysis.
        deadcode = 'deadcode.DeadStores'
        initial_codechecker_cfg['checkers'] = ['-d', deadcode]

        initial_test_project_name = self._run_name

        ret = codechecker.check(initial_codechecker_cfg,
                                initial_test_project_name,
                                test_project_path)
        if ret:
            sys.exit(1)

        # Get the results to compare.
        updated_results = get_all_run_results(self._cc_client, self._runid)

        all_bugs = self._testproject_data[self._clang_to_test]['bugs']
        deadcode_bugs = \
            [bug['hash'] for bug in all_bugs if bug['checker'] == deadcode]

        self.assertEquals(len(updated_results), len(all_bugs))
        self.assertTrue(all(map(
            lambda b: b.detectionStatus == 'unresolved',
            filter(lambda x: x in deadcode_bugs, updated_results))))
Example #17
0
    def test_skip(self):
        """ There should be no results from the skipped file. """

        runid = self._runid
        logging.debug('Get all run results from the db for runid: ' +
                      str(runid))

        run_results = get_all_run_results(self._cc_client, runid)
        self.assertIsNotNone(run_results)

        skipped_files = ["file_to_be_skipped.cpp", "skip.h", "path_end.h"]

        # IMPORTANT: This test is checking whether some reports are really not
        # stored because they were skipped during analysis with --skip flag.
        # However, since clang-tidy is not run, there will be no reports from
        # "clang-diagnostic" checker. These are handled separately here,
        # otherwise the test would believe they're missing because of --skip
        # which is not the case.

        test_proj_res = self._testproject_data[self._clang_to_test]['bugs']
        skipped = [
            x for x in test_proj_res if x['file'] in skipped_files
            or x['checker'].startswith('clang-diagnostic-')
        ]

        print("Analysis:")
        for res in run_results:
            print(res)

        print("\nTest config results:")
        for res in test_proj_res:
            print(res)

        print("\nTest config skipped results:")
        for res in skipped:
            print(res)

        missing_results = find_all(run_results, test_proj_res)

        print_run_results(run_results)

        print('Missing results:')
        for mr in missing_results:
            print(mr)

        if missing_results:
            for bug in missing_results:
                if not bug['checker'].startswith('clang-diagnostic-'):
                    self.assertIn(bug['file'], skipped_files)
        else:
            self.assertTrue(
                True, "There should be missing results because"
                "using skip")

        self.assertEqual(len(run_results), len(test_proj_res) - len(skipped))
Example #18
0
    def test_get_source_file_content(self):
        """
        Test getting the source file content stored to the database.
        Test unicode support the stored file can be decoded properly
        compare results form the database to the original file.
        """

        runid = self._runid
        report_filter = ReportFilter(checkerName=['*'], filepath=['*.c*'])

        run_result_count = self._cc_client.getRunResultCount([runid],
                                                             report_filter,
                                                             None)
        self.assertTrue(run_result_count)

        run_results = get_all_run_results(self._cc_client,
                                          runid,
                                          [],
                                          report_filter)
        self.assertIsNotNone(run_results)

        for run_res in run_results:
            self.assertTrue(re.match(r'.*\.c(pp)?$', run_res.checkedFile))

            logging.debug('Getting the content of ' + run_res.checkedFile)

            file_data = self._cc_client.getSourceFileData(run_res.fileId,
                                                          True,
                                                          None)
            self.assertIsNotNone(file_data)

            file_content1 = file_data.fileContent
            self.assertIsNotNone(file_content1)

            with codecs.open(run_res.checkedFile, 'r', 'UTF-8',
                             'replace') as source_file:
                file_content2 = source_file.read()

            self.assertEqual(file_content1, file_content2)

            file_data_b64 = self._cc_client.getSourceFileData(
                run_res.fileId, True, Encoding.BASE64)
            self.assertIsNotNone(file_data_b64)

            file_content1_b64 = base64.b64decode(file_data_b64.fileContent)
            self.assertIsNotNone(file_content1_b64)

            file_content2 = codecs.encode(file_content2, 'utf-8')
            self.assertEqual(file_content1_b64, file_content2)

        logging.debug('got ' + str(len(run_results)) + ' files')

        self.assertEqual(run_result_count, len(run_results))
Example #19
0
    def test_disable_checker(self):
        """
        The test depends on a run which was configured for update mode.
        Compared to the original test analysis in this run
        the deadcode.Deadstores checker was disabled.
        """

        run_results = get_all_run_results(self._cc_client, self._runid)

        print_run_results(run_results)

        # Run the anaysis again with different setup.
        test_project_path = self._testproject_data['project_path']
        ret = project.clean(test_project_path)
        if ret:
            sys.exit(ret)

        initial_codechecker_cfg = env.import_test_cfg(
            self._test_workspace)['codechecker_cfg']

        # Disable some checkers for the analysis.
        deadcode = 'deadcode.DeadStores'
        initial_codechecker_cfg['checkers'] = ['-d', deadcode]

        initial_test_project_name = self._run_name

        ret = codechecker.check(initial_codechecker_cfg,
                                initial_test_project_name, test_project_path)
        if ret:
            sys.exit(1)

        # Get the results to compare.
        updated_results = get_all_run_results(self._cc_client, self._runid)

        all_bugs = self._testproject_data[self._clang_to_test]['bugs']
        deadcode_bugs = [bug for bug in all_bugs if bug['checker'] == deadcode]

        self.assertEquals(len(updated_results),
                          len(all_bugs) - len(deadcode_bugs))
    def test_get_source_file_content(self):
        """
        Test getting the source file content stored to the database.
        Test unicode support the stored file can be decoded properly
        compare results form the database to the original file.
        """

        runid = self._runid
        report_filter = ReportFilter(checkerName=['*'], filepath=['*.c*'])

        run_result_count = self._cc_client.getRunResultCount([runid],
                                                             report_filter,
                                                             None)
        self.assertTrue(run_result_count)

        run_results = get_all_run_results(self._cc_client,
                                          runid,
                                          [],
                                          report_filter)
        self.assertIsNotNone(run_results)

        for run_res in run_results:
            self.assertTrue(re.match(r'.*\.c(pp)?$', run_res.checkedFile))

            logging.debug('Getting the content of ' + run_res.checkedFile)

            file_data = self._cc_client.getSourceFileData(run_res.fileId,
                                                          True,
                                                          None)
            self.assertIsNotNone(file_data)

            file_content1 = file_data.fileContent
            self.assertIsNotNone(file_content1)

            with open(run_res.checkedFile) as source_file:
                file_content2 = source_file.read()

            self.assertEqual(file_content1, file_content2)

            file_data_b64 = self._cc_client.getSourceFileData(
                run_res.fileId, True, Encoding.BASE64)
            self.assertIsNotNone(file_data_b64)

            file_content1_b64 = base64.b64decode(file_data_b64.fileContent)
            self.assertIsNotNone(file_content1_b64)

            self.assertEqual(file_content1_b64, file_content2)

        logging.debug('got ' + str(len(run_results)) + ' files')

        self.assertEqual(run_result_count, len(run_results))
Example #21
0
    def test_get_source_file_content_latin1_encoding(self):
        """ Test if the source file was saved with latin1 encoding.
        Test if the source file can be read back from the
        database even if it was not saved with utf-8 encoding.
        """
        runid = self._runid
        report_filter = ReportFilter(checkerName=['*'],
                                     filepath=['*call_and_message.cpp*'])

        run_result_count = self._cc_client.getRunResultCount([runid],
                                                             report_filter,
                                                             None)
        self.assertTrue(run_result_count)

        run_results = get_all_run_results(self._cc_client,
                                          runid,
                                          [],
                                          report_filter)
        self.assertIsNotNone(run_results)
        self.assertIsNotNone(run_results)

        for run_res in run_results:

            print('Getting the content of ' + run_res.checkedFile)

            file_data = self._cc_client.getSourceFileData(run_res.fileId,
                                                          True,
                                                          None)
            self.assertIsNotNone(file_data)

            file_content1 = file_data.fileContent
            self.assertIsNotNone(file_content1)

            with codecs.open(run_res.checkedFile, 'r', encoding='utf-8',
                             errors='ignore') as source_file:
                file_content2 = source_file.read()
            self.assertEqual(file_content1, file_content2)

            file_data_b64 = self._cc_client.getSourceFileData(
                run_res.fileId, True, Encoding.BASE64)
            self.assertIsNotNone(file_data_b64)

            file_content1_b64 = convert.from_b64(file_data_b64.fileContent)
            self.assertIsNotNone(file_content1_b64)

            self.assertEqual(file_content1_b64, file_content2)

        print('got ' + str(len(run_results)) + ' files')

        self.assertEqual(run_result_count, len(run_results))
Example #22
0
    def test_disable_checker(self):
        """
        The test depends on a run which was configured for update mode.
        Compared to the original test analysis in this run
        the deadcode.Deadstores checker was disabled.
        In this case the reports are marked as resolved.
        """

        run_results = get_all_run_results(self._cc_client, self._runid)

        print_run_results(run_results)

        # Get check command for the first storage.
        original_check_command = \
            self._cc_client.getCheckCommand(None, self._runid)

        self.assertEqual(original_check_command, "")

        initial_codechecker_cfg = env.import_test_cfg(
            self._test_workspace)['codechecker_cfg']

        initial_test_project_name = self._run_name

        disabled_reports = os.path.join(
            self._testproject_data['test_project_reports'], 'disabled')

        initial_codechecker_cfg['reportdir'] = disabled_reports
        ret = codechecker.store(initial_codechecker_cfg,
                                initial_test_project_name)
        if ret:
            sys.exit(1)

        # Get the results to compare.
        updated_results = get_all_run_results(self._cc_client, self._runid)

        for report in updated_results:
            self.assertEqual(report.detectionStatus, DetectionStatus.RESOLVED)
Example #23
0
    def test_get_run_results_no_filter(self):
        """ Get all the run results without any filtering. """
        runid = self._runid
        logging.debug('Get all run results from the db for runid: ' +
                      str(runid))

        run_result_count = self._cc_client.getRunResultCount(runid, [])
        self.assertTrue(run_result_count)

        run_results = get_all_run_results(self._cc_client, runid)

        print_run_results(run_results)

        self.assertIsNotNone(run_results)
        self.assertEqual(run_result_count, len(run_results))
    def test_get_run_results_no_filter(self):
        """ Get all the run results without any filtering. """
        runid = self._runid
        logging.debug('Get all run results from the db for runid: ' +
                      str(runid))

        run_result_count = self._cc_client.getRunResultCount([runid],
                                                             None,
                                                             None)
        self.assertTrue(run_result_count)

        run_results = get_all_run_results(self._cc_client, runid)

        print_run_results(run_results)

        self.assertIsNotNone(run_results)
        self.assertEqual(run_result_count, len(run_results))
Example #25
0
    def test_skip(self):
        """ There should be no results from the skipped file. """

        runid = self._runid
        logging.debug('Get all run results from the db for runid: ' +
                      str(runid))

        run_results = get_all_run_results(self._cc_client, runid)
        self.assertIsNotNone(run_results)

        skipped_files = ["file_to_be_skipped.cpp", "skip.h"]

        test_proj_res = self._testproject_data[self._clang_to_test]['bugs']
        skipped = [x for x in test_proj_res if x['file'] in skipped_files]

        print("Analysis:")
        for res in run_results:
            print(res)

        print("\nTest config results:")
        for res in test_proj_res:
            print(res)

        print("\nTest config skipped results:")
        for res in skipped:
            print(res)

        missing_results = find_all(run_results, test_proj_res)

        print_run_results(run_results)

        print('Missing results:')
        for mr in missing_results:
            print(mr)

        if missing_results:
            for bug in missing_results:
                self.assertIn(bug['file'], skipped_files)
        else:
            self.assertTrue(True,
                            "There should be missing results because"
                            "using skip")

        self.assertEqual(len(run_results), len(test_proj_res) - len(skipped))
Example #26
0
    def test_get_source_file_content(self):
        """
        Test getting the source file content stored to the database.
        Test unicode support the stored file can be decoded properly
        compare results form the database to the original file.
        """

        runid = self._runid
        simple_filters = [ReportFilter(checkerId='*', filepath='*.c*')]

        run_result_count = self._cc_client.getRunResultCount(
            runid, simple_filters)
        self.assertTrue(run_result_count)

        run_results = get_all_run_results(self._cc_client,
                                          runid,
                                          filters=simple_filters)
        self.assertIsNotNone(run_results)

        for run_res in run_results:
            self.assertTrue(re.match(r'.*\.c(pp)?$', run_res.checkedFile))

            logging.debug('Getting the content of ' + run_res.checkedFile)

            file_data = self._cc_client.getSourceFileData(run_res.fileId, True)
            self.assertIsNotNone(file_data)

            file_content1 = file_data.fileContent
            self.assertIsNotNone(file_content1)

            with open(run_res.checkedFile) as source_file:
                file_content2 = source_file.read()

            self.assertEqual(file_content1, file_content2)

        logging.debug('got ' + str(len(run_results)) + ' files')

        self.assertEqual(run_result_count, len(run_results))
    def test_filter_review_status_rules(self):
        """ Test filtering review status rules based on different filters. """
        run_results = get_all_run_results(self._cc_client, self._runid)
        self.assertIsNotNone(run_results)
        self.assertNotEqual(len(run_results), 0)

        bug1 = run_results[0]
        bug2 = next(r for r in run_results if r.bugHash != bug1.bugHash)

        success = self._cc_client.addReviewStatusRule(bug1.bugHash,
                                                      ReviewStatus.CONFIRMED,
                                                      'bug1')
        self.assertTrue(success)

        success = self._cc_client.addReviewStatusRule(
            bug2.bugHash, ReviewStatus.FALSE_POSITIVE, 'bug2')
        self.assertTrue(success)

        rule_filter = None
        sort_mode = None
        limit = None
        offset = 0

        # There are two rules in the database.
        rules = self._cc_client.getReviewStatusRules(rule_filter, sort_mode,
                                                     limit, offset)
        self.assertEqual(len(rules), 2)

        # Show only Confirmed rules.
        rule_filter = ReviewStatusRuleFilter(
            reviewStatuses=[ReviewStatus.CONFIRMED])
        rules = self._cc_client.getReviewStatusRules(rule_filter, sort_mode,
                                                     limit, offset)
        self.assertEqual(len(rules), 1)
        self.assertEqual(rules[0].reviewData.status, ReviewStatus.CONFIRMED)

        self.assertEqual(
            self._cc_client.getReviewStatusRulesCount(rule_filter), 1)

        # Show both Confirmed and False positive rules.
        rule_filter = ReviewStatusRuleFilter(reviewStatuses=[
            ReviewStatus.CONFIRMED, ReviewStatus.FALSE_POSITIVE
        ])
        rules = self._cc_client.getReviewStatusRules(rule_filter, sort_mode,
                                                     limit, offset)
        self.assertEqual(len(rules), 2)
        self.assertEqual(
            self._cc_client.getReviewStatusRulesCount(rule_filter), 2)

        # Filter by report hash.
        rule_filter = ReviewStatusRuleFilter(reportHashes=[bug1.bugHash])
        rules = self._cc_client.getReviewStatusRules(rule_filter, sort_mode,
                                                     limit, offset)
        self.assertEqual(len(rules), 1)
        self.assertEqual(rules[0].reviewData.status, ReviewStatus.CONFIRMED)

        # Add a review status rule with a non-existing report hash.
        success = self._cc_client.addReviewStatusRule('invalid',
                                                      ReviewStatus.INTENTIONAL,
                                                      'bug3')
        self.assertTrue(success)

        rule_filter = ReviewStatusRuleFilter(noAssociatedReports=True)
        rules = self._cc_client.getReviewStatusRules(rule_filter, sort_mode,
                                                     limit, offset)
        self.assertEqual(len(rules), 1)
        self.assertEqual(rules[0].reviewData.status, ReviewStatus.INTENTIONAL)
        self.assertEqual(rules[0].associatedReportCount, 0)
    def test_review_status_update_from_source_trim(self):
        """
        Test if the review status comments changes in the source code
        are updated at the server when trim path is used.

        The report is store twice and between the storage the
        review status as a source code comment is modified.
        The test checks is after the source code modification
        and storage the review status is updated correctly at
        the server too.
        """
        test_project_path = os.path.join(self.test_workspace,
                                         'review_status_files')
        test_project_name = 'review_status_update_proj'

        plist_file = os.path.join(test_project_path, 'divide_zero.plist')
        source_file = os.path.join(test_project_path, 'divide_zero.cpp')
        plist_test.prefix_file_path(plist_file, test_project_path)

        codechecker_cfg = env.import_codechecker_cfg(self.test_workspace)
        codechecker_cfg['reportdir'] = test_project_path

        codechecker.store(codechecker_cfg, test_project_name)

        codechecker_cfg['trim_path_prefix'] = test_project_path

        # Run data for the run created by this test case.
        run_filter = RunFilter(names=[test_project_name], exactMatch=True)

        runs = self._cc_client.getRunData(run_filter, None, 0, None)
        run = runs[0]
        runid = run.runId
        logging.debug('Get all run results from the db for runid: ' +
                      str(runid))

        reports = get_all_run_results(self._cc_client, runid)
        self.assertIsNotNone(reports)
        self.assertNotEqual(len(reports), 0)
        self.assertEqual(len(reports), 2)

        for report in reports:
            print(report)
            self.assertEqual(report.reviewData.status,
                             ReviewStatus.INTENTIONAL)

        # Modify review comments from intentional to confirmed for the
        # second store.
        with open(source_file, 'r+', encoding='utf-8', errors='ignore') as sf:
            content = sf.read()
            new_content = content.replace("codechecker_intentional",
                                          "codechecker_confirmed")
            sf.truncate(0)
            sf.write(new_content)

        # modify review comments and store the reports again
        with open(source_file, encoding='utf-8', errors='ignore') as sf:
            content = sf.read()

        # Update the plist file modification date to be newer than
        # the source file so it can be stored, because there was no
        # actual analysis.
        date = datetime.datetime.now() + datetime.timedelta(minutes=5)
        mod_time = time.mktime(date.timetuple())
        os.utime(plist_file, (mod_time, mod_time))

        codechecker.store(codechecker_cfg, test_project_name)

        # Check if all the review statuses were updated to the new at the
        # server.
        reports = get_all_run_results(self._cc_client, runid)
        self.assertIsNotNone(reports)
        self.assertNotEqual(len(reports), 0)
        self.assertEqual(len(reports), 2)
        for report in reports:
            self.assertEqual(report.reviewData.status, ReviewStatus.CONFIRMED)
    def test_multiple_change_review_status(self):
        """
        Test changing review status of the same bug.
        """
        runid = self._runid
        run_results = get_all_run_results(self._cc_client, runid)
        self.assertIsNotNone(run_results)
        self.assertNotEqual(len(run_results), 0)

        bug = run_results[0]

        self.__remove_all_comments(bug.reportId)

        # There are no system comments for this bug.
        comments = self.__get_system_comments(bug.reportId)
        self.assertEqual(len(comments), 0)

        # Change review status to confirmed bug.
        review_comment = 'This is really a bug'
        status = ReviewStatus.CONFIRMED
        success = self._cc_client.addReviewStatusRule(bug.bugHash, status,
                                                      review_comment)

        self.assertTrue(success)
        logging.debug('Bug review status changed successfully')

        report = self._cc_client.getReport(bug.reportId)
        self.assertEqual(report.reviewData.comment, review_comment)
        self.assertEqual(report.reviewData.status, status)

        # There is one system comment for this bug.
        comments = self.__get_system_comments(bug.reportId)
        self.assertEqual(len(comments), 1)

        # There is only one review status rule in the database.
        rules = self._cc_client.getReviewStatusRules(None, None, None, 0)
        self.assertEqual(len(rules), 1)

        self.assertEqual(rules[0].reportHash, bug.bugHash)
        self.assertEqual(rules[0].reviewData.comment, review_comment)
        self.assertEqual(rules[0].reviewData.status, status)
        self.assertTrue(rules[0].associatedReportCount > 0)

        self.assertEqual(self._cc_client.getReviewStatusRulesCount(None), 1)

        # Try to update the review status again with the same data and check
        # that no new system comment entry will be created.
        success = self._cc_client.addReviewStatusRule(bug.bugHash, status,
                                                      review_comment)
        comments = self.__get_system_comments(bug.reportId)
        self.assertEqual(len(comments), 1)
        self.assertEqual(self._cc_client.getReviewStatusRulesCount(None), 1)

        # Test that updating only the review status message a new system
        # comment will be created.
        success = self._cc_client.addReviewStatusRule(
            bug.bugHash, status, "test system comment change")
        self.assertTrue(success)
        comments = self.__get_system_comments(bug.reportId)
        self.assertEqual(len(comments), 2)
        self.assertEqual(self._cc_client.getReviewStatusRulesCount(None), 1)

        # Try to change review status back to unreviewed.
        status = ReviewStatus.UNREVIEWED
        success = self._cc_client.addReviewStatusRule(bug.bugHash, status,
                                                      None)

        self.assertTrue(success)
        logging.debug("Bug review status changed successfully")

        report = self._cc_client.getReport(bug.reportId)
        self.assertEqual(report.reviewData.comment, '')
        self.assertEqual(report.reviewData.status, status)

        # Change review status to false positive.
        review_comment = 'This is not a bug'
        status = ReviewStatus.FALSE_POSITIVE
        success = self._cc_client.addReviewStatusRule(bug.bugHash, status,
                                                      review_comment)

        self.assertTrue(success)
        logging.debug('Bug review status changed successfully')

        report = self._cc_client.getReport(bug.reportId)
        self.assertEqual(report.reviewData.comment, review_comment)
        self.assertEqual(report.reviewData.status, status)

        # Change review status to intentional.
        review_comment = ''
        status = ReviewStatus.INTENTIONAL
        success = self._cc_client.addReviewStatusRule(bug.bugHash, status,
                                                      review_comment)

        self.assertTrue(success)
        logging.debug('Bug review status changed successfully')

        report = self._cc_client.getReport(bug.reportId)
        self.assertEqual(report.reviewData.comment, review_comment)
        self.assertEqual(report.reviewData.status, status)

        # Make sure that removing a review status rule will change the review
        # status information of a report back to default values.
        self.__remove_all_rules()
        report = self._cc_client.getReport(bug.reportId)
        self.assertEqual(report.reviewData.comment, None)
        self.assertEqual(report.reviewData.author, None)
        self.assertEqual(report.reviewData.date, None)
        self.assertEqual(report.reviewData.status, ReviewStatus.UNREVIEWED)
Example #30
0
    def test_comment(self):
        """
        Test commenting of multiple bugs.
        """

        runid = self._test_runs[0].runId
        logging.debug('Get all run results from the db for runid: ' +
                      str(runid))

        run_results = get_all_run_results(self._cc_client, runid)
        self.assertIsNotNone(run_results)
        self.assertNotEqual(len(run_results), 0)

        bug = run_results[0]

        # There are no comments available for the first bug
        comments = self._cc_client.getComments(bug.reportId)
        self.assertEqual(len(comments), 0)

        num_comment = self._cc_client.getCommentCount(bug.reportId)
        self.assertEqual(num_comment, 0)

        # Try to add a new comment for the first bug
        comment1 = CommentData(author='anybody', message='First msg')
        success = self._cc_client.addComment(bug.reportId, comment1)
        self.assertTrue(success)
        logging.debug('Bug commented successfully')

        # Try to add another new comment for the first bug
        comment2 = CommentData(author='anybody', message='Second msg')
        success = self._cc_client.addComment(bug.reportId, comment2)
        self.assertTrue(success)
        logging.debug('Bug commented successfully')

        # Try to add another new comment with empty message.
        with self.assertRaises(RequestFailed):
            self._cc_client.addComment(bug.reportId,
                                       CommentData(author='anybody'))

        # Add new comment for a second bug with a different hash!
        bug2 = None
        for b in run_results:
            if b.bugHash != bug.bugHash:
                bug2 = b

        comment3 = CommentData(author='anybody', message='Third msg')
        success = self._cc_client.addComment(bug2.reportId, comment3)
        self.assertTrue(success)
        logging.debug('Bug commented successfully')

        # There are two comments available for the first bug
        comments = self._cc_client.getComments(bug.reportId)
        self.assertEqual(len(comments), 2)
        for c in comments:
            self.assertEqual(c.author, 'cc')

        num_comment = self._cc_client.getCommentCount(bug.reportId)
        self.assertEqual(num_comment, 2)

        # Check the order of comments (first comment is the earliest)
        self.assertGreater(comments[0].createdAt, comments[1].createdAt)

        # Remove the first comment
        print("removing comment:" + str(comments[0].id))
        success = self._cc_client.removeComment(comments[0].id)
        self.assertTrue(success)
        logging.debug('Comment removed successfully')

        # Remove the second comment as john should be unsuccessful
        print("removing comment:" + str(comments[1].id))
        with self.assertRaises(RequestFailed):
            self._cc_client_john.removeComment(comments[1].id)
            logging.debug('Comment was removed by another user')

        comments = self._cc_client.getComments(bug.reportId)
        self.assertEqual(len(comments), 1)

        num_comment = self._cc_client.getCommentCount(bug.reportId)
        self.assertEqual(num_comment, 1)

        # Edit the message of the first remaining comment
        new_msg = 'New msg'
        success = self._cc_client.updateComment(comments[0].id, new_msg)
        self.assertTrue(success)
        logging.debug('Comment edited successfully')

        # Update comment with empty message.
        with self.assertRaises(RequestFailed):
            self._cc_client.updateComment(comments[0].id, '       ')

        john_msg = 'John cannot edit'
        with self.assertRaises(RequestFailed):
            self._cc_client_john.updateComment(comments[0].id, john_msg)
            logging.debug('Comment was edited by john')

        comments = self._cc_client.getComments(bug.reportId)
        user_comments, system_comments = separate_comments(comments)

        self.assertEqual(len(user_comments), 1)
        self.assertEqual(user_comments[0].message, new_msg)
        self.assertEqual(len(system_comments), 1)

        # Remove the last comment for the first bug
        success = self._cc_client.removeComment(user_comments[0].id)
        self.assertTrue(success)
        logging.debug('Comment removed successfully')

        comments = self._cc_client.getComments(bug.reportId)
        self.assertEqual(len(comments), 1)
        self.assertEqual(comments[0].kind, CommentKind.SYSTEM)

        num_comment = self._cc_client.getCommentCount(bug.reportId)
        self.assertEqual(num_comment, 1)

        # Remove the last comment for the second bug.
        comments = self._cc_client.getComments(bug2.reportId)
        self.assertEqual(len(comments), 1)

        success = self._cc_client.removeComment(comments[0].id)
        self.assertTrue(success)
        logging.debug('Comment removed successfully')

        comments = self._cc_client.getComments(bug2.reportId)
        self.assertEqual(len(comments), 0)

        num_comment = self._cc_client.getCommentCount(bug2.reportId)
        self.assertEqual(num_comment, 0)
Example #31
0
    def test_suppress_comment_in_db(self):
        """
        Exported source suppress comment stored as a review status in the db.
        """
        runid = self._runid
        logging.debug("Get all run results from the db for runid: " +
                      str(runid))

        hash_to_suppress_msgs = {}
        with open(os.path.join(self._test_project_path, "suppress.expected"),
                  'r', encoding="utf-8", errors="ignore") as expected:
            for line in expected:
                src_code_info = line.strip().split('||')

                status = None
                if len(src_code_info) == 4:
                    # Newest source code comment format where status is given.
                    bug_hash, _, msg, status = src_code_info
                elif len(src_code_info) == 3:
                    # Old format where review status is not given.
                    bug_hash, _, msg = src_code_info
                else:
                    # Oldest source code comment format where status and file
                    # name are not given.
                    bug_hash, msg = src_code_info

                rw_status = ReviewStatus.FALSE_POSITIVE
                if status == 'confirmed':
                    rw_status = ReviewStatus.CONFIRMED
                elif status == 'intentional':
                    rw_status = ReviewStatus.INTENTIONAL

                hash_to_suppress_msgs[bug_hash] = {'message': msg,
                                                   'status': rw_status}

        run_results = get_all_run_results(self._cc_client, runid)
        self.assertIsNotNone(run_results)
        self.assertNotEqual(len(run_results), 0)

        for bug_hash in hash_to_suppress_msgs:
            expected = hash_to_suppress_msgs[bug_hash]
            report = [x for x in run_results if x.bugHash == bug_hash][0]

            # Check the stored suppress comment
            self.assertEqual(report.reviewData.comment, expected['message'])
            self.assertEqual(report.reviewData.status, expected['status'])

            # Change review status to confirmed bug.
            review_comment = "This is really a bug"
            status = ReviewStatus.CONFIRMED
            success = self._cc_client.changeReviewStatus(
                report.reportId, status, review_comment)

            self.assertTrue(success)
            logging.debug("Bug review status changed successfully")

        # Get the results to compare.
        updated_results = get_all_run_results(self._cc_client, self._runid)
        self.assertIsNotNone(updated_results)
        self.assertNotEqual(len(updated_results), 0)

        for bug_hash in hash_to_suppress_msgs:
            report = [x for x in updated_results if x.bugHash == bug_hash][0]

            # Check the stored suppress comment
            self.assertEqual(report.reviewData.comment, "This is really a bug")
            self.assertEqual(report.reviewData.status, ReviewStatus.CONFIRMED)

        # Check the same project again.
        codechecker_cfg = env.import_test_cfg(
            self._test_workspace)['codechecker_cfg']

        initial_test_project_name = self._run_name

        ret = codechecker.check_and_store(codechecker_cfg,
                                          initial_test_project_name,
                                          self._test_project_path)
        if ret:
            sys.exit(1)

        # Get the results to compare.
        updated_results = get_all_run_results(self._cc_client, self._runid)
        self.assertIsNotNone(updated_results)
        self.assertNotEqual(len(updated_results), 0)

        for bug_hash in hash_to_suppress_msgs:
            expected = hash_to_suppress_msgs[bug_hash]
            report = [x for x in updated_results if x.bugHash == bug_hash][0]

            # Check that source code comments in the database are changed back
            # after storage.
            self.assertEqual(report.reviewData.comment, expected['message'])
            self.assertEqual(report.reviewData.status, expected['status'])
    def test_multiple_change_review_status(self):
        """
        Test changing review status of the same bug.
        """
        runid = self._runid
        logging.debug('Get all run results from the db for runid: ' +
                      str(runid))

        run_results = get_all_run_results(self._cc_client, runid)
        self.assertIsNotNone(run_results)
        self.assertNotEqual(len(run_results), 0)

        bug = run_results[0]

        # Change review status to confirmed bug.
        review_comment = 'This is really a bug'
        status = ReviewStatus.CONFIRMED
        success = self._cc_client.changeReviewStatus(bug.reportId, status,
                                                     review_comment)

        self.assertTrue(success)
        logging.debug('Bug review status changed successfully')

        report = self._cc_client.getReport(bug.reportId)
        self.assertEqual(report.reviewData.comment, review_comment)
        self.assertEqual(report.reviewData.status, status)

        # Try to change review status back to unreviewed.
        status = ReviewStatus.UNREVIEWED
        success = self._cc_client.changeReviewStatus(bug.reportId, status, u'')

        self.assertTrue(success)
        logging.debug("Bug review status changed successfully")

        report = self._cc_client.getReport(bug.reportId)
        self.assertEqual(report.reviewData.comment, '')
        self.assertEqual(report.reviewData.status, status)

        # Change review status to false positive.
        review_comment = 'This is not a bug'
        status = ReviewStatus.FALSE_POSITIVE
        success = self._cc_client.changeReviewStatus(bug.reportId, status,
                                                     review_comment)

        self.assertTrue(success)
        logging.debug('Bug review status changed successfully')

        report = self._cc_client.getReport(bug.reportId)
        self.assertEqual(report.reviewData.comment, review_comment)
        self.assertEqual(report.reviewData.status, status)

        # Change review status to intentional.
        review_comment = u''
        status = ReviewStatus.INTENTIONAL
        success = self._cc_client.changeReviewStatus(bug.reportId, status,
                                                     review_comment)

        self.assertTrue(success)
        logging.debug('Bug review status changed successfully')

        report = self._cc_client.getReport(bug.reportId)
        self.assertEqual(report.reviewData.comment, review_comment)
        self.assertEqual(report.reviewData.status, status)
    def test_export_import(self):
        """
        Test exporting and importing feature
        """
        run_filter = RunFilter()
        run_filter.names = [self.test_runs[0].name]
        logging.debug('Get all run results from the db for runid: ' +
                      str(self.test_runs[0].runId))

        run_results = get_all_run_results(self._cc_client,
                                          self.test_runs[0].runId)
        self.assertIsNotNone(run_results)
        self.assertNotEqual(len(run_results), 0)

        bug = run_results[0]

        # There are no comments available for the first bug
        comments = self._cc_client.getComments(bug.reportId)
        self.assertEqual(len(comments), 0)

        comment1 = CommentData(author='anybody', message='First msg')
        success = self._cc_client.addComment(bug.reportId, comment1)
        self.assertTrue(success)
        logging.debug('Bug commented successfully')

        # Try to add another new comment for the first bug
        comment2 = CommentData(author='anybody', message='Second msg')
        success = self._cc_client.addComment(bug.reportId, comment2)
        self.assertTrue(success)
        logging.debug('Bug commented successfully')

        # Change review status
        review_comment = 'This is really a bug'
        status = ReviewStatus.CONFIRMED
        success = self._cc_client.changeReviewStatus(bug.reportId, status,
                                                     review_comment)

        self.assertTrue(success)
        logging.debug('Bug review status changed successfully')

        # Export the comments and reviews
        exported = self._cc_client.exportData(run_filter)
        exported_comments = exported.comments[bug.bugHash]
        exported_review = exported.reviewData[bug.bugHash]

        # Check if the comments are same
        user_comments = get_user_comments(exported_comments)
        self.assertEqual(user_comments[0].message, comment2.message)
        self.assertEqual(user_comments[1].message, comment1.message)

        # Check for the review status
        self.assertEqual(exported_review.status, status)
        self.assertEqual(exported_review.comment, review_comment)

        new_export_command = [
            self._codechecker_cmd, 'cmd', 'export', '-n',
            self.test_runs[0].name, '--url',
            str(self.server_url)
        ]

        print(new_export_command)
        out_json = subprocess.check_output(new_export_command,
                                           encoding="utf-8",
                                           errors="ignore")
        resolved_results = json.loads(out_json)
        print(resolved_results)
        with tempfile.NamedTemporaryFile() as component_f:
            component_f.write(out_json.encode('utf-8'))
            component_f.flush()
            component_f.seek(0)

            self._cc_client.removeComment(user_comments[0].id)
            updated_message = "The new comment for testing"
            self._cc_client.updateComment(user_comments[1].id, updated_message)

            new_import_command = [
                self._codechecker_cmd, 'cmd', 'import', '-i', component_f.name,
                '--url',
                str(self.server_url)
            ]
            print(new_import_command)
            subprocess.check_output(new_import_command,
                                    encoding="utf-8",
                                    errors="ignore")

            exported = self._cc_client.exportData(run_filter)
            new_comments = exported.comments[bug.bugHash]
            print(new_comments)

            # Check if the last and second last comments
            self.assertEqual(new_comments[-1].message, comment1.message)
            self.assertEqual(new_comments[-2].message, updated_message)
Example #34
0
    def test_assign_reports_to_cleanup_plans(self):
        """ Test assigning reports to cleanup plans. """
        name = "Test"
        description = None
        due_date = None

        cleanup_plan_id = self._cc_client.addCleanupPlan(
            name, description, due_date)

        # Get all cleanup plans.
        cleanup_plans = self._cc_client.getCleanupPlans(None)
        self.assertEqual(len(cleanup_plans), 1)

        cleanup_plan = cleanup_plans[0]
        self.assertEqual(cleanup_plan.reportHashes, [])

        # Get results from the database.
        run_results = get_all_run_results(self._cc_client)
        self.assertTrue(run_results)

        uniqued_report_hashes = set(r.bugHash for r in run_results)
        self.assertTrue(len(uniqued_report_hashes) > 2)

        it = iter(uniqued_report_hashes)
        report_hash_1 = next(it)
        report_hash_2 = next(it)
        report_hashes = [report_hash_1, report_hash_2]

        # Assign report to cleanup.
        success = self._cc_client.setCleanupPlan(
            cleanup_plan_id, report_hashes)
        self.assertTrue(success)

        cleanup_plans = self._cc_client.getCleanupPlans(None)
        cleanup_plan = cleanup_plans[0]
        self.assertCountEqual(
            cleanup_plan.reportHashes, report_hashes)

        # Filter reports based on cleanup plan.
        report_filter = ReportFilter(cleanupPlanNames=[name])
        run_results = get_all_run_results(
            self._cc_client, filters=report_filter)
        self.assertTrue(run_results)

        for report in run_results:
            self.assertTrue(report.bugHash in report_hashes,
                            f"{report.bugHash} not found in cleanup plan.")

        # Remove report hash from cleanup plan.
        success = self._cc_client.unsetCleanupPlan(
            cleanup_plan_id, [report_hash_2])
        self.assertTrue(success)

        cleanup_plans = self._cc_client.getCleanupPlans(None)
        cleanup_plan = cleanup_plans[0]
        self.assertCountEqual(
            cleanup_plan.reportHashes, [report_hash_1])

        success = self._cc_client.unsetCleanupPlan(
            cleanup_plan_id, [report_hash_1])
        self.assertTrue(success)

        cleanup_plans = self._cc_client.getCleanupPlans(None)
        cleanup_plan = cleanup_plans[0]
        self.assertFalse(cleanup_plan.reportHashes)

        # Remove cleanup.
        success = self._cc_client.removeCleanupPlan(cleanup_plan_id)
        self.assertTrue(success)
    def test_multiple_change_review_status(self):
        """
        Test changing review status of the same bug.
        """
        runid = self._runid
        logging.debug('Get all run results from the db for runid: ' +
                      str(runid))

        run_results = get_all_run_results(self._cc_client, runid)
        self.assertIsNotNone(run_results)
        self.assertNotEqual(len(run_results), 0)

        bug = run_results[0]

        # There are no system comments for this bug.
        comments = self.__get_system_comments(bug.reportId)
        self.assertEqual(len(comments), 0)

        # Change review status to confirmed bug.
        review_comment = 'This is really a bug'
        status = ReviewStatus.CONFIRMED
        success = self._cc_client.changeReviewStatus(bug.reportId, status,
                                                     review_comment)

        self.assertTrue(success)
        logging.debug('Bug review status changed successfully')

        report = self._cc_client.getReport(bug.reportId)
        self.assertEqual(report.reviewData.comment, review_comment)
        self.assertEqual(report.reviewData.status, status)

        # There is one system comment for this bug.
        comments = self.__get_system_comments(bug.reportId)
        self.assertEqual(len(comments), 1)

        # Try to update the review status again with the same data and check
        # that no new system comment entry will be created.
        success = self._cc_client.changeReviewStatus(bug.reportId, status,
                                                     review_comment)
        comments = self.__get_system_comments(bug.reportId)
        self.assertEqual(len(comments), 1)

        # Test that updating only the review status message a new system
        # comment will be created.
        success = self._cc_client.changeReviewStatus(
            bug.reportId, status, "test system comment change")
        self.assertTrue(success)
        comments = self.__get_system_comments(bug.reportId)
        self.assertEqual(len(comments), 2)

        # Try to change review status back to unreviewed.
        status = ReviewStatus.UNREVIEWED
        success = self._cc_client.changeReviewStatus(bug.reportId, status,
                                                     None)

        self.assertTrue(success)
        logging.debug("Bug review status changed successfully")

        report = self._cc_client.getReport(bug.reportId)
        self.assertEqual(report.reviewData.comment, '')
        self.assertEqual(report.reviewData.status, status)

        # Change review status to false positive.
        review_comment = 'This is not a bug'
        status = ReviewStatus.FALSE_POSITIVE
        success = self._cc_client.changeReviewStatus(bug.reportId, status,
                                                     review_comment)

        self.assertTrue(success)
        logging.debug('Bug review status changed successfully')

        report = self._cc_client.getReport(bug.reportId)
        self.assertEqual(report.reviewData.comment, review_comment)
        self.assertEqual(report.reviewData.status, status)

        # Change review status to intentional.
        review_comment = ''
        status = ReviewStatus.INTENTIONAL
        success = self._cc_client.changeReviewStatus(bug.reportId, status,
                                                     review_comment)

        self.assertTrue(success)
        logging.debug('Bug review status changed successfully')

        report = self._cc_client.getReport(bug.reportId)
        self.assertEqual(report.reviewData.comment, review_comment)
        self.assertEqual(report.reviewData.status, status)
    def test_multiple_change_review_status(self):
        """
        Test changing review status of the same bug.
        """
        runid = self._runid
        logging.debug('Get all run results from the db for runid: ' +
                      str(runid))

        run_results = get_all_run_results(self._cc_client, runid)
        self.assertIsNotNone(run_results)
        self.assertNotEqual(len(run_results), 0)

        bug = run_results[0]

        # Change review status to confirmed bug.
        review_comment = 'This is really a bug'
        status = ReviewStatus.CONFIRMED
        success = self._cc_client.changeReviewStatus(
            bug.reportId, status, review_comment)

        self.assertTrue(success)
        logging.debug('Bug review status changed successfully')

        report = self._cc_client.getReport(bug.reportId)
        self.assertEqual(report.reviewData.comment, review_comment)
        self.assertEqual(report.reviewData.status, status)

        # Try to change review status back to unreviewed.
        status = ReviewStatus.UNREVIEWED
        success = self._cc_client.changeReviewStatus(
            bug.reportId,
            status,
            u'')

        self.assertTrue(success)
        logging.debug("Bug review status changed successfully")

        report = self._cc_client.getReport(bug.reportId)
        self.assertEqual(report.reviewData.comment, '')
        self.assertEqual(report.reviewData.status, status)

        # Change review status to false positive.
        review_comment = 'This is not a bug'
        status = ReviewStatus.FALSE_POSITIVE
        success = self._cc_client.changeReviewStatus(
            bug.reportId, status, review_comment)

        self.assertTrue(success)
        logging.debug('Bug review status changed successfully')

        report = self._cc_client.getReport(bug.reportId)
        self.assertEqual(report.reviewData.comment, review_comment)
        self.assertEqual(report.reviewData.status, status)

        # Change review status to intentional.
        review_comment = u''
        status = ReviewStatus.INTENTIONAL
        success = self._cc_client.changeReviewStatus(
            bug.reportId, status, review_comment)

        self.assertTrue(success)
        logging.debug('Bug review status changed successfully')

        report = self._cc_client.getReport(bug.reportId)
        self.assertEqual(report.reviewData.comment, review_comment)
        self.assertEqual(report.reviewData.status, status)
    def test_suppress_comment_in_db(self):
        """
        Exported source suppress comment stored as a review status in the db.
        """
        runid = self._runid
        logging.debug("Get all run results from the db for runid: " +
                      str(runid))

        hash_to_suppress_msgs = {}
        with open(os.path.join(self._test_project_path,
                               "suppress.expected"), 'r') as expected:
            for line in expected:
                src_code_info = line.strip().split('||')

                status = None
                if len(src_code_info) == 4:
                    # Newest source code comment format where status is given.
                    bug_hash, _, msg, status = src_code_info
                elif len(src_code_info) == 3:
                    # Old format where review status is not given.
                    bug_hash, _, msg = src_code_info
                else:
                    # Oldest source code comment format where status and file
                    # name are not given.
                    bug_hash, msg = src_code_info

                rw_status = ReviewStatus.FALSE_POSITIVE
                if status == 'confirmed':
                    rw_status = ReviewStatus.CONFIRMED
                elif status == 'intentional':
                    rw_status = ReviewStatus.INTENTIONAL

                hash_to_suppress_msgs[bug_hash] = {'message': msg,
                                                   'status': rw_status}

        run_results = get_all_run_results(self._cc_client, runid)
        self.assertIsNotNone(run_results)
        self.assertNotEqual(len(run_results), 0)

        for bug_hash in hash_to_suppress_msgs:
            expected = hash_to_suppress_msgs[bug_hash]
            report = [x for x in run_results if x.bugHash == bug_hash][0]

            # Check the stored suppress comment
            self.assertEqual(report.reviewData.comment, expected['message'])
            self.assertEqual(report.reviewData.status, expected['status'])

            # Change review status to confirmed bug.
            review_comment = "This is really a bug"
            status = ReviewStatus.CONFIRMED
            success = self._cc_client.changeReviewStatus(
                report.reportId, status, review_comment)

            self.assertTrue(success)
            logging.debug("Bug review status changed successfully")

        # Get the results to compare.
        updated_results = get_all_run_results(self._cc_client, self._runid)
        self.assertIsNotNone(updated_results)
        self.assertNotEqual(len(updated_results), 0)

        for bug_hash in hash_to_suppress_msgs:
            report = [x for x in updated_results if x.bugHash == bug_hash][0]

            # Check the stored suppress comment
            self.assertEqual(report.reviewData.comment, "This is really a bug")
            self.assertEqual(report.reviewData.status, ReviewStatus.CONFIRMED)

        # Check the same project again.
        codechecker_cfg = env.import_test_cfg(
            self._test_workspace)['codechecker_cfg']

        initial_test_project_name = self._run_name

        ret = codechecker.check(codechecker_cfg,
                                initial_test_project_name,
                                self._test_project_path)
        if ret:
            sys.exit(1)

        # Get the results to compare.
        updated_results = get_all_run_results(self._cc_client, self._runid)
        self.assertIsNotNone(updated_results)
        self.assertNotEqual(len(updated_results), 0)

        for bug_hash in hash_to_suppress_msgs:
            expected = hash_to_suppress_msgs[bug_hash]
            report = [x for x in updated_results if x.bugHash == bug_hash][0]

            # Check that source code comments in the database are changed back
            # after storage.
            self.assertEqual(report.reviewData.comment, expected['message'])
            self.assertEqual(report.reviewData.status, expected['status'])
Example #38
0
    def test_comment(self):
        """
        Test commenting of multiple bugs.
        """

        runid = self._test_runs[0].runId
        logging.debug('Get all run results from the db for runid: ' +
                      str(runid))

        run_results = get_all_run_results(self._cc_client, runid)
        self.assertIsNotNone(run_results)
        self.assertNotEqual(len(run_results), 0)

        bug = run_results[0]

        # There are no comments available for the first bug
        comments = self._cc_client.getComments(bug.reportId)
        self.assertEqual(len(comments), 0)

        num_comment = self._cc_client.getCommentCount(bug.reportId)
        self.assertEqual(num_comment, 0)

        # Try to add a new comment for the first bug
        comment1 = CommentData(author='anybody', message='First msg')
        success = self._cc_client.addComment(bug.reportId, comment1)
        self.assertTrue(success)
        logging.debug('Bug commented successfully')

        # Try to add another new comment for the first bug
        comment2 = CommentData(author='anybody', message='Second msg')
        success = self._cc_client.addComment(bug.reportId, comment2)
        self.assertTrue(success)
        logging.debug('Bug commented successfully')

        # Add new comment for a second bug with a different hash!
        bug2 = None
        for b in run_results:
            if b.bugHash != bug.bugHash:
                bug2 = b

        comment3 = CommentData(author='anybody', message='Third msg')
        success = self._cc_client.addComment(bug2.reportId, comment3)
        self.assertTrue(success)
        logging.debug('Bug commented successfully')

        # There are two comments available for the first bug
        comments = self._cc_client.getComments(bug.reportId)
        self.assertEqual(len(comments), 2)
        for c in comments:
            self.assertEqual(c.author, 'cc')

        num_comment = self._cc_client.getCommentCount(bug.reportId)
        self.assertEqual(num_comment, 2)

        # Check the order of comments (first comment is the earliest)
        self.assertGreater(comments[0].createdAt, comments[1].createdAt)

        # Remove the first comment
        print("removing comment:"+str(comments[0].id))
        success = self._cc_client.removeComment(comments[0].id)
        self.assertTrue(success)
        logging.debug('Comment removed successfully')

        # Remove the second comment as john should be unsuccessful
        print("removing comment:"+str(comments[1].id))
        with self.assertRaises(RequestFailed):
            self._cc_client_john.removeComment(comments[1].id)
            logging.debug('Comment was removed by another user')

        comments = self._cc_client.getComments(bug.reportId)
        self.assertEqual(len(comments), 1)

        num_comment = self._cc_client.getCommentCount(bug.reportId)
        self.assertEqual(num_comment, 1)

        # Edit the message of the first remaining comment
        new_msg = 'New msg'
        success = self._cc_client.updateComment(comments[0].id, new_msg)
        self.assertTrue(success)
        logging.debug('Comment edited successfully')

        john_msg = 'John cannot edit'
        with self.assertRaises(RequestFailed):
            self._cc_client_john.updateComment(comments[0].id, john_msg)
            logging.debug('Comment was edited by john')

        comments = self._cc_client.getComments(bug.reportId)
        self.assertEqual(len(comments), 1)
        self.assertEqual(comments[0].message, new_msg)

        # Remove the last comment for the first bug
        success = self._cc_client.removeComment(comments[0].id)
        self.assertTrue(success)
        logging.debug('Comment removed successfully')

        comments = self._cc_client.getComments(bug.reportId)
        self.assertEqual(len(comments), 0)

        num_comment = self._cc_client.getCommentCount(bug.reportId)
        self.assertEqual(num_comment, 0)

        # Remove the last comment for the second bug.
        comments = self._cc_client.getComments(bug2.reportId)
        self.assertEqual(len(comments), 1)

        success = self._cc_client.removeComment(comments[0].id)
        self.assertTrue(success)
        logging.debug('Comment removed successfully')

        comments = self._cc_client.getComments(bug2.reportId)
        self.assertEqual(len(comments), 0)

        num_comment = self._cc_client.getCommentCount(bug2.reportId)
        self.assertEqual(num_comment, 0)