2559N/A/*
2559N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2559N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2559N/A *
2559N/A * This code is free software; you can redistribute it and/or modify it
2559N/A * under the terms of the GNU General Public License version 2 only, as
2559N/A * published by the Free Software Foundation.
2559N/A *
2559N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2559N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2559N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2559N/A * version 2 for more details (a copy is included in the LICENSE file that
2559N/A * accompanied this code).
2559N/A *
2559N/A * You should have received a copy of the GNU General Public License version
2559N/A * 2 along with this work; if not, write to the Free Software Foundation,
2559N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2559N/A *
2559N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2559N/A * or visit www.oracle.com if you need additional information or have any
2559N/A * questions.
2559N/A */
2559N/A
2559N/A/* @test
2559N/A * @bug 6213702
2559N/A * @summary OOB data causes a SocketChannel, with OOBINLINE disabled, to be
2559N/A * selected
2559N/A */
2559N/A
2559N/Aimport java.net.*;
2559N/Aimport java.nio.ByteBuffer;
2559N/Aimport java.nio.channels.*;
2559N/Aimport java.io.IOException;
2559N/A
2559N/Apublic class OutOfBand {
2559N/A
2559N/A public static void main(String[] args) throws Exception {
2559N/A ServerSocketChannel ssc = null;
2559N/A SocketChannel sc = null;
2559N/A Selector sel = null;
2559N/A Socket s = null;
2559N/A
2559N/A try {
2559N/A // establish loopback connection.
2559N/A ssc = ServerSocketChannel.open().bind(new InetSocketAddress(0));
2559N/A s = new Socket(InetAddress.getLocalHost(),
2559N/A ssc.socket().getLocalPort());
2559N/A sc = ssc.accept();
2559N/A
2559N/A sel = Selector.open();
2559N/A sc.configureBlocking(false);
2559N/A sc.register(sel, SelectionKey.OP_READ);
2559N/A
2559N/A // OOB data should be disabled by default
2559N/A if (sc.socket().getOOBInline())
2559N/A throw new RuntimeException("SO_OOBINLINE enabled");
2559N/A test(s, false, 0, 0, sel);
2559N/A test(s, false, 512, 0, sel);
2559N/A test(s, false, 0, 512, sel);
2559N/A test(s, false, 512, 512, sel);
2559N/A
2559N/A // enable SO_OOBINLINE
2559N/A sc.socket().setOOBInline(true);
2559N/A
2559N/A // OOB data should be received
2559N/A test(s, true, 0, 0, sel);
2559N/A test(s, true, 512, 0, sel);
2559N/A test(s, true, 0, 512, sel);
2559N/A test(s, true, 512, 512, sel);
2559N/A
2559N/A } finally {
2559N/A if (sel != null) sel.close();
2559N/A if (sc != null) sc.close();
2559N/A if (ssc != null) ssc.close();
2559N/A if (s != null) sc.close();
2559N/A }
2559N/A }
2559N/A
2559N/A static void test(Socket s, boolean urgentExpected,
2559N/A int bytesBefore, int bytesAfter,
2559N/A Selector sel)
2559N/A throws IOException
2559N/A {
2559N/A // send data
2559N/A int bytesExpected = 0;
2559N/A if (bytesBefore > 0) {
2559N/A s.getOutputStream().write(new byte[bytesBefore]);
2559N/A bytesExpected += bytesBefore;
2559N/A }
2559N/A s.sendUrgentData(0xff);
2559N/A if (urgentExpected)
2559N/A bytesExpected++;
2559N/A if (bytesAfter > 0) {
2559N/A s.getOutputStream().write(new byte[bytesAfter]);
2559N/A bytesExpected += bytesAfter;
2559N/A }
2559N/A
2559N/A // receive data, checking for spurious wakeups and reads
2559N/A int spuriousWakeups = 0;
2559N/A int spuriousReads = 0;
2559N/A int bytesRead = 0;
2559N/A ByteBuffer bb = ByteBuffer.allocate(100);
2559N/A for (;;) {
2559N/A int n = sel.select(2000);
2559N/A if (n == 0) {
2559N/A if (bytesRead == bytesExpected) {
2559N/A System.out.format("Selector wakeups %d\tSpurious reads %d%n",
2559N/A spuriousWakeups, spuriousReads);
2559N/A return;
2559N/A }
2559N/A if (++spuriousWakeups >= 3)
2559N/A throw new RuntimeException("Selector appears to be spinning" +
2559N/A " or data not received");
2559N/A continue;
2559N/A }
2559N/A if (n > 1)
2559N/A throw new RuntimeException("More than one key selected????");
2559N/A SelectionKey key = sel.selectedKeys().iterator().next();
2559N/A bb.clear();
2559N/A n = ((SocketChannel)key.channel()).read(bb);
2559N/A if (n == 0) {
2559N/A if (++spuriousReads >=3)
2559N/A throw new RuntimeException("Too many spurious reads");
2559N/A } else {
2559N/A bytesRead += n;
2559N/A if (bytesRead > bytesExpected)
2559N/A throw new RuntimeException("Received more than expected");
2559N/A }
2559N/A sel.selectedKeys().clear();
2559N/A }
2559N/A }
2559N/A}