1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
|
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8221481
* @compile/module=java.base java/net/PlatformSocketImpl.java
* @run testng/othervm BadUsages
* @summary Test the platform SocketImpl when used in unintended ways
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.SocketImpl;
import java.net.SocketOption;
import java.net.SocketOptions;
import java.net.StandardSocketOptions;
import java.util.Set;
import java.net.PlatformSocketImpl; // test helper
import org.testng.annotations.Test;
import static org.testng.Assert.*;
/**
* SocketImpl does not specify how the SocketImpl behaves when used in ways
* that are not intended, e.g. invoking socket operations before the socket is
* created or trying to establish a connection after the socket is connected or
* closed.
*
* This test exercises the platform SocketImpl to test that it is reliable, and
* throws reasonable exceptions, for these scenarios.
*/
@Test
public class BadUsages {
/**
* Test create when already created.
*/
public void testCreate1() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
expectThrows(IOException.class, () -> impl.create(true));
}
}
/**
* Test create when closed.
*/
public void testCreate2() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
expectThrows(IOException.class, () -> impl.create(true));
}
/**
* Test connect when not created.
*/
public void testConnect1() throws IOException {
try (var ss = new ServerSocket(0)) {
var impl = new PlatformSocketImpl(false);
var address = ss.getInetAddress();
int port = ss.getLocalPort();
expectThrows(IOException.class, () -> impl.connect(address, port));
}
}
/**
* Test connect with unsupported address type.
*/
public void testConnect2() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
var remote = new SocketAddress() { };
expectThrows(IOException.class, () -> impl.connect(remote, 0));
}
}
/**
* Test connect with an unresolved address.
*/
public void testConnect3() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
var remote = new InetSocketAddress("blah-blah.blah-blah", 80);
expectThrows(IOException.class, () -> impl.connect(remote, 0));
}
}
/**
* Test connect when already connected.
*/
public void testConnect4() throws IOException {
try (var ss = new ServerSocket();
var impl = new PlatformSocketImpl(false)) {
var loopback = InetAddress.getLoopbackAddress();
ss.bind(new InetSocketAddress(loopback, 0));
impl.create(true);
int port = ss.getLocalPort();
impl.connect(loopback, port);
expectThrows(IOException.class, () -> impl.connect(loopback, port));
}
}
/**
* Test connect when closed.
*/
public void testConnect5() throws IOException {
try (var ss = new ServerSocket(0)) {
var impl = new PlatformSocketImpl(false);
impl.close();
String host = ss.getInetAddress().getHostAddress();
int port = ss.getLocalPort();
expectThrows(IOException.class, () -> impl.connect(host, port));
}
}
/**
* Test bind when not created.
*/
public void testBind1() throws IOException {
var impl = new PlatformSocketImpl(false);
var loopback = InetAddress.getLoopbackAddress();
expectThrows(IOException.class, () -> impl.bind(loopback, 0));
}
/**
* Test bind when already bound.
*/
public void testBind2() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
var loopback = InetAddress.getLoopbackAddress();
impl.bind(loopback, 0);
expectThrows(IOException.class, () -> impl.bind(loopback, 0));
}
}
/**
* Test bind when connected.
*/
public void testBind3() throws IOException {
try (var ss = new ServerSocket();
var impl = new PlatformSocketImpl(false)) {
var loopback = InetAddress.getLoopbackAddress();
ss.bind(new InetSocketAddress(loopback, 0));
impl.create(true);
impl.connect(ss.getLocalSocketAddress(), 0);
expectThrows(IOException.class, () -> impl.bind(loopback, 0));
}
}
/**
* Test bind when closed.
*/
public void testBind4() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
var loopback = InetAddress.getLoopbackAddress();
expectThrows(IOException.class, () -> impl.bind(loopback, 0));
}
/**
* Test listen when not created.
*/
public void testListen1() {
var impl = new PlatformSocketImpl(false);
expectThrows(IOException.class, () -> impl.listen(16));
}
/**
* Test listen when not bound.
*/
public void testListen2() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
expectThrows(IOException.class, () -> impl.listen(16));
}
}
/**
* Test listen when closed.
*/
public void testListen3() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
expectThrows(IOException.class, () -> impl.listen(16));
}
/**
* Test accept when not created.
*/
public void testAccept1() throws IOException {
var impl = new PlatformSocketImpl(true);
var si = new PlatformSocketImpl(false);
expectThrows(IOException.class, () -> impl.accept(si));
}
/**
* Test accept when not bound.
*/
public void testAccept2() throws IOException {
try (var impl = new PlatformSocketImpl(true)) {
impl.create(true);
var si = new PlatformSocketImpl(false);
expectThrows(IOException.class, () -> impl.accept(si));
}
}
/**
* Test accept when not a stream socket.
*/
public void testAccept3() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(false);
impl.bind(InetAddress.getLoopbackAddress(), 0);
var si = new PlatformSocketImpl(false);
expectThrows(IOException.class, () -> impl.accept(si));
}
}
/**
* Test accept when closed.
*/
public void testAccept4() throws IOException {
var impl = new PlatformSocketImpl(true);
impl.close();
var si = new PlatformSocketImpl(false);
expectThrows(IOException.class, () -> impl.accept(si));
}
/**
* Test accept with SocketImpl that is already created.
*/
public void testAccept5() throws IOException {
try (var impl = new PlatformSocketImpl(true);
var si = new PlatformSocketImpl(false)) {
impl.create(true);
impl.bind(InetAddress.getLoopbackAddress(), 0);
si.create(true);
expectThrows(IOException.class, () -> impl.accept(si));
}
}
/**
* Test accept with SocketImpl that is closed.
*/
public void testAccept6() throws IOException {
try (var impl = new PlatformSocketImpl(true);
var si = new PlatformSocketImpl(false)) {
impl.create(true);
impl.bind(InetAddress.getLoopbackAddress(), 0);
si.create(true);
si.close();
expectThrows(IOException.class, () -> impl.accept(si));
}
}
/**
* Test available when not created.
*/
public void testAvailable1() throws IOException {
var impl = new PlatformSocketImpl(false);
expectThrows(IOException.class, () -> impl.available());
}
/**
* Test available when created but not connected.
*/
public void testAvailable2() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
expectThrows(IOException.class, () -> impl.available());
}
}
/**
* Test available when closed.
*/
public void testAvailable3() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
expectThrows(IOException.class, () -> impl.available());
}
/**
* Test setOption when not created.
*/
public void testSetOption1() throws IOException {
var impl = new PlatformSocketImpl(false);
expectThrows(IOException.class,
() -> impl.setOption(StandardSocketOptions.SO_REUSEADDR, true));
// legacy
expectThrows(SocketException.class,
() -> impl.setOption(SocketOptions.SO_REUSEADDR, true));
}
/**
* Test setOption when closed.
*/
public void testSetOption2() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
expectThrows(IOException.class,
() -> impl.setOption(StandardSocketOptions.SO_REUSEADDR, true));
// legacy
expectThrows(SocketException.class,
() -> impl.setOption(SocketOptions.SO_REUSEADDR, true));
}
/**
* Test setOption with unsupported option.
*/
public void testSetOption3() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
var opt = new SocketOption<String>() {
@Override public String name() { return "birthday"; }
@Override public Class<String> type() { return String.class; }
};
expectThrows(UnsupportedOperationException.class, () -> impl.setOption(opt, ""));
// legacy
expectThrows(SocketException.class, () -> impl.setOption(-1, ""));
}
}
/**
* Test setOption(int, Object) with invalid values.
*/
public void testSetOption4() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
expectThrows(SocketException.class,
() -> impl.setOption(SocketOptions.SO_REUSEADDR, -1));
expectThrows(SocketException.class,
() -> impl.setOption(SocketOptions.SO_TIMEOUT, -1));
expectThrows(SocketException.class,
() -> impl.setOption(SocketOptions.SO_SNDBUF, -1));
expectThrows(SocketException.class,
() -> impl.setOption(SocketOptions.SO_RCVBUF, -1));
}
}
/**
* Test getOption when not created.
*/
public void testGetOption1() throws IOException {
var impl = new PlatformSocketImpl(false);
expectThrows(IOException.class,
() -> impl.getOption(StandardSocketOptions.SO_REUSEADDR));
expectThrows(SocketException.class,
() -> impl.getOption(-1));
}
/**
* Test getOption when closed.
*/
public void testGetOption2() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
expectThrows(IOException.class,
() -> impl.getOption(StandardSocketOptions.SO_REUSEADDR));
expectThrows(SocketException.class,
() -> impl.getOption(SocketOptions.SO_REUSEADDR));
}
/**
* Test getOption with unsupported option.
*/
public void testGetOption3() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
var opt = new SocketOption<String>() {
@Override public String name() { return "birthday"; }
@Override public Class<String> type() { return String.class; }
};
expectThrows(UnsupportedOperationException.class, () -> impl.getOption(opt));
expectThrows(SocketException.class, () -> impl.getOption(-1));
}
}
/**
* Test shutdownInput when not created.
*/
public void testShutdownInput1() throws IOException {
var impl = new PlatformSocketImpl(false);
expectThrows(IOException.class, () -> impl.shutdownInput());
}
/**
* Test shutdownInput when not connected.
*/
public void testShutdownInput2() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
expectThrows(IOException.class, () -> impl.shutdownInput());
}
}
/**
* Test shutdownInput when closed.
*/
public void testShutdownInput3() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
expectThrows(IOException.class, () -> impl.shutdownInput());
}
/**
* Test shutdownOutput when not created.
*/
public void testShutdownOutput1() throws IOException {
var impl = new PlatformSocketImpl(false);
expectThrows(IOException.class, () -> impl.shutdownOutput());
}
/**
* Test shutdownOutput when not connected.
*/
public void testShutdownOutput2() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
expectThrows(IOException.class, () -> impl.shutdownOutput());
}
}
/**
* Test shutdownOutput when closed.
*/
public void testShutdownOutput3() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
expectThrows(IOException.class, () -> impl.shutdownOutput());
}
/**
* Test sendUrgentData when not created.
*/
public void testSendUrgentData1() throws IOException {
var impl = new PlatformSocketImpl(false);
expectThrows(IOException.class, () -> impl.sendUrgentData(0));
}
/**
* Test sendUrgentData when not connected.
*/
public void testSendUrgentData2() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
expectThrows(IOException.class, () -> impl.sendUrgentData(0));
}
}
/**
* Test sendUrgentData when closed.
*/
public void testSendUrgentData3() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
expectThrows(IOException.class, () -> impl.sendUrgentData(0));
}
}
|