def test_bind_unix_socket(self): name = self.get_testfn() sock = bind_unix_socket(name) with contextlib.closing(sock): self.assertEqual(sock.family, socket.AF_UNIX) self.assertEqual(sock.type, socket.SOCK_STREAM) self.assertEqual(sock.getsockname(), name) assert os.path.exists(name) assert stat.S_ISSOCK(os.stat(name).st_mode) # UDP name = self.get_testfn() sock = bind_unix_socket(name, type=socket.SOCK_DGRAM) with contextlib.closing(sock): self.assertEqual(sock.type, socket.SOCK_DGRAM)
def test_bind_unix_socket(self): with unix_socket_path() as name: sock = bind_unix_socket(name) with contextlib.closing(sock): self.assertEqual(sock.family, socket.AF_UNIX) self.assertEqual(sock.type, socket.SOCK_STREAM) self.assertEqual(sock.getsockname(), name) assert os.path.exists(name) assert stat.S_ISSOCK(os.stat(name).st_mode) # UDP with unix_socket_path() as name: sock = bind_unix_socket(name, type=socket.SOCK_DGRAM) with contextlib.closing(sock): self.assertEqual(sock.type, socket.SOCK_DGRAM)
def test_net_connections(self): with unix_socket_path() as name: with closing(bind_unix_socket(name)): cons = psutil.net_connections(kind='unix') assert cons for conn in cons: self.assertIsInstance(conn.laddr, str)
def create_sockets(self): """Open as many socket families / types as possible. This is needed to excercise as many C code sections as possible for net_connections() functions. The returned sockets are already scheduled for cleanup. """ socks = [] try: socks.append(bind_socket(socket.AF_INET, socket.SOCK_STREAM)) socks.append(bind_socket(socket.AF_INET, socket.SOCK_DGRAM)) if supports_ipv6(): socks.append(bind_socket(socket.AF_INET6, socket.SOCK_STREAM)) socks.append(bind_socket(socket.AF_INET6, socket.SOCK_DGRAM)) if POSIX and not SUNOS: # TODO: SunOS name1 = unix_socket_path().__enter__() name2 = unix_socket_path().__enter__() s1, s2 = unix_socketpair(name1) s3 = bind_unix_socket(name2, type=socket.SOCK_DGRAM) self.addCleanup(safe_rmpath, name1) self.addCleanup(safe_rmpath, name2) for s in (s1, s2, s3): socks.append(s) return socks finally: for s in socks: self.addCleanup(s.close)
def test_net_connections(self): with unix_socket_path() as name: with closing(bind_unix_socket(name)): cons = psutil.net_connections(kind='unix') assert cons for conn in cons: self.assertIsInstance(conn.laddr, str)
def test_proc_connections(self): suffix = os.path.basename(self.funky_name) with unix_socket_path(suffix=suffix) as name: try: sock = bind_unix_socket(name) except UnicodeEncodeError: if PY3: raise else: raise unittest.SkipTest("not supported") with closing(sock): conn = psutil.Process().connections('unix')[0] self.assertEqual(conn.laddr, name)
def test_proc_connections(self): name = self.get_testfn(suffix=self.funky_suffix) try: sock = bind_unix_socket(name) except UnicodeEncodeError: if PY3: raise else: raise unittest.SkipTest("not supported") with closing(sock): conn = psutil.Process().connections('unix')[0] self.assertIsInstance(conn.laddr, str) # AF_UNIX addr not set on OpenBSD if not OPENBSD: # XXX self.assertEqual(conn.laddr, name)
def test_proc_connections(self): suffix = os.path.basename(self.funky_name) with unix_socket_path(suffix=suffix) as name: try: sock = bind_unix_socket(name) except UnicodeEncodeError: if PY3: raise else: raise unittest.SkipTest("not supported") with closing(sock): conn = psutil.Process().connections('unix')[0] self.assertIsInstance(conn.laddr, str) # AF_UNIX addr not set on OpenBSD if not OPENBSD: self.assertEqual(conn.laddr, name)
def test_proc_connections(self): suffix = os.path.basename(self.funky_name) with unix_socket_path(suffix=suffix) as name: try: sock = bind_unix_socket(name) except UnicodeEncodeError: if PY3: raise else: raise unittest.SkipTest("not supported") with closing(sock): conn = psutil.Process().connections('unix')[0] self.assertIsInstance(conn.laddr, str) # AF_UNIX addr not set on OpenBSD if not OPENBSD and not CIRRUS: # XXX self.assertEqual(conn.laddr, name)
def test_net_connections(self): def find_sock(cons): for conn in cons: if os.path.basename(conn.laddr).startswith(TESTFILE_PREFIX): return conn raise ValueError("connection not found") suffix = os.path.basename(self.funky_name) with unix_socket_path(suffix=suffix) as name: try: sock = bind_unix_socket(name) except UnicodeEncodeError: if PY3: raise else: raise unittest.SkipTest("not supported") with closing(sock): cons = psutil.net_connections(kind='unix') conn = find_sock(cons) self.assertEqual(conn.laddr, name)
def test_net_connections(self): def find_sock(cons): for conn in cons: if os.path.basename(conn.laddr).startswith(TESTFN_PREFIX): return conn raise ValueError("connection not found") name = self.get_testfn(suffix=self.funky_suffix) try: sock = bind_unix_socket(name) except UnicodeEncodeError: if PY3: raise else: raise unittest.SkipTest("not supported") with closing(sock): cons = psutil.net_connections(kind='unix') # AF_UNIX addr not set on OpenBSD if not OPENBSD: conn = find_sock(cons) self.assertIsInstance(conn.laddr, str) self.assertEqual(conn.laddr, name)
def test_connections(self): def create_socket(family, type): sock = socket.socket(family, type) sock.bind(('', 0)) if type == socket.SOCK_STREAM: sock.listen(1) return sock # Open as many socket types as possible so that we excercise # as many C code sections as possible. socks = [] socks.append(create_socket(socket.AF_INET, socket.SOCK_STREAM)) socks.append(create_socket(socket.AF_INET, socket.SOCK_DGRAM)) if supports_ipv6(): socks.append(create_socket(socket.AF_INET6, socket.SOCK_STREAM)) socks.append(create_socket(socket.AF_INET6, socket.SOCK_DGRAM)) if POSIX and not SUNOS: # TODO: SunOS name1 = unix_socket_path().__enter__() name2 = unix_socket_path().__enter__() s1, s2 = unix_socketpair(name1) s3 = bind_unix_socket(name2, type=socket.SOCK_DGRAM) self.addCleanup(safe_rmpath, name1) self.addCleanup(safe_rmpath, name2) for s in (s1, s2, s3): socks.append(s) # TODO: UNIX sockets are temporarily implemented by parsing # 'pfiles' cmd output; we don't want that part of the code to # be executed. kind = 'inet' if SUNOS else 'all' # Make sure we did a proper setup. self.assertEqual( len(psutil.Process().connections(kind=kind)), len(socks)) try: self.execute(self.proc.connections, kind) finally: for s in socks: s.close()
def test_net_connections(self): def find_sock(cons): for conn in cons: if os.path.basename(conn.laddr).startswith(TESTFILE_PREFIX): return conn raise ValueError("connection not found") suffix = os.path.basename(self.funky_name) with unix_socket_path(suffix=suffix) as name: try: sock = bind_unix_socket(name) except UnicodeEncodeError: if PY3: raise else: raise unittest.SkipTest("not supported") with closing(sock): cons = psutil.net_connections(kind='unix') # AF_UNIX addr not set on OpenBSD if not OPENBSD: conn = find_sock(cons) self.assertIsInstance(conn.laddr, str) self.assertEqual(conn.laddr, name)
def test_unix_udp(self): testfn = self.get_testfn() with closing(bind_unix_socket(testfn, type=SOCK_STREAM)) as sock: conn = self.check_socket(sock) assert not conn.raddr self.assertEqual(conn.status, psutil.CONN_NONE)
def test_unix_udp(self): with unix_socket_path() as name: with closing(bind_unix_socket(name, type=SOCK_STREAM)) as sock: conn = self.check_socket(sock) assert not conn.raddr self.assertEqual(conn.status, psutil.CONN_NONE)
def test_unix_udp(self): with unix_socket_path() as name: with closing(bind_unix_socket(name, type=SOCK_STREAM)) as sock: conn = self.check_socket(sock) assert not conn.raddr self.assertEqual(conn.status, psutil.CONN_NONE)