File test_integrity.py of Package pyasn-data

#
# The MIT License (MIT)
#
# Copyright (c) 2025 munix9@googlemail.com
# Copyright (c) 2014-2017 Hadi Asghari
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#

import os
from pyasn import pyasn
from unittest import TestCase

IPASN_DB_PATH = os.environ.get('IPASN_DB_PATH')
AS_NAMES_FILE_PATH = os.environ.get('AS_NAMES_FILE_PATH')

class TestIntegrity(TestCase):

    db = pyasn(IPASN_DB_PATH, as_names_file=AS_NAMES_FILE_PATH)

    def test_ipv4(self):
        """
            Test IPv4 address lookup
        """
        asn, prefix = self.db.lookup('8.8.8.8')
        for i in range(100):
            tmp_asn, tmp_prefix = self.db.lookup('8.8.8.8')
            self.assertEqual(asn, tmp_asn)
            self.assertEqual(prefix, tmp_prefix)

        asn, prefix = self.db.lookup('9.9.9.9')
        for i in range(100):
            tmp_asn, tmp_prefix = self.db.lookup('9.9.9.9')
            self.assertEqual(asn, tmp_asn)
            self.assertEqual(prefix, tmp_prefix)

    def test_ipv6(self):
        """
            Test IPv6 address lookup
        """
        known_ips = [
            ('2607:f8b0:4006:80f::200e', 15169),   # Google
            ('2620:fe::fe', 19281),   # Quad9
            ('d::d', None),  # Random unused IPv6
        ]

        for ip, known_as in known_ips:
            asn, prefix = self.db.lookup(ip)
            self.assertEqual(asn, known_as)

    def test_asnames(self):
        """
            Test AS names lookup
        """
        asn, prefix = self.db.lookup('8.8.8.8')
        name = self.db.get_as_name(asn)
        print("name: '" + name + "'")
        self.assertTrue(name.lower().find("google") >= 0, "ASN Name Incorrect! Should be Google")

        asn, prefix = self.db.lookup('9.9.9.9')
        name = self.db.get_as_name(asn)
        print("name: '" + name + "'")
        self.assertTrue(name.lower().find("quad9") >= 0, "ASN Name Incorrect! Should be Quad9")

        name = self.db.get_as_name(-1)
        if isinstance(name, str):
            print("name: '" + name + "'")
        self.assertTrue(name is None, "ASN Name Incorrect! Should be None")

openSUSE Build Service is sponsored by