test_connections.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Tests for psutil.net_connections() and Process.net_connections() APIs."""
  6. import os
  7. import socket
  8. import textwrap
  9. from contextlib import closing
  10. from socket import AF_INET
  11. from socket import AF_INET6
  12. from socket import SOCK_DGRAM
  13. from socket import SOCK_STREAM
  14. import psutil
  15. from psutil import FREEBSD
  16. from psutil import LINUX
  17. from psutil import MACOS
  18. from psutil import NETBSD
  19. from psutil import OPENBSD
  20. from psutil import POSIX
  21. from psutil import SUNOS
  22. from psutil import WINDOWS
  23. from psutil._common import supports_ipv6
  24. from psutil.tests import AF_UNIX
  25. from psutil.tests import HAS_NET_CONNECTIONS_UNIX
  26. from psutil.tests import SKIP_SYSCONS
  27. from psutil.tests import PsutilTestCase
  28. from psutil.tests import bind_socket
  29. from psutil.tests import bind_unix_socket
  30. from psutil.tests import check_connection_ntuple
  31. from psutil.tests import create_sockets
  32. from psutil.tests import filter_proc_net_connections
  33. from psutil.tests import pytest
  34. from psutil.tests import reap_children
  35. from psutil.tests import retry_on_failure
  36. from psutil.tests import skip_on_access_denied
  37. from psutil.tests import tcp_socketpair
  38. from psutil.tests import unix_socketpair
  39. from psutil.tests import wait_for_file
  40. SOCK_SEQPACKET = getattr(socket, "SOCK_SEQPACKET", object())
  41. def this_proc_net_connections(kind):
  42. cons = psutil.Process().net_connections(kind=kind)
  43. if kind in {"all", "unix"}:
  44. return filter_proc_net_connections(cons)
  45. return cons
  46. @pytest.mark.xdist_group(name="serial")
  47. class ConnectionTestCase(PsutilTestCase):
  48. def setUp(self):
  49. assert this_proc_net_connections(kind='all') == []
  50. def tearDown(self):
  51. # Make sure we closed all resources.
  52. assert this_proc_net_connections(kind='all') == []
  53. def compare_procsys_connections(self, pid, proc_cons, kind='all'):
  54. """Given a process PID and its list of connections compare
  55. those against system-wide connections retrieved via
  56. psutil.net_connections.
  57. """
  58. try:
  59. sys_cons = psutil.net_connections(kind=kind)
  60. except psutil.AccessDenied:
  61. # On MACOS, system-wide connections are retrieved by iterating
  62. # over all processes
  63. if MACOS:
  64. return
  65. else:
  66. raise
  67. # Filter for this proc PID and exlucde PIDs from the tuple.
  68. sys_cons = [c[:-1] for c in sys_cons if c.pid == pid]
  69. sys_cons.sort()
  70. proc_cons.sort()
  71. assert proc_cons == sys_cons
  72. class TestBasicOperations(ConnectionTestCase):
  73. @pytest.mark.skipif(SKIP_SYSCONS, reason="requires root")
  74. def test_system(self):
  75. with create_sockets():
  76. for conn in psutil.net_connections(kind='all'):
  77. check_connection_ntuple(conn)
  78. def test_process(self):
  79. with create_sockets():
  80. for conn in this_proc_net_connections(kind='all'):
  81. check_connection_ntuple(conn)
  82. def test_invalid_kind(self):
  83. with pytest.raises(ValueError):
  84. this_proc_net_connections(kind='???')
  85. with pytest.raises(ValueError):
  86. psutil.net_connections(kind='???')
  87. @pytest.mark.xdist_group(name="serial")
  88. class TestUnconnectedSockets(ConnectionTestCase):
  89. """Tests sockets which are open but not connected to anything."""
  90. def get_conn_from_sock(self, sock):
  91. cons = this_proc_net_connections(kind='all')
  92. smap = {c.fd: c for c in cons}
  93. if NETBSD or FREEBSD:
  94. # NetBSD opens a UNIX socket to /var/log/run
  95. # so there may be more connections.
  96. return smap[sock.fileno()]
  97. else:
  98. assert len(cons) == 1
  99. if cons[0].fd != -1:
  100. assert smap[sock.fileno()].fd == sock.fileno()
  101. return cons[0]
  102. def check_socket(self, sock):
  103. """Given a socket, makes sure it matches the one obtained
  104. via psutil. It assumes this process created one connection
  105. only (the one supposed to be checked).
  106. """
  107. conn = self.get_conn_from_sock(sock)
  108. check_connection_ntuple(conn)
  109. # fd, family, type
  110. if conn.fd != -1:
  111. assert conn.fd == sock.fileno()
  112. assert conn.family == sock.family
  113. # see: http://bugs.python.org/issue30204
  114. assert conn.type == sock.getsockopt(socket.SOL_SOCKET, socket.SO_TYPE)
  115. # local address
  116. laddr = sock.getsockname()
  117. if not laddr and isinstance(laddr, bytes):
  118. # See: http://bugs.python.org/issue30205
  119. laddr = laddr.decode()
  120. if sock.family == AF_INET6:
  121. laddr = laddr[:2]
  122. assert conn.laddr == laddr
  123. # XXX Solaris can't retrieve system-wide UNIX sockets
  124. if sock.family == AF_UNIX and HAS_NET_CONNECTIONS_UNIX:
  125. cons = this_proc_net_connections(kind='all')
  126. self.compare_procsys_connections(os.getpid(), cons, kind='all')
  127. return conn
  128. def test_tcp_v4(self):
  129. addr = ("127.0.0.1", 0)
  130. with closing(bind_socket(AF_INET, SOCK_STREAM, addr=addr)) as sock:
  131. conn = self.check_socket(sock)
  132. assert conn.raddr == ()
  133. assert conn.status == psutil.CONN_LISTEN
  134. @pytest.mark.skipif(not supports_ipv6(), reason="IPv6 not supported")
  135. def test_tcp_v6(self):
  136. addr = ("::1", 0)
  137. with closing(bind_socket(AF_INET6, SOCK_STREAM, addr=addr)) as sock:
  138. conn = self.check_socket(sock)
  139. assert conn.raddr == ()
  140. assert conn.status == psutil.CONN_LISTEN
  141. def test_udp_v4(self):
  142. addr = ("127.0.0.1", 0)
  143. with closing(bind_socket(AF_INET, SOCK_DGRAM, addr=addr)) as sock:
  144. conn = self.check_socket(sock)
  145. assert conn.raddr == ()
  146. assert conn.status == psutil.CONN_NONE
  147. @pytest.mark.skipif(not supports_ipv6(), reason="IPv6 not supported")
  148. def test_udp_v6(self):
  149. addr = ("::1", 0)
  150. with closing(bind_socket(AF_INET6, SOCK_DGRAM, addr=addr)) as sock:
  151. conn = self.check_socket(sock)
  152. assert conn.raddr == ()
  153. assert conn.status == psutil.CONN_NONE
  154. @pytest.mark.skipif(not POSIX, reason="POSIX only")
  155. def test_unix_tcp(self):
  156. testfn = self.get_testfn()
  157. with closing(bind_unix_socket(testfn, type=SOCK_STREAM)) as sock:
  158. conn = self.check_socket(sock)
  159. assert conn.raddr == ""
  160. assert conn.status == psutil.CONN_NONE
  161. @pytest.mark.skipif(not POSIX, reason="POSIX only")
  162. def test_unix_udp(self):
  163. testfn = self.get_testfn()
  164. with closing(bind_unix_socket(testfn, type=SOCK_STREAM)) as sock:
  165. conn = self.check_socket(sock)
  166. assert conn.raddr == ""
  167. assert conn.status == psutil.CONN_NONE
  168. @pytest.mark.xdist_group(name="serial")
  169. class TestConnectedSocket(ConnectionTestCase):
  170. """Test socket pairs which are actually connected to
  171. each other.
  172. """
  173. # On SunOS, even after we close() it, the server socket stays around
  174. # in TIME_WAIT state.
  175. @pytest.mark.skipif(SUNOS, reason="unreliable on SUNOS")
  176. def test_tcp(self):
  177. addr = ("127.0.0.1", 0)
  178. assert this_proc_net_connections(kind='tcp4') == []
  179. server, client = tcp_socketpair(AF_INET, addr=addr)
  180. try:
  181. cons = this_proc_net_connections(kind='tcp4')
  182. assert len(cons) == 2
  183. assert cons[0].status == psutil.CONN_ESTABLISHED
  184. assert cons[1].status == psutil.CONN_ESTABLISHED
  185. # May not be fast enough to change state so it stays
  186. # commenteed.
  187. # client.close()
  188. # cons = this_proc_net_connections(kind='all')
  189. # assert len(cons) == 1
  190. # assert cons[0].status == psutil.CONN_CLOSE_WAIT
  191. finally:
  192. server.close()
  193. client.close()
  194. @pytest.mark.skipif(not POSIX, reason="POSIX only")
  195. @pytest.mark.skipif(
  196. not HAS_NET_CONNECTIONS_UNIX, reason="can't list UNIX sockets"
  197. )
  198. def test_unix(self):
  199. testfn = self.get_testfn()
  200. server, client = unix_socketpair(testfn)
  201. try:
  202. cons = this_proc_net_connections(kind='unix')
  203. assert not (cons[0].laddr and cons[0].raddr), cons
  204. assert not (cons[1].laddr and cons[1].raddr), cons
  205. if NETBSD or FREEBSD:
  206. # On NetBSD creating a UNIX socket will cause
  207. # a UNIX connection to /var/run/log.
  208. cons = [c for c in cons if c.raddr != '/var/run/log']
  209. assert len(cons) == 2
  210. if LINUX or FREEBSD or SUNOS or OPENBSD:
  211. # remote path is never set
  212. assert cons[0].raddr == ""
  213. assert cons[1].raddr == ""
  214. # one local address should though
  215. assert testfn == (cons[0].laddr or cons[1].laddr)
  216. else:
  217. # On other systems either the laddr or raddr
  218. # of both peers are set.
  219. assert (cons[0].laddr or cons[1].laddr) == testfn
  220. finally:
  221. server.close()
  222. client.close()
  223. class TestFilters(ConnectionTestCase):
  224. def test_filters(self):
  225. def check(kind, families, types):
  226. for conn in this_proc_net_connections(kind=kind):
  227. assert conn.family in families
  228. assert conn.type in types
  229. if not SKIP_SYSCONS:
  230. for conn in psutil.net_connections(kind=kind):
  231. assert conn.family in families
  232. assert conn.type in types
  233. with create_sockets():
  234. check(
  235. 'all',
  236. [AF_INET, AF_INET6, AF_UNIX],
  237. [SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET],
  238. )
  239. check('inet', [AF_INET, AF_INET6], [SOCK_STREAM, SOCK_DGRAM])
  240. check('inet4', [AF_INET], [SOCK_STREAM, SOCK_DGRAM])
  241. check('tcp', [AF_INET, AF_INET6], [SOCK_STREAM])
  242. check('tcp4', [AF_INET], [SOCK_STREAM])
  243. check('tcp6', [AF_INET6], [SOCK_STREAM])
  244. check('udp', [AF_INET, AF_INET6], [SOCK_DGRAM])
  245. check('udp4', [AF_INET], [SOCK_DGRAM])
  246. check('udp6', [AF_INET6], [SOCK_DGRAM])
  247. if HAS_NET_CONNECTIONS_UNIX:
  248. check(
  249. 'unix',
  250. [AF_UNIX],
  251. [SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET],
  252. )
  253. @skip_on_access_denied(only_if=MACOS)
  254. def test_combos(self):
  255. reap_children()
  256. def check_conn(proc, conn, family, type, laddr, raddr, status, kinds):
  257. all_kinds = (
  258. "all",
  259. "inet",
  260. "inet4",
  261. "inet6",
  262. "tcp",
  263. "tcp4",
  264. "tcp6",
  265. "udp",
  266. "udp4",
  267. "udp6",
  268. )
  269. check_connection_ntuple(conn)
  270. assert conn.family == family
  271. assert conn.type == type
  272. assert conn.laddr == laddr
  273. assert conn.raddr == raddr
  274. assert conn.status == status
  275. for kind in all_kinds:
  276. cons = proc.net_connections(kind=kind)
  277. if kind in kinds:
  278. assert cons != []
  279. else:
  280. assert cons == []
  281. # compare against system-wide connections
  282. # XXX Solaris can't retrieve system-wide UNIX
  283. # sockets.
  284. if HAS_NET_CONNECTIONS_UNIX:
  285. self.compare_procsys_connections(proc.pid, [conn])
  286. tcp_template = textwrap.dedent("""
  287. import socket, time
  288. s = socket.socket({family}, socket.SOCK_STREAM)
  289. s.bind(('{addr}', 0))
  290. s.listen(5)
  291. with open('{testfn}', 'w') as f:
  292. f.write(str(s.getsockname()[:2]))
  293. [time.sleep(0.1) for x in range(100)]
  294. """)
  295. udp_template = textwrap.dedent("""
  296. import socket, time
  297. s = socket.socket({family}, socket.SOCK_DGRAM)
  298. s.bind(('{addr}', 0))
  299. with open('{testfn}', 'w') as f:
  300. f.write(str(s.getsockname()[:2]))
  301. [time.sleep(0.1) for x in range(100)]
  302. """)
  303. # must be relative on Windows
  304. testfile = os.path.basename(self.get_testfn(dir=os.getcwd()))
  305. tcp4_template = tcp_template.format(
  306. family=int(AF_INET), addr="127.0.0.1", testfn=testfile
  307. )
  308. udp4_template = udp_template.format(
  309. family=int(AF_INET), addr="127.0.0.1", testfn=testfile
  310. )
  311. tcp6_template = tcp_template.format(
  312. family=int(AF_INET6), addr="::1", testfn=testfile
  313. )
  314. udp6_template = udp_template.format(
  315. family=int(AF_INET6), addr="::1", testfn=testfile
  316. )
  317. # launch various subprocess instantiating a socket of various
  318. # families and types to enrich psutil results
  319. tcp4_proc = self.pyrun(tcp4_template)
  320. tcp4_addr = eval(wait_for_file(testfile, delete=True))
  321. udp4_proc = self.pyrun(udp4_template)
  322. udp4_addr = eval(wait_for_file(testfile, delete=True))
  323. if supports_ipv6():
  324. tcp6_proc = self.pyrun(tcp6_template)
  325. tcp6_addr = eval(wait_for_file(testfile, delete=True))
  326. udp6_proc = self.pyrun(udp6_template)
  327. udp6_addr = eval(wait_for_file(testfile, delete=True))
  328. else:
  329. tcp6_proc = None
  330. udp6_proc = None
  331. tcp6_addr = None
  332. udp6_addr = None
  333. for p in psutil.Process().children():
  334. cons = p.net_connections()
  335. assert len(cons) == 1
  336. for conn in cons:
  337. # TCP v4
  338. if p.pid == tcp4_proc.pid:
  339. check_conn(
  340. p,
  341. conn,
  342. AF_INET,
  343. SOCK_STREAM,
  344. tcp4_addr,
  345. (),
  346. psutil.CONN_LISTEN,
  347. ("all", "inet", "inet4", "tcp", "tcp4"),
  348. )
  349. # UDP v4
  350. elif p.pid == udp4_proc.pid:
  351. check_conn(
  352. p,
  353. conn,
  354. AF_INET,
  355. SOCK_DGRAM,
  356. udp4_addr,
  357. (),
  358. psutil.CONN_NONE,
  359. ("all", "inet", "inet4", "udp", "udp4"),
  360. )
  361. # TCP v6
  362. elif p.pid == getattr(tcp6_proc, "pid", None):
  363. check_conn(
  364. p,
  365. conn,
  366. AF_INET6,
  367. SOCK_STREAM,
  368. tcp6_addr,
  369. (),
  370. psutil.CONN_LISTEN,
  371. ("all", "inet", "inet6", "tcp", "tcp6"),
  372. )
  373. # UDP v6
  374. elif p.pid == getattr(udp6_proc, "pid", None):
  375. check_conn(
  376. p,
  377. conn,
  378. AF_INET6,
  379. SOCK_DGRAM,
  380. udp6_addr,
  381. (),
  382. psutil.CONN_NONE,
  383. ("all", "inet", "inet6", "udp", "udp6"),
  384. )
  385. def test_count(self):
  386. with create_sockets():
  387. # tcp
  388. cons = this_proc_net_connections(kind='tcp')
  389. assert len(cons) == (2 if supports_ipv6() else 1)
  390. for conn in cons:
  391. assert conn.family in {AF_INET, AF_INET6}
  392. assert conn.type == SOCK_STREAM
  393. # tcp4
  394. cons = this_proc_net_connections(kind='tcp4')
  395. assert len(cons) == 1
  396. assert cons[0].family == AF_INET
  397. assert cons[0].type == SOCK_STREAM
  398. # tcp6
  399. if supports_ipv6():
  400. cons = this_proc_net_connections(kind='tcp6')
  401. assert len(cons) == 1
  402. assert cons[0].family == AF_INET6
  403. assert cons[0].type == SOCK_STREAM
  404. # udp
  405. cons = this_proc_net_connections(kind='udp')
  406. assert len(cons) == (2 if supports_ipv6() else 1)
  407. for conn in cons:
  408. assert conn.family in {AF_INET, AF_INET6}
  409. assert conn.type == SOCK_DGRAM
  410. # udp4
  411. cons = this_proc_net_connections(kind='udp4')
  412. assert len(cons) == 1
  413. assert cons[0].family == AF_INET
  414. assert cons[0].type == SOCK_DGRAM
  415. # udp6
  416. if supports_ipv6():
  417. cons = this_proc_net_connections(kind='udp6')
  418. assert len(cons) == 1
  419. assert cons[0].family == AF_INET6
  420. assert cons[0].type == SOCK_DGRAM
  421. # inet
  422. cons = this_proc_net_connections(kind='inet')
  423. assert len(cons) == (4 if supports_ipv6() else 2)
  424. for conn in cons:
  425. assert conn.family in {AF_INET, AF_INET6}
  426. assert conn.type in {SOCK_STREAM, SOCK_DGRAM}
  427. # inet6
  428. if supports_ipv6():
  429. cons = this_proc_net_connections(kind='inet6')
  430. assert len(cons) == 2
  431. for conn in cons:
  432. assert conn.family == AF_INET6
  433. assert conn.type in {SOCK_STREAM, SOCK_DGRAM}
  434. # Skipped on BSD becayse by default the Python process
  435. # creates a UNIX socket to '/var/run/log'.
  436. if HAS_NET_CONNECTIONS_UNIX and not (FREEBSD or NETBSD):
  437. cons = this_proc_net_connections(kind='unix')
  438. assert len(cons) == 3
  439. for conn in cons:
  440. assert conn.family == AF_UNIX
  441. assert conn.type in {SOCK_STREAM, SOCK_DGRAM}
  442. @pytest.mark.skipif(SKIP_SYSCONS, reason="requires root")
  443. class TestSystemWideConnections(ConnectionTestCase):
  444. """Tests for net_connections()."""
  445. def test_it(self):
  446. def check(cons, families, types_):
  447. for conn in cons:
  448. assert conn.family in families
  449. if conn.family != AF_UNIX:
  450. assert conn.type in types_
  451. check_connection_ntuple(conn)
  452. with create_sockets():
  453. from psutil._common import conn_tmap
  454. for kind, groups in conn_tmap.items():
  455. # XXX: SunOS does not retrieve UNIX sockets.
  456. if kind == 'unix' and not HAS_NET_CONNECTIONS_UNIX:
  457. continue
  458. families, types_ = groups
  459. cons = psutil.net_connections(kind)
  460. assert len(cons) == len(set(cons))
  461. check(cons, families, types_)
  462. @retry_on_failure()
  463. def test_multi_sockets_procs(self):
  464. # Creates multiple sub processes, each creating different
  465. # sockets. For each process check that proc.net_connections()
  466. # and psutil.net_connections() return the same results.
  467. # This is done mainly to check whether net_connections()'s
  468. # pid is properly set, see:
  469. # https://github.com/giampaolo/psutil/issues/1013
  470. with create_sockets() as socks:
  471. expected = len(socks)
  472. pids = []
  473. times = 10
  474. fnames = []
  475. for _ in range(times):
  476. fname = self.get_testfn()
  477. fnames.append(fname)
  478. src = textwrap.dedent(f"""\
  479. import time, os
  480. from psutil.tests import create_sockets
  481. with create_sockets():
  482. with open(r'{fname}', 'w') as f:
  483. f.write("hello")
  484. [time.sleep(0.1) for x in range(100)]
  485. """)
  486. sproc = self.pyrun(src)
  487. pids.append(sproc.pid)
  488. # sync
  489. for fname in fnames:
  490. wait_for_file(fname)
  491. syscons = [
  492. x for x in psutil.net_connections(kind='all') if x.pid in pids
  493. ]
  494. for pid in pids:
  495. assert len([x for x in syscons if x.pid == pid]) == expected
  496. p = psutil.Process(pid)
  497. assert len(p.net_connections('all')) == expected
  498. class TestMisc(PsutilTestCase):
  499. def test_net_connection_constants(self):
  500. ints = []
  501. strs = []
  502. for name in dir(psutil):
  503. if name.startswith('CONN_'):
  504. num = getattr(psutil, name)
  505. str_ = str(num)
  506. assert str_.isupper(), str_
  507. assert str not in strs
  508. assert num not in ints
  509. ints.append(num)
  510. strs.append(str_)
  511. if SUNOS:
  512. psutil.CONN_IDLE # noqa: B018
  513. psutil.CONN_BOUND # noqa: B018
  514. if WINDOWS:
  515. psutil.CONN_DELETE_TCB # noqa: B018