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

[tor-commits] [nyx/master] Test _draw_right_column()



commit 681e5f8e4c1d514d656fada12ce47af2ac72fd60
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sun Jul 10 10:36:35 2016 -0700

    Test _draw_right_column()
---
 nyx/panel/connection.py  | 43 ++++++++++++++++---------------
 test/panel/connection.py | 67 +++++++++++++++++++++++++++++++++---------------
 2 files changed, 68 insertions(+), 42 deletions(-)

diff --git a/nyx/panel/connection.py b/nyx/panel/connection.py
index b5a3297..c189362 100644
--- a/nyx/panel/connection.py
+++ b/nyx/panel/connection.py
@@ -457,7 +457,7 @@ class ConnectionPanel(nyx.panel.DaemonPanel):
 
     self._draw_address_column(subwindow, x, y, line, attr)
     self._draw_line_details(subwindow, 57, y, line, width - 57 - 20, attr)
-    self._draw_right_column(subwindow, width - 18, y, line, current_time, attr)
+    _draw_right_column(subwindow, width - 18, y, line, current_time, attr)
 
   def _draw_address_column(self, subwindow, x, y, line, attr):
     src = tor_controller().get_info('address', line.connection.local_address)
@@ -506,26 +506,6 @@ class ConnectionPanel(nyx.panel.DaemonPanel):
       else:
         return
 
-  def _draw_right_column(self, subwindow, x, y, line, current_time, attr):
-    if line.line_type == LineType.CIRCUIT:
-      circ_path = [fp for fp, _ in line.circuit.path]
-      circ_index = circ_path.index(line.fingerprint)
-
-      if circ_index == len(circ_path) - 1:
-        placement_type = 'Exit' if line.circuit.status == 'BUILT' else 'Extending'
-      elif circ_index == 0:
-        placement_type = 'Guard'
-      else:
-        placement_type = 'Middle'
-
-      subwindow.addstr(x + 4, y, '%i / %s' % (circ_index + 1, placement_type), *attr)
-    else:
-      x = subwindow.addstr(x, y, '+' if line.connection.is_legacy else ' ', *attr)
-      x = subwindow.addstr(x, y, '%5s' % str_tools.time_label(current_time - line.connection.start_time, 1), *attr)
-      x = subwindow.addstr(x, y, ' (', *attr)
-      x = subwindow.addstr(x, y, line.entry.get_type().upper(), BOLD, *attr)
-      x = subwindow.addstr(x, y, ')', *attr)
-
   def _update(self):
     """
     Fetches the newest resolved connections.
@@ -654,3 +634,24 @@ def _draw_details(subwindow, selected):
           break
 
   subwindow.box(0, 0, subwindow.width, DETAILS_HEIGHT + 2)
+
+
+def _draw_right_column(subwindow, x, y, line, current_time, attr):
+  if line.line_type == LineType.CIRCUIT:
+    circ_path = [fp for fp, _ in line.circuit.path]
+    circ_index = circ_path.index(line.fingerprint)
+
+    if circ_index == len(circ_path) - 1:
+      placement_type = 'Exit' if line.circuit.status == 'BUILT' else 'Extending'
+    elif circ_index == 0:
+      placement_type = 'Guard'
+    else:
+      placement_type = 'Middle'
+
+    subwindow.addstr(x + 4, y, '%i / %s' % (circ_index + 1, placement_type), *attr)
+  else:
+    x = subwindow.addstr(x, y, '+' if line.connection.is_legacy else ' ', *attr)
+    x = subwindow.addstr(x, y, '%5s' % str_tools.time_label(current_time - line.connection.start_time, 1), *attr)
+    x = subwindow.addstr(x, y, ' (', *attr)
+    x = subwindow.addstr(x, y, line.entry.get_type().upper(), BOLD, *attr)
+    x = subwindow.addstr(x, y, ')', *attr)
diff --git a/test/panel/connection.py b/test/panel/connection.py
index 31f00f3..d7f9d1c 100644
--- a/test/panel/connection.py
+++ b/test/panel/connection.py
@@ -10,11 +10,14 @@ import stem.version
 import nyx.panel.connection
 import test
 
-from stem.util import connection
+from nyx.tracker import Connection
 from nyx.panel.connection import Category, LineType, Line, Entry
 from test import require_curses
 from mock import Mock, patch
 
+TIMESTAMP = 1468170303.7
+CONNECTION = Connection(TIMESTAMP, False, '127.0.0.1', 3531, '75.119.206.243', 22, 'tcp', False)
+
 DETAILS_BUILDING_CIRCUIT = """
 +------------------------------------------------------------------------------+
 | Building Circuit...                                                          |
@@ -92,44 +95,50 @@ class MockEntry(Entry):
     return self._is_private
 
 
+class MockCircuit(object):
+  def __init__(self, status = 'BUILT', path = None):
+    self.status = status
+
+    if path:
+      self.path = path
+    else:
+      self.path = [
+        ('1F43EE37A0670301AD9CB555D94AFEC2C89FDE86', 'Unnamed'),
+        ('B6D83EC2D9E18B0A7A33428F8CFA9C536769E209', 'moria1'),
+        ('E0BD57A11F00041A9789577C53A1B784473669E4', 'caerSidi'),
+      ]
+
+
+def line(entry = MockEntry(), line_type = LineType.CONNECTION, connection = CONNECTION, circ = MockCircuit(), fingerprint = '1F43EE37A0670301AD9CB555D94AFEC2C89FDE86', nickname = 'Unnamed', locale = 'de'):
+  return Line(entry, line_type, connection, circ, fingerprint, nickname, locale)
+
+
 class TestConnectionPanel(unittest.TestCase):
   @require_curses
   def test_draw_title(self):
     self.assertEqual('Connection Details:', test.render(nyx.panel.connection._draw_title, [], True).content)
     self.assertEqual('Connections:', test.render(nyx.panel.connection._draw_title, [], False).content)
 
