def testStringToIPv6(self): try: from socket import inet_ntop, AF_INET6, has_ipv6 if not has_ipv6: self.skipTest('IPv6 not available') except ImportError: self.skipTest('could not import needed symbols from socket') if sys.platform == "win32": try: inet_ntop(AF_INET6, b'\x00' * 16) except OSError as e: if e.winerror == 10022: self.skipTest('IPv6 might not be supported') f = lambda a: inet_ntop(AF_INET6, a) assertInvalid = lambda a: self.assertRaises( (OSError, ValueError), f, a) self.assertEqual('::', f(b'\x00' * 16)) self.assertEqual('::1', f(b'\x00' * 15 + b'\x01')) self.assertEqual( 'aef:b01:506:1001:ffff:9997:55:170', f(b'\x0a\xef\x0b\x01\x05\x06\x10\x01\xff\xff\x99\x97\x00\x55\x01\x70' )) self.assertEqual('::1', f(bytearray(b'\x00' * 15 + b'\x01'))) assertInvalid(b'\x12' * 15) assertInvalid(b'\x12' * 17) assertInvalid(b'\x12' * 4)
def testStringToIPv4(self): if not hasattr(socket, 'inet_ntop'): return # No inet_ntop() on this platform from socket import inet_ntoa as f, inet_ntop, AF_INET g = lambda a: inet_ntop(AF_INET, a) self.assertEquals('1.0.1.0', f('\x01\x00\x01\x00')) self.assertEquals('170.85.170.85', f('\xaa\x55\xaa\x55')) self.assertEquals('255.255.255.255', f('\xff\xff\xff\xff')) self.assertEquals('1.2.3.4', f('\x01\x02\x03\x04')) self.assertEquals('1.0.1.0', g('\x01\x00\x01\x00')) self.assertEquals('170.85.170.85', g('\xaa\x55\xaa\x55')) self.assertEquals('255.255.255.255', g('\xff\xff\xff\xff'))
def testStringToIPv4(self): if not hasattr(socket, 'inet_ntop'): return # No inet_ntop() on this platform from socket import inet_ntoa as f, inet_ntop, AF_INET g = lambda a: inet_ntop(AF_INET, a) self.assertEquals('1.0.1.0', f(b'\x01\x00\x01\x00')) self.assertEquals('170.85.170.85', f(b'\xaa\x55\xaa\x55')) self.assertEquals('255.255.255.255', f(b'\xff\xff\xff\xff')) self.assertEquals('1.2.3.4', f(b'\x01\x02\x03\x04')) self.assertEquals('1.0.1.0', g(b'\x01\x00\x01\x00')) self.assertEquals('170.85.170.85', g(b'\xaa\x55\xaa\x55')) self.assertEquals('255.255.255.255', g(b'\xff\xff\xff\xff'))
def testIPv4toString(self): if not hasattr(socket, 'inet_pton'): return # No inet_pton() on this platform from socket import inet_aton as f, inet_pton, AF_INET g = lambda a: inet_pton(AF_INET, a) self.assertEquals('\x00\x00\x00\x00', f('0.0.0.0')) self.assertEquals('\xff\x00\xff\x00', f('255.0.255.0')) self.assertEquals('\xaa\xaa\xaa\xaa', f('170.170.170.170')) self.assertEquals('\x01\x02\x03\x04', f('1.2.3.4')) self.assertEquals('\x00\x00\x00\x00', g('0.0.0.0')) self.assertEquals('\xff\x00\xff\x00', g('255.0.255.0')) self.assertEquals('\xaa\xaa\xaa\xaa', g('170.170.170.170'))
def testStringToIPv4(self): if not hasattr(socket, "inet_ntop"): return # No inet_ntop() on this platform from socket import inet_ntoa as f, inet_ntop, AF_INET g = lambda a: inet_ntop(AF_INET, a) self.assertEquals("1.0.1.0", f("\x01\x00\x01\x00")) self.assertEquals("170.85.170.85", f("\xaa\x55\xaa\x55")) self.assertEquals("255.255.255.255", f("\xff\xff\xff\xff")) self.assertEquals("1.2.3.4", f("\x01\x02\x03\x04")) self.assertEquals("1.0.1.0", g("\x01\x00\x01\x00")) self.assertEquals("170.85.170.85", g("\xaa\x55\xaa\x55")) self.assertEquals("255.255.255.255", g("\xff\xff\xff\xff"))
def testStringToIPv6(self): if not hasattr(socket, 'inet_ntop'): return # No inet_ntop() on this platform try: from socket import inet_ntop, AF_INET6, has_ipv6 if not has_ipv6: return except ImportError: return f = lambda a: inet_ntop(AF_INET6, a) self.assertEquals('::', f('\x00' * 16)) self.assertEquals('::1', f('\x00' * 15 + '\x01')) self.assertEquals( 'aef:b01:506:1001:ffff:9997:55:170', f('\x0a\xef\x0b\x01\x05\x06\x10\x01\xff\xff\x99\x97\x00\x55\x01\x70') )
def testStringToIPv6(self): if not hasattr(socket, 'inet_ntop'): return # No inet_ntop() on this platform try: from socket import inet_ntop, AF_INET6, has_ipv6 if not has_ipv6: return except ImportError: return f = lambda a: inet_ntop(AF_INET6, a) self.assertEquals('::', f(b'\x00' * 16)) self.assertEquals('::1', f(b'\x00' * 15 + b'\x01')) self.assertEquals( 'aef:b01:506:1001:ffff:9997:55:170', f(b'\x0a\xef\x0b\x01\x05\x06\x10\x01\xff\xff\x99\x97\x00\x55\x01\x70') )
def testIPv6toString(self): if not hasattr(socket, 'inet_pton'): return # No inet_pton() on this platform try: from socket import inet_pton, AF_INET6, has_ipv6 if not has_ipv6: return except ImportError: return f = lambda a: inet_pton(AF_INET6, a) self.assertEquals('\x00' * 16, f('::')) self.assertEquals('\x00' * 16, f('0::0')) self.assertEquals('\x00\x01' + '\x00' * 14, f('1::')) self.assertEquals( '\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae', f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae'))
def testIPv4toString(self): if not hasattr(socket, "inet_pton"): return # No inet_pton() on this platform from socket import inet_aton as f, inet_pton, AF_INET g = lambda a: inet_pton(AF_INET, a) self.assertEquals("\x00\x00\x00\x00", f("0.0.0.0")) self.assertEquals("\xff\x00\xff\x00", f("255.0.255.0")) self.assertEquals("\xaa\xaa\xaa\xaa", f("170.170.170.170")) self.assertEquals("\x01\x02\x03\x04", f("1.2.3.4")) self.assertEquals("\xff\xff\xff\xff", f("255.255.255.255")) self.assertEquals("\x00\x00\x00\x00", g("0.0.0.0")) self.assertEquals("\xff\x00\xff\x00", g("255.0.255.0")) self.assertEquals("\xaa\xaa\xaa\xaa", g("170.170.170.170")) self.assertEquals("\xff\xff\xff\xff", g("255.255.255.255"))
def testStringToIPv6(self): if not hasattr(socket, "inet_ntop"): return # No inet_ntop() on this platform try: from socket import inet_ntop, AF_INET6, has_ipv6 if not has_ipv6: return except ImportError: return f = lambda a: inet_ntop(AF_INET6, a) self.assertEquals("::", f(b"\x00" * 16)) self.assertEquals("::1", f(b"\x00" * 15 + b"\x01")) self.assertEquals( "aef:b01:506:1001:ffff:9997:55:170", f(b"\x0a\xef\x0b\x01\x05\x06\x10\x01\xff\xff\x99\x97\x00\x55\x01\x70") )
def testIPv6toString(self): if not hasattr(socket, 'inet_pton'): return # No inet_pton() on this platform try: from socket import inet_pton, AF_INET6, has_ipv6 if not has_ipv6: return except ImportError: return f = lambda a: inet_pton(AF_INET6, a) self.assertEquals('\x00' * 16, f('::')) self.assertEquals('\x00' * 16, f('0::0')) self.assertEquals('\x00\x01' + '\x00' * 14, f('1::')) self.assertEquals( '\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae', f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae') )
def testIPv6toString(self): if not hasattr(socket, "inet_pton"): return # No inet_pton() on this platform try: from socket import inet_pton, AF_INET6, has_ipv6 if not has_ipv6: return except ImportError: return f = lambda a: inet_pton(AF_INET6, a) self.assertEquals("\x00" * 16, f("::")) self.assertEquals("\x00" * 16, f("0::0")) self.assertEquals("\x00\x01" + "\x00" * 14, f("1::")) self.assertEquals( "\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae", f("45ef:76cb:1a:56ef:afeb:bac:1924:aeae"), )
def testStringToIPv4(self): from socket import inet_ntoa as f, inet_ntop, AF_INET g = lambda a: inet_ntop(AF_INET, a) assertInvalid = lambda func, a: self.assertRaises( (socket.error, OSError, ValueError), func, a) self.assertEqual('1.0.1.0', f(b'\x01\x00\x01\x00')) self.assertEqual('170.85.170.85', f(b'\xaa\x55\xaa\x55')) self.assertEqual('255.255.255.255', f(b'\xff\xff\xff\xff')) self.assertEqual('1.2.3.4', f(b'\x01\x02\x03\x04')) assertInvalid(f, b'\x00' * 3) assertInvalid(f, b'\x00' * 5) assertInvalid(f, b'\x00' * 16) self.assertEqual('170.85.170.85', f(bytearray(b'\xaa\x55\xaa\x55'))) self.assertEqual('1.0.1.0', g(b'\x01\x00\x01\x00')) self.assertEqual('170.85.170.85', g(b'\xaa\x55\xaa\x55')) self.assertEqual('255.255.255.255', g(b'\xff\xff\xff\xff')) assertInvalid(g, b'\x00' * 3) assertInvalid(g, b'\x00' * 5) assertInvalid(g, b'\x00' * 16) self.assertEqual('170.85.170.85', g(bytearray(b'\xaa\x55\xaa\x55')))
def testIPv6toString(self): try: from socket import inet_pton, AF_INET6, has_ipv6 if not has_ipv6: self.skipTest('IPv6 not available') except ImportError: self.skipTest('could not import needed symbols from socket') if sys.platform == "win32": try: inet_pton(AF_INET6, '::') except OSError as e: if e.winerror == 10022: self.skipTest('IPv6 might not be supported') f = lambda a: inet_pton(AF_INET6, a) assertInvalid = lambda a: self.assertRaises( (OSError, ValueError), f, a) self.assertEqual(b'\x00' * 16, f('::')) self.assertEqual(b'\x00' * 16, f('0::0')) self.assertEqual(b'\x00\x01' + b'\x00' * 14, f('1::')) self.assertEqual( b'\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae', f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae')) self.assertEqual( b'\xad\x42\x0a\xbc' + b'\x00' * 4 + b'\x01\x27\x00\x00\x02\x54\x00\x02', f('ad42:abc::127:0:254:2')) self.assertEqual(b'\x00\x12\x00\x0a' + b'\x00' * 12, f('12:a::')) self.assertEqual(b'\x00' * 16, f(u'::')) self.assertEqual(b'\x00' * 16, f(u'0::0')) self.assertEqual(b'\x00\x01' + b'\x00' * 14, f(u'1::')) self.assertEqual( b'\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae', f(u'45ef:76cb:1a:56ef:afeb:bac:1924:aeae')) self.assertEqual( b'\xad\x42\x0a\xbc' + b'\x00' * 4 + b'\x01\x27\x00\x00\x02\x54\x00\x02', f(u'ad42:abc::127:0:254:2')) self.assertEqual(b'\x00\x12\x00\x0a' + b'\x00' * 12, f(u'12:a::')) self.assertEqual(b'\x00' * 16, f(b'::')) self.assertEqual(b'\x00' * 16, f(b'0::0')) self.assertEqual(b'\x00\x01' + b'\x00' * 14, f(b'1::')) self.assertEqual( b'\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae', f(b'45ef:76cb:1a:56ef:afeb:bac:1924:aeae')) self.assertEqual( b'\xad\x42\x0a\xbc' + b'\x00' * 4 + b'\x01\x27\x00\x00\x02\x54\x00\x02', f(b'ad42:abc::127:0:254:2')) self.assertEqual(b'\x00\x12\x00\x0a' + b'\x00' * 12, f(b'12:a::')) assertInvalid('0x20::') assertInvalid(':::') assertInvalid('::0::') assertInvalid('1::abc::') assertInvalid('1::abc::def') assertInvalid('1:2:3:4:5:6:') assertInvalid('1:2:3:4:5:6') assertInvalid('1:2:3:4:5:6:7:8:') assertInvalid('1:2:3:4:5:6:7:8:0') self.assertEqual(b'\x00' * 12 + b'\xfe\x2a\x17\x40', f('::254.42.23.64')) self.assertEqual( b'\x00\x42' + b'\x00' * 8 + b'\xa2\x9b\xfe\x2a\x17\x40', f('42::a29b:254.42.23.64')) self.assertEqual( b'\x00\x42\xa8\xb9\x00\x00\x00\x02\xff\xff\xa2\x9b\xfe\x2a\x17\x40', f('42:a8b9:0:2:ffff:a29b:254.42.23.64')) self.assertEqual(b'\x00' * 12 + b'\xfe\x2a\x17\x40', f(u'::254.42.23.64')) self.assertEqual( b'\x00\x42' + b'\x00' * 8 + b'\xa2\x9b\xfe\x2a\x17\x40', f(u'42::a29b:254.42.23.64')) self.assertEqual( b'\x00\x42\xa8\xb9\x00\x00\x00\x02\xff\xff\xa2\x9b\xfe\x2a\x17\x40', f(u'42:a8b9:0:2:ffff:a29b:254.42.23.64')) self.assertEqual(b'\x00' * 12 + b'\xfe\x2a\x17\x40', f(b'::254.42.23.64')) self.assertEqual( b'\x00\x42' + b'\x00' * 8 + b'\xa2\x9b\xfe\x2a\x17\x40', f(b'42::a29b:254.42.23.64')) self.assertEqual( b'\x00\x42\xa8\xb9\x00\x00\x00\x02\xff\xff\xa2\x9b\xfe\x2a\x17\x40', f(b'42:a8b9:0:2:ffff:a29b:254.42.23.64')) assertInvalid('255.254.253.252') assertInvalid('1::260.2.3.0') assertInvalid('1::0.be.e.0') assertInvalid('1:2:3:4:5:6:7:1.2.3.4') assertInvalid('::1.2.3.4:0') assertInvalid('0.100.200.0:3:4:5:6:7:8')
def testIPv4toString(self): from socket import inet_aton as f, inet_pton, AF_INET g = lambda a: inet_pton(AF_INET, a) assertInvalid = lambda func, a: self.assertRaises( (socket.error, OSError, ValueError), func, a) self.assertEqual(b'\x00\x00\x00\x00', f('0.0.0.0')) self.assertEqual(b'\xff\x00\xff\x00', f('255.0.255.0')) self.assertEqual(b'\xaa\xaa\xaa\xaa', f('170.170.170.170')) self.assertEqual(b'\x01\x02\x03\x04', f('1.2.3.4')) self.assertEqual(b'\xff\xff\xff\xff', f('255.255.255.255')) self.assertEqual(b'\x00\x00\x00\x00', f(u'0.0.0.0')) self.assertEqual(b'\xff\x00\xff\x00', f(u'255.0.255.0')) self.assertEqual(b'\xaa\xaa\xaa\xaa', f(u'170.170.170.170')) self.assertEqual(b'\x01\x02\x03\x04', f(u'1.2.3.4')) self.assertEqual(b'\xff\xff\xff\xff', f(u'255.255.255.255')) self.assertEqual(b'\x00\x00\x00\x00', f(b'0.0.0.0')) self.assertEqual(b'\xff\x00\xff\x00', f(b'255.0.255.0')) self.assertEqual(b'\xaa\xaa\xaa\xaa', f(b'170.170.170.170')) self.assertEqual(b'\x01\x02\x03\x04', f(b'1.2.3.4')) self.assertEqual(b'\xff\xff\xff\xff', f(b'255.255.255.255')) assertInvalid(f, '0.0.0.') assertInvalid(f, '300.0.0.0') assertInvalid(f, 'a.0.0.0') assertInvalid(f, '1.2.3.4.5') assertInvalid(f, '::1') self.assertEqual(b'\x00\x00\x00\x00', g('0.0.0.0')) self.assertEqual(b'\xff\x00\xff\x00', g('255.0.255.0')) self.assertEqual(b'\xaa\xaa\xaa\xaa', g('170.170.170.170')) self.assertEqual(b'\xff\xff\xff\xff', g('255.255.255.255')) self.assertEqual(b'\x00\x00\x00\x00', g(u'0.0.0.0')) self.assertEqual(b'\xff\x00\xff\x00', g(u'255.0.255.0')) self.assertEqual(b'\xaa\xaa\xaa\xaa', g(u'170.170.170.170')) self.assertEqual(b'\xff\xff\xff\xff', g(u'255.255.255.255')) self.assertEqual(b'\x00\x00\x00\x00', g(b'0.0.0.0')) self.assertEqual(b'\xff\x00\xff\x00', g(b'255.0.255.0')) self.assertEqual(b'\xaa\xaa\xaa\xaa', g(b'170.170.170.170')) self.assertEqual(b'\xff\xff\xff\xff', g(b'255.255.255.255')) assertInvalid(g, '0.0.0.') assertInvalid(g, '300.0.0.0') assertInvalid(g, 'a.0.0.0') assertInvalid(g, '1.2.3.4.5') assertInvalid(g, '::1')