class TorStreamTests(unittest.TestCase): def setUp(self): reactor = Mock() self.cfg = Mock() self.tor = Tor(reactor, self.cfg) def test_sanity(self): self.assertTrue(_is_non_public_numeric_address(u'10.0.0.0')) self.assertTrue(_is_non_public_numeric_address(u'::1')) def test_v6(self): import ipaddress ipaddress.ip_address(u'2603:3023:807:3d00:21e:52ff:fe71:a4ce') def test_stream_private_ip(self): with self.assertRaises(Exception) as ctx: self.tor.stream_via('10.0.0.1', '1234') self.assertTrue("isn't going to work over Tor", str(ctx.exception)) def test_stream_v6(self): with self.assertRaises(Exception) as ctx: self.tor.stream_via(u'::1', '1234') self.assertTrue("isn't going to work over Tor", str(ctx.exception)) def test_public_v6(self): # should not be an error self.tor.stream_via(u'2603:3023:807:3d00:21e:52ff:fe71:a4ce', '4321') def test_public_v4(self): # should not be an error self.tor.stream_via(u'8.8.8.8', '4321') def test_stream_host(self): self.tor.stream_via(b'meejah.ca', '1234')
def test_quit_no_protocol(self): tor = Tor(Mock(), Mock()) tor._protocol = None tor._process_protocol = None with self.assertRaises(RuntimeError) as ctx: yield tor.quit() self.assertTrue('no protocol instance' in str(ctx.exception))
def test_web_agent_error(self): reactor = Mock() cfg = Mock() tor = Tor(reactor, cfg) with self.assertRaises(ValueError) as ctx: agent = tor.web_agent(pool=self.pool, _socks_endpoint=object()) yield agent.request('GET', b'meejah.ca') self.assertTrue("'_socks_endpoint' should be" in str(ctx.exception))
def test_web_agent_deferred(self): socks_d = defer.succeed("9151") reactor = Mock() cfg = Mock() tor = Tor(reactor, cfg) agent = tor.web_agent(pool=self.pool, _socks_endpoint=socks_d) resp = yield agent.request('GET', b'meejah.ca') self.assertEqual(self.expected_response, resp)
def test_web_agent_endpoint(self): socks = Mock() directlyProvides(socks, IStreamClientEndpoint) reactor = Mock() cfg = Mock() tor = Tor(reactor, cfg) agent = tor.web_agent(pool=self.pool, _socks_endpoint=socks) resp = yield agent.request('GET', b'meejah.ca') self.assertEqual(self.expected_response, resp)
def test_dns_resolve_no_configured_socks(self, fake_socks): answer = object() tor = Tor(Mock(), Mock()) def boom(*args, **kw): raise RuntimeError("no socks") tor._config.socks_endpoint = Mock(side_effect=boom) fake_socks.resolve = Mock(return_value=defer.succeed(answer)) ans = yield tor.dns_resolve("meejah.ca") self.assertEqual(ans, answer)
def test_dns_resolve_existing_socks(self, fake_socks): answer = object() tor = Tor(Mock(), Mock()) fake_socks.resolve = Mock(return_value=defer.succeed(answer)) ans0 = yield tor.dns_resolve("meejah.ca") # do it again to exercise the _default_socks_port() case when # we already got the default fake_socks.resolve = Mock(return_value=defer.succeed(answer)) ans1 = yield tor.dns_resolve("meejah.ca") self.assertEqual(ans0, answer) self.assertEqual(ans1, answer)
def test_web_agent_defaults(self): reactor = Mock() cfg = Mock() cfg.create_socks_endpoint = Mock(return_value=defer.succeed("9050")) tor = Tor(reactor, cfg) try: agent = tor.web_agent(pool=self.pool) except ImportError as e: if 'IAgentEndpointFactory' in str(e): print("Skipping; appears we don't have web support") return resp = yield agent.request('GET', b'meejah.ca') self.assertEqual(self.expected_response, resp)
def test_dns_resolve_ptr(self, fake_socks): answer = object() tor = Tor(Mock(), Mock()) fake_socks.resolve_ptr = Mock(return_value=defer.succeed(answer)) ans = yield tor.dns_resolve_ptr("4.3.2.1") self.assertEqual(ans, answer)
def test_dns_resolve(self, fake_socks): answer = object() tor = Tor(Mock(), Mock()) fake_socks.resolve = Mock(return_value=defer.succeed(answer)) ans = yield tor.dns_resolve("meejah.ca") self.assertEqual(ans, answer)
def test_quit(self): tor = Tor(Mock(), Mock()) tor._protocol = Mock() tor._process_protocol = Mock() yield tor.quit()
def test_str(self): tor = Tor(Mock(), Mock()) str(tor)
def test_create_state(self): tor = Tor(Mock(), Mock()) with patch('txtorcon.controller.TorState') as ts: ts.post_boostrap = defer.succeed('boom') yield tor.create_state()
def setUp(self): reactor = Mock() self.cfg = Mock() self.tor = Tor(reactor, self.cfg)