forked from jsommers/pytricia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestload.py
executable file
·74 lines (62 loc) · 2.56 KB
/
testload.py
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
#
# This file is part of Pytricia.
# Joel Sommers <jsommers@colgate.edu>
#
# Pytricia 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 3 of the License, or
# (at your option) any later version.
#
# Pytricia 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.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pytricia. If not, see <http://www.gnu.org/licenses/>.
#
from __future__ import print_function
import unittest
import pytricia
import gzip
def dumppyt(t):
print ("\nDumping Pytricia")
for x in t.keys():
print ("\t {}: {}".format(x,t[x]))
class PyTriciaLoadTest(unittest.TestCase):
_files = ['routeviews-rv2-20160202-1200.pfx2as.gz', 'routeviews-rv6-20160202-1200.pfx2as.gz']
def testLoaditUp(self):
pyt = pytricia.PyTricia(128)
# load routeviews prefix -> AS mappings; all of them.
for f in PyTriciaLoadTest._files:
print ("loading routeviews data from {}".format(f))
with gzip.GzipFile(f, 'r') as inf:
for line in inf:
ipnet,prefix,asn = line.split()
network = '{}/{}'.format(ipnet.decode(), prefix.decode())
if network in pyt:
pyt[network].append(asn.decode())
else:
pyt[network] = [asn.decode()]
# verify that everything was stuffed into pyt correctly
for f in PyTriciaLoadTest._files:
print ("verifying routeviews data from {}".format(f))
with gzip.GzipFile(f, 'r') as inf:
for line in inf:
ipnet,prefix,asn = line.split()
asn = asn.decode()
network = '{}/{}'.format(ipnet.decode(), prefix.decode())
self.assertIn(network, pyt)
ipnet = str(ipnet.decode())
self.assertIn(ipnet, pyt)
asnlist = pyt[network]
self.assertIn(asn, asnlist)
# dump everything out...
# dumppyt(pyt)
print("removing everything.")
netlist = [ n for n in pyt.keys() ]
for net in netlist:
del pyt[net]
self.assertEqual(len(pyt), 0)
if __name__ == '__main__':
unittest.main()