@@ -803,11 +803,6 @@ def testHostnameRes(self):
803
803
self .fail ("Error testing host resolution mechanisms. (fqdn: %s, all: %s)" % (fqhn , repr (all_host_names )))
804
804
805
805
def test_host_resolution (self ):
806
- for addr in ['0.1.1.~1' , '1+.1.1.1' , '::1q' , '::1::2' ,
807
- '1:1:1:1:1:1:1:1:1' ]:
808
- self .assertRaises (OSError , socket .gethostbyname , addr )
809
- self .assertRaises (OSError , socket .gethostbyaddr , addr )
810
-
811
806
for addr in [support .HOST , '10.0.0.1' , '255.255.255.255' ]:
812
807
self .assertEqual (socket .gethostbyname (addr ), addr )
813
808
@@ -816,6 +811,21 @@ def test_host_resolution(self):
816
811
for host in [support .HOST ]:
817
812
self .assertIn (host , socket .gethostbyaddr (host )[2 ])
818
813
814
+ def test_host_resolution_bad_address (self ):
815
+ # These are all malformed IP addresses and expected not to resolve to
816
+ # any result. But some ISPs, e.g. AWS, may successfully resolve these
817
+ # IPs.
818
+ explanation = (
819
+ "resolving an invalid IP address did not raise OSError; "
820
+ "can be caused by a broken DNS server"
821
+ )
822
+ for addr in ['0.1.1.~1' , '1+.1.1.1' , '::1q' , '::1::2' ,
823
+ '1:1:1:1:1:1:1:1:1' ]:
824
+ with self .assertRaises (OSError ):
825
+ socket .gethostbyname (addr )
826
+ with self .assertRaises (OSError , msg = explanation ):
827
+ socket .gethostbyaddr (addr )
828
+
819
829
@unittest .skipUnless (hasattr (socket , 'sethostname' ), "test needs socket.sethostname()" )
820
830
@unittest .skipUnless (hasattr (socket , 'gethostname' ), "test needs socket.gethostname()" )
821
831
def test_sethostname (self ):
0 commit comments