1 /*
  2  * Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 8155808
 27  * @requires vm.flagless
 28  * @run main SkipTest
 29  * @summary verify skip method of Process Input Stream
 30  */
 31 
 32 import java.io.BufferedOutputStream;
 33 import java.io.File;
 34 import java.io.IOException;
 35 import java.io.InputStream;
 36 import java.io.OutputStream;
 37 
 38 public class SkipTest {
 39     private static final String javaExe =
 40             System.getProperty("java.home") +
 41                     File.separator + "bin" + File.separator + "java";
 42 
 43     private static final String classpath =
 44             System.getProperty("java.class.path");
 45 
 46     static final int BLOCK_SIZE = 10000;
 47 
 48 
 49     public static void main(String[] args) throws Throwable {
 50 
 51         // Start a Process to generate the test data to stdout and stderr
 52         ProcessBuilder pb = new ProcessBuilder(javaExe, "-classpath", classpath, "SkipTest$GenerateData");
 53         System.out.printf("cmd: %s%n", pb.command());
 54         Process process = pb.start();
 55 
 56         /*
 57          * Verify the data expected is received mixing reads and skip.
 58          */
 59         try (InputStream in = process.getInputStream()) {
 60 
 61             // Note: Process.getInputStream() actually returns a BufferedInputStream whose
 62             // skip() method works perfectly, partially obscuring the bug. Only when the
 63             // BufferedInputStream's buffer is drained, and it passes the skip() call to
 64             // the underlying native stream, does the problem occur.
 65 
 66             long n = in.skip(-1);
 67             if (n != 0) {
 68                 throw new AssertionError("skip(-1) should return 0");
 69             }
 70             n = in.skip(0);
 71             if (n != 0) {
 72                 throw new AssertionError("skip(0) should return 0");
 73             }
 74 
 75             // Now iterate all the data blocks
 76             int header;
 77             for (int expectedHeader = 'A'; (header = in.read()) != -1; expectedHeader++) {
 78                 // The header byte should be simple 'A' to 'Z'.
 79                 // When the bug hits, we will get lowercase letters instead.
 80                 if (header != expectedHeader) {
 81                     throw new AssertionError("header char wrong, expected: " +
 82                             expectedHeader + ", actual: " + header);
 83                 }
 84 
 85                 // Handle the data bytes.
 86                 // If the correct number of bytes are not skipped,
 87                 // then subsequent reads become out-of-sync;
 88                 int remaining = BLOCK_SIZE;
 89                 do {
 90                     remaining -= in.skip(remaining);
 91                 } while (remaining != 0);
 92             }
 93             n = in.skip(1);
 94             if (n != 0) {
 95                 throw new AssertionError("skip(1) at eof should return 0");
 96             }
 97         }
 98 
 99         /**
100          * Do the same for the standard error stream.
101          */
102         try (InputStream in = process.getErrorStream()) {
103             long n = in.skip(-1);
104             if (n != 0) {
105                 throw new AssertionError("skip(-1) should return 0");
106             }
107             n = in.skip(0);
108             if (n != 0) {
109                 throw new AssertionError("skip(0) should return 0");
110             }
111 
112             // Now iterate all the data blocks
113             int header;
114             for (int expectedHeader = 'A'; (header = in.read()) != -1; expectedHeader++) {
115                 // The header byte should be simple 'A' to 'Z'.
116                 // When the bug hits, we will get lowercase letters instead.
117                 if (header != expectedHeader) {
118                     throw new AssertionError("header char wrong, expected: " +
119                             expectedHeader + ", actual: " + header);
120                 }
121 
122                 // Handle the data bytes.
123                 // If the correct number of bytes are not skipped,
124                 // then subsequent reads become out-of-sync;
125                 int remaining = BLOCK_SIZE;
126                 do {
127                     remaining -= in.skip(remaining);
128                 } while (remaining != 0);
129             }
130             n = in.skip(1);
131             if (n != 0) {
132                 throw new AssertionError("skip(1) at eof should return 0");
133             }
134         }
135     }
136 
137     /**
138      * Alternate main to generate the test data to standard output
139      * and standard error.
140      */
141     static class GenerateData {
142 
143         public static void main(String[] args) {
144             // Generate test data containing a series of data blocks of length BLOCK_SIZE,
145             // each with a one-byte header. For example's sake, the "header" is a capital letter,
146             // and the "data" is the lowercase version of that letter repeated BLOCK_SIZE times:
147             try (OutputStream out = new BufferedOutputStream(System.out)) {
148                 for (int header = 'A'; header <= 'Z'; header++) {
149                     out.write(header);
150 
151                     int data = Character.toLowerCase(header);
152                     for (int i = 0; i < BLOCK_SIZE; i++) {
153                         out.write(data);
154                     }
155                 }
156             } catch (IOException ioe) {
157                 ioe.printStackTrace();
158                 System.exit(1);
159             }
160             // Generate the same data to the error output
161             try (OutputStream err = new BufferedOutputStream(System.err)) {
162                 for (int header = 'A'; header <= 'Z'; header++) {
163                     err.write(header);
164 
165                     int data = Character.toLowerCase(header);
166                     for (int i = 0; i < BLOCK_SIZE; i++) {
167                         err.write(data);
168                     }
169                 }
170             } catch (IOException ioe) {
171                 ioe.printStackTrace();
172                 System.exit(1);
173             }
174         }
175     }
176 }