def test_get_connections(self, open_mock, readlink_mock, listdir_mock): """ Tests the get_connections function. """ pid = 1111 listdir_mock.side_effect = lambda param: { '/proc/%s/fd' % pid: ['1', '2', '3', '4'], }[param] readlink_mock.side_effect = lambda param: { '/proc/%s/fd/1' % pid: 'socket:[99999999]', '/proc/%s/fd/2' % pid: 'socket:[IIIIIIII]', '/proc/%s/fd/3' % pid: 'pipe:[30303]', '/proc/%s/fd/4' % pid: 'pipe:[40404]', }[param] tcp = '\n 0: 11111111:1111 22222222:2222 01 44444444:44444444 55:55555555 66666666 1111 8 99999999' udp = '\n A: BBBBBBBB:BBBB CCCCCCCC:CCCC DD EEEEEEEE:EEEEEEEE FF:FFFFFFFF GGGGGGGG 1111 H IIIIIIII' open_mock.side_effect = lambda param: { '/proc/net/tcp': StringIO.StringIO(tcp), '/proc/net/udp': StringIO.StringIO(udp) }[param] # tests the edge case of pid = 0 self.assertEquals([], proc.get_connections(0)) expected_results = [ ('17.17.17.17', 4369, '34.34.34.34', 8738, 'tcp'), ('187.187.187.187', 48059, '204.204.204.204', 52428, 'udp'), ] self.assertEquals(expected_results, proc.get_connections(pid))
def test_get_connections(self): """ Tests the get_connections function. """ pid = 1111 mocking.mock(os.listdir, mocking.return_for_args({ ('/proc/%s/fd' % pid,): ['1', '2', '3', '4'], }), os) mocking.mock(os.readlink, mocking.return_for_args({ ('/proc/%s/fd/1' % pid,): 'socket:[99999999]', ('/proc/%s/fd/2' % pid,): 'socket:[IIIIIIII]', ('/proc/%s/fd/3' % pid,): 'pipe:[30303]', ('/proc/%s/fd/4' % pid,): 'pipe:[40404]', }), os) tcp = '\n 0: 11111111:1111 22222222:2222 01 44444444:44444444 55:55555555 66666666 1111 8 99999999' udp = '\n A: BBBBBBBB:BBBB CCCCCCCC:CCCC DD EEEEEEEE:EEEEEEEE FF:FFFFFFFF GGGGGGGG 1111 H IIIIIIII' mocking.mock(open, mocking.return_for_args({ ('/proc/net/tcp',): StringIO.StringIO(tcp), ('/proc/net/udp',): StringIO.StringIO(udp) })) # tests the edge case of pid = 0 self.assertEquals([], proc.get_connections(0)) expected_results = [ ('17.17.17.17', 4369, '34.34.34.34', 8738), ('187.187.187.187', 48059, '204.204.204.204', 52428), ] self.assertEquals(expected_results, proc.get_connections(pid))
def test_get_connections(self): """ Checks for our control port in the stem.util.proc.get_connections output if we have one. """ runner = test.runner.get_runner() if not proc.is_available(): test.runner.skip(self, "(proc unavailable)") return elif not test.runner.Torrc.PORT in runner.get_options(): test.runner.skip(self, "(no control port)") return elif not test.runner.get_runner().is_ptraceable(): test.runner.skip(self, "(DisableDebuggerAttachment is set)") return # making a controller connection so that we have something to query for with runner.get_tor_socket(): tor_pid = test.runner.get_runner().get_pid() for conn in proc.get_connections(tor_pid): if ("127.0.0.1", test.runner.CONTROL_PORT) == conn[:2]: return self.fail()
def getConnections(resolutionCmd, processName, processPid = ""): """ Retrieves a list of the current connections for a given process, providing a tuple list of the form: [(local_ipAddr1, local_port1, foreign_ipAddr1, foreign_port1), ...] this raises an IOError if no connections are available or resolution fails (in most cases these appear identical). Common issues include: - insufficient permissions - resolution command is unavailable - usage of the command is non-standard (particularly an issue for BSD) Arguments: resolutionCmd - command to use in resolving the address processName - name of the process for which connections are fetched processPid - process ID (this helps improve accuracy) """ if resolutionCmd == Resolver.PROC: # Attempts resolution via checking the proc contents. if not processPid: raise ValueError("proc resolution requires a pid") try: return proc.get_connections(processPid) except Exception, exc: raise IOError(str(exc))
def test_get_connections(self): """ Tests the get_connections function. """ pid = 1111 mocking.mock(os.listdir, mocking.return_for_args({ ('/proc/%s/fd' % pid,): ['1', '2', '3', '4'], }), os) mocking.mock(os.readlink, mocking.return_for_args({ ('/proc/%s/fd/1' % pid,): 'socket:[99999999]', ('/proc/%s/fd/2' % pid,): 'socket:[IIIIIIII]', ('/proc/%s/fd/3' % pid,): 'pipe:[30303]', ('/proc/%s/fd/4' % pid,): 'pipe:[40404]', }), os) tcp = '\n 0: 11111111:1111 22222222:2222 01 44444444:44444444 55:55555555 66666666 1111 8 99999999' udp = '\n A: BBBBBBBB:BBBB CCCCCCCC:CCCC DD EEEEEEEE:EEEEEEEE FF:FFFFFFFF GGGGGGGG 1111 H IIIIIIII' if stem.prereq.is_python_3(): import builtins mocking.mock(builtins.open, mocking.return_for_args({ ('/proc/net/tcp',): StringIO.StringIO(tcp), ('/proc/net/udp',): StringIO.StringIO(udp) }), builtins) else: mocking.mock(open, mocking.return_for_args({ ('/proc/net/tcp',): StringIO.StringIO(tcp), ('/proc/net/udp',): StringIO.StringIO(udp) })) # tests the edge case of pid = 0 self.assertEquals([], proc.get_connections(0)) expected_results = [ ('17.17.17.17', 4369, '34.34.34.34', 8738), ('187.187.187.187', 48059, '204.204.204.204', 52428), ] self.assertEquals(expected_results, proc.get_connections(pid))