Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Fix tests on Twisted trunk. #16528

Merged
merged 11 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions tests/http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
import subprocess
from typing import List

from incremental import Version
from zope.interface import implementer

import twisted
from OpenSSL import SSL
from OpenSSL.SSL import Connection
from twisted.internet.address import IPv4Address
from twisted.internet.interfaces import (
IOpenSSLServerConnectionCreator,
IProtocolFactory,
IReactorTime,
)
from twisted.internet.ssl import Certificate, trustRootFromCertificates
from twisted.protocols.tls import TLSMemoryBIOFactory, TLSMemoryBIOProtocol
Expand Down Expand Up @@ -157,7 +160,7 @@ def serverConnectionForTLS(self, tlsProtocol: TLSMemoryBIOProtocol) -> Connectio


def wrap_server_factory_for_tls(
factory: IProtocolFactory, sanlist: List[bytes]
factory: IProtocolFactory, clock: IReactorTime, sanlist: List[bytes]
) -> TLSMemoryBIOFactory:
"""Wrap an existing Protocol Factory with a test TLSMemoryBIOFactory

Expand All @@ -172,9 +175,15 @@ def wrap_server_factory_for_tls(
interfaces.IProtocolFactory
"""
connection_creator = TestServerTLSConnectionFactory(sanlist=sanlist)
return TLSMemoryBIOFactory(
connection_creator, isClient=False, wrappedFactory=factory
)
# Twisted > 23.8.0 has a different API that accepts a clock.
if twisted.version <= Version("Twisted", 23, 8, 0):
return TLSMemoryBIOFactory(
connection_creator, isClient=False, wrappedFactory=factory
)
else:
return TLSMemoryBIOFactory(
connection_creator, isClient=False, wrappedFactory=factory, clock=clock # type: ignore[call-arg]
)
Comment on lines +178 to +186
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did Twisted make a breaking change to TLSMemoryBIOFactory here, or are we just taking advantage of a newer API?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I can't see anything in https://github.com/twisted/twisted/blob/trunk/NEWS.rst#deprecations-and-removals so assuming we're using a newer API.)

Copy link
Member Author

@clokep clokep Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't a breaking change, you don't have to provide clock, but you do if you want our tests to pass. (Otherwise it uses the trial reactor instead of the test reactor.)



# A dummy address, useful for tests that use FakeTransport and don't care about where
Expand Down
2 changes: 2 additions & 0 deletions tests/http/federation/test_matrix_federation_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def _make_connection(
if ssl:
server_factory = wrap_server_factory_for_tls(
server_factory,
self.reactor,
tls_sanlist
or [
b"DNS:testserv",
Expand Down Expand Up @@ -443,6 +444,7 @@ def _do_get_via_proxy(
# now we make another test server to act as the upstream HTTP server.
server_ssl_protocol = wrap_server_factory_for_tls(
_get_test_protocol_factory(),
self.reactor,
sanlist=[
b"DNS:testserv",
b"DNS:target-server",
Expand Down
6 changes: 3 additions & 3 deletions tests/http/test_proxyagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def _make_connection(
"""
if ssl:
server_factory = wrap_server_factory_for_tls(
server_factory, tls_sanlist or [b"DNS:test.com"]
server_factory, self.reactor, tls_sanlist or [b"DNS:test.com"]
)

server_protocol = server_factory.buildProtocol(dummy_address)
Expand Down Expand Up @@ -617,7 +617,7 @@ def _do_https_request_via_proxy(

# now we make another test server to act as the upstream HTTP server.
server_ssl_protocol = wrap_server_factory_for_tls(
_get_test_protocol_factory(), sanlist=[b"DNS:test.com"]
_get_test_protocol_factory(), self.reactor, sanlist=[b"DNS:test.com"]
).buildProtocol(dummy_address)

# Tell the HTTP server to send outgoing traffic back via the proxy's transport.
Expand Down Expand Up @@ -784,7 +784,7 @@ def test_https_request_via_uppercase_proxy_with_blocklist(self) -> None:

# now we can replace the proxy channel with a new, SSL-wrapped HTTP channel
ssl_factory = wrap_server_factory_for_tls(
_get_test_protocol_factory(), sanlist=[b"DNS:test.com"]
_get_test_protocol_factory(), self.reactor, sanlist=[b"DNS:test.com"]
)
ssl_protocol = ssl_factory.buildProtocol(dummy_address)
assert isinstance(ssl_protocol, TLSMemoryBIOProtocol)
Expand Down
2 changes: 1 addition & 1 deletion tests/replication/test_multi_media_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def _get_media_req(
server_factory.log = _log_request

server_tls_protocol = wrap_server_factory_for_tls(
server_factory, sanlist=[b"DNS:example.com"]
server_factory, self.reactor, sanlist=[b"DNS:example.com"]
).buildProtocol(None)

# now, tell the client protocol factory to build the client protocol (it will be a
Expand Down
14 changes: 13 additions & 1 deletion tests/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
Union,
cast,
)
from unittest.mock import Mock
from unittest.mock import Mock, patch

import attr
from typing_extensions import ParamSpec
Expand Down Expand Up @@ -474,6 +474,18 @@ def getHostByName(
return fail(DNSLookupError("OH NO: unknown %s" % (name,)))
return succeed(lookups[name])

# In order for the TLS protocols to use the proper we need to patch
clokep marked this conversation as resolved.
Show resolved Hide resolved
# _get_default_clock.
#
# This is *super* dirty since we never unpatch and rely on the next test
# to patch over us.
#
# Use create=True for backwards compatibilty with Twisted <= 23.8.0.
self._tls_clock_patcher = patch(
"twisted.protocols.tls._get_default_clock", return_value=self, create=True
)
self._tls_clock_patcher.start()

self.nameResolver = SimpleResolverComplexifier(FakeResolver())
super().__init__()

Expand Down
Loading