[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Unit tests for interpretor commands



commit 1fab359f84b3c462f0b0fea1baeb4230be39ccab
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Mon May 5 17:47:12 2014 -0700

    Unit tests for interpretor commands
    
    Tests for issuing commands to the interpretor prompt.
---
 stem/interpretor/commands.py      |    8 +++++
 test/unit/interpretor/commands.py |   71 +++++++++++++++++++++++++++++++++++++
 2 files changed, 79 insertions(+)

diff --git a/stem/interpretor/commands.py b/stem/interpretor/commands.py
index b2ffdb9..8e94f93 100644
--- a/stem/interpretor/commands.py
+++ b/stem/interpretor/commands.py
@@ -236,6 +236,14 @@ class ControlInterpretor(object):
           output = format('\n'.join(response.values()), *STANDARD_OUTPUT)
         except stem.ControllerError as exc:
           output = format(str(exc), *ERROR_OUTPUT)
+      elif cmd == 'GETCONF':
+        try:
+          response = self._controller.get_conf_map(arg.split())
+
+          for arg in response:
+            output += format(arg, *BOLD_OUTPUT) + format(' => ' + ', '.join(response[arg]), *STANDARD_OUTPUT) + '\n'
+        except stem.ControllerError as exc:
+          output = format(str(exc), *ERROR_OUTPUT)
       elif cmd in ('SETCONF', 'RESETCONF'):
         # arguments can either be '<param>', '<param>=<value>', or
         # '<param>="<value>"' entries
diff --git a/test/unit/interpretor/commands.py b/test/unit/interpretor/commands.py
index a99f4f2..9ed7a92 100644
--- a/test/unit/interpretor/commands.py
+++ b/test/unit/interpretor/commands.py
@@ -1,3 +1,4 @@
+import collections
 import datetime
 import unittest
 
@@ -33,6 +34,11 @@ moria1 (9695DFC35FFEB861329B9F1AB04C46397020CE31)
 \x1b[34;1mcontact: \x1b[0m1024D/28988BF5 arma mit edu
 """
 
+EXPECTED_GETCONF_RESPONSE = """\
+\x1b[34;1mlog\x1b[0m\x1b[34m => notice stdout\x1b[0m
+\x1b[34;1maddress\x1b[0m\x1b[34m => \x1b[0m
+"""
+
 FINGERPRINT = '9695DFC35FFEB861329B9F1AB04C46397020CE31'
 
 
@@ -86,9 +92,17 @@ class TestInterpretorCommands(unittest.TestCase):
   def test_get_fingerprint_for_unrecognized_inputs(self):
     self.assertRaises(ValueError, _get_fingerprint, 'blarg!', Mock())
 
+  def test_when_disconnected(self):
+    controller = Mock()
+    controller.is_alive.return_value = False
+
+    interpretor = ControlInterpretor(controller)
+    self.assertRaises(stem.SocketClosed, interpretor.run_command, '/help')
+
   def test_quit(self):
     interpretor = ControlInterpretor(CONTROLLER)
     self.assertRaises(stem.SocketClosed, interpretor.run_command, '/quit')
+    self.assertRaises(stem.SocketClosed, interpretor.run_command, 'QUIT')
 
   def test_help(self):
     interpretor = ControlInterpretor(CONTROLLER)
@@ -143,3 +157,60 @@ class TestInterpretorCommands(unittest.TestCase):
 
     interpretor = ControlInterpretor(controller)
     self.assertEqual(EXPECTED_INFO_RESPONSE, interpretor.run_command('/info ' + FINGERPRINT))
+
+  def test_unrecognized_interpretor_command(self):
+    interpretor = ControlInterpretor(CONTROLLER)
+
+    expected = "\x1b[1;31m'/unrecognized' isn't a recognized command\x1b[0m\n"
+    self.assertEqual(expected, interpretor.run_command('/unrecognized'))
+
+  def test_getinfo(self):
+    controller, getinfo = Mock(), collections.OrderedDict()
+    controller.get_info.return_value = getinfo
+
+    interpretor = ControlInterpretor(controller)
+
+    getinfo['version'] = '0.2.5.1-alpha-dev (git-245ecfff36c0cecc)'
+    self.assertEqual('\x1b[34m0.2.5.1-alpha-dev (git-245ecfff36c0cecc)\x1b[0m', interpretor.run_command('GETINFO version'))
+    controller.get_info.assert_called_with(['version'])
+
+    getinfo['process/user'] = 'atagar'
+    self.assertEqual('\x1b[34m0.2.5.1-alpha-dev (git-245ecfff36c0cecc)\natagar\x1b[0m', interpretor.run_command('getinfo version process/user'))
+    controller.get_info.assert_called_with(['version', 'process/user'])
+
+    controller.get_info.side_effect = stem.ControllerError('kaboom!')
+    self.assertEqual('\x1b[1;31mkaboom!\x1b[0m', interpretor.run_command('getinfo process/user'))
+
+  def test_getconf(self):
+    controller, getconf = Mock(), collections.OrderedDict()
+    controller.get_conf_map.return_value = getconf
+
+    interpretor = ControlInterpretor(controller)
+
+    getconf['log'] = ['notice stdout']
+    getconf['address'] = ['']
+
+    self.assertEqual(EXPECTED_GETCONF_RESPONSE, interpretor.run_command('GETCONF log address'))
+    controller.get_conf_map.assert_called_with(['log', 'address'])
+
+  def test_setconf(self):
+    controller = Mock()
+    interpretor = ControlInterpretor(controller)
+
+    self.assertEqual('', interpretor.run_command('SETCONF ControlPort=9051'))
+    controller.set_options.assert_called_with([('ControlPort', '9051')], False)
+
+  def test_setevents(self):
+    controller = Mock()
+    interpretor = ControlInterpretor(controller)
+
+    self.assertEqual('\x1b[34mListing for BW events\n\x1b[0m', interpretor.run_command('SETEVENTS BW'))
+    controller.add_event_listener.assert_called_with(interpretor.register_event, 'BW')
+
+  def test_raw_commands(self):
+    controller = Mock()
+    controller.msg.return_value = 'response'
+    interpretor = ControlInterpretor(controller)
+
+    self.assertEqual('\x1b[34mresponse\x1b[0m', interpretor.run_command('NEW_COMMAND spiffyness'))
+    controller.msg.assert_called_with('NEW_COMMAND spiffyness')



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits