[tor-commits] [stem/master] Unit test for /info

atagar at torproject.org atagar at torproject.org
Tue May 6 01:21:13 UTC 2014


commit ee2f739ab1b5d14661193543ac58f17e471d2ae3
Author: Damian Johnson <atagar at torproject.org>
Date:   Mon May 5 13:52:42 2014 -0700

    Unit test for /info
---
 stem/interpretor/commands.py      |   31 +++++++++++-------------------
 test/unit/interpretor/commands.py |   38 +++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 20 deletions(-)

diff --git a/stem/interpretor/commands.py b/stem/interpretor/commands.py
index faf9509..b2ffdb9 100644
--- a/stem/interpretor/commands.py
+++ b/stem/interpretor/commands.py
@@ -150,26 +150,18 @@ class ControlInterpretor(object):
     else:
       exit_policy_label = 'Unknown'
 
-    output = '%s (%s)\n' % (ns_desc.nickname, fingerprint)
-
-    output += format('address: ', *BOLD_OUTPUT)
-    output += '%s:%s%s\n' % (ns_desc.address, ns_desc.or_port, locale_label)
-
-    output += format('published: ', *BOLD_OUTPUT)
-    output += ns_desc.published.strftime('%H:%M:%S %d/%m/%Y') + '\n'
+    lines = [
+      '%s (%s)' % (ns_desc.nickname, fingerprint),
+      format('address: ', *BOLD_OUTPUT) + '%s:%s%s' % (ns_desc.address, ns_desc.or_port, locale_label),
+      format('published: ', *BOLD_OUTPUT) + ns_desc.published.strftime('%H:%M:%S %d/%m/%Y'),
+    ]
 
     if server_desc:
-      output += format('os: ', *BOLD_OUTPUT)
-      output += server_desc.platform.decode('utf-8', 'replace') + '\n'
-
-      output += format('version: ', *BOLD_OUTPUT)
-      output += str(server_desc.tor_version) + '\n'
-
-    output += format('flags: ', *BOLD_OUTPUT)
-    output += ', '.join(ns_desc.flags) + '\n'
+      lines.append(format('os: ', *BOLD_OUTPUT) + server_desc.platform.decode('utf-8', 'replace'))
+      lines.append(format('version: ', *BOLD_OUTPUT) + str(server_desc.tor_version))
 
-    output += format('exit policy: ', *BOLD_OUTPUT)
-    output += exit_policy_label + '\n'
+    lines.append(format('flags: ', *BOLD_OUTPUT) + ', '.join(ns_desc.flags))
+    lines.append(format('exit policy: ', *BOLD_OUTPUT) + exit_policy_label)
 
     if server_desc:
       contact = server_desc.contact
@@ -182,10 +174,9 @@ class ControlInterpretor(object):
       for alias in (' dot ', ' DOT '):
         contact = contact.replace(alias, '.')
 
-      output += format('contact: ', *BOLD_OUTPUT)
-      output += contact + '\n'
+      lines.append(format('contact: ', *BOLD_OUTPUT) + contact)
 
-    return output.strip()
+    return '\n'.join(lines)
 
   def run_command(self, command):
     """
diff --git a/test/unit/interpretor/commands.py b/test/unit/interpretor/commands.py
index 1517e3a..a99f4f2 100644
--- a/test/unit/interpretor/commands.py
+++ b/test/unit/interpretor/commands.py
@@ -1,7 +1,9 @@
+import datetime
 import unittest
 
 import stem
 import stem.response
+import stem.version
 
 from stem.interpretor.commands import ControlInterpretor, _get_fingerprint
 
@@ -20,6 +22,17 @@ EXPECTED_EVENTS_RESPONSE = """\
 \x1b[34mDEBUG connection_edge_process_relay_cell(): Got an extended cell! Yay.\x1b[0m
 """
 
+EXPECTED_INFO_RESPONSE = """\
+moria1 (9695DFC35FFEB861329B9F1AB04C46397020CE31)
+\x1b[34;1maddress: \x1b[0m128.31.0.34:9101 (us)
+\x1b[34;1mpublished: \x1b[0m05:52:05 05/05/2014
+\x1b[34;1mos: \x1b[0mLinux
+\x1b[34;1mversion: \x1b[0m0.2.5.3-alpha-dev
+\x1b[34;1mflags: \x1b[0mAuthority, Fast, Guard, HSDir, Named, Running, Stable, V2Dir, Valid
+\x1b[34;1mexit policy: \x1b[0mreject 1-65535
+\x1b[34;1mcontact: \x1b[0m1024D/28988BF5 arma mit edu
+"""
+
 FINGERPRINT = '9695DFC35FFEB861329B9F1AB04C46397020CE31'
 
 
@@ -105,3 +118,28 @@ class TestInterpretorCommands(unittest.TestCase):
       interpretor.register_event(event)
 
     self.assertEqual(EXPECTED_EVENTS_RESPONSE, interpretor.run_command('/events'))
+
+  def test_info(self):
+    controller, server_desc, ns_desc = Mock(), Mock(), Mock()
+
+    controller.get_microdescriptor.return_value = None
+    controller.get_server_descriptor.return_value = server_desc
+    controller.get_network_status.return_value = ns_desc
+
+    controller.get_info.side_effect = lambda arg, _: {
+      'ip-to-country/128.31.0.34': 'us',
+    }[arg]
+
+    ns_desc.address = '128.31.0.34'
+    ns_desc.or_port = 9101
+    ns_desc.published = datetime.datetime(2014, 5, 5, 5, 52, 5)
+    ns_desc.nickname = 'moria1'
+    ns_desc.flags = ['Authority', 'Fast', 'Guard', 'HSDir', 'Named', 'Running', 'Stable', 'V2Dir', 'Valid']
+
+    server_desc.exit_policy.summary.return_value = 'reject 1-65535'
+    server_desc.platform = 'Linux'
+    server_desc.tor_version = stem.version.Version('0.2.5.3-alpha-dev')
+    server_desc.contact = '1024D/28988BF5 arma mit edu'
+
+    interpretor = ControlInterpretor(controller)
+    self.assertEqual(EXPECTED_INFO_RESPONSE, interpretor.run_command('/info ' + FINGERPRINT))





More information about the tor-commits mailing list