FileDocCategorySizeDatePackage
ZipUtilTest.javaAPI DocAndroid 5.1 API6557Thu Mar 12 22:22:48 GMT 2015android.support.multidex

ZipUtilTest

public class ZipUtilTest extends Object
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.

Fields Summary
private static final File
zipFile
Constructors Summary
Methods Summary
public static voidsetupClass()

    
           
        // just verify the zip is valid
        new ZipFile(zipFile).close();
    
public voidtestCrcDoNotCrash()


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

    
public voidtestCrcRange()

        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();
    
public voidtestCrcValue()

        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();
    
public voidtestInvalidCrcValue()

        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();