1b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson/*
2b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson * Licensed to the Apache Software Foundation (ASF) under one or more
3b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson * contributor license agreements.  See the NOTICE file distributed with
4b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson * this work for additional information regarding copyright ownership.
5b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson * The ASF licenses this file to You under the Apache License, Version 2.0
6b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson * (the "License"); you may not use this file except in compliance with
7b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson * the License.  You may obtain a copy of the License at
8b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson *
9b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson *     http://www.apache.org/licenses/LICENSE-2.0
10b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson *
11b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson * Unless required by applicable law or agreed to in writing, software
12b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
13b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson * See the License for the specific language governing permissions and
15b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson * limitations under the License.
16b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson */
17b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilsonpackage libcore.java.util.jar;
18b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
19b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilsonimport java.io.File;
20b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilsonimport java.io.IOException;
21b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilsonimport java.io.InputStream;
22b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilsonimport java.util.jar.JarEntry;
23b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilsonimport java.util.jar.JarFile;
24b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilsonimport java.util.zip.ZipException;
25b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilsonimport java.util.zip.ZipFile;
26b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilsonimport junit.framework.TestCase;
27b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilsonimport tests.support.resource.Support_Resources;
28b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
29b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilsonpublic class OldJarFileTest extends TestCase {
30b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
31b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    private final String jarName = "hyts_patch.jar"; // a 'normal' jar file
32b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    private final String entryName = "foo/bar/A.class";
336a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson    private File resources = Support_Resources.createTempFolder();
34b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
35b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    public void test_ConstructorLjava_io_File() throws IOException {
36b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        try {
37b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            new JarFile(new File("Wrong.file"));
38b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            fail("Should throw IOException");
396a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson        } catch (IOException expected) {
40b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        }
41b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
42b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        Support_Resources.copyFile(resources, null, jarName);
43b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        new JarFile(new File(resources, jarName));
44b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    }
45b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
46b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    public void test_ConstructorLjava_lang_String() throws IOException {
47b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        try {
48b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            new JarFile("Wrong.file");
49b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            fail("Should throw IOException");
506a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson        } catch (IOException expected) {
51b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        }
52b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
53b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        Support_Resources.copyFile(resources, null, jarName);
54b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        String fileName = (new File(resources, jarName)).getCanonicalPath();
55b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        new JarFile(fileName);
56b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    }
57b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
58b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    public void test_ConstructorLjava_lang_StringZ() throws IOException {
59b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        try {
60b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            new JarFile("Wrong.file", false);
61b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            fail("Should throw IOException");
626a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson        } catch (IOException expected) {
63b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        }
64b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
65b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        Support_Resources.copyFile(resources, null, jarName);
66b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        String fileName = (new File(resources, jarName)).getCanonicalPath();
67b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        new JarFile(fileName, true);
68b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    }
69b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
70b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    public void test_ConstructorLjava_io_FileZ() throws IOException {
71b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        try {
72b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            new JarFile(new File("Wrong.file"), true);
73b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            fail("Should throw IOException");
746a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson        } catch (IOException expected) {
75b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        }
76b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
77b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        Support_Resources.copyFile(resources, null, jarName);
78b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        new JarFile(new File(resources, jarName), false);
79b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    }
80b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
816a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson    public void test_ConstructorLjava_io_FileZI() throws IOException {
82b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        try {
83b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            new JarFile(new File("Wrong.file"), true,
84b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson                    ZipFile.OPEN_READ);
85b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            fail("Should throw IOException");
866a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson        } catch (IOException expected) {
87b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        }
88b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
896a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson        Support_Resources.copyFile(resources, null, jarName);
906a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson        new JarFile(new File(resources, jarName), false,
916a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson                ZipFile.OPEN_READ);
92b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
93b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        try {
94b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            Support_Resources.copyFile(resources, null, jarName);
95b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            new JarFile(new File(resources, jarName), false,
96b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson                    ZipFile.OPEN_READ | ZipFile.OPEN_DELETE + 33);
97b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            fail("Should throw IllegalArgumentException");
986a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson        } catch (IllegalArgumentException expected) {
99b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        }
100b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    }
101b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
102b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    public void test_close() throws IOException {
103b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        String modifiedJarName = "Modified_SF_EntryAttributes.jar";
104b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        Support_Resources.copyFile(resources, null, modifiedJarName);
105b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        JarFile jarFile = new JarFile(new File(resources, modifiedJarName), true);
106b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        jarFile.entries();
107b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
108b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        jarFile.close();
109b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        jarFile.close();
110b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
111b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        // Can not check IOException
112b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    }
113b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
114b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    public void test_getInputStreamLjava_util_jar_JarEntry() throws IOException {
1156a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson        Support_Resources.copyFile(resources, null, jarName);
1166a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson        File localFile = new File(resources, jarName);
117b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
118b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        byte[] b = new byte[1024];
1196a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson        JarFile jf = new JarFile(localFile);
1206a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson        InputStream is = jf.getInputStream(jf.getEntry(entryName));
1216a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson        assertTrue("Returned invalid stream", is.available() > 0);
1226a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson        int r = is.read(b, 0, 1024);
1236a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson        is.close();
1246a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson        StringBuilder stringBuffer = new StringBuilder(r);
1256a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson        for (int i = 0; i < r; i++) {
1266a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson            stringBuffer.append((char) (b[i] & 0xff));
127b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        }
1286a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson        String contents = stringBuffer.toString();
1296a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson        assertTrue("Incorrect stream read", contents.indexOf("bar") > 0);
1306a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson        jf.close();
131b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
1326a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson        jf = new JarFile(localFile);
1336a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson        InputStream in = jf.getInputStream(new JarEntry("invalid"));
1346a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson        assertNull("Got stream for non-existent entry", in);
135b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
136b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        try {
137b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            Support_Resources.copyFile(resources, null, jarName);
138b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            File signedFile = new File(resources, jarName);
1396a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson            jf = new JarFile(signedFile);
140b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            JarEntry jre = new JarEntry("foo/bar/A.class");
141b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            jf.getInputStream(jre);
142b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            // InputStream returned in any way, exception can be thrown in case
143b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            // of reading from this stream only.
144b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            // fail("Should throw ZipException");
1456a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson        } catch (ZipException expected) {
146b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        }
147b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson
148b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        try {
149b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            Support_Resources.copyFile(resources, null, jarName);
150b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            File signedFile = new File(resources, jarName);
1516a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson            jf = new JarFile(signedFile);
152b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            JarEntry jre = new JarEntry("foo/bar/A.class");
153b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            jf.close();
154b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            jf.getInputStream(jre);
155b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            // InputStream returned in any way, exception can be thrown in case
156b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            // of reading from this stream only.
157b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            // The same for IOException
158b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson            fail("Should throw IllegalStateException");
1596a8d5d27f7bd3e4ae00aa16e3f1148086b0b890eJesse Wilson        } catch (IllegalStateException expected) {
160b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson        }
161b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson    }
162b18d7a024d28fcce3497e2cd8d78642b5d057d45Jesse Wilson}
163