Skip to content

Commit

Permalink
network: add tcp test
Browse files Browse the repository at this point in the history
JIRA: CI-343
  • Loading branch information
adamdebek committed Apr 24, 2024
1 parent 7212860 commit cceda4f
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions network/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ def get_config():
target_mask = host_mask

if doc_ip is not None:
octets = doc_ip.split('.')
fourth_octet = int(octets[3]) + 1
doc_ip = '.'.join(octets[:3] + [str(fourth_octet)])
host_ip = doc_ip

return host_ip, target_ip, target_mask
Expand All @@ -52,26 +49,26 @@ def target_setup(p, iface, ip, netmask):
psh.assert_prompt_after_cmd(p, ifconfig_setup_cmd, "success")


def con_setup(dut: Dut, host_ip, host_port):
def con_setup(dut: Dut, host_ip, target_ip, port):
try:
host_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
host_socket.bind((host_ip, host_port))
host_socket.bind((host_ip, port))
host_socket.listen()

except socket.error as e:
print(f"Failed to create socket: {e}")
exit(1)

dut.send(f"/bin/test-network {host_ip}:{host_port}\n")
dut.expect(f"/bin/test-network {host_ip}:{host_port}" + psh.EOL)
dut.send(f"/bin/test-network {target_ip}:{port}\n")
dut.expect(f"/bin/test-network {target_ip}:{port}" + psh.EOL)

ready_to_read, _, _ = select.select([host_socket], [], [], 20)

if ready_to_read:
peer_socket, _ = host_socket.accept()
else:
print("connect timeout")
peer_socket = None

host_socket.close()

Expand Down Expand Up @@ -99,10 +96,11 @@ def harness(dut: Dut, ctx: TestContext, result: TestResult):
print(target_ip)
print(target_mask)

target_setup(dut, 'en1', target_ip, target_mask)
peer_socket = con_setup(dut, host_ip, port)
target_setup(dut, 'en1', host_ip, target_ip, target_mask)
peer_socket = con_setup(dut, host_ip, target_ip, port)
random_bytes = bytes([random.randint(0, 255) for _ in range(128)])
peer_socket.send(random_bytes)
if peer_socket is not None:
peer_socket.send(random_bytes)

while True:
idx = dut.expect([assert_re, result_re, result_fail_re, final_re], timeout=timeout_val)
Expand Down

0 comments on commit cceda4f

Please sign in to comment.