-    entries = [
-      MockEntry(entry_type = Category.INBOUND),
-      MockEntry(entry_type = Category.INBOUND),
-      MockEntry(entry_type = Category.OUTBOUND),
-      MockEntry(entry_type = Category.INBOUND),
-      MockEntry(entry_type = Category.CONTROL),
-    ]
-
+    entries = [MockEntry(entry_type = category) for category in (Category.INBOUND, Category.INBOUND, Category.OUTBOUND, Category.INBOUND, Category.CONTROL)]
     self.assertEqual('Connections (3 inbound, 1 outbound, 1 control):', test.render(nyx.panel.connection._draw_title, entries, False).content)
 
   @require_curses
   def test_draw_details_incomplete_circuit(self):
-    circ = Mock()
-    circ.status = 'EXTENDING'
-
-    selected = Line(MockEntry(), LineType.CIRCUIT_HEADER, None, circ, None, None, None)
+    selected = line(line_type = LineType.CIRCUIT_HEADER, circ = MockCircuit(status = 'EXTENDING'))
     self.assertEqual(DETAILS_BUILDING_CIRCUIT, test.render(nyx.panel.connection._draw_details, selected).content)
 
   @require_curses
   @patch('nyx.tracker.get_consensus_tracker')
   def test_draw_details_no_consensus_data(self, consensus_tracker_mock):
     consensus_tracker_mock().get_relay_fingerprints.return_value = None
-
-    selected = Line(MockEntry(), LineType.CONNECTION, connection.Connection('127.0.0.1', 3531, '75.119.206.243', 22, 'tcp', False), None, None, None, 'de')
-    self.assertEqual(DETAILS_NO_CONSENSUS_DATA, test.render(nyx.panel.connection._draw_details, selected).content)
+    self.assertEqual(DETAILS_NO_CONSENSUS_DATA, test.render(nyx.panel.connection._draw_details, line()).content)
 
   @require_curses
   @patch('nyx.tracker.get_consensus_tracker')
   def test_draw_details_when_private(self, consensus_tracker_mock):
     consensus_tracker_mock().get_relay_fingerprints.return_value = None
 
-    selected = Line(MockEntry(is_private = True), LineType.CONNECTION, connection.Connection('127.0.0.1', 3531, '75.119.206.243', 22, 'tcp', False), None, None, None, 'de')
+    selected = line(entry = MockEntry(is_private = True))
     self.assertEqual(DETAILS_WHEN_PRIVATE, test.render(nyx.panel.connection._draw_details, selected).content)
 
   @require_curses
@@ -157,8 +166,7 @@ class TestConnectionPanel(unittest.TestCase):
       22: 'B6D83EC2D9E18B0A7A33428F8CFA9C536769E209'
     }
 
-    selected = Line(MockEntry(), LineType.CONNECTION, connection.Connection('127.0.0.1', 3531, '75.119.206.243', 22, 'tcp', False), None, None, None, 'de')
-    self.assertEqual(DETAILS_FOR_RELAY, test.render(nyx.panel.connection._draw_details, selected).content)
+    self.assertEqual(DETAILS_FOR_RELAY, test.render(nyx.panel.connection._draw_details, line()).content)
 
   @require_curses
   @patch('nyx.tracker.get_consensus_tracker')
@@ -169,5 +177,22 @@ class TestConnectionPanel(unittest.TestCase):
       443: 'E0BD57A11F00041A9789577C53A1B784473669E4',
     }
 
-    selected = Line(MockEntry(), LineType.CONNECTION, connection.Connection('127.0.0.1', 3531, '75.119.206.243', 22, 'tcp', False), None, None, None, 'de')
-    self.assertEqual(DETAILS_FOR_MULTIPLE_MATCHES, test.render(nyx.panel.connection._draw_details, selected).content)
+    self.assertEqual(DETAILS_FOR_MULTIPLE_MATCHES, test.render(nyx.panel.connection._draw_details, line()).content)
+
+  @require_curses
+  def test_draw_right_column(self):
+    self.assertEqual('  1.0m (INBOUND)', test.render(nyx.panel.connection._draw_right_column, 0, 0, line(), TIMESTAMP + 62, ()).content)
+
+    legacy_connection = Connection(TIMESTAMP, True, '127.0.0.1', 3531, '75.119.206.243', 22, 'tcp', False)
+    test_line = line(entry = MockEntry(entry_type = Category.CONTROL), connection = legacy_connection)
+    self.assertEqual('+ 1.1m (CONTROL)', test.render(nyx.panel.connection._draw_right_column, 0, 0, test_line, TIMESTAMP + 68, ()).content)
+
+    test_data = {
+      '1F43EE37A0670301AD9CB555D94AFEC2C89FDE86': '    1 / Guard',
+      'B6D83EC2D9E18B0A7A33428F8CFA9C536769E209': '    2 / Middle',
+      'E0BD57A11F00041A9789577C53A1B784473669E4': '    3 / Exit',
+    }
+
+    for fp, expected in test_data.items():
+      test_line = line(line_type = LineType.CIRCUIT, fingerprint = fp)
+      self.assertEqual(expected, test.render(nyx.panel.connection._draw_right_column, 0, 0, test_line, TIMESTAMP + 62, ()).content)



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