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