[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [stem/master] Fixed tests
commit c24c6b7bb915d5c892cc6b9f58a2db704757446b
Author: icodemachine <gauthamnekk@xxxxxxxxx>
Date: Tue Feb 17 14:02:42 2015 +0530
Fixed tests
---
stem/util/connection.py | 24 ++++++++++++------------
stem/util/system.py | 37 +++++++++++++++++--------------------
test/integ/test - Shortcut.lnk | Bin 0 -> 1155 bytes
test/integ/util/connection.py | 4 ++--
test/integ/util/system.py | 4 +---
test/unit/util/connection.py | 5 ++---
test/unit/util/system.py | 18 ++++++++++--------
7 files changed, 44 insertions(+), 48 deletions(-)
diff --git a/stem/util/connection.py b/stem/util/connection.py
index 93e137a..c863d97 100644
--- a/stem/util/connection.py
+++ b/stem/util/connection.py
@@ -31,7 +31,7 @@ Connection and networking based utility functions.
Resolver Description
================= ===========
**PROC** /proc contents
- **NETSTAT** netstat
+ **NETSTAT** netstat
**NETSTAT_WINDOWS** netstat command under Windows
**SS** ss command
**LSOF** lsof command
@@ -93,9 +93,9 @@ RESOLVER_COMMAND = {
# -n = prevents dns lookups, -p = include process
Resolver.NETSTAT: 'netstat -np',
- #-ano is a Windows variant for netstat including pid
+ # -ano is a Windows variant for netstat including pid
Resolver.NETSTAT_WINDOWS: 'netstat -ano',
-
+
# -n = numeric ports, -p = include process, -t = tcp sockets, -u = udp sockets
Resolver.SS: 'ss -nptu',
@@ -117,10 +117,10 @@ RESOLVER_FILTER = {
# tcp 0 586 192.168.0.1:44284 38.229.79.2:443 ESTABLISHED 15843/tor
Resolver.NETSTAT: '^{protocol}\s+.*\s+{local_address}:{local_port}\s+{remote_address}:{remote_port}\s+ESTABLISHED\s+{pid}/{name}\s*$',
-
+
# tcp 586 192.168.0.1:44284 38.229.79.2:443 ESTABLISHED 15843
-
- Resolver.NETSTAT_WINDOWS: '^\s*{protocol}\s+{local_address}:{local_port}\s+{remote_address}:{remote_port}\s+ESTABLISHED\s+{pid}\s*$',
+
+ Resolver.NETSTAT_WINDOWS: '^\s*{protocol}\s+{local_address}:{local_port}\s+{remote_address}:{remote_port}\s+ESTABLISHED\s+{pid}\s*$',
# tcp ESTAB 0 0 192.168.0.20:44415 38.229.79.2:443 users:(("tor",15843,9))
Resolver.SS: '^{protocol}\s+ESTAB\s+.*\s+{local_address}:{local_port}\s+{remote_address}:{remote_port}\s+users:\(\("{name}",{pid},[0-9]+\)\)$',
@@ -187,17 +187,17 @@ def get_connections(resolver, process_pid = None, process_name = None):
return [Connection(*conn) for conn in stem.util.proc.connections(process_pid)]
resolver_command = RESOLVER_COMMAND[resolver].format(pid = process_pid)
-
- #In case, process_name is only specified
+
+ # In case, process_name is only specified
if resolver == Resolver.NETSTAT_WINDOWS:
if not process_pid and process_name:
- process_pid = stem.util.system.pid_by_name(process_name)[0]
-
+ process_pid = stem.util.system.pid_by_name(process_name)
+
try:
results = stem.util.system.call(resolver_command)
except OSError as exc:
raise IOError("Unable to query '%s': %s" % (resolver_command, exc))
-
+
resolver_regex_str = RESOLVER_FILTER[resolver].format(
protocol = '(?P<protocol>\S+)',
local_address = '(?P<local_address>[0-9.]+)',
@@ -649,4 +649,4 @@ def _cryptovariables_equal(x, y):
# We renamed our methods to drop a redundant 'get_*' prefix, so alias the old
# names for backward compatability.
-get_system_resolvers = system_resolvers
\ No newline at end of file
+get_system_resolvers = system_resolvers
diff --git a/stem/util/system.py b/stem/util/system.py
index 98b64f7..efe8663 100644
--- a/stem/util/system.py
+++ b/stem/util/system.py
@@ -79,7 +79,7 @@ GET_PID_BY_NAME_PIDOF = 'pidof %s'
GET_PID_BY_NAME_PS_LINUX = 'ps -o pid -C %s'
GET_PID_BY_NAME_PS_BSD = 'ps axc'
GET_PID_BY_NAME_LSOF = 'lsof -tc %s'
-GET_PID_BY_NAME_TASKLIST = 'tasklist | findstr %s.exe'
+GET_PID_BY_NAME_TASKLIST = 'tasklist | findstr %s'
GET_PID_BY_PORT_NETSTAT = 'netstat -npltu'
GET_PID_BY_PORT_SOCKSTAT = 'sockstat -4l -P tcp -p %s'
GET_PID_BY_PORT_LSOF = 'lsof -wnP -iTCP -sTCP:LISTEN'
@@ -304,7 +304,7 @@ def pid_by_name(process_name, multiple = False):
3. ps -o pid -C <name> (linux)
ps axc | egrep " <name>$" (bsd)
4. lsof -tc <name>
- 5. tasklist | str <name>.exe
+ 5. tasklist | str <name>.exe
:param str process_name: process name for which to fetch the pid
:param bool multiple: provides a list of all pids if **True**, otherwise
@@ -438,51 +438,48 @@ def pid_by_name(process_name, multiple = False):
except ValueError:
pass
- #Calling and Parsing tasklist command on Windows (Because call method doesn't work properly with it)
- #Process name may or may not include .exe
-
- if is_available('netstat -ano'):
-
+ # Calling and Parsing tasklist command on Windows (Because call method doesn't work properly with it)
+ # Process name may or may not include .exe
+ if is_available('netstat -ano') and is_windows():
+
if process_name.find(".exe") == -1:
- process_name = process_name + '.exe'
-
+ process_name = process_name + '.exe'
+
command = GET_PID_BY_NAME_TASKLIST % process_name
process_ids = []
-
+
try:
results = stem.util.system.call('tasklist')
tasklist_regex_str = '^\s*' + process_name + '\s+(?P<pid>[0-9]*)'
tasklist_regex = re.compile(tasklist_regex_str)
-
+
if not results:
raise IOError("No results found for tasklist")
-
+
for line in results:
match = tasklist_regex.search(line)
if match:
attr = match.groupdict()
id = int(attr['pid'])
process_ids.append(id)
-
- if process_ids == []:
- raise IOError("Process Name not Found : %s" % process_name)
-
+
if multiple:
return process_ids
elif len(process_ids) > 0:
return process_ids[0]
-
+
except OSError as exc:
log.debug("failed to query '%s': %s" % (command, exc))
- raise IOError("Unable to query '%s': %s" % (command, exc))
-
+ raise IOError("Unable to query '%s': %s" % (command, exc))
log.debug("failed to resolve a pid for '%s'" % process_name)
return [] if multiple else None
def pid_by_port(port):
+
"""
+
Attempts to determine the process id for a process with the given port,
using...
@@ -1140,4 +1137,4 @@ get_cwd = cwd
get_user = user
get_start_time = start_time
get_bsd_jail_id = bsd_jail_id
-get_bsd_jail_path = bsd_jail_path
\ No newline at end of file
+get_bsd_jail_path = bsd_jail_path
diff --git a/test/integ/test - Shortcut.lnk b/test/integ/test - Shortcut.lnk
new file mode 100644
index 0000000..f8568cd
Binary files /dev/null and b/test/integ/test - Shortcut.lnk differ
diff --git a/test/integ/util/connection.py b/test/integ/util/connection.py
index 391869e..64653d1 100644
--- a/test/integ/util/connection.py
+++ b/test/integ/util/connection.py
@@ -40,7 +40,7 @@ class TestConnection(unittest.TestCase):
self.check_resolver(Resolver.NETSTAT)
def test_get_connections_by_windows_netstat(self):
- self.check_resolver(Resolver.NETSTAT_WINDOWS)
+ self.check_resolver(Resolver.NETSTAT_WINDOWS)
def test_get_connections_by_ss(self):
self.check_resolver(Resolver.SS)
@@ -64,7 +64,7 @@ class TestConnection(unittest.TestCase):
recognized_resolvers = (
Resolver.PROC,
Resolver.NETSTAT,
- Resolver.NETSTAT_WINDOWS,
+ Resolver.NETSTAT_WINDOWS,
Resolver.SS,
Resolver.LSOF,
Resolver.SOCKSTAT,
diff --git a/test/integ/util/system.py b/test/integ/util/system.py
index 3971e61..aa18f45 100644
--- a/test/integ/util/system.py
+++ b/test/integ/util/system.py
@@ -215,7 +215,7 @@ class TestSystem(unittest.TestCase):
if len(all_tor_pids) == 1:
self.assertEqual(our_tor_pid, all_tor_pids[0])
-
+
def test_pid_by_name_tasklist(self):
"""
Tests the pid_by_name function with a tasklist response.
@@ -233,12 +233,10 @@ class TestSystem(unittest.TestCase):
tor_cmd = test.runner.get_runner().get_tor_command(True)
self.assertEqual(tor_pid, stem.util.system.pid_by_name(tor_cmd))
-
def test_pid_by_port(self):
"""
Checks general usage of the stem.util.system.pid_by_port function.
"""
-
runner = test.runner.get_runner()
if stem.util.system.is_windows():
test.runner.skip(self, '(unavailable on windows)')
diff --git a/test/unit/util/connection.py b/test/unit/util/connection.py
index 233cb29..eba0f03 100644
--- a/test/unit/util/connection.py
+++ b/test/unit/util/connection.py
@@ -204,12 +204,11 @@ class TestConnection(unittest.TestCase):
expected = [Connection('192.168.0.1', 44284, '38.229.79.2', 443, 'tcp')]
self.assertEqual(expected, stem.util.connection.get_connections(Resolver.NETSTAT_WINDOWS, process_pid = 15843, process_name = 'tor'))
- #self.assertRaises(IOError, stem.util.connection.get_connections, Resolver.NETSTAT, process_pid = 15843, process_name = 'stuff')
self.assertRaises(IOError, stem.util.connection.get_connections, Resolver.NETSTAT_WINDOWS, process_pid = 1111, process_name = 'tor')
-
call_mock.side_effect = OSError('Unable to call netstat')
+
self.assertRaises(IOError, stem.util.connection.get_connections, Resolver.NETSTAT_WINDOWS, process_pid = 1111)
-
+
@patch('stem.util.system.call')
def test_get_connections_by_ss(self, call_mock):
"""
diff --git a/test/unit/util/system.py b/test/unit/util/system.py
index 94bb7fa..b733769 100644
--- a/test/unit/util/system.py
+++ b/test/unit/util/system.py
@@ -52,9 +52,9 @@ GET_PID_BY_NAME_TASKLIST_RESULTS = [
'svchost.exe 872 Services 0 8,744 K',
'hpservice.exe 1112 Services 0 3,828 K',
'tor.exe 3712 Console 1 29,976 K',
- 'tor.exe 3713 Console 1 21,976 K',
+ 'tor.exe 3713 Console 1 21,976 K',
'conhost.exe 3012 Console 1 4,652 K']
-
+
GET_PID_BY_PORT_NETSTAT_RESULTS = [
'Active Internet connections (only servers)',
'Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name',
@@ -63,7 +63,7 @@ GET_PID_BY_PORT_NETSTAT_RESULTS = [
'tcp6 0 0 ::1:631 :::* LISTEN - ',
'udp 0 0 0.0.0.0:5353 0.0.0.0:* - ',
'udp6 0 0 fe80::7ae4:ff:fe2f::123 :::* - ']
-
+
GET_PID_BY_PORT_SOCKSTAT_RESULTS = [
'_tor tor 4397 7 tcp4 51.64.7.84:9051 *:*',
'_tor tor 4397 12 tcp4 51.64.7.84:54011 80.3.121.7:9051',
@@ -172,6 +172,7 @@ class TestSystem(unittest.TestCase):
@patch('stem.util.system.call')
@patch('stem.util.system.is_available', Mock(return_value = True))
+ @patch('stem.util.system.is_windows', Mock(return_value = False))
def test_pid_by_name_pgrep(self, call_mock):
"""
Tests the pid_by_name function with pgrep responses.
@@ -190,6 +191,7 @@ class TestSystem(unittest.TestCase):
@patch('stem.util.system.call')
@patch('stem.util.system.is_available', Mock(return_value = True))
+ @patch('stem.util.system.is_windows', Mock(return_value = False))
def test_pid_by_name_pidof(self, call_mock):
"""
Tests the pid_by_name function with pidof responses.
@@ -208,6 +210,7 @@ class TestSystem(unittest.TestCase):
@patch('stem.util.system.call')
@patch('stem.util.system.is_bsd', Mock(return_value = False))
+ @patch('stem.util.system.is_windows', Mock(return_value = False))
@patch('stem.util.system.is_available', Mock(return_value = True))
def test_pid_by_name_ps_linux(self, call_mock):
"""
@@ -227,6 +230,7 @@ class TestSystem(unittest.TestCase):
@patch('stem.util.system.call')
@patch('stem.util.system.is_bsd', Mock(return_value = True))
+ @patch('stem.util.system.is_windows', Mock(return_value = False))
@patch('stem.util.system.is_available', Mock(return_value = True))
def test_pid_by_name_ps_bsd(self, call_mock):
"""
@@ -244,6 +248,7 @@ class TestSystem(unittest.TestCase):
@patch('stem.util.system.call')
@patch('stem.util.system.is_available', Mock(return_value = True))
+ @patch('stem.util.system.is_windows', Mock(return_value = False))
def test_pid_by_name_lsof(self, call_mock):
"""
Tests the pid_by_name function with lsof responses.
@@ -267,13 +272,12 @@ class TestSystem(unittest.TestCase):
Tests the pid_by_name function with tasklist responses.
"""
- call_mock.return_value = GET_PID_BY_NAME_TASKLIST_RESULTS
+ call_mock.return_value = GET_PID_BY_NAME_TASKLIST_RESULTS
self.assertEqual(3712, system.pid_by_name('tor'))
self.assertEqual(None, system.pid_by_name('DirectoryService'))
self.assertEqual(None, system.pid_by_name('blarg'))
-
self.assertEqual([3712, 3713], system.pid_by_name('tor', multiple = True))
-
+
@patch('stem.util.system.call')
@patch('stem.util.system.is_available', Mock(return_value = True))
def test_pid_by_port_netstat(self, call_mock):
@@ -293,7 +297,6 @@ class TestSystem(unittest.TestCase):
"""
Tests the pid_by_port function with a sockstat response.
"""
-
call_mock.side_effect = mock_call(system.GET_PID_BY_PORT_SOCKSTAT % 9051, GET_PID_BY_PORT_SOCKSTAT_RESULTS)
self.assertEqual(4397, system.pid_by_port(9051))
self.assertEqual(4397, system.pid_by_port('9051'))
@@ -334,7 +337,6 @@ class TestSystem(unittest.TestCase):
"""
Tests the cwd function with a pwdx response.
"""
-
responses = {
'3799': ['3799: /home/atagar'],
'5839': ['5839: No such process'],
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits