Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
 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)
Ejemplo n.º 3
0
 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)
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
 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()
Ejemplo n.º 7
0
 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)
Ejemplo n.º 8
0
    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()
Ejemplo n.º 9
0
    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))
Ejemplo n.º 10
0
 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()
Ejemplo n.º 11
0
 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)
Ejemplo n.º 12
0
 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)
Ejemplo n.º 13
0
 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()
Ejemplo n.º 14
0
    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)
Ejemplo n.º 15
0
    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)
Ejemplo n.º 16
0
 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)
Ejemplo n.º 17
0
 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)