[tor-commits] [stem/master] Make the GETINFO parser raise InvalidArguments instead of ProtocolError when appropriate

atagar at torproject.org atagar at torproject.org
Wed Jul 4 21:34:20 UTC 2012


commit 42ae08cfdda0ea2f90bccb2b3621b182ddf99e0a
Author: Ravi Chandra Padmala <neenaoffline at gmail.com>
Date:   Wed Jun 13 07:38:13 2012 +0530

    Make the GETINFO parser raise InvalidArguments instead of ProtocolError when appropriate
---
 stem/response/getinfo.py      |   11 ++++++++++-
 test/unit/response/getinfo.py |   17 +++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletions(-)

diff --git a/stem/response/getinfo.py b/stem/response/getinfo.py
index 9b1fe75..3fac253 100644
--- a/stem/response/getinfo.py
+++ b/stem/response/getinfo.py
@@ -25,7 +25,16 @@ class GetInfoResponse(stem.response.ControlMessage):
     remaining_lines = list(self)
     
     if not self.is_ok() or not remaining_lines.pop() == "OK":
-      raise stem.socket.ProtocolError("GETINFO response didn't have an OK status:\n%s" % self)
+      unrecognized_keywords = []
+      for code, _, line in self.content():
+        if code == '552' and line.startswith("Unrecognized key \"") and line.endswith("\""):
+          unrecognized_keywords.append(line[18:-1])
+
+      if unrecognized_keywords:
+        raise stem.socket.InvalidArguments("GETCONF request contained unrecognized keywords: %s\n" \
+            % ', '.join(unrecognized_keywords), unrecognized_keywords)
+      else:
+        raise stem.socket.ProtocolError("GETINFO response didn't have an OK status:\n%s" % self)
     
     while remaining_lines:
       try:
diff --git a/test/unit/response/getinfo.py b/test/unit/response/getinfo.py
index 5e82404..6d21faf 100644
--- a/test/unit/response/getinfo.py
+++ b/test/unit/response/getinfo.py
@@ -38,6 +38,10 @@ NON_KEY_VALUE_ENTRY = """\
 250-address 67.137.76.214
 250 OK"""
 
+UNRECOGNIZED_KEY_ENTRY = """\
+552 Unrecognized key "blackhole"
+"""
+
 MISSING_MULTILINE_NEWLINE = """\
 250+config-text=ControlPort 9051
 DataDirectory /home/atagar/.tor
@@ -109,6 +113,19 @@ class TestGetInfoResponse(unittest.TestCase):
     control_message = mocking.get_message(NON_KEY_VALUE_ENTRY)
     self.assertRaises(stem.socket.ProtocolError, stem.response.convert, "GETINFO", control_message)
   
+  def test_unrecognized_key_response(self):
+    """
+    Parses a GETCONF reply that contains an error code with an unrecognized key.
+    """
+    
+    control_message = mocking.get_message(UNRECOGNIZED_KEY_ENTRY)
+    self.assertRaises(stem.socket.InvalidArguments, stem.response.convert, "GETINFO", control_message)
+    
+    try:
+      stem.response.convert("GETINFO", control_message)
+    except stem.socket.InvalidArguments, exc:
+      self.assertEqual(exc.arguments, ["blackhole"])
+  
   def test_invalid_multiline_content(self):
     """
     Parses a malformed GETINFO reply with a multi-line entry missing a newline





More information about the tor-commits mailing list