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 import java.util.Arrays;
  45 import java.util.Collections;
  46 import java.util.Iterator;
  47 import java.util.LinkedList;
  48 import java.util.List;
  49 import java.util.concurrent.ConcurrentHashMap;
  50 import java.util.function.Function;
  51 
  52 import static org.testng.Assert.assertEquals;
  53 
  54 /*
  55  * @test
  56  * @bug 8029689
  57  * @summary checks the bounds part of the contract of java.io.Reader.read(char[], int, int):
  58  *
  59  *              0 <= off <= off+len <= cbuf.length
  60  *
  61  *          for publicly exported subtypes of java.io.Reader
  62  *
  63  * @run testng ReaderBulkReadContract
  64  */
  65 public class ReaderBulkReadContract {
  66 
  67     @DataProvider(name = "args", parallel = false)
  68     public Iterator<Object[]> args() {
  69 
  70         Integer[] lens = {Integer.MIN_VALUE, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, Integer.MAX_VALUE};
  71         Integer[] offs = {Integer.MIN_VALUE, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, Integer.MAX_VALUE};
  72         Integer[] sizes = {0, 1, 2, 3, 4, 5};
  73         String[] contents = {"", "a", "ab"};
  74 
  75         List<Function<String, Reader>> fs = Arrays.asList(
  76                 (String s) -> new BufferedReader(new StringReader(s)),
  77                 (String s) -> new LineNumberReader(new StringReader(s)),
  78                 (String s) -> new CharArrayReader(s.toCharArray()),
  79                 (String s) -> new InputStreamReader(new ByteArrayInputStream(s.getBytes())),
  80                 (String s) -> newFileReader(s),
  81                 (String s) -> new PushbackReader(new StringReader(s)),
  82                 (String s) -> newPipedReader(s),
  83                 (String s) -> new StringReader(s)
  84         );
  85 
  86         // The easiest way to produce a cartesian product from a small fixed number of sets
  87         List<Object[]> tuples = Collections.synchronizedList(new LinkedList<>());
  88         for (Integer len : lens)
  89             for (Integer off : offs)
  90                 for (String s : contents)
  91                     for (Integer size : sizes)
  92                         for (Function<String, Reader> f : fs)
  93                             tuples.add(new Object[]{f.apply(s), size, off, len});
  94 
  95         return tuples.iterator();
  96     }
  97 
  98     @Test(dataProvider = "args")
  99     public void read(Reader r, int size, int off, int len) throws IOException {
 100         IndexOutOfBoundsException ex = null;
 101         try {
 102             r.read(new char[size], off, len);
 103         } catch (IndexOutOfBoundsException e) {
 104             ex = e;
 105         }
 106 
 107         boolean incorrectBounds = off < 0 || len < 0 || len > size - off;
 108         boolean exceptionThrown = ex != null;
 109 
 110         assertEquals(incorrectBounds, exceptionThrown);
 111     }
 112 
 113     @AfterMethod
 114     public void close(ITestResult r) throws IOException {
 115         Reader reader = (Reader) r.getParameters()[0];
 116         reader.close();
 117     }
 118 
 119     private static PipedReader newPipedReader(String contents) {
 120         try (PipedWriter w = new PipedWriter()) {
 121             PipedReader r = new PipedReader(w);
 122             w.write(contents);
 123             return r;
 124         } catch (IOException e) {
 125             throw new UncheckedIOException(e);
 126         }
 127     }
 128 
 129     private FileReader newFileReader(String contents) {
 130         try {
 131             // To not create an enormous amount of files
 132             File f = cache.computeIfAbsent(contents,
 133                     ReaderBulkReadContract::createTempFileWithContents);
 134             return new FileReader(f);
 135         } catch (IOException e) {
 136             throw new UncheckedIOException(e);
 137         }
 138     }
 139 
 140     private static File createTempFileWithContents(String contents) {
 141         try {
 142             File file = File.createTempFile("ReaderContract", "");
 143 //            System.out.printf("Created a file '%s' with contents: %s%n", file, contents);
 144             try (FileWriter w = new FileWriter(file)) {
 145                 w.write(contents);
 146             }
 147             return file;
 148         } catch (IOException e) {
 149             throw new UncheckedIOException(e);
 150         }
 151     }
 152 
 153     // Do not use anything other than CHM as a cache implementation here,
 154     // otherwise you could pollute your drive (hope it's not an SSD) with
 155     // myriads of tiny files. CHM promises a crucial thing:
 156     //
 157     // ...The entire method invocation is performed atomically, so the
 158     // function is applied at most once per key...
 159     //
 160     private final ConcurrentHashMap<String, File> cache = new ConcurrentHashMap<>();
 161 }