1 /*
2  * Copyright (c) 1997, 2018, 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 4017158 8180410
27  * @library /test/lib
28  * @build jdk.test.lib.RandomFactory
29  * @run testng Write
30  * @summary Check for correct implementation of ByteArrayInputStream.write
31  * @key randomness
32  */
33 package test.java.io.ByteArrayOutputStream;
34 
35 import java.io.ByteArrayOutputStream;
36 import java.util.Arrays;
37 import java.util.Random;
38 import org.testng.annotations.Test;
39 import static org.testng.Assert.*;
40 
41 public class Write {
42 
doBoundsTest(byte[] b, int off, int len, ByteArrayOutputStream baos)43     private static void doBoundsTest(byte[] b, int off, int len,
44             ByteArrayOutputStream baos)
45             throws Exception {
46         if (b != null) {
47             System.out.println("ByteArrayOutStream.write: b.length = " +
48                     b.length + " off = " + off + " len = " + len);
49         } else{
50             System.out.println("ByteArrayOutStream.write: b is null off = " +
51                     off + " len = " + len);
52         }
53 
54         try {
55             baos.write(b, off, len);
56         } catch (IndexOutOfBoundsException e) {
57             System.out.println("IndexOutOfBoundsException is thrown: OKAY");
58         } catch (NullPointerException e) {
59             System.out.println("NullPointerException is thrown: OKAY");
60         } catch (Throwable e){
61             throw new RuntimeException("Unexpected Exception is thrown", e);
62         }
63 
64         if (b != null) {
65             System.out.println("ByteArrayOutStream.writeBytes: b.length = " +
66                     b.length);
67         } else{
68             System.out.println("ByteArrayOutStream.writeBytes: b is null");
69         }
70 
71         try {
72             baos.writeBytes(b);
73         } catch (NullPointerException e) {
74             System.out.println("NullPointerException is thrown: OKAY");
75         } catch (Throwable e){
76             throw new RuntimeException("Unexpected Exception is thrown", e);
77         }
78     }
79 
80     @Test
boundsTest()81     public void boundsTest() throws Exception {
82         byte array1[] = {1 , 2 , 3 , 4 , 5};     // Simple array
83 
84         //Create new ByteArrayOutputStream object
85         ByteArrayOutputStream y1 = new ByteArrayOutputStream(5);
86 
87         doBoundsTest(array1, 0, Integer.MAX_VALUE , y1);
88         doBoundsTest(array1, 0, array1.length+100, y1);
89         doBoundsTest(array1, -1, 2, y1);
90         doBoundsTest(array1, 0, -1, y1);
91         doBoundsTest(null, 0, 2, y1);
92     }
93 
94     @Test
writeTest()95     public void writeTest() throws Exception {
96         ByteArrayOutputStream baos = new ByteArrayOutputStream();
97         Random rnd = new Random();
98         final int size = 17 + rnd.nextInt(128);
99 
100         byte[] b = new byte[size];
101         rnd.nextBytes(b);
102 
103         int off1 = rnd.nextInt(size / 4) + 1;
104         int len1 = Math.min(rnd.nextInt(size / 4) + 1, size - off1);
105         int off2 = rnd.nextInt(size / 2) + 1;
106         int len2 = Math.min(rnd.nextInt(size / 2) + 1, size - off2);
107 
108         System.out.format("size: %d, off1: %d, len1: %d, off2: %d, len2: %d%n",
109                 size, off1, len1, off2, len2);
110 
111         baos.write(b, off1, len1);
112         byte[] b1 = baos.toByteArray();
113         assertEquals(b1.length, len1, "Array length test 1 failed.");
114         assertEquals(b1, Arrays.copyOfRange(b, off1, off1 + len1),
115                 "Array equality test 1 failed.");
116 
117         baos.write(b, off2, len2);
118         byte[] b2 = baos.toByteArray();
119         assertEquals(b2.length, len1 + len2, "Array length test 2 failed.");
120         assertEquals(Arrays.copyOfRange(b2, 0, len1),
121                 Arrays.copyOfRange(b, off1, off1 + len1),
122                 "Array equality test 2A failed.");
123         assertEquals(Arrays.copyOfRange(b2, len1, len1 + len2),
124                 Arrays.copyOfRange(b, off2, off2 + len2),
125                 "Array equality test 2B failed.");
126 
127         baos.writeBytes(b);
128         byte[] b3 = baos.toByteArray();
129         int len3 = len1 + len2 + b.length;
130         if (b3.length != len1 + len2 + b.length) {
131             throw new RuntimeException("Array length test 3 failed.");
132         }
133         assertEquals(b3.length, len3, "Array length test 3 failed.");
134         assertEquals(Arrays.copyOfRange(b3, 0, len1),
135                 Arrays.copyOfRange(b, off1, off1 + len1),
136                 "Array equality test 3A failed.");
137         assertEquals(Arrays.copyOfRange(b3, len1, len1 + len2),
138                 Arrays.copyOfRange(b, off2, off2 + len2),
139                 "Array equality test 3B failed.");
140         assertEquals(Arrays.copyOfRange(b3, len1 + len2, len3), b,
141                 "Array equality test 3C failed.");
142     }
143 }