View Javadoc
1   /*
2    * Copyright (C) 2016, Chrisian Halstrick <christian.halstrick@sap.com> and
3    * other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v1.0 which accompanies this
7    * distribution, is reproduced below, and is available at
8    * http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or without
13   * modification, are permitted provided that the following conditions are met:
14   *
15   * - Redistributions of source code must retain the above copyright notice, this
16   * list of conditions and the following disclaimer.
17   *
18   * - Redistributions in binary form must reproduce the above copyright notice,
19   * this list of conditions and the following disclaimer in the documentation
20   * and/or other materials provided with the distribution.
21   *
22   * - Neither the name of the Eclipse Foundation, Inc. nor the names of its
23   * contributors may be used to endorse or promote products derived from this
24   * software without specific prior written permission.
25   *
26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36   * POSSIBILITY OF SUCH DAMAGE.
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   * Test how the content of the environment variables http[s]_proxy (upper- and
56   * lowercase) influence the setting of the system properties
57   * http[s].proxy[Host|Port]
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 }