예제 #1
0
    def test_try_filetype_with_coverage_via_afl_fail(
            self, subprocess_check_output: unittest.mock.MagicMock):
        binary_path = "tshark"
        repo_path = ""
        max_timeout = 2500
        memory_limit = "none"

        h = HeuristicConfigCreator(binary_path=binary_path,
                                   repo_path=repo_path,
                                   dummyfiles_path=os.getcwd())
        h.MAX_TIMEOUT = max_timeout
        h.memory_limit = memory_limit
        h.FAILED_INVOCATIONS_THRESHOLD = 2
        h.afl_cmin_path = "afl-cmin"
        mocked_return_value = "cmin-output"
        subprocess_check_output.return_value = bytes(mocked_return_value,
                                                     encoding="utf-8")
        subprocess_check_output.side_effect = subprocess.CalledProcessError(
            returncode=-1, cmd="alja")
        h.try_filetype_with_coverage_via_afl("-f",
                                             os.getcwd(),
                                             file_type=".test")
        with self.assertRaises(helpers.exceptions.NoCoverageInformation):
            h.try_filetype_with_coverage_via_afl("-g",
                                                 os.getcwd(),
                                                 file_type=".test")
예제 #2
0
    async def test_prepare(
        self,
        mock_instance_repo: unittest.mock.MagicMock,
    ) -> None:
        async def _run_sub_test(
            wait_for_containers: bool,
            expected_container_status: ContainerInstanceStatus,
        ) -> None:
            with patch.object(
                    CppUnionPIDDataPreparerService,
                    "prepare_on_container_async"
            ) as mock_prepare_on_container_async, patch.object(
                    PIDStage, "update_instance_containers"):
                container = ContainerInstance(
                    instance_id="123",
                    ip_address="192.0.2.0",
                    status=expected_container_status,
                )
                mock_prepare_on_container_async.return_value = container
                stage = PIDPrepareStage(
                    stage=UnionPIDStage.PUBLISHER_PREPARE,
                    instance_repository=mock_instance_repo,
                    storage_svc="STORAGE",  # pyre-ignore
                    onedocker_svc="ONEDOCKER",  # pyre-ignore
                    onedocker_binary_config=MagicMock(
                        task_definition="offline-task:1#container",
                        tmp_directory="/tmp/",
                        binary_version="latest",
                    ),
                )

                res = await stage.prepare(
                    instance_id="123",
                    input_path="in",
                    output_path="out",
                    num_shards=1,
                    wait_for_containers=wait_for_containers,
                )
                self.assertEqual(
                    PIDStage.get_stage_status_from_containers([container]),
                    res,
                )

        data_tests = (
            (True, ContainerInstanceStatus.COMPLETED),
            (True, ContainerInstanceStatus.FAILED),
            (False, ContainerInstanceStatus.STARTED),
        )
        for data_test in data_tests:
            with self.subTest(
                    wait_for_containers=data_test[0],
                    expected_container_status=data_test[1],
            ):
                # reset mocks for each subTests
                mock_instance_repo.reset_mock()
                await _run_sub_test(
                    data_test[0],
                    data_test[1],
                )
예제 #3
0
파일: test_helpers.py 프로젝트: fgsect/fexm
 def test_check_and_wait_for_rate_limit(
         self, time_sleep: unittest.mock.MagicMock):
     with unittest.mock.patch('helpers.helpers.get_utc_now',
                              return_value=datetime(2017, 10, 18, 1, 30, 0,
                                                    0)):
         utils.wait_for_rate_limit("2017-10-18T02:00:00Z")
         time_sleep.assert_called_once_with(30 * 60)
         self.assertEqual(time_sleep.call_count, 1)
예제 #4
0
    def test_skips_already_installed_dependencies(
        self, subprocess_mock: unittest.mock.MagicMock
    ) -> None:
        unit = InstallSystemPackagesBuildUnit()

        # Definitely installed already
        unit.dependencies = ["python"]

        unit.build()
        subprocess_mock.assert_not_called()
