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.tests.java.util.jar;
19
20import java.io.File;
21import java.io.IOException;
22import java.io.InputStream;
23import java.security.CodeSigner;
24import java.util.List;
25import java.util.jar.JarEntry;
26import java.util.jar.JarFile;
27import java.util.zip.ZipEntry;
28import junit.framework.TestCase;
29import libcore.io.Streams;
30import tests.support.resource.Support_Resources;
31
32public class JarEntryTest extends TestCase {
33    private ZipEntry zipEntry;
34
35    private JarEntry jarEntry;
36
37    private JarFile jarFile;
38
39    private final String jarName = "hyts_patch.jar";
40
41    private final String entryName = "foo/bar/A.class";
42
43    private final String entryName2 = "Blah.txt";
44
45    private final String attJarName = "hyts_att.jar";
46
47    private final String attEntryName = "HasAttributes.txt";
48
49    private final String attEntryName2 = "NoAttributes.txt";
50
51    private File resources;
52
53    @Override
54    protected void setUp() throws Exception {
55        resources = Support_Resources.createTempFolder();
56        Support_Resources.copyFile(resources, null, jarName);
57        jarFile = new JarFile(new File(resources, jarName));
58    }
59
60    @Override
61    protected void tearDown() throws Exception {
62        if (jarFile != null) {
63            jarFile.close();
64        }
65    }
66
67    /**
68     * @throws IOException
69     * java.util.jar.JarEntry#JarEntry(java.util.jar.JarEntry)
70     */
71    public void test_ConstructorLjava_util_jar_JarEntry() throws IOException {
72        JarEntry newJarEntry = new JarEntry(jarFile.getJarEntry(entryName));
73        assertNotNull(newJarEntry);
74
75        jarEntry = null;
76        try {
77            newJarEntry = new JarEntry(jarEntry);
78            fail("Should throw NullPointerException");
79        } catch (NullPointerException e) {
80            // Expected
81        }
82    }
83
84    public void test_ConstructorLjava_util_zip_ZipEntry() {
85        assertNotNull("Jar file is null", jarFile);
86        zipEntry = jarFile.getEntry(entryName);
87        assertNotNull("Zip entry is null", zipEntry);
88        jarEntry = new JarEntry(zipEntry);
89        assertNotNull("Jar entry is null", jarEntry);
90        assertEquals("Wrong entry constructed--wrong name", entryName, jarEntry
91                .getName());
92        assertEquals("Wrong entry constructed--wrong size", 311, jarEntry
93                .getSize());
94    }
95
96    /**
97     * java.util.jar.JarEntry#getAttributes()
98     */
99    public void test_getAttributes() throws Exception {
100        JarFile attrJar = null;
101        File file = null;
102
103        Support_Resources.copyFile(resources, null, attJarName);
104        file = new File(resources, attJarName);
105        attrJar = new JarFile(file);
106
107        jarEntry = attrJar.getJarEntry(attEntryName);
108        assertNotNull("Should have Manifest attributes", jarEntry
109                .getAttributes());
110
111        jarEntry = attrJar.getJarEntry(attEntryName2);
112        assertNull("Shouldn't have any Manifest attributes", jarEntry
113                .getAttributes());
114        attrJar.close();
115    }
116
117    // http://b/1864326
118    public void testCertificatesAndCodesigners() throws Exception {
119        zipEntry = jarFile.getEntry(entryName2);
120        jarEntry = new JarEntry(zipEntry);
121        assertNull(jarEntry.getCertificates());
122
123        // Regression Test for HARMONY-3424, b/1864326
124        String jarFileName = "TestCodeSigners.jar";
125        Support_Resources.copyFile(resources, null, jarFileName);
126        File file = new File(resources, jarFileName);
127        JarFile jarFile = new JarFile(file);
128        JarEntry jarEntry1 = jarFile.getJarEntry("Test.class");
129        JarEntry jarEntry2 = jarFile.getJarEntry("Test.class");
130
131        try (InputStream in = jarFile.getInputStream(jarEntry1)) {
132            // Code signers and certs must be {@code null} until the entry is completely
133            // read.
134            assertNull(jarEntry1.getCertificates());
135            assertNull(jarEntry2.getCertificates());
136            assertNull(jarEntry1.getCodeSigners());
137            assertNull(jarEntry2.getCodeSigners());
138
139            // Read a few bytes from the stream.
140            in.read(new byte[64]);
141            assertNull(jarEntry1.getCertificates());
142            assertNull(jarEntry2.getCertificates());
143            assertNull(jarEntry1.getCodeSigners());
144            assertNull(jarEntry2.getCodeSigners());
145
146            // Read the rest of the stream.
147            Streams.skipByReading(in, Long.MAX_VALUE);
148
149            assertEquals(-1, in.read());
150            assertNotNull(jarEntry1.getCodeSigners());
151            assertNotNull(jarEntry2.getCodeSigners());
152            assertNotNull(jarEntry1.getCertificates());
153            assertNotNull(jarEntry2.getCertificates());
154        }
155    }
156
157    public void test_getCodeSigners() throws IOException {
158        String jarFileName = "TestCodeSigners.jar";
159        Support_Resources.copyFile(resources, null, jarFileName);
160        File file = new File(resources, jarFileName);
161        JarFile jarFile = new JarFile(file);
162        JarEntry jarEntry = jarFile.getJarEntry("Test.class");
163        InputStream in = jarFile.getInputStream(jarEntry);
164        byte[] buffer = new byte[1024];
165        while (in.available() > 0) {
166            assertNull("getCodeSigners() should be null until the entry is read",
167                    jarEntry.getCodeSigners());
168            in.read(buffer);
169        }
170        assertEquals("the file is fully read", -1, in.read());
171        CodeSigner[] codeSigners = jarEntry.getCodeSigners();
172        assertEquals(2, codeSigners.length);
173        List<?> certs_bob = codeSigners[0].getSignerCertPath()
174                .getCertificates();
175        List<?> certs_alice = codeSigners[1].getSignerCertPath()
176                .getCertificates();
177        if (1 == certs_bob.size()) {
178            List<?> temp = certs_bob;
179            certs_bob = certs_alice;
180            certs_alice = temp;
181        }
182        assertEquals(2, certs_bob.size());
183        assertEquals(1, certs_alice.size());
184        assertNull(
185                "getCodeSigners() should be null for a primitive JarEntry",
186                new JarEntry("aaa").getCodeSigners());
187    }
188}
189