1111import sys
1212import threading
1313import time
14+ from unittest import mock
1415
1516import pytest
1617
5657
5758# debug_logger()
5859
60+ _original_getaddrinfo = socket .getaddrinfo
61+
62+
63+ def getaddrinfo_no_ipv4 (* args , ** kwargs ):
64+ """Calls socket.getaddrinfo and leaves out the IPv4 results."""
65+ entries = _original_getaddrinfo (* args , ** kwargs )
66+ return [addr for addr in entries if addr [0 ] != socket .AF_INET ]
67+
5968
6069class TestAddressInformation :
6170 """Tests for AssociationInformation."""
@@ -93,6 +102,24 @@ def test_ipv6_init_maximal(self):
93102 assert addr .scope_id == 11
94103 assert addr .as_tuple == ("::1" , 0 , 10 , 11 )
95104
105+ def test_no_ipv4 (self ):
106+ # Check to ensure we have IPv6 entries
107+ localhost_entries = getaddrinfo_no_ipv4 ("localhost" , 0 )
108+
109+ with mock .patch ("socket.getaddrinfo" , new = getaddrinfo_no_ipv4 ):
110+ if localhost_entries :
111+ addr = AddressInformation ("localhost" , 0 )
112+ assert addr .address == "::1"
113+ assert addr .port == 0
114+
115+ addr = AddressInformation ("" , 0 )
116+ assert addr .address in ("::0" , "::" )
117+ assert addr .port == 0
118+
119+ # IPv6 does not use broadcast.
120+ with pytest .raises (socket .gaierror , match = "Address resolution failed" ):
121+ AddressInformation ("<broadcast>" , 0 )
122+
96123 def test_from_tuple (self ):
97124 addr = AddressInformation .from_tuple (("localhost" , get_port ()))
98125 assert isinstance (addr , AddressInformation )
@@ -106,7 +133,7 @@ def test_from_tuple(self):
106133
107134 addr = AddressInformation .from_tuple (("::0" , get_port ("remote" )))
108135 assert isinstance (addr , AddressInformation )
109- assert addr .address == "::0"
136+ assert addr .address in ( "::0" , "::" )
110137 assert addr .port == get_port ("remote" )
111138
112139 def test_from_add_port (self ):
@@ -117,14 +144,14 @@ def test_from_add_port(self):
117144
118145 addr = AddressInformation .from_addr_port ("::0" , get_port ("remote" ))
119146 assert isinstance (addr , AddressInformation )
120- assert addr .address == "::0"
147+ assert addr .address in ( "::0" , "::" )
121148 assert addr .port == get_port ("remote" )
122149 assert addr .flowinfo == 0
123150 assert addr .scope_id == 0
124151
125152 addr = AddressInformation .from_addr_port (("::0" , 12 , 13 ), get_port ("remote" ))
126153 assert isinstance (addr , AddressInformation )
127- assert addr .address == "::0"
154+ assert addr .address in ( "::0" , "::" )
128155 assert addr .port == get_port ("remote" )
129156 assert addr .flowinfo == 12
130157 assert addr .scope_id == 13
@@ -143,12 +170,16 @@ def test_address(self):
143170 assert addr .address == "192.168.0.1"
144171 assert addr .address_family == socket .AF_INET
145172 addr .address = "::0"
146- assert addr .address == "::0"
173+ assert addr .address in ( "::0" , "::" )
147174 assert addr .address_family == socket .AF_INET6
148175 addr .address = "192.168.0.1"
149176 assert addr .address == "192.168.0.1"
150177 assert addr .address_family == socket .AF_INET
151178
179+ def test_resolve_hostname (self ):
180+ with pytest .raises (socket .gaierror ):
181+ AddressInformation ("remotehost" , 0 )
182+
152183
153184class TestTConnect :
154185 """Tests for T_CONNECT."""
@@ -164,9 +195,9 @@ def test_bad_addr_raises(self):
164195 def test_address_request (self ):
165196 """Test init with an A-ASSOCIATE primitive"""
166197 request = A_ASSOCIATE ()
167- request .called_presentation_address = AddressInformation ("123 .4" , 12 )
198+ request .called_presentation_address = AddressInformation ("1.2.3 .4" , 12 )
168199 conn = T_CONNECT (request )
169- assert conn .address == ("123 .4" , 12 )
200+ assert conn .address == ("1.2.3 .4" , 12 )
170201 assert conn .request is request
171202
172203 msg = r"A connection attempt has not yet been made"
@@ -176,7 +207,7 @@ def test_address_request(self):
176207 def test_result_setter (self ):
177208 """Test setting the result value."""
178209 request = A_ASSOCIATE ()
179- request .called_presentation_address = AddressInformation ("123 .4" , 12 )
210+ request .called_presentation_address = AddressInformation ("1.2.3 .4" , 12 )
180211 conn = T_CONNECT (request )
181212
182213 msg = r"Invalid connection result 'foo'"
@@ -191,7 +222,7 @@ def test_result_setter(self):
191222
192223 def test_address_inf (self ):
193224 request = A_ASSOCIATE ()
194- request .called_presentation_address = AddressInformation ("123 .4" , 12 )
225+ request .called_presentation_address = AddressInformation ("1.2.3 .4" , 12 )
195226 conn = T_CONNECT (request )
196227 assert conn .address_info is request .called_presentation_address
197228
0 commit comments