8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor

Reviewed-by: chegar, alanb
This commit is contained in:
Mark Sheppard 2016-04-11 03:00:50 +01:00
parent 25b68378f6
commit 8906a1381d
64 changed files with 836 additions and 1819 deletions
jdk
src
java.base/share/classes
jdk.naming.dns/share/classes
test
ProblemList.txt
java/net
javax/net/ssl
sun

@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -30,8 +30,10 @@ import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.Objects;
import java.util.ServiceLoader;
import java.util.Scanner;
import java.security.AccessController;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.ObjectStreamException;
import java.io.ObjectStreamField;
import java.io.IOException;
@ -49,7 +51,6 @@ import jdk.internal.misc.SharedSecrets;
import sun.security.action.*;
import sun.net.InetAddressCachePolicy;
import sun.net.util.IPAddressUtil;
import sun.net.spi.nameservice.*;
/**
* This class represents an Internet Protocol (IP) address.
@ -207,6 +208,7 @@ class InetAddress implements java.io.Serializable {
/* Specify address family preference */
static transient boolean preferIPv6Address = false;
static class InetAddressHolder {
/**
* Reserve the original application specified hostname.
@ -279,7 +281,7 @@ class InetAddress implements java.io.Serializable {
}
/* Used to store the name service provider */
private static List<NameService> nameServices = null;
private static transient NameService nameService = null;
/* Used to store the best available hostname */
private transient String canonicalHostName = null;
@ -623,7 +625,6 @@ class InetAddress implements java.io.Serializable {
*/
private static String getHostFromNameService(InetAddress addr, boolean check) {
String host = null;
for (NameService nameService : nameServices) {
try {
// first lookup the hostname
host = nameService.getHostByAddr(addr.getAddress());
@ -657,18 +658,12 @@ class InetAddress implements java.io.Serializable {
host = addr.getHostAddress();
return host;
}
break;
} catch (SecurityException e) {
host = addr.getHostAddress();
break;
} catch (UnknownHostException e) {
host = addr.getHostAddress();
// let next provider resolve the hostname
}
}
return host;
}
@ -860,88 +855,287 @@ class InetAddress implements java.io.Serializable {
}
}
static InetAddressImpl impl;
/**
* NameService provides host and address lookup service
*
* @since 9
*/
private interface NameService {
private static NameService createNSProvider(String provider) {
if (provider == null)
return null;
/**
* Lookup a host mapping by name. Retrieve the IP addresses
* associated with a host
*
* @param host the specified hostname
* @return array of IP addresses for the requested host
* @throws UnknownHostException
* if no IP address for the {@code host} could be found
*/
InetAddress[] lookupAllHostAddr(String host)
throws UnknownHostException;
/**
* Lookup the host corresponding to the IP address provided
*
* @param addr byte array representing an IP address
* @return {@code String} representing the host name mapping
* @throws UnknownHostException
* if no host found for the specified IP address
*/
String getHostByAddr(byte[] addr) throws UnknownHostException;
}
/**
* The default NameService implementation, which delegates to the underlying
* OS network libraries to resolve host address mappings.
*
* @since 9
*/
private static final class PlatformNameService implements NameService {
NameService nameService = null;
if (provider.equals("default")) {
// initialize the default name service
nameService = new NameService() {
public InetAddress[] lookupAllHostAddr(String host)
throws UnknownHostException {
return impl.lookupAllHostAddr(host);
}
public String getHostByAddr(byte[] addr)
throws UnknownHostException {
return impl.getHostByAddr(addr);
}
};
} else {
final String providerName = provider;
try {
nameService = java.security.AccessController.doPrivileged(
new java.security.PrivilegedExceptionAction<>() {
public NameService run() {
Iterator<NameServiceDescriptor> itr =
ServiceLoader.load(NameServiceDescriptor.class)
.iterator();
while (itr.hasNext()) {
NameServiceDescriptor nsd = itr.next();
if (providerName.
equalsIgnoreCase(nsd.getType()+","
+nsd.getProviderName())) {
try {
return nsd.createNameService();
} catch (Exception e) {
e.printStackTrace();
System.err.println(
"Cannot create name service:"
+providerName+": " + e);
}
}
}
return null;
}
}
);
} catch (java.security.PrivilegedActionException e) {
}
public String getHostByAddr(byte[] addr) throws UnknownHostException {
return impl.getHostByAddr(addr);
}
return nameService;
}
/**
* The HostsFileNameService provides host address mapping
* by reading the entries in a hosts file, which is specified by
* {@code jdk.net.hosts.file} system property
*
* <p>The file format is that which corresponds with the /etc/hosts file
* IP Address host alias list.
*
* <p>When the file lookup is enabled it replaces the default NameService
* implementation
*
* @since 9
*/
private static final class HostsFileNameService implements NameService {
private final String hostsFile;
public HostsFileNameService (String hostsFileName) {
this.hostsFile = hostsFileName;
}
private String addrToString(byte addr[]) {
String stringifiedAddress = null;
if (addr.length == Inet4Address.INADDRSZ) {
stringifiedAddress = Inet4Address.numericToTextFormat(addr);
} else { // treat as an IPV6 jobby
byte[] newAddr
= IPAddressUtil.convertFromIPv4MappedAddress(addr);
if (newAddr != null) {
stringifiedAddress = Inet4Address.numericToTextFormat(addr);
} else {
stringifiedAddress = Inet6Address.numericToTextFormat(addr);
}
}
return stringifiedAddress;
}
/**
* Lookup the host name corresponding to the IP address provided.
* Search the configured host file a host name corresponding to
* the specified IP address.
*
* @param addr byte array representing an IP address
* @return {@code String} representing the host name mapping
* @throws UnknownHostException
* if no host found for the specified IP address
*/
@Override
public String getHostByAddr(byte[] addr) throws UnknownHostException {
String hostEntry;
String host = null;
String addrString = addrToString(addr);
try (Scanner hostsFileScanner = new Scanner(new File(hostsFile), "UTF-8")) {
while (hostsFileScanner.hasNextLine()) {
hostEntry = hostsFileScanner.nextLine();
if (!hostEntry.startsWith("#")) {
hostEntry = removeComments(hostEntry);
if (hostEntry.contains(addrString)) {
host = extractHost(hostEntry, addrString);
if (host != null) {
break;
}
}
}
}
} catch (FileNotFoundException e) {
throw new UnknownHostException("Unable to resolve address "
+ addrString + " as hosts file " + hostsFile
+ " not found ");
}
if ((host == null) || (host.equals("")) || (host.equals(" "))) {
throw new UnknownHostException("Requested address "
+ addrString
+ " resolves to an invalid entry in hosts file "
+ hostsFile);
}
return host;
}
/**
* <p>Lookup a host mapping by name. Retrieve the IP addresses
* associated with a host.
*
* <p>Search the configured hosts file for the addresses assocaited with
* with the specified host name.
*
* @param host the specified hostname
* @return array of IP addresses for the requested host
* @throws UnknownHostException
* if no IP address for the {@code host} could be found
*/
public InetAddress[] lookupAllHostAddr(String host)
throws UnknownHostException {
String hostEntry;
String addrStr = null;
InetAddress[] res = null;
byte addr[] = new byte[4];
ArrayList<InetAddress> inetAddresses = null;
// lookup the file and create a list InetAddress for the specfied host
try (Scanner hostsFileScanner = new Scanner(new File(hostsFile), "UTF-8")) {
while (hostsFileScanner.hasNextLine()) {
hostEntry = hostsFileScanner.nextLine();
if (!hostEntry.startsWith("#")) {
hostEntry = removeComments(hostEntry);
if (hostEntry.contains(host)) {
addrStr = extractHostAddr(hostEntry, host);
if ((addrStr != null) && (!addrStr.equals(""))) {
addr = createAddressByteArray(addrStr);
if (inetAddresses == null) {
inetAddresses = new ArrayList<>(1);
}
if (addr != null) {
inetAddresses.add(InetAddress.getByAddress(host, addr));
}
}
}
}
}
} catch (FileNotFoundException e) {
throw new UnknownHostException("Unable to resolve host " + host
+ " as hosts file " + hostsFile + " not found ");
}
if (inetAddresses != null) {
res = inetAddresses.toArray(new InetAddress[inetAddresses.size()]);
} else {
throw new UnknownHostException("Unable to resolve host " + host
+ " in hosts file " + hostsFile);
}
return res;
}
private String removeComments(String hostsEntry) {
String filteredEntry = hostsEntry;
int hashIndex;
if ((hashIndex = hostsEntry.indexOf("#")) != -1) {
filteredEntry = hostsEntry.substring(0, hashIndex);
}
return filteredEntry;
}
private byte [] createAddressByteArray(String addrStr) {
byte[] addrArray;
// check if IPV4 address - most likely
addrArray = IPAddressUtil.textToNumericFormatV4(addrStr);
if (addrArray == null) {
addrArray = IPAddressUtil.textToNumericFormatV6(addrStr);
}
return addrArray;
}
/** host to ip address mapping */
private String extractHostAddr(String hostEntry, String host) {
String[] mapping = hostEntry.split("\\s+");
String hostAddr = null;
if (mapping.length >= 2) {
// look at the host aliases
for (int i = 1; i < mapping.length; i++) {
if (mapping[i].equalsIgnoreCase(host)) {
hostAddr = mapping[0];
}
}
}
return hostAddr;
}
/**
* IP Address to host mapping
* use first host alias in list
*/
private String extractHost(String hostEntry, String addrString) {
String[] mapping = hostEntry.split("\\s+");
String host = null;
if (mapping.length >= 2) {
if (mapping[0].equalsIgnoreCase(addrString)) {
host = mapping[1];
}
}
return host;
}
}
static final InetAddressImpl impl;
static {
// create the impl
impl = InetAddressImplFactory.create();
// get name service if provided and requested
String provider = null;;
String propPrefix = "sun.net.spi.nameservice.provider.";
int n = 1;
nameServices = new ArrayList<>();
provider = AccessController.doPrivileged(
new GetPropertyAction(propPrefix + n));
while (provider != null) {
NameService ns = createNSProvider(provider);
if (ns != null)
nameServices.add(ns);
n++;
provider = AccessController.doPrivileged(
new GetPropertyAction(propPrefix + n));
// create name service
nameService = createNameService();
}
// if not designate any name services provider,
// create a default one
if (nameServices.size() == 0) {
NameService ns = createNSProvider("default");
nameServices.add(ns);
/**
* Create an instance of the NameService interface based on
* the setting of the {@codejdk.net.hosts.file} system property.
*
* <p>The default NameService is the PlatformNameService, which typically
* delegates name and address resolution calls to the underlying
* OS network libraries.
*
* <p> A HostsFileNameService is created if the {@code jdk.net.hosts.file}
* system property is set. If the specified file doesn't exist, the name or
* address lookup will result in an UnknownHostException. Thus, non existent
* hosts file is handled as if the file is empty.
*
* @return a NameService
*/
private static NameService createNameService() {
String hostsFileName = AccessController
.doPrivileged(new GetPropertyAction("jdk.net.hosts.file"));
NameService theNameService;
if (hostsFileName != null) {
theNameService = new HostsFileNameService(hostsFileName);
} else {
theNameService = new PlatformNameService();
}
return theNameService;
}
/**
@ -1286,20 +1480,16 @@ class InetAddress implements java.io.Serializable {
InetAddress[] addresses = null;
UnknownHostException ex = null;
for (NameService nameService : nameServices) {
try {
addresses = nameService.lookupAllHostAddr(host);
break;
} catch (UnknownHostException uhe) {
if (host.equalsIgnoreCase("localhost")) {
addresses = new InetAddress[] { impl.loopbackAddress() };
break;
}
else {
ex = uhe;
}
}
}
if (addresses == null) {
throw ex == null ? new UnknownHostException(host) : ex;

@ -192,8 +192,6 @@ module java.base {
exports sun.net.dns to
java.security.jgss,
jdk.naming.dns;
exports sun.net.spi.nameservice to
jdk.naming.dns;
exports sun.net.util to
java.desktop,
jdk.jconsole,
@ -286,7 +284,6 @@ module java.base {
// JDK-internal service types
uses jdk.internal.logger.DefaultLoggerFinder;
uses sun.net.spi.nameservice.NameServiceDescriptor;
uses sun.security.ssl.ClientKeyExchangeService;
uses sun.util.spi.CalendarProvider;
uses sun.util.locale.provider.LocaleDataMetaInfo;

@ -1,33 +0,0 @@
/*
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.net.spi.nameservice;
import java.net.UnknownHostException;
public interface NameService {
public java.net.InetAddress[] lookupAllHostAddr(String host) throws UnknownHostException;
public String getHostByAddr(byte[] addr) throws UnknownHostException;
}

@ -1,45 +0,0 @@
/*
* Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.net.spi.nameservice;
public interface NameServiceDescriptor {
/**
* Create a new instance of the corresponding name service.
*/
public NameService createNameService () throws Exception ;
/**
* Returns this service provider's name
*
*/
public String getProviderName();
/**
* Returns this name service type
* "dns" "nis" etc
*/
public String getType();
}

@ -31,7 +31,5 @@ module jdk.naming.dns {
provides javax.naming.spi.InitialContextFactory
with com.sun.jndi.dns.DnsContextFactory;
provides sun.net.spi.nameservice.NameServiceDescriptor
with sun.net.spi.nameservice.dns.DNSNameServiceDescriptor;
}

@ -1,501 +0,0 @@
/*
* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.net.spi.nameservice.dns;
import java.lang.ref.SoftReference;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.naming.*;
import javax.naming.directory.*;
import javax.naming.spi.NamingManager;
import java.util.*;
import sun.net.util.IPAddressUtil;
import sun.net.dns.ResolverConfiguration;
import sun.net.spi.nameservice.*;
import java.security.AccessController;
import java.security.PrivilegedAction;
/*
* A name service provider based on JNDI-DNS.
*/
public final class DNSNameService implements NameService {
// List of domains specified by property
private LinkedList<String> domainList = null;
// JNDI-DNS URL for name servers specified via property
private String nameProviderUrl = null;
// Per-thread soft cache of the last temporary context
private static ThreadLocal<SoftReference<ThreadContext>> contextRef =
new ThreadLocal<>();
// Simple class to encapsulate the temporary context
private static class ThreadContext {
private DirContext dirCtxt;
private List<String> nsList;
public ThreadContext(DirContext dirCtxt, List<String> nsList) {
this.dirCtxt = dirCtxt;
this.nsList = nsList;
}
public DirContext dirContext() {
return dirCtxt;
}
public List<String> nameservers() {
return nsList;
}
}
// Returns a per-thread DirContext
private DirContext getTemporaryContext() throws NamingException {
SoftReference<ThreadContext> ref = contextRef.get();
ThreadContext thrCtxt = null;
List<String> nsList = null;
// if no property specified we need to obtain the list of servers
//
if (nameProviderUrl == null)
nsList = ResolverConfiguration.open().nameservers();
// if soft reference hasn't been gc'ed no property has been
// specified then we need to check if the DNS configuration
// has changed.
//
if ((ref != null) && ((thrCtxt = ref.get()) != null)) {
if (nameProviderUrl == null) {
if (!thrCtxt.nameservers().equals(nsList)) {
// DNS configuration has changed
thrCtxt = null;
}
}
}
// new thread context needs to be created
if (thrCtxt == null) {
final Hashtable<String,Object> env = new Hashtable<>();
env.put("java.naming.factory.initial",
"com.sun.jndi.dns.DnsContextFactory");
// If no nameservers property specified we create provider URL
// based on system configured name servers
//
String provUrl = nameProviderUrl;
if (provUrl == null) {
provUrl = createProviderURL(nsList);
if (provUrl.length() == 0) {
throw new RuntimeException("bad nameserver configuration");
}
}
env.put("java.naming.provider.url", provUrl);
// Need to create directory context in privileged block
// as JNDI-DNS needs to resolve the name servers.
//
DirContext dirCtxt;
try {
dirCtxt = java.security.AccessController.doPrivileged(
new java.security.PrivilegedExceptionAction<DirContext>() {
public DirContext run() throws NamingException {
// Create the DNS context using NamingManager rather than using
// the initial context constructor. This avoids having the initial
// context constructor call itself.
Context ctx = NamingManager.getInitialContext(env);
if (!(ctx instanceof DirContext)) {
return null; // cannot create a DNS context
}
return (DirContext)ctx;
}
});
} catch (java.security.PrivilegedActionException pae) {
throw (NamingException)pae.getException();
}
// create new soft reference to our thread context
//
thrCtxt = new ThreadContext(dirCtxt, nsList);
contextRef.set(new SoftReference<ThreadContext>(thrCtxt));
}
return thrCtxt.dirContext();
}
/**
* Resolves the specified entry in DNS.
*
* Canonical name records are recursively resolved (to a maximum
* of 5 to avoid performance hit and potential CNAME loops).
*
* @param ctx JNDI directory context
* @param name name to resolve
* @param ids record types to search
* @param depth call depth - pass as 0.
*
* @return array list with results (will have at least on entry)
*
* @throws UnknownHostException if lookup fails or other error.
*/
private ArrayList<String> resolve(final DirContext ctx, final String name,
final String[] ids, int depth)
throws UnknownHostException
{
ArrayList<String> results = new ArrayList<>();
Attributes attrs;
// do the query
try {
attrs = java.security.AccessController.doPrivileged(
new java.security.PrivilegedExceptionAction<Attributes>() {
public Attributes run() throws NamingException {
return ctx.getAttributes(name, ids);
}
});
} catch (java.security.PrivilegedActionException pae) {
throw new UnknownHostException(pae.getException().getMessage());
}
// non-requested type returned so enumeration is empty
NamingEnumeration<? extends Attribute> ne = attrs.getAll();
if (!ne.hasMoreElements()) {
throw new UnknownHostException("DNS record not found");
}
// iterate through the returned attributes
UnknownHostException uhe = null;
try {
while (ne.hasMoreElements()) {
Attribute attr = ne.next();
String attrID = attr.getID();
for (NamingEnumeration<?> e = attr.getAll(); e.hasMoreElements();) {
String addr = (String)e.next();
// for canoncical name records do recursive lookup
// - also check for CNAME loops to avoid stack overflow
if (attrID.equals("CNAME")) {
if (depth > 4) {
throw new UnknownHostException(name + ": possible CNAME loop");
}
try {
results.addAll(resolve(ctx, addr, ids, depth+1));
} catch (UnknownHostException x) {
// canonical name can't be resolved.
if (uhe == null)
uhe = x;
}
} else {
results.add(addr);
}
}
}
} catch (NamingException nx) {
throw new UnknownHostException(nx.getMessage());
}
// pending exception as canonical name could not be resolved.
if (results.isEmpty() && uhe != null) {
throw uhe;
}
return results;
}
public DNSNameService() throws Exception {
// default domain
String domain = AccessController.doPrivileged(
(PrivilegedAction<String>) () -> System.getProperty("sun.net.spi.nameservice.domain"));
if (domain != null && domain.length() > 0) {
domainList = new LinkedList<String>();
domainList.add(domain);
}
// name servers
String nameservers = AccessController.doPrivileged(
(PrivilegedAction<String>) () -> System.getProperty("sun.net.spi.nameservice.nameservers"));
if (nameservers != null && nameservers.length() > 0) {
nameProviderUrl = createProviderURL(nameservers);
if (nameProviderUrl.length() == 0) {
throw new RuntimeException("malformed nameservers property");
}
} else {
// no property specified so check host DNS resolver configured
// with at least one nameserver in dotted notation.
//
List<String> nsList = ResolverConfiguration.open().nameservers();
if (nsList.isEmpty()) {
throw new RuntimeException("no nameservers provided");
}
boolean found = false;
for (String addr: nsList) {
if (IPAddressUtil.isIPv4LiteralAddress(addr) ||
IPAddressUtil.isIPv6LiteralAddress(addr)) {
found = true;
break;
}
}
if (!found) {
throw new RuntimeException("bad nameserver configuration");
}
}
}
public InetAddress[] lookupAllHostAddr(String host) throws UnknownHostException {
// DNS records that we search for
String[] ids = {"A", "AAAA", "CNAME"};
// first get directory context
DirContext ctx;
try {
ctx = getTemporaryContext();
} catch (NamingException nx) {
throw new Error(nx);
}
ArrayList<String> results = null;
UnknownHostException uhe = null;
// If host already contains a domain name then just look it up
if (host.indexOf('.') >= 0) {
try {
results = resolve(ctx, host, ids, 0);
} catch (UnknownHostException x) {
uhe = x;
}
}
// Here we try to resolve the host using the domain suffix or
// the domain suffix search list. If the host cannot be resolved
// using the domain suffix then we attempt devolution of
// the suffix - eg: if we are searching for "foo" and our
// domain suffix is "eng.sun.com" we will try to resolve
// "foo.eng.sun.com" and "foo.sun.com".
// It's not normal to attempt devolation with domains on the
// domain suffix search list - however as ResolverConfiguration
// doesn't distinguish domain or search list in the list it
// returns we approximate by doing devolution on the domain
// suffix if the list has one entry.
if (results == null) {
List<String> searchList = null;
Iterator<String> i;
boolean usingSearchList = false;
if (domainList != null) {
i = domainList.iterator();
} else {
searchList = ResolverConfiguration.open().searchlist();
if (searchList.size() > 1) {
usingSearchList = true;
}
i = searchList.iterator();
}
// iterator through each domain suffix
while (i.hasNext()) {
String parentDomain = i.next();
int start = 0;
while ((start = parentDomain.indexOf('.')) != -1
&& start < parentDomain.length() -1) {
try {
results = resolve(ctx, host+"."+parentDomain, ids, 0);
break;
} catch (UnknownHostException x) {
uhe = x;
if (usingSearchList) {
break;
}
// devolve
parentDomain = parentDomain.substring(start+1);
}
}
if (results != null) {
break;
}
}
}
// finally try the host if it doesn't have a domain name
if (results == null && (host.indexOf('.') < 0)) {
results = resolve(ctx, host, ids, 0);
}
// if not found then throw the (last) exception thrown.
if (results == null) {
assert uhe != null;
throw uhe;
}
/**
* Convert the array list into a byte aray list - this
* filters out any invalid IPv4/IPv6 addresses.
*/
assert results.size() > 0;
InetAddress[] addrs = new InetAddress[results.size()];
int count = 0;
for (int i=0; i<results.size(); i++) {
String addrString = results.get(i);
byte addr[] = IPAddressUtil.textToNumericFormatV4(addrString);
if (addr == null) {
addr = IPAddressUtil.textToNumericFormatV6(addrString);
}
if (addr != null) {
addrs[count++] = InetAddress.getByAddress(host, addr);
}
}
/**
* If addresses are filtered then we need to resize the
* array. Additionally if all addresses are filtered then
* we throw an exception.
*/
if (count == 0) {
throw new UnknownHostException(host + ": no valid DNS records");
}
if (count < results.size()) {
InetAddress[] tmp = new InetAddress[count];
for (int i=0; i<count; i++) {
tmp[i] = addrs[i];
}
addrs = tmp;
}
return addrs;
}
/**
* Reverse lookup code. I.E: find a host name from an IP address.
* IPv4 addresses are mapped in the IN-ADDR.ARPA. top domain, while
* IPv6 addresses can be in IP6.ARPA or IP6.INT.
* In both cases the address has to be converted into a dotted form.
*/
public String getHostByAddr(byte[] addr) throws UnknownHostException {
String host = null;
try {
String literalip = "";
String[] ids = { "PTR" };
DirContext ctx;
ArrayList<String> results = null;
try {
ctx = getTemporaryContext();
} catch (NamingException nx) {
throw new Error(nx);
}
if (addr.length == 4) { // IPv4 Address
for (int i = addr.length-1; i >= 0; i--) {
literalip += (addr[i] & 0xff) +".";
}
literalip += "IN-ADDR.ARPA.";
results = resolve(ctx, literalip, ids, 0);
host = results.get(0);
} else if (addr.length == 16) { // IPv6 Address
/**
* Because RFC 3152 changed the root domain name for reverse
* lookups from IP6.INT. to IP6.ARPA., we need to check
* both. I.E. first the new one, IP6.ARPA, then if it fails
* the older one, IP6.INT
*/
for (int i = addr.length-1; i >= 0; i--) {
literalip += Integer.toHexString((addr[i] & 0x0f)) +"."
+Integer.toHexString((addr[i] & 0xf0) >> 4) +".";
}
String ip6lit = literalip + "IP6.ARPA.";
try {
results = resolve(ctx, ip6lit, ids, 0);
host = results.get(0);
} catch (UnknownHostException e) {
host = null;
}
if (host == null) {
// IP6.ARPA lookup failed, let's try the older IP6.INT
ip6lit = literalip + "IP6.INT.";
results = resolve(ctx, ip6lit, ids, 0);
host = results.get(0);
}
}
} catch (Exception e) {
throw new UnknownHostException(e.getMessage());
}
// Either we couldn't find it or the address was neither IPv4 or IPv6
if (host == null)
throw new UnknownHostException();
// remove trailing dot
if (host.endsWith(".")) {
host = host.substring(0, host.length() - 1);
}
return host;
}
// ---------
private static void appendIfLiteralAddress(String addr, StringBuilder sb) {
if (IPAddressUtil.isIPv4LiteralAddress(addr)) {
sb.append("dns://").append(addr).append(' ');
} else {
if (IPAddressUtil.isIPv6LiteralAddress(addr)) {
sb.append("dns://[").append(addr).append("] ");
}
}
}
/*
* @return String containing the JNDI-DNS provider URL
* corresponding to the supplied List of nameservers.
*/
private static String createProviderURL(List<String> nsList) {
StringBuilder sb = new StringBuilder();
for (String s: nsList) {
appendIfLiteralAddress(s, sb);
}
return sb.toString();
}
/*
* @return String containing the JNDI-DNS provider URL
* corresponding to the list of nameservers
* contained in the provided str.
*/
private static String createProviderURL(String str) {
StringBuilder sb = new StringBuilder();
StringTokenizer st = new StringTokenizer(str, ",");
while (st.hasMoreTokens()) {
appendIfLiteralAddress(st.nextToken(), sb);
}
return sb.toString();
}
}

@ -1,53 +0,0 @@
/*
* Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.net.spi.nameservice.dns;
import sun.net.spi.nameservice.*;
public final class DNSNameServiceDescriptor implements NameServiceDescriptor {
/**
* Create a new instance of the corresponding name service.
*/
public NameService createNameService() throws Exception {
return new DNSNameService();
}
/**
* Returns this service provider's name
*
*/
public String getProviderName() {
return "sun";
}
/**
* Returns this name service type
* "dns" "nis" etc
*/
public String getType() {
return "dns";
}
}

@ -159,8 +159,6 @@ sun/management/jmxremote/bootstrap/JMXInterfaceBindingTest.java 8147985 generic-
# jdk_net
sun/net/InetAddress/nameservice/simple/CacheTest.java 7148829 generic-all
sun/net/InetAddress/nameservice/simple/DefaultCaching.java 7148829 generic-all
java/net/MulticastSocket/NoLoopbackPackets.java 7122846 macosx-all
java/net/MulticastSocket/SetLoopbackMode.java 7122846 macosx-all
@ -284,6 +282,8 @@ sun/security/tools/keytool/autotest.sh 8130302 generic-
sun/security/provider/NSASuiteB/TestDSAGenParameterSpec.java 8137255 generic-all
sun/security/x509/URICertStore/ExtensionsWithLDAP.java 8134577 generic-all
############################################################################
# jdk_sound
@ -310,6 +310,7 @@ javax/imageio/plugins/tiff/WriteToSequenceAfterAbort.java 8148454 generic-
# jdk_time
############################################################################
# jdk_tools

@ -1,45 +0,0 @@
/*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* A simple name service which throws an exception when invoked
*/
import java.net.UnknownHostException;
import java.net.InetAddress;
import sun.net.spi.nameservice.*;
import java.util.*;
public final class DummyNameService implements NameService {
public DummyNameService() throws Exception {
}
public InetAddress[] lookupAllHostAddr(String host) throws UnknownHostException {
throw new UnknownHostException("Dummy name service");
}
public String getHostByAddr(byte[] addr) throws UnknownHostException {
throw new UnknownHostException("Dummy name service");
}
}

@ -1,54 +0,0 @@
/*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* Descriptor for the dummy name service
*/
import sun.net.spi.nameservice.*;
public final class DummyNameServiceDescriptor implements NameServiceDescriptor {
/**
* Create a new instance of the corresponding name service.
*/
public NameService createNameService() throws Exception {
return new DummyNameService();
}
/**
* Returns this service provider's name
*
*/
public String getProviderName() {
return "oracle";
}
/**
* Returns this name service type
* "dns" "nis" etc
*/
public String getType() {
return "dummy";
}
}

@ -1,22 +0,0 @@
# Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
DummyNameServiceDescriptor # name service provider descriptor

@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2016 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,9 +25,7 @@
* @test
* @bug 4749938 8087190
* @summary Bug in the parsing IPv4 literal addresses
* @modules java.base/sun.net.spi.nameservice
* @compile -XDignore.symbol.file=true DummyNameService.java DummyNameServiceDescriptor.java
* @run main/othervm -Dsun.net.spi.nameservice.provider.1=dummy,oracle textToNumericFormat
* @run main/othervm textToNumericFormat
*/
/**
@ -68,6 +66,8 @@ public class textToNumericFormat {
"1..1.1",
"1.1.1.",
"..." };
String hostsFileName = System.getProperty("test.src", ".") + "/TestToNumericFormatHosts";
System.setProperty("jdk.net.hosts.file", hostsFileName);
for (int i=0; i<goodAddrs.length; i++) {
try {

@ -0,0 +1,226 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 8134577
* @summary Test the internal NameService implementation which is enabled via
* the system property jdk.net.hosts.file. This property specifies
* a file name that contains address host mappings, similar to those in
* /etc/hosts file.
* @run main/othervm InternalNameServiceTest
*/
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
public class InternalNameServiceTest {
public static void main(String args[]) throws Exception {
String hostsFileName = System.getProperty("test.src", ".") + "/TestHosts";
System.setProperty("jdk.net.hosts.file", hostsFileName);
System.setProperty("sun.net.inetaddr.ttl", "0");
testHostToIPAddressMappings(hostsFileName);
testIpAddressToHostNameMappings(hostsFileName);
}
private static void testHostToIPAddressMappings(String hostsFileName)
throws Exception, UnknownHostException {
System.out.println(" TEST HOST TO IP ADDRESS MAPPINGS ");
InetAddress testAddress;
byte[] retrievedIpAddr;
byte[] expectedIpAddr1 = { 1, 2, 3, 4 };
byte[] expectedIpAddr2 = { 5, 6, 7, 8 };
byte[] expectedIpAddrIpv6_1 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
// hosts file with
// # test hosts file for internal NameService
// 1.2.3.4 host.sample-domain
// 5.6.7.8 host1.sample-domain
// 1.2.3.4 host2.sample-domain # this is a comment
// host3.sample-domain # no ip address
// host4.sample-domain # space as ip address
// host5.sample-domain # double space as ip address
// add comment to hosts file
addMappingToHostsFile("test hosts file for internal NameService ", "#", hostsFileName,
false);
addMappingToHostsFile("host.sample-domain", "1.2.3.4", hostsFileName,
true);
testAddress = InetAddress.getByName("host.sample-domain");
retrievedIpAddr = testAddress.getAddress();
if (!Arrays.equals(retrievedIpAddr, expectedIpAddr1)) {
throw new RuntimeException(
"retrievedIpAddr not equal to expectedipAddr");
}
addMappingToHostsFile("host1.sample-domain", "5.6.7.8", hostsFileName,
true);
addMappingToHostsFile("host2.sample-domain", "1.2.3.4", hostsFileName,
true);
testAddress = InetAddress.getByName("host1.sample-domain");
retrievedIpAddr = testAddress.getAddress();
if (!Arrays.equals(retrievedIpAddr, expectedIpAddr2)) {
throw new RuntimeException(
"retrievedIpAddr not equal to expectedIpAddr");
}
testAddress = InetAddress.getByName("host2.sample-domain");
retrievedIpAddr = testAddress.getAddress();
if (!Arrays.equals(retrievedIpAddr, expectedIpAddr1)) {
throw new RuntimeException(
"retrievedIpAddr not equal to expectedIpAddr");
}
try {
addMappingToHostsFile("host3.sample-domain", "", hostsFileName,
true);
testAddress = InetAddress.getByName("host3.sample-domain");
throw new RuntimeException(
"Expected UnknownHostException not thrown");
} catch (UnknownHostException uhEx) {
System.out.println("UnknownHostException as expected for host host3.sample-domain");
}
try {
addMappingToHostsFile("host4.sample-domain", " ", hostsFileName,
true);
testAddress = InetAddress.getByName("host4.sample-domain");
throw new RuntimeException(
"Expected UnknownHostException not thrown");
} catch (UnknownHostException uhEx) {
System.out.println("UnknownHostException as expected for host host4.sample-domain");
}
try {
addMappingToHostsFile("host5.sample-domain", " ", hostsFileName,
true);
testAddress = InetAddress.getByName("host4.sample-domain");
throw new RuntimeException(
"Expected UnknownHostException not thrown");
} catch (UnknownHostException uhEx) {
System.out.println("UnknownHostException as expected for host host5.sample-domain");
}
// IPV6 tests
// IPV6 tests
addMappingToHostsFile("host-ipv6.sample-domain", "::1", hostsFileName,
true);
testAddress = InetAddress.getByName("host-ipv6.sample-domain");
retrievedIpAddr = testAddress.getAddress();
if (!Arrays.equals(retrievedIpAddr, expectedIpAddrIpv6_1)) {
System.out.println("retrieved ipv6 addr == " + Arrays.toString(retrievedIpAddr));
System.out.println("expected ipv6 addr == " + Arrays.toString(expectedIpAddrIpv6_1));
throw new RuntimeException(
"retrieved IPV6 Addr not equal to expected IPV6 Addr");
}
}
private static void testIpAddressToHostNameMappings(String hostsFileName)
throws Exception {
System.out.println(" TEST IP ADDRESS TO HOST MAPPINGS ");
InetAddress testAddress;
String retrievedHost;
String expectedHost = "testHost.testDomain";
byte[] testHostIpAddr = { 10, 2, 3, 4 };
byte[] testHostIpAddr2 = { 10, 5, 6, 7 };
byte[] testHostIpAddr3 = { 10, 8, 9, 10 };
byte[] testHostIpAddr4 = { 10, 8, 9, 11 };
// add comment to hosts file
addMappingToHostsFile("test hosts file for internal NameService ", "#", hostsFileName,
false);
addMappingToHostsFile("testHost.testDomain", "10.2.3.4", hostsFileName,
true);
testAddress = InetAddress.getByAddress(testHostIpAddr);
System.out.println("******* testAddress == " + testAddress);
retrievedHost = testAddress.getHostName();
if (!expectedHost.equals(retrievedHost)) {
throw new RuntimeException(
"retrieved host name not equal to expected host name");
}
addMappingToHostsFile("testHost.testDomain", "10.5.6.7", hostsFileName,
true);
testAddress = InetAddress.getByAddress(testHostIpAddr2);
System.out.println("******* testAddress == " + testAddress);
retrievedHost = testAddress.getHostName();
System.out.println("******* retrievedHost == " + retrievedHost);
if (!expectedHost.equals(retrievedHost)) {
throw new RuntimeException("retrieved host name " + retrievedHost
+ " not equal to expected host name" + expectedHost);
}
testAddress = InetAddress.getByAddress(testHostIpAddr4);
System.out.println("******* testAddress == " + testAddress);
if ("10.8.9.11".equalsIgnoreCase(testAddress.getCanonicalHostName())) {
System.out.println("addr = " + addrToString(testHostIpAddr4)
+ " resolve to a host address as expected");
} else {
System.out.println("addr = " + addrToString(testHostIpAddr4)
+ " does not resolve as expected, testAddress == " + testAddress.getCanonicalHostName());
throw new RuntimeException("problem with resolving "
+ addrToString(testHostIpAddr4));
}
try {
addMappingToHostsFile("", "10.8.9.10", hostsFileName, true);
testAddress = InetAddress.getByAddress(testHostIpAddr3);
System.out.println("******* testAddress == " + testAddress);
retrievedHost = testAddress.getCanonicalHostName();
} catch (Throwable t) {
throw new RuntimeException("problem with resolving "
+ addrToString(testHostIpAddr3));
}
}
private static String addrToString(byte addr[]) {
return Byte.toString(addr[0]) + "." + Byte.toString(addr[1]) + "."
+ Byte.toString(addr[2]) + "." + Byte.toString(addr[3]);
}
private static void addMappingToHostsFile( String host,
String addr,
String hostsFileName,
boolean append)
throws Exception {
String mapping = addr + " " + host;
try (PrintWriter hfPWriter = new PrintWriter(new BufferedWriter(
new FileWriter(hostsFileName, append)))) {
hfPWriter.println(mapping);
}
}
}

@ -0,0 +1,97 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 8134577
* @summary Test the internal NameService implementation which is enabled via
* the system property jdk.net.hosts.file. This property specifies
* a file name that contains address host mappings, similar to those in
* /etc/hosts file. TestHosts-III file exist, with a set of ipv4 and ipv6
* mappings
* @run main/othervm -Dsun.net.inetaddr.ttl=0 InternalNameServiceWithHostsFileTest
*/
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
public class InternalNameServiceWithHostsFileTest {
public static void main(String args[]) throws Exception {
// System.getProperty("test.src", ".");
String hostsFileName = System.getProperty("test.src", ".")
+ "/TestHosts-III";
System.setProperty("jdk.net.hosts.file", hostsFileName);
System.setProperty("sun.net.inetaddr.ttl", "0");
// fe80::1
byte[] expectedIpv6Address = { (byte) 0xfe, (byte) 0x80, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1 };
// fe00::0
byte[] expectedIpv6LocalAddress = { (byte) 0xfe, (byte) 0x00, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
// 10.2.3.4
byte[] expectedIpv4Address = { 10, 2, 3, 4 };
//
byte[] expectedIpv6LocalhostAddress = { 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1 };
try {
// 10.2.3.4 testHost.testDomain
testHostsMapping(expectedIpv4Address, "testHost.testDomain");
// ::1 ip6-localhost ip6-loopback
testHostsMapping(expectedIpv6LocalhostAddress, "ip6-localhost");
// fe00::0 ip6-localnet
testHostsMapping(expectedIpv6LocalAddress, "ip6-localnet");
// fe80::1 link-local-host
testHostsMapping(expectedIpv6Address, "link-local-host");
} catch (UnknownHostException uhEx) {
System.out.println("UHE unexpected caught == " + uhEx.getMessage());
}
}
private static void testHostsMapping(byte[] expectedIpAddress, String hostName)
throws UnknownHostException {
InetAddress testAddress;
byte[] rawIpAddress;
testAddress = InetAddress.getByName(hostName);
System.out
.println("############################ InetAddress == "
+ testAddress);
rawIpAddress = testAddress.getAddress();
if (!Arrays.equals(rawIpAddress, expectedIpAddress)) {
System.out.println("retrieved address == "
+ Arrays.toString(rawIpAddress)
+ " not equal to expected address == "
+ Arrays.toString(expectedIpAddress));
throw new RuntimeException(
"retrieved address not equal to expected address");
}
System.out.println("retrieved address == "
+ Arrays.toString(rawIpAddress)
+ " equal to expected address == "
+ Arrays.toString(expectedIpAddress));
}
}

@ -0,0 +1,55 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 8134577
* @summary Test the internal NameService implementation which is enabled via
* the system property jdk.net.hosts.file. This property specifies
* a file name that contains address host mappings, similar to those in
* /etc/hosts file. TestHosts-II file doesn't exist, so a UHE should be
* thrown
* @run main/othervm -Dsun.net.inetaddr.ttl=0 InternalNameServiceWithNoHostsFileTest
*/
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InternalNameServiceWithNoHostsFileTest {
public static void main(String args[]) throws Exception {
String hostsFileName = System.getProperty("test.src", ".") + "/TestHosts-II";
System.setProperty("jdk.net.hosts.file", hostsFileName);
System.setProperty("sun.net.inetaddr.ttl", "0");
InetAddress testAddress = null;
try {
testAddress = InetAddress.getByName("host.sample-domain");
throw new RuntimeException ("UnknownHostException expected");
} catch (UnknownHostException uhEx) {
System.out.println("UHE caught as expected == " + uhEx.getMessage());
}
}
}

@ -0,0 +1,4 @@
# test hosts file for internal NameService
10.2.3.4 testHost.testDomain
10.5.6.7 testHost.testDomain
10.8.9.10

@ -0,0 +1,11 @@
# test hosts file for internal NameService
10.2.3.4 testHost.testDomain
10.5.6.7 testHost.testDomain
10.8.9.10
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe80::1 link-local-host
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2016 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -63,14 +63,18 @@ public class LookupTest {
static ServerSocket serverSocket;
public static void main(String args[]) throws Exception {
String cmd = args[0];
if (cmd.equals("-getport")) {
port = Utils.getFreePort();
System.out.print(port);
} else if (cmd.equals("-runtest")) {
port = Integer.parseInt(args[1]);
SimpleNameService.put("allowedAndFound.com", "127.0.0.1");
SimpleNameService.put("notAllowedButFound.com", "99.99.99.99");
String hostsFileName = System.getProperty("test.src", ".") + "/LookupTestHosts";
System.setProperty("jdk.net.hosts.file", hostsFileName);
addMappingToHostsFile("allowedAndFound.com", "127.0.0.1", hostsFileName, false);
addMappingToHostsFile("notAllowedButFound.com", "99.99.99.99", hostsFileName, true);
// name "notAllowedAndNotFound.com" is not in map
// name "allowedButNotfound.com" is not in map
try {
@ -124,4 +128,17 @@ public class LookupTest {
throw new RuntimeException ("Test failed to initialize", e);
}
}
private static void addMappingToHostsFile (String host,
String addr,
String hostsFileName,
boolean append)
throws Exception {
String mapping = addr + " " + host;
try (PrintWriter hfPWriter = new PrintWriter(new BufferedWriter(
new FileWriter(hostsFileName, append)))) {
hfPWriter.println(mapping);
}
}
}

@ -0,0 +1,2 @@
127.0.0.1 allowedAndFound.com
99.99.99.99 notAllowedButFound.com

@ -1,22 +0,0 @@
# Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
SimpleNameServiceDescriptor # name service provider descriptor

@ -1,102 +0,0 @@
/*
* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* A simple name service based on an in-memory HashMap.
*/
import java.net.UnknownHostException;
import java.net.InetAddress;
import sun.net.spi.nameservice.*;
import java.util.*;
public final class SimpleNameService implements NameService {
private static LinkedHashMap hosts = new LinkedHashMap();
private static String addrToString(byte addr[]) {
return Byte.toString(addr[0]) + "." +
Byte.toString(addr[1]) + "." +
Byte.toString(addr[2]) + "." +
Byte.toString(addr[3]);
}
// ------------
public static void put(String host, String addr) {
hosts.put(host, addr);
}
public static void put(String host, byte addr[]) {
hosts.put(host, addrToString(addr));
}
public static void remove(String host) {
hosts.remove(host);
}
public static int entries () {
return hosts.size();
}
public static int lookupCalls() {
return lookupCalls;
}
static int lookupCalls = 0;
// ------------
public SimpleNameService() throws Exception {
}
public InetAddress[] lookupAllHostAddr(String host) throws UnknownHostException {
lookupCalls ++;
String value = (String)hosts.get(host);
if (value == null) {
throw new UnknownHostException(host);
}
StringTokenizer st = new StringTokenizer(value, ".");
byte addr[] = new byte[4];
for (int i=0; i<4; i++) {
addr[i] = (byte)Integer.parseInt(st.nextToken());
}
InetAddress[] res = new InetAddress[1];
res[0] = InetAddress.getByAddress(host, addr);
return res;
}
public String getHostByAddr(byte[] addr) throws UnknownHostException {
String addrString = addrToString(addr);
Iterator i = hosts.keySet().iterator();
while (i.hasNext()) {
String host = (String)i.next();
String value = (String)hosts.get(host);
if (value.equals(addrString)) {
return host;
}
}
throw new UnknownHostException();
}
}

@ -1,52 +0,0 @@
/*
* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* Descriptor for the simple name service
*/
import sun.net.spi.nameservice.*;
public final class SimpleNameServiceDescriptor implements NameServiceDescriptor {
/**
* Create a new instance of the corresponding name service.
*/
public NameService createNameService() throws Exception {
return new SimpleNameService();
}
/**
* Returns this service provider's name
*
*/
public String getProviderName() {
return "sun";
}
/**
* Returns this name service type
* "dns" "nis" etc
*/
public String getType() {
return "simple";
}
}

@ -1,6 +1,6 @@
#!/bin/sh
#
# Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2013, 2016 Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
@ -24,10 +24,8 @@
# @test
# @library /lib/testlibrary
# @modules java.base/sun.net.spi.nameservice
# @build jdk.testlibrary.*
# @compile -XDignore.symbol.file=true SimpleNameService.java
# LookupTest.java SimpleNameServiceDescriptor.java
# @compile -XDignore.symbol.file=true LookupTest.java
# @run shell/timeout=50 lookup.sh
#
@ -58,7 +56,6 @@ grant {
POLICY
${TESTJAVA}/bin/java ${TESTVMOPTS} \
-XaddExports:java.base/sun.net.spi.nameservice=ALL-UNNAMED \
-Djava.security.policy=file:./policy \
-Dsun.net.spi.nameservice.provider.1=simple,sun \
-Dtest.src=${TESTSRC} \
-cp ${TESTCLASSPATH}${PS}${TESTSRC} LookupTest -runtest ${port}

@ -4,5 +4,4 @@ modules = \
java.security.jgss/sun.security.krb5.internal.ccache \
java.security.jgss/sun.security.krb5.internal \
java.security.jgss/sun.security.krb5.internal.ktab \
java.base/sun.net.spi.nameservice \
java.base/sun.security.util

@ -4,5 +4,4 @@ modules = \
java.security.jgss/sun.security.krb5.internal.ccache \
java.security.jgss/sun.security.krb5.internal \
java.security.jgss/sun.security.krb5.internal.ktab \
java.base/sun.net.spi.nameservice \
java.base/sun.security.util

@ -4,6 +4,5 @@ modules = \
java.security.jgss/sun.security.krb5 \
java.security.jgss/sun.security.krb5.internal.ccache \
java.security.jgss/sun.security.krb5.internal \
java.base/sun.net.spi.nameservice \
java.base/sun.security.util \
jdk.crypto.ec/sun.security.ec

@ -4,5 +4,4 @@ modules = \
java.security.jgss/sun.security.krb5.internal.ccache \
java.security.jgss/sun.security.krb5.internal \
java.security.jgss/sun.security.krb5.internal.ktab \
java.base/sun.net.spi.nameservice \
java.base/sun.security.util

@ -4,5 +4,4 @@ modules = \
java.security.jgss/sun.security.krb5.internal.ccache \
java.security.jgss/sun.security.krb5.internal \
java.security.jgss/sun.security.krb5.internal.ktab \
java.base/sun.net.spi.nameservice \
java.base/sun.security.util

@ -1,23 +0,0 @@
# Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
Simple1NameServiceDescriptor
Simple2NameServiceDescriptor

@ -1,81 +0,0 @@
/*
* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 4762344
* @summary 2nd nameservice provider is non functional
* @modules java.base/sun.net.spi.nameservice
* @compile -XDignore.symbol.file=true SimpleNameService.java
* Simple1NameServiceDescriptor.java
* Simple2NameServiceDescriptor.java
* @run main/othervm -Dsun.net.spi.nameservice.provider.1=simple1,sun -Dsun.net.spi.nameservice.provider.2=simple2,sun Providers
*/
import java.net.*;
import java.util.*;
public class Providers {
private static String[][] hostnames = new String[][] {
// both providers know this host, but with different address
new String[] {"blade", "10.0.0.1"},
// provider1 knwos this host
new String[] {"blade.domain1", "10.0.0.2"},
// provider2 knows this host
new String[] {"blade.domain2", "20.0.0.2"}
};
private static String[][] hostaddrs = new String[][] {
new String[] {"10.0.0.1", "blade"},
new String[] {"10.0.0.2", "blade.domain1"},
new String[] {"20.0.0.2", "blade.domain2"}
};
public static void main(String[] args) throws Exception {
for (int i = 0; i < hostnames.length; i++) {
doLookup(hostnames[i][0], hostnames[i][1]);
}
for (int i = 0; i < hostaddrs.length; i++) {
doReverseLookup(hostaddrs[i][0], hostaddrs[i][1]);
}
}
private static void doLookup(String host, String addr) throws Exception {
String res = InetAddress.getByName(host).getHostAddress();
if (!res.equals(addr)) {
throw new RuntimeException("Test failed: wrong address for host " + host);
}
}
private static void doReverseLookup(String addr, String host) throws Exception {
StringTokenizer tokenizer = new StringTokenizer(addr, ".");
byte addrs[] = new byte[4];
for (int i = 0; i < 4; i++) {
addrs[i] = (byte)Integer.parseInt(tokenizer.nextToken());
}
String res = InetAddress.getByAddress(addrs).getHostName();
if (!res.equals(host)) {
throw new RuntimeException("Test failed: wrong host name for address " + addr);
}
}
}

@ -1,49 +0,0 @@
/*
* Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
*/
import sun.net.spi.nameservice.*;
public class Simple1NameServiceDescriptor implements NameServiceDescriptor {
public NameService createNameService() {
SimpleNameService ns = new SimpleNameService();
// both providers know this host, but the address is different
ns.put("blade", "10.0.0.1");
// only this provider knows this host
ns.put("blade.domain1", "10.0.0.2");
return ns;
}
public String getProviderName() {
return "sun";
}
public String getType() {
return "simple1";
}
}

@ -1,48 +0,0 @@
/*
* Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
*/
import sun.net.spi.nameservice.*;
public class Simple2NameServiceDescriptor implements NameServiceDescriptor {
public NameService createNameService() {
SimpleNameService ns = new SimpleNameService();
// both providers know this host, but the address of it is different
ns.put("blade", "20.0.0.1");
// only this provider knows this host
ns.put("blade.domain2", "20.0.0.2");
return ns;
}
public String getProviderName() {
return "sun";
}
public String getType() {
return "simple2";
}
}

@ -1,78 +0,0 @@
/*
* Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
*/
import java.net.*;
import java.util.*;
import sun.net.spi.nameservice.*;
public class SimpleNameService implements NameService {
// host name <-> host addr mapping
private HashMap<String, String> hosts = new LinkedHashMap<String, String>();
public void put(String host, String addr) {
hosts.put(host, addr);
}
private static String addrToString(byte addr[]) {
return Byte.toString(addr[0]) + "." +
Byte.toString(addr[1]) + "." +
Byte.toString(addr[2]) + "." +
Byte.toString(addr[3]);
}
public SimpleNameService() {
}
public InetAddress[] lookupAllHostAddr(String host) throws UnknownHostException {
String addr = hosts.get(host);
if (addr == null) {
throw new UnknownHostException(host);
}
StringTokenizer tokenizer = new StringTokenizer(addr, ".");
byte addrs[] = new byte[4];
for (int i = 0; i < 4; i++) {
addrs[i] = (byte)Integer.parseInt(tokenizer.nextToken());
}
InetAddress[] ret = new InetAddress[1];
ret[0] = InetAddress.getByAddress(host, addrs);
return ret;
}
public String getHostByAddr(byte[] addr) throws UnknownHostException {
String addrString = addrToString(addr);
Iterator i = hosts.keySet().iterator();
while (i.hasNext()) {
String host = (String)i.next();
String value = (String)hosts.get(host);
if (value.equals(addrString)) {
return host;
}
}
throw new UnknownHostException();
}
}

@ -1,47 +0,0 @@
/*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 7012768
* @modules java.base/sun.net.spi.nameservice
* @compile -XDignore.symbol.file=true ThrowingNameService.java
* ThrowingNameServiceDescriptor.java
* @run main/othervm/timeout=30 -Dsun.net.spi.nameservice.provider.1=throwing,sun Hang
* @summary InetAddress lookupTable leaks/deadlocks when using unsupported
* name service spi
*/
import java.net.InetAddress;
public class Hang {
public static void main(String[] args) throws Exception {
try {
// 1st attempt - IllegalStateException caught below
InetAddress.getByName("host.company.com");
} catch (IllegalStateException e) { }
// 2nd attempt - Stuck here forever if bug exists
InetAddress.getByName("host.company.com");
}
}

@ -1,22 +0,0 @@
# Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
ThrowingNameServiceDescriptor

@ -1,47 +0,0 @@
/*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.net.InetAddress;
import java.net.UnknownHostException;
import sun.net.spi.nameservice.NameService;
public class ThrowingNameService implements NameService {
static boolean firstCall = true;
@Override
public InetAddress[] lookupAllHostAddr(String host) throws UnknownHostException {
if (firstCall) {
firstCall = false;
// throw unchecked exception first time round
throw new IllegalStateException();
}
// return any valid address
return new InetAddress[] { InetAddress.getLoopbackAddress() };
}
@Override
public String getHostByAddr(byte[] addr) throws UnknownHostException {
throw new IllegalStateException();
}
}

@ -1,40 +0,0 @@
/*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import sun.net.spi.nameservice.*;
public class ThrowingNameServiceDescriptor implements NameServiceDescriptor {
public NameService createNameService() {
return new ThrowingNameService();
}
@Override
public String getProviderName() {
return "sun";
}
@Override
public String getType() {
return "throwing";
}
}

@ -66,8 +66,8 @@ echo "};" >> ${POLICY}
np="-Dsun.net.spi.nameservice.provider.1=dns,sun"
sm="-Djava.security.manager -Djava.security.policy=${POLICY}"
go "$np" "$HOST"
go "$np $sm" "$HOST"
go "" "$HOST"
go "$sm" "$HOST"
#

@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -26,14 +26,14 @@
* @summary Check that InetAddress doesn't continue to throw UHE
* after the name service has recovered and the negative ttl
* on the initial lookup has expired.
* @modules java.base/sun.net.spi.nameservice
* @compile -XDignore.symbol.file=true SimpleNameService.java
* SimpleNameServiceDescriptor.java
* @run main/othervm/timeout=200 -Dsun.net.spi.nameservice.provider.1=simple,sun CacheTest
* @run main/othervm/timeout=200 CacheTest
*/
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.Security;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.BufferedWriter;
public class CacheTest {
@ -59,6 +59,8 @@ public class CacheTest {
return;
}
String hostsFileName = System.getProperty("test.src", ".") + "/CacheTestHosts";
System.setProperty("jdk.net.hosts.file", hostsFileName);
/*
* The following outlines how the test works :-
@ -78,7 +80,7 @@ public class CacheTest {
*/
// name service needs to resolve this.
SimpleNameService.put("theclub", "129.156.220.219");
addMappingToHostsFile("theclub", "129.156.220.219", hostsFileName, false);
// this lookup will succeed
InetAddress.getByName("theclub");
@ -89,12 +91,12 @@ public class CacheTest {
try {
InetAddress.getByName("luster");
throw new RuntimeException("Test internal error " +
" - luster is bring resolved by name service");
" - luster is being resolved by name service");
} catch (UnknownHostException x) {
}
// name service now needs to know about luster
SimpleNameService.put("luster", "10.5.18.21");
addMappingToHostsFile("luster", "10.5.18.21", hostsFileName, true);
// wait for the cache entry to expire and lookup should
// succeed.
@ -102,4 +104,16 @@ public class CacheTest {
InetAddress.getByName("luster");
}
private static void addMappingToHostsFile ( String host,
String addr,
String hostsFileName,
boolean append)
throws Exception {
String mapping = addr + " " + host;
try (PrintWriter hfPWriter = new PrintWriter(new BufferedWriter(
new FileWriter(hostsFileName, append)))) {
hfPWriter.println(mapping);
}
}
}

@ -0,0 +1,2 @@
129.156.220.219 theclub
10.5.18.21 luster

@ -1,5 +1,5 @@
/*
* Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,35 +25,40 @@
* @bug 6442088
* @summary Change default DNS caching behavior for code not running under
* security manager.
* @modules java.base/sun.net.spi.nameservice
* @compile -XDignore.symbol.file=true SimpleNameService.java
* SimpleNameServiceDescriptor.java
* @run main/othervm/timeout=200 -Dsun.net.inetaddr.ttl=20 -Dsun.net.spi.nameservice.provider.1=simple,sun DefaultCaching
* @run main/othervm/timeout=200 -Dsun.net.inetaddr.ttl=20 DefaultCaching
*/
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.Security;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.BufferedWriter;
public class DefaultCaching {
public static void main(String args[]) throws Exception {
String hostsFileName = System.getProperty("test.src", ".") + "/DefaultCachingHosts";
System.setProperty("jdk.net.hosts.file", hostsFileName);
// initial mapping
// name service needs to resolve this.
SimpleNameService.put("theclub", "129.156.220.219");
addMappingToHostsFile("theclub", "129.156.220.219", hostsFileName, false);
test ("theclub", "129.156.220.219", true); // lk: 1
test ("luster", "1.16.20.2", false); // lk: 2
// name service now needs to know about luster
SimpleNameService.put("luster", "10.5.18.21");
addMappingToHostsFile("luster", "10.5.18.21", hostsFileName, true);
test ("luster", "1.16.20.2", false); // lk: 2
sleep (10+1);
test("luster", "10.5.18.21", true, 3); // lk: 3
sleep (5);
SimpleNameService.put("foo", "10.5.18.22");
SimpleNameService.put("theclub", "129.156.220.1");
// new mapping for theclub and rewrite existing foo and luster mappings
addMappingToHostsFile("theclub", "129.156.220.1", hostsFileName, false);
addMappingToHostsFile("foo", "10.5.18.22", hostsFileName, true);
addMappingToHostsFile("luster", "10.5.18.21", hostsFileName, true);
test ("theclub", "129.156.220.219", true, 3);
test ("luster", "10.5.18.21", true, 3);
@ -84,10 +89,6 @@ public class DefaultCaching {
static void test (String host, String address,
boolean shouldSucceed, int count) {
test (host, address, shouldSucceed);
int got = SimpleNameService.lookupCalls();
if (got != count) {
throw new RuntimeException ("lookups exp/got: " + count+"/"+got);
}
}
static void sleep (int seconds) {
@ -114,4 +115,16 @@ public class DefaultCaching {
}
}
private static void addMappingToHostsFile (String host,
String addr,
String hostsFileName,
boolean append)
throws Exception {
String mapping = addr + " " + host;
try (PrintWriter hfPWriter = new PrintWriter(new BufferedWriter(
new FileWriter(hostsFileName, append)))) {
hfPWriter.println(mapping);
}
}
}

@ -0,0 +1,3 @@
129.156.220.1 theclub
10.5.18.22 foo
10.5.18.21 luster

@ -1,22 +0,0 @@
# Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
SimpleNameServiceDescriptor # name service provider descriptor

@ -1,102 +0,0 @@
/*
* Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* A simple name service based on an in-memory HashMap.
*/
import java.net.UnknownHostException;
import java.net.InetAddress;
import sun.net.spi.nameservice.*;
import java.util.*;
public final class SimpleNameService implements NameService {
private static LinkedHashMap hosts = new LinkedHashMap();
private static String addrToString(byte addr[]) {
return Byte.toString(addr[0]) + "." +
Byte.toString(addr[1]) + "." +
Byte.toString(addr[2]) + "." +
Byte.toString(addr[3]);
}
// ------------
public static void put(String host, String addr) {
hosts.put(host, addr);
}
public static void put(String host, byte addr[]) {
hosts.put(host, addrToString(addr));
}
public static void remove(String host) {
hosts.remove(host);
}
public static int entries () {
return hosts.size();
}
public static int lookupCalls() {
return lookupCalls;
}
static int lookupCalls = 0;
// ------------
public SimpleNameService() throws Exception {
}
public InetAddress[] lookupAllHostAddr(String host) throws UnknownHostException {
lookupCalls ++;
String value = (String)hosts.get(host);
if (value == null) {
throw new UnknownHostException(host);
}
StringTokenizer st = new StringTokenizer(value, ".");
byte addr[] = new byte[4];
for (int i=0; i<4; i++) {
addr[i] = (byte)Integer.parseInt(st.nextToken());
}
InetAddress[] res = new InetAddress[1];
res[0] = InetAddress.getByAddress(host, addr);
return res;
}
public String getHostByAddr(byte[] addr) throws UnknownHostException {
String addrString = addrToString(addr);
Iterator i = hosts.keySet().iterator();
while (i.hasNext()) {
String host = (String)i.next();
String value = (String)hosts.get(host);
if (value.equals(addrString)) {
return host;
}
}
throw new UnknownHostException();
}
}

@ -1,52 +0,0 @@
/*
* Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* Descriptor for the simple name service
*/
import sun.net.spi.nameservice.*;
public final class SimpleNameServiceDescriptor implements NameServiceDescriptor {
/**
* Create a new instance of the corresponding name service.
*/
public NameService createNameService() throws Exception {
return new SimpleNameService();
}
/**
* Returns this service provider's name
*
*/
public String getProviderName() {
return "sun";
}
/**
* Returns this name service type
* "dns" "nis" etc
*/
public String getType() {
return "simple";
}
}

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2016 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -82,7 +82,7 @@ public class BogusKDC {
// and wrong port for slave KDC
try (PrintWriter w = new PrintWriter(new FileWriter(KRB5_CONF))) {
w.write(String.format(KRB5_CONF_TEMPLATE,
KDC.KDCNameService.NOT_EXISTING_HOST, WRONG_KDC_PORT));
KDC.NOT_EXISTING_HOST, WRONG_KDC_PORT));
w.flush();
}
@ -98,7 +98,7 @@ public class BogusKDC {
// but correct port for slave KDC
try (PrintWriter w = new PrintWriter(new FileWriter(KRB5_CONF))) {
w.write(String.format(KRB5_CONF_TEMPLATE,
KDC.KDCNameService.NOT_EXISTING_HOST, kdc.getPort()));
KDC.NOT_EXISTING_HOST, kdc.getPort()));
w.flush();
}

@ -24,8 +24,7 @@
/*
* @test
* @bug 6578647 6829283
* @modules java.base/sun.net.spi.nameservice
* java.base/sun.security.util
* @modules java.base/sun.security.util
* java.security.jgss/sun.security.jgss
* java.security.jgss/sun.security.krb5
* java.security.jgss/sun.security.krb5.internal

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -35,8 +35,6 @@ import java.time.temporal.TemporalUnit;
import java.util.*;
import java.util.concurrent.*;
import sun.net.spi.nameservice.NameService;
import sun.net.spi.nameservice.NameServiceDescriptor;
import sun.security.krb5.*;
import sun.security.krb5.internal.*;
import sun.security.krb5.internal.ccache.CredentialsCache;
@ -129,6 +127,8 @@ public class KDC {
public static final int DEFAULT_LIFETIME = 39600;
public static final int DEFAULT_RENEWTIME = 86400;
public static String NOT_EXISTING_HOST = "not.existing.host";
// Under the hood.
// The random generator to generate random keys (including session keys)
@ -219,7 +219,8 @@ public class KDC {
};
static {
System.setProperty("sun.net.spi.nameservice.provider.1", "ns,mock");
String hostsFileName = System.getProperty("test.src", ".") + "/TestHosts";
System.setProperty("jdk.net.hosts.file", hostsFileName);
}
/**
@ -1448,45 +1449,6 @@ public class KDC {
}
}
public static class KDCNameService implements NameServiceDescriptor {
public static String NOT_EXISTING_HOST = "not.existing.host";
@Override
public NameService createNameService() throws Exception {
NameService ns = new NameService() {
@Override
public InetAddress[] lookupAllHostAddr(String host)
throws UnknownHostException {
// Everything is localhost except NOT_EXISTING_HOST
if (NOT_EXISTING_HOST.equals(host)) {
throw new UnknownHostException("Unknown host name: "
+ NOT_EXISTING_HOST);
}
return new InetAddress[]{
InetAddress.getByAddress(host, new byte[]{127,0,0,1})
};
}
@Override
public String getHostByAddr(byte[] addr)
throws UnknownHostException {
// No reverse lookup, PrincipalName use original string
throw new UnknownHostException();
}
};
return ns;
}
@Override
public String getProviderName() {
return "mock";
}
@Override
public String getType() {
return "ns";
}
}
// Calling private methods thru reflections
private static final Field getPADataField;

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2016 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -24,6 +24,7 @@
/*
* @test
* @bug 7032354
* @run main/othervm NoAddresses setup
* @run main/othervm NoAddresses 1
* @run main/othervm NoAddresses 2
* @run main/othervm/fail NoAddresses 3
@ -34,12 +35,24 @@ import java.net.InetAddress;
import org.ietf.jgss.ChannelBinding;
import sun.security.jgss.GSSUtil;
import sun.security.krb5.Config;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.BufferedWriter;
public class NoAddresses {
public static void main(String[] args)
throws Exception {
if (args[0].equalsIgnoreCase("setup")) {
// add a mapping of test host name to 127.0.0.1 to test's hosts file
InetAddress localHost = InetAddress.getLocalHost();
String localHostName = localHost.getHostName();
String hostsFileName = System.getProperty("test.src", ".") + "/TestHosts";
String loopBackAddress = "127.0.0.1";
System.setProperty("jdk.net.hosts.file", hostsFileName);
addMappingToHostsFile(localHostName, loopBackAddress, hostsFileName, true);
} else {
OneKDC kdc = new OneKDC(null);
kdc.writeJAASConf();
KDC.saveConfig(OneKDC.KRB5_CONF, kdc,
@ -79,3 +92,16 @@ public class NoAddresses {
Context.handshake(c, s);
}
}
private static void addMappingToHostsFile (String host,
String addr,
String hostsFileName,
boolean append)
throws Exception {
String mapping = addr + " " + host;
try (PrintWriter hfPWriter = new PrintWriter(new BufferedWriter(
new FileWriter(hostsFileName, append)))) {
hfPWriter.println(mapping);
}
}
}

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2016 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -26,8 +26,7 @@
* @bug 8058290
* @summary JAAS Krb5LoginModule has suspect ticket-renewal logic,
* relies on clockskew grace
* @modules java.base/sun.net.spi.nameservice
* java.base/sun.security.util
* @modules java.base/sun.security.util
* java.security.jgss/sun.security.krb5
* java.security.jgss/sun.security.krb5.internal
* java.security.jgss/sun.security.krb5.internal.ccache

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2016 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -45,8 +45,11 @@ public class Renewal {
static OneKDC kdc;
static String clazz = "sun.security.krb5.internal.tools.Kinit";
static String hostsFileName = null;
public static void main(String[] args) throws Exception {
hostsFileName = System.getProperty("test.src", ".") + "/TestHosts";
System.setProperty("jdk.net.hosts.file", hostsFileName);
kdc = new OneKDC(null);
kdc.writeJAASConf();
@ -95,7 +98,7 @@ public class Renewal {
count++;
p.args(OneKDC.USER, new String(OneKDC.PASS))
.inheritIO()
.prop("sun.net.spi.nameservice.provider.1", "ns,mock")
.prop("jdk.net.hosts.file", hostsFileName)
.prop("java.security.krb5.conf", OneKDC.KRB5_CONF)
.env("KRB5CCNAME", "ccache" + count)
.start();
@ -114,10 +117,11 @@ public class Renewal {
}
static void checkKinitRenew() throws Exception {
Proc p = Proc.create(clazz)
.args("-R")
.inheritIO()
.prop("sun.net.spi.nameservice.provider.1", "ns,mock")
.prop("jdk.net.hosts.file", hostsFileName)
.prop("java.security.krb5.conf", OneKDC.KRB5_CONF)
.env("KRB5CCNAME", "ccache" + count)
.start();

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2016 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -84,19 +84,21 @@ public class SSLwithPerms {
).getBytes());
fos.close();
String hostsFileName = System.getProperty("test.src", ".") + "/TestHosts";
Proc pc = Proc.create("SSLwithPerms")
.args("client")
.inheritIO()
.prop("java.security.manager", "")
.prop("java.security.krb5.conf", KRB5_CONF)
.prop("sun.net.spi.nameservice.provider.1", "ns,mock")
.prop("jdk.net.hosts.file", hostsFileName)
.prop("javax.net.ssl", "handshake")
.prop("sun.security.krb5.debug", "true")
.perm(new SecurityPermission("setProperty.jdk.tls.disabledAlgorithms"))
.perm(new PropertyPermission("sun.security.krb5.principal", "read"))
.perm(new FilePermission("port", "read"))
.perm(new FilePermission(hostsFileName, "read"))
.perm(new FilePermission(KTAB, "read"))
.perm(new RuntimePermission("accessClassInPackage.sun.net.spi.nameservice"))
.perm(new AuthPermission("modifyPrincipals"))
.perm(new AuthPermission("modifyPrivateCredentials"))
.perm(new AuthPermission("doAs"))
@ -110,11 +112,13 @@ public class SSLwithPerms {
.prop("java.security.manager", "")
.prop("java.security.krb5.conf", KRB5_CONF)
.prop("java.security.auth.login.config", JAAS_CONF)
.prop("jdk.net.hosts.file", hostsFileName)
.prop("javax.net.ssl", "handshake")
.prop("sun.security.krb5.debug", "true")
.perm(new SecurityPermission("setProperty.jdk.tls.disabledAlgorithms"))
.perm(new AuthPermission("createLoginContext.ssl"))
.perm(new AuthPermission("doAs"))
.perm(new FilePermission(hostsFileName, "read"))
.perm(new FilePermission("port", "write"))
.perm(new SocketPermission("127.0.0.1", "accept"))
.perm(new ServicePermission("host/host.realm@REALM", "accept"))

@ -1,5 +1,4 @@
modules java.base/jdk.internal.misc \
java.base/sun.net.spi.nameservice \
java.base/sun.security.util \
java.security.jgss/sun.security.jgss \
java.security.jgss/sun.security.krb5 \

@ -0,0 +1,16 @@
127.0.0.1 host.rabbit.hole
127.0.0.1 kdc.rabbit.hole
127.0.0.1 kdc.snake.hole
127.0.0.1 kdc.web.domain
127.0.0.1 host.web.domain
127.0.0.1 host.proxy.domain
127.0.0.1 kdc.proxy.domain
127.0.0.1 client.rabbit.hole
127.0.0.1 localhost
127.0.0.1 host.r3.local
127.0.0.1 kdc.r1
127.0.0.1 kdc.r2
127.0.0.1 kdc.r3
127.0.0.1 host.realm
127.0.0.1 realm
127.0.0.1 irlga09

@ -0,0 +1 @@
127.0.0.1 localhost

@ -11,6 +11,7 @@ grant {
permission javax.security.auth.AuthPermission "modifyPrincipals";
permission javax.security.auth.AuthPermission "getSubject";
permission java.util.PropertyPermission "*", "read,write";
permission java.io.FilePermission "/-", "read,write,delete";
permission java.io.FilePermission "*", "read,write,delete";
permission java.lang.RuntimePermission "accessDeclaredMembers";
permission java.lang.reflect.ReflectPermission "suppressAccessChecks";

@ -1,6 +1,7 @@
grant {
permission java.util.PropertyPermission "*", "read,write";
permission java.net.SocketPermission "*:*", "listen,resolve,accept,connect";
permission java.io.FilePermission "/-", "read";
permission java.io.FilePermission "*", "read,write,delete";
permission java.lang.RuntimePermission "accessDeclaredMembers";
permission java.lang.reflect.ReflectPermission "suppressAccessChecks";

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2016 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -24,20 +24,19 @@
* @test
* @bug 6682516
* @summary SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
* @modules java.base/sun.net.spi.nameservice
* java.security.jgss/sun.security.krb5
* @run main/othervm -Dsun.net.spi.nameservice.provider.1=ns,mock -Djava.security.krb5.conf=krb5.conf Test
* @modules java.security.jgss/sun.security.krb5
* @run main/othervm -Djava.security.krb5.conf=krb5.conf Test
*/
import java.net.InetAddress;
import java.net.UnknownHostException;
import sun.net.spi.nameservice.NameService;
import sun.net.spi.nameservice.NameServiceDescriptor;
import sun.security.krb5.PrincipalName;
public class Test implements NameServiceDescriptor {
public class Test {
public static void main(String[] args) throws Exception {
// This config file is generated using Kerberos.app on a Mac
String hostsFileName = System.getProperty("test.src", ".") + "/TestHosts";
System.setProperty("jdk.net.hosts.file", hostsFileName);
System.setProperty("java.security.krb5.realm", "THIS.REALM");
System.setProperty("java.security.krb5.kdc", "localhost");
@ -64,42 +63,4 @@ public class Test implements NameServiceDescriptor {
throw new Exception("Output is " + pn);
}
}
@Override
public NameService createNameService() throws Exception {
NameService ns = new NameService() {
@Override
public InetAddress[] lookupAllHostAddr(String host)
throws UnknownHostException {
// All c<n>.* goes to 127.0.0.n
int i = Integer.valueOf(host.split("\\.")[0].substring(1));
return new InetAddress[]{
InetAddress.getByAddress(host, new byte[]{127,0,0,(byte)i})
};
}
@Override
public String getHostByAddr(byte[] addr)
throws UnknownHostException {
int i = addr[3];
switch (i) {
case 1: return "c1.this.domain"; // Good
case 2: return "127.0.0.2"; // Only IP
case 3: return "d3.this.domain"; // name change
default:
throw new UnknownHostException();
}
}
};
return ns;
}
@Override
public String getProviderName() {
return "mock";
}
@Override
public String getType() {
return "ns";
}
}

@ -0,0 +1,6 @@
127.0.0.1 c1.this.domain
127.0.0.1 c1
127.0.0.2 127.0.0.2
127.0.0.2 c2
127.0.0.3 c3
127.0.0.1 c1.this

@ -44,14 +44,11 @@ import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import sun.net.spi.nameservice.NameService;
import sun.net.spi.nameservice.NameServiceDescriptor;
/*
* @test
* @bug 8134708
* @summary Check if LDAP resources from CRLDP and AIA extensions can be loaded
* @modules java.base/sun.net.spi.nameservice
* @run main/othervm ExtensionsWithLDAP
*/
public class ExtensionsWithLDAP {
@ -149,7 +146,8 @@ public class ExtensionsWithLDAP {
System.setProperty("com.sun.security.enableAIAcaIssuers", "true");
// register a local name service
System.setProperty("sun.net.spi.nameservice.provider.1", "ns,localdns");
String hostsFileName = System.getProperty("test.src", ".") + "/ExtensionsWithLDAPHosts";
System.setProperty("jdk.net.hosts.file", hostsFileName);
X509Certificate trustedCert = loadCertificate(CA_CERT);
X509Certificate eeCert = loadCertificate(EE_CERT);
@ -201,48 +199,8 @@ public class ExtensionsWithLDAP {
}
// a local name service which log requested host names
public static class LocalNameService implements NameServiceDescriptor {
public static class LocalNameService {
static final List<String> requestedHosts = new ArrayList<>();
@Override
public NameService createNameService() throws Exception {
System.out.println("LocalNameService: createNameService() called");
NameService ns = new NameService() {
@Override
public InetAddress[] lookupAllHostAddr(String host)
throws UnknownHostException {
System.out.println("LocalNameService: "
+ "NameService.lookupAllHostAddr(): " + host);
requestedHosts.add(host);
throw new UnknownHostException();
}
@Override
public String getHostByAddr(byte[] addr)
throws UnknownHostException {
System.out.println("LocalNameService: "
+ "NameService.getHostByAddr(): "
+ Arrays.toString(addr));
throw new UnknownHostException("No reverse lookup");
}
};
return ns;
}
@Override
public String getProviderName() {
return "localdns";
}
@Override
public String getType() {
return "ns";
}
}
}