Ticket #3014: ipv6.2.patch
| File ipv6.2.patch, 56.1 KB (added by dripton, 5 years ago) |
|---|
-
twisted/internet/abstract.py
diff --git a/twisted/internet/abstract.py b/twisted/internet/abstract.py index 4419d61..eea676a 100644
a b 1 1 # -*- test-case-name: twisted.test.test_abstract -*- 2 # Copyright (c) 2001-200 7Twisted Matrix Laboratories.2 # Copyright (c) 2001-2008 Twisted Matrix Laboratories. 3 3 # See LICENSE for details. 4 4 5 5 … … 10 10 Maintainer: U{Itamar Shtull-Trauring<mailto:twisted@itamarst.org>} 11 11 """ 12 12 13 import socket 14 13 15 from zope.interface import implements 14 16 15 17 # Twisted Imports … … 366 368 return False 367 369 368 370 371 def isIPv6Address(ip): 372 """ 373 Return True iff ip is a valid bare IPv6 address. 374 375 Return False for 'enhanced' IPv6 addresses like '::1%lo' and '::1/128' 376 """ 377 try: 378 socket.inet_pton(socket.AF_INET6, ip) 379 except (ValueError, socket.error): 380 return False 381 return True 382 383 369 384 __all__ = ["FileDescriptor"] -
twisted/internet/address.py
diff --git a/twisted/internet/address.py b/twisted/internet/address.py index b349080..3f25770 100644
a b 21 21 # _bwHack is given to old users who think we are a tuple. They expected 22 22 # addr[0] to define the socket type rather than the address family, so 23 23 # the value comes from a different namespace than the new .type value: 24 24 25 25 # type = map[_bwHack] 26 26 # map = { 'SSL': 'TCP', 'INET': 'TCP', 'INET_UDP': 'UDP' } 27 27 28 28 implements(IAddress) 29 29 30 30 def __init__(self, type, host, port, _bwHack = None): 31 assert type in ('TCP', 'UDP') 31 if type not in ('TCP', 'UDP'): 32 raise ValueError, "illegal transport type" 32 33 self.type = type 33 34 self.host = host 34 35 self.port = port … … 56 57 def __str__(self): 57 58 return 'IPv4Address(%s, %r, %d)' % (self.type, self.host, self.port) 58 59 60 class IPv6Address(object): 61 """ 62 Object representing an IPv6 socket endpoint. 63 64 @ivar type: A string describing the type of transport, either 'TCP' or 'UDP'. 65 @ivar host: A string containing the coloned-oct IP address. 66 @ivar port: An integer representing the port number. 67 @ivar flowinfo: An integer representing the sockaddr-in6 flowinfo 68 @ivar scopeid: An integer representing the sockaddr-in6 scopeid 69 """ 70 71 implements(IAddress) 72 73 def __init__(self, type, host, port, flowinfo=0, scopeid=0): 74 if type not in ('TCP', 'UDP'): 75 raise ValueError, "illegal transport type" 76 self.type = type 77 self.host = host 78 self.port = port 79 self.flowinfo = flowinfo 80 self.scopeid = scopeid 81 82 def __eq__(self, other): 83 if isinstance(other, tuple): 84 return tuple(self) == other 85 elif isinstance(other, IPv6Address): 86 a = (self.type, self.host, self.port, self.flowinfo, self.scopeid) 87 b = (other.type, other.host, other.port, other.flowinfo, other.scopeid) 88 return a == b 89 return False 90 91 def __str__(self): 92 return 'IPv6Address(%s, %r, %d, %s, %s, %s)' % (self.type, self.host, 93 self.port, self.flowinfo, self.scopeid) 59 94 60 95 class UNIXAddress(object): 61 96 """ … … 66 101 """ 67 102 68 103 implements(IAddress) 69 104 70 105 def __init__(self, name, _bwHack='UNIX'): 71 106 self.name = name 72 107 self._bwHack = _bwHack 73 108 74 109 def __getitem__(self, index): 75 110 warnings.warn("UNIXAddress.__getitem__ is deprecated. Use attributes instead.", 76 111 category=DeprecationWarning, stacklevel=2) … … 100 135 101 136 class _ServerFactoryIPv4Address(IPv4Address): 102 137 """Backwards compatability hack. Just like IPv4Address in practice.""" 103 138 104 139 def __eq__(self, other): 105 140 if isinstance(other, tuple): 106 141 warnings.warn("IPv4Address.__getitem__ is deprecated. Use attributes instead.", -
twisted/internet/base.py
diff --git a/twisted/internet/base.py b/twisted/internet/base.py index 3337fca..8f7e16c 100644
a b 194 194 return "".join(L) 195 195 196 196 197 def getHostByName(name): 198 """ 199 Call socket.getaddrinfo, using the args and return value type of 200 socket.gethostbyname 201 """ 202 lst = socket.getaddrinfo(name, None) 203 res = lst[0] 204 (family, socktype, proto, canonname, sockaddr) = res 205 address = sockaddr[0] 206 return address 207 208 197 209 class ThreadedResolver: 198 210 implements(IResolverSimple) 199 211 … … 224 236 else: 225 237 userDeferred.callback(result) 226 238 239 240 227 241 def getHostByName(self, name, timeout = (1, 3, 11, 45)): 228 242 if timeout: 229 243 timeoutDelay = reduce(operator.add, timeout) 230 244 else: 231 245 timeoutDelay = 60 232 246 userDeferred = defer.Deferred() 233 lookupDeferred = threads.deferToThread( socket.gethostbyname, name)247 lookupDeferred = threads.deferToThread(getHostByName, name) 234 248 cancelCall = self.reactor.callLater( 235 249 timeoutDelay, self._cleanup, name, lookupDeferred) 236 250 self._runningQueries[lookupDeferred] = (userDeferred, cancelCall) … … 242 256 243 257 def getHostByName(self, name, timeout = (1, 3, 11, 45)): 244 258 try: 245 address = socket.gethostbyname(name)259 address = getHostByName(name) 246 260 except socket.error: 247 261 msg = "address %r not found" % (name,) 248 262 err = error.DNSLookupError(msg) -
twisted/internet/interfaces.py
diff --git a/twisted/internet/interfaces.py b/twisted/internet/interfaces.py index d9c2744..3d1b45a 100644
a b 209 209 docs for details. 210 210 """ 211 211 212 class IReactorTCP6(Interface): 213 214 def listenTCP6(port, factory, backlog=50, interface=''): 215 """ 216 Connects a given protocol factory to the given numeric TCP/IPv6 port. 217 218 @param port: a port number on which to listen 219 220 @param factory: a L{twisted.internet.protocol.ServerFactory} instance 221 222 @param backlog: size of the listen queue 223 224 @param interface: the hostname to bind to, defaults to '' (all) 225 226 @return: an object that provides L{IListeningPort}. 227 228 @raise CannotListenError: as defined here 229 L{twisted.internet.error.CannotListenError}, 230 if it cannot listen on this port (e.g., it 231 cannot bind to the required port number) 232 """ 233 234 def connectTCP6(host, port, factory, timeout=30, bindAddress=None): 235 """ 236 Connect a TCPv6 client. 237 238 @param host: a host name 239 240 @param port: a port number 241 242 @param factory: a L{twisted.internet.protocol.ClientFactory} instance 243 244 @param timeout: number of seconds to wait before assuming the 245 connection has failed. 246 247 @param bindAddress: a (host, port) tuple of local address to bind 248 to, or None. 249 250 @return: An object which provides L{IConnector}. This connector will 251 call various callbacks on the factory when a connection is 252 made, failed, or lost - see 253 L{ClientFactory<twisted.internet.protocol.ClientFactory>} 254 docs for details. 255 """ 256 212 257 class IReactorSSL(Interface): 213 258 214 259 def connectSSL(host, port, factory, contextFactory, timeout=30, bindAddress=None): -
twisted/internet/posixbase.py
diff --git a/twisted/internet/posixbase.py b/twisted/internet/posixbase.py index ab2d9b5..691f265 100644
a b 1 1 # -*- test-case-name: twisted.test.test_internet -*- 2 2 # 3 # Copyright (c) 2001-200 7Twisted Matrix Laboratories.3 # Copyright (c) 2001-2008 Twisted Matrix Laboratories. 4 4 # See LICENSE for details. 5 5 6 6 … … 22 22 23 23 from twisted.internet.interfaces import IReactorUNIX, IReactorUNIXDatagram 24 24 from twisted.internet.interfaces import IReactorTCP, IReactorUDP, IReactorSSL, IReactorArbitrary 25 from twisted.internet.interfaces import IReactorTCP6 25 26 from twisted.internet.interfaces import IReactorProcess, IReactorMulticast 26 27 from twisted.internet.interfaces import IHalfCloseableDescriptor 27 28 from twisted.internet import error … … 167 168 """ 168 169 A basis for reactors that use file descriptors. 169 170 """ 170 implements(IReactorArbitrary, IReactorTCP, IReactorUDP, IReactorMulticast) 171 implements(IReactorArbitrary, IReactorTCP, IReactorTCP6, IReactorUDP, 172 IReactorMulticast) 171 173 172 174 def __init__(self): 173 175 ReactorBase.__init__(self) … … 481 483 c.connect() 482 484 return c 483 485 486 # IReactorTCP6 487 488 def listenTCP6(self, port, factory, backlog=50, interface=''): 489 """ 490 @see: twisted.internet.interfaces.IReactorTCP.listenTCP6 491 """ 492 p = tcp.Port6(port, factory, backlog, interface, self) 493 p.startListening() 494 return p 495 496 def connectTCP6(self, host, port, factory, timeout=30, bindAddress=None, 497 flowinfo=0, scopeid=0): 498 """ 499 @see: twisted.internet.interfaces.IReactorTCP.connectTCP6 500 """ 501 c = tcp.Connector6(host, port, factory, timeout, bindAddress, self, 502 flowinfo, scopeid) 503 c.connect() 504 return c 505 484 506 # IReactorSSL (sometimes, not implemented) 485 507 486 508 def connectSSL(self, host, port, factory, contextFactory, timeout=30, bindAddress=None): -
twisted/internet/protocol.py
diff --git a/twisted/internet/protocol.py b/twisted/internet/protocol.py index 15e344c..5909d09 100644
a b 1 1 # -*- test-case-name: twisted.test.test_factories -*- 2 2 # 3 # Copyright (c) 2001-200 4Twisted Matrix Laboratories.3 # Copyright (c) 2001-2008 Twisted Matrix Laboratories. 4 4 # See LICENSE for details. 5 5 6 6 … … 137 137 """Factory used by ClientCreator.""" 138 138 139 139 noisy = False 140 140 141 141 def __init__(self, reactor, instance, deferred): 142 142 self.reactor = reactor 143 143 self.instance = instance … … 145 145 146 146 def __repr__(self): 147 147 return "<ClientCreator factory: %r>" % (self.instance, ) 148 148 149 149 def buildProtocol(self, addr): 150 150 self.reactor.callLater(0, self.deferred.callback, self.instance) 151 151 del self.deferred … … 162 162 The various connect* methods create a protocol instance using the given 163 163 protocol class and arguments, and connect it, returning a Deferred of the 164 164 resulting protocol instance. 165 165 166 166 Useful for cases when we don't really need a factory. Mainly this 167 167 is when there is no shared state between protocol instances, and no need 168 168 to reconnect. … … 181 181 self.reactor.connectTCP(host, port, f, timeout=timeout, bindAddress=bindAddress) 182 182 return d 183 183 184 def connectTCP6(self, host, port, timeout=30, bindAddress=None): 185 """ 186 Connect to remote host, return Deferred of resulting protocol instance. 187 @see twisted.internet.interfaces.IReactorTCP6.connectTCP6 188 """ 189 d = defer.Deferred() 190 f = _InstanceFactory(self.reactor, self.protocolClass(*self.args, 191 **self.kwargs), d) 192 self.reactor.connectTCP6(host, port, f, timeout=timeout, 193 bindAddress=bindAddress) 194 return d 195 184 196 def connectUNIX(self, address, timeout = 30, checkPID=0): 185 197 """Connect to Unix socket, return Deferred of resulting protocol instance.""" 186 198 d = defer.Deferred() 187 199 f = _InstanceFactory(self.reactor, self.protocolClass(*self.args, **self.kwargs), d) 188 200 self.reactor.connectUNIX(address, f, timeout = timeout, checkPID=checkPID) 189 201 return d 190 202 191 203 def connectSSL(self, host, port, contextFactory, timeout=30, bindAddress=None): 192 204 """Connect to SSL server, return Deferred of resulting protocol instance.""" 193 205 d = defer.Deferred() … … 630 642 def pauseProducing(self): 631 643 # Never sends data anyways 632 644 pass 633 645 634 646 def stopProducing(self): 635 647 self.loseConnection() 636 637 648 638 __all__ = ["Factory", "ClientFactory", "ReconnectingClientFactory", "connectionDone", 649 650 __all__ = ["Factory", "ClientFactory", "ReconnectingClientFactory", "connectionDone", 639 651 "Protocol", "ProcessProtocol", "FileWrapper", "ServerFactory", 640 652 "AbstractDatagramProtocol", "DatagramProtocol", "ConnectedDatagramProtocol", 641 653 "ClientCreator"] -
twisted/internet/tcp.py
diff --git a/twisted/internet/tcp.py b/twisted/internet/tcp.py index bbb7029..2a567eb 100644
a b 52 52 ENOMEM = object() 53 53 EAGAIN = EWOULDBLOCK 54 54 from errno import WSAECONNRESET as ECONNABORTED 55 56 from twisted.python.win32 import formatError as strerror 55 56 from twisted.python.win32 import formatError as strerror 57 57 else: 58 58 from errno import EPERM 59 59 from errno import EINVAL … … 70 70 from errno import ENOMEM 71 71 from errno import EAGAIN 72 72 from errno import ECONNABORTED 73 73 74 74 from os import strerror 75 75 76 76 from errno import errorcode … … 892 892 893 893 def getDestination(self): 894 894 return address.IPv4Address('TCP', self.host, self.port, 'INET') 895 896 897 class Client6(Client): 898 """ 899 A TCP6 client. 900 """ 901 addressFamily = socket.AF_INET6 902 903 def __init__(self, host, port, bindAddress, connector, reactor=None, 904 flowinfo=0, scopeid=0): 905 Client.__init__(self, host, port, bindAddress, connector, reactor) 906 self.addr = (host, port, flowinfo, scopeid) 907 908 def resolveAddress(self): 909 """ 910 Lookup the IPv6 address for self.addr[0] if necessary, then set 911 self.realAddress to that IPv6 address. 912 """ 913 if abstract.isIPv6Address(self.addr[0]): 914 self._setRealAddress(self.addr[0]) 915 else: 916 d = self.reactor.resolve(self.addr[0]) 917 d.addCallbacks(self._setRealAddress, self.failIfNotConnected) 918 919 def _setRealAddress(self, address): 920 """ 921 Set self.realAddress[0] to address. Set the remaining parts of 922 self.realAddress to the corresponding parts of self.addr. 923 """ 924 self.realAddress = (address, self.addr[1], self.addr[2], self.addr[3]) 925 self.doConnect() 926 927 def getHost(self): 928 """ 929 Returns an IPv6Address. 930 931 This indicates the address from which I am connecting. 932 """ 933 return address.IPv6Address('TCP', *(self.socket.getsockname())) 934 935 def getPeer(self): 936 """ 937 Returns an IPv6Address. 938 939 This indicates the address that I am connected to. 940 """ 941 return address.IPv6Address('TCP', *(self.addr)) 942 943 def __repr__(self): 944 s = '<%s to %s at %x>' % (self.__class__, self.addr, unsignedID(self)) 945 return s 946 947 948 class Server6(Server): 949 """ 950 IPv6 serverside socket-stream connection class. 951 952 This is a serverside network connection transport; a socket which came from 953 an accept() on a server. 954 """ 955 def getHost(self): 956 """ 957 Returns an IPv6Address. 958 959 This indicates the server's address. 960 """ 961 return address.IPv6Address('TCP', *(self.socket.getsockname())) 962 963 def getPeer(self): 964 """ 965 Returns an IPv6Address. 966 967 This indicates the client's address. 968 """ 969 return address.IPv6Address('TCP', *(self.client)) 970 971 972 class Connector6(base.BaseConnector): 973 """ 974 IPv6 implementation of connector 975 976 @ivar flowinfo An integer representing the sockaddr-in6 flowinfo 977 @ivar scopeid An integer representing the sockaddr-in6 scopeid 978 """ 979 980 def __init__(self, host, port, factory, timeout, bindAddress, 981 reactor=None, flowinfo=0, scopeid=0): 982 self.host = host 983 if isinstance(port, types.StringTypes): 984 try: 985 port = socket.getservbyname(port, 'tcp') 986 except socket.error, e: 987 raise error.ServiceNameUnknownError(string="%s (%r)" % (e, port)) 988 self.port = port 989 self.bindAddress = bindAddress 990 self.flowinfo = flowinfo 991 self.scopeid = scopeid 992 base.BaseConnector.__init__(self, factory, timeout, reactor) 993 994 def _makeTransport(self): 995 """ 996 Build and return a TCP6 client for the connector's transport. 997 """ 998 return Client6(self.host, self.port, self.bindAddress, self, 999 self.reactor, self.flowinfo, self.scopeid) 1000 1001 def getDestination(self): 1002 """ 1003 @see twisted.internet.interfaces.IConnector.getDestination 1004 """ 1005 return address.IPv6Address('TCP', self.host, self.port, self.flowinfo, 1006 self.scopeid) 1007 1008 1009 class Port6(Port): 1010 """ 1011 I am a TCP server port, listening for connections. 1012 1013 When a connection is accepted, I will call my factory's buildProtocol with 1014 the incoming connection as an argument, according to the specification 1015 described in twisted.internet.interfaces.IProtocolFactory. 1016 1017 If you wish to change the sort of transport that will be used, my 1018 `transport' attribute will be called with the signature expected for 1019 Server.__init__, so it can be replaced. 1020 """ 1021 addressFamily = socket.AF_INET6 1022 transport = Server6 1023 1024 def _buildAddr(self, (host, port, flowinfo, scopeid)): 1025 """ 1026 Build and return an IPv6Address from the passed sockaddr-in6 tuple. 1027 """ 1028 return address.IPv6Address('TCP', host, port, flowinfo, scopeid) 1029 1030 def getHost(self): 1031 """ 1032 Returns an IPv6Address. 1033 1034 This indicates the server's address. 1035 """ 1036 return address.IPv6Address('TCP', *(self.socket.getsockname())) -
twisted/test/test_abstract.py
diff --git a/twisted/test/test_abstract.py b/twisted/test/test_abstract.py index 44b4646..294a69e 100644
a b 1 # Copyright (c) 2007 Twisted Matrix Laboratories.1 # Copyright (c) 2007-2008 Twisted Matrix Laboratories. 2 2 # See LICENSE for details. 3 3 4 4 """ … … 8 8 from twisted.trial.unittest import TestCase 9 9 10 10 from twisted.internet.abstract import isIPAddress 11 from twisted.internet.abstract import isIPv6Address 11 12 12 13 13 14 class AddressTests(TestCase): … … 81 82 self.assertFalse(isIPAddress('0.0.256.0')) 82 83 self.assertFalse(isIPAddress('0.0.0.256')) 83 84 self.assertFalse(isIPAddress('256.256.256.256')) 85 86 87 class IPv6AddressTests(TestCase): 88 """ 89 Tests for IPv6 addresses. 90 """ 91 def test_hexadecimalColoned(self): 92 """ 93 L{isIPv6Address} should return C{True} for any valid colon-separated 94 hexadecimal representation of an IPv6 address. 95 """ 96 self.assertTrue(isIPv6Address('::1')) 97 self.assertTrue(isIPv6Address('fe80::215:cbfe:feac:8888')) 98 self.assertTrue(isIPv6Address('fe80::215:cbfe:feac:8888')) 99 self.assertTrue(isIPv6Address('fe80:5:5:5:215:cbfe:feac:8888')) 100 101 def test_illegalHexadecimalColoned(self): 102 """ 103 L{isIPv6Address} should return C{False} for any invalid colon-separated 104 hexadecimal representation of an IPv6 address. 105 """ 106 self.assertFalse(isIPv6Address(':1::2')) 107 self.assertFalse(isIPv6Address('fe80:5:5:5:215:cbfe:feac:8888:5')) 108 self.assertFalse(isIPv6Address('g::h')) -
twisted/test/test_tcp.py
diff --git a/twisted/test/test_tcp.py b/twisted/test/test_tcp.py index ac7b8b9..d3cf433 100644
a b 14 14 from twisted.python.log import msg 15 15 from twisted.internet import protocol, reactor, defer, interfaces 16 16 from twisted.internet import error 17 from twisted.internet.address import IPv4Address 17 from twisted.internet.address import IPv4Address, IPv6Address 18 18 from twisted.internet.interfaces import IHalfCloseableProtocol, IPullProducer 19 19 from twisted.protocols import policies 20 20 … … 179 179 180 180 181 181 class ListeningTestCase(PortCleanerUpper): 182 """ 183 Tests for listening on a port. 184 185 @ivar listenMethod Method used to listen 186 @ivar connectMethod Method used to connect 187 @ivar loopback Loopback interface to use for testing 188 """ 189 190 listenMethod = reactor.listenTCP 191 connectMethod = reactor.connectTCP 192 loopback = "127.0.0.1" 182 193 183 194 def testListen(self): 184 195 f = MyServerFactory() 185 p1 = reactor.listenTCP(0, f, interface="127.0.0.1")196 p1 = self.listenMethod(0, f, interface=self.loopback) 186 197 self.failUnless(interfaces.IListeningPort.providedBy(p1)) 187 198 return p1.stopListening() 188 199 189 200 def testStopListening(self): 190 201 f = MyServerFactory() 191 port = reactor.listenTCP(0, f, interface="127.0.0.1")202 port = self.listenMethod(0, f, interface=self.loopback) 192 203 n = port.getHost().port 193 204 self.ports.append(port) 194 205 195 206 def cbStopListening(ignored): 196 207 # Make sure we can rebind the port right away 197 port = reactor.listenTCP(n, f, interface="127.0.0.1")208 port = self.listenMethod(n, f, interface=self.loopback) 198 209 self.ports.append(port) 199 210 200 211 d = defer.maybeDeferred(port.stopListening) … … 204 215 def testNumberedInterface(self): 205 216 f = MyServerFactory() 206 217 # listen only on the loopback interface 207 p1 = reactor.listenTCP(0, f, interface='127.0.0.1')218 p1 = self.listenMethod(0, f, interface=self.loopback) 208 219 return p1.stopListening() 209 220 210 221 def testPortRepr(self): 211 222 f = MyServerFactory() 212 p = reactor.listenTCP(0, f)223 p = self.listenMethod(0, f) 213 224 portNo = str(p.getHost().port) 214 225 self.failIf(repr(p).find(portNo) == -1) 215 226 def stoppedListening(ign): … … 225 236 """ 226 237 server = MyServerFactory() 227 238 serverConnMade = server.protocolConnectionMade = defer.Deferred() 228 port = reactor.listenTCP(0, server)239 port = self.listenMethod(0, server) 229 240 self.addCleanup(port.stopListening) 230 241 231 242 client = MyClientFactory() 232 243 clientConnMade = client.protocolConnectionMade = defer.Deferred() 233 connector = reactor.connectTCP("127.0.0.1",234 port.getHost().port,client)244 connector = self.connectMethod(self.loopback, port.getHost().port, 245 client) 235 246 self.addCleanup(connector.disconnect) 236 247 def check((serverProto, clientProto)): 237 248 portNumber = port.getHost().port … … 242 253 return defer.gatherResults([serverConnMade, clientConnMade] 243 254 ).addCallback(check) 244 255 256 class ListeningIPv6TestCase(ListeningTestCase): 257 """ 258 Tests for listening on a port using IPv6. 259 260 @ivar listenMethod Method used to listen 261 @ivar connectMethod Method used to connect 262 @ivar loopback Loopback interface to use for testing 263 """ 264 listenMethod = reactor.listenTCP6 265 connectMethod = reactor.connectTCP6 266 loopback = "::1" 245 267 246 268 247 269 def callWithSpew(f): … … 254 276 sys.settrace(None) 255 277 256 278 class LoopbackTestCase(PortCleanerUpper): 257 """Test loopback connections.""" 279 """ 280 Test loopback connections. 258 281 259 n = 10081 282 @ivar listenMethod Method used to listen 283 @ivar connectMethod Method used to connect 284 @ivar loopback Loopback interface to use for testing 285 @ivar addressFamily Socket address family to use 286 """ 260 287 288 listenMethod = reactor.listenTCP 289 connectMethod = reactor.connectTCP 290 creatorConnectMethod = protocol.ClientCreator.connectTCP 291 loopback = "127.0.0.1" 292 addressFamily = socket.AF_INET 293 n = 10081 261 294 262 295 def testClosePortInProtocolFactory(self): 263 296 f = ClosingFactory() 264 port = reactor.listenTCP(0, f, interface="127.0.0.1")297 port = self.listenMethod(0, f, interface=self.loopback) 265 298 self.n = port.getHost().port 266 299 self.ports.append(port) 267 300 f.port = port 268 301 clientF = MyClientFactory() 269 reactor.connectTCP("127.0.0.1", self.n, clientF)302 self.connectMethod(self.loopback, self.n, clientF) 270 303 def check(x): 271 304 self.assert_(clientF.protocol.made) 272 305 self.assert_(port.disconnected) … … 278 311 279 312 def testTcpNoDelay(self): 280 313 f = MyServerFactory() 281 port = reactor.listenTCP(0, f, interface="127.0.0.1")314 port = self.listenMethod(0, f, interface=self.loopback) 282 315 283 316 self.n = port.getHost().port 284 317 self.ports.append(port) 285 318 clientF = MyClientFactory() 286 reactor.connectTCP("127.0.0.1", self.n, clientF)319 self.connectMethod(self.loopback, self.n, clientF) 287 320 288 321 d = loopUntil(lambda: (f.called > 0 and 289 322 getattr(clientF, 'protocol', None) is not None)) … … 301 334 302 335 def testTcpKeepAlive(self): 303 336 f = MyServerFactory() 304 port = reactor.listenTCP(0, f, interface="127.0.0.1")337 port = self.listenMethod(0, f, interface=self.loopback) 305 338 self.n = port.getHost().port 306 339 self.ports.append(port) 307 340 clientF = MyClientFactory() 308 reactor.connectTCP("127.0.0.1", self.n, clientF)341 self.connectMethod(self.loopback, self.n, clientF) 309 342 310 343 d = loopUntil(lambda :(f.called > 0 and 311 344 getattr(clientF, 'protocol', None) is not None)) … … 324 357 def testFailing(self): 325 358 clientF = MyClientFactory() 326 359 # XXX we assume no one is listening on TCP port 69 327 reactor.connectTCP("127.0.0.1", 69, clientF, timeout=5)360 self.connectMethod(self.loopback, 69, clientF, timeout=5) 328 361 def check(ignored): 329 362 clientF.reason.trap(error.ConnectionRefusedError) 330 363 return clientF.failDeferred.addCallback(check) … … 357 390 358 391 serverSockets = [] 359 392 for i in xrange(10): 360 serverSocket = socket.socket( )361 serverSocket.bind(( '127.0.0.1', 0))393 serverSocket = socket.socket(self.addressFamily) 394 serverSocket.bind((self.loopback, 0)) 362 395 serverSocket.listen(1) 363 396 serverSockets.append(serverSocket) 364 397 random.shuffle(serverSockets) … … 376 409 self.fail("Could not fail to connect - could not test errno for that case.") 377 410 378 411 serverSocket = serverSockets.pop() 379 serverHost, serverPort = serverSocket.getsockname() 412 serverHost, serverPort = serverSocket.getsockname()[:2] 380 413 serverSocket.close() 381 414 382 connectDeferred = clientCreator.connectTCP(serverHost, serverPort) 415 connectDeferred = self.creatorConnectMethod(clientCreator, 416 serverHost, serverPort) 383 417 connectDeferred.addCallback(connected) 384 418 return connectDeferred 385 419 … … 398 432 399 433 def testConnectByServiceFail(self): 400 434 try: 401 reactor.connectTCP("127.0.0.1", "thisbetternotexist",435 self.connectMethod(self.loopback, "thisbetternotexist", 402 436 MyClientFactory()) 403 437 except error.ServiceNameUnknownError: 404 438 return … … 409 443 d = defer.succeed(None) 410 444 try: 411 445 s = MyServerFactory() 412 port = reactor.listenTCP(0, s, interface="127.0.0.1")446 port = self.listenMethod(0, s, interface=self.loopback) 413 447 self.n = port.getHost().port 414 448 socket.getservbyname = (lambda s, p,n=self.n: 415 449 s == 'http' and p == 'tcp' and n or 10) 416 450 self.ports.append(port) 417 451 cf = MyClientFactory() 418 452 try: 419 c = reactor.connectTCP('127.0.0.1', 'http', cf)453 c = self.connectMethod(self.loopback, 'http', cf) 420 454 except: 421 455 socket.getservbyname = serv 422 456 raise … … 432 466 '%s was not called' % (s,))) 433 467 return d 434 468 469 class LoopbackIPv6TestCase(LoopbackTestCase): 470 """ 471 Test IPv6 loopback connections. 472 """ 473 listenMethod = reactor.listenTCP6 474 connectMethod = reactor.connectTCP6 475 creatorConnectMethod = protocol.ClientCreator.connectTCP6 476 loopback = "::1" 477 addressFamily = socket.AF_INET6 478 435 479 436 480 class StartStopFactory(protocol.Factory): 437 481 … … 466 510 467 511 468 512 class FactoryTestCase(PortCleanerUpper): 469 """Tests for factories.""" 513 """ 514 Tests for factories. 515 516 @ivar listenMethod Method used to listen 517 @ivar loopback Loopback interface to use for testing 518 """ 519 listenMethod = reactor.listenTCP 520 loopback = "127.0.0.1" 470 521 471 522 def testServerStartStop(self): 472 523 f = StartStopFactory() 473 524 474 525 # listen on port 475 p1 = reactor.listenTCP(0, f, interface='127.0.0.1')526 p1 = self.listenMethod(0, f, interface=self.loopback) 476 527 self.n1 = p1.getHost().port 477 528 self.ports.append(p1) 478 529 … … 482 533 def _testServerStartStop(self, ignored, f, p1): 483 534 self.assertEquals((f.started, f.stopped), (1,0)) 484 535 # listen on two more ports 485 p2 = reactor.listenTCP(0, f, interface='127.0.0.1')536 p2 = self.listenMethod(0, f, interface=self.loopback) 486 537 self.n2 = p2.getHost().port 487 538 self.ports.append(p2) 488 p3 = reactor.listenTCP(0, f, interface='127.0.0.1')539 p3 = self.listenMethod(0, f, interface=self.loopback) 489 540 self.n3 = p3.getHost().port 490 541 self.ports.append(p3) 491 542 d = loopUntil(lambda :(p2.connected == 1 and p3.connected == 1)) … … 513 564 514 565 def testClientStartStop(self): 515 566 f = ClosingFactory() 516 p = reactor.listenTCP(0, f, interface="127.0.0.1")567 p = self.listenMethod(0, f, interface=self.loopback) 517 568 self.n = p.getHost().port 518 569 self.ports.append(p) 519 570 f.port = p … … 521 572 d = loopUntil(lambda :p.connected) 522 573 def check(ignored): 523 574 factory = ClientStartStopFactory() 524 reactor.connectTCP("127.0.0.1", self.n, factory)575 self.connectMethod(self.loopback, self.n, factory) 525 576 self.assert_(factory.started) 526 577 return loopUntil(lambda :factory.stopped) 527 578 d.addCallback(check) 528 579 d.addBoth(lambda _: self.cleanPorts(*self.ports)) 529 580 return d 530 581 582 class FactoryIPv6TestCase(FactoryTestCase): 583 """ 584 IPv6 Tests for factories. 585 586 @ivar listenMethod Method used to listen 587 @ivar loopback Loopback interface to use for testing 588 """ 589 listenMethod = reactor.listenTCP6 590 loopback = "::1" 591 531 592 532 593 class ConnectorTestCase(PortCleanerUpper): 594 """ 595 Tests for connectors. 596 597 @ivar listenMethod Method used to listen 598 @ivar connectMethod Method used to connect 599 @ivar loopback Loopback interface to use for testing 600 """ 601 listenMethod = reactor.listenTCP 602 connectMethod = reactor.connectTCP 603 loopback = "127.0.0.1" 533 604 534 605 def testConnectorIdentity(self): 535 606 f = ClosingFactory() 536 p = reactor.listenTCP(0, f, interface="127.0.0.1")607 p = self.listenMethod(0, f, interface=self.loopback) 537 608 n = p.getHost().port 538 609 self.ports.append(p) 539 610 f.port = p … … 547 618 factory.clientConnectionLost = lambda c, r: (l.append(c), 548 619 m.append(r)) 549 620 factory.startedConnecting = lambda c: l.append(c) 550 connector = reactor.connectTCP("127.0.0.1", n, factory)621 connector = self.connectMethod(self.loopback, n, factory) 551 622 self.failUnless(interfaces.IConnector.providedBy(connector)) 552 623 dest = connector.getDestination() 553 624 self.assertEquals(dest.type, "TCP") 554 self.assertEquals(dest.host, "127.0.0.1")625 self.assertEquals(dest.host, self.loopback) 555 626 self.assertEquals(dest.port, n) 556 627 557 628 d = loopUntil(lambda :factory.stopped) … … 564 635 565 636 def testUserFail(self): 566 637 f = MyServerFactory() 567 p = reactor.listenTCP(0, f, interface="127.0.0.1")638 p = self.listenMethod(0, f, interface=self.loopback) 568 639 n = p.getHost().port 569 640 self.ports.append(p) 570 641 … … 573 644 574 645 factory = ClientStartStopFactory() 575 646 factory.startedConnecting = startedConnecting 576 reactor.connectTCP("127.0.0.1", n, factory)647 self.connectMethod(self.loopback, n, factory) 577 648 578 649 d = loopUntil(lambda :factory.stopped) 579 650 def check(ignored): … … 585 656 586 657 def testReconnect(self): 587 658 f = ClosingFactory() 588 p = reactor.listenTCP(0, f, interface="127.0.0.1")659 p = self.listenMethod(0, f, interface=self.loopback) 589 660 n = p.getHost().port 590 661 self.ports.append(p) 591 662 f.port = p … … 596 667 def clientConnectionLost(c, reason): 597 668 c.connect() 598 669 factory.clientConnectionLost = clientConnectionLost 599 reactor.connectTCP("127.0.0.1", n, factory)670 self.connectMethod(self.loopback, n, factory) 600 671 return loopUntil(lambda :factory.failed) 601 672 602 673 def step2(ignored): … … 609 680 return d.addCallback(step1).addCallback(step2) 610 681 611 682 683 class ConnectorIPv6TestCase(ConnectorTestCase): 684 """ 685 Tests for IPv6 connectors. 686 """ 687 listenMethod = reactor.listenTCP6 688 connectMethod = reactor.connectTCP6 689 loopback = "::1" 690 691 612 692 class CannotBindTestCase(PortCleanerUpper): 613 """Tests for correct behavior when a reactor cannot bind to the required 614 TCP port.""" 693 """ 694 Tests for correct behavior when a reactor cannot bind to the required 695 TCP port. 696 697 @ivar listenMethod Method used to listen 698 @ivar connectMethod Method used to connect 699 @ivar loopback Loopback interface to use for testing 700 """ 701 702 listenMethod = reactor.listenTCP 703 connectMethod = reactor.connectTCP 704 loopback = "127.0.0.1" 615 705 616 706 def testCannotBind(self): 617 707 f = MyServerFactory() 618 708 619 p1 = reactor.listenTCP(0, f, interface='127.0.0.1')709 p1 = self.listenMethod(0, f, interface=self.loopback) 620 710 n = p1.getHost().port 621 711 self.ports.append(p1) 622 712 dest = p1.getHost() 623 713 self.assertEquals(dest.type, "TCP") 624 self.assertEquals(dest.host, "127.0.0.1")714 self.assertEquals(dest.host, self.loopback) 625 715 self.assertEquals(dest.port, n) 626 716 627 717 # make sure new listen raises error 628 718 self.assertRaises(error.CannotListenError, 629 reactor.listenTCP, n, f, interface= '127.0.0.1')719 reactor.listenTCP, n, f, interface=self.loopback) 630 720 631 721 return self.cleanPorts(*self.ports) 632 722 … … 644 734 theDeferred = defer.Deferred() 645 735 sf = MyServerFactory() 646 736 sf.startFactory = self._fireWhenDoneFunc(theDeferred, sf.startFactory) 647 p = reactor.listenTCP(0, sf, interface="127.0.0.1")737 p = self.listenMethod(0, sf, interface=self.loopback) 648 738 self.ports.append(p) 649 739 650 740 def _connect1(results): 651 741 d = defer.Deferred() 652 742 cf1 = MyClientFactory() 653 743 cf1.buildProtocol = self._fireWhenDoneFunc(d, cf1.buildProtocol) 654 reactor.connectTCP("127.0.0.1", p.getHost().port, cf1,655 bindAddress=( "127.0.0.1", 0))744 self.connectMethod(self.loopback, p.getHost().port, cf1, 745 bindAddress=(self.loopback, 0)) 656 746 d.addCallback(_conmade, cf1) 657 747 return d 658 748 … … 673 763 cf2.clientConnectionFailed = self._fireWhenDoneFunc( 674 764 d1, cf2.clientConnectionFailed) 675 765 cf2.stopFactory = self._fireWhenDoneFunc(d2, cf2.stopFactory) 676 reactor.connectTCP("127.0.0.1", p.getHost().port, cf2,677 bindAddress=( "127.0.0.1", port))766 self.connectMethod(self.loopback, p.getHost().port, cf2, 767 bindAddress=(self.loopback, port)) 678 768 d1.addCallback(_check2failed, cf1, cf2) 679 769 d2.addCallback(_check2stopped, cf1, cf2) 680 770 dl = defer.DeferredList([d1, d2]) … … 705 795 theDeferred.addCallback(_connect1) 706 796 return theDeferred 707 797 798 799 class CannotBindIPv6TestCase(CannotBindTestCase): 800 """ 801 Tests for correct behavior when a reactor cannot bind to the required 802 TCPv6 port. 803 """ 804 listenMethod = reactor.listenTCP6 805 connectMethod = reactor.connectTCP6 806 loopback = "::1" 807 808 708 809 class MyOtherClientFactory(protocol.ClientFactory): 709 810 def buildProtocol(self, address): 710 811 self.address = address … … 714 815 715 816 716 817 class LocalRemoteAddressTestCase(PortCleanerUpper): 717 """Tests for correct getHost/getPeer values and that the correct address 818 """ 819 Tests for correct getHost/getPeer values and that the correct address 718 820 is passed to buildProtocol. 821 822 @ivar listenMethod Method used to listen 823 @ivar connectMethod Method used to connect 824 @ivar loopback Loopback interface to use for testing 719 825 """ 826 827 listenMethod = reactor.listenTCP 828 connectMethod = reactor.connectTCP 829 loopback = "127.0.0.1" 830 720 831 def testHostAddress(self): 721 832 f1 = MyServerFactory() 722 p1 = reactor.listenTCP(0, f1, interface='127.0.0.1')833 p1 = self.listenMethod(0, f1, interface=self.loopback) 723 834 n = p1.getHost().port 724 835 self.ports.append(p1) 725 836 726 837 f2 = MyOtherClientFactory() 727 p2 = reactor.connectTCP('127.0.0.1', n, f2)838 p2 = self.connectMethod(self.loopback, n, f2) 728 839 729 840 d = loopUntil(lambda :p2.state == "connected") 730 841 def check(ignored): … … 737 848 return d.addCallback(check).addCallback(cleanup) 738 849 739 850 851 class LocalRemoteAddressIPv6TestCase(LocalRemoteAddressTestCase): 852 """ 853 IPv6 tests for correct getHost/getPeer values and that the correct address 854 is passed to buildProtocol. 855 """ 856 listenMethod = reactor.listenTCP6 857 connectMethod = reactor.connectTCP6 858 loopback = "::1" 859 860 740 861 class WriterProtocol(protocol.Protocol): 741 862 def connectionMade(self): 742 863 # use everything ITransport claims to provide. If something here … … 778 899 class WriteDataTestCase(PortCleanerUpper): 779 900 """Test that connected TCP sockets can actually write data. Try to 780 901 exercise the entire ITransport interface. 902 903 @ivar listenMethod Method used to listen 904 @ivar connectMethod Method used to connect 905 @ivar loopback Loopback interface to use for testing 781 906 """ 907 listenMethod = reactor.listenTCP 908 connectMethod = reactor.connectTCP 909 loopback = "127.0.0.1" 782 910 783 911 def testWriter(self): 784 912 f = protocol.Factory() … … 786 914 f.done = 0 787 915 f.problem = 0 788 916 wrappedF = WiredFactory(f) 789 p = reactor.listenTCP(0, wrappedF, interface="127.0.0.1")917 p = self.listenMethod(0, wrappedF, interface=self.loopback) 790 918 n = p.getHost().port 791 919 self.ports.append(p) 792 920 clientF = WriterClientFactory() 793 921 wrappedClientF = WiredFactory(clientF) 794 reactor.connectTCP("127.0.0.1", n, wrappedClientF)922 self.connectMethod(self.loopback, n, wrappedClientF) 795 923 796 924 def check(ignored): 797 925 self.failUnless(f.done, "writer didn't finish, it probably died") … … 820 948 # Gtk reactor cannot pass this test, though, because it fails to 821 949 # implement IReactorTCP entirely correctly. Gtk is quite old at 822 950 # this point, so it's more likely that gtkreactor will be deprecated 823 # and removed rather than fixed to handle this case correctly. 951 # and removed rather than fixed to handle this case correctly. 824 952 # Since this is a pre-existing (and very long-standing) issue with 825 953 # the Gtk reactor, there's no reason for it to prevent this test 826 954 # being added to exercise the other reactors, for which the behavior … … 872 1000 # Create the server port to which a connection will be made. 873 1001 server = protocol.ServerFactory() 874 1002 server.protocol = Disconnecter 875 port = reactor.listenTCP(0, server, interface='127.0.0.1')1003 port = self.listenMethod(0, server, interface=self.loopback) 876 1004 self.addCleanup(port.stopListening) 877 1005 addr = port.getHost() 878 1006 … … 942 1070 return client.lostReason 943 1071 clientConnectionLost.addCallback(cbClientLost) 944 1072 msg('Connecting to %s:%s' % (addr.host, addr.port)) 945 connector = reactor.connectTCP(addr.host, addr.port, client)1073 connector = self.connectMethod(addr.host, addr.port, client) 946 1074 947 1075 # By the end of the test, the client should have received notification 948 1076 # of unclean disconnection. 949 1077 msg('Returning Deferred') 950 1078 return self.assertFailure(clientConnectionLost, error.ConnectionLost) 951 1079 1080 class WriteDataIPv6TestCase(WriteDataTestCase): 1081 """ 1082 Test that connected TCPv6 sockets can actually write data. Try to 1083 exercise the entire ITransport interface. 1084 """ 1085 listenMethod = reactor.listenTCP6 1086 connectMethod = reactor.connectTCP6 1087 loopback = "::1" 1088 952 1089 953 1090 954 1091 class ConnectionLosingProtocol(protocol.Protocol): … … 1053 1190 serverFactory = protocol.ServerFactory() 1054 1191 serverFactory.protocol = lambda: ConnectionLostNotifyingProtocol( 1055 1192 onServerConnectionLost) 1056 serverPort = self.createServer( '127.0.0.1', 0, serverFactory)1193 serverPort = self.createServer(self.loopback, 0, serverFactory) 1057 1194 1058 1195 onClientConnectionLost = defer.Deferred() 1059 1196 serverAddr = serverPort.getHost() … … 1098 1235 1099 1236 1100 1237 class ProperlyCloseFilesTestCase(unittest.TestCase, ProperlyCloseFilesMixin): 1101 def createServer(self, address, portNumber, factory):1102 return reactor.listenTCP(portNumber, factory, interface=address)1238 """ 1239 Test that we properly close files. 1103 1240 1241 @ivar listenMethod Method used to listen 1242 @ivar connectMethod Method used to connect 1243 @ivar loopback Loopback interface to use for testing 1244 """ 1104 1245 1105 def connectClient(self, address, portNumber, clientCreator): 1106 return clientCreator.connectTCP(address, portNumber) 1246 listenMethod = reactor.listenTCP 1247 creatorConnectMethod = protocol.ClientCreator.connectTCP 1248 loopback = "127.0.0.1" 1107 1249 1250 def createServer(self, address, portNumber, factory): 1251 return self.listenMethod(portNumber, factory, interface=address) 1252 1253 def connectClient(self, address, portNumber, clientCreator): 1254 return self.creatorConnectMethod(clientCreator, address, portNumber) 1108 1255 1109 1256 def getHandleExceptionType(self): 1110 1257 return socket.error 1111 1258 1259 class ProperlyCloseFilesIPv6TestCase(ProperlyCloseFilesTestCase): 1260 """ 1261 Test that we properly close files using IPv6 1262 """ 1263 listenMethod = reactor.listenTCP6 1264 creatorConnectMethod = protocol.ClientCreator.connectTCP6 1265 loopback = "::1" 1112 1266 1113 1267 1114 1268 class WiredForDeferreds(policies.ProtocolWrapper): … … 1138 1292 class AddressTestCase(unittest.TestCase): 1139 1293 """ 1140 1294 Tests for address-related interactions with client and server protocols. 1295 1296 @ivar listenMethod Method used to listen 1297 @ivar connectMethod Method used to connect 1298 @ivar loopback Loopback interface to use for testing 1299 @ivar addrType Type of address we expect to see 1141 1300 """ 1301 1302 listenMethod = reactor.listenTCP 1303 connectMethod = reactor.connectTCP 1304 loopback = "127.0.0.1" 1305 addrtype = IPv4Address 1306 1142 1307 def setUp(self): 1143 1308 """ 1144 1309 Create a port and connected client/server pair which can be used … … 1189 1354 self.clientConnLost = self.client.protocolConnectionLost = defer.Deferred() 1190 1355 self.clientWrapper = RememberingWrapper(self.client) 1191 1356 1192 self.port = reactor.listenTCP(0, self.serverWrapper, interface='127.0.0.1')1193 self.connector = reactor.connectTCP(1357 self.port = self.listenMethod(0, self.serverWrapper, interface=self.loopback) 1358 self.connector = self.connectMethod( 1194 1359 self.port.getHost().host, self.port.getHost().port, self.clientWrapper) 1195 1360 1196 1361 return defer.gatherResults([self.serverConnMade, self.clientConnMade]) … … 1220 1385 1221 1386 self.assertEqual( 1222 1387 self.clientWrapper.addresses, 1223 [ IPv4Address('TCP', serverHost.host, serverHost.port)])1388 [self.addrtype('TCP', serverHost.host, serverHost.port)]) 1224 1389 self.assertEqual( 1225 1390 self.clientWrapper.addresses, 1226 [ IPv4Address('TCP', clientPeer.host, clientPeer.port)])1391 [self.addrtype('TCP', clientPeer.host, clientPeer.port)]) 1227 1392 1228 1393 1229 1394 def test_buildProtocolServer(self): … … 1239 1404 1240 1405 self.assertEqual( 1241 1406 self.serverWrapper.addresses, 1242 [ IPv4Address('TCP', serverPeer.host, serverPeer.port)])1407 [self.addrtype('TCP', serverPeer.host, serverPeer.port)]) 1243 1408 self.assertEqual( 1244 1409 self.serverWrapper.addresses, 1245 [ IPv4Address('TCP', clientHost.host, clientHost.port)])1410 [self.addrtype('TCP', clientHost.host, clientHost.port)]) 1246 1411 1247 1412 1413 class AddressIPv6TestCase(AddressTestCase): 1414 """ 1415 Tests for address-related interactions with client and server IPv6 1416 protocols. 1417 """ 1418 listenMethod = reactor.listenTCP6 1419 connectMethod = reactor.connectTCP6 1420 loopback = "::1" 1421 addrtype = IPv6Address 1422 1248 1423 1249 1424 class LargeBufferWriterProtocol(protocol.Protocol): 1250 1425 … … 1292 1467 1293 1468 1294 1469 class LargeBufferTestCase(PortCleanerUpper): 1295 """Test that buffering large amounts of data works.1296 1470 """ 1471 Test that buffering large amounts of data works. 1472 1473 @ivar listenMethod Method used to listen 1474 @ivar connectMethod Method used to connect 1475 @ivar loopback Loopback interface to use for testing 1476 """ 1477 listenMethod = reactor.listenTCP 1478 connectMethod = reactor.connectTCP 1479 loopback = "127.0.0.1" 1297 1480 1298 1481 datalen = 60*1024*1024 1299 1482 def testWriter(self): … … 1303 1486 f.problem = 0 1304 1487 f.len = self.datalen 1305 1488 wrappedF = FireOnCloseFactory(f) 1306 p = reactor.listenTCP(0, wrappedF, interface="127.0.0.1")1489 p = self.listenMethod(0, wrappedF, interface=self.loopback) 1307 1490 n = p.getHost().port 1308 1491 self.ports.append(p) 1309 1492 clientF = LargeBufferReaderClientFactory() 1310 1493 wrappedClientF = FireOnCloseFactory(clientF) 1311 reactor.connectTCP("127.0.0.1", n, wrappedClientF)1494 self.connectMethod(self.loopback, n, wrappedClientF) 1312 1495 1313 1496 d = defer.gatherResults([wrappedF.deferred, wrappedClientF.deferred]) 1314 1497 def check(ignored): … … 1320 1503 "client didn't see connection dropped") 1321 1504 return d.addCallback(check) 1322 1505 1506 class LargeBufferIPv6TestCase(LargeBufferTestCase): 1507 """ 1508 Test that buffering large amounts of data works over IPv6 1509 """ 1510 listenMethod = reactor.listenTCP6 1511 connectMethod = reactor.connectTCP6 1512 loopback = "::1" 1513 1323 1514 1324 1515 class MyHCProtocol(MyProtocol): 1325 1516 … … 1355 1546 1356 1547 1357 1548 class HalfCloseTestCase(PortCleanerUpper): 1358 """Test half-closing connections.""" 1549 """ 1550 Test half-closing connections. 1551 1552 @ivar listenMethod Method used to listen 1553 @ivar connectMethod Method used to connect 1554 @ivar creatorConnectMethod Method used to connect with a ClientCreator 1555 @ivar loopback Loopback interface to use for testing 1556 """ 1557 listenMethod = reactor.listenTCP 1558 connectMethod = reactor.connectTCP 1559 creatorConnectMethod = protocol.ClientCreator.connectTCP 1560 loopback = "127.0.0.1" 1359 1561 1360 1562 def setUp(self): 1361 1563 PortCleanerUpper.setUp(self) 1362 1564 self.f = f = MyHCFactory() 1363 self.p = p = reactor.listenTCP(0, f, interface="127.0.0.1")1565 self.p = p = self.listenMethod(0, f, interface=self.loopback) 1364 1566 self.ports.append(p) 1365 1567 d = loopUntil(lambda :p.connected) 1366 1568 1367 1569 self.cf = protocol.ClientCreator(reactor, MyHCProtocol) 1368 1570 1369 d.addCallback(lambda _: self.cf.connectTCP(p.getHost().host, 1370 p.getHost().port)) 1571 d.addCallback(lambda _: self.creatorConnectMethod(self.cf, 1572 p.getHost().host, 1573 p.getHost().port)) 1371 1574 d.addCallback(self._setUp) 1372 1575 return d 1373 1576 … … 1436 1639 f.protocol.readHalfClosed, False)) 1437 1640 return d 1438 1641 1642 class HalfCloseIPv6TestCase(HalfCloseTestCase): 1643 """ 1644 Test half-closing IPv6 connections. 1645 """ 1646 listenMethod = reactor.listenTCP6 1647 connectMethod = reactor.connectTCP6 1648 creatorConnectMethod = protocol.ClientCreator.connectTCP6 1649 loopback = "::1" 1650 1439 1651 1440 1652 class HalfClose2TestCase(unittest.TestCase): 1653 """ 1654 Test half-closing connections. 1655 1656 @ivar listenMethod Method used to listen 1657 @ivar creatorConnectMethod Method used to connect with a ClientCreator 1658 @ivar loopback Loopback interface to use for testing 1659 """ 1660 listenMethod = reactor.listenTCP 1661 creatorConnectMethod = protocol.ClientCreator.connectTCP 1662 loopback = "127.0.0.1" 1441 1663 1442 1664 def setUp(self): 1443 1665 self.f = f = MyServerFactory() 1444 1666 self.f.protocolConnectionMade = defer.Deferred() 1445 self.p = p = reactor.listenTCP(0, f, interface="127.0.0.1")1667 self.p = p = self.listenMethod(0, f, interface=self.loopback) 1446 1668 1447 1669 # XXX we don't test server side yet since we don't do it yet 1448 d = protocol.ClientCreator(reactor, MyProtocol).connectTCP( 1449 p.getHost().host, p.getHost().port) 1670 creator = protocol.ClientCreator(reactor, MyProtocol) 1671 d = self.creatorConnectMethod(creator, p.getHost().host, 1672 p.getHost().port) 1450 1673 d.addCallback(self._gotClient) 1451 1674 return d 1452 1675 … … 1491 1714 self.failUnlessEqual(self.f.protocol.closed, True)) 1492 1715 return defer.gatherResults([d, d2]) 1493 1716 1717 class HalfClose2IPv6TestCase(HalfClose2TestCase): 1718 """ 1719 Test half-closing IPv6 connections. 1720 """ 1721 listenMethod = reactor.listenTCP6 1722 creatorConnectMethod = protocol.ClientCreator.connectTCP6 1723 loopback = "::1" 1494 1724 1495 1725 class HalfCloseBuggyApplicationTests(unittest.TestCase): 1496 1726 """ 1497 1727 Test half-closing connections where notification code has bugs. 1728 1729 @ivar listenMethod Method used to listen 1730 @ivar creatorConnectMethod Method used to connect with a ClientCreator 1731 @ivar loopback Loopback interface to use for testing 1498 1732 """ 1733 listenMethod = reactor.listenTCP 1734 creatorConnectMethod = protocol.ClientCreator.connectTCP 1735 loopback = "127.0.0.1" 1499 1736 1500 1737 def setUp(self): 1501 1738 """ … … 1504 1741 """ 1505 1742 self.serverFactory = MyHCFactory() 1506 1743 self.serverFactory.protocolConnectionMade = defer.Deferred() 1507 self.port = reactor.listenTCP(1508 0, self.serverFactory, interface= "127.0.0.1")1744 self.port = self.listenMethod( 1745 0, self.serverFactory, interface=self.loopback) 1509 1746 self.addCleanup(self.port.stopListening) 1510 1747 addr = self.port.getHost() 1511 1748 creator = protocol.ClientCreator(reactor, MyHCProtocol) 1512 clientDeferred = creator.connectTCP(addr.host, addr.port)1749 clientDeferred = self.creatorConnectMethod(creator, addr.host, addr.port) 1513 1750 def setClient(clientProtocol): 1514 1751 self.clientProtocol = clientProtocol 1515 1752 clientDeferred.addCallback(setClient) … … 1559 1796 self.clientProtocol.writeConnectionLost = self.aBug 1560 1797 return self._notificationRaisesTest() 1561 1798 1799 class HalfCloseBuggyApplicationIPv6Tests(HalfCloseBuggyApplicationTests): 1800 """ 1801 Test half-closing IPv6 connections where notification code has bugs. 1802 """ 1803 listenMethod = reactor.listenTCP6 1804 creatorConnectMethod = protocol.ClientCreator.connectTCP6 1805 loopback = "::1" 1562 1806 1563 1807 1564 1808 class LogTestCase(unittest.TestCase): 1565 1809 """ 1566 1810 Test logging facility of TCP base classes. 1811 1812 @ivar listenMethod Method used to listen 1813 @ivar connectMethod Method used to connect 1814 @ivar loopback Loopback interface to use for testing 1567 1815 """ 1816 listenMethod = reactor.listenTCP 1817 connectMethod = reactor.connectTCP 1818 loopback = "127.0.0.1" 1568 1819 1569 1820 def test_logstrClientSetup(self): 1570 1821 """ … … 1576 1827 client = MyClientFactory() 1577 1828 client.protocolConnectionMade = defer.Deferred() 1578 1829 1579 port = reactor.listenTCP(0, server, interface='127.0.0.1')1830 port = self.listenMethod(0, server, interface=self.loopback) 1580 1831 self.addCleanup(port.stopListening) 1581 1832 1582 connector = reactor.connectTCP(1833 connector = self.connectMethod( 1583 1834 port.getHost().host, port.getHost().port, client) 1584 1835 self.addCleanup(connector.disconnect) 1585 1836 … … 1593 1844 client.protocolConnectionMade.addCallback(cb) 1594 1845 return client.protocolConnectionMade 1595 1846 1847 class LogIPv6TestCase(LogTestCase): 1848 """ 1849 Test logging facility of TCPv6 base classes. 1850 """ 1851 listenMethod = reactor.listenTCP6 1852 connectMethod = reactor.connectTCP6 1853 loopback = "::1" 1596 1854 1597 1855 1598 1856 try: … … 1602 1860 else: 1603 1861 numRounds = resource.getrlimit(resource.RLIMIT_NOFILE)[0] + 10 1604 1862 ProperlyCloseFilesTestCase.numberRounds = numRounds 1863 1864 try: 1865 s = socket.socket(socket.AF_INET6) 1866 except socket.error: 1867 for klass in [ 1868 "ListeningIPv6TestCase", 1869 "LoopbackIPv6TestCase", 1870 "FactoryIPv6TestCase", 1871 "ConnectorIPv6TestCase", 1872 "CannotBindIPv6TestCase", 1873 "LocalRemoteAddressIPv6TestCase", 1874 "WriteDataIPv6TestCase", 1875 "ProperlyCloseFilesIPv6TestCase", 1876 "AddressIPv6TestCase", 1877 "LargeBufferIPv6TestCase", 1878 "HalfCloseIPv6TestCase", 1879 "HalfClose2IPv6TestCase", 1880 "HalfCloseBuggyApplicationIPv6Tests", 1881 "LogIPv6TestCase", 1882 ]: 1883 klass.skip = "IPv6 is not enabled" 1884
