From 5b2a7a61fd5cf9982f105a3081c2ed7a298b71df Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Sat, 27 Nov 2021 13:14:28 +0100 Subject: add Selenium- and Python-based test system --- test/__init__.py | 1 + test/__main__.py | 59 +++++++++ test/data/pages/gotmyowndomain.html | 35 +++++ test/data/pages/gotmyowndomain_https.html | 35 +++++ test/default_profiles/icecat_empty/extensions.json | 1 + test/misc_constants.py | 70 ++++++++++ test/profiles.py | 66 ++++++++++ test/proxy_core.py | 141 +++++++++++++++++++++ test/server.py | 108 ++++++++++++++++ test/test_unit.py | 41 ++++++ test/world_wide_library.py | 39 ++++++ 11 files changed, 596 insertions(+) create mode 100644 test/__init__.py create mode 100644 test/__main__.py create mode 100644 test/data/pages/gotmyowndomain.html create mode 100644 test/data/pages/gotmyowndomain_https.html create mode 100644 test/default_profiles/icecat_empty/extensions.json create mode 100644 test/misc_constants.py create mode 100755 test/profiles.py create mode 100644 test/proxy_core.py create mode 100755 test/server.py create mode 100644 test/test_unit.py create mode 100644 test/world_wide_library.py (limited to 'test') diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..19b869e --- /dev/null +++ b/test/__init__.py @@ -0,0 +1 @@ +# SPDX-License-Identifier: CC0-1.0 diff --git a/test/__main__.py b/test/__main__.py new file mode 100644 index 0000000..c3437ea --- /dev/null +++ b/test/__main__.py @@ -0,0 +1,59 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later + +""" +Run a Firefox-type browser with WebDriver attached and Python console open +""" + +# This file is part of Haketilo. +# +# Copyright (C) 2021 jahoti +# Copyright (C) 2021 Wojtek Kosior +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +# +# I, Wojtek Kosior, thereby promise not to sue for violation of this +# file's license. Although I request that you do not make use this code +# in a proprietary program, I am not going to enforce this in court. + +import sys +import time +import code + +from .server import do_an_internet +from .misc_constants import * +from .profiles import firefox_safe_mode + +def fail(msg, error_code): + print('Error:', msg) + print('Usage:', sys.argv[0], '[certificates_directory] [proxy_port]') + sys.exit(error_code) + +certdir = Path(sys.argv[1]).resolve() if len(sys.argv) > 1 else default_cert_dir +if not certdir.is_dir(): + fail('selected certificate directory does not exist.', 2) + +port = sys.argv[2] if len(sys.argv) > 2 else str(default_proxy_port) +if not port.isnumeric(): + fail('port must be an integer.', 3) + +httpd = do_an_internet(certdir, int(port)) +driver = firefox_safe_mode(proxy_port=int(port)) + +print("You can now control the browser through 'driver' object") + +code.InteractiveConsole(locals=globals()).interact() + +driver.quit() +httpd.shutdown() diff --git a/test/data/pages/gotmyowndomain.html b/test/data/pages/gotmyowndomain.html new file mode 100644 index 0000000..42c26cc --- /dev/null +++ b/test/data/pages/gotmyowndomain.html @@ -0,0 +1,35 @@ + + + + + + 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..95c0be4 --- /dev/null +++ b/test/data/pages/gotmyowndomain_https.html @@ -0,0 +1,35 @@ + + + + + + Schrodinger's Document + + + A nice, simple page for testing (using HTTPS). + + + diff --git a/test/default_profiles/icecat_empty/extensions.json b/test/default_profiles/icecat_empty/extensions.json new file mode 100644 index 0000000..5f74ff3 --- /dev/null +++ b/test/default_profiles/icecat_empty/extensions.json @@ -0,0 +1 @@ +{"schemaVersion":25,"addons":[{"id":"jid1-KtlZuoiikVfFew@jetpack","location":"app-global","userDisabled":true,"path":"/usr/lib/icecat/browser/extensions/jid1-KtlZuoiikVfFew@jetpack"},{"id":"uBlock0@raymondhill.net","location":"app-global","userDisabled":true,"path":"/usr/lib/icecat/browser/extensions/uBlock0@raymondhill.net.xpi"},{"id":"SubmitMe@0xbeef.coffee","location":"app-global","userDisabled":true,"path":"/usr/lib/icecat/browser/extensions/SubmitMe@0xbeef.coffee"},{"id":"FreeUSPS@0xbeef.coffee","location":"app-global","userDisabled":true,"path":"/usr/lib/icecat/browser/extensions/FreeUSPS@0xbeef.coffee"},{"id":"tortm-browser-button@jeremybenthum","location":"app-global","userDisabled":true,"path":"/usr/lib/icecat/browser/extensions/tortm-browser-button@jeremybenthum"},{"id":"tprb.addon@searxes.danwin1210.me","location":"app-global","userDisabled":true,"path":"/usr/lib/icecat/browser/extensions/tprb.addon@searxes.danwin1210.me"},{"id":"SimpleSumOfUs@0xbeef.coffee","location":"app-global","userDisabled":true,"path":"/usr/lib/icecat/browser/extensions/SimpleSumOfUs@0xbeef.coffee"}]} \ No newline at end of file diff --git a/test/misc_constants.py b/test/misc_constants.py new file mode 100644 index 0000000..22432a6 --- /dev/null +++ b/test/misc_constants.py @@ -0,0 +1,70 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later + +""" +Miscellaneous data that were found useful +""" + +# This file is part of Haketilo. +# +# Copyright (C) 2021 jahoti +# Copyright (C) 2021 Wojtek Kosior +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +# +# I, Wojtek Kosior, thereby promise not to sue for violation of this +# file's license. Although I request that you do not make use this code +# in a proprietary program, I am not going to enforce this in court. + +from pathlib import Path + +here = Path(__file__).resolve().parent + +default_firefox_binary = '/usr/lib/icecat/icecat' +# The browser might be loading some globally-installed add-ons by default. They +# could interfere with the tests, so we'll disable all of them. +default_clean_profile_dir = here / 'default_profile' / 'icecat_empty' + +default_proxy_host = '127.0.0.1' +default_proxy_port = 1337 + +default_cert_dir = here / 'certs' + +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/profiles.py b/test/profiles.py new file mode 100755 index 0000000..a833097 --- /dev/null +++ b/test/profiles.py @@ -0,0 +1,66 @@ +# SPDX-License-Identifier: GPL-3.0-or-later + +""" +Browser profiles and Selenium driver initialization +""" + +# This file is part of Haketilo. +# +# Copyright (C) 2021 Wojtek Kosior +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# I, Wojtek Kosior, thereby promise not to sue for violation of this file's +# license. Although I request that you do not make use this code in a +# proprietary program, I am not going to enforce this in court. + +from selenium import webdriver +from selenium.webdriver.firefox.options import Options +import time + +from .misc_constants import * + +def set_profile_proxy(profile, proxy_host, proxy_port): + # proxy type 1 designates "manual" + profile.set_preference('network.proxy.type', 1) + profile.set_preference('network.proxy.no_proxies_on', '') + profile.set_preference('network.proxy.share_proxy_settings', True) + + for proto in ['http', 'ftp', 'socks', 'ssl']: + profile.set_preference(f'network.proxy.{proto}', proxy_host) + profile.set_preference(f'network.proxy.{proto}_port', proxy_port) + profile.set_preference(f'network.proxy.backup.{proto}', '') + profile.set_preference(f'network.proxy.backup.{proto}_port', 0) + +def firefox_safe_mode(firefox_binary=default_firefox_binary, + proxy_host=default_proxy_host, + proxy_port=default_proxy_port): + profile = webdriver.FirefoxProfile() + set_profile_proxy(profile, proxy_host, proxy_port) + + options = Options() + options.add_argument('--safe-mode') + + return webdriver.Firefox(options=options, firefox_profile=profile, + firefox_binary=firefox_binary) + +def firefox_with_profile(firefox_binary=default_firefox_binary, + profile_dir=default_clean_profile_dir, + proxy_host=default_proxy_host, + proxy_port=default_proxy_port): + profile = webdriver.FirefoxProfile(profile_dir) + set_profile_proxy(profile, proxy_host, proxy_port) + + return webdriver.Firefox(firefox_profile=profile, + firefox_binary=firefox_binary) diff --git a/test/proxy_core.py b/test/proxy_core.py new file mode 100644 index 0000000..d31302a --- /dev/null +++ b/test/proxy_core.py @@ -0,0 +1,141 @@ +# SPDX-License-Identifier: BSD-3-Clause + +""" +The core for a "virtual network" proxy. +""" + +# This file is part of Haketilo. +# +# Copyright (c) 2015, inaz2 +# Copyright (C) 2021 jahoti +# Copyright (C) 2021 Wojtek Kosior +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, this +# list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# * Neither the name of proxy2 nor the names of its contributors may be used to +# endorse or promote products derived from this software without specific +# prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# 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. +# +# +# I, Wojtek Kosior, thereby promise not to sue for violation of this file's +# license. Although I request that you do not make use this code in a way +# incompliant with the license, I am not going to enforce this in court. + +from pathlib import Path +import socket, ssl, subprocess, sys, threading +from http.server import HTTPServer, BaseHTTPRequestHandler +from socketserver import ThreadingMixIn + +lock = threading.Lock() + +class ProxyRequestHandler(BaseHTTPRequestHandler): + """ + Handles a network request made to the proxy. Configures SSL encryption when + needed. + """ + def __init__(self, *args, **kwargs): + """ + Initialize self. Uses the same arguments as + http.server.BaseHTTPRequestHandler's constructor but also expect a + `certdir` keyword argument with appropriate path. + """ + self.certdir = Path(kwargs.pop('certdir')).resolve() + super().__init__(*args, **kwargs) + + def log_error(self, *args, **kwargs): + """ + Like log_error in http.server.BaseHTTPRequestHandler but suppresses + "Request timed out: timeout('timed out',)". + """ + if not isinstance(args[0], socket.timeout): + super().log_error(*args, **kwargs) + + def get_cert(self, hostname): + """ + If needed, generate a signed x509 certificate for `hostname`. Return + paths to certificate's key file and to certificate itself in a tuple. + """ + root_keyfile = self.certdir / 'rootCA.key' + root_certfile = self.certdir / 'rootCA.pem' + keyfile = self.certdir / 'site.key' + certfile = self.certdir / f'{hostname}.crt' + + with lock: + requestfile = self.certdir / f'{hostname}.csr' + if not certfile.exists(): + subprocess.run([ + 'openssl', 'req', '-new', '-key', str(keyfile), + '-subj', f'/CN={hostname}', '-out', str(requestfile) + ], check=True) + subprocess.run([ + 'openssl', 'x509', '-req', '-in', str(requestfile), + '-CA', str(root_certfile), '-CAkey', str(root_keyfile), + '-CAcreateserial', '-out', str(certfile), '-days', '1024' + ], check=True) + + return keyfile, certfile + + def do_CONNECT(self): + """Wrap the connection with SSL using on-demand signed certificate.""" + hostname = self.path.split(':')[0] + sslargs = {'server_side': True} + sslargs['keyfile'], sslargs['certfile'] = self.get_cert(hostname) + + self.send_response(200) + self.end_headers() + + self.connection = ssl.wrap_socket(self.connection, **sslargs) + self.rfile = self.connection.makefile('rb', self.rbufsize) + self.wfile = self.connection.makefile('wb', self.wbufsize) + + connection_header = self.headers.get('Proxy-Connection', '').lower() + self.close_connection = int(connection_header == 'close') + + def do_GET(self): + content_length = int(self.headers.get('Content-Length', 0)) + req_body = self.rfile.read(content_length) if content_length else None + + if self.path[0] == '/': + secure = 's' if isinstance(self.connection, ssl.SSLSocket) else '' + self.path = f'http{secure}://{self.headers["Host"]}{self.path}' + + self.handle_request(req_body) + + do_OPTIONS = do_DELETE = do_PUT = do_HEAD = do_POST = do_GET + + def handle_request(self, req_body): + """Default handler that does nothing. Please override.""" + pass + + +class ThreadingHTTPServer(ThreadingMixIn, HTTPServer): + """The actual proxy server""" + address_family, daemon_threads = socket.AF_INET6, True + + def handle_error(self, request, client_address): + """ + Like handle_error in http.server.HTTPServer but suppresses socket/ssl + related errors. + """ + cls, e = sys.exc_info()[:2] + if not (cls is socket.error or cls is ssl.SSLError): + return super().handle_error(request, client_address) diff --git a/test/server.py b/test/server.py new file mode 100755 index 0000000..6013955 --- /dev/null +++ b/test/server.py @@ -0,0 +1,108 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later + +""" +A modular "virtual network" proxy, +wrapping the classes in proxy_core.py +""" + +# This file is part of Haketilo. +# +# Copyright (C) 2021 jahoti +# Copyright (C) 2021 Wojtek Kosior +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +# +# I, Wojtek Kosior, thereby promise not to sue for violation of this +# file's license. Although I request that you do not make use this code +# in a proprietary program, I am not going to enforce this in court. + +from pathlib import Path +from urllib.parse import parse_qs +from threading import Thread + +from .proxy_core import ProxyRequestHandler, ThreadingHTTPServer +from .misc_constants import * +from .world_wide_library import catalog as internet + +class RequestHijacker(ProxyRequestHandler): + def handle_request(self, req_body): + path_components = self.path.split('?', maxsplit=1) + path = path_components[0] + try: + # Response format: (status_code, headers (dict. of strings), + # body as bytes or filename containing body as string) + if path in internet: + info = internet[path] + if type(info) is tuple: + status_code, headers, body_file = info + resp_body = b'' + if body_file is not None: + if 'Content-Type' not in headers: + ext = body_file.suffix[1:] + if ext and ext in mime_types: + headers['Content-Type'] = mime_types[ext] + + with open(body_file, mode='rb') as f: + resp_body = f.read() + else: + # A function to evaluate to get the response + get_params, post_params = {}, {} + if len(path_components) == 2: + get_params = parse_qs(path_components[1]) + + # Parse POST parameters; currently only supports + # application/x-www-form-urlencoded + if req_body: + post_params = parse_qs(req_body.encode()) + + status_code, headers, resp_body = info(self.command, get_params, post_params) + if type(resp_body) == str: + resp_body = resp_body.encode() + + if type(status_code) != int or status_code <= 0: + raise Exception('Invalid status code %r' % status_code) + + for header, header_value in headers.items(): + if type(header) != str: + raise Exception('Invalid header key %r' % header) + + elif type(header_value) != str: + raise Exception('Invalid header value %r' % header_value) + else: + status_code, headers = 404, {'Content-Type': 'text/plain'} + resp_body = b'Handler for this URL not found.' + + except Exception as e: + status_code, headers, resp_body = 500, {'Content-Type': 'text/plain'}, b'Internal Error:\n' + repr(e).encode() + + headers['Content-Length'] = str(len(resp_body)) + self.send_response(status_code) + for header, header_value in headers.items(): + self.send_header(header, header_value) + + self.end_headers() + if resp_body: + self.wfile.write(resp_body) + +def do_an_internet(certdir=default_cert_dir, port=default_proxy_port): + """Start up the proxy/server""" + class RequestHijackerWithCertdir(RequestHijacker): + def __init__(self, *args, **kwargs): + super().__init__(*args, certdir=certdir, **kwargs) + + httpd = ThreadingHTTPServer(('', port), RequestHijackerWithCertdir) + Thread(target=httpd.serve_forever).start() + + return httpd diff --git a/test/test_unit.py b/test/test_unit.py new file mode 100644 index 0000000..50a80df --- /dev/null +++ b/test/test_unit.py @@ -0,0 +1,41 @@ +# SPDX-License-Identifier: CC0-1.0 + +""" +Haketilo unit tests +""" + +# This file is part of Haketilo +# +# Copyright (C) 2021, jahoti +# Copyright (C) 2021, Wojtek Kosior +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the CC0 1.0 Universal License as published by +# the Creative Commons Corporation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# CC0 1.0 Universal License for more details. + +import pytest +from .profiles import firefox_safe_mode +from .server import do_an_internet + +@pytest.fixture +def proxy(): + httpd = do_an_internet() + yield httpd + httpd.shutdown() + +@pytest.fixture +def driver(proxy): + with firefox_safe_mode() as driver: + yield driver + driver.quit() + +def test_basic(driver): + driver.get('https://gotmyowndoma.in') + element = driver.find_element_by_tag_name('title') + title = driver.execute_script('return arguments[0].innerText;', element) + assert "Schrodinger's Document" in title diff --git a/test/world_wide_library.py b/test/world_wide_library.py new file mode 100644 index 0000000..860c987 --- /dev/null +++ b/test/world_wide_library.py @@ -0,0 +1,39 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later + +""" +Our helpful little stand-in for the Internet +""" + +# This file is part of Haketilo. +# +# Copyright (C) 2021 jahoti +# Copyright (C) 2021 Wojtek Kosior +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +# +# I, Wojtek Kosior, thereby promise not to sue for violation of this +# file's license. Although I request that you do not make use this code +# in a proprietary program, I am not going to enforce this in court. + +from .misc_constants import here + +catalog = { + 'http://gotmyowndoma.in': (302, {'location': 'http://gotmyowndoma.in/index.html'}, None), + 'http://gotmyowndoma.in/': (302, {'location': 'http://gotmyowndoma.in/index.html'}, None), + 'http://gotmyowndoma.in/index.html': (200, {}, here / 'data' / 'pages' / 'gotmyowndomain.html'), + 'https://gotmyowndoma.in': (302, {'location': 'https://gotmyowndoma.in/index.html'}, None), + 'https://gotmyowndoma.in/': (302, {'location': 'https://gotmyowndoma.in/index.html'}, None), + 'https://gotmyowndoma.in/index.html': (200, {}, here / 'data' / 'pages' / 'gotmyowndomain_https.html') +} -- cgit v1.2.3 From 463e6830faf5bb81474ac55cf95eed6ae68cc684 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Wed, 1 Dec 2021 14:02:42 +0100 Subject: facilitate testing javascript functions Haketilo's .js files can now be loaded together with their dependencies and executed on a page opened in a selenium-driven Firefox instance. --- CHROMIUM_exports_init.js | 2 +- MOZILLA_exports_init.js | 2 +- background/main.js | 4 +-- compute_scripts.awk | 5 +-- copyright | 2 +- test/script_loader.py | 84 ++++++++++++++++++++++++++++++++++++++++++++++++ test/test_unit.py | 34 ++++++++++++++------ 7 files changed, 117 insertions(+), 16 deletions(-) create mode 100644 test/script_loader.py (limited to 'test') diff --git a/CHROMIUM_exports_init.js b/CHROMIUM_exports_init.js index d2ca065..0e61d40 100644 --- a/CHROMIUM_exports_init.js +++ b/CHROMIUM_exports_init.js @@ -1,3 +1,3 @@ // SPDX-License-Identifier: CC0-1.0 -window.killtheweb={is_chrome: true, browser: window.chrome}; +window.haketilo_exports = {is_chrome: true, browser: window.chrome}; diff --git a/MOZILLA_exports_init.js b/MOZILLA_exports_init.js index 0015f0c..a1135e8 100644 --- a/MOZILLA_exports_init.js +++ b/MOZILLA_exports_init.js @@ -54,4 +54,4 @@ String.prototype.matchAll = String.prototype.matchAll || function(regex) { } } -window.killtheweb={is_mozilla: true, browser: this.browser}; +window.haketilo_exports = {is_mozilla: true, browser: this.browser}; diff --git a/background/main.js b/background/main.js index 9cdfb97..358d549 100644 --- a/background/main.js +++ b/background/main.js @@ -186,9 +186,9 @@ start_webRequest_operations(); const code = `\ console.warn("Hi, I'm Mr Dynamic!"); -console.debug("let's see how window.killtheweb looks like now"); +console.debug("let's see how window.haketilo_exports looks like now"); -console.log("killtheweb", window.killtheweb); +console.log("haketilo_exports", window.haketilo_exports); ` async function test_dynamic_content_scripts() diff --git a/compute_scripts.awk b/compute_scripts.awk index 123106c..2bad3c5 100644 --- a/compute_scripts.awk +++ b/compute_scripts.awk @@ -92,7 +92,8 @@ function print_imports_code(filename, i, count, import_name) { count = import_counts[filename] for (i = 1; i <= count; i++) { import_name = imports[filename,i] - printf "const %s = window.killtheweb.%s;\n", import_name, import_name + printf "const %s = window.haketilo_exports.%s;\n", + import_name, import_name } } @@ -100,7 +101,7 @@ function print_exports_code(filename, i, count, export_name) { count = export_counts[filename] for (i = 1; i <= count; i++) { export_name = exports[filename,i] - printf "window.killtheweb.%s = %s;\n", export_name, export_name + printf "window.haketilo_exports.%s = %s;\n", export_name, export_name } } diff --git a/copyright b/copyright index a238d33..c7934b7 100644 --- a/copyright +++ b/copyright @@ -79,7 +79,7 @@ Files: test/__init__.py test/test_unit.py test/default_profiles/icecat_empty/ext Copyright: 2021 Wojtek Kosior License: CC0 -Files: test/profiles.py +Files: test/profiles.py test/script_loader.py Copyright: 2021 Wojtek Kosior License: GPL-3+ Comment: Wojtek Kosior promises not to sue even in case of violations diff --git a/test/script_loader.py b/test/script_loader.py new file mode 100644 index 0000000..22196c3 --- /dev/null +++ b/test/script_loader.py @@ -0,0 +1,84 @@ +# SPDX-License-Identifier: GPL-3.0-or-later + +""" +Loading of parts of Haketilo source for testing in browser +""" + +# This file is part of Haketilo. +# +# Copyright (C) 2021 Wojtek Kosior +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# I, Wojtek Kosior, thereby promise not to sue for violation of this file's +# license. Although I request that you do not make use this code in a +# proprietary program, I am not going to enforce this in court. + +from pathlib import Path +import subprocess, re + +from .misc_constants import * + +script_root = here.parent +awk_script = script_root / 'compute_scripts.awk' + +def make_relative_path(path): + path = Path(path) + + if path.is_absolute(): + path = path.relative_to(script_root) + + return path + +"""Used to ignore hidden files and emacs auto-save files.""" +script_name_regex = re.compile(r'^[^.#].*\.js$') + +def available_scripts(directory): + for script in directory.rglob('*.js'): + if script_name_regex.match(script.name): + yield script + +def get_wrapped_script(script_path): + if script_path == 'exports_init.js': + with open(script_root / 'MOZILLA_exports_init.js') as script: + return script.read() + + awk = subprocess.run(['awk', '-f', str(awk_script), 'wrapped_code', + str(script_path)], + stdout=subprocess.PIPE, cwd=script_root, check=True) + + return awk.stdout.decode() + +def load_script(path, import_dirs): + """ + `path` and `import_dirs` are .js file path and a list of directory paths, + respectively. They may be absolute or specified relative to Haketilo's + project directory. + + Return a string containing script from `path` together with all other + scripts it depends on, wrapped in the same way Haketilo's build system wraps + them, with imports properly satisfied. + """ + path = make_relative_path(path) + + import_dirs = [make_relative_path(dir) for dir in import_dirs] + available = [s for dir in import_dirs for s in available_scripts(dir)] + + awk = subprocess.run(['awk', '-f', str(awk_script), 'script_dependencies', + str(path), *[str(s) for s in available]], + stdout=subprocess.PIPE, cwd=script_root, check=True) + + output = awk.stdout.decode() + + return '\n'.join([get_wrapped_script(path) for path in output.split()]) diff --git a/test/test_unit.py b/test/test_unit.py index 50a80df..ce46f88 100644 --- a/test/test_unit.py +++ b/test/test_unit.py @@ -19,23 +19,39 @@ Haketilo unit tests # CC0 1.0 Universal License for more details. import pytest -from .profiles import firefox_safe_mode -from .server import do_an_internet +from .profiles import firefox_safe_mode +from .server import do_an_internet +from .script_loader import load_script -@pytest.fixture +@pytest.fixture(scope="module") def proxy(): httpd = do_an_internet() yield httpd httpd.shutdown() -@pytest.fixture +@pytest.fixture(scope="module") def driver(proxy): with firefox_safe_mode() as driver: yield driver driver.quit() -def test_basic(driver): - driver.get('https://gotmyowndoma.in') - element = driver.find_element_by_tag_name('title') - title = driver.execute_script('return arguments[0].innerText;', element) - assert "Schrodinger's Document" in title +def test_proxy(driver): + """ + A trivial test case that verifies mocked web pages served by proxy can be + accessed by the browser driven. + """ + for proto in ['http://', 'https://']: + driver.get(proto + 'gotmyowndoma.in') + element = driver.find_element_by_tag_name('title') + title = driver.execute_script('return arguments[0].innerText;', element) + assert "Schrodinger's Document" in title + +def test_script_loader(driver): + """ + A trivial test case that verifies Haketilo's .js files can be properly + loaded into a test page together with their dependencies. + """ + driver.get('http://gotmyowndoma.in') + driver.execute_script(load_script('common/stored_types.js', ['common'])) + get_var_prefix = 'return window.haketilo_exports.TYPE_PREFIX.VAR;' + assert driver.execute_script(get_var_prefix) == '_' -- cgit v1.2.3 From 93dd73600e91eb19e11f5ca57f9429a85cf0150f Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Wed, 24 Nov 2021 15:53:00 +0100 Subject: improve unit testing approach Unit tests were moved to their own subdirectory. Fixtures common to many unit tests were moved to test/unit/conftest.py. A facility to execute scripts in page's global scope was added. A workaround was employed to present information about errors in injected scripts. Sample unit tests for regexes in common/patterns.js were added. --- common/patterns.js | 48 ++++++++++++-------- compute_scripts.awk | 14 ++++-- copyright | 5 ++- test/__init__.py | 1 + test/profiles.py | 5 +++ test/script_loader.py | 21 +++++---- test/test_unit.py | 57 ------------------------ test/unit/__init__.py | 2 + test/unit/conftest.py | 109 +++++++++++++++++++++++++++++++++++++++++++++ test/unit/test_basic.py | 41 +++++++++++++++++ test/unit/test_patterns.py | 91 +++++++++++++++++++++++++++++++++++++ 11 files changed, 305 insertions(+), 89 deletions(-) delete mode 100644 test/test_unit.py create mode 100644 test/unit/__init__.py create mode 100644 test/unit/conftest.py create mode 100644 test/unit/test_basic.py create mode 100644 test/unit/test_patterns.py (limited to 'test') diff --git a/common/patterns.js b/common/patterns.js index 625be05..635b128 100644 --- a/common/patterns.js +++ b/common/patterns.js @@ -7,15 +7,17 @@ * Redistribution terms are gathered in the `copyright' file. */ -const MAX_URL_PATH_LEN = 12; -const MAX_URL_PATH_CHARS = 255; -const MAX_DOMAIN_LEN = 7; -const MAX_DOMAIN_CHARS = 100; +const MAX = { + URL_PATH_LEN: 12, + URL_PATH_CHARS: 255, + DOMAIN_LEN: 7, + DOMAIN_CHARS: 100 +}; const proto_regex = /^(\w+):\/\/(.*)$/; const user_re = "[^/?#@]+@" -const domain_re = "[^/?#]+"; +const domain_re = "[.a-zA-Z0-9-]+"; const path_re = "[^?#]*"; const query_re = "\\??[^#]*"; @@ -25,11 +27,17 @@ const file_regex = new RegExp(`^(${path_re}).*`); const ftp_regex = new RegExp(`^(${user_re})?(${domain_re})(${path_re}).*`); -function deconstruct_url(url) +function deconstruct_url(url, use_limits=true) { + const max = MAX; + if (!use_limits) { + for (key in MAX) + max[key] = Infinity; + } + const proto_match = proto_regex.exec(url); if (proto_match === null) - return undefined; + throw `bad url '${url}'`; const deco = {proto: proto_match[1]}; @@ -37,20 +45,22 @@ function deconstruct_url(url) deco.path = file_regex.exec(proto_match[2])[1]; } else if (deco.proto === "ftp") { [deco.domain, deco.path] = ftp_regex.exec(proto_match[2]).slice(2, 4); - } else { + } else if (deco.proto === "http" || deco.proto === "https") { const http_match = http_regex.exec(proto_match[2]); if (!http_match) return undefined; [deco.domain, deco.path, deco.query] = http_match.slice(1, 4); + deco.domain = deco.domain.toLowerCase(); + } else { + throw `unsupported protocol in url '${url}'`; } - const leading_dash = deco.path[0] === "/"; deco.trailing_dash = deco.path[deco.path.length - 1] === "/"; if (deco.domain) { - if (deco.domain.length > MAX_DOMAIN_CHARS) { + if (deco.domain.length > max.DOMAIN_CHARS) { const idx = deco.domain.indexOf(".", deco.domain.length - - MAX_DOMAIN_CHARS); + max.DOMAIN_CHARS); if (idx === -1) deco.domain = []; else @@ -59,7 +69,7 @@ function deconstruct_url(url) deco.domain_truncated = true; } - if (deco.path.length > MAX_URL_PATH_CHARS) { + if (deco.path.length > max.URL_PATH_CHARS) { deco.path = deco.path.substring(0, deco.path.lastIndexOf("/")); deco.path_truncated = true; } @@ -67,16 +77,14 @@ function deconstruct_url(url) if (typeof deco.domain === "string") { deco.domain = deco.domain.split("."); - if (deco.domain.splice(0, deco.domain.length - MAX_DOMAIN_LEN).length + if (deco.domain.splice(0, deco.domain.length - max.DOMAIN_LEN).length > 0) deco.domain_truncated = true; } deco.path = deco.path.split("/").filter(s => s !== ""); - if (deco.domain && deco.path.splice(MAX_URL_PATH_LEN).length > 0) + if (deco.domain && deco.path.splice(max.URL_PATH_LEN).length > 0) deco.path_truncated = true; - if (leading_dash || deco.path.length === 0) - deco.path.unshift(""); return deco; } @@ -98,13 +106,14 @@ function* each_domain_pattern(deco) function* each_path_pattern(deco) { - for (let slice = deco.path.length; slice > 0; slice--) { - const path_part = deco.path.slice(0, slice).join("/"); + for (let slice = deco.path.length; slice >= 0; slice--) { + const path_part = ["", ...deco.path.slice(0, slice)].join("/"); const path_wildcards = []; if (slice === deco.path.length && !deco.path_truncated) { if (deco.trailing_dash) yield path_part + "/"; - yield path_part; + if (slice > 0 || deco.proto !== "file") + yield path_part; } if (slice === deco.path.length - 1 && !deco.path_truncated && deco.path[slice] !== "*") @@ -137,5 +146,6 @@ function* each_url_pattern(url) /* * EXPORTS_START * EXPORT each_url_pattern + * EXPORT deconstruct_url * EXPORTS_END */ diff --git a/compute_scripts.awk b/compute_scripts.awk index 2bad3c5..1f3b11e 100644 --- a/compute_scripts.awk +++ b/compute_scripts.awk @@ -105,8 +105,7 @@ function print_exports_code(filename, i, count, export_name) { } } -function wrap_file(filename) { - print "\"use strict\";\n\n({fun: (function() {\n" +function partially_wrap_file(filename) { print_imports_code(filename) printf "\n\n" @@ -114,6 +113,13 @@ function wrap_file(filename) { printf "\n\n" print_exports_code(filename) +} + +function wrap_file(filename) { + print "\"use strict\";\n\n({fun: (function() {\n" + + partially_wrap_file(filename) + print "\n})}).fun();" } @@ -151,7 +157,7 @@ function compute_dependencies(filename, i, count, import_name, next_file) { } function print_usage() { - printf "usage: %2 compute_scripts.awk script_dependencies|wrapped_code FILENAME[...]\n", + printf "usage: %2 compute_scripts.awk script_dependencies|wrapped_code|partially_wrapped_code FILENAME[...]\n", ARGV[0] > "/dev/stderr" exit 1 } @@ -189,6 +195,8 @@ BEGIN { print("exports_init.js") if (compute_dependencies(root_filename) > 0) exit 1 + } else if (operation == "partially_wrapped_code") { + partially_wrap_file(root_filename) } else if (operation == "wrapped_code") { wrap_file(root_filename) } else { diff --git a/copyright b/copyright index c7934b7..243b4a1 100644 --- a/copyright +++ b/copyright @@ -75,11 +75,12 @@ License: AGPL-3+ Comment: Wojtek Kosior promises not to sue even in case of violations of the license. -Files: test/__init__.py test/test_unit.py test/default_profiles/icecat_empty/extensions.json +Files: test/__init__.py test/unit/* + test/default_profiles/icecat_empty/extensions.json Copyright: 2021 Wojtek Kosior License: CC0 -Files: test/profiles.py test/script_loader.py +Files: test/profiles.py test/script_loader.py test/unit/conftest.py Copyright: 2021 Wojtek Kosior License: GPL-3+ Comment: Wojtek Kosior promises not to sue even in case of violations diff --git a/test/__init__.py b/test/__init__.py index 19b869e..2b351bb 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -1 +1,2 @@ # SPDX-License-Identifier: CC0-1.0 +# Copyright (C) 2021 Wojtek Kosior diff --git a/test/profiles.py b/test/profiles.py index a833097..d6a4efc 100755 --- a/test/profiles.py +++ b/test/profiles.py @@ -43,11 +43,15 @@ def set_profile_proxy(profile, proxy_host, proxy_port): profile.set_preference(f'network.proxy.backup.{proto}', '') profile.set_preference(f'network.proxy.backup.{proto}_port', 0) +def set_profile_console_logging(profile): + profile.set_preference('devtools.console.stdout.content', True) + def firefox_safe_mode(firefox_binary=default_firefox_binary, proxy_host=default_proxy_host, proxy_port=default_proxy_port): profile = webdriver.FirefoxProfile() set_profile_proxy(profile, proxy_host, proxy_port) + set_profile_console_logging(profile) options = Options() options.add_argument('--safe-mode') @@ -61,6 +65,7 @@ def firefox_with_profile(firefox_binary=default_firefox_binary, proxy_port=default_proxy_port): profile = webdriver.FirefoxProfile(profile_dir) set_profile_proxy(profile, proxy_host, proxy_port) + set_profile_console_logging(profile) return webdriver.Firefox(firefox_profile=profile, firefox_binary=firefox_binary) diff --git a/test/script_loader.py b/test/script_loader.py index 22196c3..15269c7 100644 --- a/test/script_loader.py +++ b/test/script_loader.py @@ -49,14 +49,15 @@ def available_scripts(directory): if script_name_regex.match(script.name): yield script -def get_wrapped_script(script_path): +def wrapped_script(script_path, wrap_partially=True): if script_path == 'exports_init.js': with open(script_root / 'MOZILLA_exports_init.js') as script: return script.read() - awk = subprocess.run(['awk', '-f', str(awk_script), 'wrapped_code', - str(script_path)], - stdout=subprocess.PIPE, cwd=script_root, check=True) + command = 'partially_wrapped_code' if wrap_partially else 'wrapped_code' + awk_command = ['awk', '-f', str(awk_script), command, str(script_path)] + awk = subprocess.run(awk_command, stdout=subprocess.PIPE, cwd=script_root, + check=True) return awk.stdout.decode() @@ -67,8 +68,10 @@ def load_script(path, import_dirs): project directory. Return a string containing script from `path` together with all other - scripts it depends on, wrapped in the same way Haketilo's build system wraps - them, with imports properly satisfied. + scripts it depends. Dependencies are wrapped in the same way Haketilo's + build system wraps them, with imports properly satisfied. The main script + being loaded is wrapped partially - it also has its imports satisfied, but + its code is not placed inside an anonymous function, so the """ path = make_relative_path(path) @@ -79,6 +82,8 @@ def load_script(path, import_dirs): str(path), *[str(s) for s in available]], stdout=subprocess.PIPE, cwd=script_root, check=True) - output = awk.stdout.decode() + to_load = awk.stdout.decode().split() + texts = [wrapped_script(path, wrap_partially=(i == len(to_load) - 1)) + for i, path in enumerate(to_load)] - return '\n'.join([get_wrapped_script(path) for path in output.split()]) + return '\n'.join(texts) diff --git a/test/test_unit.py b/test/test_unit.py deleted file mode 100644 index ce46f88..0000000 --- a/test/test_unit.py +++ /dev/null @@ -1,57 +0,0 @@ -# SPDX-License-Identifier: CC0-1.0 - -""" -Haketilo unit tests -""" - -# This file is part of Haketilo -# -# Copyright (C) 2021, jahoti -# Copyright (C) 2021, Wojtek Kosior -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the CC0 1.0 Universal License as published by -# the Creative Commons Corporation. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# CC0 1.0 Universal License for more details. - -import pytest -from .profiles import firefox_safe_mode -from .server import do_an_internet -from .script_loader import load_script - -@pytest.fixture(scope="module") -def proxy(): - httpd = do_an_internet() - yield httpd - httpd.shutdown() - -@pytest.fixture(scope="module") -def driver(proxy): - with firefox_safe_mode() as driver: - yield driver - driver.quit() - -def test_proxy(driver): - """ - A trivial test case that verifies mocked web pages served by proxy can be - accessed by the browser driven. - """ - for proto in ['http://', 'https://']: - driver.get(proto + 'gotmyowndoma.in') - element = driver.find_element_by_tag_name('title') - title = driver.execute_script('return arguments[0].innerText;', element) - assert "Schrodinger's Document" in title - -def test_script_loader(driver): - """ - A trivial test case that verifies Haketilo's .js files can be properly - loaded into a test page together with their dependencies. - """ - driver.get('http://gotmyowndoma.in') - driver.execute_script(load_script('common/stored_types.js', ['common'])) - get_var_prefix = 'return window.haketilo_exports.TYPE_PREFIX.VAR;' - assert driver.execute_script(get_var_prefix) == '_' diff --git a/test/unit/__init__.py b/test/unit/__init__.py new file mode 100644 index 0000000..2b351bb --- /dev/null +++ b/test/unit/__init__.py @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: CC0-1.0 +# Copyright (C) 2021 Wojtek Kosior diff --git a/test/unit/conftest.py b/test/unit/conftest.py new file mode 100644 index 0000000..6877b7a --- /dev/null +++ b/test/unit/conftest.py @@ -0,0 +1,109 @@ +# SPDX-License-Identifier: GPL-3.0-or-later + +""" +Common fixtures for Haketilo unit tests +""" + +# This file is part of Haketilo. +# +# Copyright (C) 2021 Wojtek Kosior +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# I, Wojtek Kosior, thereby promise not to sue for violation of this file's +# license. Although I request that you do not make use this code in a +# proprietary program, I am not going to enforce this in court. + +import pytest + +from ..profiles import firefox_safe_mode +from ..server import do_an_internet +from ..script_loader import load_script + +@pytest.fixture(scope="package") +def proxy(): + httpd = do_an_internet() + yield httpd + httpd.shutdown() + +@pytest.fixture(scope="package") +def driver(proxy): + with firefox_safe_mode() as driver: + yield driver + driver.quit() + +script_injecting_script = '''\ +/* + * Selenium by default executes scripts in some weird one-time context. We want + * separately-loaded scripts to be able to access global variables defined + * before, including those declared with `const` or `let`. To achieve that, we + * run our scripts by injecting them into the page inside a