Changeset 15469 in josm for trunk/test/unit/org/openstreetmap
- Timestamp:
- 2019-10-22T23:32:51+02:00 (5 years ago)
- Location:
- trunk/test/unit/org/openstreetmap/josm
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/io/remotecontrol/RemoteControlTest.java
r14217 r15469 11 11 import java.net.URL; 12 12 import java.nio.charset.StandardCharsets; 13 import java.nio.file.Files;14 import java.nio.file.Paths;15 13 import java.security.GeneralSecurityException; 16 14 import java.security.KeyStore.TrustedCertificateEntry; 17 import java.security.SecureRandom;18 import java.security.cert.X509Certificate;19 20 import javax.net.ssl.HostnameVerifier;21 import javax.net.ssl.HttpsURLConnection;22 import javax.net.ssl.SSLContext;23 import javax.net.ssl.TrustManager;24 import javax.net.ssl.X509TrustManager;25 15 26 16 import org.junit.After; … … 31 21 import org.openstreetmap.josm.spi.preferences.Config; 32 22 import org.openstreetmap.josm.testutils.JOSMTestRules; 33 import org.openstreetmap.josm.tools.Logging;34 23 import org.openstreetmap.josm.tools.PlatformHookWindows; 35 24 import org.openstreetmap.josm.tools.PlatformManager; … … 45 34 46 35 private String httpBase; 47 private String httpsBase;48 36 49 37 private static class PlatformHookWindowsMock extends MockUp<PlatformHookWindows> { … … 67 55 @Before 68 56 public void setUp() throws GeneralSecurityException { 69 RemoteControl.PROP_REMOTECONTROL_HTTPS_ENABLED.put(true);70 deleteKeystore();71 72 57 if (PlatformManager.isPlatformWindows() && "True".equals(System.getenv("APPVEYOR"))) { 73 58 // appveyor doesn't like us tinkering with the root keystore, so mock this out … … 77 62 78 63 RemoteControl.start(); 79 disableCertificateValidation();80 64 httpBase = "http://127.0.0.1:"+Config.getPref().getInt("remote.control.port", 8111); 81 httpsBase = "https://127.0.0.1:"+Config.getPref().getInt("remote.control.https.port", 8112);82 }83 84 /**85 * Deletes JOSM keystore, if it exists.86 */87 public static void deleteKeystore() {88 try {89 Files.deleteIfExists(Paths.get(90 RemoteControl.getRemoteControlDir()).resolve(RemoteControlHttpsServer.KEYSTORE_FILENAME));91 } catch (IOException e) {92 Logging.error(e);93 }94 }95 96 /**97 * Disable all HTTPS validation mechanisms as described98 * <a href="http://stackoverflow.com/a/2893932/2257172">here</a> and99 * <a href="http://stackoverflow.com/a/19542614/2257172">here</a>100 * @throws GeneralSecurityException if a security error occurs101 */102 public void disableCertificateValidation() throws GeneralSecurityException {103 // Create a trust manager that does not validate certificate chains104 TrustManager[] trustAllCerts = new TrustManager[] {105 new X509TrustManager() {106 @Override107 @SuppressFBWarnings(value = "WEAK_TRUST_MANAGER")108 public X509Certificate[] getAcceptedIssuers() {109 return new X509Certificate[0];110 }111 112 @Override113 @SuppressFBWarnings(value = "WEAK_TRUST_MANAGER")114 public void checkClientTrusted(X509Certificate[] certs, String authType) {115 }116 117 @Override118 @SuppressFBWarnings(value = "WEAK_TRUST_MANAGER")119 public void checkServerTrusted(X509Certificate[] certs, String authType) {120 }121 }122 };123 124 // Install the all-trusting trust manager125 SSLContext sc = SSLContext.getInstance("TLS");126 sc.init(null, trustAllCerts, new SecureRandom());127 HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());128 129 // Create all-trusting host name verifier130 HostnameVerifier allHostsValid = (hostname, session) -> true;131 132 // Install the all-trusting host verifier133 HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);134 65 } 135 66 … … 149 80 public void testHttpListOfCommands() throws Exception { 150 81 testListOfCommands(httpBase); 151 }152 153 /**154 * Tests that sending an HTTPS request without command results in HTTP 400, with all available commands in error message.155 * @throws Exception if an error occurs156 */157 @Test158 public void testHttpsListOfCommands() throws Exception {159 testListOfCommands(httpsBase);160 82 } 161 83 -
trunk/test/unit/org/openstreetmap/josm/tools/PlatformHookOsxTest.java
r14190 r15469 38 38 public void testStartupHook() { 39 39 hook.startupHook((a, b, c, d) -> System.out.println("callback")); 40 }41 42 /**43 * Test method for {@code PlatformHookOsx#setupHttpsCertificate}44 * @throws Exception if an error occurs45 */46 @Test47 public void testSetupHttpsCertificate() throws Exception {48 assertFalse(hook.setupHttpsCertificate(null, null));49 40 } 50 41 -
trunk/test/unit/org/openstreetmap/josm/tools/PlatformHookWindowsTest.java
r14412 r15469 7 7 import static org.junit.Assert.assertNull; 8 8 import static org.junit.Assert.assertTrue; 9 import static org.junit.Assume.assumeFalse;10 9 import static org.junit.Assert.fail; 11 10 … … 13 12 import java.io.File; 14 13 import java.io.IOException; 15 import java.security.KeyStore;16 import java.security.KeyStore.TrustedCertificateEntry;17 14 import java.security.KeyStoreException; 18 15 import java.util.Collection; … … 22 19 import org.openstreetmap.josm.JOSMFixture; 23 20 import org.openstreetmap.josm.TestUtils; 24 import org.openstreetmap.josm.io.remotecontrol.RemoteControlHttpsServer;25 import org.openstreetmap.josm.io.remotecontrol.RemoteControlTest;26 21 import org.openstreetmap.josm.spi.preferences.Config; 27 22 … … 64 59 try { 65 60 PlatformHookWindows.getRootKeystore(); 66 fail("Expected KeyStoreException");67 } catch (KeyStoreException e) {68 Logging.info(e.getMessage());69 }70 }71 }72 73 /**74 * Test method for {@code PlatformHookWindows#removeInsecureCertificates}75 * @throws Exception if an error occurs76 */77 @Test78 public void testRemoveInsecureCertificates() throws Exception {79 if (PlatformManager.isPlatformWindows()) {80 PlatformHookWindows.removeInsecureCertificates();81 } else {82 try {83 PlatformHookWindows.removeInsecureCertificates();84 fail("Expected KeyStoreException");85 } catch (KeyStoreException e) {86 Logging.info(e.getMessage());87 }88 }89 }90 91 /**92 * Test method for {@code PlatformHookWindows#setupHttpsCertificate}93 * @throws Exception if an error occurs94 */95 @Test96 public void testSetupHttpsCertificate() throws Exception {97 // appveyor doesn't like us tinkering with the root keystore98 assumeFalse(PlatformManager.isPlatformWindows() && "True".equals(System.getenv("APPVEYOR")));99 100 RemoteControlTest.deleteKeystore();101 KeyStore ks = RemoteControlHttpsServer.loadJosmKeystore();102 TrustedCertificateEntry trustedCert = new KeyStore.TrustedCertificateEntry(ks.getCertificate(ks.aliases().nextElement()));103 if (PlatformManager.isPlatformWindows()) {104 hook.setupHttpsCertificate(RemoteControlHttpsServer.ENTRY_ALIAS, trustedCert);105 } else {106 try {107 hook.setupHttpsCertificate(RemoteControlHttpsServer.ENTRY_ALIAS, trustedCert);108 61 fail("Expected KeyStoreException"); 109 62 } catch (KeyStoreException e) {
Note:
See TracChangeset
for help on using the changeset viewer.