1import os
2import time
3
4import infra.basetest
5
6
7class TestTcpdump(infra.basetest.BRTest):
8    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
9        """
10        BR2_PACKAGE_BUSYBOX_SHOW_OTHERS=y
11        BR2_PACKAGE_TCPDUMP=y
12        BR2_TARGET_ROOTFS_CPIO=y
13        # BR2_TARGET_ROOTFS_TAR is not set
14        """
15
16    def test_run(self):
17        cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
18        self.emulator.boot(arch="armv5",
19                           kernel="builtin",
20                           options=["-initrd", cpio_file])
21        self.emulator.login()
22
23        capture_file = "capture.pcap"
24        decode_log = "decode.log"
25
26        # Check the program can execute.
27        self.assertRunOk("tcpdump --version")
28
29        # Run ping in background.
30        cmd = "ping localhost >/dev/null &"
31        self.assertRunOk(cmd)
32
33        time.sleep(1)
34
35        # Capture 3 packets with the message.
36        cmd = f"tcpdump -c 3 -w {capture_file} icmp"
37        self.assertRunOk(cmd)
38
39        # Decode the capture file.
40        cmd = f"tcpdump -r {capture_file} > {decode_log}"
41        self.assertRunOk(cmd)
42
43        # Check we have ICMP echo requests/replies in the
44        # decoded capture.
45        cmd = f"grep -E 'ICMP echo (request|reply)' {decode_log}"
46        self.assertRunOk(cmd)
47