source: josm/trunk/test/unit/org/openstreetmap/josm/io/remotecontrol/RemoteControlTest.java@ 8540

Last change on this file since 8540 was 8510, checked in by Don-vip, 9 years ago

checkstyle: enable relevant whitespace checks and fix them

  • Property svn:eol-style set to native
File size: 5.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.remotecontrol;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.fail;
6
7import java.io.BufferedReader;
8import java.io.IOException;
9import java.io.InputStream;
10import java.io.InputStreamReader;
11import java.net.HttpURLConnection;
12import java.net.MalformedURLException;
13import java.net.URL;
14import java.nio.charset.StandardCharsets;
15import java.nio.file.Files;
16import java.nio.file.Paths;
17import java.security.GeneralSecurityException;
18import java.security.SecureRandom;
19import java.security.cert.X509Certificate;
20
21import javax.net.ssl.HostnameVerifier;
22import javax.net.ssl.HttpsURLConnection;
23import javax.net.ssl.SSLContext;
24import javax.net.ssl.SSLSession;
25import javax.net.ssl.TrustManager;
26import javax.net.ssl.X509TrustManager;
27
28import org.junit.After;
29import org.junit.Before;
30import org.junit.Test;
31import org.openstreetmap.josm.JOSMFixture;
32import org.openstreetmap.josm.Main;
33
34/**
35 * Unit tests for Remote Control
36 */
37public class RemoteControlTest {
38
39 private String httpBase;
40 private String httpsBase;
41
42 /**
43 * Starts Remote control before testing requests.
44 */
45 @Before
46 public void setUp() {
47 JOSMFixture.createUnitTestFixture().init();
48 RemoteControl.PROP_REMOTECONTROL_HTTPS_ENABLED.put(true);
49 try {
50 Files.deleteIfExists(Paths.get(
51 RemoteControl.getRemoteControlDir()).resolve(RemoteControlHttpsServer.KEYSTORE_FILENAME));
52 } catch (IOException e) {
53 Main.error(e);
54 }
55
56 RemoteControl.start();
57 disableCertificateValidation();
58 httpBase = "http://127.0.0.1:"+Main.pref.getInteger("remote.control.port", 8111);
59 httpsBase = "https://127.0.0.1:"+Main.pref.getInteger("remote.control.https.port", 8112);
60 }
61
62 /**
63 * Disable all HTTPS validation mechanisms as described
64 * <a href="http://stackoverflow.com/a/2893932/2257172">here</a> and
65 * <a href="http://stackoverflow.com/a/19542614/2257172">here</a>
66 */
67 public void disableCertificateValidation() {
68 // Create a trust manager that does not validate certificate chains
69 TrustManager[] trustAllCerts = new TrustManager[] {
70 new X509TrustManager() {
71 public X509Certificate[] getAcceptedIssuers() {
72 return null;
73 }
74
75 public void checkClientTrusted(X509Certificate[] certs, String authType) {
76 }
77
78 public void checkServerTrusted(X509Certificate[] certs, String authType) {
79 }
80 }
81 };
82
83 // Install the all-trusting trust manager
84 try {
85 SSLContext sc = SSLContext.getInstance("TLS");
86 sc.init(null, trustAllCerts, new SecureRandom());
87 HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
88 } catch (GeneralSecurityException e) {
89 fail(e.getMessage());
90 }
91
92 // Create all-trusting host name verifier
93 HostnameVerifier allHostsValid = new HostnameVerifier() {
94 @Override
95 public boolean verify(String hostname, SSLSession session) {
96 return true;
97 }
98 };
99
100 // Install the all-trusting host verifier
101 HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
102 }
103
104 /**
105 * Stops Remote control after testing requests.
106 */
107 @After
108 public void tearDown() {
109 RemoteControl.stop();
110 }
111
112 /**
113 * Tests that sending an HTTP request without command results in HTTP 400, with all available commands in error message.
114 * @throws IOException if an I/O error occurs
115 * @throws MalformedURLException if HTTP URL is invalid
116 */
117 @Test
118 public void testHttpListOfCommands() throws MalformedURLException, IOException {
119 testListOfCommands(httpBase);
120 }
121
122 /**
123 * Tests that sending an HTTPS request without command results in HTTP 400, with all available commands in error message.
124 * @throws IOException if an I/O error occurs
125 * @throws MalformedURLException if HTTPS URL is invalid
126 */
127 @Test
128 public void testHttpsListOfCommands() throws MalformedURLException, IOException {
129 testListOfCommands(httpsBase);
130 }
131
132 private void testListOfCommands(String url) throws MalformedURLException, IOException {
133 HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
134 connection.connect();
135 assertEquals(connection.getResponseCode(), HttpURLConnection.HTTP_BAD_REQUEST);
136 try (InputStream is = connection.getErrorStream()) {
137 // TODO this code should be refactored somewhere in Utils as it is used in several JOSM classes
138 StringBuilder responseBody = new StringBuilder();
139 try (BufferedReader in = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
140 String s;
141 while ((s = in.readLine()) != null) {
142 responseBody.append(s);
143 responseBody.append("\n");
144 }
145 }
146 assert responseBody.toString().contains(RequestProcessor.getUsageAsHtml());
147 } catch (IllegalAccessException e) {
148 fail(e.getMessage());
149 } catch (InstantiationException e) {
150 fail(e.getMessage());
151 }
152 }
153}
Note: See TracBrowser for help on using the repository browser.