Beispiel #1
0
    def domain(self, domain=None, last_domain=None):
        """
        Manage the case that we want to test only a domain.

        Argument:
            - domain: str
                The domain or IP to test.
            - last_domain: str
                The last domain of the file we are testing.
        """

        if domain:
            PyFunceble.CONFIGURATION["domain"] = self._format_domain(domain)
        self.print_header()

        if PyFunceble.CONFIGURATION["domain"]:
            if __name__ == "PyFunceble.core":
                if PyFunceble.CONFIGURATION["simple"]:
                    print(ExpirationDate().get())
                else:
                    status = ExpirationDate().get()

                self._file_decision(domain, last_domain, status)
            else:
                ExpirationDate().get()
                return
    def test_is_domains_valid(self):
        """
        This method test ExpirationDate().is_domain_valid().
        """

        # Test of the case that the domains is valid
        expected = True
        PyFunceble.CONFIGURATION["domain"] = "hello.world"
        actual = ExpirationDate().is_domain_valid()

        self.assertEqual(expected, actual)

        # Test of the case that the domains is not valid
        not_valid = [
            "hello-world",
            "-hello.world",
            "hello-.world",
            "*****@*****.**",
            "hello_world.com",
        ]
        expected = False

        for domain in not_valid:
            PyFunceble.CONFIGURATION["domain"] = domain
            actual = ExpirationDate().is_domain_valid()

            self.assertEqual(expected, actual, msg="%s is valid." % domain)
    def test_is_ip_valid(self):
        """
        This method test ExpirationDate().is_ip_valid().
        """

        # test of the case that the ip is valid
        expected = True
        valid = ["15.47.85.65", "45.66.255.240"]

        for ip_to_test in valid:
            PyFunceble.CONFIGURATION["domain"] = ip_to_test
            actual = ExpirationDate().is_ip_valid()

            self.assertEqual(expected,
                             actual,
                             msg="%s is invalid." % ip_to_test)

        # test of the case that the ip is invalid
        expected = False
        invalid = ["google.com", "287.468.45.26", "245.85.69.17:8081"]

        for ip_to_test in invalid:
            PyFunceble.CONFIGURATION["domain"] = ip_to_test
            actual = ExpirationDate().is_ip_valid()

            self.assertEqual(expected, actual, msg="%s is valid." % ip_to_test)
Beispiel #4
0
    def is_url_valid(cls, url=None):
        """
        Check if the domain of the given URL is valid.

        Argument:
            - url: str
                The url to test.

        Returns: bool
            - True: is valid.
            - False: is invalid.
        """

        if url:
            to_test = url
        else:
            to_test = PyFunceble.CONFIGURATION["URL"]

        if to_test.startswith("http"):
            regex = r"((http:\/\/|https:\/\/)(.+?(?=\/)|.+?$))"
            domain = Regex(to_test, regex, return_data=True,
                           rematch=True).match()[2]

            domain_status = ExpirationDate().is_domain_valid(domain)
            ip_status = ExpirationDate().is_domain_valid(domain)

            if domain_status or ip_status:
                return True

        return False
    def test_convert_or_shorten_month(self):
        """
        This method test ExpirationDate()._convert_or_shorten_month().
        """

        expected = "jan"

        for element in ["1", "01", "Jan", "January"]:
            actual = ExpirationDate()._convert_or_shorten_month(element)

            self.assertEqual(expected, actual)

        expected = "Hello"
        actual = ExpirationDate()._convert_or_shorten_month("Hello")

        self.assertEqual(expected, actual)
    def test_convert_1_to_2_digits(self):
        """
        This method test ExpirationDate()._convert_1_to_2_digits().
        """

        expected = [
            "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11"
        ]

        for index, number in enumerate(expected):
            actual = ExpirationDate()._convert_1_to_2_digits(index + 1)
            self.assertEqual(number, actual)
Beispiel #7
0
    def _format_adblock_decoded(cls, to_format, result=None):
        """
        Format the exctracted adblock line before passing it to the system.

        Arguments:
            - to_format: str
                The extracted line from the file.
            - result: None or list
                The list of extracted domain.

        Returns: list
            The list of extracted domains.
        """

        if not result:
            result = []

        for data in List(to_format).format():
            if data:
                if "#" in data:
                    return cls._format_adblock_decoded(data.split("#"), result)

                elif "," in data:
                    return cls._format_adblock_decoded(data.split(","), result)

                elif "~" in data:
                    return cls._format_adblock_decoded(data.split("~"), result)

                elif "!" in data:
                    return cls._format_adblock_decoded(data.split("!"), result)

                elif "|" in data:
                    return cls._format_adblock_decoded(data.split("|"), result)

                elif data and (ExpirationDate.is_domain_valid(data)
                               or ExpirationDate.is_ip_valid(data)):
                    result.append(data)

        return result
