httpproxy.c 5.18 KB
Newer Older
1
#include "clar_libgit2.h"
2
#include "futils.h"
3
#include "net.h"
4
#include "remote.h"
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

static git_repository *repo;
static git_net_url url = GIT_NET_URL_INIT;

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);

23 24 25 26 27 28 29 30
	/* Clear everything for a fresh start */
	orig_http_proxy = cl_getenv("HTTP_PROXY");
	orig_https_proxy = cl_getenv("HTTPS_PROXY");
	orig_no_proxy = cl_getenv("NO_PROXY");

	cl_setenv("HTTP_PROXY", NULL);
	cl_setenv("HTTPS_PROXY", NULL);
	cl_setenv("NO_PROXY", NULL);
31 32 33 34
}

void test_remote_httpproxy__cleanup(void)
{
35 36 37 38 39 40 41
	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);
42 43 44 45 46

	git_net_url_dispose(&url);
	cl_git_sandbox_cleanup();
}

47
static void assert_proxy_is(const char *expected)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
{
	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);
}

64
static void assert_config_match(const char *config, const char *expected)
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
{
	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", "");
}

113
static void assert_global_config_match(const char *config, const char *expected)
114 115 116 117 118 119
{
	git_remote *remote;
	char *proxy;
	git_config* cfg;

	if (config) {
120 121 122
		cl_git_pass(git_config_open_default(&cfg));
		git_config_set_string(cfg, config, expected);
		git_config_free(cfg);
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
	}

	cl_git_pass(git_remote_create_detached(&remote, "https://github.com/libgit2/libgit2"));
	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_detached_remote(void)
{
139
	cl_fake_globalconfig(NULL);
140 141 142 143 144 145 146 147 148 149

	assert_global_config_match(NULL, NULL);
	assert_global_config_match("http.proxy", "http://localhost:1/");
	assert_global_config_match("http.https://github.com.proxy", "http://localhost:2/");
	assert_global_config_match("http.https://github.com/.proxy", "http://localhost:3/");
	assert_global_config_match("http.https://github.com/libgit2.proxy", "http://localhost:4/");
	assert_global_config_match("http.https://github.com/libgit2/.proxy", "http://localhost:5/");
	assert_global_config_match("http.https://github.com/libgit2/libgit2.proxy", "http://localhost:6/");
}

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
void test_remote_httpproxy__env(void)
{
	/* 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);

173 174 175
	cl_setenv("HTTPS_PROXY", "");
	assert_proxy_is(NULL);

176
	/* configuration overrides environment variables */
177
	cl_setenv("HTTPS_PROXY", "http://localhost:10/");
178 179 180
	cl_setenv("NO_PROXY", "github.none");
	assert_config_match("http.https://github.com.proxy", "http://localhost:11/");
}