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.1KB

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