]>
git.ipfire.org Git - location/libloc.git/blob - tests/python/networks-dedup.py
34a82d41bce1921f23ea5631a304b7cca294be57
2 ###############################################################################
4 # libloc - A library to determine the location of someone on the Internet #
6 # Copyright (C) 2024 IPFire Development Team <info@ipfire.org> #
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. #
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. #
18 ###############################################################################
25 class Test(unittest
.TestCase
):
27 # Show even very large diffs
30 def __test(self
, inputs
, outputs
=None):
32 Takes a list of networks that are written to the database and
33 compares the result with the second argument.
36 outputs
= [network
for network
, cc
, asn
in inputs
]
38 with tempfile
.NamedTemporaryFile() as f
:
42 for network
, cc
, asn
in inputs
:
43 n
= w
.add_network(network
)
56 # Re-open the database
57 db
= location
.Database(f
.name
)
59 # Check if the output matches what we expect
60 self
.assertCountEqual(
61 outputs
, ["%s" % network
for network
in db
.networks
],
64 def test_dudup_simple(self
):
66 Creates a couple of redundant networks and expects fewer being written
70 ("10.0.0.0/8", None, None),
71 ("10.0.0.0/16", None, None),
72 ("10.0.0.0/24", None, None),
75 # Everything should be put into the /8 subnet
79 def test_dedup_noop(self
):
81 Nothing should be changed here
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),
95 # The input should match the output
98 def test_dedup_with_properties(self
):
100 A more complicated deduplication test where properties have been set
102 # Nothing should change here because of different countries
105 ("10.0.0.0/8", "DE", None),
106 ("10.0.0.0/16", "AT", None),
107 ("10.0.0.0/24", "DE", None),
111 # Nothing should change here because of different ASNs
114 ("10.0.0.0/8", None, 1000),
115 ("10.0.0.0/16", None, 2000),
116 ("10.0.0.0/24", None, 1000),
120 # Everything can be merged again
123 ("10.0.0.0/8", "DE", 1000),
124 ("10.0.0.0/16", "DE", 1000),
125 ("10.0.0.0/24", "DE", 1000),
130 def test_merge(self
):
132 Checks whether the merging algorithm works
136 ("10.0.0.0/9", None, None),
137 ("10.128.0.0/9", None, None),
143 if __name__
== "__main__":