1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
#!/usr/bin/python3
###############################################################################
# #
# libloc - A library to determine the location of someone on the Internet #
# #
# Copyright (C) 2022 IPFire Development Team <info@ipfire.org> #
# #
# This library is free software; you can redistribute it and/or #
# modify it under the terms of the GNU Lesser General Public #
# License as published by the Free Software Foundation; either #
# version 2.1 of the License, or (at your option) any later version. #
# #
# This library is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
# Lesser General Public License for more details. #
# #
###############################################################################
import location
import os
import unittest
TEST_DATA_DIR = os.environ["TEST_DATA_DIR"]
class Test(unittest.TestCase):
def setUp(self):
path = os.path.join(TEST_DATA_DIR, "database.db")
# Load the database
self.db = location.Database(path)
def test_metadata(self):
"""
Check if any metadata matches what we expected
"""
# Vendor
self.assertEqual(self.db.vendor, "IPFire Project")
# Description
self.assertEqual(self.db.description,
"This database has been obtained from https://location.ipfire.org/\n\nFind the full license terms at https://creativecommons.org/licenses/by-sa/4.0/")
# License
self.assertEqual(self.db.license, "CC BY-SA 4.0")
# Created At
self.assertIsInstance(self.db.created_at, int)
def test_fetch_network(self):
"""
Try fetching some results that should exist
"""
n = self.db.lookup("81.3.27.38")
self.assertIsInstance(n, location.Network)
n = self.db.lookup("1.1.1.1")
self.assertIsInstance(n, location.Network)
n = self.db.lookup("8.8.8.8")
self.assertIsInstance(n, location.Network)
def test_fetch_network_nonexistant(self):
"""
Try to fetch something that should not exist
"""
n = self.db.lookup("255.255.255.255")
self.assertIsNone(n)
def test_fetch_network_invalid(self):
"""
Feed some invalid inputs into the lookup function
"""
with self.assertRaises(ValueError):
self.db.lookup("XXX")
with self.assertRaises(ValueError):
self.db.lookup("455.455.455.455")
def test_verify(self):
"""
Verify the database
"""
# Path to the signature file
path = os.path.join(TEST_DATA_DIR, "signing-key.pem")
# Try to verify with an invalid signature
with self.assertRaises(TypeError):
self.db.verify(None)
# Perform verification with the correct key
with open(path, "r") as f:
self.assertTrue(self.db.verify(f))
# Perform verification with invalid keys
with open("/dev/null", "r") as f:
self.assertFalse(self.db.verify(f))
with open("/dev/urandom", "r") as f:
self.assertFalse(self.db.verify(f))
def test_search_as(self):
"""
Try to fetch an AS
"""
# Fetch an existing AS
self.assertIsInstance(self.db.get_as(204867), location.AS)
# Fetch a non-existing AS
self.assertIsNone(self.db.get_as(0))
# Fetch an AS with a number that is out of range
with self.assertRaises(OverflowError):
self.db.get_as(2**32 + 1)
def test_get_country(self):
"""
Try fetching a country
"""
# Fetch an existing country
self.assertIsInstance(self.db.get_country("DE"), location.Country)
# Fetch a non-existing country
self.assertIsNone(self.db.get_country("AA"))
# Fetch a country with an invalid country code
with self.assertRaises(ValueError):
self.db.get_country("XXX")
def test_list_bogons(self):
"""
Generate a list of bogons
"""
# Fetch all bogons
bogons = self.db.list_bogons()
# We should have received an enumerator full of networks
self.assertIsInstance(bogons, location.DatabaseEnumerator)
for bogon in bogons:
self.assertIsInstance(bogon, location.Network)
if __name__ == "__main__":
unittest.main()
|