1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17package org.apache.harmony.tests.java.io;
18
19import java.io.ByteArrayInputStream;
20import java.io.ByteArrayOutputStream;
21import java.io.DataInput;
22import java.io.DataInputStream;
23import java.io.DataOutputStream;
24import java.io.EOFException;
25import java.io.IOException;
26import java.io.InputStream;
27
28public class DataInputStreamTest extends junit.framework.TestCase {
29
30    private DataOutputStream os;
31
32    private DataInputStream dis;
33
34    private ByteArrayOutputStream bos;
35
36    String unihw = "\u0048\u0065\u006C\u006C\u006F\u0020\u0057\u006F\u0072\u006C\u0064";
37
38    public String fileString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_DataInputStream\n";
39
40    /**
41     * java.io.DataInputStream#DataInputStream(java.io.InputStream)
42     */
43    public void test_ConstructorLjava_io_InputStream() throws IOException {
44        try {
45            os.writeChar('t');
46            os.close();
47            openDataInputStream();
48        } finally {
49            dis.close();
50        }
51    }
52
53    /**
54     * java.io.DataInputStream#read(byte[])
55     */
56    public void test_read$B() throws IOException {
57        os.write(fileString.getBytes());
58        os.close();
59        openDataInputStream();
60        byte rbytes[] = new byte[fileString.length()];
61        dis.read(rbytes);
62        assertTrue("Incorrect data read", new String(rbytes, 0, fileString
63                .length()).equals(fileString));
64    }
65
66    /**
67     * java.io.DataInputStream#read(byte[], int, int)
68     */
69    public void test_read$BII() throws IOException {
70        os.write(fileString.getBytes());
71        os.close();
72        openDataInputStream();
73        byte rbytes[] = new byte[fileString.length()];
74        dis.read(rbytes, 0, rbytes.length);
75        assertTrue("Incorrect data read", new String(rbytes, 0, fileString
76                .length()).equals(fileString));
77    }
78
79    /**
80     * java.io.DataInputStream#readBoolean()
81     */
82    public void test_readBoolean() throws IOException {
83        os.writeBoolean(true);
84        os.close();
85        openDataInputStream();
86        assertTrue("Incorrect boolean written", dis.readBoolean());
87    }
88
89    /**
90     * java.io.DataInputStream#readByte()
91     */
92    public void test_readByte() throws IOException {
93        os.writeByte((byte) 127);
94        os.close();
95        openDataInputStream();
96        assertTrue("Incorrect byte read", dis.readByte() == (byte) 127);
97    }
98
99    /**
100     * java.io.DataInputStream#readChar()
101     */
102    public void test_readChar() throws IOException {
103        os.writeChar('t');
104        os.close();
105        openDataInputStream();
106        assertEquals("Incorrect char read", 't', dis.readChar());
107    }
108
109    /**
110     * java.io.DataInputStream#readDouble()
111     */
112    public void test_readDouble() throws IOException {
113        os.writeDouble(2345.76834720202);
114        os.close();
115        openDataInputStream();
116        assertEquals("Incorrect double read", 2345.76834720202, dis
117                .readDouble());
118    }
119
120    /**
121     * java.io.DataInputStream#readFloat()
122     */
123    public void test_readFloat() throws IOException {
124        os.writeFloat(29.08764f);
125        os.close();
126        openDataInputStream();
127        assertTrue("Incorrect float read", dis.readFloat() == 29.08764f);
128    }
129
130    /**
131     * java.io.DataInputStream#readFully(byte[])
132     */
133    public void test_readFully$B() throws IOException {
134        os.write(fileString.getBytes());
135        os.close();
136        openDataInputStream();
137        byte rbytes[] = new byte[fileString.length()];
138        dis.readFully(rbytes);
139        assertTrue("Incorrect data read", new String(rbytes, 0, fileString
140                .length()).equals(fileString));
141    }
142
143    /**
144     * java.io.DataInputStream#readFully(byte[], int, int)
145     */
146    public void test_readFully$BII() throws IOException {
147        os.write(fileString.getBytes());
148        os.close();
149        openDataInputStream();
150        byte rbytes[] = new byte[fileString.length()];
151        dis.readFully(rbytes, 0, fileString.length());
152        assertTrue("Incorrect data read", new String(rbytes, 0, fileString
153                .length()).equals(fileString));
154    }
155
156    /**
157     * java.io.DataInputStream#readFully(byte[], int, int)
158     */
159    public void test_readFully$BII_Exception() throws IOException {
160        DataInputStream is = new DataInputStream(new ByteArrayInputStream(
161                new byte[fileString.length()]));
162
163        byte[] byteArray = new byte[fileString.length()];
164
165        try {
166            is.readFully(byteArray, -1, -1);
167            fail("should throw IndexOutOfBoundsException");
168        } catch (IndexOutOfBoundsException e) {
169            // expected
170        }
171
172        try {
173            is.readFully(byteArray, 0, -1);
174            fail("should throw IndexOutOfBoundsException");
175        } catch (IndexOutOfBoundsException e) {
176            // expected
177        }
178
179        try {
180            is.readFully(byteArray, 1, -1);
181            fail("should throw IndexOutOfBoundsException");
182        } catch (IndexOutOfBoundsException e) {
183            // expected
184        }
185
186        is.readFully(byteArray, -1, 0);
187        is.readFully(byteArray, 0, 0);
188        is.readFully(byteArray, 1, 0);
189
190        try {
191            is.readFully(byteArray, -1, 1);
192            fail("should throw IndexOutOfBoundsException");
193        } catch (IndexOutOfBoundsException e) {
194            // expected
195        }
196
197        is.readFully(byteArray, 0, 1);
198        is.readFully(byteArray, 1, 1);
199        try {
200            is.readFully(byteArray, 0, Integer.MAX_VALUE);
201            fail("should throw IndexOutOfBoundsException");
202        } catch (IndexOutOfBoundsException e) {
203            // expected
204        }
205    }
206
207    /**
208     * java.io.DataInputStream#readFully(byte[], int, int)
209     */
210    public void test_readFully$BII_NullArray() throws IOException {
211        DataInputStream is = new DataInputStream(new ByteArrayInputStream(
212                new byte[fileString.length()]));
213
214        byte[] nullByteArray = null;
215
216        try {
217            is.readFully(nullByteArray, -1, -1);
218            fail();
219        } catch (NullPointerException expected) {
220        } catch (IndexOutOfBoundsException expected) {
221        }
222
223        try {
224            is.readFully(nullByteArray, 0, -1);
225            fail();
226        } catch (NullPointerException expected) {
227        } catch (IndexOutOfBoundsException expected) {
228        }
229
230        try {
231            is.readFully(nullByteArray, 1, -1);
232            fail();
233        } catch (NullPointerException expected) {
234        } catch (IndexOutOfBoundsException expected) {
235        }
236
237        is.readFully(nullByteArray, -1, 0);
238        is.readFully(nullByteArray, 0, 0);
239        is.readFully(nullByteArray, 1, 0);
240
241        try {
242            is.readFully(nullByteArray, -1, 1);
243            fail();
244        } catch (NullPointerException expected) {
245        } catch (IndexOutOfBoundsException expected) {
246        }
247
248        try {
249            is.readFully(nullByteArray, 0, 1);
250            fail("should throw NullPointerException");
251        } catch (NullPointerException e) {
252            // expected
253        }
254
255        try {
256            is.readFully(nullByteArray, 1, 1);
257            fail("should throw NullPointerException");
258        } catch (NullPointerException e) {
259            // expected
260        }
261
262        try {
263            is.readFully(nullByteArray, 0, Integer.MAX_VALUE);
264            fail("should throw NullPointerException");
265        } catch (NullPointerException e) {
266            // expected
267        }
268    }
269
270    /**
271     * java.io.DataInputStream#readFully(byte[], int, int)
272     */
273    public void test_readFully$BII_NullStream() throws IOException {
274        DataInputStream is = new DataInputStream(null);
275        byte[] byteArray = new byte[fileString.length()];
276
277        try {
278            is.readFully(byteArray, -1, -1);
279            fail();
280        } catch (NullPointerException expected) {
281        } catch (IndexOutOfBoundsException expected) {
282        }
283
284        try {
285            is.readFully(byteArray, 0, -1);
286            fail();
287        } catch (NullPointerException expected) {
288        } catch (IndexOutOfBoundsException expected) {
289        }
290
291        try {
292            is.readFully(byteArray, 1, -1);
293            fail();
294        } catch (NullPointerException expected) {
295        } catch (IndexOutOfBoundsException expected) {
296        }
297
298        is.readFully(byteArray, -1, 0);
299        is.readFully(byteArray, 0, 0);
300        is.readFully(byteArray, 1, 0);
301
302        try {
303            is.readFully(byteArray, -1, 1);
304            fail("should throw NullPointerException");
305        } catch (NullPointerException e) {
306            // expected
307        }
308
309        try {
310            is.readFully(byteArray, 0, 1);
311            fail("should throw NullPointerException");
312        } catch (NullPointerException e) {
313            // expected
314        }
315
316        try {
317            is.readFully(byteArray, 1, 1);
318            fail("should throw NullPointerException");
319        } catch (NullPointerException e) {
320            // expected
321        }
322
323        try {
324            is.readFully(byteArray, 0, Integer.MAX_VALUE);
325            fail("should throw NullPointerException");
326        } catch (NullPointerException e) {
327            // expected
328        }
329    }
330
331    /**
332     * java.io.DataInputStream#readFully(byte[], int, int)
333     */
334    public void test_readFully$BII_NullStream_NullArray() throws IOException {
335        DataInputStream is = new DataInputStream(null);
336        byte[] nullByteArray = null;
337
338        try {
339            is.readFully(nullByteArray, -1, -1);
340            fail();
341        } catch (NullPointerException expected) {
342        } catch (IndexOutOfBoundsException expected) {
343        }
344
345        try {
346            is.readFully(nullByteArray, 0, -1);
347            fail();
348        } catch (NullPointerException expected) {
349        } catch (IndexOutOfBoundsException expected) {
350        }
351
352        try {
353            is.readFully(nullByteArray, 1, -1);
354            fail();
355        } catch (NullPointerException expected) {
356        } catch (IndexOutOfBoundsException expected) {
357        }
358
359        is.readFully(nullByteArray, -1, 0);
360        is.readFully(nullByteArray, 0, 0);
361        is.readFully(nullByteArray, 1, 0);
362
363        try {
364            is.readFully(nullByteArray, -1, 1);
365            fail("should throw NullPointerException");
366        } catch (NullPointerException e) {
367            // expected
368        }
369
370        try {
371            is.readFully(nullByteArray, 0, 1);
372            fail("should throw NullPointerException");
373        } catch (NullPointerException e) {
374            // expected
375        }
376
377        try {
378            is.readFully(nullByteArray, 1, 1);
379            fail("should throw NullPointerException");
380        } catch (NullPointerException e) {
381            // expected
382        }
383
384        try {
385            is.readFully(nullByteArray, 0, Integer.MAX_VALUE);
386            fail("should throw NullPointerException");
387        } catch (NullPointerException e) {
388            // expected
389        }
390    }
391
392    /**
393     * java.io.DataInputStream#readInt()
394     */
395    public void test_readInt() throws IOException {
396        os.writeInt(768347202);
397        os.close();
398        openDataInputStream();
399        assertEquals("Incorrect int read", 768347202, dis.readInt());
400    }
401
402    /**
403     * java.io.DataInputStream#readLine()
404     */
405    @SuppressWarnings("deprecation")
406    public void test_readLine() throws IOException {
407        os.writeBytes("Hello");
408        os.close();
409        openDataInputStream();
410        String line = dis.readLine();
411        assertTrue("Incorrect line read: " + line, line.equals("Hello"));
412    }
413
414    /**
415     * java.io.DataInputStream#readLong()
416     */
417    public void test_readLong() throws IOException {
418        os.writeLong(9875645283333L);
419        os.close();
420        openDataInputStream();
421        assertEquals("Incorrect long read", 9875645283333L, dis.readLong());
422    }
423
424    /**
425     * java.io.DataInputStream#readShort()
426     */
427    public void test_readShort() throws IOException {
428        os.writeShort(9875);
429        os.close();
430        openDataInputStream();
431        assertTrue("Incorrect short read", dis.readShort() == (short) 9875);
432    }
433
434    /**
435     * java.io.DataInputStream#readUnsignedByte()
436     */
437    public void test_readUnsignedByte() throws IOException {
438        os.writeByte((byte) -127);
439        os.close();
440        openDataInputStream();
441        assertEquals("Incorrect byte read", 129, dis.readUnsignedByte());
442    }
443
444    /**
445     * java.io.DataInputStream#readUnsignedShort()
446     */
447    public void test_readUnsignedShort() throws IOException {
448        os.writeShort(9875);
449        os.close();
450        openDataInputStream();
451        assertEquals("Incorrect short read", 9875, dis.readUnsignedShort());
452    }
453
454    /**
455     * java.io.DataInputStream#readUTF()
456     */
457    public void test_readUTF() throws IOException {
458        os.writeUTF(unihw);
459        os.close();
460        openDataInputStream();
461        assertTrue("Failed to write string in UTF format",
462                dis.available() == unihw.length() + 2);
463        assertTrue("Incorrect string read", dis.readUTF().equals(unihw));
464    }
465
466    static class TestDataInputStream implements DataInput {
467        public boolean readBoolean() throws IOException {
468            return false;
469        }
470
471        public byte readByte() throws IOException {
472            return (byte) 0;
473        }
474
475        public char readChar() throws IOException {
476            return (char) 0;
477        }
478
479        public double readDouble() throws IOException {
480            return 0.0;
481        }
482
483        public float readFloat() throws IOException {
484            return (float) 0.0;
485        }
486
487        public void readFully(byte[] buffer) throws IOException {
488        }
489
490        public void readFully(byte[] buffer, int offset, int count)
491                throws IOException {
492        }
493
494        public int readInt() throws IOException {
495            return 0;
496        }
497
498        public String readLine() throws IOException {
499            return null;
500        }
501
502        public long readLong() throws IOException {
503            return (long) 0;
504        }
505
506        public short readShort() throws IOException {
507            return (short) 0;
508        }
509
510        public int readUnsignedByte() throws IOException {
511            return 0;
512        }
513
514        public int readUnsignedShort() throws IOException {
515            return 0;
516        }
517
518        public String readUTF() throws IOException {
519            return DataInputStream.readUTF(this);
520        }
521
522        public int skipBytes(int count) throws IOException {
523            return 0;
524        }
525    }
526
527    /**
528     * java.io.DataInputStream#readUTF(java.io.DataInput)
529     */
530    public void test_readUTFLjava_io_DataInput() throws IOException {
531        os.writeUTF(unihw);
532        os.close();
533        openDataInputStream();
534        assertTrue("Failed to write string in UTF format",
535                dis.available() == unihw.length() + 2);
536        assertTrue("Incorrect string read", DataInputStream.readUTF(dis)
537                .equals(unihw));
538
539        // Regression test for HARMONY-5336
540        new TestDataInputStream().readUTF();
541    }
542
543    /**
544     * java.io.DataInputStream#skipBytes(int)
545     */
546    public void test_skipBytesI() throws IOException {
547        byte fileBytes[] = fileString.getBytes();
548        os.write(fileBytes);
549        os.close();
550        openDataInputStream();
551        dis.skipBytes(100);
552        byte rbytes[] = new byte[fileString.length()];
553        dis.read(rbytes, 0, 50);
554        dis.close();
555        assertTrue("Incorrect data read", new String(rbytes, 0, 50)
556                .equals(fileString.substring(100, 150)));
557
558        int skipped = 0;
559        openDataInputStream();
560        try {
561            skipped = dis.skipBytes(50000);
562        } catch (EOFException e) {
563        }
564        assertTrue("Skipped should report " + fileString.length() + " not "
565                + skipped, skipped == fileString.length());
566    }
567
568    // b/30268192 : Some apps rely on the exact calls that
569    // DataInputStream makes on the wrapped InputStream. This
570    // test is to prevent *unintentional* regressions but may
571    // change in future releases.
572    public void test_readShortUsesMultiByteRead() throws IOException {
573        ThrowExceptionOnSingleByteReadInputStream
574                is = new ThrowExceptionOnSingleByteReadInputStream();
575        DataInputStream dis = new DataInputStream(is);
576        dis.readShort();
577        is.assertMultiByteReadWasCalled();
578    }
579
580    // b/30268192 : Some apps rely on the exact calls that
581    // DataInputStream makes on the wrapped InputStream. This
582    // test is to prevent *unintentional* regressions but may
583    // change in future releases.
584    public void test_readCharUsesMultiByteRead() throws IOException {
585        ThrowExceptionOnSingleByteReadInputStream
586                is = new ThrowExceptionOnSingleByteReadInputStream();
587        DataInputStream dis = new DataInputStream(is);
588        dis.readChar();
589        is.assertMultiByteReadWasCalled();
590    }
591
592    // b/30268192 : Some apps rely on the exact calls that
593    // DataInputStream makes on the wrapped InputStream. This
594    // test is to prevent *unintentional* regressions but may
595    // change in future releases.
596    public void test_readIntUsesMultiByteRead() throws IOException {
597        ThrowExceptionOnSingleByteReadInputStream
598                is = new ThrowExceptionOnSingleByteReadInputStream();
599        DataInputStream dis = new DataInputStream(is);
600        dis.readInt();
601        is.assertMultiByteReadWasCalled();
602    }
603
604    // b/30268192 : Some apps rely on the exact calls that
605    // DataInputStream makes on the wrapped InputStream. This
606    // test is to prevent *unintentional* regressions but may
607    // change in future releases.
608    public void test_readUnsignedShortUsesMultiByteRead() throws IOException {
609        ThrowExceptionOnSingleByteReadInputStream
610                is = new ThrowExceptionOnSingleByteReadInputStream();
611        DataInputStream dis = new DataInputStream(is);
612        dis.readUnsignedShort();
613        is.assertMultiByteReadWasCalled();
614    }
615
616    private void openDataInputStream() throws IOException {
617        dis = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
618    }
619
620    /**
621     * Sets up the fixture, for example, open a network connection. This method
622     * is called before a test is executed.
623     */
624    protected void setUp() {
625        bos = new ByteArrayOutputStream();
626        os = new DataOutputStream(bos);
627    }
628
629    /**
630     * Tears down the fixture, for example, close a network connection. This
631     * method is called after a test is executed.
632     */
633    protected void tearDown() {
634        try {
635            os.close();
636        } catch (Exception e) {
637        }
638        try {
639            dis.close();
640        } catch (Exception e) {
641        }
642    }
643
644    public static class ThrowExceptionOnSingleByteReadInputStream extends InputStream {
645
646        private boolean multiByteReadWasCalled = false;
647
648        @Override
649        public int read() throws IOException {
650            fail("Should not call single byte read");
651            return 0;
652        }
653
654        @Override
655        public int read(byte[] b, int i, int j) throws IOException {
656            multiByteReadWasCalled = true;
657            return j;
658        }
659
660        public void assertMultiByteReadWasCalled() {
661            if (!multiByteReadWasCalled) {
662                fail("read(byte[], int, int) was not called");
663            }
664        }
665    }
666}
667