Path: blob/master/test/jdk/java/nio/channels/etc/ProtocolFamilies.java
41153 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*/2223import jdk.test.lib.NetworkConfiguration;24import jdk.test.lib.net.IPSupport;25import org.testng.annotations.BeforeTest;26import org.testng.annotations.DataProvider;27import org.testng.annotations.Test;28import org.testng.Assert.ThrowingRunnable;29import java.io.IOException;30import java.net.*;31import java.nio.channels.*;32import java.nio.channels.spi.SelectorProvider;33import static java.lang.System.out;34import static java.net.StandardProtocolFamily.INET;35import static java.net.StandardProtocolFamily.INET6;36import static jdk.test.lib.net.IPSupport.*;37import static org.testng.Assert.assertEquals;38import static org.testng.Assert.assertThrows;3940/*41* @test42* @summary Test SocketChannel, ServerSocketChannel and DatagramChannel43* with various ProtocolFamily combinations44* @library /test/lib45* @build jdk.test.lib.NetworkConfiguration46* @run testng ProtocolFamilies47* @run testng/othervm -Djava.net.preferIPv4Stack=true ProtocolFamilies48*/495051public class ProtocolFamilies {52static final boolean hasIPv6 = hasIPv6();53static final boolean preferIPv4 = preferIPv4Stack();54static Inet4Address ia4;55static Inet6Address ia6;5657@BeforeTest()58public void setup() throws Exception {59NetworkConfiguration.printSystemConfiguration(out);60IPSupport.printPlatformSupport(out);61throwSkippedExceptionIfNonOperational();6263ia4 = getLocalIPv4Address();64ia6 = getLocalIPv6Address();65out.println("ia4: " + ia4);66out.println("ia6: " + ia6 + "\n");67}6869static final Class<UnsupportedAddressTypeException> UATE = UnsupportedAddressTypeException.class;70static final Class<UnsupportedOperationException> UOE = UnsupportedOperationException.class;7172@DataProvider(name = "open")73public Object[][] open() {74if (hasIPv6 && !preferIPv4) {75return new Object[][]{76{ INET, null },77{ INET6, null }78};79} else {80return new Object[][]{81{ INET, null },82{ INET6, UOE }83};84}85}8687@Test(dataProvider = "open")88public void scOpen(StandardProtocolFamily family,89Class<? extends Exception> expectedException)90throws Throwable91{92SocketChannel sc = null;93try {94if (expectedException == UOE) {95try {96sc = openSC(family);97} catch (UnsupportedOperationException e) {}98} else {99sc = openSC(family);100}101} finally {102if (sc != null)103sc.close();104}105}106107@Test(dataProvider = "open")108public void sscOpen(StandardProtocolFamily family,109Class<? extends Exception> expectedException)110throws Throwable111{112ServerSocketChannel ssc = null;113try {114if (expectedException == UOE) {115try {116ssc = openSSC(family);117} catch (UnsupportedOperationException e) {}118} else {119openSSC(family);120}121} finally {122if (ssc != null)123ssc.close();124}125}126127@Test(dataProvider = "open")128public void dcOpen(StandardProtocolFamily family,129Class<? extends Exception> expectedException)130throws Throwable131{132DatagramChannel dc = null;133try {134if (expectedException == UOE) {135try {136dc = openDC(family);137} catch (UnsupportedOperationException e) {}138} else {139openDC(family);140}141} finally {142if (dc != null)143dc.close();144}145}146147@DataProvider(name = "openBind")148public Object[][] openBind() {149if (hasIPv6 && !preferIPv4) {150return new Object[][]{151{ INET, INET, null },152{ INET, INET6, UATE },153{ INET, null, null },154{ INET6, INET, null },155{ INET6, INET6, null },156{ INET6, null, null },157{ null, INET, null },158{ null, INET6, null },159{ null, null, null }160};161} else {162return new Object[][]{163{ INET, INET, null },164{ INET, INET6, UATE },165{ INET, null, null },166{ null, INET, null },167{ null, INET6, UATE },168{ null, null, null }169};170}171}172173// SocketChannel open - INET, INET6, default174// SocketChannel bind - INET, INET6, null175176@Test(dataProvider = "openBind")177public void scOpenBind(StandardProtocolFamily ofamily,178StandardProtocolFamily bfamily,179Class<? extends Exception> expectedException)180throws Throwable181{182try (SocketChannel sc = openSC(ofamily)) {183SocketAddress addr = getSocketAddress(bfamily);184ThrowingRunnable bindOp = () -> sc.bind(addr);185if (expectedException == null)186bindOp.run();187else188assertThrows(expectedException, bindOp);189}190}191192// ServerSocketChannel open - INET, INET6, default193// ServerSocketChannel bind - INET, INET6, null194195@Test(dataProvider = "openBind")196public void sscOpenBind(StandardProtocolFamily ofamily,197StandardProtocolFamily bfamily,198Class<? extends Exception> expectedException)199throws Throwable200{201try (ServerSocketChannel ssc = openSSC(ofamily)) {202SocketAddress addr = getSocketAddress(bfamily);203ThrowingRunnable bindOp = () -> ssc.bind(addr);204if (expectedException == null)205bindOp.run();206else207assertThrows(expectedException, bindOp);208}209}210211// DatagramChannel open - INET, INET6, default212// DatagramChannel bind - INET, INET6, null213214@Test(dataProvider = "openBind")215public void dcOpenBind(StandardProtocolFamily ofamily,216StandardProtocolFamily bfamily,217Class<? extends Exception> expectedException)218throws Throwable219{220try (DatagramChannel dc = openDC(ofamily)) {221SocketAddress addr = getSocketAddress(bfamily);222ThrowingRunnable bindOp = () -> dc.bind(addr);223if (expectedException == null)224bindOp.run();225else226assertThrows(expectedException, bindOp);227}228}229230// SocketChannel open - INET, INET6, default231// SocketChannel connect - INET, INET6, default232233@DataProvider(name = "openConnect")234public Object[][] openConnect() {235if (hasIPv6 && !preferIPv4) {236return new Object[][]{237{ INET, INET, null },238{ INET, INET6, null },239{ INET, null, null },240{ INET6, INET, UATE },241{ INET6, INET6, null },242{ INET6, null, null },243{ null, INET, UATE },244{ null, INET6, null },245{ null, null, null }246};247} else {248// INET6 channels cannot be created - UOE - tested elsewhere249return new Object[][]{250{ INET, INET, null },251{ INET, null, null },252{ null, INET, null },253{ null, null, null }254};255}256}257258@Test(dataProvider = "openConnect")259public void scOpenConnect(StandardProtocolFamily sfamily,260StandardProtocolFamily cfamily,261Class<? extends Exception> expectedException)262throws Throwable263{264try (ServerSocketChannel ssc = openSSC(sfamily)) {265ssc.bind(null);266SocketAddress saddr = ssc.getLocalAddress();267try (SocketChannel sc = openSC(cfamily)) {268if (expectedException == null)269sc.connect(saddr);270else271assertThrows(expectedException, () -> sc.connect(saddr));272}273}274}275276static final Class<NullPointerException> NPE = NullPointerException.class;277278// Tests null handling279@Test280public void testNulls() {281assertThrows(NPE, () -> SocketChannel.open((ProtocolFamily)null));282assertThrows(NPE, () -> ServerSocketChannel.open(null));283assertThrows(NPE, () -> DatagramChannel.open(null));284285assertThrows(NPE, () -> SelectorProvider.provider().openSocketChannel(null));286assertThrows(NPE, () -> SelectorProvider.provider().openServerSocketChannel(null));287assertThrows(NPE, () -> SelectorProvider.provider().openDatagramChannel(null));288}289290static final ProtocolFamily BAD_PF = () -> "BAD_PROTOCOL_FAMILY";291292// Tests UOE handling293@Test294public void testUoe() {295assertThrows(UOE, () -> SocketChannel.open(BAD_PF));296assertThrows(UOE, () -> ServerSocketChannel.open(BAD_PF));297assertThrows(UOE, () -> DatagramChannel.open(BAD_PF));298299assertThrows(UOE, () -> SelectorProvider.provider().openSocketChannel(BAD_PF));300assertThrows(UOE, () -> SelectorProvider.provider().openServerSocketChannel(BAD_PF));301assertThrows(UOE, () -> SelectorProvider.provider().openDatagramChannel(BAD_PF));302}303304// Helper methods305306private static SocketChannel openSC(StandardProtocolFamily family)307throws IOException {308SocketChannel sc = family == null ? SocketChannel.open()309: SocketChannel.open(family);310return sc;311}312313private static ServerSocketChannel openSSC(StandardProtocolFamily family)314throws IOException {315ServerSocketChannel ssc = family == null ? ServerSocketChannel.open()316: ServerSocketChannel.open(family);317return ssc;318}319320private static DatagramChannel openDC(StandardProtocolFamily family)321throws IOException {322DatagramChannel dc = family == null ? DatagramChannel.open()323: DatagramChannel.open(family);324return dc;325}326327private static SocketAddress getSocketAddress(StandardProtocolFamily family) {328return family == null ? null : switch (family) {329case INET -> new InetSocketAddress(ia4, 0);330case INET6 -> new InetSocketAddress(ia6, 0);331default -> throw new RuntimeException("Unexpected protocol family");332};333}334335private static SocketAddress getLoopback(StandardProtocolFamily family, int port)336throws UnknownHostException {337if ((family == null || family == INET6) && hasIPv6) {338return new InetSocketAddress(InetAddress.getByName("::1"), port);339} else {340return new InetSocketAddress(InetAddress.getByName("127.0.0.1"), port);341}342}343344private static Inet4Address getLocalIPv4Address()345throws Exception {346return NetworkConfiguration.probe()347.ip4Addresses()348.filter(a -> !a.isLoopbackAddress())349.findFirst()350.orElse((Inet4Address)InetAddress.getByName("0.0.0.0"));351}352353private static Inet6Address getLocalIPv6Address()354throws Exception {355return NetworkConfiguration.probe()356.ip6Addresses()357.filter(a -> !a.isLoopbackAddress())358.findFirst()359.orElse((Inet6Address) InetAddress.getByName("::0"));360}361}362363364