=IP version 6 Strategy Module=

Copyright (c) 2008-2014, David P. D. Moss. All rights reserved.

{{{

>>> from netaddr.strategy.ipv6 import *

}}}

==Basic Smoke Tests==

{{{

>>> b = '0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:1111111111111111:1111111111111110'
>>> i = 4294967294
>>> t = (0, 0, 0, 0, 0, 0, 0xffff, 0xfffe)
>>> s = '::255.255.255.254'
>>> p = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xfe'

>>> bits_to_int(b) == 4294967294
True

>>> int_to_bits(i) == b
True

>>> int_to_str(i)
'::255.255.255.254'

>>> int_to_words(i)
(0, 0, 0, 0, 0, 0, 65535, 65534)

>>> int_to_packed(i)
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xfe'

>>> str_to_int(s) == 4294967294
True

>>> words_to_int(t) == 4294967294
True

>>> words_to_int(list(t)) == 4294967294
True

>>> packed_to_int(p) == 4294967294
True

}}}

==More Specific IPv6 Tests==

IPv6 string address variants that are all equivalent.

{{{

>>> i = 42540766411282592856903984951992014763
>>> str_to_int('2001:0db8:0000:0000:0000:0000:1428:57ab') == i
True

>>> str_to_int('2001:0db8:0000:0000:0000::1428:57ab') == i
True

>>> str_to_int('2001:0db8:0:0:0:0:1428:57ab') == i
True

>>> str_to_int('2001:0db8:0:0::1428:57ab') == i
True

>>> str_to_int('2001:0db8::1428:57ab') == i
True

>>> str_to_int('2001:0DB8:0000:0000:0000:0000:1428:57AB') == i
True

>>> str_to_int('2001:DB8::1428:57AB') == i
True

}}}

Intensive IPv6 string address validation testing.

Positive tests.

{{{

>>> valid_addrs = (
... #   RFC 4291
... #   Long forms.
... 'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210',
... '1080:0:0:0:8:800:200C:417A',   #   a unicast address
... 'FF01:0:0:0:0:0:0:43',      	#   a multicast address
... '0:0:0:0:0:0:0:1',          	#   the loopback address
... '0:0:0:0:0:0:0:0',          	#   the unspecified addresses
...
... #   Short forms.
... '1080::8:800:200C:417A',    	#   a unicast address
... 'FF01::43',                 	#   a multicast address
... '::1',                      	#   the loopback address
... '::',                       	#   the unspecified addresses
...
... #   IPv4 compatible forms.
... '::192.0.2.1',
... '::ffff:192.0.2.1',
... '0:0:0:0:0:0:192.0.2.1',
... '0:0:0:0:0:FFFF:192.0.2.1',
... '0:0:0:0:0:0:13.1.68.3',
... '0:0:0:0:0:FFFF:129.144.52.38',
... '::13.1.68.3',
... '::FFFF:129.144.52.38',
...
... #   Other tests.
... '1::',
... '::ffff',
... 'ffff::',
... 'ffff::ffff',
... '0:1:2:3:4:5:6:7',
... '8:9:a:b:c:d:e:f',
... '0:0:0:0:0:0:0:0',
... 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
... )

>>> for addr in valid_addrs:
...    addr, valid_str(addr) 
('FEDC:BA98:7654:3210:FEDC:BA98:7654:3210', True)
('1080:0:0:0:8:800:200C:417A', True)
('FF01:0:0:0:0:0:0:43', True)
('0:0:0:0:0:0:0:1', True)
('0:0:0:0:0:0:0:0', True)
('1080::8:800:200C:417A', True)
('FF01::43', True)
('::1', True)
('::', True)
('::192.0.2.1', True)
('::ffff:192.0.2.1', True)
('0:0:0:0:0:0:192.0.2.1', True)
('0:0:0:0:0:FFFF:192.0.2.1', True)
('0:0:0:0:0:0:13.1.68.3', True)
('0:0:0:0:0:FFFF:129.144.52.38', True)
('::13.1.68.3', True)
('::FFFF:129.144.52.38', True)
('1::', True)
('::ffff', True)
('ffff::', True)
('ffff::ffff', True)
('0:1:2:3:4:5:6:7', True)
('8:9:a:b:c:d:e:f', True)
('0:0:0:0:0:0:0:0', True)
('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', True)

}}}