예제 #5
0
    def test_withManifest_succeeds(self, mock_ingest: unittest.mock.MagicMock) -> None:
        # Act
        request_args = {"manifest_path": "gs://fake-bucket/foo/manifest.yaml"}
        headers = {"X-Appengine-Cron": "test-cron"}
        response = self.client.get(
            "/ingest", query_string=request_args, headers=headers
        )

        # Assert
        self.assertEqual(200, response.status_code)
        mock_ingest.assert_called_with(
            ANY, GcsfsFilePath(bucket_name="fake-bucket", blob_name="foo/manifest.yaml")
        )
예제 #6
0
파일: test_helpers.py 프로젝트: fgsect/fexm
 def test_download_seed_to_folder(self,
                                  requests_get: unittest.mock.MagicMock):
     """
     Test if the Building process works.
     """
     chunks = [b"a", b"b", b"c"]
     md5_hash_chunk = "900150983cd24fb0d6963f7d28e17f72"  # The md5 hash of all the chunks saved together as string
     response_mock = unittest.mock.MagicMock()
     response_mock.iter_content.return_value = iter(chunks)
     requests_get.return_value = response_mock
     tmp_dir = "tmp/"
     if os.path.isdir(tmp_dir):
         shutil.rmtree(tmp_dir)
     os.mkdir(tmp_dir)
     download_link = "somerandomlink"  # Link not important, we have mocked the response
     filename = "Makefile"
     # This file should exists now:
     self.assertTrue(
         utils.download_seed_to_folder(download_link=download_link,
                                       to_directory=tmp_dir,
                                       filename=filename))
     self.assertTrue(os.path.isfile(tmp_dir + "/" + filename))
     self.assertEqual(utils.md5(tmp_dir + "/" + filename), md5_hash_chunk)
     # We are simulating a reponse that would be the exact same file again: Should not be downloaded.
     response_mock.iter_content.return_value = (chunks)
     self.assertFalse(
         utils.download_seed_to_folder(download_link=download_link,
                                       to_directory=tmp_dir,
                                       filename=filename + "_new"))
     self.assertFalse(os.path.exists(tmp_dir + "/" + filename + "_new"))
     shutil.rmtree(tmp_dir)
예제 #7
0
    def test_build_jobs(self, mock_cpu_count: unittest.mock.MagicMock) -> None:
        """Tests that the number of build jobs comes out correctly."""
        mock_cpu_count.return_value = 13
        cases = [
            # MAX_JOBS, USE_NINJA, IS_WINDOWS,         want
            ((     '8',      True,     False),          ['-j', '8']),  # noqa: E201,E241
            ((    None,      True,     False),                 None),  # noqa: E201,E241
            ((     '7',     False,     False),          ['-j', '7']),  # noqa: E201,E241
            ((    None,     False,     False),         ['-j', '13']),  # noqa: E201,E241
            ((     '6',      True,      True),          ['-j', '6']),  # noqa: E201,E241
            ((    None,      True,      True),                 None),  # noqa: E201,E241
            ((    '11',     False,      True), ['/p:CL_MPCount=11']),  # noqa: E201,E241
            ((    None,     False,      True), ['/p:CL_MPCount=13']),  # noqa: E201,E241
        ]
        for (max_jobs, use_ninja, is_windows), want in cases:
            with self.subTest(MAX_JOBS=max_jobs, USE_NINJA=use_ninja, IS_WINDOWS=is_windows):
                with contextlib.ExitStack() as stack:
                    stack.enter_context(env_var('MAX_JOBS', max_jobs))
                    stack.enter_context(unittest.mock.patch.object(tools.setup_helpers.cmake, 'USE_NINJA', use_ninja))
                    stack.enter_context(unittest.mock.patch.object(tools.setup_helpers.cmake, 'IS_WINDOWS', is_windows))

                    cmake = tools.setup_helpers.cmake.CMake()

                    with unittest.mock.patch.object(cmake, 'run') as cmake_run:
                        cmake.build({})

                    cmake_run.assert_called_once()
                    call, = cmake_run.mock_calls
                    build_args, _ = call.args

                if want is None:
                    self.assertNotIn('-j', build_args)
                else:
                    self.assert_contains_sequence(build_args, want)
