// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.


package org.openqa.selenium.environment;

import org.openqa.selenium.environment.webserver.AppServer;

import com.google.common.base.Preconditions;

public class DomainHelper {

  private AppServer appServer;

  public DomainHelper(AppServer appServer) {
    this.appServer = appServer;
  }

  public String getUrlForFirstValidHostname(String path) {
    Preconditions.checkArgument(
      isValidHostname(appServer.getHostName()),
      "Expected valid hostname but was %s",
      appServer.getHostName());
    return appServer.whereIs(path);
  }

  public String getSecureUrlForFirstValidHostname(String path) {
    Preconditions.checkArgument(
        isValidHostname(appServer.getHostName()),
        "Expected valid hostname but was %s",
        appServer.getHostName());
    return appServer.whereIsSecure(path);
  }

  public String getUrlForSecondValidHostname(String path) {
    Preconditions.checkArgument(
      isValidHostname(appServer.getAlternateHostName()),
      "Expected valid hostname but was %s",
      appServer.getAlternateHostName());
    return appServer.whereElseIs(path);
  }

  public boolean checkIsOnValidHostname() {
    boolean correct = getHostName() != null && isValidHostname(getHostName());
    if (!correct) {
      System.out.println("Skipping test: unable to find domain name to use, hostname: " + getHostName());
    }
    return correct;
  }

  public boolean checkIsOnValidSubDomain() {
    boolean correct = getHostName() != null && isValidSubDomain(getHostName());

    if (!correct) {
      System.out.println("Skipping test: unable to find sub domain name to use, hostname: " + getHostName());
    }
    return correct;
  }

  public boolean checkHasValidAlternateHostname() {
    String hostname = appServer.getAlternateHostName();
    boolean correct = getHostName() != null && isValidHostname(hostname);
    if (!correct) {
      System.out.println(
          "Skipping test: unable to find alternate domain name to use, hostname: " + hostname);
    }
    return correct;
  }

  private boolean isValidSubDomain(String hostname) {
    /*
    * /etc/hosts needs to have e.g.
    *
    * 127.0.0.1       sub.selenium.tests
    *
    */
    return hostname.split("\\.").length >= 3;
  }

  private boolean isIpv4Address(String string) {
    return string.matches("\\d{1,3}(?:\\.\\d{1,3}){3}");
  }

  public boolean isValidHostname(String hostname) {
    return !isIpv4Address(hostname) && !"localhost".equals(hostname);
  }

  public String getHostName() {
    return appServer.getHostName();
  }
}

@@ -945,16 +945,60 @@
 ditions;
+%0Aimport com.google.common.net.InetAddresses;
 %0A%0Apublic
@@ -3071,38 +3071,37 @@
 h %3E= 3;%0A  %7D%0A%0A  p
-rivate
+ublic
  boolean isIpv4A
@@ -3099,169 +3099,307 @@
 n is
-Ipv4Address(String string) %7B%0A    return string.matches(%22%5C%5Cd%7B1,3%7D(?:%5C%5C.%5C%5Cd%7B1,3%7D)%7B3%7D%22);%0A  %7D%0A%0A  public boolean isValidHostname(String hostname) %7B%0A    return !isIpv4
+ValidHostname(String hostname) %7B%0A    // Strip the IPv6 zone index, if present. For example, %22fe80::1%25eth0%22 becomes %22fe80::1%22.%0A    int zoneIndexStart = hostname.indexOf('%25');%0A    if (zoneIndexStart %3E= 0) %7B%0A      hostname = hostname.substring(0, zoneIndexStart);%0A    %7D%0A    return !InetAddresses.isInet
 Addr

Check for IPv6 literals in isValidHostname.