Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cc659671d6893ee70a65ea68e41468f76803b467 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*******************************************************************************
 * Copyright (c) 2008, 2010 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.core.tests.net;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.eclipse.core.internal.net.ProxyData;
import org.eclipse.core.internal.net.ProxySelector;
import org.eclipse.core.net.proxy.IProxyData;
import org.eclipse.core.net.proxy.IProxyService;

public class SystemProxyTest extends TestCase {

	private boolean isProxiesDefault;
	private boolean isSystemProxiesDefault;

	private Map proxyDataMap = new HashMap();

	public SystemProxyTest() {
		super();
	}

	public SystemProxyTest(String name) {
		super(name);
	}

	public static Test suite() {
		return new TestSuite(SystemProxyTest.class);
	}

	protected void setUp() throws Exception {
		super.setUp();
		isSystemProxiesDefault = isSystemProxiesEnabled();
		setSystemProxiesEnabled(true);
		isProxiesDefault = isProxiesEnabled();
		setProxiesEnabled(true);
	}

	protected void tearDown() throws Exception {
		super.tearDown();
		setProxiesEnabled(isProxiesDefault);
		setSystemProxiesEnabled(isSystemProxiesDefault);
		IProxyData[] data = getProxyManager().getProxyData();
		for (int i = 0; i < data.length; i++) {
			IProxyData proxyData = data[i];
			proxyData.disable();
		}
		getProxyManager().setProxyData(data);
	}

	private IProxyService getProxyManager() {
		return Activator.getDefault().getProxyService();
	}

	private boolean isProxiesEnabled() {
		return this.getProxyManager().isProxiesEnabled();
	}

	private boolean isSystemProxiesEnabled() {
		return this.getProxyManager().isProxiesEnabled();
	}

	private void setProxiesEnabled(boolean enabled) {
		this.getProxyManager().setProxiesEnabled(enabled);
		if (enabled && this.getProxyManager().isSystemProxiesEnabled()
				&& !this.getProxyManager().hasSystemProxies()) {
			assertEquals(false, this.getProxyManager().isProxiesEnabled());
		} else {
			assertEquals(enabled, this.getProxyManager().isProxiesEnabled());
		}
	}

	private void setSystemProxiesEnabled(boolean enabled) {
		this.getProxyManager().setSystemProxiesEnabled(enabled);
		assertEquals(enabled, this.getProxyManager().isSystemProxiesEnabled());
	}

	private void assertProxyDataEqual(IProxyData actual, IProxyData expected) {
		ProxyData data = (ProxyData) actual;
		ProxyData expectedData = (ProxyData) expected;
		assertTrue(expectedData.getType() == data.getType());
		assertEquals(expectedData.getHost(), data.getHost());
		assertEquals(expectedData.getPort(), data.getPort());
		assertEquals(expectedData.getUserId(), data.getUserId());
		assertEquals(expectedData.getPassword(), data.getPassword());
		assertEquals(expectedData.isRequiresAuthentication(), data
				.isRequiresAuthentication());
		assertEquals(expectedData.getSource(), data.getSource());
	}

	private void checkGetProxyDataForHost() {
		checkGetProxyDataForHost("http://www.something.org");
		checkGetProxyDataForHost("https://www.something.org");
		checkGetProxyDataForHost("socks://www.something.org");
		checkGetProxyDataForHost("www.something.org");
	}

	private void checkGetProxyDataForHost(String host) {
		IProxyData[] proxiesData = getProxyManager().getProxyDataForHost(host);
		assertNotNull(proxiesData);

		Map typeMap = new HashMap();
		for (int i = 0; i < proxiesData.length; i++) {
			assertProxyDataEqual(proxiesData[i], (IProxyData) proxyDataMap
					.get(proxiesData[i].getType()));
			typeMap.put(proxiesData[i].getType(), proxiesData[i].getType());
		}

		assertEquals(3, typeMap.size());
	}

