001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.core;
043:
044: import java.lang.reflect.Field;
045: import java.net.ProxySelector;
046: import java.util.logging.Logger;
047: import java.util.prefs.AbstractPreferences;
048: import java.util.prefs.PreferenceChangeEvent;
049: import java.util.prefs.PreferenceChangeListener;
050: import java.util.prefs.Preferences;
051: import junit.framework.TestResult;
052: import org.netbeans.junit.*;
053: import junit.textui.TestRunner;
054: import org.openide.util.NbPreferences;
055:
056: /** Tests HTTP Proxy settings.
057: *
058: * @author Jiri Rechtacek
059: * @see http://www.netbeans.org/issues/show_bug.cgi?id=51641
060: */
061: public class HttpSettingsTest extends NbTestCase {
062: private final Object sync = getEventQueueSync();
063: private static String SYSTEM_PROXY_HOST = "system.cache.org";
064: private static String SYSTEM_PROXY_PORT = "777";
065: private static String USER_PROXY_HOST = "my.webcache";
066: private static String USER_PROXY_PORT = "8080";
067: private static String USER_HTTPS_PROXY_HOST = "secure.webcache";
068: private static String USER_HTTPS_PROXY_PORT = "8080";
069:
070: private Preferences proxyPreferences;
071: private static String SILLY_USER_PROXY_HOST = "http://my.webcache";
072: private static String SILLY_SYSTEM_PROXY_HOST = "http://system.cache.org";
073: private static String SILLY_SYSTEM_PROXY_PORT = "777//";
074: private static String MY_NON_PROXY_HOSTS = "myhost.mydomain.net";
075:
076: private static String NETBEANS_ORG = "*.netbeans.org";
077: private static String OTHER_ORG = "*.other.org";
078:
079: public HttpSettingsTest(String name) {
080: super (name);
081: }
082:
083: public static void main(String[] args) {
084: TestRunner.run(new NbTestSuite(HttpSettingsTest.class));
085: }
086:
087: public void run(final TestResult result) {
088: //just initialize Preferences before code NbTestCase
089: Preferences.userRoot();
090: super .run(result);
091: }
092:
093: protected int timeOut() {
094: return 20 * 1000;
095: }
096:
097: protected void setUp() throws Exception {
098: super .setUp();
099: System.setProperty("http.nonProxyHosts", NETBEANS_ORG + ','
100: + NETBEANS_ORG);
101: ProxySelector.setDefault(new NbProxySelector());
102: proxyPreferences = NbPreferences.root().node(
103: "/org/netbeans/core");
104: proxyPreferences
105: .addPreferenceChangeListener(new PreferenceChangeListener() {
106: public void preferenceChange(
107: PreferenceChangeEvent arg0) {
108: synchronized (sync) {
109: sync.notifyAll();
110: }
111: }
112: });
113: System.setProperty("netbeans.system_http_proxy",
114: SYSTEM_PROXY_HOST + ":" + SYSTEM_PROXY_PORT);
115: System.setProperty("netbeans.system_http_non_proxy_hosts",
116: OTHER_ORG);
117: synchronized (sync) {
118: if (!USER_PROXY_HOST.equals(proxyPreferences.get(
119: "proxyHttpHost", ""))) {
120: proxyPreferences.put("proxyHttpHost", USER_PROXY_HOST);
121: sync.wait(10000);
122: }
123: }
124: synchronized (sync) {
125: if (!USER_PROXY_PORT.equals(proxyPreferences.get(
126: "proxyHttpPort", ""))) {
127: proxyPreferences.put("proxyHttpPort", USER_PROXY_PORT);
128: sync.wait(10000);
129: }
130: }
131: synchronized (sync) {
132: if (!USER_HTTPS_PROXY_HOST.equals(proxyPreferences.get(
133: "proxyHttpsHost", ""))) {
134: proxyPreferences.put("proxyHttpsHost",
135: USER_HTTPS_PROXY_HOST);
136: sync.wait(10000);
137: }
138: }
139: synchronized (sync) {
140: if (!USER_HTTPS_PROXY_PORT.equals(proxyPreferences.get(
141: "proxyHttpsPort", ""))) {
142: proxyPreferences.put("proxyHttpsPort",
143: USER_HTTPS_PROXY_PORT);
144: sync.wait(10000);
145: }
146: }
147: }
148:
149: private void sillySetUp() throws Exception {
150: synchronized (sync) {
151: if (ProxySettings.DIRECT_CONNECTION != (proxyPreferences
152: .getInt("proxyType", -1))) {
153: proxyPreferences.putInt("proxyType",
154: ProxySettings.DIRECT_CONNECTION);
155: sync.wait();
156: }
157: }
158:
159: System
160: .setProperty("netbeans.system_http_proxy",
161: SILLY_SYSTEM_PROXY_HOST + ":"
162: + SILLY_SYSTEM_PROXY_PORT);
163: synchronized (sync) {
164: if (!SILLY_USER_PROXY_HOST.equals(proxyPreferences.get(
165: "proxyHttpHost", ""))) {
166: proxyPreferences.put("proxyHttpHost",
167: SILLY_USER_PROXY_HOST);
168: sync.wait(10000);
169: }
170: }
171: proxyPreferences.put("proxyHttpPort", USER_PROXY_PORT);
172: synchronized (sync) {
173: if (!USER_PROXY_PORT.equals(proxyPreferences.get(
174: "proxyHttpPort", ""))) {
175: proxyPreferences.put("proxyHttpPort", USER_PROXY_PORT);
176: sync.wait(10000);
177: }
178: }
179: }
180:
181: private void setUpAutoDirect() throws Exception {
182: synchronized (sync) {
183: if (ProxySettings.DIRECT_CONNECTION != (proxyPreferences
184: .getInt("proxyType", -1))) {
185: proxyPreferences.putInt("proxyType",
186: ProxySettings.DIRECT_CONNECTION);
187: sync.wait();
188: }
189: }
190: System.setProperty("netbeans.system_http_proxy", "DIRECT");
191: synchronized (sync) {
192: if (!"".equals(proxyPreferences.get("proxyHttpHost", ""))) {
193: proxyPreferences.put("proxyHttpHost", "");
194: sync.wait(10000);
195: }
196: }
197: synchronized (sync) {
198: if (ProxySettings.AUTO_DETECT_PROXY != (proxyPreferences
199: .getInt("proxyType", -1))) {
200: proxyPreferences.putInt("proxyType",
201: ProxySettings.AUTO_DETECT_PROXY);
202: sync.wait();
203: }
204: }
205: }
206:
207: public void testDirectConnection() throws InterruptedException {
208: System.setProperty("http.proxyHost", "");
209: System.setProperty("http.proxyPort", "");
210: synchronized (sync) {
211: if (ProxySettings.DIRECT_CONNECTION != (proxyPreferences
212: .getInt("proxyType", -1))) {
213: proxyPreferences.putInt("proxyType",
214: ProxySettings.DIRECT_CONNECTION);
215: sync.wait();
216: }
217: }
218: assertEquals("Proxy type DIRECT_CONNECTION.",
219: ProxySettings.DIRECT_CONNECTION, ProxySettings
220: .getProxyType());
221: assertEquals("No Proxy Host set.", null, System
222: .getProperty("http.proxyHost"));
223: assertEquals("No Proxy Port set.", null, System
224: .getProperty("http.proxyPort"));
225: }
226:
227: public void testAutoDetectProxy() throws InterruptedException {
228: System.setProperty("http.proxyHost", "");
229: System.setProperty("http.proxyPort", "");
230: synchronized (sync) {
231: if (ProxySettings.AUTO_DETECT_PROXY != (proxyPreferences
232: .getInt("proxyType", -1))) {
233: proxyPreferences.putInt("proxyType",
234: ProxySettings.AUTO_DETECT_PROXY);
235: sync.wait();
236: }
237: }
238: assertEquals("Proxy type AUTO_DETECT_PROXY.",
239: ProxySettings.AUTO_DETECT_PROXY, ProxySettings
240: .getProxyType());
241: assertEquals("System Proxy Host: ", SYSTEM_PROXY_HOST, System
242: .getProperty("http.proxyHost"));
243: assertEquals("System Proxy Port: ", SYSTEM_PROXY_PORT, System
244: .getProperty("http.proxyPort"));
245: }
246:
247: public void testManualSetProxy() throws InterruptedException {
248: synchronized (sync) {
249: if (ProxySettings.MANUAL_SET_PROXY != (proxyPreferences
250: .getInt("proxyType", -1))) {
251: proxyPreferences.putInt("proxyType",
252: ProxySettings.MANUAL_SET_PROXY);
253: sync.wait();
254: }
255: }
256: assertEquals("Proxy type MANUAL_SET_PROXY.",
257: ProxySettings.MANUAL_SET_PROXY, ProxySettings
258: .getProxyType());
259: assertEquals("Manual Set Proxy Host from ProxySettings: ",
260: USER_PROXY_HOST, ProxySettings.getHttpHost());
261: assertEquals("Manual Set Proxy Port from ProxySettings: ",
262: USER_PROXY_PORT, ProxySettings.getHttpPort());
263: assertEquals(
264: "Manual Set Proxy Host from System.getProperty(): ",
265: USER_PROXY_HOST, System.getProperty("http.proxyHost"));
266: assertEquals(
267: "Manual Set Proxy Port from System.getProperty(): ",
268: USER_PROXY_PORT, System.getProperty("http.proxyPort"));
269: }
270:
271: public void testHttpsManualSetProxy() throws InterruptedException {
272: synchronized (sync) {
273: if (ProxySettings.MANUAL_SET_PROXY != (proxyPreferences
274: .getInt("proxyType", -1))) {
275: proxyPreferences.putInt("proxyType",
276: ProxySettings.MANUAL_SET_PROXY);
277: sync.wait();
278: }
279: }
280: assertEquals("Proxy type MANUAL_SET_PROXY.",
281: ProxySettings.MANUAL_SET_PROXY, ProxySettings
282: .getProxyType());
283:
284: // HTTP
285: assertEquals("Manual Set Proxy Host from ProxySettings: ",
286: USER_PROXY_HOST, ProxySettings.getHttpHost());
287: assertEquals("Manual Set Proxy Port from ProxySettings: ",
288: USER_PROXY_PORT, ProxySettings.getHttpPort());
289: assertEquals(
290: "Manual Set Proxy Host from System.getProperty(): ",
291: USER_PROXY_HOST, System.getProperty("http.proxyHost"));
292: assertEquals(
293: "Manual Set Proxy Port from System.getProperty(): ",
294: USER_PROXY_PORT, System.getProperty("http.proxyPort"));
295:
296: // HTTPS
297: assertEquals(
298: "Manual Set HTTPS Proxy Host from ProxySettings: ",
299: USER_HTTPS_PROXY_HOST, ProxySettings.getHttpsHost());
300: assertEquals(
301: "Manual Set HTTPS Proxy Port from ProxySettings: ",
302: USER_HTTPS_PROXY_PORT, ProxySettings.getHttpsPort());
303: assertEquals(
304: "Manual Set HTTPS Proxy Host from System.getProperty(): ",
305: USER_HTTPS_PROXY_HOST, System
306: .getProperty("https.proxyHost"));
307: assertEquals(
308: "Manual Set HTTPS Proxy Port from System.getProperty(): ",
309: USER_HTTPS_PROXY_PORT, System
310: .getProperty("https.proxyPort"));
311: }
312:
313: public void testIfTakeUpNonProxyFromProperty() {
314: assertTrue(
315: NETBEANS_ORG + " in one of Non-Proxy hosts.",
316: ProxySettings.getNonProxyHosts().indexOf(NETBEANS_ORG) != -1);
317: }
318:
319: public void testNonProxy() throws InterruptedException {
320: assertEquals(
321: "The ProxySettings takes as same value as System properties in initial.",
322: System.getProperty("http.nonProxyHosts"), ProxySettings
323: .getNonProxyHosts());
324:
325: // change value in ProxySettings
326: synchronized (sync) {
327: if (ProxySettings.MANUAL_SET_PROXY != (proxyPreferences
328: .getInt("proxyType", -1))) {
329: proxyPreferences.putInt("proxyType",
330: ProxySettings.MANUAL_SET_PROXY);
331: sync.wait();
332: }
333: }
334: synchronized (sync) {
335: if (!MY_NON_PROXY_HOSTS.equals(proxyPreferences.get(
336: "proxyNonProxyHosts", ""))) {
337: proxyPreferences.put("proxyNonProxyHosts",
338: MY_NON_PROXY_HOSTS);
339: sync.wait();
340: }
341: }
342: assertEquals("ProxySettings returns new value.",
343: "myhost.mydomain.net", ProxySettings.getNonProxyHosts());
344: assertEquals(
345: "System property http.nonProxyHosts was changed as well.",
346: ProxySettings.getNonProxyHosts(), System
347: .getProperty("http.nonProxyHosts"));
348:
349: // switch proxy type to DIRECT_CONNECTION
350: //System.setProperty ("http.nonProxyHosts", "");
351: synchronized (sync) {
352: if (ProxySettings.DIRECT_CONNECTION != (proxyPreferences
353: .getInt("proxyType", -1))) {
354: proxyPreferences.putInt("proxyType",
355: ProxySettings.DIRECT_CONNECTION);
356: sync.wait();
357: }
358: }
359: Thread.sleep(1000); // XXX: tunning test on Windows platform, other platforms are passing
360: assertEquals(
361: "System.getProperty() doesn't return new value if DIRECT_CONNECTION set.",
362: null, System.getProperty("http.nonProxyHosts"));
363:
364: // switch proxy type back to MANUAL_SET_PROXY
365: synchronized (sync) {
366: if (ProxySettings.MANUAL_SET_PROXY != (proxyPreferences
367: .getInt("proxyType", -1))) {
368: proxyPreferences.putInt("proxyType",
369: ProxySettings.MANUAL_SET_PROXY);
370: sync.wait();
371: }
372: }
373: assertEquals("ProxySettings again returns new value.",
374: "myhost.mydomain.net", ProxySettings.getNonProxyHosts());
375: assertEquals(
376: "System property http.nonProxyHosts was changed as well.",
377: ProxySettings.getNonProxyHosts(), System
378: .getProperty("http.nonProxyHosts"));
379:
380: // switch proxy type to AUTO_DETECT_PROXY
381: synchronized (sync) {
382: if (ProxySettings.AUTO_DETECT_PROXY != (proxyPreferences
383: .getInt("proxyType", -1))) {
384: proxyPreferences.putInt("proxyType",
385: ProxySettings.AUTO_DETECT_PROXY);
386: sync.wait();
387: }
388: }
389: log("AUTO_DETECT_PROXY: ProxySettings.SystemProxySettings.getNonProxyHosts (): "
390: + ProxySettings.SystemProxySettings.getNonProxyHosts());
391: log("AUTO_DETECT_PROXY: System.getProperty (\"http.nonProxyHosts\"): "
392: + System.getProperty("http.nonProxyHosts"));
393: assertTrue(
394: "ProxySettings contains OTHER_ORG if AUTO_DETECT_PROXY set.",
395: System.getProperty("http.nonProxyHosts").indexOf(
396: OTHER_ORG) > 0);
397:
398: // switch proxy type back to MANUAL_SET_PROXY
399: synchronized (sync) {
400: if (ProxySettings.MANUAL_SET_PROXY != (proxyPreferences
401: .getInt("proxyType", -1))) {
402: proxyPreferences.putInt("proxyType",
403: ProxySettings.MANUAL_SET_PROXY);
404: sync.wait();
405: }
406: }
407: assertEquals("ProxySettings again returns new value.",
408: "myhost.mydomain.net", ProxySettings.getNonProxyHosts());
409: assertEquals(
410: "System property http.nonProxyHosts was changed as well.",
411: ProxySettings.getNonProxyHosts(), System
412: .getProperty("http.nonProxyHosts"));
413: }
414:
415: public void testAvoidDuplicateNonProxySetting()
416: throws InterruptedException {
417: synchronized (sync) {
418: if (ProxySettings.AUTO_DETECT_PROXY != (proxyPreferences
419: .getInt("proxyType", -1))) {
420: proxyPreferences.putInt("proxyType",
421: ProxySettings.AUTO_DETECT_PROXY);
422: sync.wait();
423: }
424: }
425: assertTrue(NETBEANS_ORG
426: + " is among Non-proxy hosts detect from OS.",
427: ProxySettings.SystemProxySettings.getNonProxyHosts()
428: .indexOf(NETBEANS_ORG) != -1);
429: assertFalse(NETBEANS_ORG + " is in Non-Proxy hosts only once.",
430: ProxySettings.SystemProxySettings.getNonProxyHosts()
431: .indexOf(NETBEANS_ORG) < ProxySettings
432: .getNonProxyHosts().lastIndexOf(NETBEANS_ORG));
433: assertEquals(
434: "System property http.nonProxyHosts was changed as well.",
435: ProxySettings.SystemProxySettings.getNonProxyHosts(),
436: System.getProperty("http.nonProxyHosts"));
437: }
438:
439: public void testReadNonProxySettingFromSystem()
440: throws InterruptedException {
441: synchronized (sync) {
442: if (ProxySettings.AUTO_DETECT_PROXY != (proxyPreferences
443: .getInt("proxyType", -1))) {
444: proxyPreferences.putInt("proxyType",
445: ProxySettings.AUTO_DETECT_PROXY);
446: sync.wait();
447: }
448: }
449: assertTrue(OTHER_ORG
450: + " is among Non-proxy hosts detect from OS.",
451: ProxySettings.SystemProxySettings.getNonProxyHosts()
452: .indexOf(OTHER_ORG) != -1);
453: assertEquals(
454: "System property http.nonProxyHosts was changed as well.",
455: ProxySettings.SystemProxySettings.getNonProxyHosts(),
456: System.getProperty("http.nonProxyHosts"));
457: }
458:
459: public void testSillySetManualProxy() throws Exception {
460: sillySetUp();
461: synchronized (sync) {
462: if (ProxySettings.MANUAL_SET_PROXY != (proxyPreferences
463: .getInt("proxyType", -1))) {
464: proxyPreferences.putInt("proxyType",
465: ProxySettings.MANUAL_SET_PROXY);
466: sync.wait();
467: }
468: }
469: assertEquals("Proxy type MANUAL_SET_PROXY.",
470: ProxySettings.MANUAL_SET_PROXY, ProxySettings
471: .getProxyType());
472: assertEquals(
473: "Manual Set Proxy Host from ProxySettings doesn't return SILLY_USER_PROXY_HOST anymore.",
474: USER_PROXY_HOST, ProxySettings.getHttpHost());
475: assertEquals(
476: "Manual Set Proxy Port from ProxySettings doesn't return SILLY_USER_PROXY_PORT anymore.",
477: USER_PROXY_PORT, ProxySettings.getHttpPort());
478: assertEquals(
479: "Manual Set Proxy Host from System.getProperty(): ",
480: USER_PROXY_HOST, System.getProperty("http.proxyHost"));
481: assertEquals(
482: "Manual Set Proxy Port from System.getProperty(): ",
483: USER_PROXY_PORT, System.getProperty("http.proxyPort"));
484: }
485:
486: public void testAutoDetectSillySetProxy() throws Exception {
487: sillySetUp();
488: synchronized (sync) {
489: if (ProxySettings.AUTO_DETECT_PROXY != (proxyPreferences
490: .getInt("proxyType", -1))) {
491: proxyPreferences.putInt("proxyType",
492: ProxySettings.AUTO_DETECT_PROXY);
493: sync.wait();
494: }
495: }
496: assertEquals("Proxy type AUTO_DETECT_PROXY.",
497: ProxySettings.AUTO_DETECT_PROXY, ProxySettings
498: .getProxyType());
499: assertEquals(
500: "Auto Detected Proxy Host from ProxySettings doesn't return SILLY_SYSTEM_PROXY_HOST anymore.",
501: SYSTEM_PROXY_HOST, ProxySettings.SystemProxySettings
502: .getHttpHost());
503: assertEquals(
504: "Auto Detected Proxy Port from ProxySettings doesn't return SILLY_SYSTEM_PROXY_PORT anymore.",
505: SYSTEM_PROXY_PORT, ProxySettings.SystemProxySettings
506: .getHttpPort());
507: assertEquals("System Proxy Host: ", SYSTEM_PROXY_HOST, System
508: .getProperty("http.proxyHost"));
509: assertEquals("System Proxy Port: ", SYSTEM_PROXY_PORT, System
510: .getProperty("http.proxyPort"));
511: }
512:
513: public void testSwitchAutoAndManualMode() throws Exception {
514: setUpAutoDirect();
515:
516: // ensure auto detect mode
517: assertEquals("Proxy type AUTO_DETECT_PROXY.",
518: ProxySettings.AUTO_DETECT_PROXY, ProxySettings
519: .getProxyType());
520: assertEquals("Auto Proxy Host from System.getProperty(): ",
521: null, System.getProperty("http.proxyHost"));
522: assertEquals("Auto Proxy Port from System.getProperty(): ",
523: null, System.getProperty("http.proxyPort"));
524:
525: // switch to manual
526: proxyPreferences.put("proxyHttpHost", "foo");
527: synchronized (sync) {
528: if (ProxySettings.MANUAL_SET_PROXY != (proxyPreferences
529: .getInt("proxyType", -1))) {
530: proxyPreferences.putInt("proxyType",
531: ProxySettings.MANUAL_SET_PROXY);
532: sync.wait();
533: }
534: }
535: assertEquals("Proxy type MANUAL_SET_PROXY.",
536: ProxySettings.MANUAL_SET_PROXY, ProxySettings
537: .getProxyType());
538: assertEquals("Auto Proxy Host from System.getProperty(): ",
539: "foo", System.getProperty("http.proxyHost"));
540:
541: // switch back to auto detect
542: synchronized (sync) {
543: if (ProxySettings.AUTO_DETECT_PROXY != (proxyPreferences
544: .getInt("proxyType", -1))) {
545: proxyPreferences.putInt("proxyType",
546: ProxySettings.AUTO_DETECT_PROXY);
547: sync.wait();
548: }
549: }
550: assertEquals("Proxy type AUTO_DETECT_PROXY.",
551: ProxySettings.AUTO_DETECT_PROXY, ProxySettings
552: .getProxyType());
553: assertEquals("Auto Proxy Host from System.getProperty(): ",
554: null, System.getProperty("http.proxyHost"));
555: assertEquals("Auto Proxy Port from System.getProperty(): ",
556: null, System.getProperty("http.proxyPort"));
557:
558: }
559:
560: private Object getEventQueueSync() {
561: try {
562: Field f = AbstractPreferences.class
563: .getDeclaredField("eventQueue");
564: f.setAccessible(true);
565: return f.get(null);
566:
567: } catch (Exception ex) {
568: Logger.getLogger("global")
569: .log(java.util.logging.Level.SEVERE,
570: ex.getMessage(), ex);
571: }
572: return null;
573: }
574: }
|