def test_info(self, downloader_mock): controller, server_desc, ns_desc = Mock(), Mock(), Mock() controller.get_microdescriptor.return_value = None controller.get_server_descriptor.return_value = server_desc controller.get_network_status.return_value = ns_desc downloader_mock().get_server_descriptors.return_value = [server_desc] controller.get_info.side_effect = lambda arg, _: { 'ip-to-country/128.31.0.34': 'us', }[arg] ns_desc.address = '128.31.0.34' ns_desc.or_port = 9101 ns_desc.published = datetime.datetime(2014, 5, 5, 5, 52, 5) ns_desc.nickname = 'moria1' ns_desc.flags = ['Authority', 'Fast', 'Guard', 'HSDir', 'Named', 'Running', 'Stable', 'V2Dir', 'Valid'] server_desc.exit_policy = 'reject *:*' server_desc.platform = 'Linux' server_desc.tor_version = stem.version.Version('0.2.5.4-alpha-dev') server_desc.contact = '1024D/28988BF5 arma mit edu' interpreter = ControlInterpreter(controller) self.assertTrue(interpreter.run_command('/info ' + FINGERPRINT).startswith(EXPECTED_INFO_RESPONSE))
def test_setevents(self): controller = Mock() controller.msg.return_value = ControlMessage.from_str('250 OK\r\n') interpreter = ControlInterpreter(controller) self.assertEqual('\x1b[34m250 OK\x1b[0m\n', interpreter.run_command('SETEVENTS BW'))
def test_setevents(self): controller = Mock() controller.msg.return_value = mocking.get_message('250 OK') interpreter = ControlInterpreter(controller) self.assertEqual('\x1b[34m250 OK\x1b[0m\n', interpreter.run_command('SETEVENTS BW'))
def test_help(self): interpreter = ControlInterpreter(CONTROLLER) self.assertTrue('Interpreter commands include:' in interpreter.run_command('/help')) self.assertTrue('Queries the tor process for information.' in interpreter.run_command('/help GETINFO')) self.assertTrue('Queries the tor process for information.' in interpreter.run_command('/help GETINFO version'))
def test_getconf(self): response = '250-Log=notice stdout\r\n250 Address' controller = Mock() controller.msg.return_value = mocking.get_message(response) interpreter = ControlInterpreter(controller) self.assertEqual('\x1b[34m250-Log=notice stdout\r\x1b[0m\n\x1b[34m250 Address\x1b[0m\n', interpreter.run_command('GETCONF log address')) controller.msg.assert_called_with('GETCONF log address')
def test_when_disconnected(self): controller = Mock() controller.msg.side_effect = stem.SocketClosed('kaboom!') interpreter = ControlInterpreter(controller) # we should be able to run non-tor commands self.assertTrue('Interpreter commands include:' in interpreter.run_command('/help')) # ... but tor commands should provide exceptions self.assertRaises(stem.SocketClosed, interpreter.run_command, 'GETINFO version')
def test_getconf(self): controller = Mock() controller.msg.return_value = ControlMessage.from_str( '250-Log=notice stdout\r\n250 Address\r\n') interpreter = ControlInterpreter(controller) self.assertEqual( '\x1b[34m250-Log=notice stdout\r\x1b[0m\n\x1b[34m250 Address\x1b[0m\n', interpreter.run_command('GETCONF log address')) controller.msg.assert_called_with('GETCONF log address')
def test_getinfo(self): controller = Mock() controller.msg.return_value = ControlMessage.from_str('250-version=0.2.5.1-alpha-dev (git-245ecfff36c0cecc)\r\n250 OK\r\n') interpreter = ControlInterpreter(controller) self.assertEqual('\x1b[34m250-version=0.2.5.1-alpha-dev (git-245ecfff36c0cecc)\r\x1b[0m\n\x1b[34m250 OK\x1b[0m\n', interpreter.run_command('GETINFO version')) self.assertEqual('\x1b[34m250-version=0.2.5.1-alpha-dev (git-245ecfff36c0cecc)\r\x1b[0m\n\x1b[34m250 OK\x1b[0m\n', interpreter.run_command('GETINFO version')) controller.msg.assert_called_with('GETINFO version') controller.msg.side_effect = stem.ControllerError('kaboom!') self.assertEqual('\x1b[1;31mkaboom!\x1b[0m\n', interpreter.run_command('getinfo process/user'))
def test_getinfo(self): response = '250-version=0.2.5.1-alpha-dev (git-245ecfff36c0cecc)\r\n250 OK' controller = Mock() controller.msg.return_value = mocking.get_message(response) interpreter = ControlInterpreter(controller) self.assertEqual('\x1b[34m250-version=0.2.5.1-alpha-dev (git-245ecfff36c0cecc)\r\x1b[0m\n\x1b[34m250 OK\x1b[0m\n', interpreter.run_command('GETINFO version')) self.assertEqual('\x1b[34m250-version=0.2.5.1-alpha-dev (git-245ecfff36c0cecc)\r\x1b[0m\n\x1b[34m250 OK\x1b[0m\n', interpreter.run_command('GETINFO version')) controller.msg.assert_called_with('GETINFO version') controller.msg.side_effect = stem.ControllerError('kaboom!') self.assertEqual('\x1b[1;31mkaboom!\x1b[0m\n', interpreter.run_command('getinfo process/user'))
def test_getconf(self): controller = Mock() controller.msg.return_value = ControlMessage.from_str('250-Log=notice stdout\r\n250 Address\r\n') interpreter = ControlInterpreter(controller) self.assertEqual('\x1b[34m250-Log=notice stdout\r\x1b[0m\n\x1b[34m250 Address\x1b[0m\n', interpreter.run_command('GETCONF log address')) controller.msg.assert_called_with('GETCONF log address')
def test_events(self): interpreter = ControlInterpreter(CONTROLLER) # no received events self.assertEqual('', interpreter.run_command('/events')) # with enqueued events event_contents = ( '650 BW 15 25', '650 BW 758 570', '650 DEBUG connection_edge_process_relay_cell(): Got an extended cell! Yay.', ) for content in event_contents: event = ControlMessage.from_str(content, 'EVENT', normalize = True) interpreter._received_events.append(event) self.assertEqual(EXPECTED_EVENTS_RESPONSE, interpreter.run_command('/events'))
def test_events(self): interpreter = ControlInterpreter(CONTROLLER) # no received events self.assertEqual('', interpreter.run_command('/events')) # with enqueued events event_contents = ( '650 BW 15 25', '650 BW 758 570', '650 DEBUG connection_edge_process_relay_cell(): Got an extended cell! Yay.', ) for content in event_contents: event = ControlMessage.from_str(content, 'EVENT', normalize=True) interpreter._received_events.append(event) self.assertEqual(EXPECTED_EVENTS_RESPONSE, interpreter.run_command('/events'))
def test_events(self): interpreter = ControlInterpreter(CONTROLLER) # no received events self.assertEqual('\n', interpreter.run_command('/events')) # with enqueued events event_contents = ( '650 BW 15 25', '650 BW 758 570', '650 DEBUG connection_edge_process_relay_cell(): Got an extended cell! Yay.', ) for content in event_contents: event = mocking.get_message(content) stem.response.convert('EVENT', event) interpreter._received_events.append(event) self.assertEqual(EXPECTED_EVENTS_RESPONSE, interpreter.run_command('/events'))
def test_unrecognized_interpreter_command(self): interpreter = ControlInterpreter(CONTROLLER) expected = "\x1b[1;31m'/unrecognized' isn't a recognized command\x1b[0m\n" self.assertEqual(expected, interpreter.run_command('/unrecognized'))
def test_when_disconnected(self): controller = Mock() controller.is_alive.return_value = False interpreter = ControlInterpreter(controller) self.assertRaises(stem.SocketClosed, interpreter.run_command, '/help')
def test_quit(self): interpreter = ControlInterpreter(CONTROLLER) self.assertRaises(stem.SocketClosed, interpreter.run_command, '/quit') self.assertRaises(stem.SocketClosed, interpreter.run_command, 'QUIT')