1 /* 2 * Copyright (c) 2020, 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 8243099 27 * @library /test/lib 28 * @modules jdk.net 29 * @summary Check ExtendedSocketOption NAPI_ID support for AsynchronousSocketChannel and 30 * AsynchronousServerSocketChannel 31 * @run testng AsynchronousSocketChannelNAPITest 32 * @run testng/othervm -Djava.net.preferIPv4Stack=true AsynchronousSocketChannelNAPITest 33 */ 34 35 import jdk.test.lib.net.IPSupport; 36 import org.testng.SkipException; 37 import org.testng.annotations.BeforeTest; 38 import org.testng.annotations.Test; 39 40 import java.io.IOException; 41 import java.net.InetAddress; 42 import java.net.InetSocketAddress; 43 import java.net.SocketException; 44 import java.nio.ByteBuffer; 45 import java.nio.channels.AsynchronousServerSocketChannel; 46 import java.nio.channels.AsynchronousSocketChannel; 47 48 import static org.testng.Assert.assertEquals; 49 import static org.testng.Assert.assertThrows; 50 import static org.testng.Assert.assertTrue; 51 52 import static jdk.net.ExtendedSocketOptions.SO_INCOMING_NAPI_ID; 53 54 public class AsynchronousSocketChannelNAPITest { 55 private InetAddress hostAddr; 56 private static final Class<SocketException> SE = SocketException.class; 57 private static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class; 58 private static final Class<UnsupportedOperationException> UOE = UnsupportedOperationException.class; 59 60 @BeforeTest setup()61 public void setup() throws IOException { 62 IPSupport.throwSkippedExceptionIfNonOperational(); 63 try (var sc = AsynchronousSocketChannel.open(); 64 var ssc = AsynchronousServerSocketChannel.open()) { 65 if (!sc.supportedOptions().contains(SO_INCOMING_NAPI_ID)) { 66 assertThrows(UOE, () -> sc.getOption(SO_INCOMING_NAPI_ID)); 67 assertThrows(UOE, () -> sc.setOption(SO_INCOMING_NAPI_ID, 42)); 68 assertThrows(UOE, () -> sc.setOption(SO_INCOMING_NAPI_ID, null)); 69 assertThrows(UOE, () -> ssc.getOption(SO_INCOMING_NAPI_ID)); 70 assertThrows(UOE, () -> ssc.setOption(SO_INCOMING_NAPI_ID, 42)); 71 assertThrows(UOE, () -> ssc.setOption(SO_INCOMING_NAPI_ID, null)); 72 throw new SkipException("NAPI ID not supported on this system"); 73 } 74 } 75 hostAddr = InetAddress.getLocalHost(); 76 } 77 78 @Test testSetGetOptionSocketChannel()79 public void testSetGetOptionSocketChannel() throws IOException { 80 try (var sc = AsynchronousSocketChannel.open()) { 81 assertEquals((int) sc.getOption(SO_INCOMING_NAPI_ID), 0); 82 assertThrows(SE, () -> sc.setOption(SO_INCOMING_NAPI_ID, 42)); 83 assertThrows(IAE, () -> sc.setOption(SO_INCOMING_NAPI_ID, null)); 84 } 85 } 86 87 @Test testSetGetOptionServerSocketChannel()88 public void testSetGetOptionServerSocketChannel() throws IOException { 89 try (var ssc = AsynchronousServerSocketChannel.open()) { 90 assertEquals((int) ssc.getOption(SO_INCOMING_NAPI_ID), 0); 91 assertThrows(SE, () -> ssc.setOption(SO_INCOMING_NAPI_ID, 42)); 92 assertThrows(IAE, () -> ssc.setOption(SO_INCOMING_NAPI_ID, null)); 93 } 94 } 95 96 @Test testSocketChannel()97 public void testSocketChannel() throws Exception { 98 int socketID, clientID, tempID = 0; 99 boolean initialRun = true; 100 try (var ss = AsynchronousServerSocketChannel.open()) { 101 ss.bind(new InetSocketAddress(hostAddr, 0)); 102 103 try (var c = AsynchronousSocketChannel.open()) { 104 c.connect(ss.getLocalAddress()).get(); 105 106 try (var s = ss.accept().get()) { 107 assertEquals((int) s.getOption(SO_INCOMING_NAPI_ID), 0); 108 109 for (int i = 0; i < 10; i++) { 110 s.write(ByteBuffer.wrap("test".getBytes())); 111 socketID = s.getOption(SO_INCOMING_NAPI_ID); 112 assertEquals(socketID, 0, "AsynchronousSocketChannel: Sender"); 113 114 c.read(ByteBuffer.allocate(128)); 115 clientID = ss.getOption(SO_INCOMING_NAPI_ID); 116 117 // check ID remains consistent 118 if (initialRun) { 119 assertTrue(clientID >= 0, "AsynchronousSocketChannel: Receiver"); 120 } else { 121 assertEquals(clientID, tempID); 122 initialRun = false; 123 } 124 tempID = clientID; 125 } 126 } 127 } 128 } 129 } 130 } 131