Path: blob/master/test/jdk/java/net/ipv6tests/Tests.java
41149 views
/*1* Copyright (c) 2003, 2016, 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*/2223import jdk.test.lib.NetworkConfiguration;2425import java.io.IOException;26import java.io.InputStream;27import java.io.OutputStream;28import java.net.DatagramPacket;29import java.net.DatagramSocket;30import java.net.Inet4Address;31import java.net.Inet6Address;32import java.net.InetAddress;33import java.net.InetSocketAddress;34import java.net.Socket;35import java.net.SocketAddress;3637public class Tests {3839static final boolean isWindows =40System.getProperty("os.name").startsWith("Windows");41static final boolean isMacOS =42System.getProperty("os.name").contains("OS X");4344/**45* performs a simple exchange of data between the two sockets46* and throws an exception if there is any problem.47*/48public static void simpleDataExchange (Socket s1, Socket s2)49throws Exception {5051InputStream i1 = s1.getInputStream();52InputStream i2 = s2.getInputStream();53OutputStream o1 = s1.getOutputStream();54OutputStream o2 = s2.getOutputStream();5556startSimpleWriter("SimpleWriter-1", o1, 100);57startSimpleWriter("SimpleWriter-2", o2, 200);58simpleRead (i2, 100);59simpleRead (i1, 200);60}6162static void startSimpleWriter(String threadName, final OutputStream os, final int start) {63(new Thread(new Runnable() {64public void run() {65try { simpleWrite(os, start); }66catch (Exception e) {unexpected(e); }67}}, threadName)).start();68}6970static void unexpected(Exception e ) {71System.out.println("Unexcepted Exception: " + e);72e.printStackTrace();73}7475/**76* Send a packet from s1 to s2 (ia2/s2.localPort) and check it77* Send a packet from s2 to s1 (ia1/s1.localPort) and check it78*/79public static void simpleDataExchange (DatagramSocket s1, InetAddress ia1,80DatagramSocket s2, InetAddress ia2)81throws Exception {8283SocketAddress dest1 = new InetSocketAddress (ia1, s1.getLocalPort());84dprintln ("dest1 = " + dest1);85SocketAddress dest2 = new InetSocketAddress (ia2, s2.getLocalPort());86dprintln ("dest2 = " + dest2);8788byte[] ba = "Hello world".getBytes();89byte[] bb = "HELLO WORLD1".getBytes();90DatagramPacket p1 = new DatagramPacket (ba, ba.length, dest1);91DatagramPacket p2 = new DatagramPacket (ba, ba.length, dest2);9293DatagramPacket r1 = new DatagramPacket (new byte[256], 256);94DatagramPacket r2 = new DatagramPacket (new byte[256], 256);9596s2.send (p1);97s1.send (p2);98s1.receive (r1);99s2.receive (r2);100comparePackets (p1, r1);101comparePackets (p2, r2);102}103104/**105* Send a packet from s1 to s2 (ia2/s2.localPort) and send same packet106* back from s2 to sender. Check s1 receives original packet107*/108109public static void datagramEcho (DatagramSocket s1, DatagramSocket s2,110InetAddress ia2)111throws Exception {112113byte[] ba = "Hello world".getBytes();114DatagramPacket p1;115116SocketAddress dest2 = null;117if (ia2 != null) {118dest2 = new InetSocketAddress (ia2, s2.getLocalPort());119p1 = new DatagramPacket (ba, ba.length, dest2);120} else {121p1 = new DatagramPacket (ba, ba.length);122}123124dprintln ("dest2 = " + dest2);125126127DatagramPacket r1 = new DatagramPacket (new byte[256], 256);128DatagramPacket r2 = new DatagramPacket (new byte[256], 256);129130s1.send (p1);131s2.receive (r1);132s2.send (r1);133s1.receive (r2);134comparePackets (p1, r1);135comparePackets (p1, r2);136}137138public static void comparePackets (DatagramPacket p1, DatagramPacket p2)139throws Exception {140141byte[] b1 = p1.getData();142byte[] b2 = p2.getData();143int len = p1.getLength () > p2.getLength() ? p2.getLength()144: p1.getLength();145for (int i=0; i<len; i++) {146if (b1[i] != b2[i]) {147throw new Exception ("packets not the same");148}149}150}151152/* check the time got is within 50% of the time expected */153public static void checkTime (long got, long expected) {154checkTime(got, expected, expected);155}156157/* check the time got is between start and end, given 50% tolerance */158public static void checkTime(long got, long start, long end) {159dprintln("checkTime: got = " + got + " start = " + start + " end = " + end);160long upper = end + (end / 2);161long lower = start - (start / 2);162if (got > upper || got < lower) {163throw new RuntimeException("checkTime failed: got " + got164+ ", expected between " + start + " and " + end);165}166}167168static boolean debug = false;169170public static void checkDebug (String[] args) {171debug = args.length > 0 && args[0].equals("-d");172}173174public static void dprint (String s) {175if (debug) {176System.out.print (s);177}178}179180public static void dprintln (String s) {181if (debug) {182System.out.println (s);183}184}185186public static Inet4Address getFirstLocalIPv4Address () {187return getNetworkConfig().ip4Addresses()188.filter(a -> !a.isLoopbackAddress())189.findFirst()190.orElse(null);191}192193public static Inet6Address getFirstLocalIPv6Address () {194return getNetworkConfig().ip6Addresses()195.filter(a -> !a.isLoopbackAddress())196.findFirst()197.orElse(null);198}199200private static NetworkConfiguration getNetworkConfig() {201try {202return NetworkConfiguration.probe();203} catch (IOException e) {204System.out.println("Failed to probe NetworkConfiguration");205throw new RuntimeException(e);206}207}208209/**210* Throws a RuntimeException if the boolean condition is false211*/212public static void t_assert (boolean assertion) {213if (assertion) {214return;215}216Throwable t = new Throwable();217StackTraceElement[] strace = t.getStackTrace();218String msg = "Assertion failed at: " + strace[1].toString();219throw new RuntimeException (msg);220}221222private static void simpleRead (InputStream is, int start) throws Exception {223byte b[] = new byte [2];224for (int i=start; i<start+100; i++) {225int x = is.read (b);226if (x == 1) {227x += is.read (b,1,1);228}229if (x!=2) {230throw new Exception ("read error");231}232int r = bytes (b[0], b[1]);233if (r != i) {234throw new Exception ("read " + r + " expected " +i);235}236}237}238239/* convert signed bytes to unisigned int */240private static int bytes (byte b1, byte b2) {241int i1 = (int)b1 & 0xFF;242int i2 = (int)b2 & 0xFF;243return i1 * 256 + i2;244}245246static void simpleWrite (OutputStream os, int start) throws Exception {247byte b[] = new byte [2];248for (int i=start; i<start+100; i++) {249b[0] = (byte) (i / 256);250b[1] = (byte) (i % 256);251os.write (b);252}253}254255private static class Runner extends Thread {256Runnable runnee;257long delay;258259Runner (Runnable runnee, long delay) {260super();261this.runnee = runnee;262this.delay = delay;263}264265public void run () {266try {267Thread.sleep (delay);268runnee.run ();269} catch (Exception e) {270e.printStackTrace();271}272}273}274275/*276* Take the given Runnable and run it in a spawned thread277* after the given time has elapsed. runAfter() returns immediately278*/279public static void runAfter (long millis, Runnable runnee) {280Runner runner = new Runner (runnee, millis);281runner.start ();282}283284static String osname;285286static {287osname = System.getProperty ("os.name");288}289290static boolean isLinux () {291return osname.equals ("Linux");292}293294static boolean isWindows () {295return osname.startsWith ("Windows");296}297}298299300