def test_unix(self): testfn = self.get_testfn() server, client = unix_socketpair(testfn) try: cons = thisproc.connections(kind='unix') assert not (cons[0].laddr and cons[0].raddr) assert not (cons[1].laddr and cons[1].raddr) if NETBSD or FREEBSD: # On NetBSD creating a UNIX socket will cause # a UNIX connection to /var/run/log. cons = [c for c in cons if c.raddr != '/var/run/log'] if CIRRUS: cons = [c for c in cons if c.fd in (server.fileno(), client.fileno())] self.assertEqual(len(cons), 2, msg=cons) if LINUX or FREEBSD or SUNOS: # remote path is never set self.assertEqual(cons[0].raddr, "") self.assertEqual(cons[1].raddr, "") # one local address should though self.assertEqual(testfn, cons[0].laddr or cons[1].laddr) elif OPENBSD: # No addresses whatsoever here. for addr in (cons[0].laddr, cons[0].raddr, cons[1].laddr, cons[1].raddr): self.assertEqual(addr, "") else: # On other systems either the laddr or raddr # of both peers are set. self.assertEqual(cons[0].laddr or cons[1].laddr, testfn) self.assertEqual(cons[0].raddr or cons[1].raddr, testfn) finally: server.close() client.close()
def test_unix(self): with unix_socket_path() as name: server, client = unix_socketpair(name) with nested(closing(server), closing(client)): cons = thisproc.connections(kind='unix') assert not (cons[0].laddr and cons[0].raddr) assert not (cons[1].laddr and cons[1].raddr) if NETBSD: # On NetBSD creating a UNIX socket will cause # a UNIX connection to /var/run/log. cons = [c for c in cons if c.raddr != '/var/run/log'] self.assertEqual(len(cons), 2) if LINUX or FREEBSD: # remote path is never set self.assertEqual(cons[0].raddr, "") self.assertEqual(cons[1].raddr, "") # one local address should though self.assertEqual(name, cons[0].laddr or cons[1].laddr) elif OPENBSD: # No addresses whatsoever here. for addr in (cons[0].laddr, cons[0].raddr, cons[1].laddr, cons[1].raddr): self.assertEqual(addr, "") else: # On other systems either the laddr or raddr # of both peers are set. self.assertEqual(cons[0].laddr or cons[1].laddr, name) self.assertEqual(cons[0].raddr or cons[1].raddr, name)
def test_unix(self): with unix_socket_path() as name: server, client = unix_socketpair(name) try: cons = thisproc.connections(kind='unix') assert not (cons[0].laddr and cons[0].raddr) assert not (cons[1].laddr and cons[1].raddr) if NETBSD: # On NetBSD creating a UNIX socket will cause # a UNIX connection to /var/run/log. cons = [c for c in cons if c.raddr != '/var/run/log'] self.assertEqual(len(cons), 2) if LINUX or FREEBSD or SUNOS: # remote path is never set self.assertEqual(cons[0].raddr, "") self.assertEqual(cons[1].raddr, "") # one local address should though self.assertEqual(name, cons[0].laddr or cons[1].laddr) elif OPENBSD: # No addresses whatsoever here. for addr in (cons[0].laddr, cons[0].raddr, cons[1].laddr, cons[1].raddr): self.assertEqual(addr, "") else: # On other systems either the laddr or raddr # of both peers are set. self.assertEqual(cons[0].laddr or cons[1].laddr, name) self.assertEqual(cons[0].raddr or cons[1].raddr, name) finally: server.close() client.close()
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_unix(self): with unix_socket_path() as name: server, client = unix_socketpair(name) with nested(closing(server), closing(client)): cons = psutil.Process().connections(kind='unix') self.assertEqual(len(cons), 2) server_conn, client_conn = self.distinguish_unix_socks(cons) self.check_socket(server, conn=server_conn) self.check_socket(client, conn=client_conn) self.assertEqual(server_conn.laddr, name) # TODO: https://github.com/giampaolo/psutil/issues/1035 self.assertIn(server_conn.raddr, ("", None)) # TODO: https://github.com/giampaolo/psutil/issues/1035 self.assertIn(client_conn.laddr, ("", None))
def test_unix_socketpair(self): p = psutil.Process() num_fds = p.num_fds() assert not p.connections(kind='unix') name = self.get_testfn() server, client = unix_socketpair(name) try: assert os.path.exists(name) assert stat.S_ISSOCK(os.stat(name).st_mode) self.assertEqual(p.num_fds() - num_fds, 2) self.assertEqual(len(p.connections(kind='unix')), 2) self.assertEqual(server.getsockname(), name) self.assertEqual(client.getpeername(), name) finally: client.close() server.close()
def test_unix_socketpair(self): p = psutil.Process() num_fds = p.num_fds() assert not p.connections(kind='unix') with unix_socket_path() as name: server, client = unix_socketpair(name) try: assert os.path.exists(name) assert stat.S_ISSOCK(os.stat(name).st_mode) self.assertEqual(p.num_fds() - num_fds, 2) self.assertEqual(len(p.connections(kind='unix')), 2) self.assertEqual(server.getsockname(), name) self.assertEqual(client.getpeername(), name) finally: client.close() server.close()
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()