commit 85c923bfc784aaf8080e08053ef6b577e8c23944 Author: Ravi Chandra Padmala neenaoffline@gmail.com Date: Thu Jun 21 22:08:30 2012 +0530
Add SETCONF integration tests, SingleLineResponse unit tests and the bugfixes that come with them --- stem/control.py | 21 +++++++++++---- test/integ/control/controller.py | 49 ++++++++++++++++++++++++++++++++++++++ test/unit/response/singleline.py | 33 +++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 6 deletions(-)
diff --git a/stem/control.py b/stem/control.py index 290f178..0e0d611 100644 --- a/stem/control.py +++ b/stem/control.py @@ -685,21 +685,30 @@ class Controller(BaseController): """
if len(args) == 2: - options = {args[0]: args[1]} + args = {args[0]: args[1]} elif len(args) == 1: - options = args[0] + args = args[0] else: raise TypeError("set_conf expected 1 or 2 arguments, got %d", len(args))
- response = self.msg("SETCONF %s" % " ".join(["=".join(opts) for opts in args.items()])) + options = [] + for key, value in args.iteritems(): + options.append(key + "="" + value + """) + + response = self.msg("SETCONF %s" % " ".join(options)) stem.response.convert("SINGLELINE", response)
if response.is_ok(): return True - elif response.code in ("513", "552", "553"): - raise InvalidRequest(response.code, response.message) + elif response.code == "552": + if response.message.startswith("Unrecognized option: Unknown option '"): + key = response.message[37:response.message.find("'", 37)] + raise stem.socket.InvalidArguments(response.code, response.message, [key]) + raise stem.socket.InvalidRequest(response.code, response.message) + elif response.code in ("513", "553"): + raise stem.socket.InvalidRequest(response.code, response.message) else: - raise ProtocolError("SETCONF returned unexpected status code") + raise stem.socket.ProtocolError("SETCONF returned unexpected status code")
def _case_insensitive_lookup(entries, key): """ diff --git a/test/integ/control/controller.py b/test/integ/control/controller.py index 55f7210..4a517dd 100644 --- a/test/integ/control/controller.py +++ b/test/integ/control/controller.py @@ -191,4 +191,53 @@ class TestController(unittest.TestCase): self.assertEqual("la-di-dah", controller.get_conf("", "la-di-dah")) self.assertEqual({}, controller.get_conf_map("", "la-di-dah")) self.assertEqual({}, controller.get_conf_map([], "la-di-dah")) + + # context-sensitive keys + + keys = { + "HiddenServiceDir": "/tmp/stemtestdir", + "HiddenServicePort": "17234 127.0.0.1:17235" + } + controller.set_conf(keys) + self.assertEqual("/tmp/stemtestdir", controller.get_conf("HiddenServiceDir")) + self.assertEqual("17234 127.0.0.1:17235", controller.get_conf("HiddenServiceDir")) + + def test_setconf(self): + """ + Exercises SETCONF with valid and invalid requests. + """ + + runner = test.runner.get_runner() + + with runner.get_tor_controller() as controller: + # Single key, valid and invalid + connlimit = int(controller.get_conf("ConnLimit")) + controller.set_conf("connlimit", str(connlimit - 1)) + self.assertEqual(connlimit - 1, int(controller.get_conf("ConnLimit"))) + try: + controller.set_conf("invalidkeyboo", "abcde") + except stem.socket.InvalidArguments, exc: + self.assertEqual(["invalidkeyboo"], exc.arguments) + + settings = { + "connlimit": str(connlimit - 2), + "contactinfo": "stem@testing" + } + controller.set_conf(settings) + self.assertEqual(connlimit - 2, int(controller.get_conf("ConnLimit"))) + self.assertEqual("stem@testing", controller.get_conf("contactinfo")) + + settings["bombay"] = "vadapav" + try: + controller.set_conf(settings) + except stem.socket.InvalidArguments, exc: + self.assertEqual(["bombay"], exc.arguments) + + settings = { + "HiddenServiceDir": "/tmp/stemtestdir", + "HiddenServicePort": "17234 127.0.0.1:17235" + } + controller.set_conf(settings) + self.assertEqual("17234 127.0.0.1:17235", controller.get_conf("hiddenserviceport")) + self.assertEqual("/tmp/stemtestdir", controller.get_conf("hiddenservicedir"))
diff --git a/test/unit/response/singleline.py b/test/unit/response/singleline.py new file mode 100644 index 0000000..8781bf9 --- /dev/null +++ b/test/unit/response/singleline.py @@ -0,0 +1,33 @@ +""" +Unit tests for the stem.response.getconf.GetConfResponse class. +""" + +import unittest + +import stem.socket +import stem.response +import test.mocking as mocking + +MULTILINE_RESPONSE = """250-MULTI +250 LINE""" + +class TestSingleLineResponse(unittest.TestCase): + def test_single_line_response(self): + message = mocking.get_message("552 NOTOK") + stem.response.convert("SINGLELINE", message) + self.assertEqual(False, message.is_ok()) + message = mocking.get_message("250 KK") + stem.response.convert("SINGLELINE", message) + self.assertEqual(True, message.is_ok()) + + message = mocking.get_message("250 OK") + stem.response.convert("SINGLELINE", message) + self.assertEqual(True, message.is_ok(True)) + message = mocking.get_message("250 HMM") + stem.response.convert("SINGLELINE", message) + self.assertEqual(False, message.is_ok(True)) + + def test_multi_line_response(self): + message = mocking.get_message(MULTILINE_RESPONSE) + self.assertRaises(stem.socket.ProtocolError, stem.response.convert, "SINGLELINE", message) +
tor-commits@lists.torproject.org