Example #1
0
    def test_success(self):
        """
        Connect a stream via a circuit
        """
        reactor = Mock()
        torstate = Mock()
        target = Mock()
        target.connect = Mock(return_value=defer.succeed('fake proto'))
        circ = Mock()
        circ.state = 'NEW'
        src_addr = Mock()
        src_addr.host = 'host'
        src_addr.port = 1234
        target._get_address = Mock(return_value=defer.succeed(src_addr))
        stream = Mock()
        stream.source_port = 1234
        stream.source_addr = 'host'

        # okay, so we fire up our circuit-endpoint with mostly mocked
        # things, and a circuit that's already in 'FAILED' state.
        ep = TorCircuitEndpoint(reactor, torstate, circ, target)

        # should get a Failure from the connect()
        d = ep.connect(Mock())
        attacher = yield _get_circuit_attacher(reactor, torstate)
        yield attacher.attach_stream(stream, [circ])
        proto = yield d
        self.assertEqual(proto, 'fake proto')
Example #2
0
    def test_circuit_stream_failure(self):
        """
        If the stream-attach fails the error propagates
        """
        reactor = Mock()
        torstate = Mock()
        target = Mock()
        target.connect = Mock(return_value=defer.succeed(None))
        circ = Mock()
        circ.state = 'FAILED'
        src_addr = Mock()
        src_addr.host = 'host'
        src_addr.port = 1234
        target._get_address = Mock(return_value=defer.succeed(src_addr))
        stream = Mock()
        stream.source_port = 1234
        stream.source_addr = 'host'

        # okay, so we fire up our circuit-endpoint with mostly mocked
        # things, and a circuit that's already in 'FAILED' state.
        ep = TorCircuitEndpoint(reactor, torstate, circ, target)

        # should get a Failure from the connect()
        d = ep.connect(Mock())
        attacher = yield _get_circuit_attacher(reactor, Mock())
        attacher.attach_stream_failure(stream, RuntimeError("a bad thing"))
        try:
            yield d
            self.fail("Should get exception")
        except RuntimeError as e:
            self.assertEqual("a bad thing", str(e))
Example #3
0
    def test_attach_stream_failure(self):

        @implementer(ICircuitContainer)
        class FakeContainer(object):
            pass

        container = FakeContainer()
        stream = Stream(container)
        stream.source_addr = ipaddress.IPv4Address(u'0.0.0.0')
        stream.source_port = 12345
        circuit = Mock()
        circuit.when_built = Mock(return_value=Failure(Exception('testing1234')))
        target_endpoint = Mock()
        src_addr = Mock()
        src_addr.host = u'0.0.0.0'
        src_addr.port = 12345
        target_endpoint._get_address = Mock(return_value=defer.succeed(src_addr))
        reactor = Mock()
        state = Mock()

        TorCircuitEndpoint(
            reactor, state, circuit, target_endpoint,
        )

        attacher = yield _get_circuit_attacher(reactor, state)
        d = attacher.add_endpoint(target_endpoint, circuit)
        self.assertEquals(len(attacher._circuit_targets), 1)
        # this will fail, but should be ignored
        yield attacher.attach_stream(stream, [])
        with self.assertRaises(Exception) as ctx:
            yield d
        self.assertTrue("testing1234" in str(ctx.exception))
Example #4
0
 def endpointForURI(self, uri):
     """IAgentEndpointFactory API"""
     torsocks = TorSocksEndpoint(
         self._socks_ep,
         uri.host, uri.port,
         tls=uri.scheme == b'https',
     )
     from txtorcon.circuit import TorCircuitEndpoint
     return TorCircuitEndpoint(
         self._reactor, self._circ._torstate, self._circ, torsocks,
     )
Example #5
0
    def test_attach(self):

        @implementer(ICircuitContainer)
        class FakeContainer(object):
            pass

        container = FakeContainer()
        stream = Stream(container)
        circuit = Mock()
        target_endpoint = Mock()
        reactor = Mock()
        state = Mock()

        TorCircuitEndpoint(
            reactor, state, circuit, target_endpoint,
        )

        attacher = yield _get_circuit_attacher(reactor, state)
        attacher.add_endpoint(target_endpoint, circuit)
        yield attacher.attach_stream(stream, [])