34da3b1
From 17960b57d51ffb19b2b20df3e53da42c555f022c Mon Sep 17 00:00:00 2001
34da3b1
From: Tomas Hozza <thozza@redhat.com>
34da3b1
Date: Fri, 12 May 2017 19:17:30 +0200
34da3b1
Subject: [PATCH 1/4] Added tests for HTTP authentication using credentials
34da3b1
 from .netrc
34da3b1
34da3b1
Getting credentials from .netrc has been broken from time to time, thus
34da3b1
adding a test coverage to prevent regressions.
34da3b1
34da3b1
Also added setting of "HOME" environment variable when executing wget,
34da3b1
to make sure LocalFiles like .netrc, which are created just for the
34da3b1
test, are actually used.
34da3b1
34da3b1
Signed-off-by: Tomas Hozza <thozza@redhat.com>
34da3b1
---
34da3b1
 testenv/Makefile.am                         |  3 ++
34da3b1
 testenv/Test-auth-basic-netrc-pass-given.py | 68 +++++++++++++++++++++++++++++
34da3b1
 testenv/Test-auth-basic-netrc-user-given.py | 68 +++++++++++++++++++++++++++++
34da3b1
 testenv/Test-auth-basic-netrc.py            | 66 ++++++++++++++++++++++++++++
34da3b1
 testenv/test/base_test.py                   |  2 +-
34da3b1
 5 files changed, 206 insertions(+), 1 deletion(-)
34da3b1
 create mode 100755 testenv/Test-auth-basic-netrc-pass-given.py
34da3b1
 create mode 100755 testenv/Test-auth-basic-netrc-user-given.py
34da3b1
 create mode 100755 testenv/Test-auth-basic-netrc.py
34da3b1
34da3b1
diff --git a/testenv/Makefile.am b/testenv/Makefile.am
34da3b1
index 3febec7..7104314 100644
34da3b1
--- a/testenv/Makefile.am
34da3b1
+++ b/testenv/Makefile.am
34da3b1
@@ -75,6 +75,9 @@ if HAVE_PYTHON3
34da3b1
   TESTS = Test-504.py                               \
34da3b1
     Test-auth-basic-fail.py                         \
34da3b1
     Test-auth-basic.py                              \
34da3b1
+    Test-auth-basic-netrc.py                        \
34da3b1
+    Test-auth-basic-netrc-user-given.py             \
34da3b1
+    Test-auth-basic-netrc-pass-given.py             \
34da3b1
     Test-auth-both.py                               \
34da3b1
     Test-auth-digest.py                             \
34da3b1
     Test-auth-no-challenge.py                       \
