1 /* 2 * Copyright (C) 2016 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 com.android.tradefed.log.ITestLogger; 19 import com.android.tradefed.result.LogDataType; 20 21 import org.easymock.EasyMock; 22 import org.junit.After; 23 import org.junit.Assert; 24 import org.junit.Before; 25 import org.junit.Test; 26 27 import java.io.BufferedOutputStream; 28 import java.io.ByteArrayInputStream; 29 import java.io.File; 30 import java.io.FileOutputStream; 31 import java.io.IOException; 32 import java.io.InputStream; 33 import java.util.LinkedList; 34 import java.util.List; 35 import java.util.zip.ZipOutputStream; 36 37 /** 38 * Unit test suite for {@link Bugreport} 39 */ 40 public class BugreportTest { 41 42 private static final String BUGREPORT_PREFIX = "bugreport_DEVICE_"; 43 private static final String BUGREPORT_CONTENT = "BUGREPORT CONTENT"; 44 45 private Bugreport mBugreport; 46 private File mZipFile; 47 private File mRegularFile; 48 49 @Before 50 public void setUp() throws Exception { 51 mRegularFile = FileUtil.createTempFile("bugreport-test", "txt"); 52 File tempDir = null; 53 try { 54 tempDir = FileUtil.createTempDir("bugreportz"); 55 File mainEntry = FileUtil.createTempFile("main_entry", ".txt", tempDir); 56 File bugreport = FileUtil.createTempFile(BUGREPORT_PREFIX, ".txt", tempDir); 57 InputStream name = new ByteArrayInputStream(bugreport.getName().getBytes()); 58 FileUtil.writeToFile(name, mainEntry); 59 InputStream content = new ByteArrayInputStream(BUGREPORT_CONTENT.getBytes()); 60 FileUtil.writeToFile(content, bugreport); 61 mainEntry.renameTo(new File(tempDir, "main_entry.txt")); 62 // We create a fake bugreport zip with main_entry.txt and the bugreport. 63 mZipFile = ZipUtil.createZip(new File("")); 64 FileOutputStream fileStream = new FileOutputStream(mZipFile); 65 ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(fileStream)); 66 ZipUtil.addToZip(out, bugreport, new LinkedList<String>()); 67 ZipUtil.addToZip(out, new File(tempDir, "main_entry.txt"), new LinkedList<String>()); 68 StreamUtil.close(out); 69 } finally { 70 FileUtil.recursiveDelete(tempDir); 71 } 72 } 73 74 @After 75 public void tearDown() throws Exception { 76 FileUtil.deleteFile(mRegularFile); 77 FileUtil.deleteFile(mZipFile); 78 } 79 80 /** 81 * Test {@link Bugreport#getMainFile()} for a flat bugreport. 82 */ 83 @Test 84 public void testBugreport_flat() throws IOException { 85 mBugreport = new Bugreport(mRegularFile, false); 86 try { 87 Assert.assertEquals(mRegularFile, mBugreport.getMainFile()); 88 Assert.assertNull(mBugreport.getListOfFiles()); 89 } finally { 90 mBugreport.close(); 91 } 92 } 93 94 /** 95 * Test {@link Bugreport#getMainFile()} for a zipped bugreport. 96 */ 97 @Test 98 public void testBugreport_zipped() throws IOException { 99 mBugreport = new Bugreport(mZipFile, true); 100 File mainFile = null; 101 try { 102 Assert.assertEquals(2, mBugreport.getListOfFiles().size()); 103 mainFile = mBugreport.getMainFile(); 104 Assert.assertNotSame(mZipFile, mainFile); 105 Assert.assertEquals(BUGREPORT_CONTENT, FileUtil.readStringFromFile(mainFile)); 106 } finally { 107 FileUtil.deleteFile(mainFile); 108 mBugreport.close(); 109 } 110 } 111 112 /** 113 * Test {@link Bugreport#getFileByName(String)} for a zipped bugreport. 114 */ 115 @Test 116 public void testBugreport_getFileByName() throws IOException { 117 mBugreport = new Bugreport(mZipFile, true); 118 File mainFile = null; 119 try { 120 List<String> listFile = mBugreport.getListOfFiles(); 121 Assert.assertEquals(2, listFile.size()); 122 String toFetch = null; 123 for (String name : listFile) { 124 if (name.startsWith(BUGREPORT_PREFIX)) { 125 toFetch = name; 126 } 127 } 128 Assert.assertNotNull(toFetch); 129 mainFile = mBugreport.getFileByName(toFetch); 130 Assert.assertEquals(BUGREPORT_CONTENT, FileUtil.readStringFromFile(mainFile)); 131 } finally { 132 FileUtil.deleteFile(mainFile); 133 mBugreport.close(); 134 } 135 } 136 137 /** 138 * Test when created with a null file for zipped and unzipped. 139 */ 140 @Test 141 public void testBugreport_nullFile() { 142 mBugreport = new Bugreport(null, true); 143 Assert.assertNull(mBugreport.getMainFile()); 144 Assert.assertNull(mBugreport.getListOfFiles()); 145 Assert.assertNull(mBugreport.getFileByName("")); 146 147 mBugreport = new Bugreport(null, false); 148 Assert.assertNull(mBugreport.getMainFile()); 149 Assert.assertNull(mBugreport.getListOfFiles()); 150 Assert.assertNull(mBugreport.getFileByName("")); 151 } 152 153 /** 154 * Test when the zipped bugreport does not contain a main_entry.txt 155 */ 156 @Test 157 public void testBugreportz_noMainFile() throws Exception { 158 File tempDir = null; 159 File zipFile = null; 160 try { 161 tempDir = FileUtil.createTempDir("bugreportz"); 162 File bugreport = FileUtil.createTempFile(BUGREPORT_PREFIX, ".txt", tempDir); 163 InputStream content = new ByteArrayInputStream(BUGREPORT_CONTENT.getBytes()); 164 FileUtil.writeToFile(content, bugreport); 165 // We create a fake bugreport zip no main_entry.txt and the bugreport. 166 zipFile = ZipUtil.createZip(new File("")); 167 FileOutputStream fileStream = new FileOutputStream(zipFile); 168 ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(fileStream)); 169 ZipUtil.addToZip(out, bugreport, new LinkedList<String>()); 170 StreamUtil.close(out); 171 172 mBugreport = new Bugreport(zipFile, true); 173 Assert.assertNull(mBugreport.getMainFile()); 174 } finally { 175 FileUtil.recursiveDelete(tempDir); 176 FileUtil.deleteFile(zipFile); 177 mBugreport.close(); 178 } 179 } 180 181 /** 182 * Test that logging a zip bugreport use the proper type. 183 */ 184 @Test 185 public void testLogBugreport() throws Exception { 186 final String dataName = "TEST"; 187 try { 188 mBugreport = new Bugreport(mZipFile, true); 189 ITestLogger logger = EasyMock.createMock(ITestLogger.class); 190 logger.testLog(EasyMock.eq(dataName), EasyMock.eq(LogDataType.BUGREPORTZ), 191 EasyMock.anyObject()); 192 EasyMock.replay(logger); 193 mBugreport.log(dataName, logger); 194 EasyMock.verify(logger); 195 } finally { 196 mBugreport.close(); 197 } 198 } 199 200 /** 201 * Test that logging a flat bugreport use the proper type. 202 */ 203 @Test 204 public void testLogBugreportFlat() throws Exception { 205 final String dataName = "TEST"; 206 try { 207 mBugreport = new Bugreport(mRegularFile, false); 208 ITestLogger logger = EasyMock.createMock(ITestLogger.class); 209 logger.testLog(EasyMock.eq(dataName), EasyMock.eq(LogDataType.BUGREPORT), 210 EasyMock.anyObject()); 211 EasyMock.replay(logger); 212 mBugreport.log(dataName, logger); 213 EasyMock.verify(logger); 214 } finally { 215 mBugreport.close(); 216 } 217 } 218 } 219