예제 #8
0
    def test_ingestFails_raisesError(
        self, mock_ingest: unittest.mock.MagicMock
    ) -> None:
        # Arrange
        mock_ingest.side_effect = ValueError("Malformed manifest")

        # Act
        request_args = {"manifest_path": "gs://fake-bucket/foo/manifest.yaml"}
        headers = {"X-Appengine-Cron": "test-cron"}
        with self.assertRaisesRegex(ValueError, "Malformed manifest"):
            self.client.get("/ingest", query_string=request_args, headers=headers)

        # Assert
        mock_ingest.assert_called_with(
            ANY, GcsfsFilePath(bucket_name="fake-bucket", blob_name="foo/manifest.yaml")
        )
예제 #9
0
    def test_try_to_query_status_from_reputation(
        self, reputation_checker_path: unittest.mock.MagicMock
    ) -> None:
        """
        Tests of the method that tries to define the status from the reputation
        checker.
        """

        self.checker.subject = "192.168.1.1"

        reputation_checker_path.return_value = ReputationCheckerStatus(
            subject="192.168.1.1",
            idna_subject="192.168.1.1",
            status="SANE",
            status_source="REPUTATION",
            tested_at=datetime.utcnow(),
        )

        self.checker.try_to_query_status_from_reputation()

        expected_status = None
        actual_status = self.checker.status.status

        self.assertEqual(expected_status, actual_status)

        reputation_checker_path.return_value = ReputationCheckerStatus(
            subject="192.168.1.1",
            idna_subject="192.168.1.1",
            status="MALICIOUS",
            status_source="REPUTATION",
            tested_at=datetime.utcnow(),
        )

        self.checker.try_to_query_status_from_reputation()

        expected_status = "ACTIVE"
        actual_status = self.checker.status.status

        self.assertEqual(expected_status, actual_status)

        expected_source = "REPUTATION"
        actual_source = self.checker.status.status_source

        self.assertEqual(expected_source, actual_source)
예제 #10
0
    def test_ingestFails_raisesError(
            self, mock_ingest: unittest.mock.MagicMock) -> None:
        # Arrange
        mock_ingest.side_effect = ValueError("Malformed manifest")

        # Act
        request_args = {"manifest_path": "gs://fake-bucket/foo/manifest.yaml"}
        headers = {"X-Appengine-Cron": "test-cron"}
        response = self.client.get("/ingest",
                                   query_string=request_args,
                                   headers=headers)

        # Assert
        self.assertEqual(500, response.status_code)
        self.assertEqual("Error ingesting data: 'Malformed manifest'",
                         response.get_data().decode())
        mock_ingest.assert_called_with(
            ANY,
            GcsfsFilePath(bucket_name="fake-bucket",
                          blob_name="foo/manifest.yaml"))
예제 #11
0
    def test_get_current(self, getcwd_path: unittest.mock.MagicMock) -> None:
        """
        Tests the method which let us get the current directory.
        """

        getcwd_path.return_value = "/hello/world"

        expected = "/hello/world"
        actual = self.helper.get_current()

        self.assertEqual(expected, actual)
예제 #12
0
 def test_invoke_afl_cmin(self, subprocess_check_output: unittest.mock.MagicMock):
     binary_path = "tshark"
     repo_path = ""
     max_timeout = 2500
     memory_limit = "none"
     h = HeuristicConfigCreator(binary_path=binary_path, repo_path=repo_path, dummyfile_path=__file__,
                                dummyfiles_path=os.getcwd())
     h.MAX_TIMEOUT = max_timeout
     h.memory_limit = memory_limit
     h.afl_cmin_path = "afl-cmin"
     mocked_return_value = "cmin-output"
     subprocess_check_output.return_value = bytes(mocked_return_value, encoding="utf-8")
     self.assertEqual(h.invoke_afl_cmin("-f", "tmp_dir"), mocked_return_value)
예제 #13
0
    def test_query_dns_record_ptr(
            self, dns_query_patch: unittest.mock.MagicMock) -> None:
        """
        Tests the method that let us query the (right) DNS record of the given
        subject.
        """

        dns_query_patch.return_value = ["example.org"]
        given = "192.168.1.1"
        expected = {"PTR": ["example.org"]}

        self.checker.subject = given

        actual = self.checker.query_dns_record()

        self.assertEqual(expected, actual)
