]> git.ipfire.org Git - location/libloc.git/blob - tests/python/networks-dedup.py
34a82d41bce1921f23ea5631a304b7cca294be57
[location/libloc.git] / tests / python / networks-dedup.py
1 #!/usr/bin/python3
2 ###############################################################################
3 # #
4 # libloc - A library to determine the location of someone on the Internet #
5 # #
6 # Copyright (C) 2024 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
20 import location
21 import os
22 import tempfile
23 import unittest
24
25 class Test(unittest.TestCase):
26 def setUp(self):
27 # Show even very large diffs
28 self.maxDiff = None
29
30 def __test(self, inputs, outputs=None):
31 """
32 Takes a list of networks that are written to the database and
33 compares the result with the second argument.
34 """
35 if outputs is None:
36 outputs = [network for network, cc, asn in inputs]
37
38 with tempfile.NamedTemporaryFile() as f:
39 w = location.Writer()
40
41 # Add all inputs
42 for network, cc, asn in inputs:
43 n = w.add_network(network)
44
45 # Add CC
46 if cc:
47 n.country_code = cc
48
49 # Add ASN
50 if asn:
51 n.asn = asn
52
53 # Write file
54 w.write(f.name)
55
56 # Re-open the database
57 db = location.Database(f.name)
58
59 # Check if the output matches what we expect
60 self.assertCountEqual(
61 outputs, ["%s" % network for network in db.networks],
62 )
63
64 def test_dudup_simple(self):
65 """
66 Creates a couple of redundant networks and expects fewer being written
67 """
68 self.__test(
69 (
70 ("10.0.0.0/8", None, None),
71 ("10.0.0.0/16", None, None),
72 ("10.0.0.0/24", None, None),
73 ),
74
75 # Everything should be put into the /8 subnet
76 ("10.0.0.0/8",),
77 )
78
79 def test_dedup_noop(self):
80 """
81 Nothing should be changed here
82 """
83 networks = (
84 ("10.0.0.0/8", None, None),
85 ("20.0.0.0/8", None, None),
86 ("30.0.0.0/8", None, None),
87 ("40.0.0.0/8", None, None),
88 ("50.0.0.0/8", None, None),
89 ("60.0.0.0/8", None, None),
90 ("70.0.0.0/8", None, None),
91 ("80.0.0.0/8", None, None),
92 ("90.0.0.0/8", None, None),
93 )
94
95 # The input should match the output
96 self.__test(networks)
97
98 def test_dedup_with_properties(self):
99 """
100 A more complicated deduplication test where properties have been set
101 """
102 # Nothing should change here because of different countries
103 self.__test(
104 (
105 ("10.0.0.0/8", "DE", None),
106 ("10.0.0.0/16", "AT", None),
107 ("10.0.0.0/24", "DE", None),
108 ),
109 )
110
111 # Nothing should change here because of different ASNs
112 self.__test(
113 (
114 ("10.0.0.0/8", None, 1000),
115 ("10.0.0.0/16", None, 2000),
116 ("10.0.0.0/24", None, 1000),
117 ),
118 )
119
120 # Everything can be merged again
121 self.__test(
122 (
123 ("10.0.0.0/8", "DE", 1000),
124 ("10.0.0.0/16", "DE", 1000),
125 ("10.0.0.0/24", "DE", 1000),
126 ),
127 ("10.0.0.0/8",),
128 )
129
130 def test_merge(self):
131 """
132 Checks whether the merging algorithm works
133 """
134 self.__test(
135 (
136 ("10.0.0.0/9", None, None),
137 ("10.128.0.0/9", None, None),
138 ),
139 ("10.0.0.0/8",),
140 )
141
142
143 if __name__ == "__main__":
144 unittest.main()