summaryrefslogtreecommitdiffstats
path: root/netaddr/tests/ip/test_network_ops.py
blob: f682c4fef2b0e4e05c6e9c181ce8adbee58dfacc (plain)
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
import types

from netaddr import IPNetwork, cidr_merge

def test_ipnetwork_cidr_merge():
    ip_list = (
        list(IPNetwork('fe80::/120')) +
        [
            IPNetwork('192.0.2.0/24'),
            IPNetwork('192.0.4.0/25'),
            IPNetwork('192.0.4.128/25'),
        ]
        + list(map(str, IPNetwork('192.0.3.0/24')))
    )
    assert len(ip_list) == 515

    assert cidr_merge(ip_list) == [
        IPNetwork('192.0.2.0/23'),
        IPNetwork('192.0.4.0/24'),
        IPNetwork('fe80::/120'),
    ]

def test_subnetting():
    ip = IPNetwork('172.24.0.0/23')
    assert isinstance(ip.subnet(28), types.GeneratorType)

    subnets = list(ip.subnet(28))
    assert len(subnets) == 32

    assert subnets == [
        IPNetwork('172.24.0.0/28'),
        IPNetwork('172.24.0.16/28'),
        IPNetwork('172.24.0.32/28'),
        IPNetwork('172.24.0.48/28'),
        IPNetwork('172.24.0.64/28'),
        IPNetwork('172.24.0.80/28'),
        IPNetwork('172.24.0.96/28'),
        IPNetwork('172.24.0.112/28'),
        IPNetwork('172.24.0.128/28'),
        IPNetwork('172.24.0.144/28'),
        IPNetwork('172.24.0.160/28'),
        IPNetwork('172.24.0.176/28'),
        IPNetwork('172.24.0.192/28'),
        IPNetwork('172.24.0.208/28'),
        IPNetwork('172.24.0.224/28'),
        IPNetwork('172.24.0.240/28'),
        IPNetwork('172.24.1.0/28'),
        IPNetwork('172.24.1.16/28'),
        IPNetwork('172.24.1.32/28'),
        IPNetwork('172.24.1.48/28'),
        IPNetwork('172.24.1.64/28'),
        IPNetwork('172.24.1.80/28'),
        IPNetwork('172.24.1.96/28'),
        IPNetwork('172.24.1.112/28'),
        IPNetwork('172.24.1.128/28'),
        IPNetwork('172.24.1.144/28'),
        IPNetwork('172.24.1.160/28'),
        IPNetwork('172.24.1.176/28'),
        IPNetwork('172.24.1.192/28'),
        IPNetwork('172.24.1.208/28'),
        IPNetwork('172.24.1.224/28'),
        IPNetwork('172.24.1.240/28'),
    ]


def test_supernetting():
    ip = IPNetwork('192.0.2.114')
    supernets = ip.supernet(22)

    assert supernets == [
        IPNetwork('192.0.0.0/22'),
        IPNetwork('192.0.2.0/23'),
        IPNetwork('192.0.2.0/24'),
        IPNetwork('192.0.2.0/25'),
        IPNetwork('192.0.2.64/26'),
        IPNetwork('192.0.2.96/27'),
        IPNetwork('192.0.2.112/28'),
        IPNetwork('192.0.2.112/29'),
        IPNetwork('192.0.2.112/30'),
        IPNetwork('192.0.2.114/31'),
    ]