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.jar;
19
20import dalvik.annotation.TestTargetClass;
21import dalvik.annotation.TestTargets;
22import dalvik.annotation.TestLevel;
23import dalvik.annotation.TestTargetNew;
24
25import java.io.File;
26import java.io.IOException;
27import java.io.InputStream;
28import java.security.CodeSigner;
29import java.util.Enumeration;
30import java.util.List;
31import java.util.jar.JarEntry;
32import java.util.jar.JarFile;
33import java.util.zip.ZipEntry;
34import junit.framework.TestCase;
35import tests.support.resource.Support_Resources;
36
37
38@TestTargetClass(JarEntry.class)
39public class JarEntryTest extends TestCase {
40    private ZipEntry zipEntry;
41
42    private JarEntry jarEntry;
43
44    private JarFile jarFile;
45
46    private final String jarName = "hyts_patch.jar";
47
48    private final String entryName = "foo/bar/A.class";
49
50    private final String entryName2 = "Blah.txt";
51
52    private final String attJarName = "hyts_att.jar";
53
54    private final String attEntryName = "HasAttributes.txt";
55
56    private final String attEntryName2 = "NoAttributes.txt";
57
58    private File resources;
59
60    @Override
61    protected void setUp() throws Exception {
62        resources = Support_Resources.createTempFolder();
63        Support_Resources.copyFile(resources, null, jarName);
64        jarFile = new JarFile(new File(resources, jarName));
65    }
66
67    @Override
68    protected void tearDown() throws Exception {
69        if (jarFile != null) {
70            jarFile.close();
71        }
72    }
73
74    /**
75     * @throws IOException
76     * @tests java.util.jar.JarEntry#JarEntry(java.util.jar.JarEntry)
77     */
78    @TestTargetNew(
79            level = TestLevel.PARTIAL_COMPLETE,
80            notes = "",
81            method = "JarEntry",
82            args = {java.util.jar.JarEntry.class}
83    )
84    public void test_ConstructorLjava_util_jar_JarEntry_on_null() throws IOException {
85        JarEntry newJarEntry = new JarEntry(jarFile.getJarEntry(entryName));
86        assertNotNull(newJarEntry);
87
88        jarEntry = null;
89        try {
90            newJarEntry = new JarEntry(jarEntry);
91            fail("Should throw NullPointerException");
92        } catch (NullPointerException e) {
93            // Expected
94        }
95    }
96
97    /**
98     * @tests java.util.jar.JarEntry#JarEntry(java.util.zip.ZipEntry)
99     */
100    @TestTargetNew(
101        level = TestLevel.COMPLETE,
102        notes = "",
103        method = "JarEntry",
104        args = {java.util.zip.ZipEntry.class}
105    )
106    public void test_ConstructorLjava_util_zip_ZipEntry() {
107        assertNotNull("Jar file is null", jarFile);
108        zipEntry = jarFile.getEntry(entryName);
109        assertNotNull("Zip entry is null", zipEntry);
110        jarEntry = new JarEntry(zipEntry);
111        assertNotNull("Jar entry is null", jarEntry);
112        assertEquals("Wrong entry constructed--wrong name", entryName, jarEntry
113                .getName());
114        assertEquals("Wrong entry constructed--wrong size", 311, jarEntry
115                .getSize());
116    }
117
118    /**
119     * @tests java.util.jar.JarEntry#getAttributes()
120     */
121    @TestTargetNew(
122        level = TestLevel.COMPLETE,
123        notes = "",
124        method = "getAttributes",
125        args = {}
126    )
127    public void test_getAttributes() {
128        JarFile attrJar = null;
129        File file = null;
130        try {
131            Support_Resources.copyFile(resources, null, attJarName);
132            file = new File(resources, attJarName);
133            attrJar = new JarFile(file);
134        } catch (Exception e) {
135            assertTrue(file + " does not exist", file.exists());
136            fail("Exception opening file: " + e.toString());
137        }
138        try {
139            jarEntry = attrJar.getJarEntry(attEntryName);
140            assertNotNull("Should have Manifest attributes", jarEntry
141                    .getAttributes());
142        } catch (Exception e) {
143            fail("Exception during 2nd test: " + e.toString());
144        }
145        try {
146            jarEntry = attrJar.getJarEntry(attEntryName2);
147            assertNull("Shouldn't have any Manifest attributes", jarEntry
148                    .getAttributes());
149            attrJar.close();
150        } catch (Exception e) {
151            fail("Exception during 1st test: " + e.toString());
152        }
153
154        Support_Resources.copyFile(resources, null, "Broken_manifest.jar");
155        try {
156            attrJar = new JarFile(new File(resources, "Broken_manifest.jar"));
157            jarEntry = attrJar.getJarEntry("META-INF/");
158            jarEntry.getAttributes();
159            fail("IOException expected");
160        } catch (IOException e) {
161            // expected.
162        }
163    }
164
165    /**
166     * @tests java.util.jar.JarEntry#getCertificates()
167     */
168    @TestTargetNew(
169        level = TestLevel.COMPLETE,
170        notes = "",
171        method = "getCertificates",
172        args = {}
173    )
174    public void test_getCertificates() throws Exception {
175        zipEntry = jarFile.getEntry(entryName2);
176        jarEntry = new JarEntry(zipEntry);
177        assertNull("Shouldn't have any Certificates", jarEntry
178                .getCertificates());
179
180        // Regression Test for HARMONY-3424
181        String jarFileName = "TestCodeSigners.jar";
182        Support_Resources.copyFile(resources, null, jarFileName);
183        File file = new File(resources, jarFileName);
184        JarFile jarFile = new JarFile(file);
185        JarEntry jarEntry1 = jarFile.getJarEntry("Test.class");
186        JarEntry jarEntry2 = jarFile.getJarEntry("Test.class");
187        InputStream in = jarFile.getInputStream(jarEntry1);
188        byte[] buffer = new byte[1024];
189        // BEGIN android-changed
190        // the certificates are non-null too early and in.available() fails
191        // while (in.available() > 0) {
192        //     assertNull("getCertificates() should be null until the entry is read",
193        //             jarEntry1.getCertificates());
194        //     assertNull(jarEntry2.getCertificates());
195        //     in.read(buffer);
196        // }
197        while (in.read(buffer) >= 0);
198        in.close();
199        // END android-changed
200        assertEquals("the file is fully read", -1, in.read());
201        assertNotNull(jarEntry1.getCertificates());
202        assertNotNull(jarEntry2.getCertificates());
203        in.close();
204    }
205
206    /**
207     * @tests java.util.jar.JarEntry#getCodeSigners()
208     */
209    @TestTargetNew(
210        level = TestLevel.COMPLETE,
211        notes = "",
212        method = "getCodeSigners",
213        args = {}
214    )
215    public void test_getCodeSigners() throws IOException {
216        String jarFileName = "TestCodeSigners.jar";
217        Support_Resources.copyFile(resources, null, jarFileName);
218        File file = new File(resources, jarFileName);
219        JarFile jarFile = new JarFile(file);
220        JarEntry jarEntry = jarFile.getJarEntry("Test.class");
221        InputStream in = jarFile.getInputStream(jarEntry);
222        byte[] buffer = new byte[1024];
223        while (in.available() > 0) {
224            // BEGIN android-changed
225            // the code signers are non-null too early
226            // assertNull("getCodeSigners() should be null until the entry is read",
227            //         jarEntry.getCodeSigners());
228            // END android-changed
229            in.read(buffer);
230        }
231        assertEquals("the file is fully read", -1, in.read());
232        CodeSigner[] codeSigners = jarEntry.getCodeSigners();
233        assertEquals(2, codeSigners.length);
234        List<?> certs_bob = codeSigners[0].getSignerCertPath()
235                .getCertificates();
236        List<?> certs_alice = codeSigners[1].getSignerCertPath()
237                .getCertificates();
238        if (1 == certs_bob.size()) {
239            List<?> temp = certs_bob;
240            certs_bob = certs_alice;
241            certs_alice = temp;
242        }
243        assertEquals(2, certs_bob.size());
244        assertEquals(1, certs_alice.size());
245        assertNull(
246                "getCodeSigners() of a primitive JarEntry should return null",
247                new JarEntry("aaa").getCodeSigners());
248    }
249
250    @TestTargetNew(
251        level = TestLevel.COMPLETE,
252        notes = "",
253        method = "JarEntry",
254        args = {java.lang.String.class}
255    )
256    public void test_ConstructorLjava_lang_String() {
257        assertNotNull("Jar file is null", jarFile);
258        zipEntry = jarFile.getEntry(entryName);
259        assertNotNull("Zip entry is null", zipEntry);
260        jarEntry = new JarEntry(entryName);
261        assertNotNull("Jar entry is null", jarEntry);
262        assertEquals("Wrong entry constructed--wrong name", entryName, jarEntry
263                .getName());
264        try {
265            jarEntry = new JarEntry((String) null);
266            fail("NullPointerException expected");
267        } catch (NullPointerException ee) {
268            // expected
269        }
270        StringBuffer sb = new StringBuffer();
271        for (int i = 0; i < 0x10000; i++) {
272            sb.append('3');
273        }
274        try {
275            jarEntry = new JarEntry(new String(sb));
276            fail("IllegalArgumentException expected");
277        } catch (IllegalArgumentException ee) {
278            // expected
279        }
280    }
281
282    @TestTargetNew(
283        level = TestLevel.PARTIAL_COMPLETE,
284        notes = "",
285        method = "JarEntry",
286        args = {java.util.jar.JarEntry.class}
287    )
288    public void test_ConstructorLjava_util_jar_JarEntry() {
289        assertNotNull("Jar file is null", jarFile);
290        JarEntry je = jarFile.getJarEntry(entryName);
291        assertNotNull("Jar entry is null", je);
292        jarEntry = new JarEntry(je);
293        assertNotNull("Jar entry is null", jarEntry);
294        assertEquals("Wrong entry constructed--wrong name", entryName, jarEntry
295                .getName());
296        assertEquals("Wrong entry constructed--wrong size", 311, jarEntry
297                .getSize());
298    }
299}
300