1 /*
   2  * Copyright (c) 2015, 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 import org.testng.ITestResult;
  25 import org.testng.annotations.AfterMethod;
  26 import org.testng.annotations.DataProvider;
  27 import org.testng.annotations.Test;
  28 
  29 import java.io.BufferedReader;
  30 import java.io.ByteArrayInputStream;
  31 import java.io.CharArrayReader;
  32 import java.io.File;
  33 import java.io.FileReader;
  34 import java.io.FileWriter;
  35 import java.io.IOException;
  36 import java.io.InputStreamReader;
  37 import java.io.LineNumberReader;
  38 import java.io.PipedReader;
  39 import java.io.PipedWriter;
  40 import java.io.PushbackReader;
  41 import java.io.Reader;
  42 import java.io.StringReader;
  43 import java.io.UncheckedIOException;
  44 
  45 import static org.testng.Assert.assertEquals;
  46 
  47 /*
  48  * @test
  49  * @bug 8029689
  50  * @summary checks the bounds part of the contract of java.io.Reader.read(char[], int, int):
  51  *
  52  *              0 <= off <= off+len <= cbuf.length
  53  *
  54  *          for publicly exported subtypes of java.io.Reader
  55  *
  56  * @run testng ReaderBulkReadContract
  57  */
  58 public class ReaderBulkReadContract {
  59 
  60     @DataProvider(name = "readers")
  61     public Object[][] createReaders() throws IOException {
  62         return new Object[][]
  63                 {
  64                         {new BufferedReader(new StringReader(""))},
  65                         {new BufferedReader(new StringReader("abc"))},
  66                         {new LineNumberReader(new StringReader(""))},
  67                         {new LineNumberReader(new StringReader("abc"))},
  68                         {new CharArrayReader(new char[]{})},
  69                         {new CharArrayReader(new char[]{'a', 'b', 'c'})},
  70                         {new InputStreamReader(new ByteArrayInputStream(new byte[]{}))},
  71                         {new InputStreamReader(new ByteArrayInputStream(new byte[]{'a', 'b', 'c'}))},
  72                         {newFileReader("")},
  73                         {newFileReader("abc")},
  74                         {new PushbackReader(new StringReader(""))},
  75                         {new PushbackReader(new StringReader("abc"))},
  76                         {newPipedReader("")},
  77                         {newPipedReader("abc")},
  78                         {new StringReader("")},
  79                         {new StringReader("abc")},
  80                 };
  81     }
  82 
  83     @Test(dataProvider = "readers", expectedExceptions = IndexOutOfBoundsException.class)
  84     void off_negative_len_zero(Reader r) throws IOException {
  85         r.read(new char[8], -1, 0);
  86     }
  87 
  88     @Test(dataProvider = "readers", expectedExceptions = IndexOutOfBoundsException.class)
  89     void off_negative_len_positive(Reader r) throws IOException {
  90         r.read(new char[8], -1, 1);
  91     }
  92 
  93     @Test(dataProvider = "readers", expectedExceptions = IndexOutOfBoundsException.class)
  94     void len_negative(Reader r) throws IOException {
  95         r.read(new char[8], 0, -1);
  96     }
  97 
  98     @Test(dataProvider = "readers", expectedExceptions = IndexOutOfBoundsException.class)
  99     void integer_overflow(Reader r) throws IOException {
 100         r.read(new char[8], 4, Integer.MAX_VALUE);
 101     }
 102 
 103     @Test(dataProvider = "readers", expectedExceptions = IndexOutOfBoundsException.class)
 104     void off_plus_len_greater_than_buffers_length(Reader r) throws IOException {
 105         r.read(new char[8], 5, 4);
 106     }
 107 
 108     @Test(dataProvider = "readers")
 109     void subarray_N_0(Reader r) throws IOException {
 110         int read = r.read(new char[8], 8, 0);
 111         assertEquals(read, 0);
 112     }
 113 
 114     @Test(dataProvider = "readers")
 115     void subarray_8_0_0(Reader r) throws IOException {
 116         int read = r.read(new char[8], 0, 0);
 117         assertEquals(read, 0);
 118     }
 119 
 120     @Test(dataProvider = "readers")
 121     void subarray_0_0_0(Reader r) throws IOException {
 122         int read = r.read(new char[0], 0, 0);
 123         assertEquals(read, 0);
 124     }
 125 
 126     @AfterMethod
 127     void close(ITestResult r) throws IOException {
 128         Reader reader = (Reader) r.getParameters()[0];
 129         reader.close();
 130     }
 131 
 132     private static FileReader newFileReader(String contents) {
 133         try {
 134             File f = File.createTempFile("ReaderContract", "");
 135             try (FileWriter w = new FileWriter(f)) {
 136                 w.write(contents);
 137             }
 138             return new FileReader(f);
 139         } catch (IOException e) {
 140             throw new UncheckedIOException(e);
 141         }
 142     }
 143 
 144     private static PipedReader newPipedReader(String contents) throws IOException {
 145         try (PipedWriter w = new PipedWriter()) {
 146             PipedReader r = new PipedReader(w);
 147             w.write(contents);
 148             return r;
 149         } catch (IOException e) {
 150             throw new UncheckedIOException(e);
 151         }
 152     }
 153 }