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 libcore.java.util.jar;
18
19import java.io.File;
20import java.io.IOException;
21import java.io.InputStream;
22import java.util.jar.JarEntry;
23import java.util.jar.JarFile;
24import java.util.zip.ZipException;
25import java.util.zip.ZipFile;
26import junit.framework.TestCase;
27import tests.support.resource.Support_Resources;
28
29public class OldJarFileTest extends TestCase {
30
31    private final String jarName = "hyts_patch.jar"; // a 'normal' jar file
32    private final String entryName = "foo/bar/A.class";
33    private File resources;
34
35    @Override public void setUp() throws Exception {
36        super.setUp();
37        resources = Support_Resources.createTempFolder();
38    }
39
40    public void test_ConstructorLjava_io_File() throws IOException {
41        try {
42            new JarFile(new File("Wrong.file"));
43            fail("Should throw IOException");
44        } catch (IOException expected) {
45        }
46
47        Support_Resources.copyFile(resources, null, jarName);
48        new JarFile(new File(resources, jarName));
49    }
50
51    public void test_ConstructorLjava_lang_String() throws IOException {
52        try {
53            new JarFile("Wrong.file");
54            fail("Should throw IOException");
55        } catch (IOException expected) {
56        }
57
58        Support_Resources.copyFile(resources, null, jarName);
59        String fileName = (new File(resources, jarName)).getCanonicalPath();
60        new JarFile(fileName);
61    }
62
63    public void test_ConstructorLjava_lang_StringZ() throws IOException {
64        try {
65            new JarFile("Wrong.file", false);
66            fail("Should throw IOException");
67        } catch (IOException expected) {
68        }
69
70        Support_Resources.copyFile(resources, null, jarName);
71        String fileName = (new File(resources, jarName)).getCanonicalPath();
72        new JarFile(fileName, true);
73    }
74
75    public void test_ConstructorLjava_io_FileZ() throws IOException {
76        try {
77            new JarFile(new File("Wrong.file"), true);
78            fail("Should throw IOException");
79        } catch (IOException expected) {
80        }
81
82        Support_Resources.copyFile(resources, null, jarName);
83        new JarFile(new File(resources, jarName), false);
84    }
85
86    public void test_ConstructorLjava_io_FileZI() throws IOException {
87        try {
88            new JarFile(new File("Wrong.file"), true,
89                    ZipFile.OPEN_READ);
90            fail("Should throw IOException");
91        } catch (IOException expected) {
92        }
93
94        Support_Resources.copyFile(resources, null, jarName);
95        new JarFile(new File(resources, jarName), false,
96                ZipFile.OPEN_READ);
97
98        try {
99            Support_Resources.copyFile(resources, null, jarName);
100            new JarFile(new File(resources, jarName), false,
101                    ZipFile.OPEN_READ | ZipFile.OPEN_DELETE + 33);
102            fail("Should throw IllegalArgumentException");
103        } catch (IllegalArgumentException expected) {
104        }
105    }
106
107    public void test_close() throws IOException {
108        String modifiedJarName = "Modified_SF_EntryAttributes.jar";
109        Support_Resources.copyFile(resources, null, modifiedJarName);
110        JarFile jarFile = new JarFile(new File(resources, modifiedJarName), true);
111        jarFile.entries();
112
113        jarFile.close();
114        jarFile.close();
115
116        // Can not check IOException
117    }
118
119    public void test_getInputStreamLjava_util_jar_JarEntry() throws IOException {
120        Support_Resources.copyFile(resources, null, jarName);
121        File localFile = new File(resources, jarName);
122
123        byte[] b = new byte[1024];
124        JarFile jf = new JarFile(localFile);
125        InputStream is = jf.getInputStream(jf.getEntry(entryName));
126        assertTrue("Returned invalid stream", is.available() > 0);
127        int r = is.read(b, 0, 1024);
128        is.close();
129        StringBuilder stringBuffer = new StringBuilder(r);
130        for (int i = 0; i < r; i++) {
131            stringBuffer.append((char) (b[i] & 0xff));
132        }
133        String contents = stringBuffer.toString();
134        assertTrue("Incorrect stream read", contents.indexOf("bar") > 0);
135        jf.close();
136
137        jf = new JarFile(localFile);
138        InputStream in = jf.getInputStream(new JarEntry("invalid"));
139        assertNull("Got stream for non-existent entry", in);
140
141        try {
142            Support_Resources.copyFile(resources, null, jarName);
143            File signedFile = new File(resources, jarName);
144            jf = new JarFile(signedFile);
145            JarEntry jre = new JarEntry("foo/bar/A.class");
146            jf.getInputStream(jre);
147            // InputStream returned in any way, exception can be thrown in case
148            // of reading from this stream only.
149            // fail("Should throw ZipException");
150        } catch (ZipException expected) {
151        }
152
153        try {
154            Support_Resources.copyFile(resources, null, jarName);
155            File signedFile = new File(resources, jarName);
156            jf = new JarFile(signedFile);
157            JarEntry jre = new JarEntry("foo/bar/A.class");
158            jf.close();
159            jf.getInputStream(jre);
160            // InputStream returned in any way, exception can be thrown in case
161            // of reading from this stream only.
162            // The same for IOException
163            fail("Should throw IllegalStateException");
164        } catch (IllegalStateException expected) {
165        }
166    }
167}
168