def test_get_pid_by_port_sockstat(self): """ Tests the get_pid_by_port function with a sockstat response. """ runner = test.runner.get_runner() if not _has_port(): test.runner.skip(self, '(test instance has no port)') return elif not stem.util.system.is_available('sockstat'): test.runner.skip(self, '(sockstat unavailable)') return elif not stem.util.system.is_bsd(): test.runner.skip(self, '(bsd only)') return elif not runner.is_ptraceable(): test.runner.skip(self, '(DisableDebuggerAttachment is set)') return sockstat_prefix = stem.util.system.GET_PID_BY_PORT_SOCKSTAT % '' call_replacement = filter_system_call([sockstat_prefix]) with patch('stem.util.system.call') as call_mock: call_mock.side_effect = call_replacement tor_pid = test.runner.get_runner().get_pid() self.assertEquals( tor_pid, stem.util.system.get_pid_by_port(test.runner.CONTROL_PORT))
def test_get_pid_by_port_lsof(self): """ Tests the get_pid_by_port function with a lsof response. """ runner = test.runner.get_runner() if not _has_port(): test.runner.skip(self, '(test instance has no port)') return elif not stem.util.system.is_available('lsof'): test.runner.skip(self, '(lsof unavailable)') return elif stem.util.system.is_mac(): test.runner.skip(self, '(resolvers unavailable)') return elif not runner.is_ptraceable(): test.runner.skip(self, '(DisableDebuggerAttachment is set)') return lsof_prefix = stem.util.system.GET_PID_BY_PORT_LSOF call_replacement = filter_system_call([lsof_prefix]) with patch('stem.util.system.call') as call_mock: call_mock.side_effect = call_replacement tor_pid = test.runner.get_runner().get_pid() self.assertEquals( tor_pid, stem.util.system.get_pid_by_port(test.runner.CONTROL_PORT))
def test_get_pid_by_name_lsof(self): """ Tests the get_pid_by_name function with a lsof response. """ runner = test.runner.get_runner() if self._is_extra_tor_running(): test.runner.skip(self, '(multiple tor instances)') return elif not stem.util.system.is_available('lsof'): test.runner.skip(self, '(lsof unavailable)') return elif not runner.is_ptraceable(): test.runner.skip(self, '(DisableDebuggerAttachment is set)') return lsof_prefix = stem.util.system.GET_PID_BY_NAME_LSOF % '' call_replacement = filter_system_call([lsof_prefix]) with patch('stem.util.system.call') as call_mock: call_mock.side_effect = call_replacement our_tor_pid = test.runner.get_runner().get_pid() all_tor_pids = stem.util.system.get_pid_by_name('tor', multiple=True) if len(all_tor_pids) == 1: self.assertEquals(our_tor_pid, all_tor_pids[0])
def test_cwd_pwdx(self): """ Tests the pid_by_cwd function with a pwdx response. """ runner = test.runner.get_runner() if not stem.util.system.is_available('pwdx'): test.runner.skip(self, '(pwdx unavailable)') return elif not runner.is_ptraceable(): test.runner.skip(self, '(DisableDebuggerAttachment is set)') return # filter the call function to only allow this command pwdx_prefix = stem.util.system.GET_CWD_PWDX % '' call_replacement = filter_system_call([pwdx_prefix]) with patch('stem.util.system.call') as call_mock: call_mock.side_effect = call_replacement runner_pid, tor_cwd = runner.get_pid(), runner.get_tor_cwd() self.assertEqual(tor_cwd, stem.util.system.cwd(runner_pid))
def test_pid_by_port_netstat(self): """ Tests the pid_by_port function with a netstat response. """ runner = test.runner.get_runner() if not _has_port(): test.runner.skip(self, '(test instance has no port)') return elif not stem.util.system.is_available('netstat'): test.runner.skip(self, '(netstat unavailable)') return elif stem.util.system.is_bsd() or stem.util.system.is_windows(): test.runner.skip(self, '(linux only)') return elif stem.util.system.is_gentoo(): test.runner.skip(self, '(unavailable on gentoo)') return elif not runner.is_ptraceable(): test.runner.skip(self, '(DisableDebuggerAttachment is set)') return netstat_prefix = stem.util.system.GET_PID_BY_PORT_NETSTAT call_replacement = filter_system_call([netstat_prefix]) with patch('stem.util.system.call') as call_mock: call_mock.side_effect = call_replacement tor_pid = test.runner.get_runner().get_pid() self.assertEqual( tor_pid, stem.util.system.pid_by_port(test.runner.CONTROL_PORT))
def test_get_cwd_lsof(self): """ Tests the get_pid_by_cwd function with a lsof response. """ runner = test.runner.get_runner() if not stem.util.system.is_available('lsof'): test.runner.skip(self, '(lsof unavailable)') return elif not runner.is_ptraceable(): test.runner.skip(self, '(DisableDebuggerAttachment is set)') return # filter the call function to only allow this command lsof_prefix = 'lsof -a -p ' call_replacement = filter_system_call([lsof_prefix]) with patch('stem.util.system.call') as call_mock: call_mock.side_effect = call_replacement runner_pid, tor_cwd = runner.get_pid(), runner.get_tor_cwd() self.assertEquals(tor_cwd, stem.util.system.get_cwd(runner_pid))
def test_get_pid_by_port_sockstat(self): """ Tests the get_pid_by_port function with a sockstat response. """ runner = test.runner.get_runner() if not _has_port(): test.runner.skip(self, "(test instance has no port)") return elif not stem.util.system.is_available("sockstat"): test.runner.skip(self, "(sockstat unavailable)") return elif not stem.util.system.is_bsd(): test.runner.skip(self, "(bsd only)") return elif not runner.is_ptraceable(): test.runner.skip(self, "(DisableDebuggerAttachment is set)") return sockstat_prefix = stem.util.system.GET_PID_BY_PORT_SOCKSTAT % "" mocking.mock(stem.util.system.call, filter_system_call([sockstat_prefix])) tor_pid = test.runner.get_runner().get_pid() self.assertEquals( tor_pid, stem.util.system.get_pid_by_port(test.runner.CONTROL_PORT))
def test_pid_by_port_netstat(self): """ Tests the pid_by_port function with a netstat response. """ runner = test.runner.get_runner() if not _has_port(): test.runner.skip(self, '(test instance has no port)') return elif not stem.util.system.is_available('netstat'): test.runner.skip(self, '(netstat unavailable)') return elif stem.util.system.is_bsd() or stem.util.system.is_windows(): test.runner.skip(self, '(linux only)') return elif stem.util.system.is_gentoo(): test.runner.skip(self, '(unavailable on gentoo)') return elif not runner.is_ptraceable(): test.runner.skip(self, '(DisableDebuggerAttachment is set)') return netstat_prefix = stem.util.system.GET_PID_BY_PORT_NETSTAT call_replacement = filter_system_call([netstat_prefix]) with patch('stem.util.system.call') as call_mock: call_mock.side_effect = call_replacement tor_pid = test.runner.get_runner().get_pid() self.assertEqual(tor_pid, stem.util.system.pid_by_port(test.runner.CONTROL_PORT))
def test_get_pid_by_port_lsof(self): """ Tests the get_pid_by_port function with a lsof response. """ runner = test.runner.get_runner() if not _has_port(): test.runner.skip(self, "(test instance has no port)") return elif not stem.util.system.is_available("lsof"): test.runner.skip(self, "(lsof unavailable)") return elif stem.util.system.is_mac(): test.runner.skip(self, "(resolvers unavailable)") return elif not runner.is_ptraceable(): test.runner.skip(self, "(DisableDebuggerAttachment is set)") return lsof_prefix = stem.util.system.GET_PID_BY_PORT_LSOF mocking.mock(stem.util.system.call, filter_system_call([lsof_prefix])) tor_pid = test.runner.get_runner().get_pid() self.assertEquals( tor_pid, stem.util.system.get_pid_by_port(test.runner.CONTROL_PORT))
def test_get_pid_by_port_sockstat(self): """ Tests the get_pid_by_port function with a sockstat response. """ runner = test.runner.get_runner() if not _has_port(): test.runner.skip(self, "(test instance has no port)") return elif not stem.util.system.is_available("sockstat"): test.runner.skip(self, "(sockstat unavailable)") return elif not stem.util.system.is_bsd(): test.runner.skip(self, "(bsd only)") return elif not runner.is_ptraceable(): test.runner.skip(self, "(DisableDebuggerAttachment is set)") return sockstat_prefix = stem.util.system.GET_PID_BY_PORT_SOCKSTAT % "" call_replacement = filter_system_call([sockstat_prefix]) with patch('stem.util.system.call') as call_mock: call_mock.side_effect = call_replacement tor_pid = test.runner.get_runner().get_pid() self.assertEquals(tor_pid, stem.util.system.get_pid_by_port(test.runner.CONTROL_PORT))
def test_get_pid_by_port_lsof(self): """ Tests the get_pid_by_port function with a lsof response. """ runner = test.runner.get_runner() if not _has_port(): test.runner.skip(self, "(test instance has no port)") return elif not stem.util.system.is_available("lsof"): test.runner.skip(self, "(lsof unavailable)") return elif stem.util.system.is_mac(): test.runner.skip(self, "(resolvers unavailable)") return elif not runner.is_ptraceable(): test.runner.skip(self, "(DisableDebuggerAttachment is set)") return lsof_prefix = stem.util.system.GET_PID_BY_PORT_LSOF call_replacement = filter_system_call([lsof_prefix]) with patch('stem.util.system.call') as call_mock: call_mock.side_effect = call_replacement tor_pid = test.runner.get_runner().get_pid() self.assertEquals(tor_pid, stem.util.system.get_pid_by_port(test.runner.CONTROL_PORT))
def test_get_pid_by_name_lsof(self): """ Tests the get_pid_by_name function with a lsof response. """ runner = test.runner.get_runner() if self.is_extra_tor_running: test.runner.skip(self, "(multiple tor instances)") return elif not stem.util.system.is_available("lsof"): test.runner.skip(self, "(lsof unavailable)") return elif not runner.is_ptraceable(): test.runner.skip(self, "(DisableDebuggerAttachment is set)") return lsof_prefix = stem.util.system.GET_PID_BY_NAME_LSOF % "" call_replacement = filter_system_call([lsof_prefix]) with patch('stem.util.system.call') as call_mock: call_mock.side_effect = call_replacement our_tor_pid = test.runner.get_runner().get_pid() all_tor_pids = stem.util.system.get_pid_by_name("tor", multiple = True) if len(all_tor_pids) == 1: self.assertEquals(our_tor_pid, all_tor_pids[0])
def test_get_pid_by_port(self): """ Checks general usage of the stem.util.system.get_pid_by_port function. """ runner = test.runner.get_runner() if stem.util.system.is_windows(): test.runner.skip(self, "(unavailable on windows)") return elif not _has_port(): test.runner.skip(self, "(test instance has no port)") return elif stem.util.system.is_mac(): test.runner.skip(self, "(resolvers unavailable)") return elif not runner.is_ptraceable(): test.runner.skip(self, "(DisableDebuggerAttachment is set)") return elif not (stem.util.system.is_available("netstat") or stem.util.system.is_available("sockstat") or stem.util.system.is_available("lsof")): test.runner.skip(self, "(connection resolvers unavailable)") return tor_pid, tor_port = runner.get_pid(), test.runner.CONTROL_PORT self.assertEquals(tor_pid, stem.util.system.get_pid_by_port(tor_port)) self.assertEquals(None, stem.util.system.get_pid_by_port(99999))
def test_cwd_lsof(self): """ Tests the pid_by_cwd function with a lsof response. """ runner = test.runner.get_runner() if not stem.util.system.is_available('lsof'): test.runner.skip(self, '(lsof unavailable)') return elif not runner.is_ptraceable(): test.runner.skip(self, '(DisableDebuggerAttachment is set)') return # filter the call function to only allow this command lsof_prefix = 'lsof -a -p ' call_replacement = filter_system_call([lsof_prefix]) with patch('stem.util.system.call') as call_mock: call_mock.side_effect = call_replacement runner_pid, tor_cwd = runner.get_pid(), runner.get_tor_cwd() self.assertEqual(tor_cwd, stem.util.system.cwd(runner_pid))
def test_get_cwd(self): """ Checks general usage of the stem.util.system.get_cwd function. """ runner = test.runner.get_runner() if not runner.is_ptraceable(): self.skipTest("(DisableDebuggerAttachment is set)") runner_pid, tor_cwd = runner.get_pid(), runner.get_tor_cwd() self.assertEquals(tor_cwd, stem.util.system.get_cwd(runner_pid)) self.assertEquals(None, stem.util.system.get_cwd(99999))
def test_get_pid_by_port(self): """ Checks general usage of the stem.util.system.get_pid_by_port function. """ runner = test.runner.get_runner() if not _has_port(): self.skipTest("(test instance has no port)") elif not runner.is_ptraceable(): self.skipTest("(DisableDebuggerAttachment is set)") tor_pid, tor_port = runner.get_pid(), test.runner.CONTROL_PORT self.assertEquals(tor_pid, stem.util.system.get_pid_by_port(tor_port)) self.assertEquals(None, stem.util.system.get_pid_by_port(99999))
def test_get_cwd(self): """ Checks general usage of the stem.util.system.get_cwd function. """ runner = test.runner.get_runner() if stem.util.system.is_windows(): test.runner.skip(self, "(unavailable on windows)") return elif not runner.is_ptraceable(): test.runner.skip(self, "(DisableDebuggerAttachment is set)") return runner_pid, tor_cwd = runner.get_pid(), runner.get_tor_cwd() self.assertEquals(tor_cwd, stem.util.system.get_cwd(runner_pid)) self.assertEquals(None, stem.util.system.get_cwd(99999))
def test_get_cwd_lsof(self): """ Tests the get_pid_by_cwd function with a lsof response. """ runner = test.runner.get_runner() if not stem.util.system.is_available("lsof"): self.skipTest("(lsof unavailable)") elif not runner.is_ptraceable(): self.skipTest("(DisableDebuggerAttachment is set)") # filter the call function to only allow this command lsof_prefix = "lsof -a -p " mocking.mock(stem.util.system.call, filter_system_call([lsof_prefix])) runner_pid, tor_cwd = runner.get_pid(), runner.get_tor_cwd() self.assertEquals(tor_cwd, stem.util.system.get_cwd(runner_pid))
def test_get_pid_by_port_lsof(self): """ Tests the get_pid_by_port function with a lsof response. """ runner = test.runner.get_runner() if not _has_port(): self.skipTest("(test instance has no port)") elif not stem.util.system.is_available("lsof"): self.skipTest("(lsof unavailable)") elif not runner.is_ptraceable(): self.skipTest("(DisableDebuggerAttachment is set)") lsof_prefix = stem.util.system.GET_PID_BY_PORT_LSOF mocking.mock(stem.util.system.call, filter_system_call([lsof_prefix])) tor_pid = test.runner.get_runner().get_pid() self.assertEquals(tor_pid, stem.util.system.get_pid_by_port(test.runner.CONTROL_PORT))
def test_get_pid_by_name_lsof(self): """ Tests the get_pid_by_name function with a lsof response. """ runner = test.runner.get_runner() if self.is_extra_tor_running: self.skipTest("(multiple tor instances)") elif not stem.util.system.is_available("lsof"): self.skipTest("(lsof unavailable)") elif not runner.is_ptraceable(): self.skipTest("(DisableDebuggerAttachment is set)") lsof_prefix = stem.util.system.GET_PID_BY_NAME_LSOF % "" mocking.mock(stem.util.system.call, filter_system_call([lsof_prefix])) tor_pid = test.runner.get_runner().get_pid() self.assertEquals(tor_pid, stem.util.system.get_pid_by_name("tor"))
def test_get_cwd_pwdx(self): """ Tests the get_pid_by_cwd function with a pwdx response. """ runner = test.runner.get_runner() if not stem.util.system.is_available("pwdx"): test.runner.skip(self, "(pwdx unavailable)") return elif not runner.is_ptraceable(): test.runner.skip(self, "(DisableDebuggerAttachment is set)") return # filter the call function to only allow this command pwdx_prefix = stem.util.system.GET_CWD_PWDX % "" mocking.mock(stem.util.system.call, filter_system_call([pwdx_prefix])) runner_pid, tor_cwd = runner.get_pid(), runner.get_tor_cwd() self.assertEquals(tor_cwd, stem.util.system.get_cwd(runner_pid))
def test_get_cwd_lsof(self): """ Tests the get_pid_by_cwd function with a lsof response. """ runner = test.runner.get_runner() if not stem.util.system.is_available("lsof"): test.runner.skip(self, "(lsof unavailable)") return elif not runner.is_ptraceable(): test.runner.skip(self, "(DisableDebuggerAttachment is set)") return # filter the call function to only allow this command lsof_prefix = "lsof -a -p " mocking.mock(stem.util.system.call, filter_system_call([lsof_prefix])) runner_pid, tor_cwd = runner.get_pid(), runner.get_tor_cwd() self.assertEquals(tor_cwd, stem.util.system.get_cwd(runner_pid))
def test_get_pid_by_port_sockstat(self): """ Tests the get_pid_by_port function with a sockstat response. """ runner = test.runner.get_runner() if not _has_port(): self.skipTest("(test instance has no port)") elif not stem.util.system.is_available("sockstat"): self.skipTest("(sockstat unavailable)") elif not stem.util.system.is_bsd(): self.skipTest("(bsd only)") elif not runner.is_ptraceable(): self.skipTest("(DisableDebuggerAttachment is set)") sockstat_prefix = stem.util.system.GET_PID_BY_PORT_SOCKSTAT % "" mocking.mock(stem.util.system.call, filter_system_call([sockstat_prefix])) tor_pid = test.runner.get_runner().get_pid() self.assertEquals(tor_pid, stem.util.system.get_pid_by_port(test.runner.CONTROL_PORT))
def test_get_pid_by_name_lsof(self): """ Tests the get_pid_by_name function with a lsof response. """ runner = test.runner.get_runner() if self.is_extra_tor_running: test.runner.skip(self, "(multiple tor instances)") return elif not stem.util.system.is_available("lsof"): test.runner.skip(self, "(lsof unavailable)") return elif not runner.is_ptraceable(): test.runner.skip(self, "(DisableDebuggerAttachment is set)") return lsof_prefix = stem.util.system.GET_PID_BY_NAME_LSOF % "" mocking.mock(stem.util.system.call, filter_system_call([lsof_prefix])) tor_pid = test.runner.get_runner().get_pid() self.assertEquals(tor_pid, stem.util.system.get_pid_by_name("tor"))
def check_resolver(self, resolver): runner = test.runner.get_runner() if test.runner.Torrc.PORT not in runner.get_options(): test.runner.skip(self, '(no control port)') return elif not runner.is_ptraceable(): test.runner.skip(self, '(DisableDebuggerAttachment set)') return elif resolver not in system_resolvers(): test.runner.skip(self, '(resolver unavailable on this platform)') return with runner.get_tor_socket(): connections = get_connections(resolver, process_pid=runner.get_pid()) for conn in connections: if conn.local_address == '127.0.0.1' and conn.local_port == test.runner.CONTROL_PORT: return self.fail('Unable to find localhost connection with %s:\n%s' % (resolver, '\n'.join(connections)))
def test_get_pid_by_port_netstat(self): """ Tests the get_pid_by_port function with a netstat response. """ runner = test.runner.get_runner() if not _has_port(): test.runner.skip(self, "(test instance has no port)") return elif not stem.util.system.is_available("netstat"): test.runner.skip(self, "(netstat unavailable)") return elif stem.util.system.is_bsd() or stem.util.system.is_windows(): test.runner.skip(self, "(linux only)") return elif not runner.is_ptraceable(): test.runner.skip(self, "(DisableDebuggerAttachment is set)") return netstat_prefix = stem.util.system.GET_PID_BY_PORT_NETSTAT mocking.mock(stem.util.system.call, filter_system_call([netstat_prefix])) tor_pid = test.runner.get_runner().get_pid() self.assertEquals(tor_pid, stem.util.system.get_pid_by_port(test.runner.CONTROL_PORT))