summaryrefslogtreecommitdiffstats
path: root/netaddr/tests/strategy/test_ipv4_strategy.py
diff options
context:
space:
mode:
Diffstat (limited to 'netaddr/tests/strategy/test_ipv4_strategy.py')
-rw-r--r--netaddr/tests/strategy/test_ipv4_strategy.py83
1 files changed, 83 insertions, 0 deletions
diff --git a/netaddr/tests/strategy/test_ipv4_strategy.py b/netaddr/tests/strategy/test_ipv4_strategy.py
new file mode 100644
index 0000000..607d816
--- /dev/null
+++ b/netaddr/tests/strategy/test_ipv4_strategy.py
@@ -0,0 +1,83 @@
+import sys
+
+import pytest
+
+from netaddr import INET_PTON, AddrFormatError
+from netaddr.strategy import ipv4
+
+
+def test_strategy_ipv4():
+ b = '11000000.00000000.00000010.00000001'
+ i = 3221225985
+ t = (192, 0, 2, 1)
+ s = '192.0.2.1'
+ bin_val = '0b11000000000000000000001000000001'
+
+ assert ipv4.bits_to_int(b) == i
+ assert ipv4.int_to_bits(i) == b
+ assert ipv4.int_to_str(i) == s
+ assert ipv4.int_to_words(i) == t
+ assert ipv4.int_to_bin(i) == bin_val
+ assert ipv4.int_to_bin(i) == bin_val
+ assert ipv4.bin_to_int(bin_val) == i
+ assert ipv4.words_to_int(t) == i
+ assert ipv4.words_to_int(list(t)) == i
+ assert ipv4.valid_bin(bin_val)
+
+
+@pytest.mark.skipif(sys.version_info > (3,), reason="requires python 2.x")
+def test_strategy_ipv4_py2():
+ i = 3221225985
+ p = '\xc0\x00\x02\x01'
+ assert ipv4.int_to_packed(i) == p
+ assert ipv4.packed_to_int(p) == i
+
+
+@pytest.mark.skipif(sys.version_info < (3,), reason="requires python 3.x")
+def test_strategy_ipv4_py3():
+ i = 3221225985
+ p = b'\xc0\x00\x02\x01'
+ assert ipv4.int_to_packed(i) == p
+ assert ipv4.packed_to_int(p) == i
+
+
+def test_strategy_inet_aton_behaviour():
+ # inet_aton() is a very old system call and is very permissive with
+ # regard to what is assume is a valid IPv4 address. Unfortunately, it
+ # is also the most widely used by system software used in software today,
+ # so netaddr supports this behaviour by default.
+
+ assert ipv4.str_to_int('127') == 127
+ assert ipv4.str_to_int('0x7f') == 127
+ assert ipv4.str_to_int('0177') == 127
+ assert ipv4.str_to_int('127.1') == 2130706433
+ assert ipv4.str_to_int('0x7f.1') == 2130706433
+ assert ipv4.str_to_int('0177.1') == 2130706433
+ assert ipv4.str_to_int('127.0.0.1') == 2130706433
+
+
+def test_strategy_inet_pton_behaviour():
+ # inet_pton() is a newer system call that supports both IPv4 and IPv6.
+ # It is a lot more strict about what it deems to be a valid IPv4 address
+ # and doesn't support many of the features found in inet_aton() such as
+ # support for non- decimal octets, partial numbers of octets, etc.
+
+ with pytest.raises(AddrFormatError):
+ ipv4.str_to_int('127', flags=INET_PTON)
+
+ with pytest.raises(AddrFormatError):
+ ipv4.str_to_int('0x7f', flags=INET_PTON)
+
+ with pytest.raises(AddrFormatError):
+ ipv4.str_to_int('0177', flags=INET_PTON)
+
+ with pytest.raises(AddrFormatError):
+ ipv4.str_to_int('127.1', flags=INET_PTON)
+
+ with pytest.raises(AddrFormatError):
+ ipv4.str_to_int('0x7f.1', flags=INET_PTON)
+
+ with pytest.raises(AddrFormatError):
+ ipv4.str_to_int('0177.1', flags=INET_PTON)
+
+ assert ipv4.str_to_int('127.0.0.1', flags=INET_PTON) == 2130706433