Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/ipv6tests/Tests.java
38812 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 java.net.*;24import java.io.*;25import java.util.*;2627public class Tests {28/**29* performs a simple exchange of data between the two sockets30* and throws an exception if there is any problem.31*/32public static void simpleDataExchange (Socket s1, Socket s2)33throws Exception {3435InputStream i1 = s1.getInputStream();36InputStream i2 = s2.getInputStream();37OutputStream o1 = s1.getOutputStream();38OutputStream o2 = s2.getOutputStream();3940startSimpleWriter("SimpleWriter-1", o1, 100);41startSimpleWriter("SimpleWriter-2", o2, 200);42simpleRead (i2, 100);43simpleRead (i1, 200);44}4546static void startSimpleWriter(String threadName, final OutputStream os, final int start) {47(new Thread(new Runnable() {48public void run() {49try { simpleWrite(os, start); }50catch (Exception e) {unexpected(e); }51}}, threadName)).start();52}5354static void unexpected(Exception e ) {55System.out.println("Unexcepted Exception: " + e);56e.printStackTrace();57}5859/**60* Send a packet from s1 to s2 (ia2/s2.localPort) and check it61* Send a packet from s2 to s1 (ia1/s1.localPort) and check it62*/63public static void simpleDataExchange (DatagramSocket s1, InetAddress ia1,64DatagramSocket s2, InetAddress ia2)65throws Exception {6667SocketAddress dest1 = new InetSocketAddress (ia1, s1.getLocalPort());68dprintln ("dest1 = " + dest1);69SocketAddress dest2 = new InetSocketAddress (ia2, s2.getLocalPort());70dprintln ("dest2 = " + dest2);7172byte[] ba = "Hello world".getBytes();73byte[] bb = "HELLO WORLD1".getBytes();74DatagramPacket p1 = new DatagramPacket (ba, ba.length, dest1);75DatagramPacket p2 = new DatagramPacket (ba, ba.length, dest2);7677DatagramPacket r1 = new DatagramPacket (new byte[256], 256);78DatagramPacket r2 = new DatagramPacket (new byte[256], 256);7980s2.send (p1);81s1.send (p2);82s1.receive (r1);83s2.receive (r2);84comparePackets (p1, r1);85comparePackets (p2, r2);86}8788/**89* Send a packet from s1 to s2 (ia2/s2.localPort) and send same packet90* back from s2 to sender. Check s1 receives original packet91*/9293public static void datagramEcho (DatagramSocket s1, DatagramSocket s2,94InetAddress ia2)95throws Exception {9697byte[] ba = "Hello world".getBytes();98DatagramPacket p1;99100SocketAddress dest2 = null;101if (ia2 != null) {102dest2 = new InetSocketAddress (ia2, s2.getLocalPort());103p1 = new DatagramPacket (ba, ba.length, dest2);104} else {105p1 = new DatagramPacket (ba, ba.length);106}107108dprintln ("dest2 = " + dest2);109110111DatagramPacket r1 = new DatagramPacket (new byte[256], 256);112DatagramPacket r2 = new DatagramPacket (new byte[256], 256);113114s1.send (p1);115s2.receive (r1);116s2.send (r1);117s1.receive (r2);118comparePackets (p1, r1);119comparePackets (p1, r2);120}121122public static void comparePackets (DatagramPacket p1, DatagramPacket p2)123throws Exception {124125byte[] b1 = p1.getData();126byte[] b2 = p2.getData();127int len = p1.getLength () > p2.getLength() ? p2.getLength()128: p1.getLength();129for (int i=0; i<len; i++) {130if (b1[i] != b2[i]) {131throw new Exception ("packets not the same");132}133}134}135136/* check the time got is within 50% of the time expected */137public static void checkTime (long got, long expected) {138checkTime(got, expected, expected);139}140141/* check the time got is between start and end, given 50% tolerance */142public static void checkTime(long got, long start, long end) {143dprintln("checkTime: got = " + got + " start = " + start + " end = " + end);144long upper = end + (end / 2);145long lower = start - (start / 2);146if (got > upper || got < lower) {147throw new RuntimeException("checkTime failed: got " + got148+ ", expected between " + start + " and " + end);149}150}151152static boolean debug = false;153154public static void checkDebug (String[] args) {155debug = args.length > 0 && args[0].equals("-d");156}157158public static void dprint (String s) {159if (debug) {160System.out.print (s);161}162}163164public static void dprintln (String s) {165if (debug) {166System.out.println (s);167}168}169170static int numberInterfaces () {171try {172Enumeration ifs = NetworkInterface.getNetworkInterfaces();173int nifs=0;174while (ifs.hasMoreElements()) {175nifs++;176ifs.nextElement();177}178return nifs;179} catch (SocketException e) {180return 0;181}182}183184public static Enumeration ipv4Addresses() {185return new AddrEnum (Inet4Address.class);186}187188public static Inet4Address getFirstLocalIPv4Address () {189Enumeration e = ipv4Addresses();190if (!e.hasMoreElements()) {191return null;192}193return (Inet4Address)e.nextElement();194}195196public static Inet6Address getFirstLocalIPv6Address () {197Enumeration e = ipv6Addresses();198if (!e.hasMoreElements()) {199return null;200}201return (Inet6Address)e.nextElement();202}203204public static Enumeration ipv6Addresses() {205return new AddrEnum (Inet6Address.class);206}207208/* enumerates the Inet4Addresses or Inet6Addresses on the system */209210private static class AddrEnum implements Enumeration {211212Enumeration ifs;213NetworkInterface currIf = null;214InetAddress nextAddr=null;215Enumeration addrs=null;216Class filter;217218static final byte[] fe80_loopback = new byte [] {219(byte)0xfe,(byte)0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,1220};221222AddrEnum (Class filter) {223this.filter = filter;224try {225ifs = NetworkInterface.getNetworkInterfaces();226} catch (SocketException e) {}227}228229public boolean hasMoreElements () {230if (nextAddr == null) {231nextAddr = getNext();232}233return (nextAddr != null);234}235236public Object nextElement () {237if (!hasMoreElements()) {238throw new NoSuchElementException ("no more addresses");239}240Object next = nextAddr;241nextAddr = null;242return next;243}244245private InetAddress getNext() {246while (true) {247if (currIf == null) {248currIf = getNextIf();249if (currIf == null) {250return null;251}252addrs = currIf.getInetAddresses();253}254while (addrs.hasMoreElements()) {255InetAddress addr = (InetAddress) addrs.nextElement();256if (filter.isInstance (addr) && !addr.isLoopbackAddress()257&& !addr.isAnyLocalAddress()) {258if (Arrays.equals (addr.getAddress(), fe80_loopback)) {259continue;260}261return addr;262}263}264currIf = null;265}266}267268private NetworkInterface getNextIf () {269if (ifs != null) {270while (ifs.hasMoreElements()) {271NetworkInterface nic = (NetworkInterface)ifs.nextElement();272try {273if (nic.isUp() && !nic.isLoopback())274return nic;275} catch (SocketException e) {276// ignore277}278}279}280281return null;282}283}284285/**286* Throws a RuntimeException if the boolean condition is false287*/288public static void t_assert (boolean assertion) {289if (assertion) {290return;291}292Throwable t = new Throwable();293StackTraceElement[] strace = t.getStackTrace();294String msg = "Assertion failed at: " + strace[1].toString();295throw new RuntimeException (msg);296}297298private static void simpleRead (InputStream is, int start) throws Exception {299byte b[] = new byte [2];300for (int i=start; i<start+100; i++) {301int x = is.read (b);302if (x == 1) {303x += is.read (b,1,1);304}305if (x!=2) {306throw new Exception ("read error");307}308int r = bytes (b[0], b[1]);309if (r != i) {310throw new Exception ("read " + r + " expected " +i);311}312}313}314315/* convert signed bytes to unisigned int */316private static int bytes (byte b1, byte b2) {317int i1 = (int)b1 & 0xFF;318int i2 = (int)b2 & 0xFF;319return i1 * 256 + i2;320}321322static void simpleWrite (OutputStream os, int start) throws Exception {323byte b[] = new byte [2];324for (int i=start; i<start+100; i++) {325b[0] = (byte) (i / 256);326b[1] = (byte) (i % 256);327os.write (b);328}329}330331private static class Runner extends Thread {332Runnable runnee;333long delay;334335Runner (Runnable runnee, long delay) {336super();337this.runnee = runnee;338this.delay = delay;339}340341public void run () {342try {343Thread.sleep (delay);344runnee.run ();345} catch (Exception e) {346e.printStackTrace();347}348}349}350351/*352* Take the given Runnable and run it in a spawned thread353* after the given time has elapsed. runAfter() returns immediately354*/355public static void runAfter (long millis, Runnable runnee) {356Runner runner = new Runner (runnee, millis);357runner.start ();358}359360static String osname;361362static {363osname = System.getProperty ("os.name");364}365366static boolean isLinux () {367return osname.equals ("Linux");368}369370static boolean isWindows () {371return osname.startsWith ("Windows");372}373}374375376