summaryrefslogtreecommitdiffstats
path: root/testenv/Test-metalink-http-quoted.py
blob: af2c445b8ca850dbedd96ead5619abfa8fc59098 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/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 quoted values support in Wget.
"""

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

############# File Definitions ###############################################
File1 = "Would you like some Tea?"
File1_corrupted = "Would you like some Coffee?"
File1_lowPref = "Do not take this"
File1_sha256 = b64encode (hashlib.sha256 (File1.encode ('UTF-8')).digest ()).decode ('ascii')
Signature = '''-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)

This is no valid signature. But it should be downloaded.
The attempt to verify should fail but should not prevent
a successful metalink resource retrieval (the sig failure
should not be fatal).
-----END PGP SIGNATURE-----
'''
File2 = "No meta data for this file."

LinkHeaders = [
    # This file has low priority and should not be picked.
    "<http://{{SRV_HOST}}:{{SRV_PORT}}/File1_lowPref>; rel=\"duplicate\"; pri=\"9\"; geo=\"pl\"",
    # This file should be picked second, after hash failure.
    "<http://0.0.0.0/File1_try2_badconnection>; rel =\"duplicate\";pref; pri=\"7\"",
    # This signature download will fail.
    "<http://{{SRV_HOST}}:{{SRV_PORT}}/Sig2.asc>; rel=\"describedby\"; type=\"application/pgp-signature\"",
    # Two good signatures
    "<http://{{SRV_HOST}}:{{SRV_PORT}}/Sig.asc>; rel=\"describedby\"; type=\"application/pgp-signature\"",
    "<http://{{SRV_HOST}}:{{SRV_PORT}}/Sig.asc>; rel=\"describedby\"; type=\"application/pgp-signature\"",
    # Bad URL scheme
    "<invalid_url>; rel=\"duplicate\"; pri=\"4\"",
    # rel missing
    "<http://{{SRV_HOST}}:{{SRV_PORT}}/File1>; pri=\"1\"; pref",
    # invalid rel
    "<http://{{SRV_HOST}}:{{SRV_PORT}}/File1>; rel=\"strange\"; pri=\"4\"",
    # This file should be picked first, because it has the lowest pri among preferred.
    "<http://{{SRV_HOST}}:{{SRV_PORT}}/File1_try1_corrupted>; rel=\"duplicate\"; geo=\"su\"; pri=\"4\"; pref",
    # This file should NOT be picked third due to preferred location set to 'uk'
    "<http://{{SRV_HOST}}:{{SRV_PORT}}/File1_badgeo>; rel =\"duplicate\";pri=\"5\"",
    # This file should be picked as third try, and it should succeed
    "<http://{{SRV_HOST}}:{{SRV_PORT}}/File1_try3_ok>; rel=\'duplicate\'; pri=\"5\";geo=\"uk\""
    ]
DigestHeader = "SHA-256=\'{{FILE1_HASH}}\'"

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

FileOkServer = WgetFile ("File1_try3_ok", File1)
FileBadPref = WgetFile ("File1_lowPref", File1_lowPref)
FileBadHash = WgetFile ("File1_try1_corrupted", File1_corrupted)
MetaFile = WgetFile ("test.meta", rules=MetaFileRules)
# In case of Metalink over HTTP, the local file name is
# derived from the URL suffix.
FileOkLocal = WgetFile ("test.meta", File1)
SigFile = WgetFile ("Sig.asc", Signature)
FileNoMeta = WgetFile ("File2", File2)

WGET_OPTIONS = "--metalink-over-http --preferred-location=uk"
WGET_URLS = [["test.meta", "File2"]]

Files = [[FileOkServer, FileBadPref, FileBadHash, MetaFile, SigFile, FileNoMeta]]
Existing_Files = []

ExpectedReturnCode = 0
ExpectedDownloadedFiles = [FileNoMeta, FileOkLocal]

RequestList = [
    [
        "HEAD /test.meta",
        "GET /Sig2.asc",
        "GET /Sig.asc",
        "GET /File1_try1_corrupted",
        "GET /File1_try3_ok",
        "HEAD /File2",
        "GET /File2",
    ]
]

################ 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()
srv_host, srv_port = http_test.servers[0].server_inst.socket.getsockname ()

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

err = http_test.begin ()

exit (err)