File: networks-dedup.py

package info (click to toggle)
libloc 0.9.18-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 42,824 kB
  • sloc: ansic: 7,602; python: 2,893; makefile: 511; sh: 39; perl: 13
file content (165 lines) | stat: -rwxr-xr-x 4,451 bytes parent folder | download
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/python3
###############################################################################
#                                                                             #
# libloc - A library to determine the location of someone on the Internet     #
#                                                                             #
# Copyright (C) 2024 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 tempfile
import unittest

class Test(unittest.TestCase):
	def setUp(self):
		# Show even very large diffs
		self.maxDiff = None

	def __test(self, inputs, outputs=None):
		"""
			Takes a list of networks that are written to the database and
			compares the result with the second argument.
		"""
		if outputs is None:
			outputs = [network for network, cc, asn in inputs]

		with tempfile.NamedTemporaryFile() as f:
			w = location.Writer()

			# Add all inputs
			for network, cc, asn in inputs:
				n = w.add_network(network)

				# Add CC
				if cc:
					n.country_code = cc

				# Add ASN
				if asn:
					n.asn = asn

			# Write file
			w.write(f.name)

			# Re-open the database
			db = location.Database(f.name)

			# Check if the output matches what we expect
			self.assertCountEqual(
				outputs, ["%s" % network for network in db.networks],
			)

	def test_dudup_simple(self):
		"""
			Creates a couple of redundant networks and expects fewer being written
		"""
		self.__test(
			(
				("10.0.0.0/8", None, None),
				("10.0.0.0/16", None, None),
				("10.0.0.0/24", None, None),
			),

			# Everything should be put into the /8 subnet
			("10.0.0.0/8",),
		)

	def test_dedup_noop(self):
		"""
			Nothing should be changed here
		"""
		networks = (
			("10.0.0.0/8", None, None),
			("20.0.0.0/8", None, None),
			("30.0.0.0/8", None, None),
			("40.0.0.0/8", None, None),
			("50.0.0.0/8", None, None),
			("60.0.0.0/8", None, None),
			("70.0.0.0/8", None, None),
			("80.0.0.0/8", None, None),
			("90.0.0.0/8", None, None),
		)

		# The input should match the output
		self.__test(networks)

	def test_dedup_with_properties(self):
		"""
			A more complicated deduplication test where properties have been set
		"""
		# Nothing should change here because of different countries
		self.__test(
			(
				("10.0.0.0/8",  "DE", None),
				("10.0.0.0/16", "AT", None),
				("10.0.0.0/24", "DE", None),
			),
		)

		# Nothing should change here because of different ASNs
		self.__test(
			(
				("10.0.0.0/8",  None, 1000),
				("10.0.0.0/16", None, 2000),
				("10.0.0.0/24", None, 1000),
			),
		)

		# Everything can be merged again
		self.__test(
			(
				("10.0.0.0/8",  "DE", 1000),
				("10.0.0.0/16", "DE", 1000),
				("10.0.0.0/24", "DE", 1000),
			),
			("10.0.0.0/8",),
		)

	def test_merge(self):
		"""
			Checks whether the merging algorithm works
		"""
		self.__test(
			(
				("10.0.0.0/9",   None, None),
				("10.128.0.0/9", None, None),
			),
			("10.0.0.0/8",),
		)

	def test_bug13236(self):
		self.__test(
			(
				("209.38.0.0/16",   "US", None),
				("209.38.1.0/24",   "US", 14061),
				("209.38.160.0/22", "US", 14061),
				("209.38.164.0/22", "US", 14061),
				("209.38.168.0/22", "US", 14061),
				("209.38.172.0/22", "US", 14061),
				("209.38.176.0/20", "US", 14061),
				("209.38.192.0/19", "US", 14061),
				("209.38.224.0/19", "US", 14061),
			),
			(
				"209.38.0.0/16",
				"209.38.1.0/24",
				"209.38.160.0/19",
				"209.38.192.0/18",
			),
		)


if __name__ == "__main__":
	unittest.main()