/usr/lib/python2.7/dist-packages/twext/internet/test/test_gaiendpoint.py is in calendarserver 5.2+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | ##
# Copyright (c) 2012-2014 Apple Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##
"""
Test cases for L{twext.internet.gaiendpoint}
"""
from socket import getaddrinfo, AF_INET, SOCK_STREAM
from twext.internet.gaiendpoint import GAIEndpoint
from twisted.trial.unittest import TestCase
from twisted.internet.defer import Deferred
from twisted.internet.protocol import Factory, Protocol
from twisted.internet.task import Clock
class FakeTCPEndpoint(object):
def __init__(self, reactor, host, port, contextFactory):
self._reactor = reactor
self._host = host
self._port = port
self._attempt = None
self._contextFactory = contextFactory
def connect(self, factory):
self._attempt = Deferred()
self._factory = factory
return self._attempt
class GAIEndpointTestCase(TestCase):
"""
Test cases for L{GAIEndpoint}.
"""
def makeEndpoint(self, host="abcd.example.com", port=4321):
gaie = GAIEndpoint(self.clock, host, port)
gaie.subEndpoint = self.subEndpoint
gaie.deferToThread = self.deferToSomething
return gaie
def subEndpoint(self, reactor, host, port, contextFactory):
ftcpe = FakeTCPEndpoint(reactor, host, port, contextFactory)
self.fakeRealEndpoints.append(ftcpe)
return ftcpe
def deferToSomething(self, func, *a, **k):
"""
Test replacement for L{deferToThread}, which can only call
L{getaddrinfo}.
"""
d = Deferred()
if func is not getaddrinfo:
self.fail("Only getaddrinfo should be invoked in a thread.")
self.inThreads.append((d, func, a, k))
return d
def gaiResult(self, family, socktype, proto, canonname, sockaddr):
"""
A call to L{getaddrinfo} has succeeded; invoke the L{Deferred} waiting
on it.
"""
d, f, a, k = self.inThreads.pop(0)
d.callback([(family, socktype, proto, canonname, sockaddr)])
def setUp(self):
"""
Set up!
"""
self.inThreads = []
self.clock = Clock()
self.fakeRealEndpoints = []
self.makeEndpoint()
def test_simpleSuccess(self):
"""
If C{getaddrinfo} gives one L{GAIEndpoint.connect}.
"""
gaiendpoint = self.makeEndpoint()
protos = []
f = Factory()
f.protocol = Protocol
gaiendpoint.connect(f).addCallback(protos.append)
WHO_CARES = 0
WHAT_EVER = ""
self.gaiResult(AF_INET, SOCK_STREAM, WHO_CARES, WHAT_EVER,
("1.2.3.4", 4321))
self.clock.advance(1.0)
attempt = self.fakeRealEndpoints[0]._attempt
attempt.callback(self.fakeRealEndpoints[0]._factory.buildProtocol(None))
self.assertEqual(len(protos), 1)
|