Path: blob/master/test/jdk/java/nio/channels/SocketChannel/OutOfBand.java
41154 views
/*1* Copyright (c) 2010, 2015, 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/* @test24* @summary Test socket adapter sendUrgentData method25* @bug 696390726* @key randomness27*/2829import java.net.*;30import java.nio.ByteBuffer;31import java.nio.channels.*;32import java.io.IOException;33import java.util.Random;3435public class OutOfBand {3637private static final Random rand = new Random();3839public static void main(String[] args) throws Exception {40ServerSocketChannel ssc = null;41SocketChannel sc1 = null;42SocketChannel sc2 = null;4344try {4546// establish loopback connection47ssc = ServerSocketChannel.open().bind(new InetSocketAddress(0));48InetAddress lh = InetAddress.getLocalHost();49SocketAddress remote =50new InetSocketAddress(lh, ssc.socket().getLocalPort());51sc1 = SocketChannel.open(remote);52sc2 = ssc.accept();5354// enable SO_OOBLINE on server side55sc2.socket().setOOBInline(true);5657// run tests58test1(sc1, sc2);59test2(sc1, sc2);60test3(sc1, sc2);61} finally {62if (sc1 != null) sc1.close();63if (sc2 != null) sc2.close();64if (ssc != null) ssc.close();65}66}6768/**69* Basic test to check that OOB/TCP urgent byte is received.70*/71static void test1(SocketChannel client, SocketChannel server)72throws Exception73{74assert server.socket().getOOBInline();75ByteBuffer bb = ByteBuffer.allocate(100);76for (int i=0; i<1000; i++) {77int b1 = -127 + rand.nextInt(384);78client.socket().sendUrgentData(b1);7980bb.clear();81if (server.read(bb) != 1)82throw new RuntimeException("One byte expected");83bb.flip();84byte b2 = bb.get();85if ((byte)b1 != b2)86throw new RuntimeException("Unexpected byte");87}88}8990/**91* Basic test to check that OOB/TCP urgent byte is received, maybe with92* OOB mark changing.93*/94static void test2(final SocketChannel client, SocketChannel server)95throws Exception96{97assert server.socket().getOOBInline();98Runnable sender = new Runnable() {99public void run() {100try {101for (int i=0; i<256; i++)102client.socket().sendUrgentData(i);103} catch (IOException ioe) {104ioe.printStackTrace();105}106}107};108Thread thr = new Thread(sender);109thr.start();110111ByteBuffer bb = ByteBuffer.allocate(256);112while (bb.hasRemaining()) {113if (server.read(bb) < 0)114throw new RuntimeException("Unexpected EOF");115}116bb.flip();117byte expect = 0;118while (bb.hasRemaining()) {119if (bb.get() != expect)120throw new RuntimeException("Unexpected byte");121expect++;122}123124thr.join();125}126127/**128* Test that is close to some real world examples where an urgent byte is129* used to "cancel" a long running query or transaction on the server.130*/131static void test3(SocketChannel client, final SocketChannel server)132throws Exception133{134final int STOP = rand.nextInt(256);135136assert server.socket().getOOBInline();137Runnable reader = new Runnable() {138public void run() {139ByteBuffer bb = ByteBuffer.allocate(100);140try {141int n = server.read(bb);142if (n != 1) {143String msg = (n < 0) ? "Unexpected EOF" :144"One byte expected";145throw new RuntimeException(msg);146}147bb.flip();148if (bb.get() != (byte)STOP)149throw new RuntimeException("Unexpected byte");150bb.flip();151server.write(bb);152} catch (IOException ioe) {153ioe.printStackTrace();154}155156}157};158159Thread thr = new Thread(reader);160thr.start();161162// "stop" server163client.socket().sendUrgentData(STOP);164165// wait for server reply166ByteBuffer bb = ByteBuffer.allocate(100);167int n = client.read(bb);168if (n != 1)169throw new RuntimeException("Unexpected number of bytes");170bb.flip();171if (bb.get() != (byte)STOP)172throw new RuntimeException("Unexpected reply");173174thr.join();175}176}177178179