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