| 1 |
#!/usr/bin/env python3 |
| 2 |
from sys import exit |
| 3 |
from test.http_test import HTTPTest |
| 4 |
from misc.wget_file import WgetFile |
| 5 |
import hashlib |
| 6 |
from base64 import b64encode |
| 7 |
|
| 8 |
""" |
| 9 |
This is to test Metalink/HTTP quoted values support in Wget. |
| 10 |
""" |
| 11 |
|
| 12 |
# Helper function for hostname, port and digest substitution |
| 13 |
def SubstituteServerInfo (text, host, port, digest): |
| 14 |
text = text.replace('{{FILE1_HASH}}', digest) |
| 15 |
text = text.replace('{{SRV_HOST}}', host) |
| 16 |
text = text.replace('{{SRV_PORT}}', str (port)) |
| 17 |
return text |
| 18 |
|
| 19 |
############# File Definitions ############################################### |
| 20 |
File1 = "Would you like some Tea?" |
| 21 |
File1_corrupted = "Would you like some Coffee?" |
| 22 |
File1_lowPref = "Do not take this" |
| 23 |
File1_sha256 = b64encode (hashlib.sha256 (File1.encode ('UTF-8')).digest ()).decode ('ascii') |
| 24 |
Signature = '''-----BEGIN PGP SIGNATURE----- |
| 25 |
Version: GnuPG v1.0.7 (GNU/Linux) |
| 26 |
|
| 27 |
This is no valid signature. But it should be downloaded. |
| 28 |
The attempt to verify should fail but should not prevent |
| 29 |
a successful metalink resource retrieval (the sig failure |
| 30 |
should not be fatal). |
| 31 |
-----END PGP SIGNATURE----- |
| 32 |
''' |
| 33 |
File2 = "No meta data for this file." |
| 34 |
|
| 35 |
LinkHeaders = [ |
| 36 |
# This file has low priority and should not be picked. |
| 37 |
"<http://{{SRV_HOST}}:{{SRV_PORT}}/File1_lowPref>; rel=\"duplicate\"; pri=\"9\"; geo=\"pl\"", |
| 38 |
# This file should be picked second, after hash failure. |
| 39 |
"<http://0.0.0.0/File1_try2_badconnection>; rel =\"duplicate\";pref; pri=\"7\"", |
| 40 |
# This signature download will fail. |
| 41 |
"<http://{{SRV_HOST}}:{{SRV_PORT}}/Sig2.asc>; rel=\"describedby\"; type=\"application/pgp-signature\"", |
| 42 |
# Two good signatures |
| 43 |
"<http://{{SRV_HOST}}:{{SRV_PORT}}/Sig.asc>; rel=\"describedby\"; type=\"application/pgp-signature\"", |
| 44 |
"<http://{{SRV_HOST}}:{{SRV_PORT}}/Sig.asc>; rel=\"describedby\"; type=\"application/pgp-signature\"", |
| 45 |
# Bad URL scheme |
| 46 |
"<invalid_url>; rel=\"duplicate\"; pri=\"4\"", |
| 47 |
# rel missing |
| 48 |
"<http://{{SRV_HOST}}:{{SRV_PORT}}/File1>; pri=\"1\"; pref", |
| 49 |
# invalid rel |
| 50 |
"<http://{{SRV_HOST}}:{{SRV_PORT}}/File1>; rel=\"strange\"; pri=\"4\"", |
| 51 |
# This file should be picked first, because it has the lowest pri among preferred. |
| 52 |
"<http://{{SRV_HOST}}:{{SRV_PORT}}/File1_try1_corrupted>; rel=\"duplicate\"; geo=\"su\"; pri=\"4\"; pref", |
| 53 |
# This file should NOT be picked third due to preferred location set to 'uk' |
| 54 |
"<http://{{SRV_HOST}}:{{SRV_PORT}}/File1_badgeo>; rel =\"duplicate\";pri=\"5\"", |
| 55 |
# This file should be picked as third try, and it should succeed |
| 56 |
"<http://{{SRV_HOST}}:{{SRV_PORT}}/File1_try3_ok>; rel=\'duplicate\'; pri=\"5\";geo=\"uk\"" |
| 57 |
] |
| 58 |
DigestHeader = "SHA-256=\'{{FILE1_HASH}}\'" |
| 59 |
|
| 60 |
# This will be filled as soon as we know server hostname and port |
| 61 |
MetaFileRules = {'SendHeader' : {}} |
| 62 |
|
| 63 |
FileOkServer = WgetFile ("File1_try3_ok", File1) |
| 64 |
FileBadPref = WgetFile ("File1_lowPref", File1_lowPref) |
| 65 |
FileBadHash = WgetFile ("File1_try1_corrupted", File1_corrupted) |
| 66 |
MetaFile = WgetFile ("test.meta", rules=MetaFileRules) |
| 67 |
# In case of Metalink over HTTP, the local file name is |
| 68 |
# derived from the URL suffix. |
| 69 |
FileOkLocal = WgetFile ("test.meta", File1) |
| 70 |
SigFile = WgetFile ("Sig.asc", Signature) |
| 71 |
FileNoMeta = WgetFile ("File2", File2) |
| 72 |
|
| 73 |
WGET_OPTIONS = "--metalink-over-http --preferred-location=uk" |
| 74 |
WGET_URLS = [["test.meta", "File2"]] |
| 75 |
|
| 76 |
Files = [[FileOkServer, FileBadPref, FileBadHash, MetaFile, SigFile, FileNoMeta]] |
| 77 |
Existing_Files = [] |
| 78 |
|
| 79 |
ExpectedReturnCode = 0 |
| 80 |
ExpectedDownloadedFiles = [FileNoMeta, FileOkLocal] |
| 81 |
|
| 82 |
RequestList = [ |
| 83 |
[ |
| 84 |
"HEAD /test.meta", |
| 85 |
"GET /Sig2.asc", |
| 86 |
"GET /Sig.asc", |
| 87 |
"GET /File1_try1_corrupted", |
| 88 |
"GET /File1_try3_ok", |
| 89 |
"HEAD /File2", |
| 90 |
"GET /File2", |
| 91 |
] |
| 92 |
] |
| 93 |
|
| 94 |
################ Pre and Post Test Hooks ##################################### |
| 95 |
pre_test = { |
| 96 |
"ServerFiles" : Files, |
| 97 |
"LocalFiles" : Existing_Files |
| 98 |
} |
| 99 |
test_options = { |
| 100 |
"WgetCommands" : WGET_OPTIONS, |
| 101 |
"Urls" : WGET_URLS |
| 102 |
} |
| 103 |
post_test = { |
| 104 |
"ExpectedFiles" : ExpectedDownloadedFiles, |
| 105 |
"ExpectedRetcode" : ExpectedReturnCode, |
| 106 |
"FilesCrawled" : RequestList, |
| 107 |
} |
| 108 |
|
| 109 |
http_test = HTTPTest ( |
| 110 |
pre_hook=pre_test, |
| 111 |
test_params=test_options, |
| 112 |
post_hook=post_test, |
| 113 |
) |
| 114 |
|
| 115 |
http_test.server_setup() |
| 116 |
srv_host, srv_port = http_test.servers[0].server_inst.socket.getsockname () |
| 117 |
|
| 118 |
MetaFileRules["SendHeader"] = { |
| 119 |
'Link': [ SubstituteServerInfo (LinkHeader, srv_host, srv_port, File1_sha256) |
| 120 |
for LinkHeader in LinkHeaders ], |
| 121 |
'Digest': SubstituteServerInfo (DigestHeader, srv_host, srv_port, File1_sha256), |
| 122 |
} |
| 123 |
|
| 124 |
err = http_test.begin () |
| 125 |
|
| 126 |
exit (err) |