Path: blob/master/test/jdk/java/net/Inet4Address/textToNumericFormat.java
41149 views
/*1* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4749938 808719026* @summary Bug in the parsing IPv4 literal addresses27* @run main/othervm -Djdk.net.hosts.file=HostFileDoesNotExist textToNumericFormat28*/2930/**31* We use a dummy name service which throws UHE any time it is called.32* We do this because the "good" tests here should parse correctly33* without needing to call the name service, and the bad tests will34* not parse and then invoke the name service, where we expect35* the exception.36*/3738import java.net.InetAddress;39import java.net.UnknownHostException;40import java.util.*;4142public class textToNumericFormat {4344public static void main(String[] args) throws UnknownHostException {45List<String> goodList = new ArrayList<>();46List<String> badList = new ArrayList<>();47String goodAddrs[] = {48"224.0.1.0",49"238.255.255.255",50"239.255.255.255",51"239.255.65535",52"239.16777215",53"4294967295" };5455String badAddrs[] = {56"238.255.255.2550",57"256.255.255.255",58"238.255.2550.255",59"238.2550.255.255",60"2380.255.255.255",61"239.255.65536",62"239.16777216",63"4294967296",64".1.1.1",65"1..1.1",66"1.1.1.",67"..." };6869for (int i=0; i<goodAddrs.length; i++) {70try {71// Value is an IP Address literal, Name Service will not be called72InetAddress ia = InetAddress.getByName(goodAddrs[i]);73} catch (UnknownHostException e) {74// shouldn't have come here75goodList.add(goodAddrs[i]);76}7778}7980for (int i=0; i<badAddrs.length; i++) {81try {82// Value is not an IP Address literal, Name Service will be called83InetAddress ia = InetAddress.getByName(badAddrs[i]);84// shouldn't have come here85badList.add(badAddrs[i]);86} catch (UnknownHostException e) {87// ignore88}8990}9192// if either goodList or badList is not empty, throw exception93if (goodList.size() > 0 || badList.size() > 0) {94throw new RuntimeException((goodList.size() > 0?95("Good address not parsed: "+ goodList)96: "") +97(badList.size() > 0 ?98("Bad Address parsed: "+ badList)99: ""));100}101102}103104}105106107