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