	private void checkProxySelector() {
		IProxyData[] proxiesData = ProxySelector.getProxyData("Native");
		assertNotNull(proxiesData);

		Map typeMap = new HashMap();
		for (int i = 0; i < proxiesData.length; i++) {
			assertProxyDataEqual(proxiesData[i], (IProxyData) proxyDataMap
					.get(proxiesData[i].getType()));
			typeMap.put(proxiesData[i].getType(), proxiesData[i].getType());
		}

		assertEquals(3, typeMap.size());
	}

	/**
	 * This test needs system env set. See {@link #initializeTestProxyData()}
	 * for values.
	 */
	public void testGetProxyDataForHost_LinuxEnvSettings() {
		initializeTestProxyData("LINUX_ENV");
		checkGetProxyDataForHost();
	}

	/**
	 * This test needs system env set. See {@link #initializeTestProxyData()}
	 * for values.
	 */
	public void testProxySelector_LinuxEnvSettings() {
		initializeTestProxyData("LINUX_ENV");
		checkProxySelector();
	}

	/**
	 * This test needs Gnome settings set. See
	 * {@link #initializeTestProxyData()} for values.
	 */
	public void testGetProxyDataForHost_LinuxGnomeSettings() {
		initializeTestProxyData("LINUX_GNOME");
		checkGetProxyDataForHost();
	}

	/**
	 * This test needs Gnome settings set. See
	 * {@link #initializeTestProxyData()} for values.
	 */
	public void testProxySelector_LinuxGnomeSettings() {
		initializeTestProxyData("LINUX_GNOME");
		checkProxySelector();
	}

	/**
	 * This test needs Windows IE settings manually set. See
	 * {@link #initializeTestProxyData()} for values.
	 */
	public void testGetProxyDataForHost_WindowsIEManualSettings() {
		initializeTestProxyData("WINDOWS_IE");
		checkGetProxyDataForHost();
	}

	/**
	 * This test needs Windows IE settings manually set. See
	 * {@link #initializeTestProxyData()} for values.
	 */
	public void testProxySelector_WindowsIEManualSettings() {
		initializeTestProxyData("WINDOWS_IE");
		checkProxySelector();
	}

	/**
	 * This test needs Windows IE settings manually set. See
	 * {@link #initializeTestProxyData()} for values. Additionally set
	 * <code>"eclipse.*;nonexisting.com;*.eclipse.org;www.*.com;*.test.*"</code>
	 * as proxy bypass in the IE settings.
	 *
	 * @throws URISyntaxException
	 */
	public void testNonProxiedHosts_WindowsIEManualSettings()
			throws URISyntaxException {
		IProxyData[] proxiesData = getProxyManager().select(
				new URI("http://eclipse"));
		assertEquals(1, proxiesData.length);

		proxiesData = getProxyManager().select(
				new URI("http://eclipse.org/bugs"));
		assertEquals(0, proxiesData.length);

		proxiesData = getProxyManager().select(
				new URI("http://nonexisting.com"));
		assertEquals(0, proxiesData.length);

		proxiesData = getProxyManager().select(
				new URI("http://www.eclipse.org"));
		assertEquals(0, proxiesData.length);

		proxiesData = getProxyManager().select(
				new URI("http://www.myDomain.com"));
		assertEquals(0, proxiesData.length);

		proxiesData = getProxyManager().select(
				new URI("http://www.test.edu"));
		assertEquals(0, proxiesData.length);
	}

	private void initializeTestProxyData(String proxyDataSource) {
		proxyDataMap.put(IProxyData.HTTP_PROXY_TYPE, new ProxyData(
				IProxyData.HTTP_PROXY_TYPE, "127.0.0.1", 8081, false,
				proxyDataSource));
		proxyDataMap.put(IProxyData.HTTPS_PROXY_TYPE, new ProxyData(
				IProxyData.HTTPS_PROXY_TYPE, "127.0.0.2", 8082, false,
				proxyDataSource));
		proxyDataMap.put(IProxyData.SOCKS_PROXY_TYPE, new ProxyData(
				IProxyData.SOCKS_PROXY_TYPE, "127.0.0.3", 8083, false,
				proxyDataSource));
	}
}

Back to the top