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 impl.DelegatingProviderImpl;
25 import org.testng.Assert;
26 import org.testng.annotations.Test;
27 
28 import java.net.InetAddress;
29 import java.net.UnknownHostException;
30 
31 import static impl.DelegatingProviderImpl.changeReverseLookupAddress;
32 import static impl.DelegatingProviderImpl.lastReverseLookupThrowable;
33 
34 /*
35  * @test
36  * @summary checks delegation of illegal reverse lookup request to the built-in
37  *  InetAddressResolver.
38  * @library providers/delegating
39  * @build delegating.provider/impl.DelegatingProviderImpl
40  * @run testng/othervm ReverseLookupDelegationTest
41  */
42 public class ReverseLookupDelegationTest {
43 
44     @Test
45     public void delegateHostNameLookupWithWrongByteArray() throws UnknownHostException {
46         // The underlying resolver implementation will ignore the supplied
47         // byte array and will replace it with byte array of incorrect size.
48         changeReverseLookupAddress = true;
49         String canonicalHostName = InetAddress.getByAddress(new byte[]{1, 2, 3, 4}).getCanonicalHostName();
50         // Output canonical host name and the exception thrown by the built-in resolver
51         System.err.println("Canonical host name:" + canonicalHostName);
52         System.err.println("Exception thrown by the built-in resolver:" + lastReverseLookupThrowable);
53 
54         // Check that originally supplied byte array was used to construct canonical host name after
55         // failed reverse lookup.
56         Assert.assertEquals("1.2.3.4", canonicalHostName, "unexpected canonical hostname");
57 
58         // Check that on a provider side the IllegalArgumentException has been thrown by the built-in resolver
59         Assert.assertTrue(lastReverseLookupThrowable instanceof IllegalArgumentException,
60                 "wrong exception type is thrown by the built-in resolver");
61     }
62 }