Beispiel #1
0
    def __init__(
        self,
        subject: Optional[str] = None,
        do_syntax_check_first: Optional[bool] = None,
        db_session: Optional[Session] = None,
        use_collection: Optional[bool] = None,
    ) -> None:
        self.dns_query_tool = DNSQueryTool()
        self.ipv4_reputation_query_tool = IPV4ReputationDataset()
        self.domain_syntax_checker = DomainSyntaxChecker()
        self.ip_syntax_checker = IPSyntaxChecker()
        self.url_syntax_checker = URLSyntaxChecker()

        self.params = ReputationCheckerParams()

        self.status = ReputationCheckerStatus()
        self.status.params = self.params
        self.status.dns_lookup_record = self.dns_query_tool.lookup_record

        super().__init__(
            subject,
            do_syntax_check_first=do_syntax_check_first,
            db_session=db_session,
            use_collection=use_collection,
        )
Beispiel #2
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)
Beispiel #3
0
    def subject_propagator(self) -> "CheckerBase":
        """
        Propagate the currently set subject.

        .. warning::
            You are not invited to run this method directly.
        """

        self.dns_query_tool.set_subject(self.idna_subject)

        self.domain_syntax_checker.subject = self.idna_subject
        self.ip_syntax_checker.subject = self.idna_subject
        self.url_syntax_checker.subject = self.idna_subject

        self.status = ReputationCheckerStatus()
        self.status.params = self.params
        self.status.dns_lookup_record = self.dns_query_tool.lookup_record

        self.status.subject = self.subject
        self.status.idna_subject = self.idna_subject

        self.query_syntax_checker()

        return self
Beispiel #4
0
    def setUp(self) -> None:
        """
        Setups everything we need.
        """

        self.status = ReputationCheckerStatus(subject="example.org")
Beispiel #5
0
class TestReputationCheckerStatus(unittest.TestCase):
    """
    Tests of our reputation status handler.
    """

    def setUp(self) -> None:
        """
        Setups everything we need.
        """

        self.status = ReputationCheckerStatus(subject="example.org")

    def tearDown(self) -> None:
        """
        Destroys everything previously initiated for the tests.
        """

        del self.status

    def test_is_malicious(self) -> None:
        """
        Tests the method which let us check if the current status is a
        malicious one.
        """

        self.status.status = "MALICIOUS"

        expected = True
        actual = self.status.is_malicious()

        self.assertEqual(expected, actual)

    def test_is_not_malicious(self) -> None:
        """
        Tests the method which let us check if the current status is a
        malicious one.

        In this test we check the case that the status is actually not malicious.
        """

        self.status.status = "SANE"

        expected = False
        actual = self.status.is_malicious()

        self.assertEqual(expected, actual)

    def test_is_sane(self) -> None:
        """
        Tests the method which let us check if the current status is a
        sane one.
        """

        self.status.status = "SANE"

        expected = True
        actual = self.status.is_sane()

        self.assertEqual(expected, actual)

    def test_is_not_sane(self) -> None:
        """
        Tests the method which let us check if the current status is a
        sane one.

        In this test we check the case that the status is actually not sane.
        """

        self.status.status = "MALICIOUS"

        expected = False
        actual = self.status.is_sane()

        self.assertEqual(expected, actual)

    def test_has_bad_reputation(self) -> None:
        """
        Tests the method which let us check if the current status is a
        bad one.
        """

        self.status.status = "MALICIOUS"

        expected = True
        actual = self.status.has_bad_reputation()

        self.assertEqual(expected, actual)

    def test_has_not_bad_reputation(self) -> None:
        """
        Tests the method which let us check if the current status is a
        bad one.

        In this test we check the case that the status is actually a good one
        """

        self.status.status = "SANE"

        expected = False
        actual = self.status.has_bad_reputation()

        self.assertEqual(expected, actual)