python-tuf/tests/simple_https_server.py
Sebastien Awwad 8d64b5a2e1
Test: test download.py w/ untrusted or bad-hostname SSL certs
Rewrite test_https_connection to do a more thorough test, including
the use of an unknown certificate and the use of a good certificate
which lists a hostname not matching that expected in the connection.

In the process, made some small changes to the simple_https_server
module used in tests (takes an extra argument: certificate file to
use). Given the extent of the changes to test_https_connection, I
also made some style adjustments to better match our code style
guidelines.

I also reduced the length of a delay after the https servers
started from 1s to 0.2s, as part of a general campaign to speed up
the TUF tests. 200ms should do to start the servers, and if not,
I'll adjust it upward.

Signed-off-by: Sebastien Awwad <sebastien.awwad@gmail.com>
2018-09-13 12:22:37 -04:00

81 lines
2 KiB
Python
Executable file

#!/usr/bin/env python
# Copyright 2014 - 2017, New York University and the TUF contributors
# SPDX-License-Identifier: MIT OR Apache-2.0
"""
<Program>
simple_https_server.py
<Author>
Vladimir Diaz.
<Started>
June 17, 2014
<Copyright>
See LICENSE-MIT OR LICENSE for licensing information.
<Purpose>
Provide a simple https server that can be used by the unit tests. For
example, 'download.py' can connect to the https server started by this module
to verify that https downloads are permitted.
<Reference>
ssl.wrap_socket:
https://docs.python.org/2/library/ssl.html#functions-constants-and-exceptions
SimpleHTTPServer:
http://docs.python.org/library/simplehttpserver.html#module-SimpleHTTPServer
"""
# Help with Python 3 compatibility, where the print statement is a function, an
# implicit relative import is invalid, and the '/' operator performs true
# division. Example: print 'hello world' raises a 'SyntaxError' exception.
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
import sys
import random
import ssl
import os
import six
PORT = 0
keyfile = 'ssl_cert.key'
certfile = 'ssl_cert.crt'
def _generate_random_port():
return random.randint(30000, 45000)
if len(sys.argv) > 1:
try:
PORT = int(sys.argv[1])
if PORT < 30000 or PORT > 45000:
raise ValueError
except ValueError:
PORT = _generate_random_port()
else:
PORT = _generate_random_port()
if len(sys.argv) > 2:
if os.path.exists(sys.argv[2]):
certfile = sys.argv[2]
else:
print('simple_https_server: cert file not found: ' + sys.argv[2] +
'; using default: ' + certfile)
httpd = six.moves.BaseHTTPServer.HTTPServer(('localhost', PORT),
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))
httpd.serve_forever()