Negative tests.

{{{

>>> invalid_addrs = (
... 'g:h:i:j:k:l:m:n',      #   bad chars.
... '0:0:0:0:0:0:0:0:0'     #   too long,
... '',                     #   empty string
... #   Unexpected types.
... [],
... (),
... {},
... True,
... False,
... )

>>> for addr in invalid_addrs:
...    addr, valid_str(addr)
('g:h:i:j:k:l:m:n', False)
('0:0:0:0:0:0:0:0:0', False)
([], False)
((), False)
({}, False)
(True, False)
(False, False)

}}}

String compaction tests.

{{{

>>> valid_addrs = {
... #   RFC 4291
... 'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210' : 'fedc:ba98:7654:3210:fedc:ba98:7654:3210',
... '1080:0:0:0:8:800:200C:417A' : '1080::8:800:200c:417a', #   a unicast address
... 'FF01:0:0:0:0:0:0:43' : 'ff01::43', #   a multicast address
... '0:0:0:0:0:0:0:1' : '::1',          #   the loopback address
... '0:0:0:0:0:0:0:0' : '::',           #   the unspecified addresses
... }

>>> for long_form, short_form in valid_addrs.items():
...	    int_val = str_to_int(long_form)
...	    calc_short_form = int_to_str(int_val)
...	    calc_short_form == short_form
True
True
True
True
True

}}}

IPv6 mapped and compatible IPv4 string formatting.

{{{

>>> int_to_str(0xffffff)
'::0.255.255.255'
>>> int_to_str(0xffffffff)
'::255.255.255.255'

>>> int_to_str(0x1ffffffff)
'::1:ffff:ffff'

>>> int_to_str(0xffffffffffff)
'::ffff:255.255.255.255'

>>> int_to_str(0xfffeffffffff)
'::fffe:ffff:ffff'

>>> int_to_str(0xffffffffffff)
'::ffff:255.255.255.255'

>>> int_to_str(0xfffffffffff1)
'::ffff:255.255.255.241'

>>> int_to_str(0xfffffffffffe)
'::ffff:255.255.255.254'

>>> int_to_str(0xffffffffff00)
'::ffff:255.255.255.0'

>>> int_to_str(0xffffffff0000)
'::ffff:255.255.0.0'

>>> int_to_str(0xffffff000000)
'::ffff:255.0.0.0'

>>> int_to_str(0xffff000000)
'::ff:ff00:0'

>>> int_to_str(0xffff00000000)
'::ffff:0.0.0.0'

>>> int_to_str(0x1ffff00000000)
'::1:ffff:0:0'

>>> int_to_str(0xffff00000000)
'::ffff:0.0.0.0'

}}}

== str_to_int() Behavioural Tests (legacy_mode switch) ==

The legacy_mode switch on str_to_int() is for interface compatibility only and should not effect the behaviour of this method whether set to True or False.

{{{

>>> str_to_int('::127') == 295
True

>>> str_to_int('::0x7f')
Traceback (most recent call last):
...
netaddr.core.AddrFormatError: '::0x7f' is not a valid IPv6 address string!

>>> str_to_int('::0177') == 375
True

>>> str_to_int('::127.1')
Traceback (most recent call last):
...
netaddr.core.AddrFormatError: '::127.1' is not a valid IPv6 address string!

>>> str_to_int('::0x7f.1')
Traceback (most recent call last):
...
netaddr.core.AddrFormatError: '::0x7f.1' is not a valid IPv6 address string!

>>> str_to_int('::0177.1')
Traceback (most recent call last):
...
netaddr.core.AddrFormatError: '::0177.1' is not a valid IPv6 address string!

>>> str_to_int('::127.0.0.1') == 2130706433
True

}}}
