예제 #1
0
    def setup(self):
        self.project = create_project("-p-")
        self.misuse = create_misuse("-m-", project=self.project)
        self.version = create_version("-v-", project=self.project, misuses=[self.misuse],
                                      meta={"build": {"src": "", "classes": ""}})
        self.version_compile = self.version.get_compile("/sources")

        self.test_detector_execution = MagicMock()  # type: DetectorRun
        self.test_detector_execution.is_success = lambda: False
        self.test_detector_execution.is_error = lambda: False
        self.test_detector_execution.is_timeout = lambda: False
        self.test_detector_execution.runtime = 0
        self.test_detector_execution.number_of_findings = 0
        self.test_run_timestamp = 1337
        self.test_detector_execution.get_run_info = lambda: {
            'number_of_findings': self.test_detector_execution.number_of_findings,
            'runtime': self.test_detector_execution.runtime,
            'timestamp': self.test_run_timestamp
        }
        self.test_detector_execution.findings_path = "-findings-"

        self.created_files_per_finding = dict()

        self.detector = StubDetector()  # type: Detector
        self.detector.id = "test_detector"

        self.experiment_id = "ex1"

        self.test_potential_hits = PotentialHits([])

        self.uut = PublishFindingsTask(self.experiment_id, "/sources", "http://dummy.url", "-username-", "-password-")
예제 #2
0
    def test_publish_successful_run_code_snippets_extraction_fails(self, convert_mock, post_mock, _):
        self.test_detector_execution.is_success = lambda: True
        finding = self._create_finding({"rank": "42"}, convert_mock)
        finding.get_snippets = MagicMock(return_value=[])
        self.test_potential_hits = PotentialHits([finding])

        self.uut.run(self.project, self.version, self.test_detector_execution, self.test_potential_hits,
                     self.version_compile, self.detector)

        assert_equals([], post_mock.call_args[0][1]["potential_hits"][0]["target_snippets"])
예제 #3
0
    def test_publish_successful_run_code_snippets(self, convert_mock, post_mock, _):
        self.test_detector_execution.is_success = lambda: True
        self.test_potential_hits = PotentialHits(
            [self._create_finding({"rank": "42"}, convert_mock, snippets=[Snippet("-code-", 23)])])

        self.uut.run(self.project, self.version, self.test_detector_execution, self.test_potential_hits,
                     self.version_compile, self.detector)

        assert_equals([{"code": "-code-", "first_line_number": 23}],
                      post_mock.call_args[0][1]["potential_hits"][0]["target_snippets"])
예제 #4
0
    def test_with_markdown(self, convert_mock, post_mock, _):
        self.test_detector_execution.is_success = lambda: True
        potential_hits = [self._create_finding({"list": ["hello", "world"], "dict": {"key": "value"}}, convert_mock)]
        self.test_potential_hits = PotentialHits(potential_hits)
        self.test_detector_execution.get_run_info = lambda: {"info": {"k1": "v1"}, "timestamp": 0}

        self.uut.run(self.project, self.version, self.test_detector_execution, self.test_potential_hits,
                     self.version_compile, self.detector)

        assert_equals("k1: \nv1", post_mock.call_args[0][1]["info"])
        assert_equals([{"list": "* hello\n* world", "dict": "key: \nvalue", "target_snippets": []}],
                      post_mock.call_args[0][1]["potential_hits"])
예제 #5
0
    def test_publish_successful_run_in_partial_chunks(self, convert_mock, post_mock, _):
        self.uut.max_files_per_post = 3
        self.test_detector_execution.is_success = lambda: True
        self.test_potential_hits = PotentialHits([
            self._create_finding({"rank": "-1-"}, convert_mock, file_paths=["-file1-", "-file2-"]),
            self._create_finding({"rank": "-2-"}, convert_mock, file_paths=["-file3-", "-file4-"])
        ])

        self.uut.run(self.project, self.version, self.test_detector_execution, self.test_potential_hits,
                     self.version_compile, self.detector)

        assert_equals(2, len(post_mock.call_args_list))
예제 #6
0
    def test_publish_successful_run_convert_graphs_and_replace_by_file_name(self, create_image_mock, post_mock, __):
        self.test_detector_execution.is_success = lambda: True

        self.test_potential_hits = PotentialHits([
            self._create_finding({"rank": "-1-", "graph": "digraph G {}"})
        ])

        self.uut.run(self.project, self.version, self.test_detector_execution, self.test_potential_hits,
                     self.version_compile, self.detector)

        create_image_mock.assert_called_once_with("digraph G {}", self.test_detector_execution.findings_path, "f-1--Z3JhcGg=.svg")
        assert_equals("f-1--Z3JhcGg=.svg", post_mock.call_args[0][1]["potential_hits"][0]["graph"])
