Debian Codegraph @

tests/test_netns.py

raw symbols SCIP
1 import contextlib
2 import errno
3 import os
4 import socket
5 import subprocess
6 import time
7
8 import pytest
9
10 from labgrid.util.agentwrapper import AgentWrapper
11 from labgrid.util.netns import NetNamespace
12
13
14 def netns_allowed():
15 result = subprocess.run(["unshare", "-Umn", "true"])
16 return result.returncode == 0
17
18
19 pytestmark = pytest.mark.skipif(not netns_allowed(), reason="missing user+mount+network namespace privileges")
20
21
22 FAMILY_PARAMS = (
23 "family,host",
24 [
25 pytest.param(socket.AF_INET, "127.0.0.1", id="IPv4"),
26 pytest.param(socket.AF_INET6, "::1", id="IPv6"),
27 ],
28 )
29
30
31 @pytest.fixture
32 def netns():
33 with AgentWrapper(None) as aw:
34 netns = NetNamespace.create(aw, "be:df:8f:7a:12:db")
35
36 links = netns.get_links()
37 link_names = [link["ifname"] for link in links]
38 assert netns.intf in link_names
39
40 # Bring up lo in the namespace
41 netns.run(["ip", "link", "set", "up", "dev", "lo"], check=True)
42
43 yield netns
44
45 # Verify that all sockets were closed in the namespace
46 assert netns._agent.list_sockets() == []
47
48
49 @pytest.mark.parametrize(*FAMILY_PARAMS)
50 def test_tcp(netns, family, host):
51 with netns.socket(family, socket.SOCK_STREAM) as server_sock:
52 server_sock.bind((host, 5000))
53 server_sock.listen(0)
54
55 with netns.socket(family, socket.SOCK_STREAM) as client_sock:
56 client_sock.connect((host, 5000))
57
58 c, addr = server_sock.accept()
59 client_sock.send(b"Hello")
60 assert c.recv(1024) == b"Hello"
61
62 c.send(b"World")
63 assert client_sock.recv(1024) == b"World"
64
65
66 @pytest.mark.parametrize(*FAMILY_PARAMS)
67 def test_udp(netns, family, host):
68 with netns.socket(family, socket.SOCK_DGRAM) as server_sock:
69 server_sock.bind((host, 5000))
70 assert server_sock.family == family
71
72 # Test connected UDP client socket
73 with netns.socket(family, socket.SOCK_DGRAM) as client_sock:
74 client_sock.connect((host, 5000))
75 client_sock.send(b"Hello")
76 data, addr = server_sock.recvfrom(1024)
77 assert data == b"Hello"
78
79 server_sock.sendto(b"World", addr)
80 data, addr = client_sock.recvfrom(1024)
81 assert data == b"World"
82
83 # Test unconnected UDP client socket
84 server_addr = server_sock.getsockname()
85
86 with netns.socket(family, socket.SOCK_DGRAM) as client_sock:
87 client_sock.sendto(b"Hello", server_addr)
88 data, addr = server_sock.recvfrom(1024)
89 assert data == b"Hello"
90
91 server_sock.sendto(b"World", addr)
92 data, addr = client_sock.recvfrom(1024)
93 assert data == b"World"
94
95
96 @pytest.mark.parametrize(*FAMILY_PARAMS)
97 def test_getaddrinfo(netns, family, host):
98 # Test getaddrinfo
99 for fam, _socktype, _proto, _cannonname, _sockaddr in netns.getaddrinfo(host, 5000):
100 assert fam == family
101
102
103 def test_closed_socket(netns):
104 # Test that closed sockets result in EBADF
105 s = netns.socket(socket.AF_INET, socket.SOCK_DGRAM)
106 s.close()
107 with pytest.raises(OSError, check=lambda e: e.errno == errno.EBADF):
108 s.bind(("127.0.0.1", 5000))
109
110
111 def test_dup(netns):
112 with netns.socket(socket.AF_INET, socket.SOCK_DGRAM) as server_sock:
113 server_sock.bind(("127.0.0.1", 5000))
114
115 # Create, then close the duplicate socket. The server socket should still function
116 dup_sock = server_sock.dup()
117 dup_sock.close()
118
119 with netns.socket(socket.AF_INET, socket.SOCK_DGRAM) as client_sock:
120 client_sock.connect(("127.0.0.1", 5000))
121 client_sock.send(b"Hello")
122 data, addr = server_sock.recvfrom(1024)
123 assert data == b"Hello"
124
125 server_sock.sendto(b"World", addr)
126 data, addr = client_sock.recvfrom(1024)
127 assert data == b"World"
128
129
130 def test_detach(netns):
131 with netns.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
132 fd = s.detach()
133 os.close(fd)
134
135 # Verify that all sockets were removed in the namespace
136 assert netns._agent.list_sockets() == []
137
138
139 def test_socks(netns, tmp_path):
140 with contextlib.ExitStack() as stack:
141 (port, password) = stack.enter_context(netns.socks_proxy())
142
143 import requests
144
145 proxy_url = f"socks5h://labgrid:{password}@127.0.0.1:{port}"
146
147 proxies = {"http": proxy_url, "https": proxy_url}
148
149 session = requests.Session()
150 session.proxies.update(proxies)
151
152 tmp_file = tmp_path / "data"
153 tmp_file.write_bytes(b"test\n")
154
155 httpd = stack.enter_context(netns.Popen(["busybox", "httpd", "-f", "-h", str(tmp_path)]))
156 stack.callback(lambda: httpd.terminate())
157
158 time.sleep(1)
159
160 response = session.get("http://127.0.0.1/data", timeout=5)
161 assert response.content == b"test\n"
162 assert response.status_code == 200
163
164 response = session.get("http://127.0.0.1/missing", timeout=5)
165 assert response.status_code == 404