Debian Codegraph @

tests/linux_kernel/helpers/test_tcp.py

raw
1 # Copyright (c) Meta Platforms, Inc. and affiliates.
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 import os
5 import socket
6
7 from drgn import cast
8 from drgn.helpers.linux.fs import fget
9 from drgn.helpers.linux.pid import find_task
10 from drgn.helpers.linux.tcp import sk_tcpstate
11 from tests.linux_kernel import LinuxKernelTestCase, create_socket
12
13
14 class TestTcp(LinuxKernelTestCase):
15 def test_sk_tcpstate(self):
16 with create_socket() as sock:
17 task = find_task(self.prog, os.getpid())
18 file = fget(task, sock.fileno())
19 sk = cast("struct socket *", file.private_data).sk
20 self.assertEqual(sk_tcpstate(sk), self.prog["TCP_CLOSE"])
21
22 sock.bind(("localhost", 0))
23 sock.listen()
24 self.assertEqual(sk_tcpstate(sk), self.prog["TCP_LISTEN"])
25
26 with socket.create_connection(sock.getsockname()), sock.accept()[
27 0
28 ] as sock2:
29 file = fget(task, sock2.fileno())
30 sk = cast("struct socket *", file.private_data).sk
31 self.assertEqual(sk_tcpstate(sk), self.prog["TCP_ESTABLISHED"])