예제 #7
0
    def test_publish_successful_run_files(self, convert_mock, post_mock, _):
        self.test_detector_execution.is_success = lambda: True

        self.test_potential_hits = PotentialHits([
            self._create_finding({"rank": "-1-"}, convert_mock, file_paths=["-file1-"]),
            self._create_finding({"rank": "-2-"}, convert_mock, file_paths=["-file2-"])
        ])

        self.uut.run(self.project, self.version, self.test_detector_execution, self.test_potential_hits,
                     self.version_compile, self.detector)

        assert_equals(["-file1-", "-file2-"], post_mock.call_args[1]["file_paths"])
예제 #8
0
    def test_publish_successful_run_in_sized_chunks(self, convert_mock, post_mock, get_potential_hit_size_mock):
        self.uut.max_post_size_in_bytes = 1500
        get_potential_hit_size_mock.return_value = 1024
        self.test_detector_execution.is_success = lambda: True
        self.test_potential_hits = PotentialHits([
            self._create_finding({"rank": "-1-"}, convert_mock),
            self._create_finding({"rank": "-2-"}, convert_mock)
        ])

        self.uut.run(self.project, self.version, self.test_detector_execution, self.test_potential_hits,
                     self.version_compile, self.detector)

        assert_equals(len(post_mock.call_args_list), 2)
예제 #9
0
    def test_add_at_least_one_hit_to_chunk(self, convert_mock, post_mock, get_potential_hit_size_mock):
        self.uut.max_post_size_in_bytes = 0
        self.uut.max_files_per_post = 0
        get_potential_hit_size_mock.return_value = 1
        self.test_detector_execution.is_success = lambda: True
        self.test_potential_hits = PotentialHits([
            self._create_finding({"rank": "-1-"}, convert_mock, file_paths=["-file1-"])
        ])

        self.uut.run(self.project, self.version, self.test_detector_execution, self.test_potential_hits,
                     self.version_compile, self.detector)

        assert_equals(len(post_mock.call_args_list), 1)
        assert_equals([{"rank": "-1-", "target_snippets": []}], post_mock.call_args[0][1]["potential_hits"])
예제 #10
0
    def test_converts_directed_graphs(self, replace_mock, post_mock, _):
        self.test_detector_execution.is_success = lambda: True

        self.test_potential_hits = PotentialHits([
            self._create_finding({"graph": 'digraph G {\n' +
                                           '1 [label="JButton#button#addActionListener" shape=box style=rounded]\n' +
                                           '2 [label="JButton#button#setText" shape=box style=rounded]\n' +
                                           '3 [label="ActionListener#ActionListener#new" shape=box style=rounded]\n' +
                                           '2 -> 1 [label=""];\n' +
                                           '2 -> 3 [label=""];\n' +
                                           '3 -> 1 [label=""];\n' +
                                           '}\n'})
        ])
        replace_mock.side_effect = lambda finding, key, path: path + "/" + key

        self.uut.run(self.project, self.version, self.test_detector_execution, self.test_potential_hits,
                     self.version_compile, self.detector)

        assert_equals([self.test_detector_execution.findings_path + "/graph"], post_mock.call_args[1]["file_paths"])
예제 #11
0
    def test_publish_successful_run(self, convert_mock, post_mock, _):
        self.test_detector_execution.is_success = lambda: True
        self.test_detector_execution.runtime = 42
        self.test_detector_execution.number_of_findings = 5
        potential_hits = [
            self._create_finding({"rank": "-1-"}, convert_mock),
            self._create_finding({"rank": "-2-"}, convert_mock)
        ]
        self.test_potential_hits = PotentialHits(potential_hits)

        self.uut.run(self.project, self.version, self.test_detector_execution, self.test_potential_hits,
                     self.version_compile, self.detector)

        assert_equals({
            "result": "success",
            "runtime": 42.0,
            "number_of_findings": 5,
            "potential_hits": [
                {"rank": "-1-", "target_snippets": []},
                {"rank": "-2-", "target_snippets": []}
            ],
            "timestamp": self.test_run_timestamp
        }, post_mock.call_args[0][1])