Path: blob/master/test/jdk/jdk/net/ExtendedSocketOption/DatagramChannelNAPITest.java
42940 views
/*1* Copyright (c) 2020, 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/*24* @test25* @bug 824309926* @library /test/lib27* @modules jdk.net28* @summary Check ExtendedSocketOption NAPI_ID support for DatagramChannel29* @run testng DatagramChannelNAPITest30* @run testng/othervm -Djava.net.preferIPv4Stack=true DatagramChannelNAPITest31*/3233import jdk.test.lib.net.IPSupport;34import org.testng.SkipException;35import org.testng.annotations.BeforeTest;36import org.testng.annotations.Test;3738import java.io.IOException;39import java.net.InetAddress;40import java.net.InetSocketAddress;41import java.net.SocketException;42import java.nio.ByteBuffer;43import java.nio.channels.DatagramChannel;4445import static org.testng.Assert.assertEquals;46import static org.testng.Assert.assertThrows;47import static org.testng.Assert.assertTrue;48import static jdk.net.ExtendedSocketOptions.SO_INCOMING_NAPI_ID;4950public class DatagramChannelNAPITest {51private InetAddress hostAddr;52private static final Class<SocketException> SE = SocketException.class;53private static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class;54private static final Class<UnsupportedOperationException> UOE = UnsupportedOperationException.class;5556@BeforeTest57public void setup() throws IOException {58IPSupport.throwSkippedExceptionIfNonOperational();59try (var dc = DatagramChannel.open()) {60if (!dc.supportedOptions().contains(SO_INCOMING_NAPI_ID)) {61assertThrows(UOE, () -> dc.getOption(SO_INCOMING_NAPI_ID));62assertThrows(UOE, () -> dc.setOption(SO_INCOMING_NAPI_ID, 42));63assertThrows(UOE, () -> dc.setOption(SO_INCOMING_NAPI_ID, null));64throw new SkipException("NAPI ID not supported on this system");65}66}67hostAddr = InetAddress.getLocalHost();68}6970@Test71public void testSetGetOptionDatagramChannel() throws IOException {72try (var dc = DatagramChannel.open()) {73assertEquals((int) dc.getOption(SO_INCOMING_NAPI_ID), 0);74assertThrows(SE, () -> dc.setOption(SO_INCOMING_NAPI_ID, 42));75assertThrows(IAE, () -> dc.setOption(SO_INCOMING_NAPI_ID, null));76}77}7879@Test80public void testDatagramChannel() throws Exception {81int senderID, receiverID, tempID = 0;82boolean initialRun = true;83try (var r = DatagramChannel.open()) {84r.bind(new InetSocketAddress(hostAddr, 0));85var port = r.socket().getLocalPort();86var addr = new InetSocketAddress(hostAddr, port);8788try (var s = DatagramChannel.open()) {89s.bind(null);90for (int i = 0; i < 10; i++) {91s.send(ByteBuffer.wrap("test".getBytes()), addr);92senderID = s.getOption(SO_INCOMING_NAPI_ID);93assertEquals(senderID, 0, "DatagramChannel: Sender");9495r.receive(ByteBuffer.allocate(128));96receiverID = r.getOption(SO_INCOMING_NAPI_ID);9798// check ID remains consistent99if (initialRun) {100assertTrue(receiverID >= 0, "DatagramChannel: Receiver");101} else {102assertEquals(receiverID, tempID);103initialRun = false;104}105tempID = receiverID;106}107}108}109}110}111112113