commit 0e5cf58a99fe0adec83e06e918cf896019411480 Author: Damian Johnson atagar@torproject.org Date: Sat Apr 19 09:12:16 2014 -0700
Unit tests for interpretor argument parsing
Few simple tests for our argument handling, adapted from arm. --- stem/interpretor/arguments.py | 2 +- test/settings.cfg | 1 + test/unit/interpretor/__init__.py | 7 +++++ test/unit/interpretor/arguments.py | 57 ++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-)
diff --git a/stem/interpretor/arguments.py b/stem/interpretor/arguments.py index c2971b0..733da27 100644 --- a/stem/interpretor/arguments.py +++ b/stem/interpretor/arguments.py @@ -2,7 +2,7 @@ # See LICENSE for licensing information
""" -Commandline argument parsing for arm. +Commandline argument parsing for our interpretor prompt. """
import collections diff --git a/test/settings.cfg b/test/settings.cfg index f0eb758..684d40a 100644 --- a/test/settings.cfg +++ b/test/settings.cfg @@ -183,6 +183,7 @@ test.unit_tests |test.unit.connection.authentication.TestAuthenticate |test.unit.connection.connect.TestConnect |test.unit.control.controller.TestControl +|test.unit.interpretor.arguments.TestArgumentParsing |test.unit.doctest.TestDocumentation
test.integ_tests diff --git a/test/unit/interpretor/__init__.py b/test/unit/interpretor/__init__.py new file mode 100644 index 0000000..e2ad715 --- /dev/null +++ b/test/unit/interpretor/__init__.py @@ -0,0 +1,7 @@ +""" +Unit tests for the stem's interpretor prompt. +""" + +__all__ = [ + "arguments", +] diff --git a/test/unit/interpretor/arguments.py b/test/unit/interpretor/arguments.py new file mode 100644 index 0000000..ab835c4 --- /dev/null +++ b/test/unit/interpretor/arguments.py @@ -0,0 +1,57 @@ +import unittest + +from stem.interpretor.arguments import DEFAULT_ARGS, parse, get_help + + +class TestArgumentParsing(unittest.TestCase): + def test_that_we_get_default_values(self): + args = parse([]) + + for attr in DEFAULT_ARGS: + self.assertEqual(DEFAULT_ARGS[attr], getattr(args, attr)) + + def test_that_we_load_arguments(self): + args = parse(['--interface', '10.0.0.25:80']) + self.assertEqual('10.0.0.25', args.control_address) + self.assertEqual(80, args.control_port) + + args = parse(['--interface', '80']) + self.assertEqual(DEFAULT_ARGS['control_address'], args.control_address) + self.assertEqual(80, args.control_port) + + args = parse(['--socket', '/tmp/my_socket']) + self.assertEqual('/tmp/my_socket', args.control_socket) + + args = parse(['--help']) + self.assertEqual(True, args.print_help) + + def test_examples(self): + args = parse(['-i', '1643']) + self.assertEqual(1643, args.control_port) + + args = parse(['-s', '~/.tor/socket']) + self.assertEqual('~/.tor/socket', args.control_socket) + + def test_that_we_reject_unrecognized_arguments(self): + self.assertRaises(ValueError, parse, ['--blarg', 'stuff']) + + def test_that_we_reject_invalid_interfaces(self): + invalid_inputs = ( + '', + ' ', + 'blarg', + '127.0.0.1', + '127.0.0.1:', + ':80', + '400.0.0.1:80', + '127.0.0.1:-5', + '127.0.0.1:500000', + ) + + for invalid_input in invalid_inputs: + self.assertRaises(ValueError, parse, ['--interface', invalid_input]) + + def test_get_help(self): + help_text = get_help() + self.assertTrue('Interactive interpretor for Tor.' in help_text) + self.assertTrue('change control interface from 127.0.0.1:9051' in help_text)
tor-commits@lists.torproject.org