0N/A/*
3261N/A * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Aimport java.net.*;
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/A
0N/Apublic class Tests {
0N/A /**
0N/A * performs a simple exchange of data between the two sockets
0N/A * and throws an exception if there is any problem.
0N/A */
0N/A public static void simpleDataExchange (Socket s1, Socket s2)
0N/A throws Exception {
0N/A
0N/A InputStream i1 = s1.getInputStream();
0N/A InputStream i2 = s2.getInputStream();
0N/A OutputStream o1 = s1.getOutputStream();
0N/A OutputStream o2 = s2.getOutputStream();
0N/A
2230N/A startSimpleWriter("SimpleWriter-1", o1, 100);
2230N/A startSimpleWriter("SimpleWriter-2", o2, 200);
0N/A simpleRead (i2, 100);
0N/A simpleRead (i1, 200);
0N/A }
0N/A
2230N/A static void startSimpleWriter(String threadName, final OutputStream os, final int start) {
2230N/A (new Thread(new Runnable() {
2230N/A public void run() {
2230N/A try { simpleWrite(os, start); }
2230N/A catch (Exception e) {unexpected(e); }
2230N/A }}, threadName)).start();
2230N/A }
2230N/A
2230N/A static void unexpected(Exception e ) {
2230N/A System.out.println("Unexcepted Exception: " + e);
2230N/A e.printStackTrace();
2230N/A }
2230N/A
0N/A /**
0N/A * Send a packet from s1 to s2 (ia2/s2.localPort) and check it
0N/A * Send a packet from s2 to s1 (ia1/s1.localPort) and check it
0N/A */
0N/A public static void simpleDataExchange (DatagramSocket s1, InetAddress ia1,
0N/A DatagramSocket s2, InetAddress ia2)
0N/A throws Exception {
0N/A
0N/A SocketAddress dest1 = new InetSocketAddress (ia1, s1.getLocalPort());
0N/A dprintln ("dest1 = " + dest1);
0N/A SocketAddress dest2 = new InetSocketAddress (ia2, s2.getLocalPort());
0N/A dprintln ("dest2 = " + dest2);
0N/A
0N/A byte[] ba = "Hello world".getBytes();
0N/A byte[] bb = "HELLO WORLD1".getBytes();
0N/A DatagramPacket p1 = new DatagramPacket (ba, ba.length, dest1);
0N/A DatagramPacket p2 = new DatagramPacket (ba, ba.length, dest2);
0N/A
0N/A DatagramPacket r1 = new DatagramPacket (new byte[256], 256);
0N/A DatagramPacket r2 = new DatagramPacket (new byte[256], 256);
0N/A
0N/A s2.send (p1);
0N/A s1.send (p2);
0N/A s1.receive (r1);
0N/A s2.receive (r2);
0N/A comparePackets (p1, r1);
0N/A comparePackets (p2, r2);
0N/A }
0N/A
0N/A /**
0N/A * Send a packet from s1 to s2 (ia2/s2.localPort) and send same packet
0N/A * back from s2 to sender. Check s1 receives original packet
0N/A */
0N/A
0N/A public static void datagramEcho (DatagramSocket s1, DatagramSocket s2,
0N/A InetAddress ia2)
0N/A throws Exception {
0N/A
0N/A byte[] ba = "Hello world".getBytes();
0N/A DatagramPacket p1;
0N/A
0N/A SocketAddress dest2 = null;
0N/A if (ia2 != null) {
0N/A dest2 = new InetSocketAddress (ia2, s2.getLocalPort());
0N/A p1 = new DatagramPacket (ba, ba.length, dest2);
0N/A } else {
0N/A p1 = new DatagramPacket (ba, ba.length);
0N/A }
0N/A
0N/A dprintln ("dest2 = " + dest2);
0N/A
0N/A
0N/A DatagramPacket r1 = new DatagramPacket (new byte[256], 256);
0N/A DatagramPacket r2 = new DatagramPacket (new byte[256], 256);
0N/A
0N/A s1.send (p1);
0N/A s2.receive (r1);
0N/A s2.send (r1);
0N/A s1.receive (r2);
0N/A comparePackets (p1, r1);
0N/A comparePackets (p1, r2);
0N/A }
0N/A
0N/A public static void comparePackets (DatagramPacket p1, DatagramPacket p2)
0N/A throws Exception {
0N/A
0N/A byte[] b1 = p1.getData();
0N/A byte[] b2 = p2.getData();
0N/A int len = p1.getLength () > p2.getLength() ? p2.getLength()
0N/A : p1.getLength();
0N/A for (int i=0; i<len; i++) {
0N/A if (b1[i] != b2[i]) {
0N/A throw new Exception ("packets not the same");
0N/A }
0N/A }
0N/A }
0N/A
2612N/A /* check the time got is within 50% of the time expected */
0N/A public static void checkTime (long got, long expected) {
0N/A dprintln ("checkTime: got " + got + " expected " + expected);
2612N/A long upper = expected + (expected / 2);
2612N/A long lower = expected - (expected / 2);
0N/A if (got > upper || got < lower) {
0N/A throw new RuntimeException ("checkTime failed: got " + got + " expected " + expected);
0N/A }
0N/A }
0N/A
0N/A static boolean debug = false;
0N/A
0N/A public static void checkDebug (String[] args) {
0N/A debug = args.length > 0 && args[0].equals("-d");
0N/A }
0N/A
0N/A public static void dprint (String s) {
0N/A if (debug) {
0N/A System.out.print (s);
0N/A }
0N/A }
0N/A
0N/A public static void dprintln (String s) {
0N/A if (debug) {
0N/A System.out.println (s);
0N/A }
0N/A }
0N/A
0N/A static int numberInterfaces () {
0N/A try {
0N/A Enumeration ifs = NetworkInterface.getNetworkInterfaces();
0N/A int nifs=0;
0N/A while (ifs.hasMoreElements()) {
0N/A nifs++;
0N/A ifs.nextElement();
0N/A }
0N/A return nifs;
0N/A } catch (SocketException e) {
0N/A return 0;
0N/A }
0N/A }
0N/A
0N/A public static Enumeration ipv4Addresses() {
0N/A return new AddrEnum (Inet4Address.class);
0N/A }
0N/A
0N/A public static Inet4Address getFirstLocalIPv4Address () {
0N/A Enumeration e = ipv4Addresses();
0N/A if (!e.hasMoreElements()) {
0N/A return null;
0N/A }
0N/A return (Inet4Address)e.nextElement();
0N/A }
0N/A
0N/A public static Inet6Address getFirstLocalIPv6Address () {
0N/A Enumeration e = ipv6Addresses();
0N/A if (!e.hasMoreElements()) {
0N/A return null;
0N/A }
0N/A return (Inet6Address)e.nextElement();
0N/A }
0N/A
0N/A public static Enumeration ipv6Addresses() {
0N/A return new AddrEnum (Inet6Address.class);
0N/A }
0N/A
0N/A /* enumerates the Inet4Addresses or Inet6Addresses on the system */
0N/A
0N/A private static class AddrEnum implements Enumeration {
0N/A
0N/A Enumeration ifs;
0N/A NetworkInterface currIf = null;
0N/A InetAddress nextAddr=null;
0N/A Enumeration addrs=null;
0N/A Class filter;
0N/A
0N/A static final byte[] fe80_loopback = new byte [] {
0N/A (byte)0xfe,(byte)0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,1
0N/A };
0N/A
0N/A AddrEnum (Class filter) {
0N/A this.filter = filter;
0N/A try {
0N/A ifs = NetworkInterface.getNetworkInterfaces();
0N/A } catch (SocketException e) {}
0N/A }
0N/A
0N/A public boolean hasMoreElements () {
0N/A if (nextAddr == null) {
0N/A nextAddr = getNext();
0N/A }
0N/A return (nextAddr != null);
0N/A }
0N/A
0N/A public Object nextElement () {
0N/A if (!hasMoreElements()) {
0N/A throw new NoSuchElementException ("no more addresses");
0N/A }
0N/A Object next = nextAddr;
0N/A nextAddr = null;
0N/A return next;
0N/A }
0N/A
0N/A private InetAddress getNext() {
0N/A while (true) {
0N/A if (currIf == null) {
0N/A currIf = getNextIf();
0N/A if (currIf == null) {
0N/A return null;
0N/A }
0N/A addrs = currIf.getInetAddresses();
0N/A }
0N/A while (addrs.hasMoreElements()) {
0N/A InetAddress addr = (InetAddress) addrs.nextElement();
2557N/A if (filter.isInstance (addr) && !addr.isLoopbackAddress()
2557N/A && !addr.isAnyLocalAddress()) {
0N/A if (Arrays.equals (addr.getAddress(), fe80_loopback)) {
0N/A continue;
0N/A }
0N/A return addr;
0N/A }
0N/A }
0N/A currIf = null;
0N/A }
0N/A }
0N/A
0N/A private NetworkInterface getNextIf () {
2230N/A if (ifs != null) {
2230N/A while (ifs.hasMoreElements()) {
2230N/A NetworkInterface nic = (NetworkInterface)ifs.nextElement();
2230N/A try {
2230N/A if (nic.isUp() && !nic.isLoopback())
2230N/A return nic;
2230N/A } catch (SocketException e) {
2230N/A // ignore
2230N/A }
0N/A }
0N/A }
0N/A
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Throws a RuntimeException if the boolean condition is false
0N/A */
0N/A public static void t_assert (boolean assertion) {
0N/A if (assertion) {
0N/A return;
0N/A }
0N/A Throwable t = new Throwable();
0N/A StackTraceElement[] strace = t.getStackTrace();
0N/A String msg = "Assertion failed at: " + strace[1].toString();
0N/A throw new RuntimeException (msg);
0N/A }
0N/A
0N/A private static void simpleRead (InputStream is, int start) throws Exception {
0N/A byte b[] = new byte [2];
0N/A for (int i=start; i<start+100; i++) {
0N/A int x = is.read (b);
0N/A if (x == 1) {
0N/A x += is.read (b,1,1);
0N/A }
0N/A if (x!=2) {
0N/A throw new Exception ("read error");
0N/A }
0N/A int r = bytes (b[0], b[1]);
0N/A if (r != i) {
0N/A throw new Exception ("read " + r + " expected " +i);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /* convert signed bytes to unisigned int */
0N/A private static int bytes (byte b1, byte b2) {
0N/A int i1 = (int)b1 & 0xFF;
0N/A int i2 = (int)b2 & 0xFF;
0N/A return i1 * 256 + i2;
0N/A }
0N/A
0N/A static void simpleWrite (OutputStream os, int start) throws Exception {
0N/A byte b[] = new byte [2];
0N/A for (int i=start; i<start+100; i++) {
0N/A b[0] = (byte) (i / 256);
0N/A b[1] = (byte) (i % 256);
0N/A os.write (b);
0N/A }
0N/A }
0N/A
0N/A private static class Runner extends Thread {
0N/A Runnable runnee;
0N/A long delay;
0N/A
0N/A Runner (Runnable runnee, long delay) {
0N/A super();
0N/A this.runnee = runnee;
0N/A this.delay = delay;
0N/A }
0N/A
0N/A public void run () {
0N/A try {
0N/A Thread.sleep (delay);
0N/A runnee.run ();
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Take the given Runnable and run it in a spawned thread
0N/A * after the given time has elapsed. runAfter() returns immediately
0N/A */
0N/A public static void runAfter (long millis, Runnable runnee) {
0N/A Runner runner = new Runner (runnee, millis);
0N/A runner.start ();
0N/A }
0N/A
0N/A static String osname;
0N/A
0N/A static {
0N/A osname = System.getProperty ("os.name");
0N/A }
0N/A
0N/A static boolean isLinux () {
0N/A return osname.equals ("Linux");
0N/A }
0N/A
0N/A static boolean isWindows () {
0N/A return osname.startsWith ("Windows");
0N/A }
0N/A}