0N/A/*
3909N/A * Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 4292867
0N/A * @summary Check that InetAddress doesn't continue to throw UHE
0N/A * after the name service has recovered and the negative ttl
0N/A * on the initial lookup has expired.
3406N/A * @compile -XDignore.symbol.file=true SimpleNameService.java
3406N/A * SimpleNameServiceDescriptor.java
0N/A * @run main/othervm/timeout=200 -Dsun.net.spi.nameservice.provider.1=simple,sun CacheTest
0N/A */
0N/Aimport java.net.InetAddress;
0N/Aimport java.net.UnknownHostException;
0N/Aimport java.security.Security;
0N/A
0N/Apublic class CacheTest {
0N/A
0N/A
0N/A public static void main(String args[]) throws Exception {
0N/A
0N/A /*
0N/A * First check the ttl on negative lookups is in the <15 second
0N/A * range. If the ttl is <=0 it means we cache forever or always
0N/A * consult the name service. For ttl > 15 the test would take
0N/A * too long so we skip it (need to coordinate jtreg timeout
0N/A * with negative ttl)
0N/A */
0N/A String ttlProp = "networkaddress.cache.negative.ttl";
0N/A int ttl = 0;
0N/A String policy = Security.getProperty(ttlProp);
0N/A if (policy != null) {
0N/A ttl = Integer.parseInt(policy);
0N/A }
0N/A if (ttl <= 0 || ttl > 15) {
0N/A System.err.println("Security property " + ttlProp + " needs to " +
0N/A " in 1-15 second range to execute this test");
0N/A return;
0N/A
0N/A }
0N/A
0N/A /*
0N/A * The following outlines how the test works :-
0N/A *
0N/A * 1. Do a lookup via InetAddress.getByName that it guaranteed
0N/A * to succeed. This forces at least one entry into the cache
0N/A * that will not expire.
0N/A *
0N/A * 2. Do a lookup via InetAddress.getByName that is guarnateed
0N/A * to fail. This results in a negative lookup cached for
0N/A * for a short period to time.
0N/A *
0N/A * 3. Wait for the cache entry to expire.
0N/A *
0N/A * 4. Do a lookup (which should consult the name service) and
0N/A * the lookup should succeed.
0N/A */
0N/A
0N/A // name service needs to resolve this.
0N/A SimpleNameService.put("theclub", "129.156.220.219");
0N/A
0N/A // this lookup will succeed
0N/A InetAddress.getByName("theclub");
0N/A
0N/A // lookup "luster" - this should throw UHE as name service
0N/A // doesn't know anything about this host.
0N/A
0N/A try {
0N/A InetAddress.getByName("luster");
0N/A throw new RuntimeException("Test internal error " +
0N/A " - luster is bring resolved by name service");
0N/A } catch (UnknownHostException x) {
0N/A }
0N/A
0N/A // name service now needs to know about luster
0N/A SimpleNameService.put("luster", "10.5.18.21");
0N/A
0N/A // wait for the cache entry to expire and lookup should
0N/A // succeed.
0N/A Thread.currentThread().sleep(ttl*1000 + 1000);
0N/A InetAddress.getByName("luster");
0N/A }
0N/A
0N/A}