Changeset: 16b3708cd841 for monetdb-java
URL: https://dev.monetdb.org/hg/monetdb-java/rev/16b3708cd841
Modified Files:
        src/test/java/org/monetdb/jdbc/TLSTests.java
        src/test/java/org/monetdb/testinfra/Config.java
Branch: default
Log Message:

Do not hardcode localhost.localdomain in TLSTests


diffs (107 lines):

diff --git a/src/test/java/org/monetdb/jdbc/TLSTests.java 
b/src/test/java/org/monetdb/jdbc/TLSTests.java
--- a/src/test/java/org/monetdb/jdbc/TLSTests.java
+++ b/src/test/java/org/monetdb/jdbc/TLSTests.java
@@ -225,8 +225,7 @@ public class TLSTests {
        @Test
        public void test_refuse_wrong_host() throws IOException, SQLException, 
ValidationError {
                String host = Config.getTesterHost();
-               Assumptions.assumeTrue(host.equals("localhost"));
-               String altHost = "localhost.localdomain";
+               String altHost = Config.getTesterAltHost();
                target("server1");
                parms.setHost(altHost);
                setFile(Parameter.CERT, "/ca1.crt");
diff --git a/src/test/java/org/monetdb/testinfra/Config.java 
b/src/test/java/org/monetdb/testinfra/Config.java
--- a/src/test/java/org/monetdb/testinfra/Config.java
+++ b/src/test/java/org/monetdb/testinfra/Config.java
@@ -13,8 +13,10 @@ package org.monetdb.testinfra;
 
 import java.io.IOException;
 import java.io.Reader;
+import java.net.InetAddress;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.net.UnknownHostException;
 import java.nio.file.Files;
 import java.nio.file.InvalidPathException;
 import java.nio.file.Path;
@@ -49,6 +51,9 @@ public class Config {
        public static final String TLSTESTER_PROPERTY = "test.tlstester";
        public static final String TLSTESTER_ENVVAR = "MONETDB_TLSTESTER";
 
+       public static final String TLSTESTER_ALTHOST_PROPERTY = 
"test.tlstester.althost";
+       public static final String TLSTESTER_ALTHOST_ENVVAR = 
"MONETDB_TLSTESTER_ALTHOST";
+
        public static final String TLSTEST_KNOWNGOOD_PROPERTY = 
"test.tls.goodurl";
        public static final String TLSTEST_KNOWNGOOD_ENVVAR = 
"MONETDB_TLS_GOODURL";
        public static final String TLSTEST_KNOWNGOOD_DEFAULT = 
"jdbc:monetdbs://monetdb.ergates.nl/demo";
@@ -63,6 +68,7 @@ public class Config {
        // only access these while synchronized on cachedConfigFile:
        private static final Properties cachedConfigFile = new Properties();
        private static boolean configLoaded = false;
+       private static String cachedAltHost = null;
 
        private static String lookup(String propName, String envName, String 
defaultValue) {
                String value;
@@ -171,6 +177,61 @@ public class Config {
                return host != null ? host : "localhost";
        }
 
+       public static String getTesterAltHost() {
+               String altHost = lookup(TLSTESTER_ALTHOST_PROPERTY, 
TLSTESTER_ALTHOST_ENVVAR, null);
+               if (altHost != null && !altHost.isEmpty())
+                       return altHost;
+
+               synchronized (cachedConfigFile) {
+                       if (cachedAltHost == null) {
+                               cachedAltHost = tryPickAltHost(getTesterHost());
+                       }
+               }
+               if (cachedAltHost == null)
+                       fail("Could not guess alt tls tester alt host, please 
set " + TLSTESTER_ALTHOST_PROPERTY + " or $" + TLSTESTER_ALTHOST_ENVVAR + " 
directly");
+               return cachedAltHost;
+       }
+
+       private static String tryPickAltHost(String testerHost) {
+               String[] candidates = new String[] {
+                               "localhost.localdomain",
+                               "localhost4",
+                               "localhost6",
+                               "ip4-localhost",
+                               "ip6-localhost",
+                               "localhost.local",
+                               "alias.localhost",
+                               "127.0.0.1",
+               };
+
+               InetAddress[] testerHostAddresses;
+               try {
+                       testerHostAddresses = 
InetAddress.getAllByName(testerHost);
+               } catch (UnknownHostException e) {
+                       fail("Unable to resolve tester host: " + testerHost + 
": " + e.getMessage());
+                       // unreachable
+                       testerHostAddresses = null;
+               }
+
+               for (String candidate : candidates) {
+                       try {
+                               InetAddress[] addrs = 
InetAddress.getAllByName(candidate);
+                               for (InetAddress addr : addrs) {
+                                       for (InetAddress ta : 
testerHostAddresses) {
+                                               if (addr.equals(ta)) {
+                                                       // found an alias
+                                                       return candidate;
+                                               }
+                                       }
+                               }
+                       } catch (UnknownHostException e) {
+                               continue;
+                       }
+               }
+
+               return null;
+       }
+
        private static String addDefaultCredentials(String rawURL) {
                try {
                        assertTrue(rawURL.startsWith("jdbc:"));
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to