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 impl.ThrowingLookupsProviderImpl; 28 29 import static impl.ThrowingLookupsProviderImpl.RUNTIME_EXCEPTION_MESSAGE; 30 31 import org.testng.Assert; 32 import org.testng.annotations.Test; 33 34 /* 35 * @test 36 * @summary Test that only UnknownHostException is thrown if resolver 37 * implementation throws RuntimeException during forward or reverse lookup. 38 * @library providers/throwing 39 * @build throwing.lookups.provider/impl.ThrowingLookupsProviderImpl 40 * @run testng/othervm ResolutionWithExceptionTest 41 */ 42 43 public class ResolutionWithExceptionTest { 44 45 @Test 46 public void getByNameUnknownHostException() { 47 ThrowingLookupsProviderImpl.throwRuntimeException = false; 48 runGetByNameTest(); 49 } 50 51 @Test 52 public void getByNameRuntimeException() { 53 ThrowingLookupsProviderImpl.throwRuntimeException = true; 54 runGetByNameTest(); 55 } 56 57 @Test 58 public void getByAddressUnknownHostException() throws UnknownHostException { 59 ThrowingLookupsProviderImpl.throwRuntimeException = false; 60 runGetByAddressTest(); 61 } 62 63 @Test 64 public void getByAddressRuntimeException() throws UnknownHostException { 65 ThrowingLookupsProviderImpl.throwRuntimeException = true; 66 runGetByAddressTest(); 67 } 68 69 private void runGetByNameTest() { 70 // InetAddress.getByName() is expected to throw UnknownHostException in all cases 71 UnknownHostException uhe = Assert.expectThrows(UnknownHostException.class, 72 () -> InetAddress.getByName("doesnt.matter.com")); 73 // If provider is expected to throw RuntimeException - check that UnknownHostException 74 // is set as its cause 75 if (ThrowingLookupsProviderImpl.throwRuntimeException) { 76 Throwable cause = uhe.getCause(); 77 if (cause instanceof RuntimeException re) { 78 // Check RuntimeException message 79 Assert.assertEquals(re.getMessage(), RUNTIME_EXCEPTION_MESSAGE, 80 "incorrect exception message"); 81 } else { 82 Assert.fail("UnknownHostException cause is not RuntimeException"); 83 } 84 } 85 } 86 87 private void runGetByAddressTest() throws UnknownHostException { 88 // getCanonicalHostName is not expected to throw an exception: 89 // if there is an error during reverse lookup operation the literal IP 90 // address String will be returned. 91 String literalIP = InetAddress.getByAddress(new byte[]{1, 2, 3, 4}).getCanonicalHostName(); 92 Assert.assertEquals(literalIP, "1.2.3.4"); 93 } 94 }