Path: blob/master/test/jdk/java/net/InetAddress/CheckJNI.java
41149 views
/*1* Copyright (c) 2003, 2019, 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/* @test24* @bug 4889870 489003325* @summary java -Xcheck:jni failing in net code on Solaris / [Datagram]Socket.getLocalAddress() failure26* @library /test/lib27* @build jdk.test.lib.NetworkConfiguration28* jdk.test.lib.Platform29* @run main/othervm -Xcheck:jni CheckJNI30*/3132import java.net.*;33import java.util.*;34import java.util.stream.Collectors;35import jdk.test.lib.NetworkConfiguration;36import jdk.test.lib.net.IPSupport;3738public class CheckJNI {39public static void main (String[] args) throws Exception {40/* try to invoke as much java.net native code as possible */4142InetAddress loopback = InetAddress.getLoopbackAddress();43System.out.println("Testing loopback Socket/ServerSocket");44testSocket(loopback);4546System.out.println("Testing loopback DatagramSocket");47testDatagram(loopback);4849if (IPSupport.hasIPv4()) {50InetAddress loopback4 = InetAddress.getByName("127.0.0.1");51System.out.println("Testing IPv4 Socket/ServerSocket");52testSocket(loopback4);5354System.out.println("Testing IPv4 DatagramSocket");55testDatagram(loopback4);56}5758/* Find link local IPv6 addrs to test */59List<Inet6Address> addrs = NetworkConfiguration.probe()60.ip6Addresses()61.filter(Inet6Address::isLinkLocalAddress)62.collect(Collectors.toList());6364for (Inet6Address ia6 : addrs) {65System.out.println("Address:" + ia6);66System.out.println("Testing IPv6 Socket");67testSocket(ia6);6869System.out.println("Testing IPv6 DatagramSocket");70testDatagram(ia6);71}72System.out.println("OK");73}7475static void testSocket(InetAddress ia) throws Exception {76ServerSocket server = new ServerSocket(0, 0, ia);77Socket s = new Socket(ia, server.getLocalPort());78s.close();79server.close();80}8182static void testDatagram(InetAddress ia) throws Exception {83DatagramSocket s1 = new DatagramSocket(0, ia);84DatagramSocket s2 = new DatagramSocket(0, ia);85System.out.println("s1: local address=" + s1.getLocalAddress()86+ ", local port=" + s1.getLocalPort());87System.out.println("s2: local address=" + s2.getLocalAddress()88+ ", local port=" + s2.getLocalPort());8990DatagramPacket p1 = new DatagramPacket (91"hello world".getBytes(),920, "hello world".length(), s2.getLocalAddress(),93s2.getLocalPort()94);9596DatagramPacket p2 = new DatagramPacket(new byte[128], 128);97s1.send(p1);98s2.receive(p2);99s1.close();100s2.close();101}102}103104105