예제 #14
0
    def test_get_info_gaierror(self,
                               addrinfo_path: unittest.mock.MagicMock) -> None:
        """
        Tests the method which let us get the information to work with for the
        case that we get an gaierror exception.
        """
        def fake_hostbyaddr(*args, **kwargs):
            raise socket.gaierror("This is a test :-)")

        addrinfo_path.side_effect = fake_hostbyaddr

        given = "example.org"

        expected = dict()  # pylint: disable=use-dict-literal
        actual = HostByAddrInfo(given).get_info()

        self.assertEqual(expected, actual)
예제 #15
0
    def test_get_info_gaierror(self,
                               addrinfo_path: unittest.mock.MagicMock) -> None:
        """
        Tests the method which let us get the information to work with for the
        case that we get an gaierror exception.
        """
        def fake_getattrinfo(*args, **kwargs):
            raise socket.gaierror("This is a test :-)")

        addrinfo_path.side_effect = fake_getattrinfo

        given = "example.org"

        expected = []
        actual = AddressInfo(given).get_info()

        self.assertEqual(expected, actual)
예제 #16
0
    def test_get_info(self, addrinfo_path: unittest.mock.MagicMock) -> None:
        """
        Tests the method which let us get the information to work with.
        """
        def fake_getattrinfo(*args, **kwargs):
            _ = args
            _ = kwargs
            return [("192.169.1.1", "10.20.30.40", ["10.55.0.32"])]

        addrinfo_path.side_effect = fake_getattrinfo

        given = "example.org"

        expected = ["10.55.0.32"]
        actual = AddressInfo(given).get_info()

        self.assertEqual(expected, actual)
예제 #17
0
    def test_get_current_with_sep(
            self, getcwd_path: unittest.mock.MagicMock) -> None:
        """
        Tests the method which let us get the current directory for the case
        that we want to have the directory separator at  the end.
        """

        getcwd_path.return_value = "/hello/world"

        if PlatformUtility.is_windows():
            expected = "/hello/world\\"
        else:
            expected = "/hello/world/"

        actual = self.helper.get_current(with_end_sep=True)

        self.assertEqual(expected, actual)
    def test_get_problem(self, requests_get_mock: unittest.mock.MagicMock):
        page = unittest.mock.MagicMock()
        page.status_code = 200
        inputs = ["5 4\n3 2", "111 222\n555 2 -1 -7", "999 221\n22 1 0\n5 5 5"]
        outpus = ["1", "2\n", "2\n1 5 8\n2 5 6"]
        content = """
        <div class="sample-test">
            <div class="input">
                <div class="title">Input</div>
                <pre>{0}</pre>
            </div>
            <div class="output">
                <div class="title">Output</div>
                <pre>{1}</pre>
            </div>
            <div class="input">
                <div class="title">Input</div>
                <pre>{2}</pre>
            </div>
            <div class="output">
                <div class="title">Output</div>
                <pre>{3}</pre>
            </div>
            <div class="input">
                <div class="title">Input</div>
                <pre>{4}</pre>
            </div>
            <div class="output">
                <div class="title">Output</div>
                <pre>{5}</pre>
            </div>
        </div>
        """.format(*[pre_contents.replace("\n", "<br>") for input_output_pair in zip(inputs, outpus)
                                                        for pre_contents in input_output_pair])
        page.content = content.encode()
        requests_get_mock.return_value = page

        link = "http://codeforces.com/contest/839/problem/A"
        problem = fetch_cf_examples.Codeforces.get_problem(link)
        assert link == problem.problem_link
        assert len(problem.examples) == 3
        assert problem.examples == [
            fetch_cf_examples.Example(inputs[0], outpus[0]),
            fetch_cf_examples.Example(inputs[1], outpus[1]),
            fetch_cf_examples.Example(inputs[2], outpus[2])]
