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

Last change on this file since 7337 was 7337, checked in by Don-vip, 10 years ago

see #10230, see #10033 - SAN tweaks + fix unit test (for real?)

File size: 5.5 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 public void checkClientTrusted(X509Certificate[] certs, String authType) {
75 }
76 public void checkServerTrusted(X509Certificate[] certs, String authType) {
77 }
78 }
79 };
80
81 // Install the all-trusting trust manager
82 try {
83 SSLContext sc = SSLContext.getInstance("TLS");
84 sc.init(null, trustAllCerts, new SecureRandom());
85 HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
86 } catch (GeneralSecurityException e) {
87 fail(e.getMessage());
88 }
89
90 // Create all-trusting host name verifier
91 HostnameVerifier allHostsValid = new HostnameVerifier() {
92 @Override
93 public boolean verify(String hostname, SSLSession session) {
94 return true;
95 }
96 };
97
98 // Install the all-trusting host verifier
99 HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
100 }
101
102 /**
103 * Stops Remote control after testing requests.
104 */
105 @After
106 public void tearDown() {
107 RemoteControl.stop();
108 }
109
110 /**
111 * Tests that sending an HTTP request without command results in HTTP 400, with all available commands in error message.
112 * @throws IOException if an I/O error occurs
113 * @throws MalformedURLException if HTTP URL is invalid
114 */
115 @Test
116 public void testHttpListOfCommands() throws MalformedURLException, IOException {
117 testListOfCommands(httpBase);
118 }
119
120 /**
121 * Tests that sending an HTTPS request without command results in HTTP 400, with all available commands in error message.
122 * @throws IOException if an I/O error occurs
123 * @throws MalformedURLException if HTTPS URL is invalid
124 */
125 @Test
126 public void testHttpsListOfCommands() throws MalformedURLException, IOException {
127 testListOfCommands(httpsBase);
128 }
129
130 private void testListOfCommands(String url) throws MalformedURLException, IOException {
131 HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
132 connection.connect();
133 assertEquals(connection.getResponseCode(), HttpURLConnection.HTTP_BAD_REQUEST);
134 try (InputStream is = connection.getErrorStream()) {
135 // TODO this code should be refactored somewhere in Utils as it is used in several JOSM classes
136 StringBuilder responseBody = new StringBuilder();
137 try (BufferedReader in = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
138 String s;
139 while((s = in.readLine()) != null) {
140 responseBody.append(s);
141 responseBody.append("\n");
142 }
143 }
144 assert responseBody.toString().contains(RequestProcessor.getUsageAsHtml());
145 } catch (IllegalAccessException e) {
146 fail(e.getMessage());
147 } catch (InstantiationException e) {
148 fail(e.getMessage());
149 }
150 }
151}
Note: See TracBrowser for help on using the repository browser.