diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2020-11-10 01:20:09 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2020-11-10 01:20:09 +0100 |
commit | e2d2667e82b7ba384744fa28f0563f60433d3402 (patch) | |
tree | b54d241f4fb57a54dc3abe69d87ee0026bd2710a | |
parent | c6b2cfcc55bb6d5067d1f79ec12b0b5ab0fc9b8c (diff) | |
download | psutil-e2d2667e82b7ba384744fa28f0563f60433d3402.tar.gz |
move check_conn_addr() utility and reuse it
-rw-r--r-- | .github/workflows/build_wheel.yml | 2 | ||||
-rw-r--r-- | psutil/_psutil_windows.c | 2 | ||||
-rw-r--r-- | psutil/tests/__init__.py | 77 | ||||
-rwxr-xr-x | psutil/tests/test_connections.py | 101 | ||||
-rwxr-xr-x | psutil/tests/test_contracts.py | 2 |
5 files changed, 93 insertions, 91 deletions
diff --git a/.github/workflows/build_wheel.yml b/.github/workflows/build_wheel.yml index 68bb5d3b..f83ec4d4 100644 --- a/.github/workflows/build_wheel.yml +++ b/.github/workflows/build_wheel.yml @@ -17,7 +17,7 @@ jobs: CIBW_TEST_COMMAND_MACOS: LC_ALL='en_US.utf8' python -Wa {project}/psutil/tests/runner.py CIBW_TEST_EXTRAS: test # https://cibuildwheel.readthedocs.io/en/stable/options/ - CIBW_SKIP: cp35-* pp27-win32* + CIBW_SKIP: cp35-* pp*-win32* pp37* steps: - uses: actions/checkout@v2 - uses: actions/setup-python@v2 diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index c8b2f383..b836d708 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -211,7 +211,7 @@ psutil_proc_wait(PyObject *self, PyObject *args) { Py_RETURN_NONE; } else { - PyErr_SetFromWindowsErr(0); + PyErr_SetFromOSErrnoWithSyscall("OpenProcess"); return NULL; } } diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py index 897fed8b..7357450e 100644 --- a/psutil/tests/__init__.py +++ b/psutil/tests/__init__.py @@ -1557,6 +1557,83 @@ def check_net_address(addr, family): raise ValueError("unknown family %r", family) +def check_connection_ntuple(conn): + """Check validity of a connection namedtuple.""" + def check_ntuple(conn): + has_pid = len(conn) == 7 + assert len(conn) in (6, 7), len(conn) + assert conn[0] == conn.fd, conn.fd + assert conn[1] == conn.family, conn.family + assert conn[2] == conn.type, conn.type + assert conn[3] == conn.laddr, conn.laddr + assert conn[4] == conn.raddr, conn.raddr + assert conn[5] == conn.status, conn.status + if has_pid: + assert conn[6] == conn.pid, conn.pid + + def check_family(conn): + assert conn.family in (AF_INET, AF_INET6, AF_UNIX), conn.family + if enum is not None: + assert isinstance(conn.family, enum.IntEnum), conn + else: + assert isinstance(conn.family, int), conn + if conn.family == AF_INET: + # actually try to bind the local socket; ignore IPv6 + # sockets as their address might be represented as + # an IPv4-mapped-address (e.g. "::127.0.0.1") + # and that's rejected by bind() + s = socket.socket(conn.family, conn.type) + with contextlib.closing(s): + try: + s.bind((conn.laddr[0], 0)) + except socket.error as err: + if err.errno != errno.EADDRNOTAVAIL: + raise + elif conn.family == AF_UNIX: + assert conn.status == psutil.CONN_NONE, conn.status + + def check_type(conn): + # SOCK_SEQPACKET may happen in case of AF_UNIX socks + SOCK_SEQPACKET = getattr(socket, "SOCK_SEQPACKET", object()) + assert conn.type in (socket.SOCK_STREAM, socket.SOCK_DGRAM, + SOCK_SEQPACKET), conn.type + if enum is not None: + assert isinstance(conn.type, enum.IntEnum), conn + else: + assert isinstance(conn.type, int), conn + if conn.type == socket.SOCK_DGRAM: + assert conn.status == psutil.CONN_NONE, conn.status + + def check_addrs(conn): + # check IP address and port sanity + for addr in (conn.laddr, conn.raddr): + if conn.family in (AF_INET, AF_INET6): + assert isinstance(addr, tuple), type(addr) + if not addr: + continue + assert isinstance(addr.port, int), type(addr.port) + assert 0 <= addr.port <= 65535, addr.port + check_net_address(addr.ip, conn.family) + elif conn.family == AF_UNIX: + assert isinstance(addr, str), type(addr) + + def check_status(conn): + assert isinstance(conn.status, str), conn.status + valids = [getattr(psutil, x) for x in dir(psutil) + if x.startswith('CONN_')] + assert conn.status in valids, conn.status + if conn.family in (AF_INET, AF_INET6) and conn.type == SOCK_STREAM: + assert conn.status != psutil.CONN_NONE, conn.status + else: + assert conn.status == psutil.CONN_NONE, conn.status + + check_ntuple(conn) + check_family(conn) + check_type(conn) + check_addrs(conn) + check_status(conn) + + # =================================================================== # --- compatibility # =================================================================== diff --git a/psutil/tests/test_connections.py b/psutil/tests/test_connections.py index 7fe200da..3168cdc1 100755 --- a/psutil/tests/test_connections.py +++ b/psutil/tests/test_connections.py @@ -6,8 +6,6 @@ """Tests for net_connections() and Process.connections() APIs.""" -import contextlib -import errno import os import socket import sys @@ -32,10 +30,9 @@ from psutil._compat import PY3 from psutil.tests import AF_UNIX from psutil.tests import bind_socket from psutil.tests import bind_unix_socket -from psutil.tests import check_net_address +from psutil.tests import check_connection_ntuple from psutil.tests import CIRRUS from psutil.tests import create_sockets -from psutil.tests import enum from psutil.tests import get_free_port from psutil.tests import HAS_CONNECTIONS_UNIX from psutil.tests import PsutilTestCase @@ -57,7 +54,7 @@ PYTHON_39 = sys.version_info[:2] == (3, 9) @serialrun -class _ConnTestCase(PsutilTestCase): +class ConnectionTestCase(PsutilTestCase): def setUp(self): if not (NETBSD or FREEBSD): @@ -92,93 +89,19 @@ class _ConnTestCase(PsutilTestCase): proc_cons.sort() self.assertEqual(proc_cons, sys_cons) - def check_connection_ntuple(self, conn): - """Check validity of a connection namedtuple.""" - def check_ntuple(conn): - has_pid = len(conn) == 7 - self.assertIn(len(conn), (6, 7)) - self.assertEqual(conn[0], conn.fd) - self.assertEqual(conn[1], conn.family) - self.assertEqual(conn[2], conn.type) - self.assertEqual(conn[3], conn.laddr) - self.assertEqual(conn[4], conn.raddr) - self.assertEqual(conn[5], conn.status) - if has_pid: - self.assertEqual(conn[6], conn.pid) - - def check_family(conn): - self.assertIn(conn.family, (AF_INET, AF_INET6, AF_UNIX)) - if enum is not None: - assert isinstance(conn.family, enum.IntEnum), conn - else: - assert isinstance(conn.family, int), conn - if conn.family == AF_INET: - # actually try to bind the local socket; ignore IPv6 - # sockets as their address might be represented as - # an IPv4-mapped-address (e.g. "::127.0.0.1") - # and that's rejected by bind() - s = socket.socket(conn.family, conn.type) - with contextlib.closing(s): - try: - s.bind((conn.laddr[0], 0)) - except socket.error as err: - if err.errno != errno.EADDRNOTAVAIL: - raise - elif conn.family == AF_UNIX: - self.assertEqual(conn.status, psutil.CONN_NONE) - - def check_type(conn): - # SOCK_SEQPACKET may happen in case of AF_UNIX socks - self.assertIn(conn.type, (SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET)) - if enum is not None: - assert isinstance(conn.type, enum.IntEnum), conn - else: - assert isinstance(conn.type, int), conn - if conn.type == SOCK_DGRAM: - self.assertEqual(conn.status, psutil.CONN_NONE) - - def check_addrs(conn): - # check IP address and port sanity - for addr in (conn.laddr, conn.raddr): - if conn.family in (AF_INET, AF_INET6): - self.assertIsInstance(addr, tuple) - if not addr: - continue - self.assertIsInstance(addr.port, int) - assert 0 <= addr.port <= 65535, addr.port - check_net_address(addr.ip, conn.family) - elif conn.family == AF_UNIX: - self.assertIsInstance(addr, str) - - def check_status(conn): - self.assertIsInstance(conn.status, str) - valids = [getattr(psutil, x) for x in dir(psutil) - if x.startswith('CONN_')] - self.assertIn(conn.status, valids) - if conn.family in (AF_INET, AF_INET6) and conn.type == SOCK_STREAM: - self.assertNotEqual(conn.status, psutil.CONN_NONE) - else: - self.assertEqual(conn.status, psutil.CONN_NONE) - - check_ntuple(conn) - check_family(conn) - check_type(conn) - check_addrs(conn) - check_status(conn) - -class TestBasicOperations(_ConnTestCase): +class TestBasicOperations(ConnectionTestCase): @unittest.skipIf(SKIP_SYSCONS, "requires root") def test_system(self): with create_sockets(): for conn in psutil.net_connections(kind='all'): - self.check_connection_ntuple(conn) + check_connection_ntuple(conn) def test_process(self): with create_sockets(): for conn in psutil.Process().connections(kind='all'): - self.check_connection_ntuple(conn) + check_connection_ntuple(conn) def test_invalid_kind(self): self.assertRaises(ValueError, thisproc.connections, kind='???') @@ -186,7 +109,7 @@ class TestBasicOperations(_ConnTestCase): @serialrun -class TestUnconnectedSockets(_ConnTestCase): +class TestUnconnectedSockets(ConnectionTestCase): """Tests sockets which are open but not connected to anything.""" def get_conn_from_sock(self, sock): @@ -208,7 +131,7 @@ class TestUnconnectedSockets(_ConnTestCase): only (the one supposed to be checked). """ conn = self.get_conn_from_sock(sock) - self.check_connection_ntuple(conn) + check_connection_ntuple(conn) # fd, family, type if conn.fd != -1: @@ -285,7 +208,7 @@ class TestUnconnectedSockets(_ConnTestCase): @serialrun -class TestConnectedSocket(_ConnTestCase): +class TestConnectedSocket(ConnectionTestCase): """Test socket pairs which are are actually connected to each other. """ @@ -349,7 +272,7 @@ class TestConnectedSocket(_ConnTestCase): client.close() -class TestFilters(_ConnTestCase): +class TestFilters(ConnectionTestCase): def test_filters(self): def check(kind, families, types): @@ -401,7 +324,7 @@ class TestFilters(_ConnTestCase): def check_conn(proc, conn, family, type, laddr, raddr, status, kinds): all_kinds = ("all", "inet", "inet4", "inet6", "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6") - self.check_connection_ntuple(conn) + check_connection_ntuple(conn) self.assertEqual(conn.family, family) self.assertEqual(conn.type, type) self.assertEqual(conn.laddr, laddr) @@ -551,7 +474,7 @@ class TestFilters(_ConnTestCase): @unittest.skipIf(SKIP_SYSCONS, "requires root") -class TestSystemWideConnections(_ConnTestCase): +class TestSystemWideConnections(ConnectionTestCase): """Tests for net_connections().""" def test_it(self): @@ -560,7 +483,7 @@ class TestSystemWideConnections(_ConnTestCase): self.assertIn(conn.family, families, msg=conn) if conn.family != AF_UNIX: self.assertIn(conn.type, types_, msg=conn) - self.check_connection_ntuple(conn) + check_connection_ntuple(conn) with create_sockets(): from psutil._common import conn_tmap diff --git a/psutil/tests/test_contracts.py b/psutil/tests/test_contracts.py index 62ffbfec..d28fb449 100755 --- a/psutil/tests/test_contracts.py +++ b/psutil/tests/test_contracts.py @@ -33,6 +33,7 @@ from psutil._compat import FileNotFoundError from psutil._compat import long from psutil._compat import range from psutil.tests import APPVEYOR +from psutil.tests import check_connection_ntuple from psutil.tests import create_sockets from psutil.tests import enum from psutil.tests import GITHUB_WHEELS @@ -628,6 +629,7 @@ class TestFetchAllProcesses(PsutilTestCase): self.assertEqual(len(ret), len(set(ret))) for conn in ret: assert is_namedtuple(conn) + check_connection_ntuple(conn) def cwd(self, ret, info): if ret: # 'ret' can be None or empty |