예제 #19
0
    def test_query_dns_record_no_response(
            self, dns_query_patch: unittest.mock.MagicMock) -> None:
        """
        Tests the method that let us query the (right) DNS record of the given
        subject.

        Here we test the case that there is systematically no (valid) response.
        """

        dns_query_patch.return_value = []
        given = "example.net"
        expected = dict()  # pylint: disable=use-dict-literal

        self.checker.subject = given

        actual = self.checker.query_dns_record()

        self.assertEqual(expected, actual)
예제 #20
0
    def test_query_dns_record_not_valid_subject(
            self, dns_query_patch: unittest.mock.MagicMock) -> None:
        """
        Tests the method that let us query the (right) DNS record of the given
        subject.

        Here we test the case that the given subject is not correct.
        """

        dns_query_patch.return_value = []
        given = "a1"
        expected = dict()  # pylint: disable=use-dict-literal

        self.checker.subject = given

        actual = self.checker.query_dns_record()

        self.assertEqual(expected, actual)
예제 #21
0
    def test_build_jobs(self, mock_cpu_count: unittest.mock.MagicMock) -> None:
        """Tests that the number of build jobs comes out correctly."""
        mock_cpu_count.return_value = 13
        cases = [
            # MAX_JOBS, USE_NINJA, IS_WINDOWS,         want
            (("8", True, False), ["-j", "8"]),  # noqa: E201,E241
            ((None, True, False), None),  # noqa: E201,E241
            (("7", False, False), ["-j", "7"]),  # noqa: E201,E241
            ((None, False, False), ["-j", "13"]),  # noqa: E201,E241
            (("6", True, True), ["-j", "6"]),  # noqa: E201,E241
            ((None, True, True), None),  # noqa: E201,E241
            (("11", False, True), ["/p:CL_MPCount=11"]),  # noqa: E201,E241
            ((None, False, True), ["/p:CL_MPCount=13"]),  # noqa: E201,E241
        ]
        for (max_jobs, use_ninja, is_windows), want in cases:
            with self.subTest(MAX_JOBS=max_jobs,
                              USE_NINJA=use_ninja,
                              IS_WINDOWS=is_windows):
                with contextlib.ExitStack() as stack:
                    stack.enter_context(env_var("MAX_JOBS", max_jobs))
                    stack.enter_context(
                        unittest.mock.patch.object(tools.setup_helpers.cmake,
                                                   "USE_NINJA", use_ninja))
                    stack.enter_context(
                        unittest.mock.patch.object(tools.setup_helpers.cmake,
                                                   "IS_WINDOWS", is_windows))

                    cmake = tools.setup_helpers.cmake.CMake()

                    with unittest.mock.patch.object(cmake, "run") as cmake_run:
                        cmake.build({})

                    cmake_run.assert_called_once()
                    (call, ) = cmake_run.mock_calls
                    build_args, _ = call.args

                if want is None:
                    self.assertNotIn("-j", build_args)
                else:
                    self.assert_contains_sequence(build_args, want)
예제 #22
0
    def test_get_info(self, addrinfo_path: unittest.mock.MagicMock) -> None:
        """
        Tests the method which let us get the information to work with.
        """
        def fake_hostbyaddr(*args, **kwargs):
            _ = args
            _ = kwargs
            return [
                "www.example.org", ["example.org", "example.net"],
                ["10.55.39.20"]
            ]

        addrinfo_path.side_effect = fake_hostbyaddr

        given = "example.org"

        expected = {
            "hostname": "www.example.org",
            "aliases": ["example.org", "example.net"],
            "ips": ["10.55.39.20"],
        }
        actual = HostByAddrInfo(given).get_info()

        self.assertEqual(expected, actual)
