blob: 509fa5ea2d61d0200ceb03d52fedbe78bcd0d270 (
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
|
/*******************************************************************************
* Copyright (c) 2010, 2016 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.ua.tests.help.webapp;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import org.eclipse.help.internal.base.BaseHelpSystem;
import org.eclipse.help.internal.server.WebappManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* Test to see if the help server binds to host 127.0.0.1 in Workbench mode
*/
public class HelpServerBinding {
private int previousMode;
// Tests to access the server using it's IP need to be disabled
// for testing in the build because a firewall can block this access
// To enable these tests for local testing set testUsingIP to true.
private final boolean testUsingIP = false;
@Before
public void setUp() throws Exception {
previousMode = BaseHelpSystem.getMode();
}
@After
public void tearDown() throws Exception {
BaseHelpSystem.setMode(previousMode);
}
private String getHostIP() throws UnknownHostException {
InetAddress host = InetAddress.getLocalHost();
byte[] ipAddr = host.getAddress();
String result = "" + ipAddr[0];
for (int i = 1; i < ipAddr.length; i++) {
result += '.';
result += ipAddr[i];
}
return result;
}
@Test
public void testInfocenterBinding() throws Exception {
BaseHelpSystem.setMode(BaseHelpSystem.MODE_INFOCENTER);
WebappManager.stop("help");
WebappManager.start("help");
assertTrue(canAccessServer("127.0.0.1"));
if (testUsingIP) {
assertTrue(canAccessServer(getHostIP()));
}
}
@Test
public void testWorkbenchBinding() throws Exception {
BaseHelpSystem.setMode(BaseHelpSystem.MODE_WORKBENCH);
WebappManager.stop("help");
WebappManager.start("help");
assertTrue(canAccessServer("127.0.0.1"));
if (testUsingIP) {
assertFalse(canAccessServer(getHostIP()));
}
}
@Test
public void testStandaloneBinding() throws Exception {
BaseHelpSystem.setMode(BaseHelpSystem.MODE_STANDALONE);
WebappManager.stop("help");
WebappManager.start("help");
assertTrue(canAccessServer("127.0.0.1"));
if (testUsingIP) {
assertTrue(canAccessServer(getHostIP()));
}
}
private boolean canAccessServer(String host) throws Exception {
try {
int port = WebappManager.getPort();
URL url = new URL("http", host, port, "/help/index.jsp");
URLConnection connection = url.openConnection();
setTimeout(connection, 5000);
try (InputStream input = connection.getInputStream()) {
int firstbyte = input.read();
return firstbyte > 0;
}
} catch (Exception e) {
return false;
}
}
private static void setTimeout(URLConnection conn, int milliseconds) {
conn.setConnectTimeout(milliseconds);
conn.setReadTimeout(milliseconds);
}
}
|