test/jdk/java/net/InetAddress/getOriginalHostName.java
author mchung
Tue, 06 Nov 2018 10:01:16 -0800
changeset 52427 3c6aa484536c
parent 47216 71c04702a3d5
permissions -rw-r--r--
8211122: Reduce the number of internal classes made accessible to jdk.unsupported Reviewed-by: alanb, dfuchs, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33301
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
     1
/*
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 47216
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
33301
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
     4
 *
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
     8
 *
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    13
 * accompanied this code).
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    14
 *
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    18
 *
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    21
 * questions.
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    22
 */
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    23
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    24
/*
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    25
 * @test
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    26
 * @bug 8133196
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    27
 * @summary test functionality of getOriginalHostName(InetAddress)
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 47216
diff changeset
    28
 * @modules java.base/jdk.internal.access
33301
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    29
 */
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    30
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    31
import java.net.InetAddress;
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    32
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 47216
diff changeset
    33
import jdk.internal.access.JavaNetInetAddressAccess;
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 47216
diff changeset
    34
import jdk.internal.access.SharedSecrets;
33301
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    35
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    36
public class getOriginalHostName {
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    37
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    38
    private static final JavaNetInetAddressAccess jna =
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    39
        SharedSecrets.getJavaNetInetAddressAccess();
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    40
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    41
    public static void main(String[] args) throws Exception {
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    42
        final String HOST = "dummyserver.java.net";
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    43
        InetAddress ia = null;
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    44
        ia = InetAddress.getByName(HOST);
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    45
        testInetAddress(ia, HOST);
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    46
        ia = InetAddress.getByName("255.255.255.0");
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    47
        testInetAddress(ia, null);
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    48
        ia = InetAddress.getByAddress(new byte[]{1,1,1,1});
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    49
        testInetAddress(ia, null);
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    50
        ia = InetAddress.getLocalHost();
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    51
        testInetAddress(ia, ia.getHostName());
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    52
        ia = InetAddress.getLoopbackAddress();
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    53
        testInetAddress(ia, ia.getHostName());
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    54
    }
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    55
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    56
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    57
    private static void testInetAddress(InetAddress ia, String expected)
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    58
        throws Exception {
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    59
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    60
        System.out.println("Testing InetAddress: " + ia);
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    61
        System.out.println("Expecting original hostname of : " + expected);
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    62
        String origHostName = jna.getOriginalHostName(ia);
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    63
        System.out.println("via JavaNetAccess: " + origHostName);
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    64
        if (origHostName == null && expected != null) {
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    65
            throw new RuntimeException("Unexpected null. Testing:" + expected);
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    66
        } else if (expected != null && !origHostName.equals(expected)) {
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    67
            throw new RuntimeException("Unexpected hostname :" + origHostName);
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    68
        } else if (expected == null && origHostName != null) {
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    69
            throw new RuntimeException("Unexpected origHostName: " + origHostName);
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    70
        }
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    71
    }
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
diff changeset
    72
}