1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.remotecontrol;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 |
|
---|
6 | import java.io.BufferedReader;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStream;
|
---|
9 | import java.io.InputStreamReader;
|
---|
10 | import java.net.HttpURLConnection;
|
---|
11 | import java.net.URL;
|
---|
12 | import java.nio.charset.StandardCharsets;
|
---|
13 | import java.security.GeneralSecurityException;
|
---|
14 | import java.security.KeyStore.TrustedCertificateEntry;
|
---|
15 |
|
---|
16 | import org.junit.After;
|
---|
17 | import org.junit.Before;
|
---|
18 | import org.junit.Rule;
|
---|
19 | import org.junit.Test;
|
---|
20 | import org.openstreetmap.josm.TestUtils;
|
---|
21 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
22 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
23 | import org.openstreetmap.josm.tools.PlatformHookWindows;
|
---|
24 | import org.openstreetmap.josm.tools.PlatformManager;
|
---|
25 |
|
---|
26 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
27 | import mockit.Mock;
|
---|
28 | import mockit.MockUp;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Unit tests for Remote Control
|
---|
32 | */
|
---|
33 | public class RemoteControlTest {
|
---|
34 |
|
---|
35 | private String httpBase;
|
---|
36 |
|
---|
37 | private static class PlatformHookWindowsMock extends MockUp<PlatformHookWindows> {
|
---|
38 | @Mock
|
---|
39 | public boolean setupHttpsCertificate(String entryAlias, TrustedCertificateEntry trustedCert) {
|
---|
40 | return true;
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Setup test.
|
---|
46 | */
|
---|
47 | @Rule
|
---|
48 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
49 | public JOSMTestRules test = new JOSMTestRules().preferences().https().assertionsInEDT();
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Starts Remote control before testing requests.
|
---|
53 | * @throws GeneralSecurityException if a security error occurs
|
---|
54 | */
|
---|
55 | @Before
|
---|
56 | public void setUp() throws GeneralSecurityException {
|
---|
57 | if (PlatformManager.isPlatformWindows() && "True".equals(System.getenv("APPVEYOR"))) {
|
---|
58 | // appveyor doesn't like us tinkering with the root keystore, so mock this out
|
---|
59 | TestUtils.assumeWorkingJMockit();
|
---|
60 | new PlatformHookWindowsMock();
|
---|
61 | }
|
---|
62 |
|
---|
63 | RemoteControl.start();
|
---|
64 | httpBase = "http://127.0.0.1:"+Config.getPref().getInt("remote.control.port", 8111);
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Stops Remote control after testing requests.
|
---|
69 | */
|
---|
70 | @After
|
---|
71 | public void tearDown() {
|
---|
72 | RemoteControl.stop();
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Tests that sending an HTTP request without command results in HTTP 400, with all available commands in error message.
|
---|
77 | * @throws Exception if an error occurs
|
---|
78 | */
|
---|
79 | @Test
|
---|
80 | public void testHttpListOfCommands() throws Exception {
|
---|
81 | testListOfCommands(httpBase);
|
---|
82 | }
|
---|
83 |
|
---|
84 | private void testListOfCommands(String url) throws IOException, ReflectiveOperationException {
|
---|
85 | HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
|
---|
86 | connection.connect();
|
---|
87 | assertEquals(connection.getResponseCode(), HttpURLConnection.HTTP_BAD_REQUEST);
|
---|
88 | try (InputStream is = connection.getErrorStream()) {
|
---|
89 | // TODO this code should be refactored somewhere in Utils as it is used in several JOSM classes
|
---|
90 | StringBuilder responseBody = new StringBuilder();
|
---|
91 | try (BufferedReader in = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
|
---|
92 | String s;
|
---|
93 | while ((s = in.readLine()) != null) {
|
---|
94 | responseBody.append(s);
|
---|
95 | responseBody.append("\n");
|
---|
96 | }
|
---|
97 | }
|
---|
98 | assert responseBody.toString().contains(RequestProcessor.getUsageAsHtml());
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|