ZipOutputStreamTest.java revision a812a87e69850d1492c45bd88d7ff3dbf21d5075
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.BufferedOutputStream;
20import java.io.ByteArrayOutputStream;
21import java.io.File;
22import java.io.FileOutputStream;
23import java.io.IOException;
24import java.util.Arrays;
25import java.util.Random;
26import java.util.zip.ZipEntry;
27import java.util.zip.ZipException;
28import java.util.zip.ZipOutputStream;
29import junit.framework.TestCase;
30
31public final class ZipOutputStreamTest extends TestCase {
32    public void testShortMessage() throws IOException {
33        byte[] data = "Hello World".getBytes("UTF-8");
34        byte[] zipped = zip("short", data);
35        assertEquals(Arrays.toString(data), Arrays.toString(ZipInputStreamTest.unzip("short", zipped)));
36    }
37
38    // http://b/3181430 --- a sign-extension bug on CRCs with the top bit set.
39    public void test3181430() throws IOException {
40        byte[] data = new byte[1]; // CRC32({ 0 }) == 0xd202ef8d
41        byte[] zipped = zip("z", data);
42        assertEquals(Arrays.toString(data), Arrays.toString(ZipInputStreamTest.unzip("z", zipped)));
43    }
44
45    public void testLongMessage() throws IOException {
46        byte[] data = new byte[1024 * 1024];
47        new Random().nextBytes(data);
48        assertTrue(Arrays.equals(data, ZipInputStreamTest.unzip("r", zip("r", data))));
49    }
50
51    public static byte[] zip(String name, byte[] bytes) throws IOException {
52        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
53        ZipOutputStream zippedOut = new ZipOutputStream(bytesOut);
54
55        ZipEntry entry = new ZipEntry(name);
56        zippedOut.putNextEntry(entry);
57        zippedOut.write(bytes);
58        zippedOut.closeEntry();
59
60        zippedOut.close();
61        return bytesOut.toByteArray();
62    }
63
64    /**
65     * Reference implementation does NOT allow writing of an empty zip using a
66     * {@link ZipOutputStream}.
67     */
68    public void testCreateEmpty() throws IOException {
69        File result = File.createTempFile("ZipFileTest", "zip");
70        ZipOutputStream out =
71                new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(result)));
72        try {
73            out.close();
74            fail("Close on empty stream failed to throw exception");
75        } catch (ZipException e) {
76            // expected
77        }
78    }
79
80    /** Regression test for null comment causing a NullPointerException during write. */
81    public void testNullComment() throws IOException {
82        ZipOutputStream out = new ZipOutputStream(new ByteArrayOutputStream());
83        out.setComment(null);
84        out.putNextEntry(new ZipEntry("name"));
85        out.write(new byte[1]);
86        out.closeEntry();
87        out.finish();
88    }
89}
90