1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package org.eclipse.jgit.pgm;
39
40 import static java.nio.charset.StandardCharsets.UTF_8;
41 import static org.junit.Assert.assertEquals;
42
43 import java.io.ByteArrayOutputStream;
44 import java.io.IOException;
45 import java.io.InputStream;
46 import java.io.UnsupportedEncodingException;
47 import java.net.MalformedURLException;
48 import java.util.List;
49 import java.util.Map;
50
51 import org.junit.Before;
52 import org.junit.Test;
53
54
55
56
57
58
59 public class ProxyConfigTest {
60 private ProcessBuilder processBuilder;
61
62 private Map<String, String> environment;
63
64 @Before
65 public void setUp() {
66 String separator = System.getProperty("file.separator");
67 String classpath = System.getProperty("java.class.path");
68 String path = System.getProperty("java.home") + separator + "bin"
69 + separator + "java";
70 processBuilder = new ProcessBuilder(path, "-cp", classpath,
71 ProxyPropertiesDumper.class.getName());
72 environment = processBuilder.environment();
73 environment.remove("http_proxy");
74 environment.remove("https_proxy");
75 environment.remove("HTTP_PROXY");
76 environment.remove("HTTPS_PROXY");
77 }
78
79 @Test
80 public void testNoSetting() throws Exception {
81 Process start = processBuilder.start();
82 start.waitFor();
83 assertEquals(
84 "http.proxyHost: null, http.proxyPort: null, https.proxyHost: null, https.proxyPort: null",
85 getOutput(start));
86 }
87
88 @Test
89 public void testHttpProxy_lowerCase() throws Exception {
90 environment.put("http_proxy", "http://xx:1234");
91 Process start = processBuilder.start();
92 start.waitFor();
93 assertEquals(
94 "http.proxyHost: xx, http.proxyPort: 1234, https.proxyHost: null, https.proxyPort: null",
95 getOutput(start));
96 }
97
98 @Test
99 public void testHttpProxy_upperCase() throws Exception {
100 environment.put("HTTP_PROXY", "http://XX:1234");
101 Process start = processBuilder.start();
102 start.waitFor();
103 assertEquals(
104 "http.proxyHost: null, http.proxyPort: null, https.proxyHost: null, https.proxyPort: null",
105 getOutput(start));
106 }
107
108 @Test
109 public void testHttpProxy_bothCases() throws Exception {
110 environment.put("http_proxy", "http://xx:1234");
111 environment.put("HTTP_PROXY", "http://XX:1234");
112 Process start = processBuilder.start();
113 start.waitFor();
114 assertEquals(
115 "http.proxyHost: xx, http.proxyPort: 1234, https.proxyHost: null, https.proxyPort: null",
116 getOutput(start));
117 }
118
119 @Test
120 public void testHttpsProxy_lowerCase() throws Exception {
121 environment.put("https_proxy", "http://xx:1234");
122 Process start = processBuilder.start();
123 start.waitFor();
124 assertEquals(
125 "http.proxyHost: null, http.proxyPort: null, https.proxyHost: xx, https.proxyPort: 1234",
126 getOutput(start));
127 }
128
129 @Test
130 public void testHttpsProxy_upperCase() throws Exception {
131 environment.put("HTTPS_PROXY", "http://XX:1234");
132 Process start = processBuilder.start();
133 start.waitFor();
134 assertEquals(
135 "http.proxyHost: null, http.proxyPort: null, https.proxyHost: XX, https.proxyPort: 1234",
136 getOutput(start));
137 }
138
139 @Test
140 public void testHttpsProxy_bothCases() throws Exception {
141 environment.put("https_proxy", "http://xx:1234");
142 environment.put("HTTPS_PROXY", "http://XX:1234");
143 Process start = processBuilder.start();
144 start.waitFor();
145 assertEquals(
146 "http.proxyHost: null, http.proxyPort: null, https.proxyHost: xx, https.proxyPort: 1234",
147 getOutput(start));
148 }
149
150 @Test
151 public void testAll() throws Exception {
152 environment.put("http_proxy", "http://xx:1234");
153 environment.put("HTTP_PROXY", "http://XX:1234");
154 environment.put("https_proxy", "http://yy:1234");
155 environment.put("HTTPS_PROXY", "http://YY:1234");
156 Process start = processBuilder.start();
157 start.waitFor();
158 assertEquals(
159 "http.proxyHost: xx, http.proxyPort: 1234, https.proxyHost: yy, https.proxyPort: 1234",
160 getOutput(start));
161 }
162
163 @Test
164 public void testDontOverwriteHttp()
165 throws IOException, InterruptedException {
166 environment.put("http_proxy", "http://xx:1234");
167 environment.put("HTTP_PROXY", "http://XX:1234");
168 environment.put("https_proxy", "http://yy:1234");
169 environment.put("HTTPS_PROXY", "http://YY:1234");
170 List<String> command = processBuilder.command();
171 command.add(1, "-Dhttp.proxyHost=gondola");
172 command.add(2, "-Dhttp.proxyPort=5678");
173 command.add("dontClearProperties");
174 Process start = processBuilder.start();
175 start.waitFor();
176 assertEquals(
177 "http.proxyHost: gondola, http.proxyPort: 5678, https.proxyHost: yy, https.proxyPort: 1234",
178 getOutput(start));
179 }
180
181 @Test
182 public void testOverwriteHttpPort()
183 throws IOException, InterruptedException {
184 environment.put("http_proxy", "http://xx:1234");
185 environment.put("HTTP_PROXY", "http://XX:1234");
186 environment.put("https_proxy", "http://yy:1234");
187 environment.put("HTTPS_PROXY", "http://YY:1234");
188 List<String> command = processBuilder.command();
189 command.add(1, "-Dhttp.proxyPort=5678");
190 command.add("dontClearProperties");
191 Process start = processBuilder.start();
192 start.waitFor();
193 assertEquals(
194 "http.proxyHost: xx, http.proxyPort: 1234, https.proxyHost: yy, https.proxyPort: 1234",
195 getOutput(start));
196 }
197
198 private static String getOutput(Process p)
199 throws IOException, UnsupportedEncodingException {
200 try (InputStream inputStream = p.getInputStream()) {
201 ByteArrayOutputStream result = new ByteArrayOutputStream();
202 byte[] buffer = new byte[1024];
203 int length;
204 while ((length = inputStream.read(buffer)) != -1) {
205 result.write(buffer, 0, length);
206 }
207 return result.toString(UTF_8.name());
208 }
209 }
210 }
211
212 class ProxyPropertiesDumper {
213 public static void main(String args[]) {
214 try {
215 if (args.length == 0 || !args[0].equals("dontClearProperties")) {
216 System.clearProperty("http.proxyHost");
217 System.clearProperty("http.proxyPort");
218 System.clearProperty("https.proxyHost");
219 System.clearProperty("https.proxyPort");
220 }
221 Main.configureHttpProxy();
222 System.out.printf(
223 "http.proxyHost: %s, http.proxyPort: %s, https.proxyHost: %s, https.proxyPort: %s",
224 System.getProperty("http.proxyHost"),
225 System.getProperty("http.proxyPort"),
226 System.getProperty("https.proxyHost"),
227 System.getProperty("https.proxyPort"));
228 System.out.flush();
229 } catch (MalformedURLException e) {
230 System.out.println("exception: " + e);
231 }
232 }
233 }