Path: blob/master/test/jdk/java/net/SocketImpl/BadUsages.java
41149 views
/*1* Copyright (c) 2019, 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 822148126* @compile/module=java.base java/net/PlatformSocketImpl.java27* @run testng/othervm BadUsages28* @summary Test the platform SocketImpl when used in unintended ways29*/3031import java.io.IOException;32import java.io.InputStream;33import java.io.OutputStream;34import java.net.InetAddress;35import java.net.InetSocketAddress;36import java.net.ServerSocket;37import java.net.SocketAddress;38import java.net.SocketException;39import java.net.SocketImpl;40import java.net.SocketOption;41import java.net.SocketOptions;42import java.net.StandardSocketOptions;43import java.util.Set;4445import java.net.PlatformSocketImpl; // test helper4647import org.testng.annotations.Test;48import static org.testng.Assert.*;4950/**51* SocketImpl does not specify how the SocketImpl behaves when used in ways52* that are not intended, e.g. invoking socket operations before the socket is53* created or trying to establish a connection after the socket is connected or54* closed.55*56* This test exercises the platform SocketImpl to test that it is reliable, and57* throws reasonable exceptions, for these scenarios.58*/5960@Test61public class BadUsages {6263/**64* Test create when already created.65*/66public void testCreate1() throws IOException {67try (var impl = new PlatformSocketImpl(false)) {68impl.create(true);69expectThrows(IOException.class, () -> impl.create(true));70}71}7273/**74* Test create when closed.75*/76public void testCreate2() throws IOException {77var impl = new PlatformSocketImpl(false);78impl.close();79expectThrows(IOException.class, () -> impl.create(true));80}8182/**83* Test connect when not created.84*/85public void testConnect1() throws IOException {86try (var ss = new ServerSocket(0)) {87var impl = new PlatformSocketImpl(false);88var address = ss.getInetAddress();89int port = ss.getLocalPort();90expectThrows(IOException.class, () -> impl.connect(address, port));91}92}9394/**95* Test connect with unsupported address type.96*/97public void testConnect2() throws IOException {98try (var impl = new PlatformSocketImpl(false)) {99impl.create(true);100var remote = new SocketAddress() { };101expectThrows(IOException.class, () -> impl.connect(remote, 0));102}103}104105/**106* Test connect with an unresolved address.107*/108public void testConnect3() throws IOException {109try (var impl = new PlatformSocketImpl(false)) {110impl.create(true);111var remote = new InetSocketAddress("blah-blah.blah-blah", 80);112expectThrows(IOException.class, () -> impl.connect(remote, 0));113}114}115116/**117* Test connect when already connected.118*/119public void testConnect4() throws IOException {120try (var ss = new ServerSocket();121var impl = new PlatformSocketImpl(false)) {122var loopback = InetAddress.getLoopbackAddress();123ss.bind(new InetSocketAddress(loopback, 0));124impl.create(true);125int port = ss.getLocalPort();126impl.connect(loopback, port);127expectThrows(IOException.class, () -> impl.connect(loopback, port));128}129}130131/**132* Test connect when closed.133*/134public void testConnect5() throws IOException {135try (var ss = new ServerSocket(0)) {136var impl = new PlatformSocketImpl(false);137impl.close();138String host = ss.getInetAddress().getHostAddress();139int port = ss.getLocalPort();140expectThrows(IOException.class, () -> impl.connect(host, port));141}142}143144/**145* Test bind when not created.146*/147public void testBind1() throws IOException {148var impl = new PlatformSocketImpl(false);149var loopback = InetAddress.getLoopbackAddress();150expectThrows(IOException.class, () -> impl.bind(loopback, 0));151}152153/**154* Test bind when already bound.155*/156public void testBind2() throws IOException {157try (var impl = new PlatformSocketImpl(false)) {158impl.create(true);159var loopback = InetAddress.getLoopbackAddress();160impl.bind(loopback, 0);161expectThrows(IOException.class, () -> impl.bind(loopback, 0));162}163}164165/**166* Test bind when connected.167*/168public void testBind3() throws IOException {169try (var ss = new ServerSocket();170var impl = new PlatformSocketImpl(false)) {171var loopback = InetAddress.getLoopbackAddress();172ss.bind(new InetSocketAddress(loopback, 0));173impl.create(true);174impl.connect(ss.getLocalSocketAddress(), 0);175expectThrows(IOException.class, () -> impl.bind(loopback, 0));176}177}178179/**180* Test bind when closed.181*/182public void testBind4() throws IOException {183var impl = new PlatformSocketImpl(false);184impl.close();185var loopback = InetAddress.getLoopbackAddress();186expectThrows(IOException.class, () -> impl.bind(loopback, 0));187}188189190/**191* Test listen when not created.192*/193public void testListen1() {194var impl = new PlatformSocketImpl(false);195expectThrows(IOException.class, () -> impl.listen(16));196}197198/**199* Test listen when not bound.200*/201public void testListen2() throws IOException {202try (var impl = new PlatformSocketImpl(false)) {203impl.create(true);204expectThrows(IOException.class, () -> impl.listen(16));205}206}207208/**209* Test listen when closed.210*/211public void testListen3() throws IOException {212var impl = new PlatformSocketImpl(false);213impl.close();214expectThrows(IOException.class, () -> impl.listen(16));215}216217/**218* Test accept when not created.219*/220public void testAccept1() throws IOException {221var impl = new PlatformSocketImpl(true);222var si = new PlatformSocketImpl(false);223expectThrows(IOException.class, () -> impl.accept(si));224}225226/**227* Test accept when not bound.228*/229public void testAccept2() throws IOException {230try (var impl = new PlatformSocketImpl(true)) {231impl.create(true);232var si = new PlatformSocketImpl(false);233expectThrows(IOException.class, () -> impl.accept(si));234}235}236237/**238* Test accept when not a stream socket.239*/240public void testAccept3() throws IOException {241try (var impl = new PlatformSocketImpl(false)) {242impl.create(false);243impl.bind(InetAddress.getLoopbackAddress(), 0);244var si = new PlatformSocketImpl(false);245expectThrows(IOException.class, () -> impl.accept(si));246}247}248249/**250* Test accept when closed.251*/252public void testAccept4() throws IOException {253var impl = new PlatformSocketImpl(true);254impl.close();255var si = new PlatformSocketImpl(false);256expectThrows(IOException.class, () -> impl.accept(si));257}258259/**260* Test accept with SocketImpl that is already created.261*/262public void testAccept5() throws IOException {263try (var impl = new PlatformSocketImpl(true);264var si = new PlatformSocketImpl(false)) {265impl.create(true);266impl.bind(InetAddress.getLoopbackAddress(), 0);267si.create(true);268expectThrows(IOException.class, () -> impl.accept(si));269}270}271272/**273* Test accept with SocketImpl that is closed.274*/275public void testAccept6() throws IOException {276try (var impl = new PlatformSocketImpl(true);277var si = new PlatformSocketImpl(false)) {278impl.create(true);279impl.bind(InetAddress.getLoopbackAddress(), 0);280si.create(true);281si.close();282expectThrows(IOException.class, () -> impl.accept(si));283}284}285286/**287* Test available when not created.288*/289public void testAvailable1() throws IOException {290var impl = new PlatformSocketImpl(false);291expectThrows(IOException.class, () -> impl.available());292}293294/**295* Test available when created but not connected.296*/297public void testAvailable2() throws IOException {298try (var impl = new PlatformSocketImpl(false)) {299impl.create(true);300expectThrows(IOException.class, () -> impl.available());301}302}303304/**305* Test available when closed.306*/307public void testAvailable3() throws IOException {308var impl = new PlatformSocketImpl(false);309impl.close();310expectThrows(IOException.class, () -> impl.available());311}312313/**314* Test setOption when not created.315*/316public void testSetOption1() throws IOException {317var impl = new PlatformSocketImpl(false);318expectThrows(IOException.class,319() -> impl.setOption(StandardSocketOptions.SO_REUSEADDR, true));320// legacy321expectThrows(SocketException.class,322() -> impl.setOption(SocketOptions.SO_REUSEADDR, true));323}324325/**326* Test setOption when closed.327*/328public void testSetOption2() throws IOException {329var impl = new PlatformSocketImpl(false);330impl.close();331expectThrows(IOException.class,332() -> impl.setOption(StandardSocketOptions.SO_REUSEADDR, true));333// legacy334expectThrows(SocketException.class,335() -> impl.setOption(SocketOptions.SO_REUSEADDR, true));336}337338/**339* Test setOption with unsupported option.340*/341public void testSetOption3() throws IOException {342try (var impl = new PlatformSocketImpl(false)) {343impl.create(true);344var opt = new SocketOption<String>() {345@Override public String name() { return "birthday"; }346@Override public Class<String> type() { return String.class; }347};348expectThrows(UnsupportedOperationException.class, () -> impl.setOption(opt, ""));349// legacy350expectThrows(SocketException.class, () -> impl.setOption(-1, ""));351}352}353354/**355* Test setOption(int, Object) with invalid values.356*/357public void testSetOption4() throws IOException {358try (var impl = new PlatformSocketImpl(false)) {359impl.create(true);360expectThrows(SocketException.class,361() -> impl.setOption(SocketOptions.SO_REUSEADDR, -1));362expectThrows(SocketException.class,363() -> impl.setOption(SocketOptions.SO_TIMEOUT, -1));364expectThrows(SocketException.class,365() -> impl.setOption(SocketOptions.SO_SNDBUF, -1));366expectThrows(SocketException.class,367() -> impl.setOption(SocketOptions.SO_RCVBUF, -1));368}369}370371/**372* Test getOption when not created.373*/374public void testGetOption1() throws IOException {375var impl = new PlatformSocketImpl(false);376expectThrows(IOException.class,377() -> impl.getOption(StandardSocketOptions.SO_REUSEADDR));378expectThrows(SocketException.class,379() -> impl.getOption(-1));380}381382/**383* Test getOption when closed.384*/385public void testGetOption2() throws IOException {386var impl = new PlatformSocketImpl(false);387impl.close();388expectThrows(IOException.class,389() -> impl.getOption(StandardSocketOptions.SO_REUSEADDR));390expectThrows(SocketException.class,391() -> impl.getOption(SocketOptions.SO_REUSEADDR));392}393394/**395* Test getOption with unsupported option.396*/397public void testGetOption3() throws IOException {398try (var impl = new PlatformSocketImpl(false)) {399impl.create(true);400var opt = new SocketOption<String>() {401@Override public String name() { return "birthday"; }402@Override public Class<String> type() { return String.class; }403};404expectThrows(UnsupportedOperationException.class, () -> impl.getOption(opt));405expectThrows(SocketException.class, () -> impl.getOption(-1));406}407}408409/**410* Test shutdownInput when not created.411*/412public void testShutdownInput1() throws IOException {413var impl = new PlatformSocketImpl(false);414expectThrows(IOException.class, () -> impl.shutdownInput());415}416417/**418* Test shutdownInput when not connected.419*/420public void testShutdownInput2() throws IOException {421try (var impl = new PlatformSocketImpl(false)) {422impl.create(true);423expectThrows(IOException.class, () -> impl.shutdownInput());424}425}426427/**428* Test shutdownInput when closed.429*/430public void testShutdownInput3() throws IOException {431var impl = new PlatformSocketImpl(false);432impl.close();433expectThrows(IOException.class, () -> impl.shutdownInput());434}435436/**437* Test shutdownOutput when not created.438*/439public void testShutdownOutput1() throws IOException {440var impl = new PlatformSocketImpl(false);441expectThrows(IOException.class, () -> impl.shutdownOutput());442}443444/**445* Test shutdownOutput when not connected.446*/447public void testShutdownOutput2() throws IOException {448try (var impl = new PlatformSocketImpl(false)) {449impl.create(true);450expectThrows(IOException.class, () -> impl.shutdownOutput());451}452}453454/**455* Test shutdownOutput when closed.456*/457public void testShutdownOutput3() throws IOException {458var impl = new PlatformSocketImpl(false);459impl.close();460expectThrows(IOException.class, () -> impl.shutdownOutput());461}462463/**464* Test sendUrgentData when not created.465*/466public void testSendUrgentData1() throws IOException {467var impl = new PlatformSocketImpl(false);468expectThrows(IOException.class, () -> impl.sendUrgentData(0));469}470471/**472* Test sendUrgentData when not connected.473*/474public void testSendUrgentData2() throws IOException {475try (var impl = new PlatformSocketImpl(false)) {476impl.create(true);477expectThrows(IOException.class, () -> impl.sendUrgentData(0));478}479}480481/**482* Test sendUrgentData when closed.483*/484public void testSendUrgentData3() throws IOException {485var impl = new PlatformSocketImpl(false);486impl.close();487expectThrows(IOException.class, () -> impl.sendUrgentData(0));488}489}490491492