test: ensure address is resolvable in CertUtilsTests#testSubjectAlternativeNames

We check for an expected length but this is only valid if the address can be resolved and on some systems
127.0.0.1 may not map to a name.

Original commit: elastic/x-pack-elasticsearch@2f7c8da242
This commit is contained in:
jaymode 2016-05-20 08:10:55 -04:00
parent 8dd7be3f07
commit 71b78579a1
1 changed files with 10 additions and 1 deletions

View File

@ -7,7 +7,9 @@ package org.elasticsearch.shield.ssl;
import org.bouncycastle.asn1.x509.GeneralName;
import org.bouncycastle.asn1.x509.GeneralNames;
import org.elasticsearch.common.SuppressForbidden;
import org.elasticsearch.common.network.InetAddresses;
import org.elasticsearch.common.network.NetworkAddress;
import org.elasticsearch.test.ESTestCase;
import java.io.InputStream;
@ -113,7 +115,8 @@ public class CertUtilsTests extends ESTestCase {
GeneralName[] generalNameArray = generalNames.getNames();
assertThat(generalNameArray, notNullValue());
if (resolveName) {
logger.info("resolve name [{}], address [{}], subject alt names [{}]", resolveName, NetworkAddress.format(address), generalNames);
if (resolveName && isResolvable(address)) {
assertThat(generalNameArray.length, is(2));
int firstType = generalNameArray[0].getTagNo();
if (firstType == GeneralName.iPAddress) {
@ -129,6 +132,12 @@ public class CertUtilsTests extends ESTestCase {
}
}
@SuppressForbidden(reason = "need to use getHostName to resolve DNS name and getHostAddress to ensure we resolved the name")
private boolean isResolvable(InetAddress inetAddress) {
String hostname = inetAddress.getHostName();
return hostname.equals(inetAddress.getHostAddress()) == false;
}
public void testIsAnyLocalAddress() throws Exception {
InetAddress address = mock(InetAddress.class);
when(address.isAnyLocalAddress()).thenReturn(true);