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