From 66a6e5186ffd12679b8c3ca6980e11efd74d2f00 Mon Sep 17 00:00:00 2001 From: Paul Cristian Sarbu Date: Tue, 21 Feb 2023 15:15:59 +0100 Subject: test: Add tests for reading in Git proxy settings --- test/other_tools/git_operations/TARGETS | 15 +- .../other_tools/git_operations/git_config_proxy.sh | 328 +++++++++++++++++++++ .../git_operations/git_config_run.test.cpp | 20 +- 3 files changed, 360 insertions(+), 3 deletions(-) create mode 100644 test/other_tools/git_operations/git_config_proxy.sh (limited to 'test') diff --git a/test/other_tools/git_operations/TARGETS b/test/other_tools/git_operations/TARGETS index 4b4c43ee..8515d680 100644 --- a/test/other_tools/git_operations/TARGETS +++ b/test/other_tools/git_operations/TARGETS @@ -56,13 +56,26 @@ } , "git_config_ssl": { "type": ["@", "rules", "shell/test", "script"] + , "tainted": ["test"] , "name": ["git_config_ssl"] , "test": ["git_config_ssl.sh"] , "deps": ["git_config_run"] } +, "git_config_proxy": + { "type": ["@", "rules", "shell/test", "script"] + , "tainted": ["test"] + , "name": ["git_config_proxy"] + , "test": ["git_config_proxy.sh"] + , "deps": ["git_config_run"] + } , "TESTS": { "type": "install" , "tainted": ["test"] - , "deps": ["critical_git_ops_mp", "git_repo_remote", "git_config_ssl"] + , "deps": + [ "critical_git_ops_mp" + , "git_repo_remote" + , "git_config_ssl" + , "git_config_proxy" + ] } } diff --git a/test/other_tools/git_operations/git_config_proxy.sh b/test/other_tools/git_operations/git_config_proxy.sh new file mode 100644 index 00000000..a162513c --- /dev/null +++ b/test/other_tools/git_operations/git_config_proxy.sh @@ -0,0 +1,328 @@ +#!/bin/sh +# Copyright 2023 Huawei Cloud Computing Technology Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -eu + +### +# Run Git config proxy settings retrieval tests for various scenarios +# +# Expected Git config (relative) file path: gitconfig +## + +readonly ROOT=`pwd` +readonly TESTEXEC="${ROOT}/src/git_config_run_test" + +# ensure clean env for this type of test +unset all_proxy +unset ALL_PROXY +unset no_proxy +unset NO_PROXY +unset http_proxy +unset https_proxy +unset HTTPS_PROXY + +# Set up the test scenarios to be run in parallel +k=1 # scenarios counter + +echo +echo "Scenario $k: default (clean env and missing config)" +k=$((k+1)) +envcmd="" +url="https://192.0.2.1" +result="" +$( +cd $(mktemp -d) +eval "${envcmd} ${TESTEXEC} proxy ${url} ${result}" +cd ${ROOT} +) +echo "PASSED" + +echo +echo "Scenario $k: proxy from all_proxy envariable" +k=$((k+1)) +envcmd="all_proxy=198.51.100.1:50000" +url="https://example.com" # scheme must match envariable +result="http://198.51.100.1:50000/" # proxy default scheme is http +$( +cd $(mktemp -d) +eval "${envcmd} ${TESTEXEC} proxy ${url} ${result}" +cd ${ROOT} +) +echo "PASSED" + +echo +echo "Scenario $k: proxy from ALL_PROXY envariable" +k=$((k+1)) +envcmd="ALL_PROXY=198.51.100.1:50000" +url="https://example.com" # scheme must match envariable +result="http://198.51.100.1:50000/" # proxy default scheme is http +$( +cd $(mktemp -d) +eval "${envcmd} ${TESTEXEC} proxy ${url} ${result}" +cd ${ROOT} +) +echo "PASSED" + +echo +echo "Scenario $k: lowercase all_proxy envariable priority" +k=$((k+1)) +envcmd="all_proxy=198.51.100.1:50000 ALL_PROXY=192.0.2.1:50505" +url="https://example.com" # scheme does not match envariable +result="http://198.51.100.1:50000/" # proxy default scheme is http +$( +cd $(mktemp -d) +eval "${envcmd} ${TESTEXEC} proxy ${url} ${result}" +cd ${ROOT} +) +echo "PASSED" + +echo +echo "Scenario $k: proxy from http_proxy" +k=$((k+1)) +envcmd="http_proxy=198.51.100.1:50000" +url="http://example.com" # scheme must match envariable +result="http://198.51.100.1:50000/" # proxy default scheme is http +$( +cd $(mktemp -d) +eval "${envcmd} ${TESTEXEC} proxy ${url} ${result}" +cd ${ROOT} +) +echo "PASSED" + +echo +echo "Scenario $k: proxy from https_proxy" +k=$((k+1)) +envcmd="https_proxy=198.51.100.1:50000" +url="https://example.com" # scheme must match envariable +result="http://198.51.100.1:50000/" # proxy default scheme is http +$( +cd $(mktemp -d) +eval "${envcmd} ${TESTEXEC} proxy ${url} ${result}" +cd ${ROOT} +) +echo "PASSED" + +echo +echo "Scenario $k: proxy from HTTPS_PROXY" +k=$((k+1)) +envcmd="HTTPS_PROXY=198.51.100.1:50000" +url="https://example.com" # scheme must match envariable +result="http://198.51.100.1:50000/" # proxy default scheme is http +$( +cd $(mktemp -d) +eval "${envcmd} ${TESTEXEC} proxy ${url} ${result}" +cd ${ROOT} +) +echo "PASSED" + +echo +echo "Scenario $k: scheme mismatch in envariable" +k=$((k+1)) +envcmd="https_proxy=198.51.100.1:50000" +url="http://example.com" # scheme does not match envariable +result="" +$( +cd $(mktemp -d) +eval "${envcmd} ${TESTEXEC} proxy ${url} ${result}" +cd ${ROOT} +) +echo "PASSED" + +echo +echo "Scenario $k: lowercase http_proxy envariable priority" +k=$((k+1)) +envcmd="https_proxy=198.51.100.1:50000 HTTPS_PROXY=192.0.2.1:50505" +url="https://example.com" # scheme does not match envariable +result="http://198.51.100.1:50000/" # proxy default scheme is http +$( +cd $(mktemp -d) +eval "${envcmd} ${TESTEXEC} proxy ${url} ${result}" +cd ${ROOT} +) +echo "PASSED" + +echo +echo "Scenario $k: specific and generic envariables priority" +k=$((k+1)) +envcmd="http_proxy=198.51.100.1:50000 all_proxy=192.0.2.1:50505" +url="http://example.com" # scheme must match envariable +result="http://198.51.100.1:50000/" # proxy default scheme is http +$( +cd $(mktemp -d) +eval "${envcmd} ${TESTEXEC} proxy ${url} ${result}" +cd ${ROOT} +) +echo "PASSED" + +echo +echo "Scenario $k: proxy via http.proxy config entry" +k=$((k+1)) +envcmd="" +url="https://example.com" +result="http://198.51.100.1:50000/" # proxy default scheme is http +$( +cd $(mktemp -d) +cat > gitconfig <.proxy config entry" +k=$((k+1)) +envcmd="" +url="https://example.com" +result="http://198.51.100.1:50000/" # proxy default scheme is http +$( +cd $(mktemp -d) +cat > gitconfig < gitconfig < gitconfig <.proxy config entry" +k=$((k+1)) +envcmd="" +url="https://example.com" +result="" # no proxy +$( +cd $(mktemp -d) +cat > gitconfig < gitconfig < gitconfig < gitconfig < gitconfig < int { } } else if (test_type == "proxy") { - Logger::Log(LogLevel::Error, "Proxy tests not yet implemented"); - return 1; + auto proxy_info = + GitConfigSettings::GetProxySettings(cfg, test_url, logger); + if (not proxy_info) { + Logger::Log(LogLevel::Error, "Missing proxy_info"); + return 1; + } + // check expected result + auto expected_result = + (argc >= 4) ? std::make_optional(args[3]) + : std::nullopt; + auto actual_result = proxy_info.value(); + if (actual_result != expected_result) { + Logger::Log(LogLevel::Error, + "Expected test result {}, but obtained {}", + expected_result ? *expected_result : "nullopt", + actual_result ? *actual_result : "nullopt"); + return 1; + } } else { Logger::Log(LogLevel::Error, -- cgit v1.2.3