Skip to content

Commit

Permalink
Some basic unit testing for custom TLS context factories.
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonspeed committed May 16, 2023
1 parent 6575300 commit 89040b1
Showing 1 changed file with 81 additions and 2 deletions.
83 changes: 81 additions & 2 deletions test/test_web.py
@@ -1,6 +1,7 @@

from mock import Mock

from twisted.web.client import BrowserLikePolicyForHTTPS
from twisted.trial import unittest
from twisted.internet import defer

Expand All @@ -14,6 +15,11 @@
from txtorcon.circuit import TorCircuitEndpoint


class CustomTLSContextFactory(BrowserLikePolicyForHTTPS):
def creatorForNetloc(self, hostname, port):
return super().creatorForNetloc(b"custom.domain", port)


class WebAgentTests(unittest.TestCase):
if not _HAVE_WEB:
skip = "Missing web"
Expand Down Expand Up @@ -56,7 +62,12 @@ def test_socks_agent_tcp_host_port(self):

def getConnection(key, endpoint):
self.assertTrue(isinstance(endpoint, TorSocksEndpoint))
self.assertTrue(endpoint._tls)
self.assertIsInstance(
endpoint._tls,
BrowserLikePolicyForHTTPS().creatorForNetloc(b"host", 443).__class__
)
# This uses a Twisted private interface...
self.assertEqual(endpoint._tls._hostname, "meejah.ca")
self.assertEqual(endpoint._host, u'meejah.ca')
self.assertEqual(endpoint._port, 443)
return defer.succeed(proto)
Expand All @@ -70,6 +81,38 @@ def getConnection(key, endpoint):
res = yield agent.request(b'GET', b'https://meejah.ca')
self.assertIs(res, gold)

@defer.inlineCallbacks
def test_socks_agent_custom_tls_context_factory(self):
reactor = Mock()
config = Mock()
config.SocksPort = []
proto = Mock()
gold = object()
proto.request = Mock(return_value=defer.succeed(gold))

def getConnection(key, endpoint):

self.assertIsInstance(
endpoint._tls,
BrowserLikePolicyForHTTPS().creatorForNetloc(b"host", 443).__class__
)
# This uses a Twisted private interface...
self.assertEqual(endpoint._tls._hostname, "custom.domain")
self.assertEqual(endpoint._host, 'meejah.ca')
return defer.succeed(proto)
pool = Mock()
pool.getConnection = getConnection

# do the test
agent = yield agent_for_socks_port(
reactor, config, '127.0.0.50:1234', pool=pool,
tls_context_factory=CustomTLSContextFactory()
)

# apart from the getConnection asserts...
res = yield agent.request(b'GET', b'https://meejah.ca')
self.assertIs(res, gold)

@defer.inlineCallbacks
def test_agent(self):
reactor = Mock()
Expand All @@ -95,7 +138,12 @@ def test_agent_with_circuit(self):
def getConnection(key, endpoint):
self.assertTrue(isinstance(endpoint, TorCircuitEndpoint))
target = endpoint._target_endpoint
self.assertTrue(target._tls)
self.assertIsInstance(
target._tls,
BrowserLikePolicyForHTTPS().creatorForNetloc(b"host", 443).__class__
)
# This uses a Twisted private interface...
self.assertEqual(target._tls._hostname, "meejah.ca")
self.assertEqual(target._host, u'meejah.ca')
self.assertEqual(target._port, 443)
return defer.succeed(proto)
Expand All @@ -107,3 +155,34 @@ def getConnection(key, endpoint):
# apart from the getConnection asserts...
res = yield agent.request(b'GET', b'https://meejah.ca')
self.assertIs(res, gold)

@defer.inlineCallbacks
def test_agent_with_circuit_tls_context_factory(self):
reactor = Mock()
circuit = Mock()
socks_ep = Mock()
proto = Mock()
gold = object()
proto.request = Mock(return_value=defer.succeed(gold))

def getConnection(key, endpoint):
target = endpoint._target_endpoint
self.assertIsInstance(
target._tls,
BrowserLikePolicyForHTTPS().creatorForNetloc(b"host", 443).__class__
)
# This uses a Twisted private interface...
self.assertEqual(target._tls._hostname, "custom.domain")
self.assertEqual(target._host, 'meejah.ca')
return defer.succeed(proto)
pool = Mock()
pool.getConnection = getConnection

agent = yield tor_agent(
reactor, socks_ep, circuit=circuit, pool=pool,
tls_context_factory=CustomTLSContextFactory()
)

# apart from the getConnection asserts...
res = yield agent.request(b'GET', b'https://meejah.ca')
self.assertIs(res, gold)

0 comments on commit 89040b1

Please sign in to comment.