Beispiel #8
0
    def domain(self, domain=None, last_domain=None):
        """
        Manage the case that we want to test only a domain.

        :param domain: The domain or IP to test.
        :type domain: str

        :param last_domain:
            The last domain to test if we are testing a file.
        :type last_domain: str

        :param return_status: Tell us if we need to return the status.
        :type return_status: bool
        """

        # We print the header.
        self._print_header()

        if domain:
            # A domain is given.

            # We format and set the domain we are testing and treating.
            PyFunceble.INTERN["to_test"] = self._format_domain(domain)
        else:
            # A domain is not given.

            # We set the domain we are testing and treating to None.
            PyFunceble.INTERN["to_test"] = None

        if PyFunceble.INTERN["to_test"]:
            # The domain is given (Not None).

            if PyFunceble.CONFIGURATION["syntax"]:
                # The syntax mode is activated.

                # We get the status from Syntax.
                status = Syntax().get()
            else:
                # We test and get the status of the domain.
                status = ExpirationDate().get()

            # We run the file decision logic.
            self._file_decision(domain, last_domain, status)

            if PyFunceble.CONFIGURATION["simple"]:
                # The simple mode is activated.

                # We print the domain and the status.
                print(domain, status)
Beispiel #9
0
    def test(self):
        """
        This method avoid confusion between self.domain which is called into
        __main__ and test() which should be called out of PyFunceble's scope.

        Returns: str
            ACTIVE, INACTIVE or INVALID.

        Raise:
            - Exception: when this method is called under __name___
        """

        if not self.modulo_test:  # pylint: disable=no-member
            raise Exception(
                "You should not use this method. Please prefer self.domain()")

        else:
            return ExpirationDate().get()
Beispiel #10
0
    def get(cls):
        """
        Get the status while testing for an IP or domain.

        .. note::
            We consider that the domain or IP we are currently testing
            is into :code:`PyFunceble.INTERN["to_test"]`.
        """

        if "to_test" in PyFunceble.INTERN and PyFunceble.INTERN["to_test"]:
            expiration_date = ExpirationDate().get()

            if expiration_date is False:
                return cls.handle(status="invalid")

            if expiration_date == PyFunceble.STATUS["official"]["up"]:
                return expiration_date, "WHOIS"

            return cls.handle(status="inactive")

        raise NotImplementedError("We expect `INTERN['to_test']` to be set.")
    def test_format_date(self):
        """
        This method test ExpirationDate()._format().
        """

        expected = "02-jan-2017"

        to_test = [
            "02-jan-2017",
            "02.01.2017",
            "02/01/2017",
            "2017-01-02",
            "2017.01.02",
            "2017/01/02",
            "2017.01.02 15:00:00",
            "20170102 15:00:00",
            "2017-01-02 15:00:00",
            "02.01.2017 15:00:00",
            "02-Jan-2017 15:00:00 UTC",
            "2017/01/02 01:00:00 (+0900)",
            "2017/01/02 01:00:00",
            "Mon Jan 02 15:00:00 GMT 2017",
            "Mon Jan 02 2017",
            "2017-01-02T15:00:00",
            "2017-01-02T15:00:00Z",
            "2017-01-02T15:00:00+0200",
            "2017-01-02T15:00:00+0200.622265+03:00",
            "2017-01-02T15:00:00+0200.622265",
            "2017-01-02T23:59:59.0Z",
            "02-01-2017",
            "2017. 01. 02.",
            "2017-01-02T00:00:00+13:00",
            "20170102",
            "02-Jan-2017",
            "02.1.2017",
            "02 Jan 2017",
            "02-January-2017",
            "2017-Jan-02.",
            "Mon Jan 02 15:00:00 2017",
            "January 02 2017-Jan-02",
            "2.1.2017",
            "20170102000000",
            "January  2 2017",
            "2nd January 2017",
        ]

        for date in to_test:
            actual = ExpirationDate()._format(date)
            self.assertEqual(expected, actual, msg="Error for %s" % repr(date))

        special_case = {
            "1": ["02/13/2017", "13-feb-2017"],
            "2": ["Mon Jan 2017 15:00:00", "00-jan-2017"],
            "3": ["15 janvier 2017", ""],
            "4": ["This is not a date", ""],
        }

        for data in special_case:
            actual = ExpirationDate()._format(special_case[data][0])
            expected = special_case[data][-1]
            self.assertEqual(expected,
                             actual,
                             msg="Error for %s" % special_case[data[0]])
