Debian Codegraph @

test/units/module_utils/common/test_network.py

raw
1 # -*- coding: utf-8 -*-
2 # (c) 2017 Red Hat, Inc.
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4
5 from __future__ import annotations
6
7 import pytest
8
9 from ansible.module_utils.common.network import (
10 to_bits,
11 to_masklen,
12 to_netmask,
13 to_subnet,
14 to_ipv6_network,
15 is_mac,
16 is_masklen,
17 is_netmask
18 )
19
20
21 def test_to_masklen():
22 assert 24 == to_masklen('255.255.255.0')
23
24
25 def test_to_masklen_invalid():
26 with pytest.raises(ValueError):
27 to_masklen('255')
28
29
30 def test_to_netmask():
31 assert '255.0.0.0' == to_netmask(8)
32 assert '255.0.0.0' == to_netmask('8')
33
34
35 def test_to_netmask_invalid():
36 with pytest.raises(ValueError):
37 to_netmask(128)
38
39
40 def test_to_subnet():
41 result = to_subnet('192.168.1.1', 24)
42 assert '192.168.1.0/24' == result
43
44 result = to_subnet('192.168.1.1', 24, dotted_notation=True)
45 assert '192.168.1.0 255.255.255.0' == result
46
47
48 def test_to_subnet_invalid():
49 with pytest.raises(ValueError):
50 to_subnet('foo', 'bar')
51
52
53 def test_is_mac():
54 assert is_mac('52:54:00:e1:44:e0')
55 assert is_mac('52-54-00-e1-44-e0')
56 assert is_mac('AA:BB:CC:DD:EE:FF')
57 assert not is_mac('foo')
58 assert not is_mac('52:54:00:e1:44')
59 # mixed separators are not valid
60 assert not is_mac('52:54-00:e1:44:e0')
61 # by default a trailing newline is accepted (regex '$' matches before a final newline)
62 assert is_mac('52:54:00:e1:44:e0\n')
63 # strict=True anchors with \Z, so a trailing newline is rejected
64 assert not is_mac('52:54:00:e1:44:e0\n', strict=True)
65 # strict=True still accepts a well-formed address
66 assert is_mac('52:54:00:e1:44:e0', strict=True)
67
68
69 def test_is_masklen():
70 assert is_masklen(32)
71 assert not is_masklen(33)
72 assert not is_masklen('foo')
73
74
75 def test_is_netmask():
76 assert is_netmask('255.255.255.255')
77 assert is_netmask('0.0.0.0')
78 assert is_netmask('255.255.255.0')
79 assert is_netmask('255.255.254.0')
80 assert not is_netmask(24)
81 assert not is_netmask('foo')
82 # individually valid octets that are not a contiguous mask
83 assert not is_netmask('255.255.0.255')
84 assert not is_netmask('254.255.255.0')
85 assert not is_netmask('0.255.0.0')
86 assert not is_netmask('0.0.0.255')
87 # inet_aton-style legacy forms must stay rejected (they break to_masklen)
88 assert not is_netmask('0xffffff00')
89 assert not is_netmask('0377.0377.0377.0')
90 assert not is_netmask('4294967040')
91 assert not is_netmask('255.255.0')
92
93
94 def test_to_ipv6_network():
95 assert '2001:db8::' == to_ipv6_network('2001:db8::')
96 assert '2001:0db8:85a3::' == to_ipv6_network('2001:0db8:85a3:0000:0000:8a2e:0370:7334')
97 assert '2001:0db8:85a3::' == to_ipv6_network('2001:0db8:85a3:0:0:8a2e:0370:7334')
98
99
100 def test_to_bits():
101 assert to_bits('0') == '00000000'
102 assert to_bits('1') == '00000001'
103 assert to_bits('2') == '00000010'
104 assert to_bits('1337') == '10100111001'
105 assert to_bits('127.0.0.1') == '01111111000000000000000000000001'
106 assert to_bits('255.255.255.255') == '11111111111111111111111111111111'
107 assert to_bits('255.255.255.0') == '11111111111111111111111100000000'