1 /*
  2  * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 import java.net.InetAddress;
 25 import java.net.UnknownHostException;
 26 
 27 import org.testng.Assert;
 28 import org.testng.annotations.Test;
 29 
 30 import impl.SimpleResolverProviderImpl;
 31 
 32 
 33 /*
 34  * @test
 35  * @summary Test that InetAddress caching security properties work as expected
 36  *  when a custom resolver is installed.
 37  * @library lib providers/simple
 38  * @build test.library/testlib.ResolutionRegistry
 39  *  simple.provider/impl.SimpleResolverProviderImpl AddressesCachingTest
 40  * @run testng/othervm -Djava.security.properties=${test.src}/NeverCache.props
 41  *  -Dtest.cachingDisabled=true AddressesCachingTest
 42  * @run testng/othervm -Djava.security.properties=${test.src}/ForeverCache.props
 43  *  -Dtest.cachingDisabled=false AddressesCachingTest
 44  */
 45 public class AddressesCachingTest {
 46 
 47     @Test
 48     public void testPositiveCaching() {
 49         boolean observedTwoLookups = performLookups(false);
 50         if (CACHING_DISABLED) {
 51             Assert.assertTrue(observedTwoLookups,
 52                     "Two positive lookups are expected with caching disabled");
 53         } else {
 54             Assert.assertFalse(observedTwoLookups,
 55                     "Only one positive lookup is expected with caching enabled");
 56         }
 57     }
 58 
 59     @Test
 60     public void testNegativeCaching() {
 61         boolean observedTwoLookups = performLookups(true);
 62         if (CACHING_DISABLED) {
 63             Assert.assertTrue(observedTwoLookups,
 64                     "Two negative lookups are expected with caching disabled");
 65         } else {
 66             Assert.assertFalse(observedTwoLookups,
 67                     "Only one negative lookup is expected with caching enabled");
 68         }
 69     }
 70 
 71     /*
 72      * Performs two subsequent positive or negative lookups.
 73      * Returns true if the timestamp of this lookups differs,
 74      * false otherwise.
 75      */
 76     private static boolean performLookups(boolean performNegativeLookup) {
 77         doLookup(performNegativeLookup);
 78         long firstTimestamp = SimpleResolverProviderImpl.getLastLookupTimestamp();
 79         doLookup(performNegativeLookup);
 80         long secondTimestamp = SimpleResolverProviderImpl.getLastLookupTimestamp();
 81         return firstTimestamp != secondTimestamp;
 82     }
 83 
 84     // Performs negative or positive lookup.
 85     // It is a test error if UnknownHostException is thrown during positive lookup.
 86     // It is a test error if UnknownHostException is NOT thrown during negative lookup.
 87     private static void doLookup(boolean performNegativeLookup) {
 88         String hostName = performNegativeLookup ? "notKnowHost.org" : "javaTest.org";
 89         try {
 90             InetAddress.getByName(hostName);
 91             if (performNegativeLookup) {
 92                 Assert.fail("Host name is expected to get unresolved");
 93             }
 94         } catch (UnknownHostException uhe) {
 95             if (!performNegativeLookup) {
 96                 Assert.fail("Host name is expected to get resolved");
 97             }
 98         }
 99     }
100 
101     // Helper system property that signals to the test if both negative and positive
102     // caches are disabled.
103     private static final boolean CACHING_DISABLED = Boolean.getBoolean("test.cachingDisabled");
104 }