def test_event_listening(self): """ Exercises the add_event_listener and remove_event_listener methods. """ # set up for failure to create any events mocking.mock_method(Controller, "is_authenticated", mocking.return_true()) mocking.mock_method(Controller, "_attach_listeners", mocking.return_value(([], []))) mocking.mock_method( Controller, "get_version", mocking.return_value(stem.version.Version('0.1.0.14'))) self.assertRaises(InvalidRequest, self.controller.add_event_listener, mocking.no_op(), EventType.BW) # set up to only fail newer events mocking.mock_method( Controller, "get_version", mocking.return_value(stem.version.Version('0.2.0.35'))) # EventType.BW is one of the earliest events self.controller.add_event_listener(mocking.no_op(), EventType.BW) # EventType.SIGNAL was added in tor version 0.2.3.1-alpha self.assertRaises(InvalidRequest, self.controller.add_event_listener, mocking.no_op(), EventType.SIGNAL)
def test_get_pid_by_name_ps_bsd(self): """ Tests the get_pid_by_name function with the bsd variant of ps. """ mocking.mock(system.is_bsd, mocking.return_true()) mocking.mock(system.call, mock_call(system.GET_PID_BY_NAME_PS_BSD, GET_PID_BY_NAME_PS_BSD)) self.assertEquals(1, system.get_pid_by_name("launchd")) self.assertEquals(11, system.get_pid_by_name("DirectoryService")) self.assertEquals(None, system.get_pid_by_name("blarg"))
def test_get_pid_by_name_ps_bsd(self): """ Tests the get_pid_by_name function with the bsd variant of ps. """ mocking.mock(system.is_bsd, mocking.return_true()) mocking.mock( system.call, mock_call(system.GET_PID_BY_NAME_PS_BSD, GET_PID_BY_NAME_PS_BSD)) self.assertEquals(1, system.get_pid_by_name("launchd")) self.assertEquals(11, system.get_pid_by_name("DirectoryService")) self.assertEquals(None, system.get_pid_by_name("blarg"))
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_event_listening(self): """ Exercises the add_event_listener and remove_event_listener methods. """ # set up for failure to create any events mocking.mock_method(Controller, "is_authenticated", mocking.return_true()) mocking.mock_method(Controller, "_attach_listeners", mocking.return_value(([], []))) mocking.mock_method(Controller, "get_version", mocking.return_value(stem.version.Version('0.1.0.14'))) self.assertRaises(InvalidRequest, self.controller.add_event_listener, mocking.no_op(), EventType.BW) # set up to only fail newer events mocking.mock_method(Controller, "get_version", mocking.return_value(stem.version.Version('0.2.0.35'))) # EventType.BW is one of the earliest events self.controller.add_event_listener(mocking.no_op(), EventType.BW) # EventType.SIGNAL was added in tor version 0.2.3.1-alpha self.assertRaises(InvalidRequest, self.controller.add_event_listener, mocking.no_op(), EventType.SIGNAL)
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 setUp(self): test.runner.require_control(self) mocking.mock(stem.util.proc.is_available, mocking.return_false()) mocking.mock(stem.util.system.is_available, mocking.return_true())
def setUp(self): mocking.mock(stem.util.proc.is_available, mocking.return_false()) mocking.mock(stem.util.system.is_available, mocking.return_true())