2015-11-25 03:13:59 -05:00
|
|
|
import org.elasticsearch.gradle.LoggedExec
|
2015-12-04 02:36:35 -05:00
|
|
|
import org.elasticsearch.gradle.MavenFilteringHack
|
2016-08-30 12:57:42 -04:00
|
|
|
import org.elasticsearch.gradle.test.NodeInfo
|
|
|
|
|
|
|
|
import javax.net.ssl.HttpsURLConnection
|
|
|
|
import javax.net.ssl.KeyManagerFactory
|
|
|
|
import javax.net.ssl.SSLContext
|
|
|
|
import javax.net.ssl.TrustManagerFactory
|
|
|
|
import java.nio.charset.StandardCharsets
|
|
|
|
import java.security.KeyStore
|
|
|
|
import java.security.SecureRandom
|
2015-11-25 03:13:59 -05:00
|
|
|
|
2017-01-04 14:27:53 -05:00
|
|
|
apply plugin: 'elasticsearch.standalone-rest-test'
|
2015-11-24 23:19:04 -05:00
|
|
|
apply plugin: 'elasticsearch.rest-test'
|
|
|
|
|
|
|
|
dependencies {
|
2018-01-27 00:48:30 -05:00
|
|
|
testCompile project(path: xpackModule('core'), configuration: 'runtime')
|
2015-11-24 23:19:04 -05:00
|
|
|
}
|
|
|
|
|
2016-04-12 09:19:07 -04:00
|
|
|
String outputDir = "generated-resources/${project.name}"
|
|
|
|
task copyXPackPluginProps(type: Copy) {
|
2018-01-27 00:48:30 -05:00
|
|
|
from project(xpackModule('core')).file('src/main/plugin-metadata')
|
|
|
|
from project(xpackModule('core')).tasks.pluginProperties
|
2016-04-12 09:19:07 -04:00
|
|
|
into outputDir
|
|
|
|
}
|
|
|
|
project.sourceSets.test.output.dir(outputDir, builtBy: copyXPackPluginProps)
|
|
|
|
|
2016-02-08 08:33:27 -05:00
|
|
|
// needed to be consistent with ssl host checking
|
2016-03-14 20:22:05 -04:00
|
|
|
Object san = new SanEvaluator()
|
2015-11-24 23:19:04 -05:00
|
|
|
|
2016-02-08 08:33:27 -05:00
|
|
|
// location of generated keystores and certificates
|
|
|
|
File keystoreDir = new File(project.buildDir, 'keystore')
|
|
|
|
|
|
|
|
// Generate the node's keystore
|
|
|
|
File nodeKeystore = new File(keystoreDir, 'test-node.jks')
|
|
|
|
task createNodeKeyStore(type: LoggedExec) {
|
|
|
|
doFirst {
|
|
|
|
if (nodeKeystore.parentFile.exists() == false) {
|
|
|
|
nodeKeystore.parentFile.mkdirs()
|
|
|
|
}
|
|
|
|
if (nodeKeystore.exists()) {
|
|
|
|
delete nodeKeystore
|
|
|
|
}
|
|
|
|
}
|
2018-01-16 13:45:42 -05:00
|
|
|
executable = new File(project.runtimeJavaHome, 'bin/keytool')
|
2016-02-08 08:33:27 -05:00
|
|
|
standardInput = new ByteArrayInputStream('FirstName LastName\nUnit\nOrganization\nCity\nState\nNL\nyes\n\n'.getBytes('UTF-8'))
|
|
|
|
args '-genkey',
|
|
|
|
'-alias', 'test-node',
|
|
|
|
'-keystore', nodeKeystore,
|
|
|
|
'-keyalg', 'RSA',
|
|
|
|
'-keysize', '2048',
|
|
|
|
'-validity', '712',
|
|
|
|
'-dname', 'CN=smoke-test-plugins-ssl',
|
|
|
|
'-keypass', 'keypass',
|
|
|
|
'-storepass', 'keypass',
|
|
|
|
'-ext', san
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the client's keystore
|
|
|
|
File clientKeyStore = new File(keystoreDir, 'test-client.jks')
|
|
|
|
task createClientKeyStore(type: LoggedExec) {
|
2015-11-25 03:13:59 -05:00
|
|
|
doFirst {
|
2016-02-08 08:33:27 -05:00
|
|
|
if (clientKeyStore.parentFile.exists() == false) {
|
|
|
|
clientKeyStore.parentFile.mkdirs()
|
|
|
|
}
|
|
|
|
if (clientKeyStore.exists()) {
|
|
|
|
delete clientKeyStore
|
|
|
|
}
|
2015-11-24 23:19:04 -05:00
|
|
|
}
|
2018-01-16 13:45:42 -05:00
|
|
|
executable = new File(project.runtimeJavaHome, 'bin/keytool')
|
2015-11-25 03:13:59 -05:00
|
|
|
standardInput = new ByteArrayInputStream('FirstName LastName\nUnit\nOrganization\nCity\nState\nNL\nyes\n\n'.getBytes('UTF-8'))
|
|
|
|
args '-genkey',
|
2016-02-08 08:33:27 -05:00
|
|
|
'-alias', 'test-client',
|
|
|
|
'-keystore', clientKeyStore,
|
|
|
|
'-keyalg', 'RSA',
|
|
|
|
'-keysize', '2048',
|
|
|
|
'-validity', '712',
|
|
|
|
'-dname', 'CN=smoke-test-plugins-ssl',
|
|
|
|
'-keypass', 'keypass',
|
|
|
|
'-storepass', 'keypass',
|
|
|
|
'-ext', san
|
2015-11-24 23:19:04 -05:00
|
|
|
}
|
|
|
|
|
2016-02-08 08:33:27 -05:00
|
|
|
// Export the node's certificate
|
|
|
|
File nodeCertificate = new File(keystoreDir, 'test-node.cert')
|
|
|
|
task exportNodeCertificate(type: LoggedExec) {
|
2018-01-20 00:30:17 -05:00
|
|
|
dependsOn createNodeKeyStore
|
2016-02-08 08:33:27 -05:00
|
|
|
doFirst {
|
|
|
|
if (nodeCertificate.parentFile.exists() == false) {
|
|
|
|
nodeCertificate.parentFile.mkdirs()
|
|
|
|
}
|
|
|
|
if (nodeCertificate.exists()) {
|
|
|
|
delete nodeCertificate
|
|
|
|
}
|
|
|
|
}
|
2018-01-16 13:45:42 -05:00
|
|
|
executable = new File(project.runtimeJavaHome, 'bin/keytool')
|
2016-02-08 08:33:27 -05:00
|
|
|
args '-export',
|
|
|
|
'-alias', 'test-node',
|
|
|
|
'-keystore', nodeKeystore,
|
|
|
|
'-storepass', 'keypass',
|
|
|
|
'-file', nodeCertificate
|
|
|
|
}
|
|
|
|
|
|
|
|
// Import the node certificate in the client's keystore
|
|
|
|
task importNodeCertificateInClientKeyStore(type: LoggedExec) {
|
|
|
|
dependsOn exportNodeCertificate
|
2018-01-16 13:45:42 -05:00
|
|
|
executable = new File(project.runtimeJavaHome, 'bin/keytool')
|
2016-02-08 08:33:27 -05:00
|
|
|
args '-import',
|
|
|
|
'-alias', 'test-node',
|
|
|
|
'-keystore', clientKeyStore,
|
|
|
|
'-storepass', 'keypass',
|
|
|
|
'-file', nodeCertificate,
|
|
|
|
'-noprompt'
|
|
|
|
}
|
|
|
|
|
|
|
|
// Export the client's certificate
|
|
|
|
File clientCertificate = new File(keystoreDir, 'test-client.cert')
|
|
|
|
task exportClientCertificate(type: LoggedExec) {
|
2018-01-20 00:30:17 -05:00
|
|
|
dependsOn createClientKeyStore
|
2016-02-08 08:33:27 -05:00
|
|
|
doFirst {
|
|
|
|
if (clientCertificate.parentFile.exists() == false) {
|
|
|
|
clientCertificate.parentFile.mkdirs()
|
|
|
|
}
|
|
|
|
if (clientCertificate.exists()) {
|
|
|
|
delete clientCertificate
|
|
|
|
}
|
|
|
|
}
|
2018-01-16 13:45:42 -05:00
|
|
|
executable = new File(project.runtimeJavaHome, 'bin/keytool')
|
2016-02-08 08:33:27 -05:00
|
|
|
args '-export',
|
|
|
|
'-alias', 'test-client',
|
|
|
|
'-keystore', clientKeyStore,
|
|
|
|
'-storepass', 'keypass',
|
|
|
|
'-file', clientCertificate
|
|
|
|
}
|
|
|
|
|
|
|
|
// Import the client certificate in the node's keystore
|
|
|
|
task importClientCertificateInNodeKeyStore(type: LoggedExec) {
|
|
|
|
dependsOn exportClientCertificate
|
2018-01-16 13:45:42 -05:00
|
|
|
executable = new File(project.runtimeJavaHome, 'bin/keytool')
|
2016-02-08 08:33:27 -05:00
|
|
|
args '-import',
|
|
|
|
'-alias', 'test-client',
|
|
|
|
'-keystore', nodeKeystore,
|
|
|
|
'-storepass', 'keypass',
|
|
|
|
'-file', clientCertificate,
|
|
|
|
'-noprompt'
|
|
|
|
}
|
|
|
|
|
|
|
|
forbiddenPatterns {
|
|
|
|
exclude '**/*.cert'
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add keystores to test classpath: it expects it there
|
|
|
|
sourceSets.test.resources.srcDir(keystoreDir)
|
|
|
|
processTestResources.dependsOn(
|
|
|
|
createNodeKeyStore, createClientKeyStore,
|
|
|
|
importNodeCertificateInClientKeyStore, importClientCertificateInNodeKeyStore
|
|
|
|
)
|
2015-11-25 03:13:59 -05:00
|
|
|
|
2018-01-20 00:30:17 -05:00
|
|
|
integTestCluster.dependsOn(importClientCertificateInNodeKeyStore, importNodeCertificateInClientKeyStore)
|
2017-09-14 14:18:54 -04:00
|
|
|
|
2018-01-20 00:30:17 -05:00
|
|
|
|
|
|
|
ext.pluginsCount = 0
|
2018-01-27 00:48:30 -05:00
|
|
|
project(xpackProject('plugin').path).subprojects { Project p ->
|
2018-01-20 00:30:17 -05:00
|
|
|
// the meta plugin contains the individual xpack plugins
|
|
|
|
if (p.extensions.findByName('esplugin') != null) {
|
|
|
|
pluginsCount += 1
|
|
|
|
}
|
|
|
|
}
|
2015-12-04 02:36:35 -05:00
|
|
|
project.rootProject.subprojects.findAll { it.path.startsWith(':plugins:') }.each { subproj ->
|
|
|
|
// need to get a non-decorated project object, so must re-lookup the project by path
|
2017-02-22 03:56:52 -05:00
|
|
|
integTestCluster.plugin(subproj.path)
|
2016-02-21 13:10:21 -05:00
|
|
|
pluginsCount += 1
|
2015-11-25 03:13:59 -05:00
|
|
|
}
|
2015-11-24 23:19:04 -05:00
|
|
|
|
2017-02-22 03:56:52 -05:00
|
|
|
integTestCluster {
|
2018-02-23 10:33:34 -05:00
|
|
|
setting 'xpack.monitoring.collection.interval', '1s'
|
2017-02-22 03:56:52 -05:00
|
|
|
setting 'xpack.monitoring.exporters._http.type', 'http'
|
|
|
|
setting 'xpack.monitoring.exporters._http.enabled', 'false'
|
|
|
|
setting 'xpack.monitoring.exporters._http.ssl.truststore.path', clientKeyStore.name
|
|
|
|
setting 'xpack.monitoring.exporters._http.ssl.truststore.password', 'keypass'
|
|
|
|
setting 'xpack.monitoring.exporters._http.auth.username', 'monitoring_agent'
|
2017-06-29 16:27:57 -04:00
|
|
|
setting 'xpack.monitoring.exporters._http.auth.password', 'x-pack-test-password'
|
2017-12-05 06:07:04 -05:00
|
|
|
setting 'xpack.monitoring.exporters._http.ssl.verification_mode', 'full'
|
2017-02-22 03:56:52 -05:00
|
|
|
|
2018-03-21 23:09:44 -04:00
|
|
|
setting 'xpack.security.enabled', 'true'
|
2017-02-22 03:56:52 -05:00
|
|
|
setting 'xpack.security.http.ssl.enabled', 'true'
|
|
|
|
setting 'xpack.security.http.ssl.keystore.path', nodeKeystore.name
|
2017-06-27 13:15:12 -04:00
|
|
|
keystoreSetting 'xpack.security.http.ssl.keystore.secure_password', 'keypass'
|
2017-02-22 03:56:52 -05:00
|
|
|
|
2017-03-02 12:01:05 -05:00
|
|
|
setting 'xpack.ml.enabled', 'false'
|
|
|
|
|
2018-01-27 00:48:30 -05:00
|
|
|
plugin xpackProject('plugin').path
|
2017-02-22 03:56:52 -05:00
|
|
|
|
|
|
|
// copy keystores into config/
|
|
|
|
extraConfigFile nodeKeystore.name, nodeKeystore
|
|
|
|
extraConfigFile clientKeyStore.name, clientKeyStore
|
|
|
|
|
|
|
|
setupCommand 'setupTestUser',
|
2017-06-29 16:27:57 -04:00
|
|
|
'bin/x-pack/users', 'useradd', 'test_user', '-p', 'x-pack-test-password', '-r', 'superuser'
|
2017-02-22 03:56:52 -05:00
|
|
|
setupCommand 'setupMonitoringUser',
|
2017-06-29 16:27:57 -04:00
|
|
|
'bin/x-pack/users', 'useradd', 'monitoring_agent', '-p', 'x-pack-test-password', '-r', 'remote_monitoring_agent'
|
2017-02-22 03:56:52 -05:00
|
|
|
|
|
|
|
waitCondition = { NodeInfo node, AntBuilder ant ->
|
|
|
|
File tmpFile = new File(node.cwd, 'wait.success')
|
|
|
|
KeyStore keyStore = KeyStore.getInstance("JKS");
|
|
|
|
keyStore.load(clientKeyStore.newInputStream(), 'keypass'.toCharArray());
|
|
|
|
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
|
|
|
|
kmf.init(keyStore, 'keypass'.toCharArray());
|
|
|
|
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
|
|
|
|
tmf.init(keyStore);
|
|
|
|
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
|
|
|
|
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom());
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
// we use custom wait logic here for HTTPS
|
|
|
|
HttpsURLConnection httpURLConnection = null;
|
|
|
|
try {
|
2017-03-15 13:23:26 -04:00
|
|
|
httpURLConnection = (HttpsURLConnection) new URL("https://${node.httpUri()}/_cluster/health?wait_for_nodes=${numNodes}&wait_for_status=yellow").openConnection();
|
2017-02-22 03:56:52 -05:00
|
|
|
httpURLConnection.setSSLSocketFactory(sslContext.getSocketFactory());
|
|
|
|
httpURLConnection.setRequestProperty("Authorization", "Basic " +
|
2017-06-29 16:27:57 -04:00
|
|
|
Base64.getEncoder().encodeToString("test_user:x-pack-test-password".getBytes(StandardCharsets.UTF_8)));
|
2017-02-22 03:56:52 -05:00
|
|
|
httpURLConnection.setRequestMethod("GET");
|
|
|
|
httpURLConnection.connect();
|
|
|
|
if (httpURLConnection.getResponseCode() == 200) {
|
|
|
|
tmpFile.withWriter StandardCharsets.UTF_8.name(), {
|
|
|
|
it.write(httpURLConnection.getInputStream().getText(StandardCharsets.UTF_8.name()))
|
2016-08-30 12:57:42 -04:00
|
|
|
}
|
2016-02-08 08:33:27 -05:00
|
|
|
}
|
2017-02-22 03:56:52 -05:00
|
|
|
} catch (IOException e) {
|
|
|
|
if (i == 9) {
|
|
|
|
logger.error("final attempt of calling cluster health failed", e)
|
|
|
|
} else {
|
|
|
|
logger.debug("failed to call cluster health", e)
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (httpURLConnection != null) {
|
|
|
|
httpURLConnection.disconnect();
|
|
|
|
}
|
2016-02-08 08:33:27 -05:00
|
|
|
}
|
2017-02-22 03:56:52 -05:00
|
|
|
|
|
|
|
// did not start, so wait a bit before trying again
|
|
|
|
Thread.sleep(500L);
|
2015-11-24 23:19:04 -05:00
|
|
|
}
|
2017-02-22 03:56:52 -05:00
|
|
|
return tmpFile.exists()
|
2015-11-24 23:19:04 -05:00
|
|
|
}
|
2015-11-28 21:07:37 -05:00
|
|
|
}
|
2015-12-04 02:36:35 -05:00
|
|
|
|
|
|
|
ext.expansions = [
|
|
|
|
'expected.plugins.count': pluginsCount
|
|
|
|
]
|
|
|
|
|
|
|
|
processTestResources {
|
|
|
|
from(sourceSets.test.resources.srcDirs) {
|
2017-05-17 21:07:42 -04:00
|
|
|
include '**/*.yml'
|
2015-12-04 02:36:35 -05:00
|
|
|
inputs.properties(expansions)
|
|
|
|
MavenFilteringHack.filter(it, expansions)
|
|
|
|
}
|
|
|
|
}
|
2016-02-23 20:12:09 -05:00
|
|
|
|
2016-03-14 20:22:05 -04:00
|
|
|
/** A lazy evaluator to find the san to use for certificate generation. */
|
|
|
|
class SanEvaluator {
|
2016-02-23 20:12:09 -05:00
|
|
|
|
2016-03-14 20:22:05 -04:00
|
|
|
private static String san = null
|
2016-02-23 20:12:09 -05:00
|
|
|
|
2016-03-14 20:22:05 -04:00
|
|
|
String toString() {
|
|
|
|
synchronized (SanEvaluator.class) {
|
|
|
|
if (san == null) {
|
|
|
|
san = getSubjectAlternativeNameString()
|
|
|
|
}
|
2016-02-23 20:12:09 -05:00
|
|
|
}
|
2016-03-14 20:22:05 -04:00
|
|
|
return san
|
2016-02-23 20:12:09 -05:00
|
|
|
}
|
|
|
|
|
2016-03-14 20:22:05 -04:00
|
|
|
// Code stolen from NetworkUtils/InetAddresses/NetworkAddress to support SAN
|
|
|
|
/** Return all interfaces (and subinterfaces) on the system */
|
|
|
|
private static List<NetworkInterface> getInterfaces() throws SocketException {
|
|
|
|
List<NetworkInterface> all = new ArrayList<>();
|
|
|
|
addAllInterfaces(all, Collections.list(NetworkInterface.getNetworkInterfaces()));
|
|
|
|
Collections.sort(all, new Comparator<NetworkInterface>() {
|
|
|
|
@Override
|
|
|
|
public int compare(NetworkInterface left, NetworkInterface right) {
|
|
|
|
return Integer.compare(left.getIndex(), right.getIndex());
|
2016-02-23 20:12:09 -05:00
|
|
|
}
|
2016-03-14 20:22:05 -04:00
|
|
|
});
|
|
|
|
return all;
|
2016-02-23 20:12:09 -05:00
|
|
|
}
|
2016-03-14 20:22:05 -04:00
|
|
|
|
|
|
|
/** Helper for getInterfaces, recursively adds subinterfaces to {@code target} */
|
|
|
|
private static void addAllInterfaces(List<NetworkInterface> target, List<NetworkInterface> level) {
|
|
|
|
if (!level.isEmpty()) {
|
|
|
|
target.addAll(level);
|
|
|
|
for (NetworkInterface intf : level) {
|
|
|
|
addAllInterfaces(target, Collections.list(intf.getSubInterfaces()));
|
|
|
|
}
|
|
|
|
}
|
2016-02-23 20:12:09 -05:00
|
|
|
}
|
|
|
|
|
2016-03-14 20:22:05 -04:00
|
|
|
private static String getSubjectAlternativeNameString() {
|
|
|
|
List<InetAddress> list = new ArrayList<>();
|
|
|
|
for (NetworkInterface intf : getInterfaces()) {
|
|
|
|
if (intf.isUp()) {
|
|
|
|
// NOTE: some operating systems (e.g. BSD stack) assign a link local address to the loopback interface
|
|
|
|
// while technically not a loopback address, some of these treat them as one (e.g. OS X "localhost") so we must too,
|
|
|
|
// otherwise things just won't work out of box. So we include all addresses from loopback interfaces.
|
|
|
|
for (InetAddress address : Collections.list(intf.getInetAddresses())) {
|
|
|
|
if (intf.isLoopback() || address.isLoopbackAddress()) {
|
|
|
|
list.add(address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-23 20:12:09 -05:00
|
|
|
}
|
2016-03-14 20:22:05 -04:00
|
|
|
if (list.isEmpty()) {
|
|
|
|
throw new IllegalArgumentException("no up-and-running loopback addresses found, got " + getInterfaces());
|
2016-02-23 20:12:09 -05:00
|
|
|
}
|
|
|
|
|
2016-03-14 20:22:05 -04:00
|
|
|
StringBuilder builder = new StringBuilder("san=");
|
|
|
|
for (int i = 0; i < list.size(); i++) {
|
|
|
|
InetAddress address = list.get(i);
|
|
|
|
String hostAddress;
|
|
|
|
if (address instanceof Inet6Address) {
|
|
|
|
hostAddress = compressedIPV6Address((Inet6Address)address);
|
|
|
|
} else {
|
|
|
|
hostAddress = address.getHostAddress();
|
|
|
|
}
|
|
|
|
builder.append("ip:").append(hostAddress);
|
|
|
|
String hostname = address.getHostName();
|
|
|
|
if (hostname.equals(address.getHostAddress()) == false) {
|
|
|
|
builder.append(",dns:").append(hostname);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i != (list.size() - 1)) {
|
|
|
|
builder.append(",");
|
|
|
|
}
|
2016-02-23 20:12:09 -05:00
|
|
|
}
|
|
|
|
|
2016-03-14 20:22:05 -04:00
|
|
|
return builder.toString();
|
|
|
|
}
|
2016-02-23 20:12:09 -05:00
|
|
|
|
2016-03-14 20:22:05 -04:00
|
|
|
private static String compressedIPV6Address(Inet6Address inet6Address) {
|
|
|
|
byte[] bytes = inet6Address.getAddress();
|
|
|
|
int[] hextets = new int[8];
|
|
|
|
for (int i = 0; i < hextets.length; i++) {
|
|
|
|
hextets[i] = (bytes[2 * i] & 255) << 8 | bytes[2 * i + 1] & 255;
|
|
|
|
}
|
|
|
|
compressLongestRunOfZeroes(hextets);
|
|
|
|
return hextetsToIPv6String(hextets);
|
2016-02-23 20:12:09 -05:00
|
|
|
}
|
|
|
|
|
2016-03-14 20:22:05 -04:00
|
|
|
/**
|
|
|
|
* Identify and mark the longest run of zeroes in an IPv6 address.
|
|
|
|
*
|
|
|
|
* <p>Only runs of two or more hextets are considered. In case of a tie, the
|
|
|
|
* leftmost run wins. If a qualifying run is found, its hextets are replaced
|
|
|
|
* by the sentinel value -1.
|
|
|
|
*
|
|
|
|
* @param hextets {@code int[]} mutable array of eight 16-bit hextets
|
|
|
|
*/
|
|
|
|
private static void compressLongestRunOfZeroes(int[] hextets) {
|
|
|
|
int bestRunStart = -1;
|
|
|
|
int bestRunLength = -1;
|
|
|
|
int runStart = -1;
|
|
|
|
for (int i = 0; i < hextets.length + 1; i++) {
|
|
|
|
if (i < hextets.length && hextets[i] == 0) {
|
|
|
|
if (runStart < 0) {
|
|
|
|
runStart = i;
|
|
|
|
}
|
|
|
|
} else if (runStart >= 0) {
|
|
|
|
int runLength = i - runStart;
|
|
|
|
if (runLength > bestRunLength) {
|
|
|
|
bestRunStart = runStart;
|
|
|
|
bestRunLength = runLength;
|
|
|
|
}
|
|
|
|
runStart = -1;
|
2016-02-23 20:12:09 -05:00
|
|
|
}
|
2016-03-14 20:22:05 -04:00
|
|
|
}
|
|
|
|
if (bestRunLength >= 2) {
|
|
|
|
Arrays.fill(hextets, bestRunStart, bestRunStart + bestRunLength, -1);
|
2016-02-23 20:12:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 20:22:05 -04:00
|
|
|
/**
|
|
|
|
* Convert a list of hextets into a human-readable IPv6 address.
|
|
|
|
*
|
|
|
|
* <p>In order for "::" compression to work, the input should contain negative
|
|
|
|
* sentinel values in place of the elided zeroes.
|
|
|
|
*
|
|
|
|
* @param hextets {@code int[]} array of eight 16-bit hextets, or -1s
|
2016-02-23 20:12:09 -05:00
|
|
|
*/
|
2016-03-14 20:22:05 -04:00
|
|
|
private static String hextetsToIPv6String(int[] hextets) {
|
|
|
|
/*
|
|
|
|
* While scanning the array, handle these state transitions:
|
|
|
|
* start->num => "num" start->gap => "::"
|
|
|
|
* num->num => ":num" num->gap => "::"
|
|
|
|
* gap->num => "num" gap->gap => ""
|
|
|
|
*/
|
|
|
|
StringBuilder buf = new StringBuilder(39);
|
|
|
|
boolean lastWasNumber = false;
|
|
|
|
for (int i = 0; i < hextets.length; i++) {
|
|
|
|
boolean thisIsNumber = hextets[i] >= 0;
|
|
|
|
if (thisIsNumber) {
|
|
|
|
if (lastWasNumber) {
|
|
|
|
buf.append(':');
|
|
|
|
}
|
|
|
|
buf.append(Integer.toHexString(hextets[i]));
|
|
|
|
} else {
|
|
|
|
if (i == 0 || lastWasNumber) {
|
|
|
|
buf.append("::");
|
|
|
|
}
|
2016-02-23 20:12:09 -05:00
|
|
|
}
|
2016-03-14 20:22:05 -04:00
|
|
|
lastWasNumber = thisIsNumber;
|
2016-02-23 20:12:09 -05:00
|
|
|
}
|
2016-03-14 20:22:05 -04:00
|
|
|
return buf.toString();
|
2016-02-23 20:12:09 -05:00
|
|
|
}
|
|
|
|
}
|