Ticket #6024: tls-when-handshake-done-6204-3-clean-up-tests.patch

File tls-when-handshake-done-6204-3-clean-up-tests.patch, 8.4 KB (added by Andy Lutomirski, 7 years ago)

follow-up patch to remove HandshakeCallbackContextFactory

  • twisted/protocols/test/test_tls.py

    From efba28acedf76acf42a4b8082f0f76e9910fbe0d Mon Sep 17 00:00:00 2001
    Message-Id: <efba28acedf76acf42a4b8082f0f76e9910fbe0d.1384213241.git.luto@amacapital.net>
    In-Reply-To: <ac8953b397664aef3efef3aedbe3c3cf60340c98.1384213241.git.luto@amacapital.net>
    References: <ac8953b397664aef3efef3aedbe3c3cf60340c98.1384213241.git.luto@amacapital.net>
    From: Andy Lutomirski <luto@amacapital.net>
    Date: Mon, 11 Nov 2013 15:37:38 -0800
    Subject: [PATCH 2/2] Remove
     twisted.protocols.test.test_tls.HandshakeCallbackContextFactory
    
    It's no longer necessary -- whenHandshakeDone is a better replacement.
    ---
     twisted/protocols/test/test_tls.py | 76 ++++++++++----------------------------
     1 file changed, 19 insertions(+), 57 deletions(-)
    
    diff --git a/twisted/protocols/test/test_tls.py b/twisted/protocols/test/test_tls.py
    index b170846..c5651d6 100644
    a b from twisted.test.test_tcp import ConnectionLostNotifyingProtocol 
    4242from twisted.test.proto_helpers import StringTransport
    4343
    4444
    45 class HandshakeCallbackContextFactory:
     45class TestContextFactory:
    4646    """
    47     L{HandshakeCallbackContextFactory} is a factory for SSL contexts which
    48     allows applications to get notification when the SSL handshake completes.
    49 
    50     @ivar _finished: A L{Deferred} which will be called back when the handshake
    51         is done.
     47    L{TestContextFactory} is a trivial factory for SSL contexts.
    5248    """
    53     # pyOpenSSL needs to expose this.
    54     # https://bugs.launchpad.net/pyopenssl/+bug/372832
    55     SSL_CB_HANDSHAKE_DONE = 0x20
    56 
    57     def __init__(self):
    58         self._finished = Deferred()
    59 
    60 
    61     def factoryAndDeferred(cls):
    62         """
    63         Create a new L{HandshakeCallbackContextFactory} and return a two-tuple
    64         of it and a L{Deferred} which will fire when a connection created with
    65         it completes a TLS handshake.
    66         """
    67         contextFactory = cls()
    68         return contextFactory, contextFactory._finished
    69     factoryAndDeferred = classmethod(factoryAndDeferred)
    70 
    71 
    72     def _info(self, connection, where, ret):
    73         """
    74         This is the "info callback" on the context.  It will be called
    75         periodically by pyOpenSSL with information about the state of a
    76         connection.  When it indicates the handshake is complete, it will fire
    77         C{self._finished}.
    78         """
    79         if where & self.SSL_CB_HANDSHAKE_DONE:
    80             self._finished.callback(None)
    81 
    82 
    8349    def getContext(self):
    8450        """
    85         Create and return an SSL context configured to use L{self._info} as the
    86         info callback.
     51        Create and return an SSL context.
    8752        """
    88         context = Context(TLSv1_METHOD)
    89         context.set_info_callback(self._info)
    90         return context
     53        return Context(TLSv1_METHOD)
    9154
    9255
    9356
    class TLSMemoryBIOTests(TestCase): 
    290253        clientFactory = ClientFactory()
    291254        clientFactory.protocol = Protocol
    292255
    293         clientContextFactory, _ = (
    294             HandshakeCallbackContextFactory.factoryAndDeferred())
     256        clientContextFactory = TestContextFactory()
    295257        wrapperFactory = TLSMemoryBIOFactory(
    296258            clientContextFactory, True, clientFactory)
    297259        sslClientProtocol = wrapperFactory.buildProtocol(None)
    class TLSMemoryBIOTests(TestCase): 
    334296            lambda: ConnectionLostNotifyingProtocol(
    335297                clientConnectionLost))
    336298
    337         clientContextFactory = HandshakeCallbackContextFactory()
     299        clientContextFactory = TestContextFactory()
    338300        wrapperFactory = TLSMemoryBIOFactory(
    339301            clientContextFactory, True, clientFactory)
    340302        sslClientProtocol = wrapperFactory.buildProtocol(None)
    class TLSMemoryBIOTests(TestCase): 
    399361        clientFactory = ClientFactory()
    400362        clientFactory.protocol = Protocol
    401363
    402         clientContextFactory = HandshakeCallbackContextFactory()
     364        clientContextFactory = TestContextFactory()
    403365        wrapperFactory = TLSMemoryBIOFactory(
    404366            clientContextFactory, True, clientFactory)
    405367        sslClientProtocol = wrapperFactory.buildProtocol(None)
    class TLSMemoryBIOTests(TestCase): 
    446408        clientFactory = ClientFactory()
    447409        clientFactory.protocol = Protocol
    448410
    449         clientContextFactory = HandshakeCallbackContextFactory()
     411        clientContextFactory = TestContextFactory()
    450412        wrapperFactory = TLSMemoryBIOFactory(
    451413            clientContextFactory, True, clientFactory)
    452414        sslClientProtocol = wrapperFactory.buildProtocol(None)
    class TLSMemoryBIOTests(TestCase): 
    484446        clientFactory = ClientFactory()
    485447        clientFactory.protocol = Protocol
    486448
    487         clientContextFactory, handshakeDeferred = (
    488             HandshakeCallbackContextFactory.factoryAndDeferred())
     449        clientContextFactory = TestContextFactory()
    489450        wrapperFactory = TLSMemoryBIOFactory(
    490451            clientContextFactory, True, clientFactory)
    491452        sslClientProtocol = wrapperFactory.buildProtocol(None)
     453        handshakeDeferred = sslClientProtocol.whenHandshakeDone()
    492454
    493455        serverFactory = ServerFactory()
    494456        serverFactory.protocol = Protocol
    class TLSMemoryBIOTests(TestCase): 
    524486        clientFactory = ClientFactory()
    525487        clientFactory.protocol = lambda: clientProtocol
    526488
    527         clientContextFactory, handshakeDeferred = (
    528             HandshakeCallbackContextFactory.factoryAndDeferred())
     489        clientContextFactory = TestContextFactory()
    529490        wrapperFactory = TLSMemoryBIOFactory(
    530491            clientContextFactory, True, clientFactory)
    531492        sslClientProtocol = wrapperFactory.buildProtocol(None)
     493        handshakeDeferred = sslClientProtocol.whenHandshakeDone()
    532494
    533495        serverProtocol = AccumulatingProtocol(len(bytes))
    534496        serverFactory = ServerFactory()
    class TLSMemoryBIOTests(TestCase): 
    566528        clientFactory = ClientFactory()
    567529        clientFactory.protocol = sendingProtocol
    568530
    569         clientContextFactory, handshakeDeferred = (
    570             HandshakeCallbackContextFactory.factoryAndDeferred())
     531        clientContextFactory = TestContextFactory()
    571532        wrapperFactory = TLSMemoryBIOFactory(
    572533            clientContextFactory, True, clientFactory)
    573534        sslClientProtocol = wrapperFactory.buildProtocol(None)
     535        handshakeDeferred = sslClientProtocol.whenHandshakeDone()
    574536
    575537        serverProtocol = AccumulatingProtocol(len(bytes))
    576538        serverFactory = ServerFactory()
    class TLSMemoryBIOTests(TestCase): 
    668630        clientFactory = ClientFactory()
    669631        clientFactory.protocol = SimpleSendingProtocol
    670632
    671         clientContextFactory = HandshakeCallbackContextFactory()
     633        clientContextFactory = TestContextFactory()
    672634        wrapperFactory = TLSMemoryBIOFactory(
    673635            clientContextFactory, True, clientFactory)
    674636        sslClientProtocol = wrapperFactory.buildProtocol(None)
    class TLSMemoryBIOTests(TestCase): 
    707669        clientFactory = ClientFactory()
    708670        clientFactory.protocol = SimpleSendingProtocol
    709671
    710         clientContextFactory = HandshakeCallbackContextFactory()
     672        clientContextFactory = TestContextFactory()
    711673        wrapperFactory = TLSMemoryBIOFactory(
    712674            clientContextFactory, True, clientFactory)
    713675        sslClientProtocol = wrapperFactory.buildProtocol(None)
    class TLSMemoryBIOTests(TestCase): 
    742704            lambda: ConnectionLostNotifyingProtocol(
    743705                clientConnectionLost))
    744706
    745         clientContextFactory = HandshakeCallbackContextFactory()
     707        clientContextFactory = TestContextFactory()
    746708        wrapperFactory = TLSMemoryBIOFactory(
    747709            clientContextFactory, True, clientFactory)
    748710        sslClientProtocol = wrapperFactory.buildProtocol(None)
    class TLSMemoryBIOTests(TestCase): 
    782744        clientProtocol = NotifyingProtocol(clientConnectionLost)
    783745        clientFactory.protocol = lambda: clientProtocol
    784746
    785         clientContextFactory, handshakeDeferred = (
    786             HandshakeCallbackContextFactory.factoryAndDeferred())
     747        clientContextFactory = TestContextFactory()
    787748        wrapperFactory = TLSMemoryBIOFactory(
    788749            clientContextFactory, True, clientFactory)
    789750        sslClientProtocol = wrapperFactory.buildProtocol(None)
     751        handshakeDeferred = sslClientProtocol.whenHandshakeDone()
    790752
    791753        serverConnectionLost = Deferred()
    792754        serverProtocol = NotifyingProtocol(serverConnectionLost)