diff options
| author | Edward Thomson <ethomson@edwardthomson.com> | 2021-09-01 20:34:28 -0400 |
|---|---|---|
| committer | Edward Thomson <ethomson@edwardthomson.com> | 2021-09-01 21:20:25 -0400 |
| commit | 3c0f14cc95debb426bd53150aac0eef1a7f625d8 (patch) | |
| tree | 1ae5a9dd0aab3ffec9d7433efff5b97696db9b9d /tests/remote/httpproxy.c | |
| parent | f89dc917d7cb1504b651fdb58c96397b3081a80d (diff) | |
| download | libgit2-3c0f14cc95debb426bd53150aac0eef1a7f625d8.tar.gz | |
remote: refactor proxy detection
Update the proxy detection for a remote.
1. Honor `http.<url>.proxy` syntax for a remote's direct URL and
parent URLs.
2. Honor an empty configuration URL to override a proxy configuration.
Add tests to ensure that configuration specificity is honored.
Diffstat (limited to 'tests/remote/httpproxy.c')
| -rw-r--r-- | tests/remote/httpproxy.c | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/tests/remote/httpproxy.c b/tests/remote/httpproxy.c new file mode 100644 index 000000000..097db4cd5 --- /dev/null +++ b/tests/remote/httpproxy.c @@ -0,0 +1,139 @@ +#include "clar_libgit2.h" +#include "remote.h" +#include "net.h" + +static git_repository *repo; +static git_net_url url = GIT_NET_URL_INIT; + +static int orig_proxies_need_reset = 0; +static char *orig_http_proxy = NULL; +static char *orig_https_proxy = NULL; +static char *orig_no_proxy = NULL; + +void test_remote_httpproxy__initialize(void) +{ + git_remote *remote; + + repo = cl_git_sandbox_init("testrepo"); + cl_git_pass(git_remote_create(&remote, repo, "lg2", "https://github.com/libgit2/libgit2")); + cl_git_pass(git_net_url_parse(&url, "https://github.com/libgit2/libgit2")); + + git_remote_free(remote); + + orig_proxies_need_reset = 0; +} + +void test_remote_httpproxy__cleanup(void) +{ + if (orig_proxies_need_reset) { + cl_setenv("HTTP_PROXY", orig_http_proxy); + cl_setenv("HTTPS_PROXY", orig_https_proxy); + cl_setenv("NO_PROXY", orig_no_proxy); + + git__free(orig_http_proxy); + git__free(orig_https_proxy); + git__free(orig_no_proxy); + } + + git_net_url_dispose(&url); + cl_git_sandbox_cleanup(); +} + +void assert_proxy_is(const char *expected) +{ + git_remote *remote; + char *proxy; + + cl_git_pass(git_remote_lookup(&remote, repo, "lg2")); + cl_git_pass(git_remote__http_proxy(&proxy, remote, &url)); + + if (expected) + cl_assert_equal_s(proxy, expected); + else + cl_assert_equal_p(proxy, expected); + + git_remote_free(remote); + git__free(proxy); +} + +void assert_config_match(const char *config, const char *expected) +{ + git_remote *remote; + char *proxy; + + if (config) + cl_repo_set_string(repo, config, expected); + + cl_git_pass(git_remote_lookup(&remote, repo, "lg2")); + cl_git_pass(git_remote__http_proxy(&proxy, remote, &url)); + + if (expected) + cl_assert_equal_s(proxy, expected); + else + cl_assert_equal_p(proxy, expected); + + git_remote_free(remote); + git__free(proxy); +} + +void test_remote_httpproxy__config_overrides(void) +{ + /* + * http.proxy should be honored, then http.<url>.proxy should + * be honored in increasing specificity of the url. finally, + * remote.<name>.proxy is the most specific. + */ + assert_config_match(NULL, NULL); + assert_config_match("http.proxy", "http://localhost:1/"); + assert_config_match("http.https://github.com.proxy", "http://localhost:2/"); + assert_config_match("http.https://github.com/.proxy", "http://localhost:3/"); + assert_config_match("http.https://github.com/libgit2.proxy", "http://localhost:4/"); + assert_config_match("http.https://github.com/libgit2/.proxy", "http://localhost:5/"); + assert_config_match("http.https://github.com/libgit2/libgit2.proxy", "http://localhost:6/"); + assert_config_match("remote.lg2.proxy", "http://localhost:7/"); +} + +void test_remote_httpproxy__config_empty_overrides(void) +{ + /* + * with greater specificity, an empty config entry overrides + * a set one + */ + assert_config_match("http.proxy", "http://localhost:1/"); + assert_config_match("http.https://github.com.proxy", ""); + assert_config_match("http.https://github.com/libgit2/libgit2.proxy", "http://localhost:2/"); + assert_config_match("remote.lg2.proxy", ""); +} + +void test_remote_httpproxy__env(void) +{ + orig_http_proxy = cl_getenv("HTTP_PROXY"); + orig_https_proxy = cl_getenv("HTTPS_PROXY"); + orig_no_proxy = cl_getenv("NO_PROXY"); + orig_proxies_need_reset = 1; + + /* HTTP proxy is ignored for HTTPS */ + cl_setenv("HTTP_PROXY", "http://localhost:9/"); + assert_proxy_is(NULL); + + /* HTTPS proxy is honored for HTTPS */ + cl_setenv("HTTPS_PROXY", "http://localhost:10/"); + assert_proxy_is("http://localhost:10/"); + + /* NO_PROXY is honored */ + cl_setenv("NO_PROXY", "github.com:443"); + assert_proxy_is(NULL); + + cl_setenv("NO_PROXY", "github.com:80"); + assert_proxy_is("http://localhost:10/"); + + cl_setenv("NO_PROXY", "github.com"); + assert_proxy_is(NULL); + + cl_setenv("NO_PROXY", "github.dev,github.com,github.foo"); + assert_proxy_is(NULL); + + /* configuration overrides environment variables */ + cl_setenv("NO_PROXY", "github.none"); + assert_config_match("http.https://github.com.proxy", "http://localhost:11/"); +} |
