155e276b6718282224e0069645da2222beda7b8dfElliott Hughes/*
255e276b6718282224e0069645da2222beda7b8dfElliott Hughes * Copyright (C) 2010 The Android Open Source Project
355e276b6718282224e0069645da2222beda7b8dfElliott Hughes *
455e276b6718282224e0069645da2222beda7b8dfElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
555e276b6718282224e0069645da2222beda7b8dfElliott Hughes * you may not use this file except in compliance with the License.
655e276b6718282224e0069645da2222beda7b8dfElliott Hughes * You may obtain a copy of the License at
755e276b6718282224e0069645da2222beda7b8dfElliott Hughes *
855e276b6718282224e0069645da2222beda7b8dfElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
955e276b6718282224e0069645da2222beda7b8dfElliott Hughes *
1055e276b6718282224e0069645da2222beda7b8dfElliott Hughes * Unless required by applicable law or agreed to in writing, software
1155e276b6718282224e0069645da2222beda7b8dfElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
1255e276b6718282224e0069645da2222beda7b8dfElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1355e276b6718282224e0069645da2222beda7b8dfElliott Hughes * See the License for the specific language governing permissions and
1455e276b6718282224e0069645da2222beda7b8dfElliott Hughes * limitations under the License.
1555e276b6718282224e0069645da2222beda7b8dfElliott Hughes */
1655e276b6718282224e0069645da2222beda7b8dfElliott Hughes
1755e276b6718282224e0069645da2222beda7b8dfElliott Hughespackage libcore.java.util.zip;
1855e276b6718282224e0069645da2222beda7b8dfElliott Hughes
19ef164bf196538c04f499dcbb49a389c70ff5601aPaul Duffinimport java.io.BufferedOutputStream;
2055e276b6718282224e0069645da2222beda7b8dfElliott Hughesimport java.io.ByteArrayOutputStream;
21ef164bf196538c04f499dcbb49a389c70ff5601aPaul Duffinimport java.io.File;
22ef164bf196538c04f499dcbb49a389c70ff5601aPaul Duffinimport java.io.FileOutputStream;
2355e276b6718282224e0069645da2222beda7b8dfElliott Hughesimport java.io.IOException;
2455e276b6718282224e0069645da2222beda7b8dfElliott Hughesimport java.util.Arrays;
2555e276b6718282224e0069645da2222beda7b8dfElliott Hughesimport java.util.Random;
2655e276b6718282224e0069645da2222beda7b8dfElliott Hughesimport java.util.zip.ZipEntry;
27ef164bf196538c04f499dcbb49a389c70ff5601aPaul Duffinimport java.util.zip.ZipException;
2855e276b6718282224e0069645da2222beda7b8dfElliott Hughesimport java.util.zip.ZipOutputStream;
2955e276b6718282224e0069645da2222beda7b8dfElliott Hughesimport junit.framework.TestCase;
3055e276b6718282224e0069645da2222beda7b8dfElliott Hughes
3155e276b6718282224e0069645da2222beda7b8dfElliott Hughespublic final class ZipOutputStreamTest extends TestCase {
3255e276b6718282224e0069645da2222beda7b8dfElliott Hughes    public void testShortMessage() throws IOException {
3355e276b6718282224e0069645da2222beda7b8dfElliott Hughes        byte[] data = "Hello World".getBytes("UTF-8");
3455e276b6718282224e0069645da2222beda7b8dfElliott Hughes        byte[] zipped = zip("short", data);
3555e276b6718282224e0069645da2222beda7b8dfElliott Hughes        assertEquals(Arrays.toString(data), Arrays.toString(ZipInputStreamTest.unzip("short", zipped)));
3655e276b6718282224e0069645da2222beda7b8dfElliott Hughes    }
3755e276b6718282224e0069645da2222beda7b8dfElliott Hughes
3827495197b6f411df17116022bd0f5458aecd9c84Elliott Hughes    // http://b/3181430 --- a sign-extension bug on CRCs with the top bit set.
3927495197b6f411df17116022bd0f5458aecd9c84Elliott Hughes    public void test3181430() throws IOException {
4027495197b6f411df17116022bd0f5458aecd9c84Elliott Hughes        byte[] data = new byte[1]; // CRC32({ 0 }) == 0xd202ef8d
4127495197b6f411df17116022bd0f5458aecd9c84Elliott Hughes        byte[] zipped = zip("z", data);
4227495197b6f411df17116022bd0f5458aecd9c84Elliott Hughes        assertEquals(Arrays.toString(data), Arrays.toString(ZipInputStreamTest.unzip("z", zipped)));
4327495197b6f411df17116022bd0f5458aecd9c84Elliott Hughes    }
4427495197b6f411df17116022bd0f5458aecd9c84Elliott Hughes
4555e276b6718282224e0069645da2222beda7b8dfElliott Hughes    public void testLongMessage() throws IOException {
4655e276b6718282224e0069645da2222beda7b8dfElliott Hughes        byte[] data = new byte[1024 * 1024];
4755e276b6718282224e0069645da2222beda7b8dfElliott Hughes        new Random().nextBytes(data);
4855e276b6718282224e0069645da2222beda7b8dfElliott Hughes        assertTrue(Arrays.equals(data, ZipInputStreamTest.unzip("r", zip("r", data))));
4955e276b6718282224e0069645da2222beda7b8dfElliott Hughes    }
5055e276b6718282224e0069645da2222beda7b8dfElliott Hughes
5155e276b6718282224e0069645da2222beda7b8dfElliott Hughes    public static byte[] zip(String name, byte[] bytes) throws IOException {
5255e276b6718282224e0069645da2222beda7b8dfElliott Hughes        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
5355e276b6718282224e0069645da2222beda7b8dfElliott Hughes        ZipOutputStream zippedOut = new ZipOutputStream(bytesOut);
5455e276b6718282224e0069645da2222beda7b8dfElliott Hughes
5555e276b6718282224e0069645da2222beda7b8dfElliott Hughes        ZipEntry entry = new ZipEntry(name);
5655e276b6718282224e0069645da2222beda7b8dfElliott Hughes        zippedOut.putNextEntry(entry);
5755e276b6718282224e0069645da2222beda7b8dfElliott Hughes        zippedOut.write(bytes);
5855e276b6718282224e0069645da2222beda7b8dfElliott Hughes        zippedOut.closeEntry();
5955e276b6718282224e0069645da2222beda7b8dfElliott Hughes
6055e276b6718282224e0069645da2222beda7b8dfElliott Hughes        zippedOut.close();
6155e276b6718282224e0069645da2222beda7b8dfElliott Hughes        return bytesOut.toByteArray();
6255e276b6718282224e0069645da2222beda7b8dfElliott Hughes    }
63ef164bf196538c04f499dcbb49a389c70ff5601aPaul Duffin
64ef164bf196538c04f499dcbb49a389c70ff5601aPaul Duffin    /**
65ef164bf196538c04f499dcbb49a389c70ff5601aPaul Duffin     * Reference implementation does NOT allow writing of an empty zip using a
66ef164bf196538c04f499dcbb49a389c70ff5601aPaul Duffin     * {@link ZipOutputStream}.
67ef164bf196538c04f499dcbb49a389c70ff5601aPaul Duffin     */
68ef164bf196538c04f499dcbb49a389c70ff5601aPaul Duffin    public void testCreateEmpty() throws IOException {
69ef164bf196538c04f499dcbb49a389c70ff5601aPaul Duffin        File result = File.createTempFile("ZipFileTest", "zip");
70ef164bf196538c04f499dcbb49a389c70ff5601aPaul Duffin        ZipOutputStream out =
71ef164bf196538c04f499dcbb49a389c70ff5601aPaul Duffin                new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(result)));
72ef164bf196538c04f499dcbb49a389c70ff5601aPaul Duffin        try {
73ef164bf196538c04f499dcbb49a389c70ff5601aPaul Duffin            out.close();
74ef164bf196538c04f499dcbb49a389c70ff5601aPaul Duffin            fail("Close on empty stream failed to throw exception");
75ef164bf196538c04f499dcbb49a389c70ff5601aPaul Duffin        } catch (ZipException e) {
76ef164bf196538c04f499dcbb49a389c70ff5601aPaul Duffin            // expected
77ef164bf196538c04f499dcbb49a389c70ff5601aPaul Duffin        }
78ef164bf196538c04f499dcbb49a389c70ff5601aPaul Duffin    }
79e3d756c5dae1af2aa5f0ad8bc7f133df3e7401ebNeil Fuller
80e3d756c5dae1af2aa5f0ad8bc7f133df3e7401ebNeil Fuller    /** Regression test for null comment causing a NullPointerException during write. */
81e3d756c5dae1af2aa5f0ad8bc7f133df3e7401ebNeil Fuller    public void testNullComment() throws IOException {
82e3d756c5dae1af2aa5f0ad8bc7f133df3e7401ebNeil Fuller        ZipOutputStream out = new ZipOutputStream(new ByteArrayOutputStream());
83e3d756c5dae1af2aa5f0ad8bc7f133df3e7401ebNeil Fuller        out.setComment(null);
84e3d756c5dae1af2aa5f0ad8bc7f133df3e7401ebNeil Fuller        out.putNextEntry(new ZipEntry("name"));
85e3d756c5dae1af2aa5f0ad8bc7f133df3e7401ebNeil Fuller        out.write(new byte[1]);
86e3d756c5dae1af2aa5f0ad8bc7f133df3e7401ebNeil Fuller        out.closeEntry();
87e3d756c5dae1af2aa5f0ad8bc7f133df3e7401ebNeil Fuller        out.finish();
88e3d756c5dae1af2aa5f0ad8bc7f133df3e7401ebNeil Fuller    }
8955e276b6718282224e0069645da2222beda7b8dfElliott Hughes}
90