Path: blob/master/test/jdk/java/lang/ProcessBuilder/SkipTest.java
41149 views
/*1* Copyright (c) 2016, 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 815580826* @run main SkipTest27* @summary verify skip method of Process Input Stream28*/2930import java.io.BufferedOutputStream;31import java.io.File;32import java.io.IOException;33import java.io.InputStream;34import java.io.OutputStream;3536public class SkipTest {37private static final String javaExe =38System.getProperty("java.home") +39File.separator + "bin" + File.separator + "java";4041private static final String classpath =42System.getProperty("java.class.path");4344static final int BLOCK_SIZE = 10000;454647public static void main(String[] args) throws Throwable {4849// Start a Process to generate the test data to stdout and stderr50ProcessBuilder pb = new ProcessBuilder(javaExe, "-classpath", classpath, "SkipTest$GenerateData");51System.out.printf("cmd: %s%n", pb.command());52Process process = pb.start();5354/*55* Verify the data expected is received mixing reads and skip.56*/57try (InputStream in = process.getInputStream()) {5859// Note: Process.getInputStream() actually returns a BufferedInputStream whose60// skip() method works perfectly, partially obscuring the bug. Only when the61// BufferedInputStream's buffer is drained, and it passes the skip() call to62// the underlying native stream, does the problem occur.6364long n = in.skip(-1);65if (n != 0) {66throw new AssertionError("skip(-1) should return 0");67}68n = in.skip(0);69if (n != 0) {70throw new AssertionError("skip(0) should return 0");71}7273// Now iterate all the data blocks74int header;75for (int expectedHeader = 'A'; (header = in.read()) != -1; expectedHeader++) {76// The header byte should be simple 'A' to 'Z'.77// When the bug hits, we will get lowercase letters instead.78if (header != expectedHeader) {79throw new AssertionError("header char wrong, expected: " +80expectedHeader + ", actual: " + header);81}8283// Handle the data bytes.84// If the correct number of bytes are not skipped,85// then subsequent reads become out-of-sync;86int remaining = BLOCK_SIZE;87do {88remaining -= in.skip(remaining);89} while (remaining != 0);90}91n = in.skip(1);92if (n != 0) {93throw new AssertionError("skip(1) at eof should return 0");94}95}9697/**98* Do the same for the standard error stream.99*/100try (InputStream in = process.getErrorStream()) {101long n = in.skip(-1);102if (n != 0) {103throw new AssertionError("skip(-1) should return 0");104}105n = in.skip(0);106if (n != 0) {107throw new AssertionError("skip(0) should return 0");108}109110// Now iterate all the data blocks111int header;112for (int expectedHeader = 'A'; (header = in.read()) != -1; expectedHeader++) {113// The header byte should be simple 'A' to 'Z'.114// When the bug hits, we will get lowercase letters instead.115if (header != expectedHeader) {116throw new AssertionError("header char wrong, expected: " +117expectedHeader + ", actual: " + header);118}119120// Handle the data bytes.121// If the correct number of bytes are not skipped,122// then subsequent reads become out-of-sync;123int remaining = BLOCK_SIZE;124do {125remaining -= in.skip(remaining);126} while (remaining != 0);127}128n = in.skip(1);129if (n != 0) {130throw new AssertionError("skip(1) at eof should return 0");131}132}133}134135/**136* Alternate main to generate the test data to standard output137* and standard error.138*/139static class GenerateData {140141public static void main(String[] args) {142// Generate test data containing a series of data blocks of length BLOCK_SIZE,143// each with a one-byte header. For example's sake, the "header" is a capital letter,144// and the "data" is the lowercase version of that letter repeated BLOCK_SIZE times:145try (OutputStream out = new BufferedOutputStream(System.out)) {146for (int header = 'A'; header <= 'Z'; header++) {147out.write(header);148149int data = Character.toLowerCase(header);150for (int i = 0; i < BLOCK_SIZE; i++) {151out.write(data);152}153}154} catch (IOException ioe) {155ioe.printStackTrace();156System.exit(1);157}158// Generate the same data to the error output159try (OutputStream err = new BufferedOutputStream(System.err)) {160for (int header = 'A'; header <= 'Z'; header++) {161err.write(header);162163int data = Character.toLowerCase(header);164for (int i = 0; i < BLOCK_SIZE; i++) {165err.write(data);166}167}168} catch (IOException ioe) {169ioe.printStackTrace();170System.exit(1);171}172}173}174}175176177