commit c41a065251d54d3fb059429361712657829002d3 Author: Damian Johnson atagar@torproject.org Date: Sun Jul 10 10:58:57 2016 -0700
Test _draw_line_details() --- nyx/panel/connection.py | 45 +++++++++++++++++++++++---------------------- test/panel/connection.py | 18 +++++++++++++++++- 2 files changed, 40 insertions(+), 23 deletions(-)
diff --git a/nyx/panel/connection.py b/nyx/panel/connection.py index c189362..33ef505 100644 --- a/nyx/panel/connection.py +++ b/nyx/panel/connection.py @@ -456,7 +456,7 @@ class ConnectionPanel(nyx.panel.DaemonPanel): x += 1 # offset from edge
self._draw_address_column(subwindow, x, y, line, attr) - self._draw_line_details(subwindow, 57, y, line, width - 57 - 20, attr) + _draw_line_details(subwindow, 57, y, line, width - 57 - 20, attr) _draw_right_column(subwindow, width - 18, y, line, current_time, attr)
def _draw_address_column(self, subwindow, x, y, line, attr): @@ -485,27 +485,6 @@ class ConnectionPanel(nyx.panel.DaemonPanel): else: subwindow.addstr(x, y, '%-21s --> %-26s' % (src, dst), *attr)
- def _draw_line_details(self, subwindow, x, y, line, width, attr): - if line.line_type == LineType.CIRCUIT_HEADER: - comp = ['Purpose: %s' % line.circuit.purpose.capitalize(), ', Circuit ID: %s' % line.circuit.id] - elif line.entry.get_type() in (Category.SOCKS, Category.HIDDEN, Category.CONTROL): - try: - port = line.connection.local_port if line.entry.get_type() == Category.HIDDEN else line.connection.remote_port - process = nyx.tracker.get_port_usage_tracker().fetch(port) - comp = ['%s (%s)' % (process.name, process.pid) if process.pid else process.name] - except nyx.tracker.UnresolvedResult: - comp = ['resolving...'] - except nyx.tracker.UnknownApplication: - comp = ['UNKNOWN'] - else: - comp = ['%-40s' % (line.fingerprint if line.fingerprint else 'UNKNOWN'), ' ' + (line.nickname if line.nickname else 'UNKNOWN')] - - for entry in comp: - if width >= x + len(entry): - x = subwindow.addstr(x, y, entry, *attr) - else: - return - def _update(self): """ Fetches the newest resolved connections. @@ -636,6 +615,28 @@ def _draw_details(subwindow, selected): subwindow.box(0, 0, subwindow.width, DETAILS_HEIGHT + 2)
+def _draw_line_details(subwindow, x, y, line, width, attr): + if line.line_type == LineType.CIRCUIT_HEADER: + comp = ['Purpose: %s' % line.circuit.purpose.capitalize(), ', Circuit ID: %s' % line.circuit.id] + elif line.entry.get_type() in (Category.SOCKS, Category.HIDDEN, Category.CONTROL): + try: + port = line.connection.local_port if line.entry.get_type() == Category.HIDDEN else line.connection.remote_port + process = nyx.tracker.get_port_usage_tracker().fetch(port) + comp = ['%s (%s)' % (process.name, process.pid) if process.pid else process.name] + except nyx.tracker.UnresolvedResult: + comp = ['resolving...'] + except nyx.tracker.UnknownApplication: + comp = ['UNKNOWN'] + else: + comp = ['%-40s' % (line.fingerprint if line.fingerprint else 'UNKNOWN'), ' ' + (line.nickname if line.nickname else 'UNKNOWN')] + + for entry in comp: + if width >= x + len(entry): + x = subwindow.addstr(x, y, entry, *attr) + else: + return + + 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] diff --git a/test/panel/connection.py b/test/panel/connection.py index d7f9d1c..ac9d404 100644 --- a/test/panel/connection.py +++ b/test/panel/connection.py @@ -96,8 +96,10 @@ class MockEntry(Entry):
class MockCircuit(object): - def __init__(self, status = 'BUILT', path = None): + def __init__(self, circ_id = 7, status = 'BUILT', purpose = 'GENERAL', path = None): + self.id = circ_id self.status = status + self.purpose = purpose
if path: self.path = path @@ -180,6 +182,20 @@ class TestConnectionPanel(unittest.TestCase): self.assertEqual(DETAILS_FOR_MULTIPLE_MATCHES, test.render(nyx.panel.connection._draw_details, line()).content)
@require_curses + @patch('nyx.tracker.get_port_usage_tracker') + def test_draw_line_details(self, port_usage_tracker_mock): + process = Mock() + process.name = 'firefox' + process.pid = 722 + + port_usage_tracker_mock().fetch.return_value = process + + self.assertEqual('1F43EE37A0670301AD9CB555D94AFEC2C89FDE86 Unnamed', test.render(nyx.panel.connection._draw_line_details, 0, 0, line(), 80, ()).content) + self.assertEqual('1F43EE37A0670301AD9CB555D94AFEC2C89FDE86', test.render(nyx.panel.connection._draw_line_details, 0, 0, line(), 45, ()).content) + self.assertEqual('Purpose: General, Circuit ID: 7', test.render(nyx.panel.connection._draw_line_details, 0, 0, line(line_type = LineType.CIRCUIT_HEADER), 45, ()).content) + self.assertEqual('firefox (722)', test.render(nyx.panel.connection._draw_line_details, 0, 0, line(entry = MockEntry(entry_type = Category.CONTROL)), 80, ()).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)