textToNumericFormat.java revision 15491:6f390eafc676
1/*
2 * Copyright (c) 2002, 2016, 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/*
25 * @test
26 * @bug 4749938 8087190
27 * @summary Bug in the parsing IPv4 literal addresses
28 * @run main/othervm  textToNumericFormat
29*/
30
31/**
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 correctly
34 * without needing to call the name service, and the bad tests will
35 * not parse and then invoke the name service, where we expect
36 * the exception.
37 */
38
39import java.net.InetAddress;
40import java.net.UnknownHostException;
41import java.util.*;
42
43public class textToNumericFormat {
44
45    public static void main(String[] args) throws UnknownHostException {
46        List<String> goodList = new ArrayList<>();
47        List<String> badList = new ArrayList<>();
48        String 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" };
55
56        String 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                           "..." };
69        String hostsFileName = System.getProperty("test.src", ".") + "/TestToNumericFormatHosts";
70        System.setProperty("jdk.net.hosts.file", hostsFileName);
71
72        for (int i=0; i<goodAddrs.length; i++) {
73            try {
74                InetAddress ia = InetAddress.getByName(goodAddrs[i]);
75            } catch (UnknownHostException e) {
76                // shouldn't have come here
77                goodList.add(goodAddrs[i]);
78            }
79
80        }
81
82        for (int i=0; i<badAddrs.length; i++) {
83            try {
84                InetAddress ia = InetAddress.getByName(badAddrs[i]);
85                // shouldn't have come here
86                badList.add(badAddrs[i]);
87            } catch (UnknownHostException e) {
88                // ignore
89            }
90
91        }
92
93        // if either goodList or badList is not empty, throw exception
94        if (goodList.size() > 0 || badList.size() > 0) {
95            throw new RuntimeException((goodList.size() > 0?
96                                        ("Good address not parsed: "+ goodList)
97                                        : "") +
98                                       (badList.size() > 0 ?
99                                        ("Bad Address parsed: "+ badList)
100                                        : ""));
101        }
102
103    }
104
105}
106