| 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 with a malformed base64 Digest header. |
| 10 |
|
| 11 |
With --trust-server-names, trust the metalink:file names. |
| 12 |
|
| 13 |
Without --trust-server-names, don't trust the metalink:file names: |
| 14 |
use the basename of --input-metalink, and add a sequential number |
| 15 |
(e.g. .#1, .#2, etc.). |
| 16 |
|
| 17 |
Strip the directory from unsafe paths. |
| 18 |
""" |
| 19 |
|
| 20 |
############# File Definitions ############################################### |
| 21 |
bad = "Ouch!" |
| 22 |
bad_sha256 = b64encode (hashlib.sha256 (bad.encode ('UTF-8')).digest ()).decode ('ascii') |
| 23 |
|
| 24 |
LinkHeaders = ["<http://{{SRV_HOST}}:{{SRV_PORT}}/wrong_file>; rel=duplicate; pri=1"] |
| 25 |
DigestHeader = "SHA-256=bad_base64,SHA-256={{BAD_HASH}}" |
| 26 |
|
| 27 |
# This will be filled as soon as we know server hostname and port |
| 28 |
MetaHTTPRules = {'SendHeader' : {}} |
| 29 |
|
| 30 |
MetaHTTP = WgetFile ("main.metalink", rules=MetaHTTPRules) |
| 31 |
|
| 32 |
wrong_file = WgetFile ("wrong_file", bad) |
| 33 |
wrong_file_down = WgetFile ("main.metalink", bad) |
| 34 |
|
| 35 |
WGET_OPTIONS = "--metalink-over-http" |
| 36 |
WGET_URLS = [["main.metalink"]] |
| 37 |
|
| 38 |
RequestList = [[ |
| 39 |
"HEAD /main.metalink", |
| 40 |
"GET /wrong_file" |
| 41 |
]] |
| 42 |
|
| 43 |
Files = [[ |
| 44 |
MetaHTTP, |
| 45 |
wrong_file |
| 46 |
]] |
| 47 |
Existing_Files = [] |
| 48 |
|
| 49 |
ExpectedReturnCode = 0 |
| 50 |
ExpectedDownloadedFiles = [wrong_file_down] |
| 51 |
|
| 52 |
################ Pre and Post Test Hooks ##################################### |
| 53 |
pre_test = { |
| 54 |
"ServerFiles" : Files, |
| 55 |
"LocalFiles" : Existing_Files |
| 56 |
} |
| 57 |
test_options = { |
| 58 |
"WgetCommands" : WGET_OPTIONS, |
| 59 |
"Urls" : WGET_URLS |
| 60 |
} |
| 61 |
post_test = { |
| 62 |
"ExpectedFiles" : ExpectedDownloadedFiles, |
| 63 |
"ExpectedRetcode" : ExpectedReturnCode, |
| 64 |
"FilesCrawled" : RequestList |
| 65 |
} |
| 66 |
|
| 67 |
http_test = HTTPTest ( |
| 68 |
pre_hook=pre_test, |
| 69 |
test_params=test_options, |
| 70 |
post_hook=post_test |
| 71 |
) |
| 72 |
|
| 73 |
http_test.server_setup() |
| 74 |
### Get and use dynamic server sockname |
| 75 |
srv_host, srv_port = http_test.servers[0].server_inst.socket.getsockname () |
| 76 |
|
| 77 |
DigestHeader = DigestHeader.replace('{{BAD_HASH}}', bad_sha256) |
| 78 |
|
| 79 |
# Helper function for hostname, port and digest substitution |
| 80 |
def SubstituteServerInfo (text, host, port): |
| 81 |
text = text.replace('{{SRV_HOST}}', host) |
| 82 |
text = text.replace('{{SRV_PORT}}', str (port)) |
| 83 |
return text |
| 84 |
|
| 85 |
MetaHTTPRules["SendHeader"] = { |
| 86 |
'Link': [ SubstituteServerInfo (LinkHeader, srv_host, srv_port) |
| 87 |
for LinkHeader in LinkHeaders ], |
| 88 |
'Digest': DigestHeader |
| 89 |
} |
| 90 |
|
| 91 |
err = http_test.begin () |
| 92 |
|
| 93 |
exit (err) |