commit c58177c50fcdba85e5eac7a929dbf6d87c666852 Author: Damian Johnson atagar@torproject.org Date: Mon Jan 30 00:34:38 2012 -0800
Previous socket use could break get_protocolinfo
If prior use of the control socket had caused a disconnect and already consumed the 'Authentication required.' response then future calls of get_protocolinfo would break. --- stem/connection.py | 17 ++++++++++++----- test/integ/connection/protocolinfo.py | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-)
diff --git a/stem/connection.py b/stem/connection.py index 5c1fc55..392ad9b 100644 --- a/stem/connection.py +++ b/stem/connection.py @@ -627,16 +627,23 @@ def get_protocolinfo(control_socket): socket """
- control_socket.send("PROTOCOLINFO 1") - protocolinfo_response = control_socket.recv() + try: + control_socket.send("PROTOCOLINFO 1") + protocolinfo_response = control_socket.recv() + except: + protocolinfo_response = None
# Tor hangs up on sockets after receiving a PROTOCOLINFO query if it isn't # next followed by authentication. Transparently reconnect if that happens.
- if str(protocolinfo_response) == "Authentication required.": + if not protocolinfo_response or str(protocolinfo_response) == "Authentication required.": control_socket.connect() - control_socket.send("PROTOCOLINFO 1") - protocolinfo_response = control_socket.recv() + + try: + control_socket.send("PROTOCOLINFO 1") + protocolinfo_response = control_socket.recv() + except stem.socket.SocketClosed, exc: + raise stem.socket.SocketError(exc)
ProtocolInfoResponse.convert(protocolinfo_response)
diff --git a/test/integ/connection/protocolinfo.py b/test/integ/connection/protocolinfo.py index e0e8805..a0214a2 100644 --- a/test/integ/connection/protocolinfo.py +++ b/test/integ/connection/protocolinfo.py @@ -91,6 +91,23 @@ class TestProtocolInfo(unittest.TestCase): protocolinfo_response = stem.connection.get_protocolinfo(control_socket) self.assert_matches_test_config(protocolinfo_response)
+ def test_pre_disconnected_query(self): + """ + Tests making a PROTOCOLINFO query when previous use of the socket had + already disconnected it. + """ + + with test.runner.get_runner().get_tor_socket(False) as control_socket: + # makes a couple protocolinfo queries outside of get_protocolinfo first + control_socket.send("PROTOCOLINFO 1") + control_socket.recv() + + control_socket.send("PROTOCOLINFO 1") + control_socket.recv() + + protocolinfo_response = stem.connection.get_protocolinfo(control_socket) + self.assert_matches_test_config(protocolinfo_response) + def assert_matches_test_config(self, protocolinfo_response): """ Makes assertions that the protocolinfo response's attributes match those of
tor-commits@lists.torproject.org