File: test_fuzz.py

package info (click to toggle)
ipy 1%3A1.01-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 268 kB
  • sloc: python: 1,774; makefile: 26; sh: 7
file content (109 lines) | stat: -rw-r--r-- 3,539 bytes parent folder | download | duplicates (5)
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
"""Fuzing for IPy.py"""

# TODO: unify assert / FilIf usage

import sys
import functools
import itertools
sys.path.append('.')
sys.path.append('..')

import IPy
import unittest
import random

if sys.version_info >= (3,):
    xrange = range

# on Python-2.7 and higher, we use load_tests to multiply out the test cases so that unittest
# represents each as an individual test case.
def iterate_27(n):
    def wrap(func):
        func.iterations = n
        return func
    return wrap

def load_tests(loader, tests, pattern):
    def expand(tests):
        if isinstance(tests, unittest.TestCase):
            method_name = tests._testMethodName
            meth = getattr(tests, method_name)
            if hasattr(meth, 'iterations'):
                tests = unittest.TestSuite(type(tests)(method_name) for i in xrange(meth.iterations))
        else:
            tests = unittest.TestSuite(expand(t) for t in tests)
        return tests
    return expand(tests)

# On older Pythons, we run the requisite iterations directly, in a single test case.
def iterate_old(n):
    def wrap(func):
        @functools.wraps(func)
        def replacement(*args):
            for i in xrange(n):
                func(*args)
        return replacement
    return wrap

if sys.version_info >= (2,7):
    iterate = iterate_27
else:
    iterate = iterate_old

# utilities

def random_ipv4_prefix():
    prefixlen = random.randrange(32)
    int_ip = random.randrange(IPy.MAX_IPV4_ADDRESS)
    int_ip &= 0xffffffff << (32-prefixlen)
    return IPy.IP('.'.join(map(str, (int_ip >> 24,
                                    (int_ip >> 16) & 0xff,
                                    (int_ip >> 8) & 0xff,
                                    int_ip & 0xff)))
                           + '/%d' % prefixlen)

# tests

class ParseAndBack(unittest.TestCase):

    @iterate(500)
    def testRandomValuesv4(self):
        question = random.randrange(0xffffffff)
        self.assertEqual(IPy.parseAddress(IPy.intToIp(question, 4)), (question, 4), hex(question))

    @iterate(500)
    def testRandomValuesv6(self):
        question = random.randrange(0xffffffffffffffffffffffffffffffff)
        self.assertEqual(IPy.parseAddress(IPy.intToIp(question, 6)), (question, 6), hex(question))

class TestIPSet(unittest.TestCase):

    @iterate(1000)
    def testRandomContains(self):
        prefixes = [random_ipv4_prefix() for i in xrange(random.randrange(50))]
        question = random_ipv4_prefix()
        answer = any(question in pfx for pfx in prefixes)
        ipset = IPy.IPSet(prefixes)
        self.assertEqual(question in ipset, answer,
                "%s in %s != %s (made from %s)" % (question, ipset, answer, prefixes))
        

    @iterate(1000)
    def testRandomDisjoint(self):
        prefixes1 = [random_ipv4_prefix() for i in xrange(random.randrange(50))]
        prefixes2 = [random_ipv4_prefix() for i in xrange(random.randrange(50))]
        # test disjointnes the stupid way
        disjoint = True
        for p1, p2 in itertools.product(prefixes1, prefixes2):
            if p1 in p2 or p2 in p1:
                disjoint = False
                break
        ipset1 = IPy.IPSet(prefixes1)
        ipset2 = IPy.IPSet(prefixes2)
        self.assertEqual(ipset1.isdisjoint(ipset2), disjoint,
                "%s.isdisjoint(%s) != %s" % (ipset1, ipset2, disjoint))
        self.assertEqual(ipset2.isdisjoint(ipset1), disjoint,
                "%s.isdisjoint(%s) != %s" % (ipset2, ipset1, disjoint))

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