1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.java.util.zip;
18
19import java.io.File;
20import java.io.FileInputStream;
21import java.io.FileOutputStream;
22import java.util.Arrays;
23import java.util.List;
24import java.util.jar.JarEntry;
25import java.util.zip.ZipEntry;
26import java.util.zip.ZipFile;
27import java.util.zip.ZipInputStream;
28import java.util.zip.ZipOutputStream;
29
30public class ZipEntryTest extends junit.framework.TestCase {
31
32    /**
33     * http://code.google.com/p/android/issues/detail?id=4690
34     */
35    public void test_utf8FileNames() throws Exception {
36        // Create a zip file containing non-ASCII filenames.
37        File f = File.createTempFile("your", "mum");
38        List<String> filenames = Arrays.asList("us-ascii",
39                "\u043c\u0430\u0440\u0442\u0430", // russian
40                "\u1f00\u03c0\u1f78", // greek
41                "\u30b3\u30f3\u30cb\u30c1\u30cf"); // japanese
42        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f));
43        for (String filename : filenames) {
44            out.putNextEntry(new ZipEntry(filename));
45            out.closeEntry(); // Empty files are fine.
46        }
47        out.close();
48        // Read it back, and check we find all those names.
49        // This failed when we were mangling the encoding.
50        ZipFile zipFile = new ZipFile(f);
51        for (String filename : filenames) {
52            assertNotNull(filename, zipFile.getEntry(filename));
53        }
54        // Check that ZipInputStream works too.
55        ZipInputStream in = new ZipInputStream(new FileInputStream(f));
56        ZipEntry entry;
57        int entryCount = 0;
58        while ((entry = in.getNextEntry()) != null) {
59            assertTrue(entry.getName(), filenames.contains(entry.getName()));
60            ++entryCount;
61        }
62        assertEquals(filenames.size(), entryCount);
63        in.close();
64    }
65
66    /**
67     * http://b/2099615
68     */
69    public void testClone() {
70        byte[] extra = { 5, 7, 9 };
71        JarEntry jarEntry = new JarEntry("foo");
72        jarEntry.setExtra(extra);
73        assertSame("Expected no defensive copy of extra", extra, jarEntry.getExtra());
74
75        ZipEntry clone = (ZipEntry) jarEntry.clone();
76        assertEquals(JarEntry.class, clone.getClass());
77        assertNotSame(extra, clone.getExtra());
78    }
79}
80