Add http server test (#475)

This commit is contained in:
Chilledheart 2023-03-17 06:47:03 +08:00 committed by GitHub
parent c885c1d41e
commit fcfad1f823
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 6 deletions

View File

@ -13,6 +13,7 @@ parser = argparse.ArgumentParser()
parser.add_argument('--naive', required=True) parser.add_argument('--naive', required=True)
parser.add_argument('--rootfs') parser.add_argument('--rootfs')
parser.add_argument('--target_cpu') parser.add_argument('--target_cpu')
parser.add_argument('--server_protocol', choices=['http', 'https'], default='https')
argv = parser.parse_args() argv = parser.parse_args()
if argv.rootfs: if argv.rootfs:
@ -21,6 +22,8 @@ if argv.rootfs:
except OSError: except OSError:
pass pass
server_protocol = argv.server_protocol
_, certfile = tempfile.mkstemp() _, certfile = tempfile.mkstemp()
result = subprocess.run( result = subprocess.run(
@ -28,13 +31,15 @@ result = subprocess.run(
result.check_returncode() result.check_returncode()
HTTPS_SERVER_HOSTNAME = '127.0.0.1' HTTPS_SERVER_HOSTNAME = '127.0.0.1'
HTTP_SERVER_PORT = 60443 HTTP_SERVER_PORT = 60443 if server_protocol == 'https' else 60080
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ssl_context.load_cert_chain(certfile=certfile)
httpd = http.server.HTTPServer( httpd = http.server.HTTPServer(
(HTTPS_SERVER_HOSTNAME, HTTP_SERVER_PORT), http.server.SimpleHTTPRequestHandler) (HTTPS_SERVER_HOSTNAME, HTTP_SERVER_PORT), http.server.SimpleHTTPRequestHandler)
httpd.timeout = 1 httpd.timeout = 1
httpd.allow_reuse_address = True httpd.allow_reuse_address = True
if server_protocol == 'https':
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ssl_context.load_cert_chain(certfile=certfile)
httpd.socket = ssl_context.wrap_socket(httpd.socket, server_side=True) httpd.socket = ssl_context.wrap_socket(httpd.socket, server_side=True)
httpd_thread = threading.Thread( httpd_thread = threading.Thread(
@ -43,7 +48,7 @@ httpd_thread.start()
def test_https_server(hostname, port, proxy=None): def test_https_server(hostname, port, proxy=None):
url = f'https://{hostname}:{port}/404' url = f'{server_protocol}://{hostname}:{port}/404'
cmdline = ['curl', '-k', '-s'] cmdline = ['curl', '-k', '-s']
if proxy: if proxy:
cmdline.extend(['--proxy', proxy]) cmdline.extend(['--proxy', proxy])

View File

@ -16,4 +16,5 @@ elif [ "$WITH_SYSROOT" ]; then
fi fi
cd /tmp cd /tmp
python3 "$script_dir"/basic.py --naive="$naive" --rootfs="$rootfs" --target_cpu="$target_cpu" python3 "$script_dir"/basic.py --naive="$naive" --rootfs="$rootfs" --target_cpu="$target_cpu" --server_protocol=https
python3 "$script_dir"/basic.py --naive="$naive" --rootfs="$rootfs" --target_cpu="$target_cpu" --server_protocol=http