selftests: drv-net: add require_XYZ() helpers for validating env
[linux-block.git] / tools / testing / selftests / drivers / net / ping.py
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: GPL-2.0
3
4 from lib.py import ksft_run, ksft_exit
5 from lib.py import ksft_eq
6 from lib.py import NetDrvEpEnv
7 from lib.py import bkg, cmd, wait_port_listen, rand_port
8
9
10 def test_v4(cfg) -> None:
11     cfg.require_v4()
12
13     cmd(f"ping -c 1 -W0.5 {cfg.remote_v4}")
14     cmd(f"ping -c 1 -W0.5 {cfg.v4}", host=cfg.remote)
15
16
17 def test_v6(cfg) -> None:
18     cfg.require_v6()
19
20     cmd(f"ping -c 1 -W0.5 {cfg.remote_v6}")
21     cmd(f"ping -c 1 -W0.5 {cfg.v6}", host=cfg.remote)
22
23
24 def test_tcp(cfg) -> None:
25     cfg.require_cmd("socat", remote=True)
26
27     port = rand_port()
28     listen_cmd = f"socat -{cfg.addr_ipver} -t 2 -u TCP-LISTEN:{port},reuseport STDOUT"
29
30     with bkg(listen_cmd, exit_wait=True) as nc:
31         wait_port_listen(port)
32
33         cmd(f"echo ping | socat -t 2 -u STDIN TCP:{cfg.baddr}:{port}",
34             shell=True, host=cfg.remote)
35     ksft_eq(nc.stdout.strip(), "ping")
36
37     with bkg(listen_cmd, host=cfg.remote, exit_wait=True) as nc:
38         wait_port_listen(port, host=cfg.remote)
39
40         cmd(f"echo ping | socat -t 2 -u STDIN TCP:{cfg.remote_baddr}:{port}", shell=True)
41     ksft_eq(nc.stdout.strip(), "ping")
42
43
44 def main() -> None:
45     with NetDrvEpEnv(__file__) as cfg:
46         ksft_run(globs=globals(), case_pfx={"test_"}, args=(cfg, ))
47     ksft_exit()
48
49
50 if __name__ == "__main__":
51     main()