commit 708e082fb7bdddf2261a44fb290d20d768bc180c Author: Damian Johnson atagar@torproject.org Date: Mon Nov 21 07:42:14 2011 -0800
Moving PROTOCOLINFO tests to be grouped by subject
All the PROTOCOLINFO related tests might as well be together. Shuffling them around so all the tests can reside in a test/*/protocolinfo.py rather than have separate protocolinfo_response.py, protocolinfo_query.py, etc. --- run_tests.py | 8 +- test/integ/connection/__init__.py | 2 +- test/integ/connection/protocolinfo.py | 65 +++++++++ test/integ/connection/protocolinfo_response.py | 65 --------- test/unit/connection/__init__.py | 2 +- test/unit/connection/protocolinfo.py | 181 ++++++++++++++++++++++++ test/unit/connection/protocolinfo_response.py | 181 ------------------------ 7 files changed, 252 insertions(+), 252 deletions(-)
diff --git a/run_tests.py b/run_tests.py index 9189908..2d12a57 100755 --- a/run_tests.py +++ b/run_tests.py @@ -16,13 +16,13 @@ import test.runner import test.unit.types.control_message import test.unit.types.control_line import test.unit.types.version -import test.unit.connection.protocolinfo_response +import test.unit.connection.protocolinfo import test.unit.util.enum import test.unit.util.system import test.integ.types.control_message import test.integ.util.conf import test.integ.util.system -import test.integ.connection.protocolinfo_response +import test.integ.connection.protocolinfo
import stem.util.enum import stem.util.term as term @@ -35,13 +35,13 @@ DIVIDER = "=" * 70 UNIT_TESTS = (("stem.types.ControlMessage", test.unit.types.control_message.TestControlMessage), ("stem.types.ControlLine", test.unit.types.control_line.TestControlLine), ("stem.types.Version", test.unit.types.version.TestVerion), - ("stem.connection.ProtocolInfoResponse", test.unit.connection.protocolinfo_response.TestProtocolInfoResponse), + ("stem.connection.ProtocolInfoResponse", test.unit.connection.protocolinfo.TestProtocolInfoResponse), ("stem.util.enum", test.unit.util.enum.TestEnum), ("stem.util.system", test.unit.util.system.TestSystem), )
INTEG_TESTS = (("stem.types.ControlMessage", test.integ.types.control_message.TestControlMessage), - ("stem.connection.ProtocolInfoResponse", test.integ.connection.protocolinfo_response.TestProtocolInfoResponse), + ("stem.connection.ProtocolInfoResponse", test.integ.connection.protocolinfo.TestProtocolInfoResponse), ("stem.util.conf", test.integ.util.conf.TestConf), ("stem.util.system", test.integ.util.system.TestSystem), ) diff --git a/test/integ/connection/__init__.py b/test/integ/connection/__init__.py index 6a71f2a..dd0925e 100644 --- a/test/integ/connection/__init__.py +++ b/test/integ/connection/__init__.py @@ -2,5 +2,5 @@ Integration tests for stem.connection. """
-__all__ = ["protocolinfo_response"] +__all__ = ["protocolinfo"]
diff --git a/test/integ/connection/protocolinfo.py b/test/integ/connection/protocolinfo.py new file mode 100644 index 0000000..27b5f1f --- /dev/null +++ b/test/integ/connection/protocolinfo.py @@ -0,0 +1,65 @@ +""" +Integration tests for the stem.connections.ProtocolInfoResponse class. +""" + +import socket +import unittest + +import test.runner +import stem.types +import stem.connection + +class TestProtocolInfoResponse(unittest.TestCase): + """ + Processes a ProtocolInfo query for a variety of setups. + """ + + def testProtocolInfoResponse(self): + """ + Makes a PROTOCOLINFO query and processes the response for our control + connection. + """ + + runner = test.runner.get_runner() + connection_type = runner.get_connection_type() + + if connection_type == test.runner.TorConnection.NONE: + self.skipTest("(no connection)") + + control_socket = runner.get_tor_socket(False) + control_socket_file = control_socket.makefile() + + control_socket_file.write("PROTOCOLINFO\r\n") + control_socket_file.flush() + + protocolinfo_response = stem.types.read_message(control_socket_file) + stem.connection.ProtocolInfoResponse.convert(protocolinfo_response) + + # according to the control spec the following _could_ differ or be + # undefined but if that actually happens then it's gonna make people sad + + self.assertEqual(1, protocolinfo_response.protocol_version) + self.assertNotEqual(None, protocolinfo_response.tor_version) + self.assertNotEqual(None, protocolinfo_response.auth_methods) + + self.assertEqual((), protocolinfo_response.unknown_auth_methods) + self.assertEqual(None, protocolinfo_response.socket) + + if connection_type == test.runner.TorConnection.NO_AUTH: + self.assertEqual((stem.connection.AuthMethod.NONE,), protocolinfo_response.auth_methods) + self.assertEqual(None, protocolinfo_response.cookie_file) + elif connection_type == test.runner.TorConnection.PASSWORD: + self.assertEqual((stem.connection.AuthMethod.PASSWORD,), protocolinfo_response.auth_methods) + self.assertEqual(None, protocolinfo_response.cookie_file) + elif connection_type == test.runner.TorConnection.COOKIE: + self.assertEqual((stem.connection.AuthMethod.COOKIE,), protocolinfo_response.auth_methods) + self.assertEqual(runner.get_auth_cookie_path(), protocolinfo_response.cookie_file) + elif connection_type == test.runner.TorConnection.MULTIPLE: + self.assertEqual((stem.connection.AuthMethod.COOKIE, stem.connection.AuthMethod.PASSWORD), protocolinfo_response.auth_methods) + self.assertEqual(runner.get_auth_cookie_path(), protocolinfo_response.cookie_file) + elif connection_type == test.runner.TorConnection.SOCKET: + self.assertEqual((stem.connection.AuthMethod.NONE,), protocolinfo_response.auth_methods) + self.assertEqual(None, protocolinfo_response.cookie_file) + else: + self.fail("Unrecognized connection type: %s" % connection_type) + diff --git a/test/integ/connection/protocolinfo_response.py b/test/integ/connection/protocolinfo_response.py deleted file mode 100644 index 27b5f1f..0000000 --- a/test/integ/connection/protocolinfo_response.py +++ /dev/null @@ -1,65 +0,0 @@ -""" -Integration tests for the stem.connections.ProtocolInfoResponse class. -""" - -import socket -import unittest - -import test.runner -import stem.types -import stem.connection - -class TestProtocolInfoResponse(unittest.TestCase): - """ - Processes a ProtocolInfo query for a variety of setups. - """ - - def testProtocolInfoResponse(self): - """ - Makes a PROTOCOLINFO query and processes the response for our control - connection. - """ - - runner = test.runner.get_runner() - connection_type = runner.get_connection_type() - - if connection_type == test.runner.TorConnection.NONE: - self.skipTest("(no connection)") - - control_socket = runner.get_tor_socket(False) - control_socket_file = control_socket.makefile() - - control_socket_file.write("PROTOCOLINFO\r\n") - control_socket_file.flush() - - protocolinfo_response = stem.types.read_message(control_socket_file) - stem.connection.ProtocolInfoResponse.convert(protocolinfo_response) - - # according to the control spec the following _could_ differ or be - # undefined but if that actually happens then it's gonna make people sad - - self.assertEqual(1, protocolinfo_response.protocol_version) - self.assertNotEqual(None, protocolinfo_response.tor_version) - self.assertNotEqual(None, protocolinfo_response.auth_methods) - - self.assertEqual((), protocolinfo_response.unknown_auth_methods) - self.assertEqual(None, protocolinfo_response.socket) - - if connection_type == test.runner.TorConnection.NO_AUTH: - self.assertEqual((stem.connection.AuthMethod.NONE,), protocolinfo_response.auth_methods) - self.assertEqual(None, protocolinfo_response.cookie_file) - elif connection_type == test.runner.TorConnection.PASSWORD: - self.assertEqual((stem.connection.AuthMethod.PASSWORD,), protocolinfo_response.auth_methods) - self.assertEqual(None, protocolinfo_response.cookie_file) - elif connection_type == test.runner.TorConnection.COOKIE: - self.assertEqual((stem.connection.AuthMethod.COOKIE,), protocolinfo_response.auth_methods) - self.assertEqual(runner.get_auth_cookie_path(), protocolinfo_response.cookie_file) - elif connection_type == test.runner.TorConnection.MULTIPLE: - self.assertEqual((stem.connection.AuthMethod.COOKIE, stem.connection.AuthMethod.PASSWORD), protocolinfo_response.auth_methods) - self.assertEqual(runner.get_auth_cookie_path(), protocolinfo_response.cookie_file) - elif connection_type == test.runner.TorConnection.SOCKET: - self.assertEqual((stem.connection.AuthMethod.NONE,), protocolinfo_response.auth_methods) - self.assertEqual(None, protocolinfo_response.cookie_file) - else: - self.fail("Unrecognized connection type: %s" % connection_type) - diff --git a/test/unit/connection/__init__.py b/test/unit/connection/__init__.py index 440773e..f93234b 100644 --- a/test/unit/connection/__init__.py +++ b/test/unit/connection/__init__.py @@ -2,5 +2,5 @@ Unit tests for stem.connection. """
-__all__ = ["protocolinfo_response"] +__all__ = ["protocolinfo"]
diff --git a/test/unit/connection/protocolinfo.py b/test/unit/connection/protocolinfo.py new file mode 100644 index 0000000..b9dcbb8 --- /dev/null +++ b/test/unit/connection/protocolinfo.py @@ -0,0 +1,181 @@ +""" +Unit tests for the stem.connection.ProtocolInfoResponse class. +""" + +import unittest +import StringIO +import stem.connection +import stem.types + +NO_AUTH = """250-PROTOCOLINFO 1 +250-AUTH METHODS=NULL +250-VERSION Tor="0.2.1.30" +250 OK +""".replace("\n", "\r\n") + +PASSWORD_AUTH = """250-PROTOCOLINFO 1 +250-AUTH METHODS=HASHEDPASSWORD +250-VERSION Tor="0.2.1.30" +250 OK +""".replace("\n", "\r\n") + +COOKIE_AUTH = r"""250-PROTOCOLINFO 1 +250-AUTH METHODS=COOKIE COOKIEFILE="/tmp/my data\"dir//control_auth_cookie" +250-VERSION Tor="0.2.1.30" +250 OK +""".replace("\n", "\r\n") + +MULTIPLE_AUTH = """250-PROTOCOLINFO 1 +250-AUTH METHODS=COOKIE,HASHEDPASSWORD COOKIEFILE="/home/atagar/.tor/control_auth_cookie" +250-VERSION Tor="0.2.1.30" +250 OK +""".replace("\n", "\r\n") + +UNKNOWN_AUTH = """250-PROTOCOLINFO 1 +250-AUTH METHODS=MAGIC,HASHEDPASSWORD,PIXIE_DUST +250-VERSION Tor="0.2.1.30" +250 OK +""".replace("\n", "\r\n") + +MINIMUM_RESPONSE = """250-PROTOCOLINFO 5 +250 OK +""".replace("\n", "\r\n") + +RELATIVE_COOKIE_PATH = r"""250-PROTOCOLINFO 1 +250-AUTH METHODS=COOKIE COOKIEFILE="./tor-browser_en-US/Data/control_auth_cookie" +250-VERSION Tor="0.2.1.30" +250 OK +""".replace("\n", "\r\n") + +class TestProtocolInfoResponse(unittest.TestCase): + """ + Tests the parsing of ControlMessages for PROTOCOLINFO responses. + """ + + def test_convert(self): + """ + Exercises functionality of the convert method both when it works and + there's an error. + """ + + # working case + control_message = stem.types.read_message(StringIO.StringIO(NO_AUTH)) + stem.connection.ProtocolInfoResponse.convert(control_message) + + # now this should be a ProtocolInfoResponse (ControlMessage subclass) + self.assertTrue(isinstance(control_message, stem.types.ControlMessage)) + self.assertTrue(isinstance(control_message, stem.connection.ProtocolInfoResponse)) + + # exercise some of the ControlMessage functionality + self.assertTrue(str(control_message).startswith("PROTOCOLINFO 1")) + self.assertEquals(NO_AUTH, control_message.raw_content()) + + # attempt to convert the wrong type + self.assertRaises(TypeError, stem.connection.ProtocolInfoResponse.convert, "hello world") + + # attempt to convert a different message type + bw_event_control_message = stem.types.read_message(StringIO.StringIO("650 BW 32326 2856\r\n")) + self.assertRaises(stem.types.ProtocolError, stem.connection.ProtocolInfoResponse.convert, bw_event_control_message) + + def test_no_auth(self): + """ + Checks a response when there's no authentication. + """ + + control_message = stem.types.read_message(StringIO.StringIO(NO_AUTH)) + stem.connection.ProtocolInfoResponse.convert(control_message) + + self.assertEquals(1, control_message.protocol_version) + self.assertEquals(stem.types.Version("0.2.1.30"), control_message.tor_version) + self.assertEquals((stem.connection.AuthMethod.NONE, ), control_message.auth_methods) + self.assertEquals((), control_message.unknown_auth_methods) + self.assertEquals(None, control_message.cookie_file) + self.assertEquals(None, control_message.socket) + + def test_password_auth(self): + """ + Checks a response with password authentication. + """ + + control_message = stem.types.read_message(StringIO.StringIO(PASSWORD_AUTH)) + stem.connection.ProtocolInfoResponse.convert(control_message) + self.assertEquals((stem.connection.AuthMethod.PASSWORD, ), control_message.auth_methods) + + def test_cookie_auth(self): + """ + Checks a response with cookie authentication and a path including escape + characters. + """ + + control_message = stem.types.read_message(StringIO.StringIO(COOKIE_AUTH)) + stem.connection.ProtocolInfoResponse.convert(control_message) + self.assertEquals((stem.connection.AuthMethod.COOKIE, ), control_message.auth_methods) + self.assertEquals("/tmp/my data\"dir//control_auth_cookie", control_message.cookie_file) + + def test_multiple_auth(self): + """ + Checks a response with multiple authentication methods. + """ + + control_message = stem.types.read_message(StringIO.StringIO(MULTIPLE_AUTH)) + stem.connection.ProtocolInfoResponse.convert(control_message) + self.assertEquals((stem.connection.AuthMethod.COOKIE, stem.connection.AuthMethod.PASSWORD), control_message.auth_methods) + self.assertEquals("/home/atagar/.tor/control_auth_cookie", control_message.cookie_file) + + def test_unknown_auth(self): + """ + Checks a response with an unrecognized authtentication method. + """ + + control_message = stem.types.read_message(StringIO.StringIO(UNKNOWN_AUTH)) + stem.connection.ProtocolInfoResponse.convert(control_message) + self.assertEquals((stem.connection.AuthMethod.UNKNOWN, stem.connection.AuthMethod.PASSWORD), control_message.auth_methods) + self.assertEquals(("MAGIC", "PIXIE_DUST"), control_message.unknown_auth_methods) + + def test_minimum_response(self): + """ + Checks a PROTOCOLINFO response that only contains the minimum amount of + information to be a valid response. + """ + + control_message = stem.types.read_message(StringIO.StringIO(MINIMUM_RESPONSE)) + stem.connection.ProtocolInfoResponse.convert(control_message) + + self.assertEquals(5, control_message.protocol_version) + self.assertEquals(None , control_message.tor_version) + self.assertEquals((), control_message.auth_methods) + self.assertEquals((), control_message.unknown_auth_methods) + self.assertEquals(None, control_message.cookie_file) + self.assertEquals(None, control_message.socket) + + def test_relative_cookie(self): + """ + Checks an authentication cookie with a relative path where expansion both + succeeds and fails. + """ + + # we need to mock both pid and cwd lookups since the general cookie + # expanion works by... + # - resolving the pid of the "tor" process + # - using that to get tor's cwd + + def call_mocking(command): + if command == stem.util.system.GET_PID_BY_NAME_PGREP % "tor": + return ["10"] + if command == stem.util.system.GET_CWD_PWDX % 10: + return ["10: /tmp/foo"] + + stem.util.system.CALL_MOCKING = call_mocking + + control_message = stem.types.read_message(StringIO.StringIO(RELATIVE_COOKIE_PATH)) + stem.connection.ProtocolInfoResponse.convert(control_message) + self.assertEquals("/tmp/foo/tor-browser_en-US/Data/control_auth_cookie", control_message.cookie_file) + + # exercise cookie expansion where both calls fail (should work, just + # leaving the path unexpanded) + + stem.util.system.CALL_MOCKING = lambda cmd: None + control_message = stem.types.read_message(StringIO.StringIO(RELATIVE_COOKIE_PATH)) + stem.connection.ProtocolInfoResponse.convert(control_message) + self.assertEquals("./tor-browser_en-US/Data/control_auth_cookie", control_message.cookie_file) + diff --git a/test/unit/connection/protocolinfo_response.py b/test/unit/connection/protocolinfo_response.py deleted file mode 100644 index b9dcbb8..0000000 --- a/test/unit/connection/protocolinfo_response.py +++ /dev/null @@ -1,181 +0,0 @@ -""" -Unit tests for the stem.connection.ProtocolInfoResponse class. -""" - -import unittest -import StringIO -import stem.connection -import stem.types - -NO_AUTH = """250-PROTOCOLINFO 1 -250-AUTH METHODS=NULL -250-VERSION Tor="0.2.1.30" -250 OK -""".replace("\n", "\r\n") - -PASSWORD_AUTH = """250-PROTOCOLINFO 1 -250-AUTH METHODS=HASHEDPASSWORD -250-VERSION Tor="0.2.1.30" -250 OK -""".replace("\n", "\r\n") - -COOKIE_AUTH = r"""250-PROTOCOLINFO 1 -250-AUTH METHODS=COOKIE COOKIEFILE="/tmp/my data\"dir//control_auth_cookie" -250-VERSION Tor="0.2.1.30" -250 OK -""".replace("\n", "\r\n") - -MULTIPLE_AUTH = """250-PROTOCOLINFO 1 -250-AUTH METHODS=COOKIE,HASHEDPASSWORD COOKIEFILE="/home/atagar/.tor/control_auth_cookie" -250-VERSION Tor="0.2.1.30" -250 OK -""".replace("\n", "\r\n") - -UNKNOWN_AUTH = """250-PROTOCOLINFO 1 -250-AUTH METHODS=MAGIC,HASHEDPASSWORD,PIXIE_DUST -250-VERSION Tor="0.2.1.30" -250 OK -""".replace("\n", "\r\n") - -MINIMUM_RESPONSE = """250-PROTOCOLINFO 5 -250 OK -""".replace("\n", "\r\n") - -RELATIVE_COOKIE_PATH = r"""250-PROTOCOLINFO 1 -250-AUTH METHODS=COOKIE COOKIEFILE="./tor-browser_en-US/Data/control_auth_cookie" -250-VERSION Tor="0.2.1.30" -250 OK -""".replace("\n", "\r\n") - -class TestProtocolInfoResponse(unittest.TestCase): - """ - Tests the parsing of ControlMessages for PROTOCOLINFO responses. - """ - - def test_convert(self): - """ - Exercises functionality of the convert method both when it works and - there's an error. - """ - - # working case - control_message = stem.types.read_message(StringIO.StringIO(NO_AUTH)) - stem.connection.ProtocolInfoResponse.convert(control_message) - - # now this should be a ProtocolInfoResponse (ControlMessage subclass) - self.assertTrue(isinstance(control_message, stem.types.ControlMessage)) - self.assertTrue(isinstance(control_message, stem.connection.ProtocolInfoResponse)) - - # exercise some of the ControlMessage functionality - self.assertTrue(str(control_message).startswith("PROTOCOLINFO 1")) - self.assertEquals(NO_AUTH, control_message.raw_content()) - - # attempt to convert the wrong type - self.assertRaises(TypeError, stem.connection.ProtocolInfoResponse.convert, "hello world") - - # attempt to convert a different message type - bw_event_control_message = stem.types.read_message(StringIO.StringIO("650 BW 32326 2856\r\n")) - self.assertRaises(stem.types.ProtocolError, stem.connection.ProtocolInfoResponse.convert, bw_event_control_message) - - def test_no_auth(self): - """ - Checks a response when there's no authentication. - """ - - control_message = stem.types.read_message(StringIO.StringIO(NO_AUTH)) - stem.connection.ProtocolInfoResponse.convert(control_message) - - self.assertEquals(1, control_message.protocol_version) - self.assertEquals(stem.types.Version("0.2.1.30"), control_message.tor_version) - self.assertEquals((stem.connection.AuthMethod.NONE, ), control_message.auth_methods) - self.assertEquals((), control_message.unknown_auth_methods) - self.assertEquals(None, control_message.cookie_file) - self.assertEquals(None, control_message.socket) - - def test_password_auth(self): - """ - Checks a response with password authentication. - """ - - control_message = stem.types.read_message(StringIO.StringIO(PASSWORD_AUTH)) - stem.connection.ProtocolInfoResponse.convert(control_message) - self.assertEquals((stem.connection.AuthMethod.PASSWORD, ), control_message.auth_methods) - - def test_cookie_auth(self): - """ - Checks a response with cookie authentication and a path including escape - characters. - """ - - control_message = stem.types.read_message(StringIO.StringIO(COOKIE_AUTH)) - stem.connection.ProtocolInfoResponse.convert(control_message) - self.assertEquals((stem.connection.AuthMethod.COOKIE, ), control_message.auth_methods) - self.assertEquals("/tmp/my data\"dir//control_auth_cookie", control_message.cookie_file) - - def test_multiple_auth(self): - """ - Checks a response with multiple authentication methods. - """ - - control_message = stem.types.read_message(StringIO.StringIO(MULTIPLE_AUTH)) - stem.connection.ProtocolInfoResponse.convert(control_message) - self.assertEquals((stem.connection.AuthMethod.COOKIE, stem.connection.AuthMethod.PASSWORD), control_message.auth_methods) - self.assertEquals("/home/atagar/.tor/control_auth_cookie", control_message.cookie_file) - - def test_unknown_auth(self): - """ - Checks a response with an unrecognized authtentication method. - """ - - control_message = stem.types.read_message(StringIO.StringIO(UNKNOWN_AUTH)) - stem.connection.ProtocolInfoResponse.convert(control_message) - self.assertEquals((stem.connection.AuthMethod.UNKNOWN, stem.connection.AuthMethod.PASSWORD), control_message.auth_methods) - self.assertEquals(("MAGIC", "PIXIE_DUST"), control_message.unknown_auth_methods) - - def test_minimum_response(self): - """ - Checks a PROTOCOLINFO response that only contains the minimum amount of - information to be a valid response. - """ - - control_message = stem.types.read_message(StringIO.StringIO(MINIMUM_RESPONSE)) - stem.connection.ProtocolInfoResponse.convert(control_message) - - self.assertEquals(5, control_message.protocol_version) - self.assertEquals(None , control_message.tor_version) - self.assertEquals((), control_message.auth_methods) - self.assertEquals((), control_message.unknown_auth_methods) - self.assertEquals(None, control_message.cookie_file) - self.assertEquals(None, control_message.socket) - - def test_relative_cookie(self): - """ - Checks an authentication cookie with a relative path where expansion both - succeeds and fails. - """ - - # we need to mock both pid and cwd lookups since the general cookie - # expanion works by... - # - resolving the pid of the "tor" process - # - using that to get tor's cwd - - def call_mocking(command): - if command == stem.util.system.GET_PID_BY_NAME_PGREP % "tor": - return ["10"] - if command == stem.util.system.GET_CWD_PWDX % 10: - return ["10: /tmp/foo"] - - stem.util.system.CALL_MOCKING = call_mocking - - control_message = stem.types.read_message(StringIO.StringIO(RELATIVE_COOKIE_PATH)) - stem.connection.ProtocolInfoResponse.convert(control_message) - self.assertEquals("/tmp/foo/tor-browser_en-US/Data/control_auth_cookie", control_message.cookie_file) - - # exercise cookie expansion where both calls fail (should work, just - # leaving the path unexpanded) - - stem.util.system.CALL_MOCKING = lambda cmd: None - control_message = stem.types.read_message(StringIO.StringIO(RELATIVE_COOKIE_PATH)) - stem.connection.ProtocolInfoResponse.convert(control_message) - self.assertEquals("./tor-browser_en-US/Data/control_auth_cookie", control_message.cookie_file) -
tor-commits@lists.torproject.org