Changeset View
Changeset View
Standalone View
Standalone View
sbin/ping/tests/test_ping.py
| import pytest | import pytest | ||||
| import logging | import logging | ||||
| import os | import os | ||||
| import re | import re | ||||
| import subprocess | import subprocess | ||||
| from atf_python.sys.net.vnet import IfaceFactory | from atf_python.sys.net.vnet import IfaceFactory | ||||
| from atf_python.sys.net.vnet import SingleVnetTestTemplate | from atf_python.sys.net.vnet import SingleVnetTestTemplate | ||||
| from atf_python.sys.net.tools import ToolsHelper | from atf_python.sys.net.tools import ToolsHelper | ||||
| from typing import List | from typing import List | ||||
| from typing import Optional | from typing import Optional | ||||
| logging.getLogger("scapy").setLevel(logging.CRITICAL) | logging.getLogger("scapy").setLevel(logging.CRITICAL) | ||||
| try: | |||||
| import scapy.all as sc | import scapy.all as sc | ||||
| except ImportError as e: | |||||
| # Fake scapy well enough to be able to list test cases | |||||
| from types import SimpleNamespace | |||||
| sc = SimpleNamespace( | |||||
| scapy=SimpleNamespace( | |||||
| fields=SimpleNamespace( | |||||
| SourceIPField=0, | |||||
| ByteEnumField=0, | |||||
| MultiEnumField=0, | |||||
| BitField=0, | |||||
| FlagsField=0, | |||||
| ByteField=0, | |||||
| IPField=0, | |||||
| ShortField=0, | |||||
| ), | |||||
| layers=SimpleNamespace( | |||||
| inet=SimpleNamespace( | |||||
| DestIPField=0, | |||||
| ICMPTimeStampField=0, | |||||
| ) | |||||
| ) | |||||
| ) | |||||
| ) | |||||
| def build_response_packet(echo, ip, icmp, oip_ihl, special): | def build_response_packet(echo, ip, icmp, oip_ihl, special): | ||||
| icmp_id_seq_types = [0, 8, 13, 14, 15, 16, 17, 18, 37, 38] | icmp_id_seq_types = [0, 8, 13, 14, 15, 16, 17, 18, 37, 38] | ||||
| oip = echo[sc.IP] | oip = echo[sc.IP] | ||||
| oicmp = echo[sc.ICMP] | oicmp = echo[sc.ICMP] | ||||
| load = echo[sc.ICMP].payload | load = echo[sc.ICMP].payload | ||||
| oip[sc.IP].remove_payload() | oip[sc.IP].remove_payload() | ||||
| ▲ Show 20 Lines • Show All 98 Lines • ▼ Show 20 Lines | def generate_ip_options(opts): | ||||
| return options | return options | ||||
| def pinger( | def pinger( | ||||
| # Required arguments | # Required arguments | ||||
| # Avoid setting defaults on these arguments, | # Avoid setting defaults on these arguments, | ||||
| # as we want to set them explicitly in the tests | # as we want to set them explicitly in the tests | ||||
| iface: str, | iface: str, | ||||
| /, | /, | ||||
Lint: PEP8 E225: missing whitespace around operator | |||||
| src: sc.scapy.fields.SourceIPField, | src: sc.scapy.fields.SourceIPField, | ||||
| dst: sc.scapy.layers.inet.DestIPField, | dst: sc.scapy.layers.inet.DestIPField, | ||||
| icmp_type: sc.scapy.fields.ByteEnumField, | icmp_type: sc.scapy.fields.ByteEnumField, | ||||
| icmp_code: sc.scapy.fields.MultiEnumField, | icmp_code: sc.scapy.fields.MultiEnumField, | ||||
| # IP arguments | # IP arguments | ||||
| ihl: Optional[sc.scapy.fields.BitField] = None, | ihl: Optional[sc.scapy.fields.BitField] = None, | ||||
| flags: Optional[sc.scapy.fields.FlagsField] = 0, | flags: Optional[sc.scapy.fields.FlagsField] = 0, | ||||
| opts: Optional[str] = None, | opts: Optional[str] = None, | ||||
| ▲ Show 20 Lines • Show All 142 Lines • ▼ Show 20 Lines | pattern_replacements = [ | ||||
| (r"[0-9\.]+/[0-9.]+", "/"), | (r"[0-9\.]+/[0-9.]+", "/"), | ||||
| ] | ] | ||||
| for pattern, repl in pattern_replacements: | for pattern, repl in pattern_replacements: | ||||
| output = re.sub(pattern, repl, output) | output = re.sub(pattern, repl, output) | ||||
| return output | return output | ||||
| class TestPing(SingleVnetTestTemplate): | class TestPing(SingleVnetTestTemplate): | ||||
| IPV6_PREFIXES: List[str] = ["2001:db8::1/64"] | IPV6_PREFIXES: List[str] = ["2001:db8::1/64"] | ||||
Lint: PEP8 E701 multiple statements on one line (colon) Lint: PEP8 E701: multiple statements on one line (colon) | |||||
| IPV4_PREFIXES: List[str] = ["192.0.2.1/24"] | IPV4_PREFIXES: List[str] = ["192.0.2.1/24"] | ||||
Lint: PEP8 E701 multiple statements on one line (colon) Lint: PEP8 E701: multiple statements on one line (colon) | |||||
| # Each param in testdata contains a dictionary with the command, | # Each param in testdata contains a dictionary with the command, | ||||
| # and the expected outcome (returncode, redacted stdout, and stderr) | # and the expected outcome (returncode, redacted stdout, and stderr) | ||||
| testdata = [ | testdata = [ | ||||
| pytest.param( | pytest.param( | ||||
| { | { | ||||
| "args": "ping -4 -c1 -s56 -t1 localhost", | "args": "ping -4 -c1 -s56 -t1 localhost", | ||||
| "returncode": 0, | "returncode": 0, | ||||
| ▲ Show 20 Lines • Show All 528 Lines • ▼ Show 20 Lines | """, | ||||
| "opts": "LSRR", | "opts": "LSRR", | ||||
| }, | }, | ||||
| { | { | ||||
| "returncode": 0, | "returncode": 0, | ||||
| "stdout": """\ | "stdout": """\ | ||||
| PING 192.0.2.2 (192.0.2.2): 56 data bytes | PING 192.0.2.2 (192.0.2.2): 56 data bytes | ||||
| 64 bytes from: icmp_seq=0 ttl= time= ms | 64 bytes from: icmp_seq=0 ttl= time= ms | ||||
| LSRR: 192.0.2.10 | LSRR: 192.0.2.10 | ||||
| 192.0.2.20 | 192.0.2.20 | ||||
Lint: PEP8 E101 indentation contains mixed spaces and tabs Lint: PEP8 E101: indentation contains mixed spaces and tabs | |||||
| 192.0.2.30 | 192.0.2.30 | ||||
| 192.0.2.40 | 192.0.2.40 | ||||
| 192.0.2.50 | 192.0.2.50 | ||||
| 192.0.2.60 | 192.0.2.60 | ||||
| 192.0.2.70 | 192.0.2.70 | ||||
| 192.0.2.80 | 192.0.2.80 | ||||
| 192.0.2.90 | 192.0.2.90 | ||||
| --- 192.0.2.2 ping statistics --- | --- 192.0.2.2 ping statistics --- | ||||
| 1 packets transmitted, 1 packets received, 0.0% packet loss | 1 packets transmitted, 1 packets received, 0.0% packet loss | ||||
| round-trip min/avg/max/stddev = /// ms | round-trip min/avg/max/stddev = /// ms | ||||
| """, | """, | ||||
| "stderr": "", | "stderr": "", | ||||
Lint: PEP8 E101 indentation contains mixed spaces and tabs Lint: PEP8 E101: indentation contains mixed spaces and tabs | |||||
| "redacted": True, | "redacted": True, | ||||
| }, | }, | ||||
| id="_0_0_opts_LSRR", | id="_0_0_opts_LSRR", | ||||
| ), | ), | ||||
| pytest.param( | pytest.param( | ||||
| { | { | ||||
| "src": "192.0.2.1", | "src": "192.0.2.1", | ||||
| "dst": "192.0.2.2", | "dst": "192.0.2.2", | ||||
| Show All 26 Lines | """, | ||||
| "opts": "SSRR", | "opts": "SSRR", | ||||
| }, | }, | ||||
| { | { | ||||
| "returncode": 0, | "returncode": 0, | ||||
| "stdout": """\ | "stdout": """\ | ||||
| PING 192.0.2.2 (192.0.2.2): 56 data bytes | PING 192.0.2.2 (192.0.2.2): 56 data bytes | ||||
| 64 bytes from: icmp_seq=0 ttl= time= ms | 64 bytes from: icmp_seq=0 ttl= time= ms | ||||
| SSRR: 192.0.2.10 | SSRR: 192.0.2.10 | ||||
| 192.0.2.20 | 192.0.2.20 | ||||
Lint: PEP8 E101 indentation contains mixed spaces and tabs Lint: PEP8 E101: indentation contains mixed spaces and tabs | |||||
| 192.0.2.30 | 192.0.2.30 | ||||
| 192.0.2.40 | 192.0.2.40 | ||||
| 192.0.2.50 | 192.0.2.50 | ||||
| 192.0.2.60 | 192.0.2.60 | ||||
| 192.0.2.70 | 192.0.2.70 | ||||
| 192.0.2.80 | 192.0.2.80 | ||||
| 192.0.2.90 | 192.0.2.90 | ||||
| --- 192.0.2.2 ping statistics --- | --- 192.0.2.2 ping statistics --- | ||||
| 1 packets transmitted, 1 packets received, 0.0% packet loss | 1 packets transmitted, 1 packets received, 0.0% packet loss | ||||
| round-trip min/avg/max/stddev = /// ms | round-trip min/avg/max/stddev = /// ms | ||||
| """, | """, | ||||
| "stderr": "", | "stderr": "", | ||||
Lint: PEP8 E101 indentation contains mixed spaces and tabs Lint: PEP8 E101: indentation contains mixed spaces and tabs | |||||
| "redacted": True, | "redacted": True, | ||||
| }, | }, | ||||
| id="_0_0_opts_SSRR", | id="_0_0_opts_SSRR", | ||||
| ), | ), | ||||
| pytest.param( | pytest.param( | ||||
| { | { | ||||
| "src": "192.0.2.1", | "src": "192.0.2.1", | ||||
| "dst": "192.0.2.2", | "dst": "192.0.2.2", | ||||
| Show All 26 Lines | """, | ||||
| "opts": "RR", | "opts": "RR", | ||||
| }, | }, | ||||
| { | { | ||||
| "returncode": 0, | "returncode": 0, | ||||
| "stdout": """\ | "stdout": """\ | ||||
| PING 192.0.2.2 (192.0.2.2): 56 data bytes | PING 192.0.2.2 (192.0.2.2): 56 data bytes | ||||
| 64 bytes from: icmp_seq=0 ttl= time= ms | 64 bytes from: icmp_seq=0 ttl= time= ms | ||||
| RR: 192.0.2.10 | RR: 192.0.2.10 | ||||
| 192.0.2.20 | 192.0.2.20 | ||||
Lint: PEP8 E101 indentation contains mixed spaces and tabs Lint: PEP8 E101: indentation contains mixed spaces and tabs | |||||
| 192.0.2.30 | 192.0.2.30 | ||||
| 192.0.2.40 | 192.0.2.40 | ||||
| 192.0.2.50 | 192.0.2.50 | ||||
| 192.0.2.60 | 192.0.2.60 | ||||
| 192.0.2.70 | 192.0.2.70 | ||||
| 192.0.2.80 | 192.0.2.80 | ||||
| 192.0.2.90 | 192.0.2.90 | ||||
| --- 192.0.2.2 ping statistics --- | --- 192.0.2.2 ping statistics --- | ||||
| 1 packets transmitted, 1 packets received, 0.0% packet loss | 1 packets transmitted, 1 packets received, 0.0% packet loss | ||||
| round-trip min/avg/max/stddev = /// ms | round-trip min/avg/max/stddev = /// ms | ||||
| """, | """, | ||||
| "stderr": "", | "stderr": "", | ||||
Lint: PEP8 E101 indentation contains mixed spaces and tabs Lint: PEP8 E101: indentation contains mixed spaces and tabs | |||||
| "redacted": True, | "redacted": True, | ||||
| }, | }, | ||||
| id="_0_0_opts_RR", | id="_0_0_opts_RR", | ||||
| ), | ), | ||||
| pytest.param( | pytest.param( | ||||
| { | { | ||||
| "src": "192.0.2.1", | "src": "192.0.2.1", | ||||
| "dst": "192.0.2.2", | "dst": "192.0.2.2", | ||||
| ▲ Show 20 Lines • Show All 275 Lines • ▼ Show 20 Lines | """, | ||||
| "opts": "NOP-40", | "opts": "NOP-40", | ||||
| }, | }, | ||||
| { | { | ||||
| "returncode": 2, | "returncode": 2, | ||||
| "stdout": """\ | "stdout": """\ | ||||
| PING 192.0.2.2 (192.0.2.2): 56 data bytes | PING 192.0.2.2 (192.0.2.2): 56 data bytes | ||||
| 132 bytes from 192.0.2.2: Destination Host Unreachable | 132 bytes from 192.0.2.2: Destination Host Unreachable | ||||
| Vr HL TOS Len ID Flg off TTL Pro cks Src Dst Opts | Vr HL TOS Len ID Flg off TTL Pro cks Src Dst Opts | ||||
| 4 f 00 007c 0001 0 0000 40 01 d868 192.0.2.1 192.0.2.2 01010101010101010101010101010101010101010101010101010101010101010101010101010101 | 4 f 00 007c 0001 0 0000 40 01 d868 192.0.2.1 192.0.2.2 01010101010101010101010101010101010101010101010101010101010101010101010101010101 | ||||
Lint: PEP8 E501 line too long (142 > 79 characters) Lint: PEP8 E501: line too long (142 > 79 characters) | |||||
| --- 192.0.2.2 ping statistics --- | --- 192.0.2.2 ping statistics --- | ||||
| 1 packets transmitted, 0 packets received, 100.0% packet loss | 1 packets transmitted, 0 packets received, 100.0% packet loss | ||||
| """, | """, | ||||
| "stderr": "", | "stderr": "", | ||||
| "redacted": False, | "redacted": False, | ||||
| }, | }, | ||||
| ▲ Show 20 Lines • Show All 157 Lines • ▼ Show 20 Lines | """, | ||||
| { | { | ||||
| "returncode": 0, | "returncode": 0, | ||||
| "stdout": """\ | "stdout": """\ | ||||
| PATTERN: 0x01 | PATTERN: 0x01 | ||||
| PING 192.0.2.2 (192.0.2.2): 56 data bytes | PING 192.0.2.2 (192.0.2.2): 56 data bytes | ||||
| 64 bytes from: icmp_seq=0 ttl= time= ms | 64 bytes from: icmp_seq=0 ttl= time= ms | ||||
| wrong data byte #55 should be 0x1 but was 0x0 | wrong data byte #55 should be 0x1 but was 0x0 | ||||
| cp: xx xx xx xx xx xx xx xx | cp: xx xx xx xx xx xx xx xx | ||||
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | ||||
Lint: PEP8 E101 indentation contains mixed spaces and tabs Lint: PEP8 E101: indentation contains mixed spaces and tabs | |||||
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | ||||
Lint: PEP8 E101 indentation contains mixed spaces and tabs Lint: PEP8 E101: indentation contains mixed spaces and tabs | |||||
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 | ||||
Lint: PEP8 E101 indentation contains mixed spaces and tabs Lint: PEP8 E101: indentation contains mixed spaces and tabs | |||||
| dp: xx xx xx xx xx xx xx xx | dp: xx xx xx xx xx xx xx xx | ||||
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | ||||
Lint: PEP8 E101 indentation contains mixed spaces and tabs Lint: PEP8 E101: indentation contains mixed spaces and tabs | |||||
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | ||||
Lint: PEP8 E101 indentation contains mixed spaces and tabs Lint: PEP8 E101: indentation contains mixed spaces and tabs | |||||
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | ||||
Lint: PEP8 E101 indentation contains mixed spaces and tabs Lint: PEP8 E101: indentation contains mixed spaces and tabs | |||||
| --- 192.0.2.2 ping statistics --- | --- 192.0.2.2 ping statistics --- | ||||
| 1 packets transmitted, 1 packets received, 0.0% packet loss | 1 packets transmitted, 1 packets received, 0.0% packet loss | ||||
| round-trip min/avg/max/stddev = /// ms | round-trip min/avg/max/stddev = /// ms | ||||
| """, | """, | ||||
| "stderr": "", | "stderr": "", | ||||
Lint: PEP8 E101 indentation contains mixed spaces and tabs Lint: PEP8 E101: indentation contains mixed spaces and tabs | |||||
| "redacted": True, | "redacted": True, | ||||
| }, | }, | ||||
| id="_0_0_special_wrong", | id="_0_0_special_wrong", | ||||
| ), | ), | ||||
| ] | ] | ||||
| @pytest.mark.parametrize("pinger_kargs, expected", pinger_testdata) | @pytest.mark.parametrize("pinger_kargs, expected", pinger_testdata) | ||||
| @pytest.mark.require_progs(["scapy"]) | @pytest.mark.require_progs(["scapy"]) | ||||
| Show All 12 Lines | |||||
missing whitespace around operator