netwatch.py/netaddr-0.7.10/netaddr/tests/3.x/ip/abbreviated.txt

203 lines
3.8 KiB
Plaintext

=Abbreviated CIDR Tests=
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
{{{
>>> from netaddr import *
}}}
Abbreviation tests.
{{{
>>> ranges = (
... (IPAddress('::'), IPAddress('::')),
... (IPAddress('0.0.0.0'), IPAddress('255.255.255.255')),
... (IPAddress('::'), IPAddress('::255.255.255.255')),
... (IPAddress('0.0.0.0'), IPAddress('0.0.0.0')),
... )
>>> sorted(ranges)
[(IPAddress('0.0.0.0'), IPAddress('0.0.0.0')), (IPAddress('0.0.0.0'), IPAddress('255.255.255.255')), (IPAddress('::'), IPAddress('::')), (IPAddress('::'), IPAddress('::255.255.255.255'))]
# Integer values.
>>> cidr_abbrev_to_verbose(-1)
-1
# Class A
>>> cidr_abbrev_to_verbose(0)
'0.0.0.0/8'
>>> cidr_abbrev_to_verbose(10)
'10.0.0.0/8'
>>> cidr_abbrev_to_verbose(127)
'127.0.0.0/8'
# Class B
>>> cidr_abbrev_to_verbose(128)
'128.0.0.0/16'
>>> cidr_abbrev_to_verbose(191)
'191.0.0.0/16'
# Class C
>>> cidr_abbrev_to_verbose(192)
'192.0.0.0/24'
>>> cidr_abbrev_to_verbose(223)
'223.0.0.0/24'
# Class D (multicast)
>>> cidr_abbrev_to_verbose(224)
'224.0.0.0/4'
>>> cidr_abbrev_to_verbose(225)
'225.0.0.0/4'
>>> cidr_abbrev_to_verbose(239)
'239.0.0.0/4'
# Class E (reserved)
>>> cidr_abbrev_to_verbose(240)
'240.0.0.0/32'
>>> cidr_abbrev_to_verbose(254)
'254.0.0.0/32'
>>> cidr_abbrev_to_verbose(255)
'255.0.0.0/32'
>>> cidr_abbrev_to_verbose(256)
256
# String values.
>>> cidr_abbrev_to_verbose('-1')
'-1'
# Class A
>>> cidr_abbrev_to_verbose('0')
'0.0.0.0/8'
>>> cidr_abbrev_to_verbose('10')
'10.0.0.0/8'
>>> cidr_abbrev_to_verbose('127')
'127.0.0.0/8'
# Class B
>>> cidr_abbrev_to_verbose('128')
'128.0.0.0/16'
>>> cidr_abbrev_to_verbose('191')
'191.0.0.0/16'
# Class C
>>> cidr_abbrev_to_verbose('192')
'192.0.0.0/24'
>>> cidr_abbrev_to_verbose('223')
'223.0.0.0/24'
# Class D (multicast)
>>> cidr_abbrev_to_verbose('224')
'224.0.0.0/4'
>>> cidr_abbrev_to_verbose('225')
'225.0.0.0/4'
>>> cidr_abbrev_to_verbose('239')
'239.0.0.0/4'
# Class E (reserved)
>>> cidr_abbrev_to_verbose('240')
'240.0.0.0/32'
>>> cidr_abbrev_to_verbose('254')
'254.0.0.0/32'
>>> cidr_abbrev_to_verbose('255')
'255.0.0.0/32'
>>> cidr_abbrev_to_verbose('256')
'256'
>>> cidr_abbrev_to_verbose('128/8')
'128.0.0.0/8'
>>> cidr_abbrev_to_verbose('128.0/8')
'128.0.0.0/8'
>>> cidr_abbrev_to_verbose('128.0.0.0/8')
'128.0.0.0/8'
>>> cidr_abbrev_to_verbose('128.0.0/8')
'128.0.0.0/8'
>>> cidr_abbrev_to_verbose('192.168')
'192.168.0.0/24'
>>> cidr_abbrev_to_verbose('192.0.2')
'192.0.2.0/24'
>>> cidr_abbrev_to_verbose('192.0.2.0')
'192.0.2.0/24'
>>> cidr_abbrev_to_verbose('0.0.0.0')
'0.0.0.0/8'
# No IPv6 support current.
>>> cidr_abbrev_to_verbose('::/128')
'::/128'
# IPv6 proper, not IPv4 mapped?
>>> cidr_abbrev_to_verbose('::10/128')
'::10/128'
>>> cidr_abbrev_to_verbose('0.0.0.0.0')
'0.0.0.0.0'
>>> cidr_abbrev_to_verbose('')
''
>>> cidr_abbrev_to_verbose(None)
>>> cidr_abbrev_to_verbose([])
[]
>>> cidr_abbrev_to_verbose({})
{}
}}}
Negative testing.
{{{
>>> cidr_abbrev_to_verbose('192.0.2.0')
'192.0.2.0/24'
>>> cidr_abbrev_to_verbose('192.0.2.0/32')
'192.0.2.0/32'
#FIXME: >>> cidr_abbrev_to_verbose('192.0.2.0/33')
Traceback (most recent call last):
...
ValueError: prefixlen in address '192.0.2.0/33' out of range for IPv4!
}}}
IPv4 octet expansion routine.
{{{
>>> from netaddr.strategy import ipv4
>>> ipv4.expand_partial_address('10')
'10.0.0.0'
>>> ipv4.expand_partial_address('10.1')
'10.1.0.0'
>>> ipv4.expand_partial_address('192.168.1')
'192.168.1.0'
}}}
IPNetwork constructor testing.
{{{
>>> IPNetwork('192.168/16')
IPNetwork('192.168.0.0/16')
>>> IPNetwork('192.168.0.15')
IPNetwork('192.168.0.15/32')
>>> IPNetwork('192.168')
IPNetwork('192.168.0.0/32')
>>> IPNetwork('192.168', implicit_prefix=True)
IPNetwork('192.168.0.0/24')
>>> IPNetwork('192.168', True)
IPNetwork('192.168.0.0/24')
>>> IPNetwork('10.0.0.1', True)
IPNetwork('10.0.0.1/8')
}}}