1 /* 2 * Copyright (C) 2013 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 package com.android.tradefed.util; 17 18 import junit.framework.TestCase; 19 20 import java.io.File; 21 import java.io.IOException; 22 import java.io.InputStream; 23 import java.util.Arrays; 24 import java.util.HashSet; 25 import java.util.Set; 26 import java.util.zip.ZipFile; 27 28 /** 29 * Unit tests for {@link ZipUtil} 30 */ 31 public class ZipUtilTest extends TestCase { 32 private Set<File> mTempFiles = new HashSet<File>(); 33 34 @Override 35 protected void tearDown() throws Exception { 36 super.tearDown(); 37 for (File file : mTempFiles) { 38 if (file != null && file.exists()) { 39 if (file.isDirectory()) { 40 FileUtil.recursiveDelete(file); 41 } else { 42 file.delete(); 43 } 44 } 45 } 46 } 47 48 private File getTestDataFile(String name) throws IOException { 49 final InputStream inputStream = 50 getClass().getResourceAsStream(String.format("/util/%s.zip", name)); 51 final File zipFile = createTempFile(name, ".zip"); 52 FileUtil.writeToFile(inputStream, zipFile); 53 return zipFile; 54 } 55 56 /** 57 * Test that our _simple_ corrupt zip detection heuristics work properly. It is expected 58 * that this check will _fail_ to detect a corrupt but well-formed Zip archive. 59 */ 60 public void testSimpleCorruptZipCheck() throws Exception { 61 assertTrue("Falsely detected 'normal.zip' test file as corrupt!", 62 ZipUtil.isZipFileValid(getTestDataFile("normal"), false)); 63 assertFalse("Failed to detect 'truncated.zip' test file as corrupt!", 64 ZipUtil.isZipFileValid(getTestDataFile("truncated"), false)); 65 assertTrue("Unexpectedly detected 'normal.zip' test file as corrupt! Hope?", 66 ZipUtil.isZipFileValid(getTestDataFile("corrupt"), false)); 67 } 68 69 /** 70 * Test that our _thorough_ corrupt zip detection heuristics work properly. 71 */ 72 public void testThoroughCorruptZipCheck() throws Exception { 73 assertTrue("Falsely detected 'normal.zip' test file as corrupt with thorough check!", 74 ZipUtil.isZipFileValid(getTestDataFile("normal"), true)); 75 assertFalse("Failed to detect 'truncated.zip' test file as corrupt with thorough check!", 76 ZipUtil.isZipFileValid(getTestDataFile("truncated"), true)); 77 assertFalse("Failed to detect 'normal.zip' test file as corrupt with thorough check!", 78 ZipUtil.isZipFileValid(getTestDataFile("corrupt"), true)); 79 } 80 81 /** 82 * Test creating then extracting a zip file from a directory 83 * 84 * @throws IOException 85 */ 86 public void testCreateAndExtractZip() throws IOException { 87 File tmpParentDir = createTempDir("foo"); 88 File zipFile = null; 89 File extractedDir = createTempDir("extract-foo"); 90 try { 91 File childDir = new File(tmpParentDir, "foochild"); 92 assertTrue(childDir.mkdir()); 93 File subFile = new File(childDir, "foo.txt"); 94 FileUtil.writeToFile("contents", subFile); 95 zipFile = ZipUtil.createZip(tmpParentDir); 96 ZipUtil.extractZip(new ZipFile(zipFile), extractedDir); 97 98 // assert all contents of original zipped dir are extracted 99 File extractedParentDir = new File(extractedDir, tmpParentDir.getName()); 100 File extractedChildDir = new File(extractedParentDir, childDir.getName()); 101 File extractedSubFile = new File(extractedChildDir, subFile.getName()); 102 assertTrue(extractedParentDir.exists()); 103 assertTrue(extractedChildDir.exists()); 104 assertTrue(extractedSubFile.exists()); 105 assertTrue(FileUtil.compareFileContents(subFile, extractedSubFile)); 106 } finally { 107 FileUtil.deleteFile(zipFile); 108 } 109 } 110 111 /** 112 * Test creating then extracting a zip file from a list of files 113 * 114 * @throws IOException 115 */ 116 public void testCreateAndExtractZip_fromFiles() throws IOException { 117 File tmpParentDir = createTempDir("foo"); 118 File zipFile = null; 119 File extractedDir = createTempDir("extract-foo"); 120 try { 121 File file1 = new File(tmpParentDir, "foo.txt"); 122 File file2 = new File(tmpParentDir, "bar.txt"); 123 FileUtil.writeToFile("contents1", file1); 124 FileUtil.writeToFile("contents2", file2); 125 zipFile = ZipUtil.createZip(Arrays.asList(file1, file2)); 126 ZipUtil.extractZip(new ZipFile(zipFile), extractedDir); 127 128 // assert all contents of original zipped dir are extracted 129 File extractedFile1 = new File(extractedDir, file1.getName()); 130 File extractedFile2 = new File(extractedDir, file2.getName()); 131 assertTrue(extractedFile1.exists()); 132 assertTrue(extractedFile2.exists()); 133 assertTrue(FileUtil.compareFileContents(file1, extractedFile1)); 134 assertTrue(FileUtil.compareFileContents(file2, extractedFile2)); 135 } finally { 136 FileUtil.deleteFile(zipFile); 137 } 138 } 139 140 141 /** 142 * Test that isZipFileValid returns false if calling with a file that does not exist. 143 * 144 * @throws IOException 145 */ 146 public void testZipFileDoesNotExist() throws IOException { 147 File file = new File("/tmp/this-file-does-not-exist.zip"); 148 assertFalse(ZipUtil.isZipFileValid(file, true)); 149 assertFalse(ZipUtil.isZipFileValid(file, false)); 150 } 151 152 /** 153 * Test creating then extracting a a single file from zip file 154 * 155 * @throws IOException 156 */ 157 public void testCreateAndExtractFileFromZip() throws IOException { 158 File tmpParentDir = createTempDir("foo"); 159 File zipFile = null; 160 File extractedSubFile = null; 161 try { 162 File childDir = new File(tmpParentDir, "foochild"); 163 assertTrue(childDir.mkdir()); 164 File subFile = new File(childDir, "foo.txt"); 165 FileUtil.writeToFile("contents", subFile); 166 zipFile = ZipUtil.createZip(tmpParentDir); 167 168 extractedSubFile = ZipUtil.extractFileFromZip(new ZipFile(zipFile), 169 tmpParentDir.getName() + "/foochild/foo.txt"); 170 assertNotNull(extractedSubFile); 171 assertTrue(FileUtil.compareFileContents(subFile, extractedSubFile)); 172 } finally { 173 FileUtil.deleteFile(zipFile); 174 FileUtil.deleteFile(extractedSubFile); 175 } 176 } 177 178 /** 179 * Test that {@link ZipUtil#extractZipToTemp(File, String)} properly throws when an incorrect 180 * zip is presented. 181 */ 182 public void testExtractZipToTemp() throws Exception { 183 File tmpFile = FileUtil.createTempFile("ziputiltest", ".zip"); 184 try { 185 ZipUtil.extractZipToTemp(tmpFile, "testExtractZipToTemp"); 186 fail("Should have thrown an exception"); 187 } catch (IOException expected) { 188 // expected 189 } finally { 190 FileUtil.deleteFile(tmpFile); 191 } 192 } 193 194 // Helpers 195 private File createTempDir(String prefix) throws IOException { 196 return createTempDir(prefix, null); 197 } 198 199 private File createTempDir(String prefix, File parentDir) throws IOException { 200 File tempDir = FileUtil.createTempDir(prefix, parentDir); 201 mTempFiles.add(tempDir); 202 return tempDir; 203 } 204 205 private File createTempFile(String prefix, String suffix) throws IOException { 206 File tempFile = FileUtil.createTempFile(prefix, suffix); 207 mTempFiles.add(tempFile); 208 return tempFile; 209 } 210 } 211