Debian Codegraph @

test/test_fuzz.py

raw symbols SCIP
1 """Fuzing for IPy.py"""
2
3 # TODO: unify assert / FilIf usage
4
5 import sys
6 import functools
7 import itertools
8 sys.path.append('.')
9 sys.path.append('..')
10
11 import IPy
12 import unittest
13 import random
14
15 if sys.version_info >= (3,):
16 xrange = range
17
18 # on Python-2.7 and higher, we use load_tests to multiply out the test cases so that unittest
19 # represents each as an individual test case.
20 def iterate_27(n):
21 def wrap(func):
22 func.iterations = n
23 return func
24 return wrap
25
26 def load_tests(loader, tests, pattern):
27 def expand(tests):
28 if isinstance(tests, unittest.TestCase):
29 method_name = tests._testMethodName
30 meth = getattr(tests, method_name)
31 if hasattr(meth, 'iterations'):
32 tests = unittest.TestSuite(type(tests)(method_name) for i in xrange(meth.iterations))
33 else:
34 tests = unittest.TestSuite(expand(t) for t in tests)
35 return tests
36 return expand(tests)
37
38 # On older Pythons, we run the requisite iterations directly, in a single test case.
39 def iterate_old(n):
40 def wrap(func):
41 @functools.wraps(func)
42 def replacement(*args):
43 for i in xrange(n):
44 func(*args)
45 return replacement
46 return wrap
47
48 if sys.version_info >= (2,7):
49 iterate = iterate_27
50 else:
51 iterate = iterate_old
52
53 # utilities
54
55 def random_ipv4_prefix():
56 prefixlen = random.randrange(32)
57 int_ip = random.randrange(IPy.MAX_IPV4_ADDRESS)
58 int_ip &= 0xffffffff << (32-prefixlen)
59 return IPy.IP('.'.join(map(str, (int_ip >> 24,
60 (int_ip >> 16) & 0xff,
61 (int_ip >> 8) & 0xff,
62 int_ip & 0xff)))
63 + '/%d' % prefixlen)
64
65 # tests
66
67 class ParseAndBack(unittest.TestCase):
68
69 @iterate(500)
70 def testRandomValuesv4(self):
71 question = random.randrange(0xffffffff)
72 self.assertEqual(IPy.parseAddress(IPy.intToIp(question, 4)), (question, 4), hex(question))
73
74 @iterate(500)
75 def testRandomValuesv6(self):
76 question = random.randrange(0xffffffffffffffffffffffffffffffff)
77 self.assertEqual(IPy.parseAddress(IPy.intToIp(question, 6)), (question, 6), hex(question))
78
79 class TestIPSet(unittest.TestCase):
80
81 @iterate(1000)
82 def testRandomContains(self):
83 prefixes = [random_ipv4_prefix() for i in xrange(random.randrange(50))]
84 question = random_ipv4_prefix()
85 answer = any(question in pfx for pfx in prefixes)
86 ipset = IPy.IPSet(prefixes)
87 self.assertEqual(question in ipset, answer,
88 "%s in %s != %s (made from %s)" % (question, ipset, answer, prefixes))
89
90
91 @iterate(1000)
92 def testRandomDisjoint(self):
93 prefixes1 = [random_ipv4_prefix() for i in xrange(random.randrange(50))]
94 prefixes2 = [random_ipv4_prefix() for i in xrange(random.randrange(50))]
95 # test disjointnes the stupid way
96 disjoint = True
97 for p1, p2 in itertools.product(prefixes1, prefixes2):
98 if p1 in p2 or p2 in p1:
99 disjoint = False
100 break
101 ipset1 = IPy.IPSet(prefixes1)
102 ipset2 = IPy.IPSet(prefixes2)
103 self.assertEqual(ipset1.isdisjoint(ipset2), disjoint,
104 "%s.isdisjoint(%s) != %s" % (ipset1, ipset2, disjoint))
105 self.assertEqual(ipset2.isdisjoint(ipset1), disjoint,
106 "%s.isdisjoint(%s) != %s" % (ipset2, ipset1, disjoint))
107
108 if __name__ == "__main__":
109 unittest.main()