summaryrefslogtreecommitdiffstats
path: root/testenv/Test-metalink-http-baddigest.py
blob: 2496da76c6b4f172f849efde26ca3cd387aafb4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
from sys import exit
from test.http_test import HTTPTest
from misc.wget_file import WgetFile
import hashlib
from base64 import b64encode

"""
    This is to test Metalink/HTTP with a malformed base64 Digest header.

    With --trust-server-names, trust the metalink:file names.

    Without --trust-server-names, don't trust the metalink:file names:
    use the basename of --input-metalink, and add a sequential number
    (e.g. .#1, .#2, etc.).

    Strip the directory from unsafe paths.
"""

############# File Definitions ###############################################
bad = "Ouch!"
bad_sha256 = b64encode (hashlib.sha256 (bad.encode ('UTF-8')).digest ()).decode ('ascii')

LinkHeaders = ["<http://{{SRV_HOST}}:{{SRV_PORT}}/wrong_file>; rel=duplicate; pri=1"]
DigestHeader = "SHA-256=bad_base64,SHA-256={{BAD_HASH}}"

# This will be filled as soon as we know server hostname and port
MetaHTTPRules = {'SendHeader' : {}}

MetaHTTP = WgetFile ("main.metalink", rules=MetaHTTPRules)

wrong_file = WgetFile ("wrong_file", bad)
wrong_file_down = WgetFile ("main.metalink", bad)

WGET_OPTIONS = "--metalink-over-http"
WGET_URLS = [["main.metalink"]]

RequestList = [[
    "HEAD /main.metalink",
    "GET /wrong_file"
]]

Files = [[
    MetaHTTP,
    wrong_file
]]
Existing_Files = []

ExpectedReturnCode = 0
ExpectedDownloadedFiles = [wrong_file_down]

################ Pre and Post Test Hooks #####################################
pre_test = {
    "ServerFiles"       : Files,
    "LocalFiles"        : Existing_Files
}
test_options = {
    "WgetCommands"      : WGET_OPTIONS,
    "Urls"              : WGET_URLS
}
post_test = {
    "ExpectedFiles"     : ExpectedDownloadedFiles,
    "ExpectedRetcode"   : ExpectedReturnCode,
    "FilesCrawled"      : RequestList
}

http_test = HTTPTest (
                pre_hook=pre_test,
                test_params=test_options,
                post_hook=post_test
)

http_test.server_setup()
### Get and use dynamic server sockname
srv_host, srv_port = http_test.servers[0].server_inst.socket.getsockname ()

DigestHeader = DigestHeader.replace('{{BAD_HASH}}', bad_sha256)

# Helper function for hostname, port and digest substitution
def SubstituteServerInfo (text, host, port):
    text = text.replace('{{SRV_HOST}}', host)
    text = text.replace('{{SRV_PORT}}', str (port))
    return text

MetaHTTPRules["SendHeader"] = {
        'Link': [ SubstituteServerInfo (LinkHeader, srv_host, srv_port)
                    for LinkHeader in LinkHeaders ],
        'Digest': DigestHeader
}

err = http_test.begin ()

exit (err)