From 591c48a6903bbf324361610f81c628302cae7049 Mon Sep 17 00:00:00 2001 From: jahoti Date: Sat, 4 Sep 2021 00:00:00 +0000 Subject: Make test suite mildly usable Allow test/server.py to be run as a command and add some "webpages" for it. --- copyright | 2 +- test/data/pages/gotmyowndomain.html | 14 +++++++ test/data/pages/gotmyowndomain_https.html | 14 +++++++ test/init.sh | 4 ++ test/misc_constants.py | 34 ++++++++++++++++ test/proxy_core.py | 4 +- test/server.py | 65 +++++++++++++------------------ test/world_wide_library.py | 15 +++++++ 8 files changed, 111 insertions(+), 41 deletions(-) create mode 100644 test/data/pages/gotmyowndomain.html create mode 100644 test/data/pages/gotmyowndomain_https.html mode change 100644 => 100755 test/init.sh create mode 100644 test/misc_constants.py mode change 100644 => 100755 test/server.py create mode 100644 test/world_wide_library.py diff --git a/copyright b/copyright index 23ffb73..0831655 100644 --- a/copyright +++ b/copyright @@ -89,7 +89,7 @@ License: BSD-3 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -Files: test/gorilla.py +Files: test/gorilla.py test/misc_constants.py test/world_wide_library.py test/data/pages/* Copyright: 2021 jahoti 2021 jahoti License: AGPL-3+ or Alicense-1.0 diff --git a/test/data/pages/gotmyowndomain.html b/test/data/pages/gotmyowndomain.html new file mode 100644 index 0000000..daa9ab7 --- /dev/null +++ b/test/data/pages/gotmyowndomain.html @@ -0,0 +1,14 @@ + + + + + Schrodinger's Document + + + + A nice, simple page for testing. + + + diff --git a/test/data/pages/gotmyowndomain_https.html b/test/data/pages/gotmyowndomain_https.html new file mode 100644 index 0000000..ec2aa1f --- /dev/null +++ b/test/data/pages/gotmyowndomain_https.html @@ -0,0 +1,14 @@ + + + + + Schrodinger's Document + + + + A nice, simple page for testing (using HTTPS). + + + diff --git a/test/init.sh b/test/init.sh old mode 100644 new mode 100755 index 915db76..c0e7647 --- a/test/init.sh +++ b/test/init.sh @@ -7,6 +7,10 @@ # Initialize the root certificate for the tests proxy server # Make sure this is run in the directory where they will be put! + +if [ -n "$1" ]; then + cd "$1" +fi openssl genrsa -out ca.key 2048 openssl genrsa -out cert.key 2048 openssl req -new -x509 -days 3650 -key ca.key -out ca.crt -subj "/CN=Hachette Test" diff --git a/test/misc_constants.py b/test/misc_constants.py new file mode 100644 index 0000000..c6c134f --- /dev/null +++ b/test/misc_constants.py @@ -0,0 +1,34 @@ +# Copyright (C) 2021 jahoti +# Licensing information is collated in the `copyright` file + +""" +Miscellaneous data that were found useful +""" + +mime_types = { + "7z": "application/x-7z-compressed", "oga": "audio/ogg", + "abw": "application/x-abiword", "ogv": "video/ogg", + "arc": "application/x-freearc", "ogx": "application/ogg", + "bin": "application/octet-stream", "opus": "audio/opus", + "bz": "application/x-bzip", "otf": "font/otf", + "bz2": "application/x-bzip2", "pdf": "application/pdf", + "css": "text/css", "png": "image/png", + "csv": "text/csv", "sh": "application/x-sh", + "gif": "image/gif", "svg": "image/svg+xml", + "gz": "application/gzip", "tar": "application/x-tar", + "htm": "text/html", "ts": "video/mp2t", + "html": "text/html", "ttf": "font/ttf", + "ico": "image/vnd.microsoft.icon", "txt": "text/plain", + "js": "text/javascript", "wav": "audio/wav", + "jpeg": "image/jpeg", "weba": "audio/webm", + "jpg": "image/jpeg", "webm": "video/webm", + "json": "application/json", "woff": "font/woff", + "mjs": "text/javascript", "woff2": "font/woff2", + "mp3": "audio/mpeg", "xhtml": "application/xhtml+xml", + "mp4": "video/mp4", "zip": "application/zip", + "mpeg": "video/mpeg", + "odp": "application/vnd.oasis.opendocument.presentation", + "ods": "application/vnd.oasis.opendocument.spreadsheet", + "odt": "application/vnd.oasis.opendocument.text", + "xml": "application/xml" # text/xml if readable from casual users +} diff --git a/test/proxy_core.py b/test/proxy_core.py index da90455..dd4225d 100644 --- a/test/proxy_core.py +++ b/test/proxy_core.py @@ -5,7 +5,7 @@ """ The core for a "virtual network" proxy -Be sure to run this inside your intended certificates directory. +Be sure to set certdir to your intended certificates directory before running. """ import os, socket, ssl, subprocess, sys, threading, time @@ -20,8 +20,6 @@ def popen(command, *args, **kwargs): class ProxyRequestHandler(BaseHTTPRequestHandler): """Handles a network request made to the proxy""" - certdir = '' - def log_error(self, format, *args): # suppress "Request timed out: timeout('timed out',)" if isinstance(args[0], socket.timeout): diff --git a/test/server.py b/test/server.py old mode 100644 new mode 100755 index 83a72fa..58a84bd --- a/test/server.py +++ b/test/server.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 +# # Copyright (C) 2021 jahoti # Licensing information is collated in the `copyright` file @@ -6,41 +8,12 @@ A modular "virtual network" proxy, wrapping the classes in proxy_core.py """ -from proxy_core import * +import proxy_core from urllib.parse import parse_qs +from misc_constants import * +from world_wide_library import catalog as internet -internet = {} # Add info here later -mime_types = { - "7z": "application/x-7z-compressed", "oga": "audio/ogg", - "abw": "application/x-abiword", "ogv": "video/ogg", - "arc": "application/x-freearc", "ogx": "application/ogg", - "bin": "application/octet-stream", "opus": "audio/opus", - "bz": "application/x-bzip", "otf": "font/otf", - "bz2": "application/x-bzip2", "pdf": "application/pdf", - "css": "text/css", "png": "image/png", - "csv": "text/csv", "sh": "application/x-sh", - "gif": "image/gif", "svg": "image/svg+xml", - "gz": "application/gzip", "tar": "application/x-tar", - "htm": "text/html", "ts": "video/mp2t", - "html": "text/html", "ttf": "font/ttf", - "ico": "image/vnd.microsoft.icon", "txt": "text/plain", - "js": "text/javascript", "wav": "audio/wav", - "jpeg": "image/jpeg", "weba": "audio/webm", - "jpg": "image/jpeg", "webm": "video/webm", - "json": "application/json", "woff": "font/woff", - "mjs": "text/javascript", "woff2": "font/woff2", - "mp3": "audio/mpeg", "xhtml": "application/xhtml+xml", - "mp4": "video/mp4", "zip": "application/zip", - "mpeg": "video/mpeg", - "odp": "application/vnd.oasis.opendocument.presentation", - "ods": "application/vnd.oasis.opendocument.spreadsheet", - "odt": "application/vnd.oasis.opendocument.text", - "xml": "application/xml" # text/xml if readable from casual users -} - -class RequestHijacker(ProxyRequestHandler): - certdir = global_certdir - +class RequestHijacker(proxy_core.ProxyRequestHandler): def handle_request(self, req_body): path_components = self.path.split('?', maxsplit=1) path = path_components[0] @@ -103,8 +76,26 @@ class RequestHijacker(ProxyRequestHandler): def do_an_internet(certdir, port): """Start up the proxy/server""" - global global_certdir - global_certdir = certdir - - httpd = ThreadingHTTPServer(('', port), RequestHijacker) + proxy_core.certdir = certdir + httpd = proxy_core.ThreadingHTTPServer(('', port), RequestHijacker) httpd.serve_forever() + +if __name__ == '__main__': + import sys + def fail(msg, error_code): + print('Error:', msg) + print('Usage:', sys.argv[0], '[certificates directory] (port)') + sys.exit(error_code) + + if len(sys.argv) < 2: + fail('missing required argument "certificates directory".', 1) + + certdir = sys.argv[1] + if not proxy_core.os.path.isdir(certdir): + fail('selected certificate directory does not exist.', 2) + + port = sys.argv[2] if len(sys.argv) > 2 else '1337' + if not port.isnumeric(): + fail('port must be an integer.', 3) + + do_an_internet(certdir, int(port)) diff --git a/test/world_wide_library.py b/test/world_wide_library.py new file mode 100644 index 0000000..fc9e095 --- /dev/null +++ b/test/world_wide_library.py @@ -0,0 +1,15 @@ +# Copyright (C) 2021 jahoti +# Licensing information is collated in the `copyright` file + +""" +Our helpful little stand-in for the Internet +""" + +catalog = { + 'http://gotmyowndoma.in': (302, {'location': 'http://gotmyowndoma.in/index.html'}, b''), + 'http://gotmyowndoma.in/': (302, {'location': 'http://gotmyowndoma.in/index.html'}, b''), + 'http://gotmyowndoma.in/index.html': (200, {}, 'data/pages/gotmyowndomain.html'), + 'https://gotmyowndoma.in': (302, {'location': 'https://gotmyowndoma.in/index.html'}, b''), + 'https://gotmyowndoma.in/': (302, {'location': 'https://gotmyowndoma.in/index.html'}, b''), + 'https://gotmyowndoma.in/index.html': (200, {}, 'data/pages/gotmyowndomain_https.html') +} -- cgit v1.2.3