1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8195160
  27  * @summary Tests blocking configuration of server socket channels
  28  * @requires (os.family == "linux")
  29  * @library ../ /test/lib
  30  * @build RsocketTest
  31  * @run testng/othervm SSCConfigureBlocking
  32  */
  33 
  34 import java.io.IOException;
  35 import java.net.ProtocolFamily;
  36 import java.nio.channels.IllegalBlockingModeException;
  37 import java.nio.channels.Selector;
  38 import java.nio.channels.ServerSocketChannel;
  39 import org.testng.SkipException;
  40 import org.testng.annotations.BeforeTest;
  41 import org.testng.annotations.DataProvider;
  42 import org.testng.annotations.Test;
  43 import static java.net.StandardProtocolFamily.INET;
  44 import static java.net.StandardProtocolFamily.INET6;
  45 import static java.nio.channels.SelectionKey.OP_ACCEPT;
  46 import static jdk.net.RdmaSockets.*;
  47 import static org.testng.Assert.assertThrows;
  48 import static org.testng.Assert.assertTrue;
  49 
  50 public class SSCConfigureBlocking {
  51 
  52     @BeforeTest
  53     public void setup() throws Exception {
  54         if (!RsocketTest.isRsocketAvailable())
  55             throw new SkipException("rsocket is not available");
  56     }
  57 
  58     @DataProvider(name = "families")
  59     public Object[][] families() {
  60         return new Object[][] { { INET }, { INET6 } };
  61     }
  62 
  63      // Newly-created selectable channels are always in blocking mode.
  64 
  65     @Test(dataProvider = "families")
  66     public void testServerSocketChannel(ProtocolFamily family)
  67         throws IOException
  68     {
  69         try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
  70             assertTrue(ssc.isBlocking(), "Newly created channel is not blocking");
  71             ssc.configureBlocking(false);
  72             assertTrue(!ssc.isBlocking(), "Expected non-blocking");
  73             ssc.configureBlocking(true);
  74             assertTrue(ssc.isBlocking(), "Expected blocking");
  75         }
  76     }
  77 
  78     static final Class<IllegalBlockingModeException> IBME = IllegalBlockingModeException.class;
  79 
  80     @Test(dataProvider = "families")
  81     public void testServerSocketChannelRegister(ProtocolFamily family)
  82         throws IOException
  83     {
  84         try (ServerSocketChannel ssc = openServerSocketChannel(family);
  85              Selector selector = Selector.open()) {
  86             assertThrows(IBME, () -> ssc.register(selector, OP_ACCEPT));
  87             assertThrows(IBME, () -> ssc.register(selector, OP_ACCEPT, new Object()));
  88             ssc.configureBlocking(false);
  89             ssc.register(selector, OP_ACCEPT);
  90             assertThrows(IBME, () -> ssc.configureBlocking(true));
  91         }
  92     }
  93 }