commit d27fbbb06a247d3fb12684f76f2a7f5a51a36b04 Author: Ravi Chandra Padmala neenaoffline@gmail.com Date: Mon Aug 27 11:02:48 2012 -0700
Implement Controller.repurpose_circuit --- stem/control.py | 22 ++++++++++++++++++++++ test/integ/control/controller.py | 19 +++++++++++++++++++ 2 files changed, 41 insertions(+), 0 deletions(-)
diff --git a/stem/control.py b/stem/control.py index ece7f03..a9df1ba 100644 --- a/stem/control.py +++ b/stem/control.py @@ -29,6 +29,7 @@ interacting at a higher level. |- signal - sends a signal to the tor client |- new_circuit - create new circuits |- extend_circuit - create new circuits and extend existing ones + |- repurpose_circuit - change a circuit's purpose |- get_version - convenience method to get tor version |- authenticate - convenience method to authenticate the controller +- protocolinfo - convenience method to get the protocol info @@ -1055,6 +1056,27 @@ class Controller(BaseController):
raise stem.socket.ProtocolError("SIGNAL response contained unrecognized status code: %s" % response.code)
+ def repurpose_circuit(self, circuit_id, purpose): + """ + Changes a circuit's purpose. Currently, two purposes are recognized... + * general + * controller + + :param int circuit_id: id of the circuit whose purpose is to be changed + :param str purpose: purpose (either "general" or "controller") + + :raises: :class:`stem.socket.InvalidArguments` if the circuit doesn't exist or if the purpose was invalid + """ + + response = self.msg("SETCIRCUITPURPOSE %s purpose=%s" % (str(circuit_id), purpose)) + stem.response.convert("SINGLELINE", response) + + if not response.is_ok(): + if response.code == "552": + raise stem.socket.InvalidRequest(response.code, response.message) + else: + raise stem.socket.ProtocolError("SETCIRCUITPURPOSE returned unexpected response code: %s" % response.code) + def new_circuit(self, path = None, purpose = "general"): """ Requests a new circuit. If the path isn't provided, one is automatically diff --git a/test/integ/control/controller.py b/test/integ/control/controller.py index e1e24d8..5a078c0 100644 --- a/test/integ/control/controller.py +++ b/test/integ/control/controller.py @@ -376,4 +376,23 @@ class TestController(unittest.TestCase): self.assertRaises(stem.socket.InvalidRequest, controller.extend_circuit, "foo") self.assertRaises(stem.socket.InvalidRequest, controller.extend_circuit, 0, "thisroutershouldntexistbecausestemexists!@##$%#") self.assertRaises(stem.socket.InvalidRequest, controller.extend_circuit, 0, "thisroutershouldntexistbecausestemexists!@##$%#", "foo") + + def test_repurpose_circuit(self): + if test.runner.require_control(self): return + + runner = test.runner.get_runner() + + with runner.get_tor_controller() as controller: + first_circ = controller.get_info('circuit-status').splitlines()[0].split() + circ_id = int(first_circ[0]) + purpose = "CONTROLLER" + if "PURPOSE=CONTROLLER" in first_circ: + purpose = "GENERAL" + controller.repurpose_circuit(circ_id, purpose) + purpose_arg = "PURPOSE=%s" % purpose + circ = filter(lambda line: int(line.split()[0]) == circ_id, controller.get_info('circuit-status').splitlines())[0] + self.assertTrue(purpose_arg in circ) + + self.assertRaises(stem.socket.InvalidRequest, controller.repurpose_circuit, 'f934h9f3h4', "fooo") + self.assertRaises(stem.socket.InvalidRequest, controller.repurpose_circuit, '4', "fooo")
tor-commits@lists.torproject.org