def test_relative_cookie(self): """ Checks an authentication cookie with a relative path where expansion both succeeds and fails. """ # TODO: move into stem.connection unit tests? # we need to mock both pid and cwd lookups since the general cookie # expanion works by... # - resolving the pid of the "tor" process # - using that to get tor's cwd def call_mocking(command): if command == stem.util.system.GET_PID_BY_NAME_PGREP % "tor": return ["10"] elif command == stem.util.system.GET_CWD_PWDX % 10: return ["10: /tmp/foo"] mocking.mock(stem.util.proc.is_available, mocking.return_false()) mocking.mock(stem.util.system.is_available, mocking.return_true()) mocking.mock(stem.util.system.call, call_mocking) control_message = mocking.get_message(RELATIVE_COOKIE_PATH) stem.response.convert("PROTOCOLINFO", control_message) stem.connection._expand_cookie_path(control_message, stem.util.system.get_pid_by_name, "tor") self.assertEquals( os.path.join("/tmp/foo", "tor-browser_en-US", "Data", "control_auth_cookie"), control_message.cookie_path) # exercise cookie expansion where both calls fail (should work, just # leaving the path unexpanded) mocking.mock(stem.util.system.call, mocking.return_none()) control_message = mocking.get_message(RELATIVE_COOKIE_PATH) stem.response.convert("PROTOCOLINFO", control_message) self.assertEquals("./tor-browser_en-US/Data/control_auth_cookie", control_message.cookie_path) # reset system call mocking mocking.revert_mocking()
def test_relative_cookie(self): """ Checks an authentication cookie with a relative path where expansion both succeeds and fails. """ # TODO: move into stem.connection unit tests? # we need to mock both pid and cwd lookups since the general cookie # expanion works by... # - resolving the pid of the "tor" process # - using that to get tor's cwd def call_mocking(command): if command == stem.util.system.GET_PID_BY_NAME_PGREP % "tor": return ["10"] elif command == stem.util.system.GET_CWD_PWDX % 10: return ["10: /tmp/foo"] mocking.mock(stem.util.proc.is_available, mocking.return_false()) mocking.mock(stem.util.system.is_available, mocking.return_true()) mocking.mock(stem.util.system.call, call_mocking) control_message = mocking.get_message(RELATIVE_COOKIE_PATH) stem.response.convert("PROTOCOLINFO", control_message) stem.connection._expand_cookie_path(control_message, stem.util.system.get_pid_by_name, "tor") self.assertEquals(os.path.join("/tmp/foo", "tor-browser_en-US", "Data", "control_auth_cookie"), control_message.cookie_path) # exercise cookie expansion where both calls fail (should work, just # leaving the path unexpanded) mocking.mock(stem.util.system.call, mocking.return_none()) control_message = mocking.get_message(RELATIVE_COOKIE_PATH) stem.response.convert("PROTOCOLINFO", control_message) self.assertEquals("./tor-browser_en-US/Data/control_auth_cookie", control_message.cookie_path) # reset system call mocking mocking.revert_mocking()
def test_is_running(self): """ Exercises multiple use cases for the is_running function. """ # mock response with a linux and bsd resolver running_commands = ["irssi", "moc", "tor", "ps", " firefox "] for ps_cmd in (system.IS_RUNNING_PS_LINUX, system.IS_RUNNING_PS_BSD): mocking.mock(system.call, mock_call(ps_cmd, running_commands)) self.assertTrue(system.is_running("irssi")) self.assertTrue(system.is_running("moc")) self.assertTrue(system.is_running("tor")) self.assertTrue(system.is_running("ps")) self.assertTrue(system.is_running("firefox")) self.assertEqual(False, system.is_running("something_else")) # mock both calls failing mocking.mock(system.call, mocking.return_none()) self.assertFalse(system.is_running("irssi")) self.assertEquals(None, system.is_running("irssi"))
def setUp(self): mocking.mock(stem.util.proc.is_available, mocking.return_false()) mocking.mock(system.is_available, mocking.return_true()) mocking.mock(system.call, mocking.return_none())