Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delegate port generation for the tests to the OS #1192

Closed
25 changes: 25 additions & 0 deletions tests/fast_server_exit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python

# Copyright 2020, TUF contributors
# SPDX-License-Identifier: MIT OR Apache-2.0

"""
<Program Name>
fast_server_exit.py

<Author>
Martin Vrachev.

<Started>
October 29, 2020.

<Copyright>
See LICENSE-MIT OR LICENSE for licensing information.

<Purpose>
Used for tests in tests/test_utils.py.
"""

import sys

sys.exit(0)
24 changes: 11 additions & 13 deletions tests/proxy_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,25 +462,21 @@ def test(HandlerClass=ProxyRequestHandler, ServerClass=ThreadingHTTPServer, prot
global INTERCEPT
global TARGET_SERVER_CA_FILEPATH

if sys.argv[1:]:
port = int(sys.argv[1])
else:
port = 8080
server_address = ('localhost', port)
server_address = ('localhost', 0)

# MODIFIED: Argument added, conditional below added to control INTERCEPT
# setting.
if len(sys.argv) > 2:
if sys.argv[2].lower() == 'intercept':
if len(sys.argv) > 1:
if sys.argv[1].lower() == 'intercept':
INTERCEPT = True

# MODIFIED: Argument added to control certificate(s) the proxy expects of
# the target server(s), and added default value.
if len(sys.argv) > 3:
if os.path.exists(sys.argv[3]):
TARGET_SERVER_CA_FILEPATH = sys.argv[3]
if len(sys.argv) > 2:
if os.path.exists(sys.argv[2]):
TARGET_SERVER_CA_FILEPATH = sys.argv[2]
else:
raise Exception('Target server cert file not found: ' + sys.argv[3])
raise Exception('Target server cert file not found: ' + sys.argv[2])

# MODIFIED: Create the target-host-specific proxy certificates directory if
# it doesn't already exist.
Expand All @@ -490,11 +486,13 @@ def test(HandlerClass=ProxyRequestHandler, ServerClass=ThreadingHTTPServer, prot

HandlerClass.protocol_version = protocol
httpd = ServerClass(server_address, HandlerClass)

sa = httpd.socket.getsockname()
print "Serving HTTP Proxy on", sa[0], "port", sa[1], "..."
port_message = 'bind succeeded, server port is: ' + str(sa[1])
print(port_message)
print("Serving HTTP Proxy on", sa[0], "port", sa[1], "...")
httpd.serve_forever()



if __name__ == '__main__':
test()
8 changes: 2 additions & 6 deletions tests/repository_data/map.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@
}
],
"repositories": {
"test_repository1": [
"http://localhost:30001"
],
"test_repository2": [
"http://localhost:30002"
]
"test_repository1": [],
"test_repository2": []
}
}
25 changes: 9 additions & 16 deletions tests/simple_https_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,28 @@
from __future__ import unicode_literals

import sys
import random
import ssl
import os
import six

PORT = 0

keyfile = os.path.join('ssl_certs', 'ssl_cert.key')
certfile = os.path.join('ssl_certs', 'ssl_cert.crt')

if len(sys.argv) > 1:
PORT = int(sys.argv[1])

else:
PORT = random.randint(30000, 45000)

if len(sys.argv) > 2:

if os.path.exists(sys.argv[2]):
certfile = sys.argv[2]
if len(sys.argv) > 1:
if os.path.exists(sys.argv[1]):
certfile = sys.argv[1]
else:
print('simple_https_server: cert file not found: ' + sys.argv[2] +
print('simple_https_server: cert file not found: ' + sys.argv[1] +
'; using default: ' + certfile)

httpd = six.moves.BaseHTTPServer.HTTPServer(('localhost', PORT),
six.moves.SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd = six.moves.BaseHTTPServer.HTTPServer(('localhost', 0),
six.moves.SimpleHTTPServer.SimpleHTTPRequestHandler)

httpd.socket = ssl.wrap_socket(
httpd.socket, keyfile=keyfile, certfile=certfile, server_side=True)

#print('Starting https server on port: ' + str(PORT))
port_message = 'bind succeeded, server port is: ' \
+ str(httpd.server_address[1])
print(port_message)
httpd.serve_forever()
13 changes: 4 additions & 9 deletions tests/simple_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,6 @@
import six
from six.moves.SimpleHTTPServer import SimpleHTTPRequestHandler

PORT = 0

if len(sys.argv) > 1:
PORT = int(sys.argv[1])

else:
PORT = random.randint(30000, 45000)


class QuietHTTPRequestHandler(SimpleHTTPRequestHandler):
"""A SimpleHTTPRequestHandler that does not write incoming requests to
Expand All @@ -73,6 +65,9 @@ def log_request(self, code='-', size='-'):
# Allow re-use so you can re-run tests as often as you want even if the
# tests re-use ports. Otherwise TCP TIME-WAIT prevents reuse for ~1 minute
six.moves.socketserver.TCPServer.allow_reuse_address = True
httpd = six.moves.socketserver.TCPServer(('', PORT), handler)

httpd = six.moves.socketserver.TCPServer(('localhost', 0), handler)
port_message = 'bind succeeded, server port is: ' \
+ str(httpd.server_address[1])
print(port_message)
httpd.serve_forever()
48 changes: 11 additions & 37 deletions tests/slow_retrieval_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,10 @@
import os
import sys
import time
import random

import six


# Modify the HTTPServer class to pass the 'test_mode' argument to
# do_GET() function.
class HTTPServer_Test(six.moves.BaseHTTPServer.HTTPServer):
def __init__(self, server_address, Handler, test_mode):
six.moves.BaseHTTPServer.HTTPServer.__init__(self, server_address, Handler)
self.test_mode = test_mode



# HTTP request handler.
class Handler(six.moves.BaseHTTPServer.BaseHTTPRequestHandler):
Expand All @@ -62,38 +53,21 @@ def do_GET(self):
self.send_header('Content-length', str(len(data)))
self.end_headers()

if self.server.test_mode == 'mode_1':
# Before sending any data, the server does nothing for a long time.
DELAY = 40
time.sleep(DELAY)
self.wfile.write(data)

return

# 'mode_2'
else:
DELAY = 1
# Throttle the file by sending a character every DELAY seconds.
for i in range(len(data)):
self.wfile.write(data[i].encode('utf-8'))
time.sleep(DELAY)

return
# Before sending any data, the server does nothing for a long time.
DELAY = 40
time.sleep(DELAY)
self.wfile.write((data.encode('utf-8')))

except IOError as e:
self.send_error(404, 'File Not Found!')



def run(port, test_mode):
server_address = ('localhost', port)
httpd = HTTPServer_Test(server_address, Handler, test_mode)
httpd.handle_request()



if __name__ == '__main__':
port = int(sys.argv[1])
test_mode = sys.argv[2]
assert test_mode in ('mode_1', 'mode_2')
run(port, test_mode)
server_address = ('localhost', 0)

httpd = six.moves.BaseHTTPServer.HTTPServer(server_address, Handler)
port_message = 'bind succeeded, server port is: ' \
+ str(httpd.server_address[1])
print(port_message)
httpd.serve_forever()
24 changes: 12 additions & 12 deletions tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,29 +258,29 @@ def test_https_connection(self):
# 4: run with an HTTPS certificate that is expired
# Be sure to offset from the port used in setUp to avoid collision.

port1 = self.server_process_handler.port + 1
port2 = self.server_process_handler.port + 2
port3 = self.server_process_handler.port + 3
port4 = self.server_process_handler.port + 4

good_https_server_handler = utils.TestServerProcess(log=logger,
server='simple_https_server.py', port=port1,
server='simple_https_server.py',
extra_cmd_args=[good_cert_fname])
good2_https_server_handler = utils.TestServerProcess(log=logger,
server='simple_https_server.py', port=port2,
server='simple_https_server.py',
extra_cmd_args=[good2_cert_fname])
bad_https_server_handler = utils.TestServerProcess(log=logger,
server='simple_https_server.py', port=port3,
server='simple_https_server.py',
extra_cmd_args=[bad_cert_fname])
expd_https_server_handler = utils.TestServerProcess(log=logger,
server='simple_https_server.py', port=port4,
server='simple_https_server.py',
extra_cmd_args=[expired_cert_fname])

suffix = '/' + os.path.basename(target_filepath)
good_https_url = 'https://localhost:' + str(port1) + suffix
good2_https_url = 'https://localhost:' + str(port2) + suffix
bad_https_url = 'https://localhost:' + str(port3) + suffix
expired_https_url = 'https://localhost:' + str(port4) + suffix
good_https_url = 'https://localhost:' \
+ str(good_https_server_handler.port) + suffix
good2_https_url = 'https://localhost:' \
+ str(good2_https_server_handler.port) + suffix
bad_https_url = 'https://localhost:' \
+ str(bad_https_server_handler.port) + suffix
expired_https_url = 'https://localhost:' \
+ str(expd_https_server_handler.port) + suffix

# Download the target file using an HTTPS connection.

Expand Down
1 change: 0 additions & 1 deletion tests/test_mirrors.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

import securesystemslib
import securesystemslib.util
import six


class TestMirrors(unittest_toolbox.Modified_TestCase):
Expand Down
24 changes: 8 additions & 16 deletions tests/test_multiple_repositories_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

import os
import tempfile
import random
import logging
import shutil
import unittest
Expand Down Expand Up @@ -119,13 +118,6 @@ def setUp(self):
# the pre-generated metadata files have a specific structure, such
# as a delegated role 'targets/role1', three target files, five key files,
# etc.
self.SERVER_PORT = random.SystemRandom().randint(30000, 45000)
self.SERVER_PORT2 = random.SystemRandom().randint(30000, 45000)

# Avoid duplicate port numbers, to prevent multiple localhosts from
# listening on the same port.
while self.SERVER_PORT == self.SERVER_PORT2:
self.SERVER_PORT2 = random.SystemRandom().randint(30000, 45000)

# Needed because in some tests simple_server.py cannot be found.
# The reason is that the current working directory
Expand All @@ -134,20 +126,18 @@ def setUp(self):

# Creates a subprocess running server and uses temp file for logging.
self.server_process_handler = utils.TestServerProcess(log=logger,
port=self.SERVER_PORT, server=SIMPLE_SERVER_PATH,
popen_cwd=self.repository_directory)
server=SIMPLE_SERVER_PATH, popen_cwd=self.repository_directory)

logger.debug('Server process started.')

# Creates a subprocess running server and uses temp file for logging.
self.server_process_handler2 = utils.TestServerProcess(log=logger,
port=self.SERVER_PORT2, server=SIMPLE_SERVER_PATH,
popen_cwd=self.repository_directory2)
server=SIMPLE_SERVER_PATH, popen_cwd=self.repository_directory2)

logger.debug('Server process 2 started.')

url_prefix = 'http://localhost:' + str(self.SERVER_PORT)
url_prefix2 = 'http://localhost:' + str(self.SERVER_PORT2)
url_prefix = 'http://localhost:' + str(self.server_process_handler.port)
url_prefix2 = 'http://localhost:' + str(self.server_process_handler2.port)

self.repository_mirrors = {'mirror1': {'url_prefix': url_prefix,
'metadata_path': 'metadata',
Expand Down Expand Up @@ -265,8 +255,10 @@ def test_repository_tool(self):

# Test the behavior of the multi-repository updater.
map_file = securesystemslib.util.load_json_file(self.map_file)
map_file['repositories'][self.repository_name] = ['http://localhost:' + str(self.SERVER_PORT)]
map_file['repositories'][self.repository_name2] = ['http://localhost:' + str(self.SERVER_PORT2)]
map_file['repositories'][self.repository_name] = ['http://localhost:' \
+ str(self.server_process_handler.port)]
map_file['repositories'][self.repository_name2] = ['http://localhost:' \
+ str(self.server_process_handler2.port)]
with open(self.map_file, 'w') as file_object:
file_object.write(json.dumps(map_file))

Expand Down
11 changes: 4 additions & 7 deletions tests/test_proxy_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,13 @@ def setUpClass(cls):

# Launch an HTTPS server (serves files in the current dir).
cls.https_server_handler = utils.TestServerProcess(log=logger,
server='simple_https_server.py',
port=cls.http_server_handler.port + 1)
server='simple_https_server.py')

# Launch an HTTP proxy server derived from inaz2/proxy2.
# This one is able to handle HTTP CONNECT requests, and so can pass HTTPS
# requests on to the target server.
cls.http_proxy_handler = utils.TestServerProcess(log=logger,
server='proxy_server.py',
port=cls.http_server_handler.port + 2)
server='proxy_server.py')

# Note that the HTTP proxy server's address uses http://, regardless of the
# type of connection used with the target server.
Expand All @@ -109,9 +107,8 @@ def setUpClass(cls):
# This is only relevant if the proxy is in intercept mode.
good_cert_fpath = os.path.join('ssl_certs', 'ssl_cert.crt')
cls.https_proxy_handler = utils.TestServerProcess(log=logger,
server='proxy_server.py',
port=cls.http_server_handler.port + 3,
extra_cmd_args=['intercept', good_cert_fpath])
server='proxy_server.py', extra_cmd_args=['intercept',
good_cert_fpath])

# Note that the HTTPS proxy server's address uses https://, regardless of
# the type of connection used with the target server.
Expand Down
Loading