Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Inet4Address/textToNumericFormat.java
47178 views
/*1* Copyright (c) 2002, 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* @compile -XDignore.symbol.file=true DummyNameService.java DummyNameServiceDescriptor.java28* @run main/othervm -Dsun.net.spi.nameservice.provider.1=dummy,oracle textToNumericFormat29*/3031/**32* We use a dummy name service which throws UHE any time it is called.33* We do this because the "good" tests here should parse correctly34* without needing to call the name service, and the bad tests will35* not parse and then invoke the name service, where we expect36* the exception.37*/3839import java.net.InetAddress;40import java.net.UnknownHostException;41import java.util.*;4243public class textToNumericFormat {4445public static void main(String[] args) throws UnknownHostException {46List<String> goodList = new ArrayList<>();47List<String> badList = new ArrayList<>();48String goodAddrs[] = {49"224.0.1.0",50"238.255.255.255",51"239.255.255.255",52"239.255.65535",53"239.16777215",54"4294967295" };5556String badAddrs[] = {57"238.255.255.2550",58"256.255.255.255",59"238.255.2550.255",60"238.2550.255.255",61"2380.255.255.255",62"239.255.65536",63"239.16777216",64"4294967296",65".1.1.1",66"1..1.1",67"1.1.1.",68"..." };6970for (int i=0; i<goodAddrs.length; i++) {71try {72InetAddress 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 {82InetAddress ia = InetAddress.getByName(badAddrs[i]);83// shouldn't have come here84badList.add(badAddrs[i]);85} catch (UnknownHostException e) {86// ignore87}8889}9091// if either goodList or badList is not empty, throw exception92if (goodList.size() > 0 || badList.size() > 0) {93throw new RuntimeException((goodList.size() > 0?94("Good address not parsed: "+ goodList)95: "") +96(badList.size() > 0 ?97("Bad Address parsed: "+ badList)98: ""));99}100101}102103}104105106