Beispiel #12
0
    def get(self):
        """
        Get the status while testing for an IP or domain.
        """

        if self.subject:
            self.output.update({
                "domain_syntax_validation":
                self.checker.is_domain(),
                "expiration_date":
                None,
                "http_status_code":
                "***",
                "ipv4_range_syntax_validation":
                self.checker.is_ipv4_range(),
                "ipv4_syntax_validation":
                self.checker.is_ipv4(),
                "subdomain_syntax_validation":
                self.checker.is_subdomain(),
                "tested":
                self.subject,
                "url_syntax_validation":
                self.checker.is_url(),
                "whois_server":
                Referer(self.subject).get(),
            })

            if PyFunceble.CONFIGURATION["local"] or (
                    self.output["domain_syntax_validation"]
                    or self.output["ipv4_syntax_validation"]):
                self.output["http_status_code"] = HTTPCode(
                    self.subject, self.subject_type).get()

                if not self.output["subdomain_syntax_validation"]:
                    self.output["expiration_date"], self.output[
                        "whois_record"] = ExpirationDate(
                            self.subject,
                            self.output["whois_server"],
                            whois_db=self.whois_db,
                        ).get()

                    if isinstance(self.output["expiration_date"], str):
                        self.output["_status_source"] = self.output[
                            "status_source"] = "WHOIS"
                        self.output["_status"] = self.output[
                            "status"] = PyFunceble.STATUS["official"]["up"]

                        Generate(
                            self.subject,
                            self.subject_type,
                            self.output["status"],
                            source=self.output["status_source"],
                            expiration_date=self.output["expiration_date"],
                            http_status_code=self.output["http_status_code"],
                            whois_server=self.output["whois_server"],
                            filename=self.filename,
                            ip_validation=self.
                            output["ipv4_syntax_validation"],
                        ).status_file()
                    else:
                        self.output["_status_source"] = "DNSLOOKUP"
                        self.handle(
                            status="inactive",
                            ip_validation_status=self.
                            output["ipv4_syntax_validation"],
                        )
                else:
                    self.output["_status_source"] = "DNSLOOKUP"
                    self.handle(
                        status="inactive",
                        ip_validation_status=self.
                        output["ipv4_syntax_validation"],
                    )
            else:
                self.output["_status_source"] = "SYNTAX"
                self.output["_status"] = PyFunceble.STATUS["official"][
                    "invalid"]

                self.handle(
                    status="invalid",
                    ip_validation_status=self.output["ipv4_syntax_validation"],
                )

            return self.output

        raise ValueError("Subject should be given.")
Beispiel #13
0
    def test(self, complete=False):
        """
        Avoid confusion between self.domain which is called into
        __main__ and test() which should be called out of PyFunceble's scope.

        :param complete:
            Activate the return of a dictionnary with signigican data about
            the test.
        :type complete: bool

        :return: ACTIVE INACTIVE or INVALID.
        :rtype: str|list

        :raises:
            :code:`Exception`
                When this method is called under
                :code:`__name__ == '__main__'`

        .. note::
            This method should never be called in a
            :code:`__name__ == '__main__'` context.
        """

        if not self.modulo_test:  # pylint: disable=no-member
            # We are not used as an imported module.

            # We inform the user that they should not use this method.
            raise Exception(
                "You should not use this method. Please prefer self.domain()"
            )

        else:
            # We are used as an imported module.

            if complete:
                # We have to return much more information into our result.

                # We initiate the location and the information we have to return.
                PyFunceble.INTERN["current_test_data"] = {
                    "tested": None,
                    "expiration_date": None,
                    "domain_syntax_validation": None,
                    "http_status_code": None,
                    "ip4_syntax_validation": None,
                    "nslookup": [],
                    "status": None,
                    "url_syntax_validation": None,
                    "whois_server": None,
                    "whois_record": None,
                }

                if "to_test" in PyFunceble.INTERN and PyFunceble.INTERN["to_test"]:
                    # We are testing something.

                    # We update the tested index.
                    PyFunceble.INTERN["current_test_data"][
                        "tested"
                    ] = PyFunceble.INTERN["to_test"]

                    if PyFunceble.INTERN["to_test_type"] == "domain":
                        # We are testing a domain.

                        # We get the status of the domain.
                        PyFunceble.INTERN["current_test_data"][
                            "status"
                        ] = ExpirationDate().get()
                    elif PyFunceble.INTERN["to_test_type"] == "url":
                        # We are testing a url.

                        # We get the status of the url.
                        PyFunceble.INTERN["current_test_data"]["status"] = URL().get()
                    else:
                        raise Exception("Unknow test type.")

                if "http_code" in PyFunceble.INTERN:
                    # The http status code exist into the configuration.

                    # We update the tested index.
                    PyFunceble.INTERN["current_test_data"][
                        "http_status_code"
                    ] = PyFunceble.INTERN["http_code"]

                # We finaly return our dataset.
                return PyFunceble.INTERN["current_test_data"]

            if PyFunceble.INTERN["to_test_type"] == "domain":
                # We are testing a domain.

                # We return the status of the parsed domain.
                return ExpirationDate().get()

            if PyFunceble.INTERN["to_test_type"] == "url":
                # We are testing a url.

                # We return the status of the parsed url.
                return URL().get()

            # We raise an exception because that means that something wrong
            # happened because of the developer not the user.
            raise Exception("Unknown to_test_type. Please report issue.")