Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Socket/reset/Test.java
38828 views
/*1* Copyright (c) 2002, 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* @bug 451762225* @summary SocketException on first read after error; -1 on subsequent reads26*/27import java.net.*;28import java.io.*;29import java.util.Random;3031public class Test {3233static int TEST_COMBINATIONS = 5;3435// test server that cycles through each combination of response3637static class Server extends Thread {38ServerSocket ss;3940public Server() throws IOException {41ss = new ServerSocket(0);42System.out.println("Server listening on port: " + getPort());43}4445public void run() {4647int testCombination = 0;4849try {50for (;;) {51Socket s = ss.accept();5253switch (testCombination) {54case 0:55s.setTcpNoDelay(false);56s.getOutputStream().write(new byte[256]);57s.setSoLinger(true, 0);58break;5960case 1:61s.setTcpNoDelay(true);62s.getOutputStream().write(new byte[256]);63s.setSoLinger(true, 0);64break;6566case 2:67s.getOutputStream().write("hello".getBytes());68s.setSoLinger(true, 0);69break;7071case 3:72break; /* EOF test */7374case 4:75s.getOutputStream().write(new byte[256]);76break;77}7879s.close();8081testCombination = (testCombination + 1) % TEST_COMBINATIONS;82}83} catch (IOException ioe) {84if (!ss.isClosed()) {85System.err.println("Server failed: " + ioe);86}87}88}8990public int getPort() {91return ss.getLocalPort();92}9394public void shutdown() {95try {96ss.close();97} catch (IOException ioe) { }98}99100}101102static final int STATE_DATA = 0;103static final int STATE_EOF = 1;104static final int STATE_IOE = 2;105106static void Test(SocketAddress sa) throws Exception {107System.out.println("-----------");108109Socket s = new Socket();110s.connect(sa);111112byte b[] = new byte[50];113int state = STATE_DATA;114boolean failed = false;115116Random rand = new Random();117118for (int i=0; i<200; i++) {119switch (rand.nextInt(4)) {120case 0:121try {122s.getOutputStream().write("data".getBytes());123} catch (IOException ioe) { }124break;125126case 1:127try {128int n = s.getInputStream().available();129130// available should never return > 0 if read131// has already thrown IOE or returned EOF132133if (n > 0 && state != STATE_DATA) {134System.out.println("FAILED!! available: " + n +135" (unexpected as IOE or EOF already received)");136failed = true;137}138} catch (IOException ioe) {139System.out.println("FAILED!!! available: " + ioe);140failed = true;141}142break;143144case 2:145try {146int n = s.getInputStream().read(b);147148if (n > 0 && state == STATE_IOE) {149System.out.println("FAILED!! read: " + n +150" (unexpected as IOE already thrown)");151failed = true;152}153154if (n > 0 && state == STATE_EOF) {155System.out.println("FAILED!! read: " + n +156" (unexpected as EOF already received)");157failed = true;158}159160if (n < 0) {161if (state == STATE_IOE) {162System.out.println("FAILED!! read: EOF " +163" (unexpected as IOE already thrown)");164failed = true;165}166if (state != STATE_EOF) {167System.out.println("read: EOF");168state = STATE_EOF;169}170}171172} catch (IOException ioe) {173if (state == STATE_EOF) {174System.out.println("FAILED!! read: " + ioe +175" (unexpected as EOF already received)");176failed = true;177}178if (state != STATE_IOE) {179System.out.println("read: " + ioe);180state = STATE_IOE;181}182}183break;184185case 3:186try {187Thread.currentThread().sleep(100);188} catch (Exception ie) { }189}190191if (failed) {192failures++;193break;194}195}196197s.close();198}199200static int failures = 0;201202public static void main(String args[]) throws Exception {203SocketAddress sa = null;204Server svr = null;205206// server mode only207if (args.length > 0) {208if (args[0].equals("-server")) {209svr = new Server();210svr.start();211return;212}213}214215// run standalone or connect to remote server216if (args.length > 0) {217InetAddress rh = InetAddress.getByName(args[0]);218int port = Integer.parseInt(args[1]);219sa = new InetSocketAddress(rh, port);220} else {221svr = new Server();222svr.start();223224InetAddress lh = InetAddress.getLocalHost();225sa = new InetSocketAddress(lh, svr.getPort());226}227228for (int i=0; i<10; i++) {229Test(sa);230}231232if (svr != null) {233svr.shutdown();234}235236if (failures > 0) {237throw new Exception(failures + " sub-test(s) failed.");238}239}240}241242243