summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2011-12-19 15:22:26 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2011-12-19 15:22:26 (GMT)
commitd1301953fe355bc6637f33a4985c950bcfc73adf (patch)
treed4a3467b30ddef5dbcd569d797b720ea1f97f60c /Lib
parentb01c32dab49be5e0a29f4a63fb77b5bba95392e6 (diff)
parent7ded21e917e8069810e31753852191f823e52c27 (diff)
downloadcpython-d1301953fe355bc6637f33a4985c950bcfc73adf.zip
cpython-d1301953fe355bc6637f33a4985c950bcfc73adf.tar.gz
cpython-d1301953fe355bc6637f33a4985c950bcfc73adf.tar.bz2
Issue #5424: add tests for inet_ntoa, inet_ntop, inet_aton and inet_pton.
Patch by Philipp Hagemeister.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_socket.py65
1 files changed, 65 insertions, 0 deletions
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index 9deee01..a7cee59 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -895,16 +895,30 @@ class GeneralModuleTests(unittest.TestCase):
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, 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'))
+ 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'))
+ 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')
def testIPv6toString(self):
if not hasattr(socket, 'inet_pton'):
@@ -916,6 +930,9 @@ class GeneralModuleTests(unittest.TestCase):
except ImportError:
return
f = lambda a: inet_pton(AF_INET6, a)
+ assertInvalid = lambda a: self.assertRaises(
+ (socket.error, ValueError), f, a
+ )
self.assertEqual(b'\x00' * 16, f('::'))
self.assertEqual(b'\x00' * 16, f('0::0'))
@@ -924,21 +941,62 @@ class GeneralModuleTests(unittest.TestCase):
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::'))
+ 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')
+ )
+ 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 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)
+ assertInvalid = lambda func,a: self.assertRaises(
+ (socket.error, 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('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)
def testStringToIPv6(self):
if not hasattr(socket, 'inet_ntop'):
@@ -950,6 +1008,9 @@ class GeneralModuleTests(unittest.TestCase):
except ImportError:
return
f = lambda a: inet_ntop(AF_INET6, a)
+ assertInvalid = lambda a: self.assertRaises(
+ (socket.error, ValueError), f, a
+ )
self.assertEqual('::', f(b'\x00' * 16))
self.assertEqual('::1', f(b'\x00' * 15 + b'\x01'))
@@ -958,6 +1019,10 @@ class GeneralModuleTests(unittest.TestCase):
f(b'\x0a\xef\x0b\x01\x05\x06\x10\x01\xff\xff\x99\x97\x00\x55\x01\x70')
)
+ assertInvalid(b'\x12' * 15)
+ assertInvalid(b'\x12' * 17)
+ assertInvalid(b'\x12' * 4)
+
# XXX The following don't test module-level functionality...
def testSockName(self):