1
0
mirror of https://github.com/moparisthebest/wget synced 2024-07-03 16:38:41 -04:00
wget/testenv/Test-hsts.py
Ander Juaristi 54058d2b18 Enhancements in testsuite engine + new HSTS test.
* testenv/Makefile.am: added new test 'Test-hsts.py'.
 * testenv/Test-hsts.py: new test for HSTS.
 * testenv/conf/domains.py: new hook to override domain list.
 * testenv/test/base_test.py: (__init__): new optional parameter
   for tests 'req_protocols'.
   (get_domain_addr): set the instance variables 'addr' and 'port'.
   Return address as an array (domain, port) instead of string.
   (gen_cmd_line): take into account domain and port.
 * testenv/test/http_test.py (__init__): new optional parameter
   'req_protocols'.
   (setup): new function. Call to server_setup() decoupled from
   begin() and moved here.
   (begin): call to superclass to maintain backward compatibility.
   Removed call to server_setup().

This patch adds a new parameter to the test suite called 'req_protocols',
and a new function called 'setup'. The ability for tests to be able to set some
extra parameters such as the actual requested protocols (with 'req_protocols')
became obvious when support for HSTS was added to Wget, where the requested URI
and the actual executed URI do not have to be the same. This new parameter is optional
and if not specified, the test suite behaves as before. Also, the new function 'setup'
is provided as a means to start the test HTTP server, but not launch the test yet
(this is done when calling 'begin', as usual), in case we want to query the address
and port in which the test server listens. If 'setup' is not called, it is automatically
invoked when calling 'begin'. With these measures, we preserve backward-compatibility with
existing tests.
2015-07-20 16:06:40 +02:00

79 lines
1.9 KiB
Python
Executable File

#!/usr/bin/env python3
from sys import exit
from test.http_test import HTTPTest
from test.base_test import HTTP, HTTPS
from misc.wget_file import WgetFile
import time
import os
"""
This test makes sure Wget can parse a given HSTS database and apply the indicated HSTS policy.
"""
def hsts_database_path():
hsts_file = ".wget-hsts-testenv"
return os.path.abspath(hsts_file)
def create_hsts_database(path, host, port):
# we want the current time as an integer,
# not as a floating point
curtime = int(time.time())
max_age = "123456"
f = open(path, "w")
f.write("# dummy comment\n")
f.write(host + "\t" + str(port) + "\t0\t" + str(curtime) + "\t" + max_age + "\n")
f.close()
TEST_NAME = "HSTS basic test"
File_Name = "hw"
File_Content = "Hello, world!"
File = WgetFile(File_Name, File_Content)
Hsts_File_Path = hsts_database_path()
CAFILE = os.path.abspath(os.path.join(os.getenv('srcdir', '.'), 'certs', 'ca-cert.pem'))
WGET_OPTIONS = "--hsts-file=" + Hsts_File_Path + " --ca-certificate=" + CAFILE
WGET_URLS = [[File_Name]]
Files = [[File]]
Servers = [HTTPS]
Requests = ["http"]
ExpectedReturnCode = 0
ExpectedDownloadedFiles = [File]
pre_test = {
"ServerFiles" : Files,
"Domains" : ["localhost"]
}
post_test = {
"ExpectedFiles" : ExpectedDownloadedFiles,
"ExpectedRetCode" : ExpectedReturnCode,
}
test_options = {
"WgetCommands" : WGET_OPTIONS,
"Urls" : WGET_URLS
}
test = HTTPTest(
name = TEST_NAME,
pre_hook = pre_test,
post_hook = post_test,
test_params = test_options,
protocols = Servers,
req_protocols = Requests
)
# start the web server and create the temporary HSTS database
test.setup()
create_hsts_database(Hsts_File_Path, 'localhost', test.port)
err = test.begin()
# remove the temporary HSTS database
os.unlink(hsts_database_path())
exit(err)