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 libcore.java.util.jar;
19
20import java.io.BufferedInputStream;
21import java.io.ByteArrayInputStream;
22import java.io.File;
23import java.io.IOException;
24import java.io.InputStream;
25import java.util.jar.JarInputStream;
26import java.util.zip.ZipEntry;
27import java.util.zip.ZipException;
28import tests.support.resource.Support_Resources;
29
30public class OldJarInputStreamTest extends junit.framework.TestCase {
31
32    public void test_ConstructorLjava_io_InputStreamZ() {
33        try {
34            // we need a buffered stream because ByteArrayInputStream.close() is a no-op
35            InputStream is = new BufferedInputStream(new ByteArrayInputStream(new byte[0]));
36            is.close();
37            new JarInputStream(is, false);
38            fail("IOException expected");
39        } catch (IOException ee) {
40            // expected
41        }
42    }
43
44
45    class Mock_JarInputStream extends JarInputStream {
46
47        public Mock_JarInputStream(InputStream in) throws IOException {
48            super(in);
49        }
50
51        public ZipEntry createZipEntry(String str) {
52            return super.createZipEntry(str);
53        }
54    }
55
56    public void test_createZipEntryLjava_lang_String() throws Exception {
57        File resources = Support_Resources.createTempFolder();
58        Support_Resources.copyFile(resources, null, "Broken_entry.jar");
59        InputStream is = Support_Resources.getStream("Broken_entry.jar");
60        Mock_JarInputStream mjis = new Mock_JarInputStream(is);
61        assertNotNull(mjis.createZipEntry("New entry"));
62    }
63
64    public void test_read$ZII() throws Exception {
65        File resources = Support_Resources.createTempFolder();
66        Support_Resources.copyFile(resources, null, "Broken_entry_data.jar");
67        InputStream is = Support_Resources.getStream("Broken_entry_data.jar");
68        JarInputStream jis = new JarInputStream(is, true);
69        byte b[] = new byte[100];
70
71        jis.getNextEntry();
72        jis.read(b, 0, 100);
73        jis.getNextEntry();
74        jis.getNextEntry();
75        jis.getNextEntry();
76
77        try {
78            jis.read(b, 0, 100);
79            fail("ZipException expected");
80        } catch (ZipException ee) {
81            // expected
82        }
83
84        try {
85            jis.close();  // Android throws exception here, already!
86            jis.read(b, 0, 100);  // But RI here, only!
87            fail("IOException expected");
88        } catch (IOException ee) {
89            // expected
90        }
91    }
92}
93