34da3b1
diff --git a/testenv/Test-auth-basic-netrc-pass-given.py b/testenv/Test-auth-basic-netrc-pass-given.py
34da3b1
new file mode 100755
34da3b1
index 0000000..43dfe34
34da3b1
--- /dev/null
34da3b1
+++ b/testenv/Test-auth-basic-netrc-pass-given.py
34da3b1
@@ -0,0 +1,68 @@
34da3b1
+#!/usr/bin/env python3
34da3b1
+from sys import exit
34da3b1
+from test.http_test import HTTPTest
34da3b1
+from misc.wget_file import WgetFile
34da3b1
+
34da3b1
+"""
34da3b1
+    This test ensures Wget uses credentials from .netrc for Basic Authorization Negotiation.
34da3b1
+    In this case we test that .netrc credentials are used in case only
34da3b1
+    password is given on the command line.
34da3b1
+    Also, we ensure that Wget saves the host after a successful auth and
34da3b1
+    doesn't wait for a challenge the second time.
34da3b1
+"""
34da3b1
+############# File Definitions ###############################################
34da3b1
+File1 = "I am an invisble man."
34da3b1
+File2 = "I too am an invisible man."
34da3b1
+
34da3b1
+User = "Sauron"
34da3b1
+Password = "TheEye"
34da3b1
+
34da3b1
+File1_rules = {
34da3b1
+    "Authentication"    : {
34da3b1
+        "Type"          : "Basic",
34da3b1
+        "User"          : User,
34da3b1
+        "Pass"          : Password
34da3b1
+    }
34da3b1
+}
34da3b1
+File2_rules = {
34da3b1
+    "ExpectHeader"      : {
34da3b1
+        "Authorization" : "Basic U2F1cm9uOlRoZUV5ZQ=="
34da3b1
+    }
34da3b1
+}
34da3b1
+
34da3b1
+Netrc = "machine 127.0.0.1\n\tlogin {0}".format(User)
34da3b1
+
34da3b1
+A_File = WgetFile ("File1", File1, rules=File1_rules)
34da3b1
+B_File = WgetFile ("File2", File2, rules=File2_rules)
34da3b1
+Netrc_File = WgetFile (".netrc", Netrc)
34da3b1
+
34da3b1
+WGET_OPTIONS = "--password={0}".format(Password)
34da3b1
+WGET_URLS = [["File1", "File2"]]
34da3b1
+
34da3b1
+Files = [[A_File, B_File]]
34da3b1
+LocalFiles = [Netrc_File]
34da3b1
+
34da3b1
+ExpectedReturnCode = 0
34da3b1
+ExpectedDownloadedFiles = [A_File, B_File, Netrc_File]
34da3b1
+
34da3b1
+################ Pre and Post Test Hooks #####################################
34da3b1
+pre_test = {
34da3b1
+    "ServerFiles"       : Files,
34da3b1
+    "LocalFiles"        : LocalFiles
34da3b1
+}
34da3b1
+test_options = {
34da3b1
+    "WgetCommands"      : WGET_OPTIONS,
34da3b1
+    "Urls"              : WGET_URLS
34da3b1
+}
34da3b1
+post_test = {
34da3b1
+    "ExpectedFiles"     : ExpectedDownloadedFiles,
34da3b1
+    "ExpectedRetcode"   : ExpectedReturnCode
34da3b1
+}
34da3b1
+
34da3b1
+err = HTTPTest (
34da3b1
+                pre_hook=pre_test,
34da3b1
+                test_params=test_options,
34da3b1
+                post_hook=post_test
34da3b1
+).begin ()
34da3b1
+
34da3b1
+exit (err)
34da3b1
diff --git a/testenv/Test-auth-basic-netrc-user-given.py b/testenv/Test-auth-basic-netrc-user-given.py
34da3b1
new file mode 100755
34da3b1
index 0000000..57b6148
34da3b1
--- /dev/null
34da3b1
+++ b/testenv/Test-auth-basic-netrc-user-given.py
34da3b1
@@ -0,0 +1,68 @@
34da3b1
+#!/usr/bin/env python3
34da3b1
+from sys import exit
34da3b1
+from test.http_test import HTTPTest
34da3b1
+from misc.wget_file import WgetFile
34da3b1
+
34da3b1
+"""
34da3b1
+    This test ensures Wget uses credentials from .netrc for Basic Authorization Negotiation.
34da3b1
+    In this case we test that .netrc credentials are used in case only
34da3b1
+    user login is given on the command line.
34da3b1
+    Also, we ensure that Wget saves the host after a successful auth and
34da3b1
+    doesn't wait for a challenge the second time.
34da3b1
+"""
34da3b1
+############# File Definitions ###############################################
34da3b1
+File1 = "I am an invisble man."
34da3b1
+File2 = "I too am an invisible man."
34da3b1
+
34da3b1
+User = "Sauron"
34da3b1
+Password = "TheEye"
34da3b1
+
34da3b1
+File1_rules = {
34da3b1
+    "Authentication"    : {
34da3b1
+        "Type"          : "Basic",
34da3b1
+        "User"          : User,
34da3b1
+        "Pass"          : Password
34da3b1
+    }
34da3b1
+}
34da3b1
+File2_rules = {
34da3b1
+    "ExpectHeader"      : {
34da3b1
+        "Authorization" : "Basic U2F1cm9uOlRoZUV5ZQ=="
34da3b1
+    }
34da3b1
+}
34da3b1
+
34da3b1
+Netrc = "machine 127.0.0.1\n\tlogin {0}\n\tpassword {1}".format(User, Password)
34da3b1
+
34da3b1
+A_File = WgetFile ("File1", File1, rules=File1_rules)
34da3b1
+B_File = WgetFile ("File2", File2, rules=File2_rules)
34da3b1
+Netrc_File = WgetFile (".netrc", Netrc)
34da3b1
+
34da3b1
+WGET_OPTIONS = "--user={0}".format(User)
34da3b1
+WGET_URLS = [["File1", "File2"]]
34da3b1
+
34da3b1
+Files = [[A_File, B_File]]
34da3b1
+LocalFiles = [Netrc_File]
34da3b1
+
34da3b1
+ExpectedReturnCode = 0
34da3b1
+ExpectedDownloadedFiles = [A_File, B_File, Netrc_File]
34da3b1
+
34da3b1
+################ Pre and Post Test Hooks #####################################
34da3b1
+pre_test = {
34da3b1
+    "ServerFiles"       : Files,
34da3b1
+    "LocalFiles"        : LocalFiles
34da3b1
+}
34da3b1
+test_options = {
34da3b1
+    "WgetCommands"      : WGET_OPTIONS,
34da3b1
+    "Urls"              : WGET_URLS
34da3b1
+}
34da3b1
+post_test = {
34da3b1
+    "ExpectedFiles"     : ExpectedDownloadedFiles,
34da3b1
+    "ExpectedRetcode"   : ExpectedReturnCode
34da3b1
+}
34da3b1
+
34da3b1
+err = HTTPTest (
34da3b1
+                pre_hook=pre_test,
34da3b1
+                test_params=test_options,
34da3b1
+                post_hook=post_test
34da3b1
+).begin ()
34da3b1
+
34da3b1
+exit (err)
34da3b1
diff --git a/testenv/Test-auth-basic-netrc.py b/testenv/Test-auth-basic-netrc.py
34da3b1
new file mode 100755
34da3b1
index 0000000..5710fe7
34da3b1
--- /dev/null
34da3b1
+++ b/testenv/Test-auth-basic-netrc.py
34da3b1
@@ -0,0 +1,66 @@
34da3b1
+#!/usr/bin/env python3
34da3b1
+from sys import exit
34da3b1
+from test.http_test import HTTPTest
34da3b1
+from misc.wget_file import WgetFile
34da3b1
+
34da3b1
+"""
34da3b1
+    This test ensures Wget uses credentials from .netrc for Basic Authorization Negotiation.
34da3b1
+    In this case we test that .netrc credentials are used in case no user
34da3b1
+    login and no password is given on the command line.
34da3b1
+    Also, we ensure that Wget saves the host after a successful auth and
34da3b1
+    doesn't wait for a challenge the second time.
34da3b1
+"""
34da3b1
+############# File Definitions ###############################################
34da3b1
+File1 = "I am an invisble man."
34da3b1
+File2 = "I too am an invisible man."
34da3b1
+
34da3b1
+User = "Sauron"
34da3b1
+Password = "TheEye"
34da3b1
+
34da3b1
+File1_rules = {
34da3b1
+    "Authentication"    : {
34da3b1
+        "Type"          : "Basic",
34da3b1
+        "User"          : User,
34da3b1
+        "Pass"          : Password
34da3b1
+    }
34da3b1
+}
34da3b1
+File2_rules = {
34da3b1
+    "ExpectHeader"      : {
34da3b1
+        "Authorization" : "Basic U2F1cm9uOlRoZUV5ZQ=="
34da3b1
+    }
34da3b1
+}
34da3b1
+
34da3b1
+Netrc = "machine 127.0.0.1\n\tlogin {0}\n\tpassword {1}".format(User, Password)
34da3b1
+
34da3b1
+A_File = WgetFile ("File1", File1, rules=File1_rules)
34da3b1
+B_File = WgetFile ("File2", File2, rules=File2_rules)
34da3b1
+Netrc_File = WgetFile (".netrc", Netrc)
34da3b1
+
34da3b1
+WGET_URLS = [["File1", "File2"]]
34da3b1
+
34da3b1
+Files = [[A_File, B_File]]
34da3b1
+LocalFiles = [Netrc_File]
34da3b1
+
34da3b1
+ExpectedReturnCode = 0
34da3b1
+ExpectedDownloadedFiles = [A_File, B_File, Netrc_File]
34da3b1
+
34da3b1
+################ Pre and Post Test Hooks #####################################
34da3b1
+pre_test = {
34da3b1
+    "ServerFiles"       : Files,
34da3b1
+    "LocalFiles"        : LocalFiles
34da3b1
+}
34da3b1
+test_options = {
34da3b1
+    "Urls"              : WGET_URLS
34da3b1
+}
34da3b1
+post_test = {
34da3b1
+    "ExpectedFiles"     : ExpectedDownloadedFiles,
34da3b1
+    "ExpectedRetcode"   : ExpectedReturnCode
34da3b1
+}
34da3b1
+
34da3b1
+err = HTTPTest (
34da3b1
+                pre_hook=pre_test,
34da3b1
+                test_params=test_options,
34da3b1
+                post_hook=post_test
34da3b1
+).begin ()
34da3b1
+
34da3b1
+exit (err)
34da3b1
diff --git a/testenv/test/base_test.py b/testenv/test/base_test.py
34da3b1
index b0087e9..bb706d8 100644
34da3b1
--- a/testenv/test/base_test.py
34da3b1
+++ b/testenv/test/base_test.py
34da3b1
@@ -102,7 +102,7 @@ class BaseTest:
34da3b1
             time.sleep(float(os.getenv("SERVER_WAIT")))
34da3b1
 
34da3b1
         try:
34da3b1
-            ret_code = call(params)
34da3b1
+            ret_code = call(params, env={"HOME": os.getcwd()})
34da3b1
         except FileNotFoundError:
34da3b1
             raise TestFailed("The Wget Executable does not exist at the "
34da3b1
                              "expected path.")
34da3b1
-- 
34da3b1
2.7.5
34da3b1