/* * Copyright (c) 2015, 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. */ import org.testng.ITestResult; import org.testng.annotations.AfterMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.CharArrayReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.io.PipedReader; import java.io.PipedWriter; import java.io.PushbackReader; import java.io.Reader; import java.io.StringReader; import java.io.UncheckedIOException; import static org.testng.Assert.assertEquals; /* * @test * @bug 8029689 * @summary checks the bounds part of the contract of java.io.Reader.read(char[], int, int): * * 0 <= off <= off+len <= cbuf.length * * for publicly exported subtypes of java.io.Reader * * @run testng ReaderBulkReadContract */ public class ReaderBulkReadContract { @DataProvider(name = "readers") public Object[][] createReaders() throws IOException { return new Object[][] { {new BufferedReader(new StringReader(""))}, {new BufferedReader(new StringReader("abc"))}, {new LineNumberReader(new StringReader(""))}, {new LineNumberReader(new StringReader("abc"))}, {new CharArrayReader(new char[]{})}, {new CharArrayReader(new char[]{'a', 'b', 'c'})}, {new InputStreamReader(new ByteArrayInputStream(new byte[]{}))}, {new InputStreamReader(new ByteArrayInputStream(new byte[]{'a', 'b', 'c'}))}, {newFileReader("")}, {newFileReader("abc")}, {new PushbackReader(new StringReader(""))}, {new PushbackReader(new StringReader("abc"))}, {newPipedReader("")}, {newPipedReader("abc")}, {new StringReader("")}, {new StringReader("abc")}, }; } @Test(dataProvider = "readers", expectedExceptions = IndexOutOfBoundsException.class) void off_negative_len_zero(Reader r) throws IOException { r.read(new char[8], -1, 0); } @Test(dataProvider = "readers", expectedExceptions = IndexOutOfBoundsException.class) void off_negative_len_positive(Reader r) throws IOException { r.read(new char[8], -1, 1); } @Test(dataProvider = "readers", expectedExceptions = IndexOutOfBoundsException.class) void len_negative(Reader r) throws IOException { r.read(new char[8], 0, -1); } @Test(dataProvider = "readers", expectedExceptions = IndexOutOfBoundsException.class) void integer_overflow(Reader r) throws IOException { r.read(new char[8], 4, Integer.MAX_VALUE); } @Test(dataProvider = "readers", expectedExceptions = IndexOutOfBoundsException.class) void off_plus_len_greater_than_buffers_length(Reader r) throws IOException { r.read(new char[8], 5, 4); } @Test(dataProvider = "readers") void subarray_N_0(Reader r) throws IOException { int read = r.read(new char[8], 8, 0); assertEquals(read, 0); } @Test(dataProvider = "readers") void subarray_8_0_0(Reader r) throws IOException { int read = r.read(new char[8], 0, 0); assertEquals(read, 0); } @Test(dataProvider = "readers") void subarray_0_0_0(Reader r) throws IOException { int read = r.read(new char[0], 0, 0); assertEquals(read, 0); } @AfterMethod void close(ITestResult r) throws IOException { Reader reader = (Reader) r.getParameters()[0]; reader.close(); } private static FileReader newFileReader(String contents) { try { File f = File.createTempFile("ReaderContract", ""); try (FileWriter w = new FileWriter(f)) { w.write(contents); } return new FileReader(f); } catch (IOException e) { throw new UncheckedIOException(e); } } private static PipedReader newPipedReader(String contents) throws IOException { try (PipedWriter w = new PipedWriter()) { PipedReader r = new PipedReader(w); w.write(contents); return r; } catch (IOException e) { throw new UncheckedIOException(e); } } }