Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Socket/LinkLocal.java
38821 views
/*1* Copyright (c) 2001, 2010, 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 446986626* @summary Connecting to a link-local IPv6 address should not27* causes a SocketException to be thrown.28*/29import java.net.*;30import java.util.Enumeration;3132public class LinkLocal {3334static int testCount = 0;35static int failed = 0;3637static void TcpTest(InetAddress ia) throws Exception {38System.out.println("**************************************");39testCount++;40System.out.println("Test " + testCount + ": TCP connect to " + ia);4142/*43* Create ServerSocket on wildcard address and then44* try to connect Socket to link-local address.45*/46ServerSocket ss = new ServerSocket(0);4748Socket s = new Socket();49try {50s.connect(new InetSocketAddress(ia, ss.getLocalPort()));5152System.out.println("Test passed - connection established.");5354// connection was established so accept it55Socket s2 = ss.accept();56s2.close();57} catch (SocketException e) {58failed++;59System.out.println("Test failed: " + e);60} finally {61s.close();62ss.close();63}64}6566static void UdpTest(InetAddress ia, boolean connected) throws Exception {6768System.out.println("**************************************");69testCount++;7071if (connected) {72System.out.println("Test " + testCount + ": UDP connect to " + ia);73} else {74System.out.println("Test " + testCount + ": UDP send to " + ia);75}7677DatagramSocket ds1 = new DatagramSocket();78DatagramSocket ds2 = new DatagramSocket();7980try {81byte b[] = "Hello".getBytes();82DatagramPacket p = new DatagramPacket(b, b.length);8384if (connected) {85ds1.connect( new InetSocketAddress(ia, ds2.getLocalPort()) );86System.out.println("DatagramSocket connected.");87} else {88p.setAddress(ia);89p.setPort(ds2.getLocalPort());90}91ds1.send(p);92System.out.println("Packet has been sent.");9394ds2.setSoTimeout(5000);95ds2.receive(p);96System.out.println("Test passed - packet received.");97} catch (SocketException e) {98failed++;99System.out.println("Test failed: " + e);100} finally {101ds1.close();102ds2.close();103}104}105106static void TestAddress(InetAddress ia) throws Exception {107TcpTest(ia);108UdpTest(ia, true); /* unconnected */109UdpTest(ia, false); /* connected */110}111112public static void main(String args[]) throws Exception {113114/*115* If an argument is provided ensure that it's116* a link-local IPv6 address.117*/118if (args.length > 0) {119InetAddress ia = InetAddress.getByName(args[0]);120121if ( !(ia instanceof Inet6Address) ||122!ia.isLinkLocalAddress()) {123throw new Exception(ia +124" is not a link-local IPv6 address");125}126127TestAddress(ia);128}129130/*131* If no argument is provided then enumerate the132* local addresses and run the test on each link-local133* IPv6 address.134*/135if (args.length == 0) {136Enumeration nifs = NetworkInterface.getNetworkInterfaces();137while (nifs.hasMoreElements()) {138NetworkInterface ni = (NetworkInterface)nifs.nextElement();139if (!ni.isUp())140continue;141142Enumeration addrs = ni.getInetAddresses();143while (addrs.hasMoreElements()) {144InetAddress addr = (InetAddress)addrs.nextElement();145146if (addr instanceof Inet6Address &&147addr.isLinkLocalAddress()) {148149TestAddress(addr);150}151}152}153}154155/*156* Print results157*/158if (testCount == 0) {159System.out.println("No link-local IPv6 addresses - test skipped!");160} else {161System.out.println("**************************************");162System.out.println(testCount + " test(s) executed, " +163failed + " failed.");164if (failed > 0) {165throw new Exception( failed + " test(s) failed.");166}167}168}169}170171172