]> git.proxmox.com Git - libgit2.git/blob - tests/remote/httpproxy.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / remote / httpproxy.c
1 #include "clar_libgit2.h"
2 #include "futils.h"
3 #include "net.h"
4 #include "remote.h"
5
6 static git_repository *repo;
7 static git_net_url url = GIT_NET_URL_INIT;
8
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;
13
14 void test_remote_httpproxy__initialize(void)
15 {
16 git_remote *remote;
17
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"));
21
22 git_remote_free(remote);
23
24 orig_proxies_need_reset = 0;
25 }
26
27 void test_remote_httpproxy__cleanup(void)
28 {
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);
33
34 git__free(orig_http_proxy);
35 git__free(orig_https_proxy);
36 git__free(orig_no_proxy);
37 }
38
39 git_net_url_dispose(&url);
40 cl_git_sandbox_cleanup();
41 }
42
43 static void assert_proxy_is(const char *expected)
44 {
45 git_remote *remote;
46 char *proxy;
47
48 cl_git_pass(git_remote_lookup(&remote, repo, "lg2"));
49 cl_git_pass(git_remote__http_proxy(&proxy, remote, &url));
50
51 if (expected)
52 cl_assert_equal_s(proxy, expected);
53 else
54 cl_assert_equal_p(proxy, expected);
55
56 git_remote_free(remote);
57 git__free(proxy);
58 }
59
60 static void assert_config_match(const char *config, const char *expected)
61 {
62 git_remote *remote;
63 char *proxy;
64
65 if (config)
66 cl_repo_set_string(repo, config, expected);
67
68 cl_git_pass(git_remote_lookup(&remote, repo, "lg2"));
69 cl_git_pass(git_remote__http_proxy(&proxy, remote, &url));
70
71 if (expected)
72 cl_assert_equal_s(proxy, expected);
73 else
74 cl_assert_equal_p(proxy, expected);
75
76 git_remote_free(remote);
77 git__free(proxy);
78 }
79
80 void test_remote_httpproxy__config_overrides(void)
81 {
82 /*
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.
86 */
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/");
95 }
96
97 void test_remote_httpproxy__config_empty_overrides(void)
98 {
99 /*
100 * with greater specificity, an empty config entry overrides
101 * a set one
102 */
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", "");
107 }
108
109 static void assert_global_config_match(const char *config, const char *expected)
110 {
111 git_remote *remote;
112 char *proxy;
113 git_config* cfg;
114
115 if (config) {
116 cl_git_pass(git_config_open_default(&cfg));
117 git_config_set_string(cfg, config, expected);
118 git_config_free(cfg);
119 }
120
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));
123
124 if (expected)
125 cl_assert_equal_s(proxy, expected);
126 else
127 cl_assert_equal_p(proxy, expected);
128
129 git_remote_free(remote);
130 git__free(proxy);
131 }
132
133 void test_remote_httpproxy__config_overrides_detached_remote(void)
134 {
135 cl_fake_home();
136
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/");
144
145 cl_git_pass(git_futils_rmdir_r("home", NULL, GIT_RMDIR_REMOVE_FILES));
146 }
147
148 void test_remote_httpproxy__env(void)
149 {
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;
154
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);
159
160 /* HTTP proxy is ignored for HTTPS */
161 cl_setenv("HTTP_PROXY", "http://localhost:9/");
162 assert_proxy_is(NULL);
163
164 /* HTTPS proxy is honored for HTTPS */
165 cl_setenv("HTTPS_PROXY", "http://localhost:10/");
166 assert_proxy_is("http://localhost:10/");
167
168 /* NO_PROXY is honored */
169 cl_setenv("NO_PROXY", "github.com:443");
170 assert_proxy_is(NULL);
171
172 cl_setenv("NO_PROXY", "github.com:80");
173 assert_proxy_is("http://localhost:10/");
174
175 cl_setenv("NO_PROXY", "github.com");
176 assert_proxy_is(NULL);
177
178 cl_setenv("NO_PROXY", "github.dev,github.com,github.foo");
179 assert_proxy_is(NULL);
180
181 cl_setenv("HTTPS_PROXY", "");
182 assert_proxy_is(NULL);
183
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/");
188 }