summaryrefslogtreecommitdiff
path: root/library/test/src/android/support/multidex/ZipUtilTest.java
blob: 985d97f49a66795829b90dac7c56a0f06b11d66b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.support.multidex;

import android.support.multidex.ZipUtil.CentralDirectory;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.EOFException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

/**
 * Tests of ZipUtil class.
 *
 * The test assumes that ANDROID_BUILD_TOP environment variable is defined and point to the top of a
 * built android tree. This is the case when the console used for running the tests is setup for
 * android tree compilation.
 */
public class ZipUtilTest {
    private static final File zipFile = new File(System.getenv("ANDROID_BUILD_TOP"),
        "out/target/common/obj/JAVA_LIBRARIES/android-support-multidex_intermediates/javalib.jar");
    @BeforeClass
    public static void setupClass() throws ZipException, IOException {
        // just verify the zip is valid
        new ZipFile(zipFile).close();
    }

    @Test
    public void testCrcDoNotCrash() throws IOException {

        long crc =
                ZipUtil.getZipCrc(zipFile);
        System.out.println("crc is " + crc);

    }

    @Test
    public void testCrcRange() throws IOException {
        RandomAccessFile raf = new RandomAccessFile(zipFile, "r");
        CentralDirectory dir = ZipUtil.findCentralDirectory(raf);
        byte[] dirData = new byte[(int) dir.size];
        int length = dirData.length;
        int off = 0;
        raf.seek(dir.offset);
        while (length > 0) {
            int read = raf.read(dirData, off, length);
            if (length == -1) {
                throw new EOFException();
            }
            length -= read;
            off += read;
        }
        raf.close();
        ByteBuffer buffer = ByteBuffer.wrap(dirData);
        Map<String, ZipEntry> toCheck = new HashMap<String, ZipEntry>();
        while (buffer.hasRemaining()) {
            buffer = buffer.slice();
            buffer.order(ByteOrder.LITTLE_ENDIAN);
            ZipEntry entry = ZipEntryReader.readEntry(buffer);
            toCheck.put(entry.getName(), entry);
        }

        ZipFile zip = new ZipFile(zipFile);
        Assert.assertEquals(zip.size(), toCheck.size());
        Enumeration<? extends ZipEntry> ref = zip.entries();
        while (ref.hasMoreElements()) {
            ZipEntry refEntry = ref.nextElement();
            ZipEntry checkEntry = toCheck.get(refEntry.getName());
            Assert.assertNotNull(checkEntry);
            Assert.assertEquals(refEntry.getName(), checkEntry.getName());
            Assert.assertEquals(refEntry.getComment(), checkEntry.getComment());
            Assert.assertEquals(refEntry.getTime(), checkEntry.getTime());
            Assert.assertEquals(refEntry.getCrc(), checkEntry.getCrc());
            Assert.assertEquals(refEntry.getCompressedSize(), checkEntry.getCompressedSize());
            Assert.assertEquals(refEntry.getSize(), checkEntry.getSize());
            Assert.assertEquals(refEntry.getMethod(), checkEntry.getMethod());
            Assert.assertArrayEquals(refEntry.getExtra(), checkEntry.getExtra());
        }
        zip.close();
    }

    @Test
    public void testCrcValue() throws IOException {
        ZipFile zip = new ZipFile(zipFile);
        Enumeration<? extends ZipEntry> ref = zip.entries();
        byte[] buffer = new byte[0x2000];
        while (ref.hasMoreElements()) {
            ZipEntry refEntry = ref.nextElement();
            if (refEntry.getSize() > 0) {
                File tmp = File.createTempFile("ZipUtilTest", ".fakezip");
                InputStream in = zip.getInputStream(refEntry);
                OutputStream out = new FileOutputStream(tmp);
                int read = in.read(buffer);
                while (read != -1) {
                    out.write(buffer, 0, read);
                    read = in.read(buffer);
                }
                in.close();
                out.close();
                RandomAccessFile raf = new RandomAccessFile(tmp, "r");
                CentralDirectory dir = new CentralDirectory();
                dir.offset = 0;
                dir.size = raf.length();
                long crc = ZipUtil.computeCrcOfCentralDir(raf, dir);
                Assert.assertEquals(refEntry.getCrc(), crc);
                raf.close();
                tmp.delete();
            }
        }
        zip.close();
    }
    @Test
    public void testInvalidCrcValue() throws IOException {
        ZipFile zip = new ZipFile(zipFile);
        Enumeration<? extends ZipEntry> ref = zip.entries();
        byte[] buffer = new byte[0x2000];
        while (ref.hasMoreElements()) {
            ZipEntry refEntry = ref.nextElement();
            if (refEntry.getSize() > 0) {
                File tmp = File.createTempFile("ZipUtilTest", ".fakezip");
                InputStream in = zip.getInputStream(refEntry);
                OutputStream out = new FileOutputStream(tmp);
                int read = in.read(buffer);
                while (read != -1) {
                    out.write(buffer, 0, read);
                    read = in.read(buffer);
                }
                in.close();
                out.close();
                RandomAccessFile raf = new RandomAccessFile(tmp, "r");
                CentralDirectory dir = new CentralDirectory();
                dir.offset = 0;
                dir.size = raf.length() - 1;
                long crc = ZipUtil.computeCrcOfCentralDir(raf, dir);
                Assert.assertNotEquals(refEntry.getCrc(), crc);
                raf.close();
                tmp.delete();
            }
        }
        zip.close();
    }

}