Path: blob/master/test/jdk/java/net/Socket/LinkLocal.java
41152 views
/*1* Copyright (c) 2001, 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/*24* @test25* @bug 446986626* @library /test/lib27* @summary Connecting to a link-local IPv6 address should not28* causes a SocketException to be thrown.29* @library /test/lib30* @build jdk.test.lib.NetworkConfiguration31* jdk.test.lib.Platform32* @run main LinkLocal33* @run main/othervm -Djava.net.preferIPv4Stack=true LinkLocal34*/3536import java.net.*;37import java.util.List;38import java.util.stream.Collectors;3940import jdk.test.lib.NetworkConfiguration;41import jdk.test.lib.net.IPSupport;4243public class LinkLocal {4445static int testCount = 0;46static int failed = 0;4748static void TcpTest(InetAddress ia) throws Exception {49System.out.println("**************************************");50testCount++;51System.out.println("Test " + testCount + ": TCP connect to " + ia);5253/*54* Create ServerSocket on wildcard address and then55* try to connect Socket to link-local address.56*/57ServerSocket ss = new ServerSocket();58ss.bind(new InetSocketAddress(ia, 0));5960Socket s = new Socket();61try {62s.connect(new InetSocketAddress(ia, ss.getLocalPort()));6364System.out.println("Test passed - connection established.");6566// connection was established so accept it67Socket s2 = ss.accept();68s2.close();69} catch (SocketException e) {70failed++;71System.out.println("Test failed: " + e);72} finally {73s.close();74ss.close();75}76}7778static void UdpTest(InetAddress ia, boolean connected) throws Exception {7980System.out.println("**************************************");81testCount++;8283if (connected) {84System.out.println("Test " + testCount + ": UDP connect to " + ia);85} else {86System.out.println("Test " + testCount + ": UDP send to " + ia);87}8889DatagramSocket ds1 = new DatagramSocket();90DatagramSocket ds2 = new DatagramSocket(0, ia);9192try {93byte b[] = "Hello".getBytes();94DatagramPacket p = new DatagramPacket(b, b.length);9596if (connected) {97ds1.connect( new InetSocketAddress(ia, ds2.getLocalPort()) );98System.out.println("DatagramSocket connected.");99} else {100p.setAddress(ia);101p.setPort(ds2.getLocalPort());102}103ds1.send(p);104System.out.println("Packet has been sent.");105106ds2.setSoTimeout(5000);107ds2.receive(p);108System.out.println("Test passed - packet received.");109} catch (SocketException e) {110failed++;111System.out.println("Test failed: " + e);112} finally {113ds1.close();114ds2.close();115}116}117118static void TestAddress(InetAddress ia) throws Exception {119TcpTest(ia);120UdpTest(ia, true); /* unconnected */121UdpTest(ia, false); /* connected */122}123124public static void main(String args[]) throws Exception {125IPSupport.throwSkippedExceptionIfNonOperational();126127/*128* If an argument is provided ensure that it's129* a link-local IPv6 address.130*/131if (args.length > 0) {132InetAddress ia = InetAddress.getByName(args[0]);133134if ( !(ia instanceof Inet6Address) ||135!ia.isLinkLocalAddress()) {136throw new Exception(ia +137" is not a link-local IPv6 address");138}139140TestAddress(ia);141}142143/*144* If no argument is provided then enumerate the145* local addresses and run the test on each link-local146* IPv6 address.147*/148if (args.length == 0) {149List<Inet6Address> addrs = NetworkConfiguration.probe()150.ip6Addresses()151.filter(Inet6Address::isLinkLocalAddress)152.collect(Collectors.toList());153154for (Inet6Address addr : addrs) {155TestAddress(addr);156}157}158159/*160* Print results161*/162if (testCount == 0) {163System.out.println("No link-local IPv6 addresses - test skipped!");164} else {165System.out.println("**************************************");166System.out.println(testCount + " test(s) executed, " +167failed + " failed.");168if (failed > 0) {169throw new Exception( failed + " test(s) failed.");170}171}172}173}174175176