]>
git.proxmox.com Git - libgit2.git/blob - tests/remote/httpproxy.c
1 #include "clar_libgit2.h"
6 static git_repository
*repo
;
7 static git_net_url url
= GIT_NET_URL_INIT
;
9 static int orig_proxies_need_reset
= 0;
10 static char *orig_http_proxy
= NULL
;
11 static char *orig_https_proxy
= NULL
;
12 static char *orig_no_proxy
= NULL
;
14 void test_remote_httpproxy__initialize(void)
18 repo
= cl_git_sandbox_init("testrepo");
19 cl_git_pass(git_remote_create(&remote
, repo
, "lg2", "https://github.com/libgit2/libgit2"));
20 cl_git_pass(git_net_url_parse(&url
, "https://github.com/libgit2/libgit2"));
22 git_remote_free(remote
);
24 orig_proxies_need_reset
= 0;
27 void test_remote_httpproxy__cleanup(void)
29 if (orig_proxies_need_reset
) {
30 cl_setenv("HTTP_PROXY", orig_http_proxy
);
31 cl_setenv("HTTPS_PROXY", orig_https_proxy
);
32 cl_setenv("NO_PROXY", orig_no_proxy
);
34 git__free(orig_http_proxy
);
35 git__free(orig_https_proxy
);
36 git__free(orig_no_proxy
);
39 git_net_url_dispose(&url
);
40 cl_git_sandbox_cleanup();
43 static void assert_proxy_is(const char *expected
)
48 cl_git_pass(git_remote_lookup(&remote
, repo
, "lg2"));
49 cl_git_pass(git_remote__http_proxy(&proxy
, remote
, &url
));
52 cl_assert_equal_s(proxy
, expected
);
54 cl_assert_equal_p(proxy
, expected
);
56 git_remote_free(remote
);
60 static void assert_config_match(const char *config
, const char *expected
)
66 cl_repo_set_string(repo
, config
, expected
);
68 cl_git_pass(git_remote_lookup(&remote
, repo
, "lg2"));
69 cl_git_pass(git_remote__http_proxy(&proxy
, remote
, &url
));
72 cl_assert_equal_s(proxy
, expected
);
74 cl_assert_equal_p(proxy
, expected
);
76 git_remote_free(remote
);
80 void test_remote_httpproxy__config_overrides(void)
83 * http.proxy should be honored, then http.<url>.proxy should
84 * be honored in increasing specificity of the url. finally,
85 * remote.<name>.proxy is the most specific.
87 assert_config_match(NULL
, NULL
);
88 assert_config_match("http.proxy", "http://localhost:1/");
89 assert_config_match("http.https://github.com.proxy", "http://localhost:2/");
90 assert_config_match("http.https://github.com/.proxy", "http://localhost:3/");
91 assert_config_match("http.https://github.com/libgit2.proxy", "http://localhost:4/");
92 assert_config_match("http.https://github.com/libgit2/.proxy", "http://localhost:5/");
93 assert_config_match("http.https://github.com/libgit2/libgit2.proxy", "http://localhost:6/");
94 assert_config_match("remote.lg2.proxy", "http://localhost:7/");
97 void test_remote_httpproxy__config_empty_overrides(void)
100 * with greater specificity, an empty config entry overrides
103 assert_config_match("http.proxy", "http://localhost:1/");
104 assert_config_match("http.https://github.com.proxy", "");
105 assert_config_match("http.https://github.com/libgit2/libgit2.proxy", "http://localhost:2/");
106 assert_config_match("remote.lg2.proxy", "");
109 static void assert_global_config_match(const char *config
, const char *expected
)
116 cl_git_pass(git_config_open_default(&cfg
));
117 git_config_set_string(cfg
, config
, expected
);
118 git_config_free(cfg
);
121 cl_git_pass(git_remote_create_detached(&remote
, "https://github.com/libgit2/libgit2"));
122 cl_git_pass(git_remote__http_proxy(&proxy
, remote
, &url
));
125 cl_assert_equal_s(proxy
, expected
);
127 cl_assert_equal_p(proxy
, expected
);
129 git_remote_free(remote
);
133 void test_remote_httpproxy__config_overrides_detached_remote(void)
137 assert_global_config_match(NULL
, NULL
);
138 assert_global_config_match("http.proxy", "http://localhost:1/");
139 assert_global_config_match("http.https://github.com.proxy", "http://localhost:2/");
140 assert_global_config_match("http.https://github.com/.proxy", "http://localhost:3/");
141 assert_global_config_match("http.https://github.com/libgit2.proxy", "http://localhost:4/");
142 assert_global_config_match("http.https://github.com/libgit2/.proxy", "http://localhost:5/");
143 assert_global_config_match("http.https://github.com/libgit2/libgit2.proxy", "http://localhost:6/");
145 cl_git_pass(git_futils_rmdir_r("home", NULL
, GIT_RMDIR_REMOVE_FILES
));
148 void test_remote_httpproxy__env(void)
150 orig_http_proxy
= cl_getenv("HTTP_PROXY");
151 orig_https_proxy
= cl_getenv("HTTPS_PROXY");
152 orig_no_proxy
= cl_getenv("NO_PROXY");
153 orig_proxies_need_reset
= 1;
155 /* Clear everything for a fresh start */
156 cl_setenv("HTTP_PROXY", NULL
);
157 cl_setenv("HTTPS_PROXY", NULL
);
158 cl_setenv("NO_PROXY", NULL
);
160 /* HTTP proxy is ignored for HTTPS */
161 cl_setenv("HTTP_PROXY", "http://localhost:9/");
162 assert_proxy_is(NULL
);
164 /* HTTPS proxy is honored for HTTPS */
165 cl_setenv("HTTPS_PROXY", "http://localhost:10/");
166 assert_proxy_is("http://localhost:10/");
168 /* NO_PROXY is honored */
169 cl_setenv("NO_PROXY", "github.com:443");
170 assert_proxy_is(NULL
);
172 cl_setenv("NO_PROXY", "github.com:80");
173 assert_proxy_is("http://localhost:10/");
175 cl_setenv("NO_PROXY", "github.com");
176 assert_proxy_is(NULL
);
178 cl_setenv("NO_PROXY", "github.dev,github.com,github.foo");
179 assert_proxy_is(NULL
);
181 cl_setenv("HTTPS_PROXY", "");
182 assert_proxy_is(NULL
);
184 /* configuration overrides environment variables */
185 cl_setenv("HTTPS_PROXY", "http://localhost:10/");
186 cl_setenv("NO_PROXY", "github.none");
187 assert_config_match("http.https://github.com.proxy", "http://localhost:11/");