Adding network scanning
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
=IEEE EUI-48 Strategy Module=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr.strategy.eui48 import *
|
||||
|
||||
}}}
|
||||
|
||||
==Basic Smoke Tests==
|
||||
|
||||
{{{
|
||||
|
||||
>>> b = '00000000-00001111-00011111-00010010-11100111-00110011'
|
||||
>>> i = 64945841971
|
||||
>>> t = (0x0, 0x0f, 0x1f, 0x12, 0xe7, 0x33)
|
||||
>>> s = '00-0F-1F-12-E7-33'
|
||||
>>> p = '\x00\x0f\x1f\x12\xe73'
|
||||
|
||||
>>> bits_to_int(b) == 64945841971
|
||||
True
|
||||
|
||||
>>> int_to_bits(i) == b
|
||||
True
|
||||
|
||||
>>> int_to_str(i)
|
||||
'00-0F-1F-12-E7-33'
|
||||
|
||||
>>> int_to_words(i)
|
||||
(0, 15, 31, 18, 231, 51)
|
||||
|
||||
>>> int_to_packed(i)
|
||||
'\x00\x0f\x1f\x12\xe73'
|
||||
|
||||
>>> str_to_int(s) == 64945841971
|
||||
True
|
||||
|
||||
>>> words_to_int(t) == 64945841971
|
||||
True
|
||||
|
||||
>>> words_to_int(list(t)) == 64945841971
|
||||
True
|
||||
|
||||
>>> packed_to_int(p) == 64945841971
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
==Smoke Tests With Alternate Dialects==
|
||||
|
||||
{{{
|
||||
|
||||
>>> b = '00000000:00001111:00011111:00010010:11100111:00110011'
|
||||
>>> i = 64945841971
|
||||
>>> t = (0x0, 0x0f, 0x1f, 0x12, 0xe7, 0x33)
|
||||
>>> s = '0:f:1f:12:e7:33'
|
||||
>>> p = '\x00\x0f\x1f\x12\xe73'
|
||||
|
||||
>>> bits_to_int(b, mac_unix) == 64945841971
|
||||
True
|
||||
|
||||
>>> int_to_bits(i, mac_unix) == b
|
||||
True
|
||||
|
||||
>>> int_to_str(i, mac_unix)
|
||||
'0:f:1f:12:e7:33'
|
||||
|
||||
>>> int_to_str(i, mac_cisco)
|
||||
'000f.1f12.e733'
|
||||
|
||||
>>> int_to_str(i, mac_unix)
|
||||
'0:f:1f:12:e7:33'
|
||||
|
||||
>>> int_to_words(i, mac_unix)
|
||||
(0, 15, 31, 18, 231, 51)
|
||||
|
||||
>>> int_to_packed(i)
|
||||
'\x00\x0f\x1f\x12\xe73'
|
||||
|
||||
>>> str_to_int(s) == 64945841971
|
||||
True
|
||||
|
||||
>>> words_to_int(t, mac_unix) == 64945841971
|
||||
True
|
||||
|
||||
>>> words_to_int(list(t), mac_unix) == 64945841971
|
||||
True
|
||||
|
||||
>>> packed_to_int(p) == 64945841971
|
||||
True
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,130 @@
|
||||
=IP version 4 Strategy Module=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
Uses TEST-NET references throughout, as described in RFC 3330.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr.strategy.ipv4 import *
|
||||
|
||||
}}}
|
||||
|
||||
==Basic Smoke Tests==
|
||||
|
||||
{{{
|
||||
|
||||
>>> b = '11000000.00000000.00000010.00000001'
|
||||
>>> i = 3221225985
|
||||
>>> t = (192, 0, 2, 1)
|
||||
>>> s = '192.0.2.1'
|
||||
>>> p = '\xc0\x00\x02\x01'
|
||||
>>> bin_val = '0b11000000000000000000001000000001'
|
||||
|
||||
>>> bits_to_int(b) == 3221225985
|
||||
True
|
||||
|
||||
>>> int_to_bits(i)
|
||||
'11000000.00000000.00000010.00000001'
|
||||
|
||||
>>> int_to_str(i)
|
||||
'192.0.2.1'
|
||||
|
||||
>>> int_to_words(i) == (192, 0, 2, 1)
|
||||
True
|
||||
|
||||
>>> int_to_packed(i)
|
||||
'\xc0\x00\x02\x01'
|
||||
|
||||
>>> int_to_bin(i)
|
||||
'0b11000000000000000000001000000001'
|
||||
|
||||
>>> int_to_bin(i)
|
||||
'0b11000000000000000000001000000001'
|
||||
|
||||
>>> bin_to_int(bin_val) == 3221225985
|
||||
True
|
||||
|
||||
>>> words_to_int(t) == 3221225985
|
||||
True
|
||||
|
||||
>>> words_to_int(list(t)) == 3221225985
|
||||
True
|
||||
|
||||
>>> packed_to_int(p) == 3221225985
|
||||
True
|
||||
|
||||
>>> valid_bin(bin_val)
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
== inet_aton() Behavioural Tests ==
|
||||
|
||||
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.
|
||||
|
||||
{{{
|
||||
|
||||
>>> str_to_int('127') == 127
|
||||
True
|
||||
|
||||
>>> str_to_int('0x7f') == 127
|
||||
True
|
||||
|
||||
>>> str_to_int('0177') == 127
|
||||
True
|
||||
|
||||
>>> str_to_int('127.1') == 2130706433
|
||||
True
|
||||
|
||||
>>> str_to_int('0x7f.1') == 2130706433
|
||||
True
|
||||
|
||||
>>> str_to_int('0177.1') == 2130706433
|
||||
True
|
||||
|
||||
>>> str_to_int('127.0.0.1') == 2130706433
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
== inet_pton() Behavioural Tests ==
|
||||
|
||||
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.
|
||||
|
||||
{{{
|
||||
|
||||
>>> str_to_int('127', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: '127' is not a valid IPv4 address string!
|
||||
|
||||
>>> str_to_int('0x7f', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: '0x7f' is not a valid IPv4 address string!
|
||||
|
||||
>>> str_to_int('0177', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: '0177' is not a valid IPv4 address string!
|
||||
|
||||
>>> str_to_int('127.1', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: '127.1' is not a valid IPv4 address string!
|
||||
|
||||
>>> str_to_int('0x7f.1', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: '0x7f.1' is not a valid IPv4 address string!
|
||||
|
||||
>>> str_to_int('0177.1', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: '0177.1' is not a valid IPv4 address string!
|
||||
|
||||
>>> str_to_int('127.0.0.1', flags=INET_PTON) == 2130706433
|
||||
True
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,290 @@
|
||||
=IP version 6 Strategy Module=
|
||||
|
||||
Copyright (c) 2008-2012, 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 = '\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)
|
||||
'\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):
|
||||
...
|
||||
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):
|
||||
...
|
||||
AddrFormatError: '::127.1' is not a valid IPv6 address string!
|
||||
|
||||
>>> str_to_int('::0x7f.1')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: '::0x7f.1' is not a valid IPv6 address string!
|
||||
|
||||
>>> str_to_int('::0177.1')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: '::0177.1' is not a valid IPv6 address string!
|
||||
|
||||
>>> str_to_int('::127.0.0.1') == 2130706433
|
||||
True
|
||||
|
||||
}}}
|
||||
Reference in New Issue
Block a user