[tor-commits] [stem/master] Test mistakenly connecting to SocksPort

atagar at torproject.org atagar at torproject.org
Wed Aug 9 19:24:47 UTC 2017


commit 52d7ad697a257f4559133d281c7394a054e3733c
Author: Damian Johnson <atagar at torproject.org>
Date:   Tue Aug 8 10:56:04 2017 -0700

    Test mistakenly connecting to SocksPort
    
    While setting up a relay I accidently had nyx connect to the ORPort rather than
    the SocksPort. This caused nyx to hang.
    
    Clearly that could be better. Connecting to the wrong port will be a pritty
    common mistake. I hoped to repro this with an integ test connecting to the
    SocksPort but no luck - this doesn't hang. Oh well. More investigation needed
    to get a repro, but at least we got a nice test out of it.
---
 test/integ/connection/connect.py | 52 ++++++++++++++++++++++++++++------------
 1 file changed, 37 insertions(+), 15 deletions(-)

diff --git a/test/integ/connection/connect.py b/test/integ/connection/connect.py
index fd087423..f4df233f 100644
--- a/test/integ/connection/connect.py
+++ b/test/integ/connection/connect.py
@@ -2,30 +2,28 @@
 Integration tests for the connect_* convenience functions.
 """
 
-import sys
 import unittest
 
+import stem.connection
+import test.require
+import test.runner
+
 try:
   from StringIO import StringIO
 except ImportError:
   from io import StringIO
 
-import stem.connection
-import test.require
-import test.runner
+try:
+  # added in python 3.3
+  from unittest.mock import patch
+except ImportError:
+  from mock import patch
 
 
 class TestConnect(unittest.TestCase):
-  def setUp(self):
-    # prevents the function from printing to the real stdout
-    self.original_stdout = sys.stdout
-    sys.stdout = StringIO()
-
-  def tearDown(self):
-    sys.stdout = self.original_stdout
-
   @test.require.controller
-  def test_connect(self):
+  @patch('sys.stdout', new_callable = StringIO)
+  def test_connect(self, stdout_mock):
     """
     Basic sanity checks for the connect function.
     """
@@ -40,9 +38,11 @@ class TestConnect(unittest.TestCase):
       controller = None)
 
     test.runner.exercise_controller(self, control_socket)
+    self.assertEqual('', stdout_mock.getvalue())
 
   @test.require.controller
-  def test_connect_port(self):
+  @patch('sys.stdout', new_callable = StringIO)
+  def test_connect_port(self, stdout_mock):
     """
     Basic sanity checks for the connect_port function.
     """
@@ -58,11 +58,13 @@ class TestConnect(unittest.TestCase):
     if test.runner.Torrc.PORT in runner.get_options():
       test.runner.exercise_controller(self, control_socket)
       control_socket.close()
+      self.assertEqual('', stdout_mock.getvalue())
     else:
       self.assertEqual(control_socket, None)
 
   @test.require.controller
-  def test_connect_socket_file(self):
+  @patch('sys.stdout', new_callable = StringIO)
+  def test_connect_socket_file(self, stdout_mock):
     """
     Basic sanity checks for the connect_socket_file function.
     """
@@ -78,5 +80,25 @@ class TestConnect(unittest.TestCase):
     if test.runner.Torrc.SOCKET in runner.get_options():
       test.runner.exercise_controller(self, control_socket)
       control_socket.close()
+      self.assertEqual('', stdout_mock.getvalue())
     else:
       self.assertEqual(control_socket, None)
+
+  @test.require.controller
+  @patch('sys.stdout', new_callable = StringIO)
+  def test_connect_to_socks_port(self, stdout_mock):
+    """
+    Common user gotcha is connecting to the SocksPort or ORPort rather than the
+    ControlPort. Testing that connecting to the SocksPort errors in a
+    reasonable way.
+    """
+
+    runner = test.runner.get_runner()
+
+    control_socket = stem.connection.connect_port(
+      port = test.runner.SOCKS_PORT,
+      chroot_path = runner.get_chroot(),
+      controller = None)
+
+    self.assertEqual(None, control_socket)
+    self.assertEqual('Please check in your torrc that 1112 is the ControlPort. Maybe you\nconfigured it to be the ORPort or SocksPort instead?\n', stdout_mock.getvalue())



More information about the tor-commits mailing list