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 */
17
18package org.apache.harmony.archive.tests.java.util.zip;
19
20import dalvik.annotation.TestLevel;
21import dalvik.annotation.TestTargetClass;
22import dalvik.annotation.TestTargetNew;
23import dalvik.annotation.TestTargets;
24
25import junit.framework.TestCase;
26
27import tests.support.resource.Support_Resources;
28
29import java.io.ByteArrayInputStream;
30import java.io.ByteArrayOutputStream;
31import java.io.File;
32import java.io.FileInputStream;
33import java.io.IOException;
34import java.io.InputStream;
35import java.io.FileOutputStream;
36import java.io.EOFException;
37import java.util.zip.DeflaterOutputStream;
38import java.util.zip.Inflater;
39import java.util.zip.InflaterInputStream;
40
41@TestTargetClass(InflaterInputStream.class)
42public class InflaterInputStreamTest extends TestCase {
43
44    // files hyts_constru(O),hyts_constru(OD),hyts_constru(ODI) needs to be
45    // included as resources
46    byte outPutBuf[] = new byte[500];
47
48    class MyInflaterInputStream extends InflaterInputStream {
49        MyInflaterInputStream(InputStream in) {
50            super(in);
51        }
52
53        MyInflaterInputStream(InputStream in, Inflater infl) {
54            super(in, infl);
55        }
56
57        MyInflaterInputStream(InputStream in, Inflater infl, int size) {
58            super(in, infl, size);
59        }
60
61        void myFill() throws IOException {
62            fill();
63        }
64    }
65
66    /**
67     * @tests java.util.zip.InflaterInputStream#InflaterInputStream(java.io.InputStream)
68     */
69    @TestTargetNew(
70        level = TestLevel.COMPLETE,
71        notes = "",
72        method = "InflaterInputStream",
73        args = {java.io.InputStream.class}
74    )
75    public void test_ConstructorLjava_io_InputStream() throws IOException {
76        byte byteArray[] = new byte[100];
77        InputStream infile = Support_Resources.getStream("hyts_constru_OD.txt");
78        InflaterInputStream inflatIP = new InflaterInputStream(infile);
79
80        inflatIP.read(byteArray, 0, 5);// only suppose to read in 5 bytes
81        inflatIP.close();
82    }
83
84    /**
85     * @tests java.util.zip.InflaterInputStream#InflaterInputStream(java.io.InputStream,
86     *        java.util.zip.Inflater)
87     */
88    @TestTargetNew(
89        level = TestLevel.COMPLETE,
90        notes = "",
91        method = "InflaterInputStream",
92        args = {java.io.InputStream.class, java.util.zip.Inflater.class}
93    )
94    public void test_ConstructorLjava_io_InputStreamLjava_util_zip_Inflater()
95            throws IOException {
96        byte byteArray[] = new byte[100];
97        InputStream infile = Support_Resources.getStream("hyts_constru_OD.txt");
98        Inflater inflate = new Inflater();
99        InflaterInputStream inflatIP = new InflaterInputStream(infile, inflate);
100
101        inflatIP.read(byteArray, 0, 5);// only suppose to read in 5 bytes
102        inflatIP.close();
103    }
104
105    /**
106     * @tests java.util.zip.InflaterInputStream#InflaterInputStream(java.io.InputStream,
107     *        java.util.zip.Inflater, int)
108     */
109    @TestTargetNew(
110        level = TestLevel.COMPLETE,
111        notes = "IllegalArgumentException checking missed.",
112        method = "InflaterInputStream",
113        args = {java.io.InputStream.class, java.util.zip.Inflater.class, int.class}
114    )
115    public void test_ConstructorLjava_io_InputStreamLjava_util_zip_InflaterI()
116            throws IOException {
117        int result = 0;
118        int buffer[] = new int[500];
119        InputStream infile = Support_Resources
120                .getStream("hyts_constru_ODI.txt");
121        Inflater inflate = new Inflater();
122        InflaterInputStream inflatIP = new InflaterInputStream(infile, inflate,
123                1);
124
125        int i = 0;
126        while ((result = inflatIP.read()) != -1) {
127            buffer[i] = result;
128            i++;
129        }
130        inflatIP.close();
131
132        try {
133            inflatIP = new InflaterInputStream(infile, inflate, -1);
134            fail("IllegalArgumentException expected.");
135        } catch (IllegalArgumentException ee) {
136            // expected
137        }
138
139    }
140
141    /**
142     * @tests java.util.zip.InflaterInputStream#mark(int)
143     */
144    @TestTargetNew(
145        level = TestLevel.COMPLETE,
146        notes = "",
147        method = "mark",
148        args = {int.class}
149    )
150    public void test_markI() {
151        InputStream is = new ByteArrayInputStream(new byte[10]);
152        InflaterInputStream iis = new InflaterInputStream(is);
153        // mark do nothing, do no check
154        iis.mark(0);
155        iis.mark(-1);
156        iis.mark(10000000);
157    }
158
159    /**
160     * @tests java.util.zip.InflaterInputStream#markSupported()
161     */
162    @TestTargetNew(
163        level = TestLevel.COMPLETE,
164        notes = "",
165        method = "markSupported",
166        args = {}
167    )
168    public void test_markSupported() {
169        InputStream is = new ByteArrayInputStream(new byte[10]);
170        InflaterInputStream iis = new InflaterInputStream(is);
171        assertFalse(iis.markSupported());
172        assertTrue(is.markSupported());
173    }
174
175    /**
176     * @tests java.util.zip.InflaterInputStream#read()
177     */
178    @TestTargetNew(
179        level = TestLevel.COMPLETE,
180        notes = "",
181        method = "read",
182        args = {}
183    )
184    public void test_read() throws IOException {
185        int result = 0;
186        int buffer[] = new int[500];
187        byte orgBuffer[] = {1, 3, 4, 7, 8};
188        InputStream infile = Support_Resources.getStream("hyts_constru_OD.txt");
189        Inflater inflate = new Inflater();
190        InflaterInputStream inflatIP = new InflaterInputStream(infile, inflate);
191
192        int i = 0;
193        while ((result = inflatIP.read()) != -1) {
194            buffer[i] = result;
195            i++;
196        }
197        inflatIP.close();
198
199        for (int j = 0; j < orgBuffer.length; j++) {
200            assertTrue(
201                    "original compressed data did not equal decompressed data",
202                    buffer[j] == orgBuffer[j]);
203        }
204        inflatIP.close();
205        try {
206            inflatIP.read();
207            fail("IOException expected");
208        } catch (IOException ee) {
209            // expected.
210        }
211    }
212
213    public void testAvailableNonEmptySource() throws Exception {
214        // this byte[] is a deflation of these bytes: { 1, 3, 4, 6 }
215        byte[] deflated = { 72, -119, 99, 100, 102, 97, 3, 0, 0, 31, 0, 15, 0 };
216        InputStream in = new InflaterInputStream(new ByteArrayInputStream(deflated));
217        // InflaterInputStream.available() returns either 1 or 0, even though
218        // that contradicts the behavior defined in InputStream.available()
219        assertEquals(1, in.read());
220        assertEquals(1, in.available());
221        assertEquals(3, in.read());
222        assertEquals(1, in.available());
223        assertEquals(4, in.read());
224        assertEquals(1, in.available());
225        assertEquals(6, in.read());
226        assertEquals(0, in.available());
227        assertEquals(-1, in.read());
228        assertEquals(-1, in.read());
229    }
230
231    public void testAvailableSkip() throws Exception {
232        // this byte[] is a deflation of these bytes: { 1, 3, 4, 6 }
233        byte[] deflated = { 72, -119, 99, 100, 102, 97, 3, 0, 0, 31, 0, 15, 0 };
234        InputStream in = new InflaterInputStream(new ByteArrayInputStream(deflated));
235        assertEquals(1, in.available());
236        assertEquals(4, in.skip(4));
237        assertEquals(0, in.available());
238    }
239
240    public void testAvailableEmptySource() throws Exception {
241        // this byte[] is a deflation of the empty file
242        byte[] deflated = { 120, -100, 3, 0, 0, 0, 0, 1 };
243        InputStream in = new InflaterInputStream(new ByteArrayInputStream(deflated));
244        assertEquals(-1, in.read());
245        assertEquals(-1, in.read());
246        assertEquals(0, in.available());
247    }
248
249    /**
250     * @tests java.util.zip.InflaterInputStream#read(byte[], int, int)
251     */
252    @TestTargets({
253        @TestTargetNew(
254            level = TestLevel.PARTIAL_COMPLETE,
255            notes = "IOException & ZipException checking missed. Additional tests for fill method is not needed.",
256            method = "read",
257            args = {byte[].class, int.class, int.class}
258        ),
259        @TestTargetNew(
260            level = TestLevel.PARTIAL_COMPLETE,
261            notes = "IOException & ZipException checking missed. Additional tests for fill method is not needed.",
262            method = "fill",
263            args = {}
264        )
265    })
266    public void test_read$BII() throws IOException {
267        byte[] test = new byte[507];
268        for (int i = 0; i < 256; i++) {
269            test[i] = (byte) i;
270        }
271        for (int i = 256; i < test.length; i++) {
272            test[i] = (byte) (256 - i);
273        }
274        ByteArrayOutputStream baos = new ByteArrayOutputStream();
275        DeflaterOutputStream dos = new DeflaterOutputStream(baos);
276        dos.write(test);
277        dos.close();
278        InputStream is = new ByteArrayInputStream(baos.toByteArray());
279        InflaterInputStream iis = new InflaterInputStream(is);
280        byte[] outBuf = new byte[530];
281        int result = 0;
282        while (true) {
283            result = iis.read(outBuf, 0, 5);
284            if (result == -1) {
285                // "EOF was reached";
286                break;
287            }
288        }
289        try {
290            iis.read(outBuf, -1, 10);
291            fail("should throw IOOBE.");
292        } catch (IndexOutOfBoundsException e) {
293            // expected;
294        }
295        iis.close();
296    }
297
298    /**
299     * @tests java.util.zip.InflaterInputStream#read(byte[], int, int)
300     */
301    @TestTargets({
302        @TestTargetNew(
303            level = TestLevel.PARTIAL_COMPLETE,
304            notes = "IOException checking.",
305            method = "read",
306            args = {byte[].class, int.class, int.class}
307        ),
308        @TestTargetNew(
309            level = TestLevel.PARTIAL_COMPLETE,
310            notes = "IOException checking.",
311            method = "fill",
312            args = {}
313        )
314    })
315    public void test_read$BII2() throws IOException {
316        File resources = Support_Resources.createTempFolder();
317        Support_Resources.copyFile(resources, null, "Broken_manifest.jar");
318        FileInputStream fis = new FileInputStream(new File(resources,
319                "Broken_manifest.jar"));
320        InflaterInputStream iis = new InflaterInputStream(fis);
321        byte[] outBuf = new byte[530];
322
323        iis.close();
324        try {
325            iis.read(outBuf, 0, 5);
326            fail("IOException expected");
327        } catch (IOException ee) {
328            // expected.
329        }
330    }
331
332    @TestTargets({
333        @TestTargetNew(
334            level = TestLevel.PARTIAL_COMPLETE,
335            notes = "IOException checking.",
336            method = "read",
337            args = {byte[].class, int.class, int.class}
338        ),
339        @TestTargetNew(
340            level = TestLevel.PARTIAL_COMPLETE,
341            notes = "IOException checking.",
342            method = "fill",
343            args = {}
344        )
345    })
346    public void test_read$BII3() throws IOException {
347        File resources = Support_Resources.createTempFolder();
348        Support_Resources.copyFile(resources, null, "Broken_manifest.jar");
349        FileInputStream fis = new FileInputStream(new File(resources,
350                "Broken_manifest.jar"));
351        InflaterInputStream iis = new InflaterInputStream(fis);
352        byte[] outBuf = new byte[530];
353
354        try {
355            iis.read();
356            fail("IOException expected.");
357        } catch (IOException ee) {
358            // expected
359        }
360    }
361
362    /**
363     * @tests java.util.zip.InflaterInputStream#reset()
364     */
365    @TestTargetNew(
366        level = TestLevel.COMPLETE,
367        notes = "",
368        method = "reset",
369        args = {}
370    )
371    public void test_reset() {
372        InputStream is = new ByteArrayInputStream(new byte[10]);
373        InflaterInputStream iis = new InflaterInputStream(is);
374        try {
375            iis.reset();
376            fail("Should throw IOException");
377        } catch (IOException e) {
378            // correct
379        }
380    }
381
382    /**
383     * @tests java.util.zip.InflaterInputStream#skip(long)
384     */
385    @TestTargetNew(
386        level = TestLevel.PARTIAL_COMPLETE,
387        notes = "IOException checking missed.",
388        method = "skip",
389        args = {long.class}
390    )
391    public void test_skipJ() throws IOException {
392        InputStream is = Support_Resources.getStream("hyts_available.tst");
393        InflaterInputStream iis = new InflaterInputStream(is);
394
395        // Tests for skipping a negative number of bytes.
396        try {
397            iis.skip(-3);
398            fail("IllegalArgumentException not thrown");
399        } catch (IllegalArgumentException e) {
400            // Expected
401        }
402        assertEquals("Incorrect Byte Returned.", 5, iis.read());
403
404        try {
405            iis.skip(Integer.MIN_VALUE);
406            fail("IllegalArgumentException not thrown");
407        } catch (IllegalArgumentException e) {
408            // Expected
409        }
410        assertEquals("Incorrect Byte Returned.", 4, iis.read());
411
412        // Test to make sure the correct number of bytes were skipped
413        assertEquals("Incorrect Number Of Bytes Skipped.", 3, iis.skip(3));
414
415        // Test to see if the number of bytes skipped returned is true.
416        assertEquals("Incorrect Byte Returned.", 7, iis.read());
417
418        assertEquals("Incorrect Number Of Bytes Skipped.", 0, iis.skip(0));
419        assertEquals("Incorrect Byte Returned.", 0, iis.read());
420
421        // Test for skipping more bytes than available in the stream
422        assertEquals("Incorrect Number Of Bytes Skipped.", 2, iis.skip(4));
423        assertEquals("Incorrect Byte Returned.", -1, iis.read());
424        iis.close();
425    }
426
427    /**
428     * @tests java.util.zip.InflaterInputStream#skip(long)
429     */
430    @TestTargetNew(
431        level = TestLevel.COMPLETE,
432        notes = "IOException checking missed.",
433        method = "skip",
434        args = {long.class}
435    )
436    public void test_skipJ2() throws IOException {
437        int result = 0;
438        int buffer[] = new int[100];
439        byte orgBuffer[] = {1, 3, 4, 7, 8};
440
441        // testing for negative input to skip
442        InputStream infile = Support_Resources.getStream("hyts_constru_OD.txt");
443        Inflater inflate = new Inflater();
444        InflaterInputStream inflatIP = new InflaterInputStream(infile, inflate,
445                10);
446        long skip;
447        try {
448            skip = inflatIP.skip(Integer.MIN_VALUE);
449            fail("Expected IllegalArgumentException when skip() is called with negative parameter");
450        } catch (IllegalArgumentException e) {
451            // Expected
452        }
453        inflatIP.close();
454
455        // testing for number of bytes greater than input.
456        InputStream infile2 = Support_Resources
457                .getStream("hyts_constru_OD.txt");
458        InflaterInputStream inflatIP2 = new InflaterInputStream(infile2);
459
460        // looked at how many bytes the skip skipped. It is
461        // 5 and its supposed to be the entire input stream.
462
463        skip = inflatIP2.skip(Integer.MAX_VALUE);
464        // System.out.println(skip);
465        assertEquals("method skip() returned wrong number of bytes skipped", 5,
466                skip);
467
468        // test for skipping of 2 bytes
469        InputStream infile3 = Support_Resources
470                .getStream("hyts_constru_OD.txt");
471        InflaterInputStream inflatIP3 = new InflaterInputStream(infile3);
472        skip = inflatIP3.skip(2);
473        assertEquals(
474                "the number of bytes returned by skip did not correspond with its input parameters",
475                2, skip);
476        int i = 0;
477        result = 0;
478        while ((result = inflatIP3.read()) != -1) {
479            buffer[i] = result;
480            i++;
481        }
482        inflatIP2.close();
483
484        for (int j = 2; j < orgBuffer.length; j++) {
485            assertTrue(
486                    "original compressed data did not equal decompressed data",
487                    buffer[j - 2] == orgBuffer[j]);
488        }
489
490        try {
491            inflatIP2.skip(4);
492            fail("IOException expected.");
493        } catch (IOException ee) {
494            // expected
495        }
496    }
497
498    /**
499     * @tests java.util.zip.InflaterInputStream#available()
500     */
501    @TestTargetNew(
502        level = TestLevel.COMPLETE,
503        notes = "",
504        method = "available",
505        args = {}
506    )
507    public void test_available() throws IOException {
508        InputStream is = Support_Resources.getStream("hyts_available.tst");
509        InflaterInputStream iis = new InflaterInputStream(is);
510
511        int available;
512        for (int i = 0; i < 11; i++) {
513            iis.read();
514            available = iis.available();
515            if (available == 0) {
516                assertEquals("Expected no more bytes to read", -1, iis.read());
517            } else {
518                assertEquals("Bytes Available Should Return 1.", 1, available);
519            }
520        }
521
522        iis.close();
523        try {
524            iis.available();
525            fail("available after close should throw IOException.");
526        } catch (IOException e) {
527            // Expected
528        }
529    }
530
531    /**
532     * @tests java.util.zip.InflaterInputStream#close()
533     */
534    @TestTargetNew(
535        level = TestLevel.COMPLETE,
536        notes = "IOException can not be tested.",
537        method = "close",
538        args = {}
539    )
540    public void test_close() throws IOException {
541        InflaterInputStream iin = new InflaterInputStream(
542                new ByteArrayInputStream(new byte[0]));
543        iin.close();
544
545        // test for exception
546        iin.close();
547
548    }
549}
550