def test_draw_status(self, nyx_controller_mock): nyx_controller_mock().get_page.return_value = 1 nyx_controller_mock().get_page_count.return_value = 4 self.assertEqual('page 2 / 4 - m: menu, p: pause, h: page help, q: quit', test.render(nyx.panel.header._draw_status, 0, 0, False, None).content) self.assertEqual('Paused', test.render(nyx.panel.header._draw_status, 0, 0, True, None).content) self.assertEqual('pepperjack is wonderful!', test.render(nyx.panel.header._draw_status, 0, 0, False, 'pepperjack is wonderful!').content)
def test_draw_title(self): rendered = test.render(nyx.panel.log._draw_title, ['NOTICE', 'WARN', 'ERR'], LogFilters()) self.assertEqual('Events (NOTICE-ERR):', rendered.content) rendered = test.render(nyx.panel.log._draw_title, ['NYX_NOTICE', 'NYX_WARNING', 'NYX_ERROR', 'NOTICE', 'WARN', 'ERR'], LogFilters()) self.assertEqual('Events (TOR/NYX NOTICE-ERR):', rendered.content) rendered = test.render(nyx.panel.log._draw_title, ['NYX_DEBUG', 'NYX_INFO', 'NYX_NOTICE', 'NYX_WARNING', 'NYX_ERROR', 'NOTICE', 'WARN', 'ERR'], LogFilters()) self.assertEqual('Events (NOTICE-ERR, NYX DEBUG-ERR):', rendered.content)
def test_draw_title(self): rendered = test.render(nyx.panel.connection._draw_title, [], True) self.assertEqual('Connection Details:', rendered.content) rendered = test.render(nyx.panel.connection._draw_title, [], False) self.assertEqual('Connections:', rendered.content) entries = [MockEntry(entry_type = category) for category in (Category.INBOUND, Category.INBOUND, Category.OUTBOUND, Category.INBOUND, Category.CONTROL)] rendered = test.render(nyx.panel.connection._draw_title, entries, False) self.assertEqual('Connections (3 inbound, 1 outbound, 1 control):', rendered.content)
def test_confirm_save_torrc(self): rendered = test.render(nyx.popups.confirm_save_torrc, TORRC) self.assertEqual(EXPECTED_SAVE_TORRC_CONFIRMATION, rendered.content) self.assertEqual(False, rendered.return_value) def draw_func(): with mock_keybindings(curses.KEY_LEFT, curses.KEY_ENTER): return nyx.popups.confirm_save_torrc(TORRC) rendered = test.render(draw_func) self.assertEqual(EXPECTED_SAVE_TORRC_CONFIRMATION, rendered.content) self.assertEqual(True, rendered.return_value)
def test_confirm_save_torrc(self): rendered = test.render(nyx.popups.confirm_save_torrc, TORRC) self.assertEqual(EXPECTED_SAVE_TORRC_CONFIRMATION, rendered.content) self.assertEqual(False, rendered.return_value) def draw_func(): with patch('nyx.curses.key_input', side_effect = [nyx.curses.KeyInput(curses.KEY_LEFT), nyx.curses.KeyInput(curses.KEY_ENTER)]): return nyx.popups.confirm_save_torrc(TORRC) rendered = test.render(draw_func) self.assertEqual(EXPECTED_SAVE_TORRC_CONFIRMATION, rendered.content) self.assertEqual(True, rendered.return_value)
def test_draw_entry_with_duplicates(self): entry = LogEntry(NOW, 'NOTICE', 'feeding sulfur to baby dragons is just mean...') entry.duplicates = [1, 2] # only care about the count, not the content rendered = test.render(nyx.panel.log._draw_entry, 0, 0, 80, entry, True) self.assertEqual('16:41:37 [NOTICE] feeding sulfur to baby dragons is just mean...', rendered.content) rendered = test.render(nyx.panel.log._draw_entry, 0, 0, 80, entry, False) self.assertEqual('16:41:37 [NOTICE] feeding sulfur to baby dragons is just mean... [1 duplicate\n hidden]', rendered.content) entry.duplicates = [1, 2, 3, 4, 5, 6] rendered = test.render(nyx.panel.log._draw_entry, 0, 0, 80, entry, False) self.assertEqual('16:41:37 [NOTICE] feeding sulfur to baby dragons is just mean... [5 duplicates\n hidden]', rendered.content)
def test_scrollbar_middle(self): def _draw(subwindow): subwindow.scrollbar(15, 1, 30, fill_char = '*') # even scrolling down just one index should be visible self.assertEqual(EXPECTED_SCROLLBAR_MIDDLE, test.render(_draw).content.strip())
def test_draw_details_for_relay(self, consensus_tracker_mock, tor_controller_mock): router_status_entry = Mock() router_status_entry.or_port = 9051 router_status_entry.dir_port = 9052 router_status_entry.nickname = 'caerSidi' router_status_entry.flags = ['Fast', 'HSDir'] router_status_entry.published = datetime.datetime( 2012, 3, 1, 17, 15, 27) tor_controller_mock( ).get_network_status.return_value = router_status_entry server_descriptor = Mock() server_descriptor.exit_policy = stem.exit_policy.ExitPolicy( 'reject *:*') server_descriptor.tor_version = stem.version.Version('0.2.1.30') server_descriptor.operating_system = 'Debian' server_descriptor.contact = '*****@*****.**' tor_controller_mock( ).get_server_descriptor.return_value = server_descriptor consensus_tracker_mock().get_relay_fingerprints.return_value = { 22: 'B6D83EC2D9E18B0A7A33428F8CFA9C536769E209' } rendered = test.render(nyx.panel.connection._draw_details, line()) self.assertEqual(DETAILS_FOR_RELAY, rendered.content)
def test_help(self, get_controller_mock): header_panel = Mock() header_panel.key_handlers.return_value = ( nyx.panel.KeyHandler('n'), nyx.panel.KeyHandler('r'), ) graph_panel = Mock() graph_panel.key_handlers.return_value = ( nyx.panel.KeyHandler('r', 'resize graph'), nyx.panel.KeyHandler('s', 'graphed stats', current = 'bandwidth'), nyx.panel.KeyHandler('b', 'graph bounds', current = 'local max'), nyx.panel.KeyHandler('i', 'graph update interval', current = 'each second'), ) log_panel = Mock() log_panel.key_handlers.return_value = ( nyx.panel.KeyHandler('arrows', 'scroll up and down'), nyx.panel.KeyHandler('a', 'save snapshot of the log'), nyx.panel.KeyHandler('e', 'change logged events'), nyx.panel.KeyHandler('f', 'log regex filter', current = 'disabled'), nyx.panel.KeyHandler('u', 'duplicate log entries', current = 'hidden'), nyx.panel.KeyHandler('c', 'clear event log'), ) get_controller_mock().get_display_panels.return_value = [header_panel, graph_panel, log_panel] rendered = test.render(nyx.popups.show_help) self.assertEqual(EXPECTED_HELP_POPUP, rendered.content)
def test_select_sort_order(self): previous_order = ['Man Page Entry', 'Name', 'Is Set'] options = ['Name', 'Value', 'Value Type', 'Category', 'Usage', 'Summary', 'Description', 'Man Page Entry', 'Is Set'] rendered = test.render(nyx.popups.select_sort_order, 'Config Option Ordering:', options, previous_order, {}) self.assertEqual(EXPECTED_SORT_DIALOG_START, rendered.content) self.assertEqual(None, rendered.return_value)
def test_descriptor_without_fingerprint(self): rendered = test.render(nyx.popups.show_descriptor, None, nyx.curses.Color.RED, lambda key: key.match('esc')) self.assertEqual(EXPECTED_DESCRIPTOR_WITHOUT_FINGERPRINT, rendered.content) self.assertEqual(nyx.curses.KeyInput(27), rendered.return_value)
def test_draw_resource_usage(self): vals = nyx.panel.header.Sampling( start_time = 1460166022.231895, connection_time = 1460267022.231895, is_connected = False, tor_cpu = '2.1', nyx_cpu = '5.4', memory = '118 MB', memory_percent = '3.0', pid = '22439', ) test_input = { 80: 'cpu: 2.1% tor, 5.4% nyx mem: 118 MB (3.0%) pid: 22439 uptime: 1-04:03:20', 70: 'cpu: 2.1% tor, 5.4% nyx mem: 118 MB (3.0%) pid: 22439', 60: 'cpu: 2.1% tor, 5.4% nyx mem: 118 MB (3.0%) pid: 22439', 50: 'cpu: 2.1% tor, 5.4% nyx mem: 118 MB (3.0%)', 40: 'cpu: 2.1% tor, 5.4% nyx', 30: 'cpu: 2.1% tor, 5.4% nyx', 20: '', 10: '', 0: '', } for width, expected in test_input.items(): self.assertEqual(expected, test.render(nyx.panel.header._draw_resource_usage, 0, 0, width, vals, None).content)
def test_draw_address_column(self, tor_controller_mock): tor_controller_mock( ).get_info.side_effect = lambda param, default=None: { 'ip-to-country/ipv4-available': '1', 'address': '82.121.9.9', }[param] test_data = (( line(), '75.119.206.243:22 (de) --> 82.121.9.9:3531', ), ( line(entry=MockEntry(entry_type=Category.EXIT)), '82.121.9.9:3531 --> 75.119.206.243:22 (SSH)', ), ( line(line_type=LineType.CIRCUIT_HEADER, circ=MockCircuit(status='EXTENDING')), 'Building... --> 82.121.9.9', ), ( line(line_type=LineType.CIRCUIT), '82.121.9.9', )) for test_line, expected in test_data: rendered = test.render(nyx.panel.connection._draw_address_column, 0, 0, test_line, ()) self.assertEqual(expected, rendered.content)
def test_rendering_panel(self, sampling_mock, tor_controller_mock, nyx_controller_mock): nyx_controller_mock().get_page.return_value = 1 nyx_controller_mock().get_page_count.return_value = 4 sampling_mock.return_value = test_sampling() panel = nyx.panel.header.HeaderPanel() self.assertEqual(EXPECTED_PANEL, test.render(panel.draw).content)
def test_descriptor(self): rendered = test.render(nyx.popups.show_descriptor, '29787760145CD1A473552A2FC64C72A9A130820E', nyx.curses.Color.RED, lambda key: key.match('esc')) self.assertEqual(EXPECTED_DESCRIPTOR, rendered.content) self.assertEqual(nyx.curses.KeyInput(27), rendered.return_value)
def test_draw_line(self, tor_controller_mock): tor_controller_mock( ).get_info.side_effect = lambda param, default=None: { 'ip-to-country/ipv4-available': '1', 'address': '82.121.9.9', }[param] test_data = (( line(), ' 75.119.206.243:22 (de) --> 82.121.9.9:3531 15.4s (INBOUND)', ), ( line(entry=MockEntry(entry_type=Category.CIRCUIT), line_type=LineType.CIRCUIT_HEADER), ' 82.121.9.9 --> 75.119.206.243:22 (de) 15.4s (CIRCUIT)', ), ( line(line_type=LineType.CIRCUIT, fingerprint='1F43EE37A0670301AD9CB555D94AFEC2C89FDE86'), ' | 82.121.9.9 1 / Guard', ), ( line(line_type=LineType.CIRCUIT, fingerprint='B6D83EC2D9E18B0A7A33428F8CFA9C536769E209'), ' | 82.121.9.9 2 / Middle', ), ( line(line_type=LineType.CIRCUIT, fingerprint='E0BD57A11F00041A9789577C53A1B784473669E4'), ' +- 82.121.9.9 3 / End', )) for test_line, expected in test_data: rendered = test.render(nyx.panel.connection._draw_line, 0, 0, test_line, False, 80, TIMESTAMP + 15.4) self.assertEqual(expected, rendered.content)
def test_draw_title(self): rendered = test.render(nyx.panel.log._draw_title, ['NOTICE', 'WARN', 'ERR'], LogFilters()) self.assertEqual('Events (NOTICE-ERR):', rendered.content) rendered = test.render(nyx.panel.log._draw_title, [ 'NYX_NOTICE', 'NYX_WARNING', 'NYX_ERROR', 'NOTICE', 'WARN', 'ERR' ], LogFilters()) self.assertEqual('Events (TOR/NYX NOTICE-ERR):', rendered.content) rendered = test.render(nyx.panel.log._draw_title, [ 'NYX_DEBUG', 'NYX_INFO', 'NYX_NOTICE', 'NYX_WARNING', 'NYX_ERROR', 'NOTICE', 'WARN', 'ERR' ], LogFilters()) self.assertEqual('Events (NOTICE-ERR, NYX DEBUG-ERR):', rendered.content)
def test_draw_ports_section_with_relaying(self): vals = nyx.panel.header.Sampling( control_port = None, socket_path = '/path/to/control/socket', is_relay = False, ) self.assertEqual('Relaying Disabled, Control Socket: /path/to/control/socket', test.render(nyx.panel.header._draw_ports_section, 0, 0, 80, vals).content)
def test_draw_entry(self): entry = LogEntry(NOW, 'NOTICE', 'feeding sulfur to baby dragons is just mean...') rendered = test.render(nyx.panel.log._draw_entry, 0, 0, 80, entry, True) self.assertEqual( '16:41:37 [NOTICE] feeding sulfur to baby dragons is just mean...', rendered.content)
def test_rendering_panel(self, sampling_mock, tor_controller_mock, nyx_controller_mock): nyx_controller_mock().is_paused.return_value = False nyx_controller_mock().get_page.return_value = 1 nyx_controller_mock().get_page_count.return_value = 4 sampling_mock.return_value = test_sampling() panel = nyx.panel.header.HeaderPanel() self.assertEqual(EXPECTED_PANEL, test.render(panel._draw).content)
def test_draw_title_with_filter(self): log_filter = LogFilters() log_filter.select('stuff*') rendered = test.render(nyx.panel.log._draw_title, ['NOTICE', 'WARN', 'ERR'], log_filter) self.assertEqual('Events (NOTICE-ERR, filter: stuff*):', rendered.content)
def test_draw_line(self, tor_controller_mock): tor_controller_mock().get_info.return_value = True tor_controller_mock().get_conf.return_value = ['9051'] manual = stem.manual.Manual.from_cache() entry = nyx.panel.config.ConfigEntry('ControlPort', 'LineList', manual) rendered = test.render(nyx.panel.config._draw_line, 0, 0, entry, False, 10, 35) self.assertEqual(EXPECTED_LINE, rendered.content)
def test_draw_platform_section_without_version(self): vals = nyx.panel.header.Sampling( hostname = 'odin', platform = 'Linux 3.5.0-54-generic', version = 'Unknown', ) rendered = test.render(nyx.panel.header._draw_platform_section, 0, 0, 80, vals) self.assertEqual('nyx - odin (Linux 3.5.0-54-generic)', rendered.content)
def test_draw_details_with_multiple_matches(self, consensus_tracker_mock): consensus_tracker_mock().get_relay_fingerprints.return_value = { 52: '1F43EE37A0670301AD9CB555D94AFEC2C89FDE86', 80: 'B6D83EC2D9E18B0A7A33428F8CFA9C536769E209', 443: 'E0BD57A11F00041A9789577C53A1B784473669E4', } rendered = test.render(nyx.panel.connection._draw_details, line()) self.assertEqual(DETAILS_FOR_MULTIPLE_MATCHES, rendered.content)
def test_draw_line(self, tor_controller_mock): tor_controller_mock().get_info.return_value = True tor_controller_mock().get_conf.return_value = ['9051'] entry = nyx.panel.config.ConfigEntry('ControlPort', 'LineList') rendered = test.render(nyx.panel.config._draw_line, 0, 0, entry, False, 10, 35) self.assertEqual(EXPECTED_LINE, rendered.content)
def test_select_from_list(self): options = [ 'each second', '5 seconds', '30 seconds', 'minutely', '15 minute', '30 minute', 'hourly', 'daily' ] rendered = test.render(nyx.popups.select_from_list, 'Update Interval:', options, 'each second') self.assertEqual(EXPECTED_LIST_SELECTOR, rendered.content) self.assertEqual('each second', rendered.return_value)
def test_draw_selection_details(self, tor_controller_mock): tor_controller_mock().get_info.return_value = True tor_controller_mock().get_conf.return_value = ['9051'] selected = nyx.panel.config.ConfigEntry('ControlPort', 'LineList') rendered = test.render(nyx.panel.config._draw_selection_details, selected) self.assertEqual(EXPECTED_DETAIL_DIALOG, rendered.content)
def test_draw_subgraph_blank(self, tor_controller_mock): tor_controller_mock().get_info.return_value = None data = nyx.panel.graph.BandwidthStats() rendered = test.render(nyx.panel.graph._draw_subgraph, data.primary, 0, 30, 7, nyx.panel.graph.Bounds.LOCAL_MAX, nyx.panel.graph.Interval.EACH_SECOND, nyx.curses.Color.CYAN, '*') self.assertEqual(EXPECTED_BLANK_GRAPH, rendered.content)
def test_draw_selection_details(self, tor_controller_mock): tor_controller_mock().get_info.return_value = True tor_controller_mock().get_conf.return_value = ['9051'] manual = stem.manual.Manual.from_cache() selected = nyx.panel.config.ConfigEntry('ControlPort', 'LineList', manual) rendered = test.render(nyx.panel.config._draw_selection_details, selected) self.assertEqual(EXPECTED_DETAIL_DIALOG, rendered.content)
def test_draw_title(self): rendered = test.render(nyx.panel.connection._draw_title, [], True) self.assertEqual('Connection Details:', rendered.content) rendered = test.render(nyx.panel.connection._draw_title, [], False) self.assertEqual('Connections:', rendered.content) entries = [ MockEntry(entry_type=category) for category in (Category.INBOUND, Category.INBOUND, Category.OUTBOUND, Category.INBOUND, Category.CONTROL) ] rendered = test.render(nyx.panel.connection._draw_title, entries, False) self.assertEqual('Connections (3 inbound, 1 outbound, 1 control):', rendered.content)
def test_addstr(self): def _draw(subwindow): subwindow.addstr(0, 0, '0123456789' * 10) # should be trimmed to the subwindow width (80 columns) self.assertEqual( '01234567890123456789012345678901234567890123456789012345678901234567890123456789', test.render(_draw).content)
def test_draw_subgraph(self, tor_controller_mock): tor_controller_mock( ).get_info.return_value = '5430,5430 4210,4210 5510,5510 7100,7100 2000,2000 1750,1750 1880,1880 2500,2500 3770,3770' data = nyx.panel.graph.BandwidthStats() rendered = test.render(nyx.panel.graph._draw_subgraph, data.primary, 0, 30, 7, nyx.panel.graph.Bounds.LOCAL_MAX, nyx.panel.graph.Interval.EACH_SECOND, nyx.curses.Color.CYAN, '*') self.assertEqual(EXPECTED_GRAPH, rendered.content)
def test_counts(self): clients = { 'fr': 5, 'us': 6, 'ca': 11, 'ru': 32, 'de': 41, } rendered = test.render(nyx.popups.show_counts, 'Client Locales', clients, fill_char = '*') self.assertEqual(EXPECTED_COUNTS, rendered.content)
def test_select_event_types_initial_selection(self, controller_mock): controller = Mock() controller.get_info.return_value = 'DEBUG INFO NOTICE WARN ERR CIRC CIRC_MINOR STREAM ORCONN BW' controller_mock.return_value = controller def draw_func(): with mock_keybindings(curses.KEY_DOWN, curses.KEY_DOWN, curses.KEY_DOWN, curses.KEY_DOWN, curses.KEY_ENTER): return nyx.popups.select_event_types(['CIRC_MINOR']) rendered = test.render(draw_func) self.assertEqual(EXPECTED_EVENT_SELECTOR_INITIAL_SELECTION, rendered.content) self.assertEqual(set(['CIRC_MINOR']), rendered.return_value)
def test_multiline_panel(self, tor_controller_mock): tor_controller_mock()._handle_event = lambda event: None panel = nyx.panel.interpreter.InterpreterPanel() panel._add_line([('>>> ', ('Green', 'Bold')), ('GETINFO', ('Green', 'Bold')), (' version', ('Cyan', ))]) panel._add_line([('250-version=0.2.4.27 (git-412e3f7dc9c6c01a)', ('Blue', ))]) self.assertEqual(EXPECTED_MULTILINE_PANEL, test.render(panel._draw).content)
def test_scrollbar(self, tor_controller_mock): tor_controller_mock()._handle_event = lambda event: None panel = nyx.panel.interpreter.InterpreterPanel() panel._lines = [[('>>> ', ('Green', 'Bold')), ('GETINFO', ('Green', 'Bold')), (' version', ('Cyan', ))], [('250-version=0.2.4.27 (git-412e3f7dc9c6c01a)', ('Blue', ))]] + [()] * (panel.get_height() - 2) self.assertEqual(EXPECTED_WITH_SCROLLBAR, test.render(panel._draw).content)
def test_select_sort_order(self): previous_order = ['Man Page Entry', 'Name', 'Is Set'] options = [ 'Name', 'Value', 'Value Type', 'Category', 'Usage', 'Summary', 'Description', 'Man Page Entry', 'Is Set' ] rendered = test.render(nyx.popups.select_sort_order, 'Config Option Ordering:', options, previous_order, {}) self.assertEqual(EXPECTED_SORT_DIALOG_START, rendered.content) self.assertEqual(None, rendered.return_value)
def test_draw_right_column(self): rendered = test.render(nyx.panel.connection._draw_right_column, 0, 0, line(), TIMESTAMP + 62, ()) self.assertEqual(' 1.0m (INBOUND)', rendered.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) rendered = test.render(nyx.panel.connection._draw_right_column, 0, 0, test_line, TIMESTAMP + 68, ()) self.assertEqual('+ 1.1m (CONTROL)', rendered.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) rendered = test.render(nyx.panel.connection._draw_right_column, 0, 0, test_line, TIMESTAMP + 62, ()) self.assertEqual(expected, rendered.content)
def test_draw_entry_with_duplicates(self): entry = LogEntry(NOW, 'NOTICE', 'feeding sulfur to baby dragons is just mean...') entry.duplicates = [1, 2] # only care about the count, not the content rendered = test.render(nyx.panel.log._draw_entry, 0, 0, 80, entry, True) self.assertEqual( '16:41:37 [NOTICE] feeding sulfur to baby dragons is just mean...', rendered.content) rendered = test.render(nyx.panel.log._draw_entry, 0, 0, 80, entry, False) self.assertEqual( '16:41:37 [NOTICE] feeding sulfur to baby dragons is just mean... [1 duplicate\n hidden]', rendered.content) entry.duplicates = [1, 2, 3, 4, 5, 6] rendered = test.render(nyx.panel.log._draw_entry, 0, 0, 80, entry, False) self.assertEqual( '16:41:37 [NOTICE] feeding sulfur to baby dragons is just mean... [5 duplicates\n hidden]', rendered.content)
def test_draw_accounting_stats(self, tor_controller_mock): tor_controller_mock().is_alive.return_value = True accounting_stat = stem.control.AccountingStats( 1410723598.276578, 'awake', datetime.datetime(2014, 9, 14, 19, 41), 62, 4837, 102944, 107781, 2050, 7440, 9490, ) rendered = test.render(nyx.panel.graph._draw_accounting_stats, 0, accounting_stat) self.assertEqual(EXPECTED_ACCOUNTING, rendered.content)
def test_select_sort_order_usage(self): # Use the dialog to make a selection. At the end we render two options as # being selected (rather than three) because the act of selecing the third # closed the popup. def draw_func(): with mock_keybindings(curses.KEY_ENTER, curses.KEY_DOWN, curses.KEY_ENTER, curses.KEY_ENTER): return nyx.popups.select_sort_order('Config Option Ordering:', options, previous_order, {}) previous_order = ['Man Page Entry', 'Name', 'Is Set'] options = ['Name', 'Value', 'Value Type', 'Category', 'Usage', 'Summary', 'Description', 'Man Page Entry', 'Is Set'] rendered = test.render(draw_func) self.assertEqual(EXPECTED_SORT_DIALOG_END, rendered.content) self.assertEqual(['Name', 'Summary', 'Description'], rendered.return_value)
def test_draw_address_column(self, tor_controller_mock): tor_controller_mock().get_info.return_value = '82.121.9.9' tor_controller_mock().is_geoip_unavailable.return_value = False test_data = { line(): '75.119.206.243:22 (de) --> 82.121.9.9:3531', line(entry = MockEntry(entry_type = Category.EXIT)): '82.121.9.9:3531 --> 75.119.206.243:22 (SSH)', line(line_type = LineType.CIRCUIT_HEADER, circ = MockCircuit(status = 'EXTENDING')): 'Building... --> 82.121.9.9', line(line_type = LineType.CIRCUIT): '82.121.9.9', } for test_line, expected in test_data.items(): rendered = test.render(nyx.panel.connection._draw_address_column, 0, 0, test_line, ()) self.assertEqual(expected, rendered.content)
def test_draw_ports_section(self): vals = nyx.panel.header.Sampling( nickname = 'Unnamed', address = '174.21.17.28', or_port = '7000', dir_port = '7001', control_port = '9051', auth_type = 'cookie', is_relay = True, ) test_input = { 80: 'Unnamed - 174.21.17.28:7000, Dir Port: 7001, Control Port (cookie): 9051', 50: 'Unnamed - 174.21.17.28:7000, Dir Port: 7001, Control Port: 9051', 0: 'Unnamed - 174.21.17.28:7000, Dir Port: 7001, Control Port: 9051', } for width, expected in test_input.items(): self.assertEqual(expected, test.render(nyx.panel.header._draw_ports_section, 0, 0, width, vals).content)
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 test_data = { line(): '1F43EE37A0670301AD9CB555D94AFEC2C89FDE86 Unnamed', line(line_type=LineType.CIRCUIT_HEADER): 'Purpose: General, Circuit ID: 7', line(entry=MockEntry(entry_type=Category.CONTROL)): 'firefox (722)', } for test_line, expected in test_data.items(): rendered = test.render(nyx.panel.connection._draw_line_details, 0, 0, test_line, 80, ()) self.assertEqual(expected, rendered.content)
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 test_data = { line(): '1F43EE37A0670301AD9CB555D94AFEC2C89FDE86 Unnamed', line(line_type = LineType.CIRCUIT_HEADER): 'Purpose: General, Circuit ID: 7', line(entry = MockEntry(entry_type = Category.CONTROL)): 'firefox (722)', } for test_line, expected in test_data.items(): rendered = test.render(nyx.panel.connection._draw_line_details, 0, 0, test_line, 80, ()) self.assertEqual(expected, rendered.content)
def test_draw_line(self, tor_controller_mock): tor_controller_mock().get_info.return_value = '82.121.9.9' tor_controller_mock().is_geoip_unavailable.return_value = False test_data = { line(): ' 75.119.206.243:22 (de) --> 82.121.9.9:3531 15.4s (INBOUND)', line(entry = MockEntry(entry_type = Category.CIRCUIT), line_type = LineType.CIRCUIT_HEADER): ' 82.121.9.9 --> 75.119.206.243:22 (de) 15.4s (CIRCUIT)', line(line_type = LineType.CIRCUIT, fingerprint = '1F43EE37A0670301AD9CB555D94AFEC2C89FDE86'): ' | 82.121.9.9 1 / Guard', line(line_type = LineType.CIRCUIT, fingerprint = 'B6D83EC2D9E18B0A7A33428F8CFA9C536769E209'): ' | 82.121.9.9 2 / Middle', line(line_type = LineType.CIRCUIT, fingerprint = 'E0BD57A11F00041A9789577C53A1B784473669E4'): ' +- 82.121.9.9 3 / Exit', } for test_line, expected in test_data.items(): rendered = test.render(nyx.panel.connection._draw_line, 0, 0, test_line, False, 80, TIMESTAMP + 15.4) self.assertEqual(expected, rendered.content)
def test_draw_fingerprint_and_fd_usage(self): vals = nyx.panel.header.Sampling( fingerprint = '1A94D1A794FCB2F8B6CBC179EF8FDD4008A98D3B', fd_used = None, ) test_input = { 80: 'fingerprint: 1A94D1A794FCB2F8B6CBC179EF8FDD4008A98D3B', 70: 'fingerprint: 1A94D1A794FCB2F8B6CBC179EF8FDD4008A98D3B', 60: 'fingerprint: 1A94D1A794FCB2F8B6CBC179EF8FDD4008A98D3B', 50: 'fingerprint: 1A94D1A794FCB2F8B6CBC179EF8FDD4008...', 40: 'fingerprint: 1A94D1A794FCB2F8B6CBC179...', 30: 'fingerprint: 1A94D1A794FCB2...', 20: 'fingerprint: 1A94...', 10: 'fingerp...', 0: '', } for width, expected in test_input.items(): self.assertEqual(expected, test.render(nyx.panel.header._draw_fingerprint_and_fd_usage, 0, 0, width, vals).content)
def test_draw_fingerprint_and_fd_usage_with_fd_count(self): test_input = { 59: 'fingerprint: <stub>', 60: 'fingerprint: <stub>, file descriptors: 60 / 100 (60%)', 75: 'fingerprint: <stub>, file descriptors: 75 / 100 (75%)', 89: 'fingerprint: <stub>, file descriptors: 89 / 100 (89%)', 90: 'fingerprint: <stub>, file descriptors: 90 / 100 (90%)', 95: 'fingerprint: <stub>, file descriptors: 95 / 100 (95%)', 99: 'fingerprint: <stub>, file descriptors: 99 / 100 (99%)', 100: 'fingerprint: <stub>, file descriptors: 100 / 100 (100%)', } for fd_used, expected in test_input.items(): vals = nyx.panel.header.Sampling( fingerprint = '<stub>', fd_used = fd_used, fd_limit = 100, ) self.assertEqual(expected, test.render(nyx.panel.header._draw_fingerprint_and_fd_usage, 0, 0, 80, vals).content)
def test_draw_platform_section(self): vals = nyx.panel.header.Sampling( hostname = 'odin', platform = 'Linux 3.5.0-54-generic', version = '0.2.8.1-alpha-dev', version_status = 'unrecommended', ) test_input = { 80: 'nyx - odin (Linux 3.5.0-54-generic) Tor 0.2.8.1-alpha-dev', 70: 'nyx - odin (Linux 3.5.0-54-generic) Tor 0.2.8.1-alpha-dev', 60: 'nyx - odin (Linux 3.5.0-54-generic) Tor 0.2.8.1-al...', 50: 'nyx - odin (Linux 3.5.0-54-generic)', 40: 'nyx - odin (Linux 3.5.0-54-generic)', 30: 'nyx - odin (Linux 3.5.0-54...)', 20: 'nyx - odin (Linu...)', 10: 'nyx - odin', 0: '', } for width, expected in test_input.items(): self.assertEqual(expected, test.render(nyx.panel.header._draw_platform_section, 0, 0, width, vals).content)
def test_draw_details_for_relay(self, consensus_tracker_mock, tor_controller_mock): router_status_entry = Mock() router_status_entry.or_port = 9051 router_status_entry.dir_port = 9052 router_status_entry.nickname = 'caerSidi' router_status_entry.flags = ['Fast', 'HSDir'] router_status_entry.published = datetime.datetime(2012, 3, 1, 17, 15, 27) tor_controller_mock().get_network_status.return_value = router_status_entry server_descriptor = Mock() server_descriptor.exit_policy = stem.exit_policy.ExitPolicy('reject *:*') server_descriptor.tor_version = stem.version.Version('0.2.1.30') server_descriptor.operating_system = 'Debian' server_descriptor.contact = '*****@*****.**' tor_controller_mock().get_server_descriptor.return_value = server_descriptor consensus_tracker_mock().get_relay_fingerprints.return_value = { 22: 'B6D83EC2D9E18B0A7A33428F8CFA9C536769E209' } rendered = test.render(nyx.panel.connection._draw_details, line()) self.assertEqual(DETAILS_FOR_RELAY, rendered.content)