-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JIRA: CI-326
- Loading branch information
Showing
2 changed files
with
268 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
# Phoenix-RTOS | ||
# | ||
# phoenix-rtos-tests | ||
# | ||
# ping command test | ||
# | ||
# Copyright 2023 Phoenix Systems | ||
# Author: Damian Modzelewski | ||
# | ||
# This file is part of Phoenix-RTOS. | ||
# | ||
# %LICENSE% | ||
# | ||
|
||
import psh.tools.psh as psh | ||
import pexpect | ||
import time | ||
import subprocess | ||
import re | ||
|
||
target_ip = "" | ||
|
||
|
||
def ping_output_regex(target, iteration): | ||
iter_str = r"{" + str(iteration) + r"}" | ||
|
||
psh_outpu_regex = r"(([0-9]+[a-z\s]+ " | ||
psh_outpu_regex += str(target) | ||
psh_outpu_regex += r": ttl=[0-9]+ icmp_seq=[0-9]+ time=([0-9.]+) ms)" | ||
psh_outpu_regex += f"{psh.EOL}){iter_str}" | ||
|
||
return psh_outpu_regex | ||
|
||
|
||
def runup_linuxrc(p): | ||
global target_ip | ||
|
||
p.sendline("./linuxrc") | ||
rc = p.expect_exact(["root@?:~ # ", pexpect.TIMEOUT, "(psh)% "], timeout=10) | ||
|
||
if rc == 1: | ||
raise AssertionError("linuxrc Timeout!") | ||
if rc == 2: | ||
raise AssertionError("linuxrc doesn't exist!") | ||
|
||
# collecting valid target ip address | ||
p.sendline("ifconfig") | ||
p.expect_exact("root@?:~ # ", timeout=10) | ||
buff = p.before | ||
filtered = re.findall(r"[0-9]+.[0-9]+.72.[0-9]+", buff) | ||
target_ip = filtered[0] | ||
|
||
p.sendline("psh") | ||
psh_cmd = p.expect_exact(["(psh)% ", pexpect.TIMEOUT, "root@?:~ # "], timeout=10) | ||
|
||
if psh_cmd == 1: | ||
raise AssertionError("psh Timeout!") | ||
if psh_cmd == 2: | ||
raise AssertionError("psh fail!") | ||
|
||
|
||
def psh_ping_linuxrc_off(p): | ||
# wait for last lwip log | ||
time.sleep(5) | ||
|
||
# google dns ping (fail) | ||
ping_target = "8.8.8.8" | ||
psh.assert_cmd(p, f"ping {ping_target}", expected="ping: Fail to send a packet!", result="fail", is_regex=False) | ||
|
||
# self ping | ||
ping_target = "127.0.0.1" | ||
psh.assert_cmd( | ||
p, f"ping {ping_target}", expected=ping_output_regex(ping_target, 5), result="success", is_regex=True | ||
) | ||
|
||
|
||
def psh_ping_help(p): | ||
help = ( | ||
"Usage: ping [options] address", | ||
"Options", | ||
" -h: prints help", | ||
" -c: count, number of requests to be sent, default 5", | ||
" -i: interval in milliseconds, minimum 200 ms, default 1000", | ||
" -t: IP Time To Live, default 64", | ||
" -s: payload size, default 56, maximum 2040", | ||
" -W: socket timetout, default 2000", | ||
"", | ||
) | ||
psh.assert_cmd(p, "ping -h", expected=help, result="success", is_regex=False) | ||
|
||
|
||
def psh_ping(p): | ||
counter = 0 | ||
|
||
# google dns ping after linuxrc run (success) | ||
ping_target = "8.8.8.8" | ||
psh.assert_cmd( | ||
p, f"ping {ping_target}", expected=ping_output_regex(ping_target, 5), result="success", is_regex=True | ||
) | ||
|
||
# find TTL route | ||
for i in range(1, 10): | ||
psh.assert_cmd( | ||
p, | ||
f"ping -t {i} {ping_target}", | ||
expected=ping_output_regex(ping_target, 5) + "|Host timeout" + psh.EOL, | ||
result="dont-check", | ||
is_regex=True, | ||
) | ||
|
||
p.sendline("echo $?") | ||
p.readline() | ||
out = p.readline() | ||
if int(out) == 0: | ||
counter += 1 | ||
|
||
assert counter > 0 | ||
|
||
# long ping (assert last for only 30 seconds) | ||
psh.assert_cmd( | ||
p, | ||
f"ping -c 50 {ping_target}", | ||
expected=ping_output_regex(ping_target, 50), | ||
result="success", | ||
is_regex=True, | ||
timeout=50, | ||
) | ||
|
||
# ping with long time to respond (IBM cloud name server). | ||
ping_target = "67.228.254.4" | ||
psh.assert_cmd( | ||
p, f"ping {ping_target}", expected=ping_output_regex(ping_target, 5), result="success", is_regex=True | ||
) | ||
|
||
# ping max payload (truncated to 1480) | ||
psh.assert_cmd( | ||
p, f"ping -s 2000 {ping_target}", expected=ping_output_regex(ping_target, 5), result="success", is_regex=True | ||
) | ||
|
||
# ping max payload (Cloudflare DNS capable to hold payload) | ||
ping_target = "1.0.0.1" | ||
psh.assert_cmd( | ||
p, f"ping -s 2000 {ping_target}", expected=ping_output_regex(ping_target, 5), result="success", is_regex=True | ||
) | ||
|
||
|
||
def psh_ping_errors(p): | ||
ping_target = "192.168.0.1" | ||
psh.assert_cmd(p, f"ping {ping_target}", expected="Host timeout", result="fail", is_regex=False) | ||
|
||
# invalid ping target | ||
ping_target = "" | ||
psh.assert_cmd(p, f"ping -c 4 {ping_target}", expected="ping: Expected address!", result="fail", is_regex=False) | ||
|
||
# invalid ip | ||
ping_target = "localhost" | ||
psh.assert_cmd(p, f"ping {ping_target}", expected="ping: Invalid IP address!", result="fail", is_regex=False) | ||
|
||
# invalid ping count | ||
ping_target = "8.8.8.8" | ||
psh.assert_cmd(p, f"ping -c -1 {ping_target}", expected="ping: Wrong count value!", result="fail", is_regex=False) | ||
|
||
# invalid ttl value | ||
psh.assert_cmd(p, f"ping -t -1 {ping_target}", expected="ping: Wrong ttl value!", result="fail", is_regex=False) | ||
|
||
# invalid interval value | ||
psh.assert_cmd( | ||
p, f"ping -i -1 {ping_target}", expected="ping: Wrong interval value!", result="fail", is_regex=False | ||
) | ||
|
||
# invalid timeout | ||
psh.assert_cmd(p, f"ping -W -1 {ping_target}", expected="ping: Wrong timeout value!", result="fail", is_regex=False) | ||
|
||
# invlid_payload_len | ||
psh.assert_cmd(p, f"ping -s 2041 {ping_target}", expected="ping: Wrong payload len", result="fail", is_regex=False) | ||
|
||
|
||
def host_ping(): | ||
global target_ip | ||
subprocess.call("echo", shell=True) | ||
# checking if target is alive | ||
returned_value = subprocess.call(f"ping -c 5 {target_ip}", shell=True) | ||
assert returned_value == 0 | ||
subprocess.call("echo", shell=True) | ||
|
||
# correct ttl to target should be no more than 2 | ||
returned_value = subprocess.call(f"ping -t 2 -c 2 {target_ip}", shell=True) | ||
assert returned_value == 0 | ||
subprocess.call("echo", shell=True) | ||
|
||
# send bigger than usual size of ping payload | ||
returned_value = subprocess.call(f"ping -s 2040 -c 5 {target_ip}", shell=True) | ||
assert returned_value == 0 | ||
subprocess.call("echo", shell=True) | ||
|
||
|
||
@psh.run | ||
def harness(p): | ||
psh_ping_linuxrc_off(p) | ||
psh_ping_help(p) | ||
|
||
runup_linuxrc(p) | ||
|
||
psh_ping(p) | ||
psh_ping_errors(p) | ||
host_ping() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters