From 7b793890ce5085ad6dc8f6a11a1e3410bcec441f Mon Sep 17 00:00:00 2001
From: Hubert Tarasiuk
Date: Fri, 19 Jun 2015 14:31:16 +0200
Subject: [PATCH 5/8] Test case for Metalink over HTTP.
* testenv/Test-metalink-http.py: New test.
* testenv/Makefile.am: Add to test list.
---
testenv/Makefile.am | 3 +-
testenv/Test-metalink-http.py | 115 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 117 insertions(+), 1 deletion(-)
create mode 100755 testenv/Test-metalink-http.py
diff --git a/testenv/Makefile.am b/testenv/Makefile.am
index 58e116a..ab6009d 100644
--- a/testenv/Makefile.am
+++ b/testenv/Makefile.am
@@ -56,7 +56,8 @@ if HAVE_PYTHON3
Test-redirect-crash.py \
Test-reserved-chars.py \
Test-condget.py \
- Test-metalink-xml.py
+ Test-metalink-xml.py \
+ Test-metalink-http.py
# added test cases expected to fail here and under TESTS
XFAIL_TESTS =
diff --git a/testenv/Test-metalink-http.py b/testenv/Test-metalink-http.py
new file mode 100755
index 0000000..9072b90
--- /dev/null
+++ b/testenv/Test-metalink-http.py
@@ -0,0 +1,115 @@
+#!/usr/bin/env python3
+from sys import exit
+from test.http_test import HTTPTest
+from misc.wget_file import WgetFile
+import re
+import hashlib
+from base64 import b64encode
+
+"""
+ This is to test Metalink as HTTP file support in Wget.
+"""
+TEST_NAME = "Metalink in HTTP"
+
+# Helper function for hostname, port and digest substitution
+def SubstituteServerInfo(text, host, port, digest):
+ text = re.sub(r'{{FILE1_HASH}}', digest, text)
+ text = re.sub(r'{{SRV_HOST}}', host, text)
+ text = re.sub(r'{{SRV_PORT}}', str(port), text)
+ 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.
+ "; rel=duplicate; pri=9; geo=pl",
+ # This file should be picked second, after hash failure.
+ "; rel =duplicate;pref; pri=7",
+ # This signature download will fail.
+ "; rel=describedby; type=application/pgp-signature",
+ # Two good signatures
+ "; rel=describedby; type=application/pgp-signature",
+ "; rel=describedby; type=application/pgp-signature",
+ # Bad URL scheme
+ "; rel=duplicate; pri=4",
+ # rel missing
+ "; pri=1; pref",
+ # invalid rel
+ "; rel=strange; pri=4",
+ # This file should be picked first, because it has the lowest pri among preferred.
+ "; rel=duplicate; geo=su; pri=4; pref",
+ # This file should be picked as third try, and it should succeed
+ "; rel=duplicate; pri=5"
+ ]
+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 "
+WGET_URLS = [["test.meta", "File2"]]
+
+Files = [[FileOkServer, FileBadPref, FileBadHash, MetaFile, SigFile, FileNoMeta]]
+Existing_Files = []
+
+ExpectedReturnCode = 0
+ExpectedDownloadedFiles = [FileNoMeta, FileOkLocal]
+
+################ 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
+}
+
+http_test = HTTPTest (
+ name=TEST_NAME,
+ pre_hook=pre_test,
+ test_params=test_options,
+ post_hook=post_test,
+)
+
+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),
+}
+
+print (MetaFileRules["SendHeader"])
+
+err = http_test.begin()
+
+exit (err)
--
2.4.3