| 1 |
#!/usr/bin/env python3 |
| 2 |
from sys import exit |
| 3 |
from test.http_test import HTTPTest |
| 4 |
from test.base_test import HTTPS, SKIP_TEST |
| 5 |
from misc.wget_file import WgetFile |
| 6 |
import time |
| 7 |
import os |
| 8 |
|
| 9 |
""" |
| 10 |
This test makes sure Wget can parse a given HSTS database and apply the indicated HSTS policy. |
| 11 |
""" |
| 12 |
|
| 13 |
print (os.getenv('SSL_TESTS')) |
| 14 |
if os.getenv('SSL_TESTS') is None: |
| 15 |
exit (SKIP_TEST) |
| 16 |
|
| 17 |
def hsts_database_path(): |
| 18 |
hsts_file = ".wget-hsts-testenv" |
| 19 |
return os.path.abspath(hsts_file) |
| 20 |
|
| 21 |
def create_hsts_database(path, host, port): |
| 22 |
# we want the current time as an integer, |
| 23 |
# not as a floating point |
| 24 |
curtime = int(time.time()) |
| 25 |
max_age = "123456" |
| 26 |
|
| 27 |
f = open(path, "w") |
| 28 |
|
| 29 |
f.write("# dummy comment\n") |
| 30 |
f.write(host + "\t" + str(port) + "\t0\t" + str(curtime) + "\t" + max_age + "\n") |
| 31 |
f.close() |
| 32 |
|
| 33 |
File_Name = "hw" |
| 34 |
File_Content = "Hello, world!" |
| 35 |
File = WgetFile(File_Name, File_Content) |
| 36 |
|
| 37 |
Hsts_File_Path = hsts_database_path() |
| 38 |
|
| 39 |
CAFILE = os.path.abspath(os.path.join(os.getenv('srcdir', '.'), 'certs', 'ca-cert.pem')) |
| 40 |
|
| 41 |
WGET_OPTIONS = "--hsts-file=" + Hsts_File_Path + " --ca-certificate=" + CAFILE |
| 42 |
WGET_URLS = [[File_Name]] |
| 43 |
|
| 44 |
Files = [[File]] |
| 45 |
Servers = [HTTPS] |
| 46 |
Requests = ["http"] |
| 47 |
|
| 48 |
ExpectedReturnCode = 0 |
| 49 |
ExpectedDownloadedFiles = [File] |
| 50 |
|
| 51 |
pre_test = { |
| 52 |
"ServerFiles" : Files, |
| 53 |
"Domains" : ["localhost"] |
| 54 |
} |
| 55 |
post_test = { |
| 56 |
"ExpectedFiles" : ExpectedDownloadedFiles, |
| 57 |
"ExpectedRetCode" : ExpectedReturnCode, |
| 58 |
} |
| 59 |
test_options = { |
| 60 |
"WgetCommands" : WGET_OPTIONS, |
| 61 |
"Urls" : WGET_URLS |
| 62 |
} |
| 63 |
|
| 64 |
test = HTTPTest( |
| 65 |
pre_hook = pre_test, |
| 66 |
post_hook = post_test, |
| 67 |
test_params = test_options, |
| 68 |
protocols = Servers, |
| 69 |
req_protocols = Requests |
| 70 |
) |
| 71 |
|
| 72 |
# start the web server and create the temporary HSTS database |
| 73 |
test.setup() |
| 74 |
create_hsts_database(Hsts_File_Path, 'localhost', test.port) |
| 75 |
|
| 76 |
err = test.begin() |
| 77 |
|
| 78 |
# remove the temporary HSTS database |
| 79 |
os.unlink(hsts_database_path()) |
| 80 |
exit(err) |