You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProxyConfigTest.java 8.2KB

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