NDOJ/judge/bridge/echo_test_client.py

83 lines
2.1 KiB
Python
Raw Normal View History

2020-07-19 21:27:14 +00:00
import os
2020-01-21 06:35:58 +00:00
import socket
import struct
import time
import zlib
2022-05-14 17:57:27 +00:00
size_pack = struct.Struct("!I")
2020-01-21 06:35:58 +00:00
def open_connection():
sock = socket.create_connection((host, port))
return sock
def zlibify(data):
2022-05-14 17:57:27 +00:00
data = zlib.compress(data.encode("utf-8"))
2020-01-21 06:35:58 +00:00
return size_pack.pack(len(data)) + data
def dezlibify(data, skip_head=True):
if skip_head:
2022-05-14 17:57:27 +00:00
data = data[size_pack.size :]
return zlib.decompress(data).decode("utf-8")
2020-01-21 06:35:58 +00:00
def main():
global host, port
import argparse
2022-05-14 17:57:27 +00:00
2020-01-21 06:35:58 +00:00
parser = argparse.ArgumentParser()
2022-05-14 17:57:27 +00:00
parser.add_argument("-l", "--host", default="localhost")
parser.add_argument("-p", "--port", default=9999, type=int)
2020-01-21 06:35:58 +00:00
args = parser.parse_args()
host, port = args.host, args.port
2022-05-14 17:57:27 +00:00
print("Opening idle connection:", end=" ")
2020-01-21 06:35:58 +00:00
s1 = open_connection()
2022-05-14 17:57:27 +00:00
print("Success")
print("Opening hello world connection:", end=" ")
2020-01-21 06:35:58 +00:00
s2 = open_connection()
2022-05-14 17:57:27 +00:00
print("Success")
print("Sending Hello, World!", end=" ")
s2.sendall(zlibify("Hello, World!"))
print("Success")
print("Testing blank connection:", end=" ")
2020-01-21 06:35:58 +00:00
s3 = open_connection()
s3.close()
2022-05-14 17:57:27 +00:00
print("Success")
2020-01-21 06:35:58 +00:00
result = dezlibify(s2.recv(1024))
2022-05-14 17:57:27 +00:00
assert result == "Hello, World!"
2020-01-21 06:35:58 +00:00
print(result)
s2.close()
2022-05-14 17:57:27 +00:00
print("Large random data test:", end=" ")
2020-01-21 06:35:58 +00:00
s4 = open_connection()
2022-05-14 17:57:27 +00:00
data = os.urandom(1000000).decode("iso-8859-1")
print("Generated", end=" ")
2020-01-21 06:35:58 +00:00
s4.sendall(zlibify(data))
2022-05-14 17:57:27 +00:00
print("Sent", end=" ")
result = b""
2020-01-21 06:35:58 +00:00
while len(result) < size_pack.size:
result += s4.recv(1024)
2022-05-14 17:57:27 +00:00
size = size_pack.unpack(result[: size_pack.size])[0]
result = result[size_pack.size :]
2020-01-21 06:35:58 +00:00
while len(result) < size:
result += s4.recv(1024)
2022-05-14 17:57:27 +00:00
print("Received", end=" ")
2020-01-21 06:35:58 +00:00
assert dezlibify(result, False) == data
2022-05-14 17:57:27 +00:00
print("Success")
2020-01-21 06:35:58 +00:00
s4.close()
2022-05-14 17:57:27 +00:00
print("Test malformed connection:", end=" ")
2020-01-21 06:35:58 +00:00
s5 = open_connection()
2022-05-14 17:57:27 +00:00
s5.sendall(data[:100000].encode("utf-8"))
2020-01-21 06:35:58 +00:00
s5.close()
2022-05-14 17:57:27 +00:00
print("Success")
print("Waiting for timeout to close idle connection:", end=" ")
2020-01-21 06:35:58 +00:00
time.sleep(6)
2022-05-14 17:57:27 +00:00
print("Done")
2020-01-21 06:35:58 +00:00
s1.close()
2022-05-14 17:57:27 +00:00
if __name__ == "__main__":
2020-01-21 06:35:58 +00:00
main()