예제 #23
0
    async def test_run(
        self,
        mock_onedocker_svc: unittest.mock.MagicMock,
        mock_instance_repo: unittest.mock.MagicMock,
        mock_storage_svc: unittest.mock.MagicMock,
        mock_wait_for_containers_async: unittest.mock.MagicMock,
    ) -> None:
        async def _run_sub_test(wait_for_containers: bool, ) -> None:
            ip = "192.0.2.0"
            container = ContainerInstance(
                instance_id="123",
                ip_address=ip,
                status=ContainerInstanceStatus.STARTED)

            mock_onedocker_svc.start_containers = MagicMock(
                return_value=[container])
            mock_onedocker_svc.wait_for_pending_containers = AsyncMock(
                return_value=[container])

            container.status = (ContainerInstanceStatus.COMPLETED
                                if wait_for_containers else
                                ContainerInstanceStatus.STARTED)
            mock_wait_for_containers_async.return_value = [container]

            stage = PIDPrepareStage(
                stage=UnionPIDStage.PUBLISHER_PREPARE,
                instance_repository=mock_instance_repo,
                storage_svc=mock_storage_svc,
                onedocker_svc=mock_onedocker_svc,
                onedocker_binary_config=MagicMock(
                    task_definition="offline-task:1#container",
                    tmp_directory="/tmp/",
                    binary_version="latest",
                ),
            )
            instance_id = "444"
            stage_input = PIDStageInput(
                input_paths=["in"],
                output_paths=["out"],
                num_shards=2,
                instance_id=instance_id,
            )

            # Basic test: All good
            with patch.object(PIDPrepareStage, "files_exist") as mock_fe:
                mock_fe.return_value = True
                stage = PIDPrepareStage(
                    stage=UnionPIDStage.PUBLISHER_PREPARE,
                    instance_repository=mock_instance_repo,
                    storage_svc=mock_storage_svc,
                    onedocker_svc=mock_onedocker_svc,
                    onedocker_binary_config=MagicMock(
                        task_definition="offline-task:1#container",
                        tmp_directory="/tmp/",
                        binary_version="latest",
                    ),
                )
                status = await stage.run(
                    stage_input, wait_for_containers=wait_for_containers)
                self.assertEqual(
                    PIDStageStatus.COMPLETED
                    if wait_for_containers else PIDStageStatus.STARTED,
                    status,
                )

                self.assertEqual(
                    mock_onedocker_svc.start_containers.call_count, 2)
                if wait_for_containers:
                    self.assertEqual(mock_wait_for_containers_async.call_count,
                                     2)
                else:
                    mock_wait_for_containers_async.assert_not_called()
                mock_instance_repo.read.assert_called_with(instance_id)
                self.assertEqual(mock_instance_repo.read.call_count, 4)
                self.assertEqual(mock_instance_repo.update.call_count, 4)

            with patch.object(PIDPrepareStage,
                              "files_exist") as mock_fe, patch.object(
                                  PIDPrepareStage, "prepare") as mock_prepare:
                mock_fe.return_value = True
                status = await stage.run(
                    stage_input, wait_for_containers=wait_for_containers)
                mock_prepare.assert_called_with(instance_id, "in", "out", 2,
                                                wait_for_containers, None)

            # Input not ready
            with patch.object(PIDPrepareStage, "files_exist") as mock_fe:
                mock_fe.return_value = False
                status = await stage.run(
                    stage_input, wait_for_containers=wait_for_containers)
                self.assertEqual(PIDStageStatus.FAILED, status)

            # Multiple input paths (invariant exception)
            with patch.object(PIDPrepareStage, "files_exist") as mock_fe:
                with self.assertRaises(ValueError):
                    mock_fe.return_value = True
                    stage_input.input_paths = ["in1", "in2"]
                    stage = PIDPrepareStage(
                        stage=UnionPIDStage.PUBLISHER_PREPARE,
                        instance_repository=mock_instance_repo,
                        storage_svc=mock_storage_svc,
                        onedocker_svc=mock_onedocker_svc,
                        onedocker_binary_config=MagicMock(
                            task_definition="offline-task:1#container",
                            tmp_directory="/tmp/",
                            binary_version="latest",
                        ),
                    )
                    status = await stage.run(
                        stage_input, wait_for_containers=wait_for_containers)

        for data_test in data_test_run():
            wait_for_containers = data_test["wait_for_containers"]
            with self.subTest(
                    "Subtest with wait_for_containers: {wait_for_containers}",
                    wait_for_containers=wait_for_containers,
            ):
                # reset mocks for each subTests
                mock_onedocker_svc.reset_mock()
                mock_instance_repo.reset_mock()
                mock_storage_svc.reset_mock()
                mock_wait_for_containers_async.reset_mock()
                await _run_sub_test(wait_for_containers)