1 /* 2 * Copyright (C) 2011 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 17 package com.android.tradefed.targetprep; 18 19 import com.android.ddmlib.FileListingService; 20 import com.android.tradefed.build.DeviceBuildInfo; 21 import com.android.tradefed.build.IDeviceBuildInfo; 22 import com.android.tradefed.device.ITestDevice; 23 import com.android.tradefed.device.ITestDevice.RecoveryMode; 24 import com.android.tradefed.device.MockFileUtil; 25 import com.android.tradefed.util.IRunUtil; 26 27 import junit.framework.TestCase; 28 29 import org.easymock.EasyMock; 30 31 import java.io.File; 32 import java.util.Arrays; 33 import java.util.ArrayList; 34 import java.util.Collection; 35 import java.util.HashSet; 36 import java.util.Set; 37 38 public class DefaultTestsZipInstallerTest extends TestCase { 39 private static final String SKIP_THIS = "skipThis"; 40 41 private static final String TEST_STRING = "foo"; 42 43 private static final File SOME_PATH_1 = new File("/some/path/1"); 44 45 private static final File SOME_PATH_2 = new File("/some/path/2"); 46 47 private ITestDevice mMockDevice; 48 private IDeviceBuildInfo mDeviceBuild; 49 private DefaultTestsZipInstaller mZipInstaller; 50 51 @Override 52 protected void setUp() throws Exception { 53 super.setUp(); 54 ArrayList<String> skipThis = new ArrayList<String>(Arrays.asList(SKIP_THIS)); 55 mZipInstaller = new DefaultTestsZipInstaller(skipThis) { 56 @Override 57 File[] getTestsZipDataFiles(File hostDir, ITestDevice device) { 58 return new File[] { new File(TEST_STRING) }; 59 } 60 61 @Override 62 Set<File> findDirs(File hostDir, File deviceRootPath) { 63 Set<File> files = new HashSet<File>(2); 64 files.add(SOME_PATH_1); 65 files.add(SOME_PATH_2); 66 return files; 67 } 68 69 @Override 70 IRunUtil getRunUtil() { 71 return EasyMock.createNiceMock(IRunUtil.class); 72 } 73 }; 74 75 mMockDevice = EasyMock.createMock(ITestDevice.class); 76 EasyMock.expect(mMockDevice.getSerialNumber()).andStubReturn(TEST_STRING); 77 EasyMock.expect(mMockDevice.getProductType()).andStubReturn(TEST_STRING); 78 EasyMock.expect(mMockDevice.getBuildId()).andStubReturn("1"); 79 80 EasyMock.expect(mMockDevice.getDeviceDescriptor()).andStubReturn(null); 81 mDeviceBuild = new DeviceBuildInfo("1", TEST_STRING); 82 } 83 84 public void testSkipWipeFileSetup() throws Exception { 85 DefaultTestsZipInstaller testZipInstaller = new DefaultTestsZipInstaller(); 86 Set<String> skipList = testZipInstaller.getDataWipeSkipList(); 87 assertNull("Invalid wipe list set.", skipList); 88 testZipInstaller.setDataWipeSkipList("foo"); 89 skipList = testZipInstaller.getDataWipeSkipList(); 90 assertTrue("Invalid wipe list set. Missing value set.", skipList.contains("foo")); 91 Collection<String> skipArrayList = new ArrayList<String>(); 92 skipArrayList.add("bar"); 93 skipArrayList.add("foobar"); 94 testZipInstaller.setDataWipeSkipList(skipArrayList); 95 skipList = testZipInstaller.getDataWipeSkipList(); 96 assertFalse("Invalid wipe list set, should not contain old value.", 97 skipList.contains("foo")); 98 assertTrue("Invalid wipe list set. Missing value set.", skipList.contains("bar")); 99 assertTrue("Invalid wipe list set. Missing value set.", skipList.contains("foobar")); 100 } 101 102 public void testCantTouchFilesystem() throws Exception { 103 // expect initial android stop 104 EasyMock.expect(mMockDevice.getSerialNumber()).andStubReturn("serial_number_stub"); 105 EasyMock.expect(mMockDevice.getRecoveryMode()).andReturn(RecoveryMode.AVAILABLE); 106 EasyMock.expect(mMockDevice.executeShellCommand("stop")).andReturn(""); 107 EasyMock.expect(mMockDevice.executeShellCommand("stop installd")).andReturn(""); 108 mMockDevice.setRecoveryMode(RecoveryMode.ONLINE); 109 110 // turtle! (return false, for "write failed") 111 EasyMock.expect(mMockDevice.pushString((String) EasyMock.anyObject(), 112 (String) EasyMock.anyObject())).andReturn(false); 113 114 EasyMock.replay(mMockDevice); 115 try { 116 mZipInstaller.deleteData(mMockDevice); 117 fail("Didn't throw TargetSetupError on failed write test"); 118 } catch (TargetSetupError e) { 119 // expected 120 } 121 EasyMock.verify(mMockDevice); 122 } 123 124 /** 125 * Exercise the core logic on a successful scenario. 126 */ 127 public void testPushTestsZipOntoData() throws Exception { 128 // mock a filesystem with these contents: 129 // /data/app 130 // /data/$SKIP_THIS 131 MockFileUtil.setMockDirContents( 132 mMockDevice, FileListingService.DIRECTORY_DATA, "app", SKIP_THIS); 133 134 // expect initial android stop 135 EasyMock.expect(mMockDevice.getSerialNumber()).andStubReturn("serial_number_stub"); 136 EasyMock.expect(mMockDevice.getRecoveryMode()).andReturn(RecoveryMode.AVAILABLE); 137 mMockDevice.setRecoveryMode(RecoveryMode.ONLINE); 138 EasyMock.expect(mMockDevice.executeShellCommand("stop")).andReturn(""); 139 EasyMock.expect(mMockDevice.executeShellCommand("stop installd")).andReturn(""); 140 141 // turtle! (to make sure filesystem is writable) 142 EasyMock.expect(mMockDevice.pushString((String) EasyMock.anyObject(), 143 (String) EasyMock.anyObject())).andReturn(true); 144 145 // expect 'rm app' but not 'rm $SKIP_THIS' 146 EasyMock.expect(mMockDevice.doesFileExist("data/app")).andReturn(false); 147 EasyMock.expect(mMockDevice.executeShellCommand(EasyMock.contains("rm -r data/app"))) 148 .andReturn(""); 149 150 mMockDevice.setRecoveryMode(RecoveryMode.AVAILABLE); 151 152 EasyMock.expect(mMockDevice.syncFiles((File) EasyMock.anyObject(), 153 EasyMock.contains(FileListingService.DIRECTORY_DATA))) 154 .andReturn(Boolean.TRUE); 155 156 EasyMock.expect( 157 mMockDevice.executeShellCommand(EasyMock.startsWith("chown system.system " 158 + SOME_PATH_1.getPath()))).andReturn(""); 159 EasyMock.expect( 160 mMockDevice.executeShellCommand(EasyMock.startsWith("chown system.system " 161 + SOME_PATH_2.getPath()))).andReturn(""); 162 163 EasyMock.replay(mMockDevice); 164 mZipInstaller.pushTestsZipOntoData(mMockDevice, mDeviceBuild); 165 EasyMock.verify(mMockDevice); 166 } 167 168 /** 169 * Test repeats to delete a dir are aborted 170 */ 171 public void testPushTestsZipOntoData_retry() throws Exception { 172 // mock a filesystem with these contents: 173 // /data/app 174 // /data/$SKIP_THIS 175 MockFileUtil.setMockDirContents( 176 mMockDevice, FileListingService.DIRECTORY_DATA, "app", SKIP_THIS); 177 178 // expect initial android stop 179 EasyMock.expect(mMockDevice.getSerialNumber()).andStubReturn("serial_number_stub"); 180 EasyMock.expect(mMockDevice.getRecoveryMode()).andReturn(RecoveryMode.AVAILABLE); 181 mMockDevice.setRecoveryMode(RecoveryMode.ONLINE); 182 EasyMock.expect(mMockDevice.executeShellCommand("stop")).andReturn(""); 183 EasyMock.expect(mMockDevice.executeShellCommand("stop installd")).andReturn(""); 184 185 // turtle! (to make sure filesystem is writable) 186 EasyMock.expect(mMockDevice.pushString((String) EasyMock.anyObject(), 187 (String) EasyMock.anyObject())).andReturn(true); 188 189 // expect 'rm app' but not 'rm $SKIP_THIS' 190 EasyMock.expect(mMockDevice.doesFileExist("data/app")).andStubReturn(true); 191 EasyMock.expect(mMockDevice.executeShellCommand(EasyMock.contains("rm -r data/app"))) 192 .andStubReturn("oh noes, rm failed"); 193 194 195 EasyMock.replay(mMockDevice); 196 try { 197 mZipInstaller.pushTestsZipOntoData(mMockDevice, mDeviceBuild); 198 fail("TargetSetupError not thrown"); 199 } catch (TargetSetupError e) { 200 // expected 201 } 202 EasyMock.verify(mMockDevice); 203 } 204 } 205