1 /* 2 * Copyright (C) 2010 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.tradefed.build.DeviceBuildInfo; 20 import com.android.tradefed.build.IDeviceBuildInfo; 21 import com.android.tradefed.command.remote.DeviceDescriptor; 22 import com.android.tradefed.device.DeviceNotAvailableException; 23 import com.android.tradefed.device.ITestDevice; 24 import com.android.tradefed.device.TestDeviceState; 25 import com.android.tradefed.targetprep.IDeviceFlasher.UserDataFlashOption; 26 import com.android.tradefed.util.ArrayUtil; 27 import com.android.tradefed.util.CommandResult; 28 import com.android.tradefed.util.CommandStatus; 29 import com.android.tradefed.util.FileUtil; 30 import com.android.tradefed.util.IRunUtil; 31 32 import junit.framework.TestCase; 33 34 import org.easymock.EasyMock; 35 36 import java.io.BufferedReader; 37 import java.io.File; 38 import java.io.IOException; 39 import java.io.StringReader; 40 import java.util.ArrayList; 41 42 /** 43 * Unit tests for {@link FastbootDeviceFlasher}. 44 */ 45 public class FastbootDeviceFlasherTest extends TestCase { 46 47 /** a temp 'don't care value' string to use */ 48 private static final String TEST_STRING = "foo"; 49 private FastbootDeviceFlasher mFlasher; 50 private ITestDevice mMockDevice; 51 private IDeviceBuildInfo mMockBuildInfo; 52 private IFlashingResourcesRetriever mMockRetriever; 53 private IFlashingResourcesParser mMockParser; 54 private IRunUtil mMockRunUtil; 55 56 /** 57 * {@inheritDoc} 58 */ 59 @Override 60 protected void setUp() throws Exception { 61 super.setUp(); 62 mMockDevice = EasyMock.createMock(ITestDevice.class); 63 EasyMock.expect(mMockDevice.getSerialNumber()).andStubReturn(TEST_STRING); 64 EasyMock.expect(mMockDevice.getProductType()).andStubReturn(TEST_STRING); 65 EasyMock.expect(mMockDevice.getBuildId()).andStubReturn("1"); 66 EasyMock.expect(mMockDevice.getBuildFlavor()).andStubReturn("test-debug"); 67 EasyMock.expect(mMockDevice.getDeviceDescriptor()).andStubReturn(null); 68 mMockBuildInfo = new DeviceBuildInfo("0", TEST_STRING); 69 mMockBuildInfo.setDeviceImageFile(new File(TEST_STRING), "0"); 70 mMockBuildInfo.setUserDataImageFile(new File(TEST_STRING), "0"); 71 mMockRetriever = EasyMock.createNiceMock(IFlashingResourcesRetriever.class); 72 mMockParser = EasyMock.createNiceMock(IFlashingResourcesParser.class); 73 mMockRunUtil = EasyMock.createMock(IRunUtil.class); 74 75 mFlasher = new FastbootDeviceFlasher() { 76 @Override 77 protected IFlashingResourcesParser createFlashingResourcesParser( 78 IDeviceBuildInfo localBuild, DeviceDescriptor descriptor) { 79 return mMockParser; 80 } 81 @Override 82 protected IRunUtil getRunUtil() { 83 return mMockRunUtil; 84 } 85 }; 86 mFlasher.setFlashingResourcesRetriever(mMockRetriever); 87 mFlasher.setUserDataFlashOption(UserDataFlashOption.RETAIN); 88 } 89 90 /** 91 * Test {@link FastbootDeviceFlasher#flash(ITestDevice, IDeviceBuildInfo)} 92 * when device is not available. 93 */ 94 public void testFlash_deviceNotAvailable() throws DeviceNotAvailableException { 95 try { 96 mMockDevice.rebootIntoBootloader(); 97 // TODO: this is fixed to two arguments - how to set to expect a variable arg amount ? 98 mMockDevice.executeFastbootCommand((String)EasyMock.anyObject(), 99 (String)EasyMock.anyObject()); 100 EasyMock.expectLastCall().andThrow(new DeviceNotAvailableException()); 101 EasyMock.replay(mMockDevice); 102 mFlasher.flash(mMockDevice, mMockBuildInfo); 103 fail("TargetSetupError not thrown"); 104 } catch (TargetSetupError e) { 105 // expected 106 } 107 } 108 109 /** 110 * Test DeviceFlasher#flash(ITestDevice, IDeviceBuildInfo)} when required board info is not 111 * present. 112 */ 113 public void testFlash_missingBoard() throws DeviceNotAvailableException { 114 mMockDevice.rebootIntoBootloader(); 115 EasyMock.expect(mMockParser.getRequiredBoards()).andReturn(null); 116 EasyMock.replay(mMockDevice); 117 try { 118 mFlasher.flash(mMockDevice, mMockBuildInfo); 119 fail("TargetSetupError not thrown"); 120 } catch (TargetSetupError e) { 121 // expected 122 } 123 } 124 125 /** 126 * Test {@link FastbootDeviceFlasher#getImageVersion(ITestDevice, String)} 127 */ 128 public void testGetImageVersion() throws DeviceNotAvailableException, TargetSetupError { 129 CommandResult fastbootResult = new CommandResult(); 130 fastbootResult.setStatus(CommandStatus.SUCCESS); 131 // output of getvar is on stderr for some unknown reason 132 fastbootResult.setStderr("version-bootloader: 1.0.1\nfinished. total time: 0.001s"); 133 fastbootResult.setStdout(""); 134 EasyMock.expect(mMockDevice.executeFastbootCommand("getvar", "version-bootloader")). 135 andReturn(fastbootResult); 136 EasyMock.replay(mMockDevice, mMockRunUtil); 137 String actualVersion = mFlasher.getImageVersion(mMockDevice, "bootloader"); 138 assertEquals("1.0.1", actualVersion); 139 EasyMock.verify(mMockDevice, mMockRunUtil); 140 } 141 142 /** 143 * Test {@link FastbootDeviceFlasher#getCurrentSlot(ITestDevice)} when device is in fastboot. 144 */ 145 public void testGetCurrentSlot_fastboot() throws DeviceNotAvailableException, TargetSetupError { 146 CommandResult fastbootResult = new CommandResult(); 147 fastbootResult.setStatus(CommandStatus.SUCCESS); 148 fastbootResult.setStderr("current-slot: _a\nfinished. total time 0.001s"); 149 fastbootResult.setStdout(""); 150 EasyMock.expect(mMockDevice.getDeviceState()).andReturn(TestDeviceState.FASTBOOT); 151 EasyMock.expect(mMockDevice.executeFastbootCommand("getvar", "current-slot")) 152 .andReturn(fastbootResult); 153 EasyMock.replay(mMockDevice, mMockRunUtil); 154 String currentSlot = mFlasher.getCurrentSlot(mMockDevice); 155 assertEquals("a", currentSlot); 156 EasyMock.verify(mMockDevice, mMockRunUtil); 157 } 158 159 /** Test {@link FastbootDeviceFlasher#getCurrentSlot(ITestDevice)} when device is in adb. */ 160 public void testGetCurrentSlot_adb() throws DeviceNotAvailableException, TargetSetupError { 161 String adbResult = "[ro.boot.slot_suffix]: [_b]\n"; 162 EasyMock.expect(mMockDevice.getDeviceState()).andReturn(TestDeviceState.ONLINE); 163 EasyMock.expect(mMockDevice.executeShellCommand("getprop ro.boot.slot_suffix")) 164 .andReturn(adbResult); 165 EasyMock.replay(mMockDevice, mMockRunUtil); 166 String currentSlot = mFlasher.getCurrentSlot(mMockDevice); 167 assertEquals("b", currentSlot); 168 EasyMock.verify(mMockDevice, mMockRunUtil); 169 } 170 171 /** 172 * Test {@link FastbootDeviceFlasher#getCurrentSlot(ITestDevice)} when device does not support 173 * A/B. 174 */ 175 public void testGetCurrentSlot_null() throws DeviceNotAvailableException, TargetSetupError { 176 String adbResult = "\n"; 177 EasyMock.expect(mMockDevice.getDeviceState()).andReturn(TestDeviceState.ONLINE); 178 EasyMock.expect(mMockDevice.executeShellCommand("getprop ro.boot.slot_suffix")) 179 .andReturn(adbResult); 180 EasyMock.replay(mMockDevice, mMockRunUtil); 181 String currentSlot = mFlasher.getCurrentSlot(mMockDevice); 182 assertNull(currentSlot); 183 EasyMock.verify(mMockDevice, mMockRunUtil); 184 } 185 186 /** Test that a fastboot command is retried if it does not output anything. */ 187 public void testRetryGetVersionCommand() throws DeviceNotAvailableException, TargetSetupError { 188 // The first time command is tried, make it return an empty string. 189 CommandResult fastbootInValidResult = new CommandResult(); 190 fastbootInValidResult.setStatus(CommandStatus.SUCCESS); 191 // output of getvar is on stderr for some unknown reason 192 fastbootInValidResult.setStderr(""); 193 fastbootInValidResult.setStdout(""); 194 195 // Return the correct value on second attempt. 196 CommandResult fastbootValidResult = new CommandResult(); 197 fastbootValidResult.setStatus(CommandStatus.SUCCESS); 198 fastbootValidResult.setStderr("version-baseband: 1.0.1\nfinished. total time: 0.001s"); 199 fastbootValidResult.setStdout(""); 200 201 EasyMock.expect(mMockDevice.executeFastbootCommand("getvar", "version-baseband")). 202 andReturn(fastbootInValidResult); 203 EasyMock.expect(mMockDevice.executeFastbootCommand("getvar", "version-baseband")). 204 andReturn(fastbootValidResult); 205 mMockRunUtil.sleep(EasyMock.anyLong()); 206 EasyMock.expectLastCall(); 207 208 EasyMock.replay(mMockDevice, mMockRunUtil); 209 String actualVersion = mFlasher.getImageVersion(mMockDevice, "baseband"); 210 EasyMock.verify(mMockDevice, mMockRunUtil); 211 assertEquals("1.0.1", actualVersion); 212 } 213 214 /** 215 * Test that baseband can be flashed when current baseband version is empty 216 */ 217 public void testFlashBaseband_noVersion() 218 throws DeviceNotAvailableException, TargetSetupError { 219 final String newBasebandVersion = "1.0.1"; 220 ITestDevice mockDevice = EasyMock.createMock(ITestDevice.class); 221 // expect a fastboot getvar version-baseband command 222 setFastbootResponseExpectations(mockDevice, "version-baseband: \n"); 223 setFastbootResponseExpectations(mockDevice, "version-baseband: \n"); 224 // expect a 'flash radio' command 225 setFastbootFlashExpectations(mockDevice, "radio"); 226 mockDevice.rebootIntoBootloader(); 227 EasyMock.replay(mockDevice, mMockRunUtil); 228 229 FastbootDeviceFlasher flasher = getFlasherWithParserData( 230 String.format("require version-baseband=%s", newBasebandVersion)); 231 232 IDeviceBuildInfo build = new DeviceBuildInfo("1234", "build-name"); 233 build.setBasebandImage(new File("tmp"), newBasebandVersion); 234 flasher.checkAndFlashBaseband(mockDevice, build); 235 EasyMock.verify(mockDevice, mMockRunUtil); 236 } 237 238 /** 239 * Test flashing of user data with a tests zip 240 * 241 * @throws TargetSetupError 242 */ 243 public void testFlashUserData_testsZip() throws DeviceNotAvailableException, TargetSetupError { 244 mFlasher.setUserDataFlashOption(UserDataFlashOption.TESTS_ZIP); 245 246 ITestsZipInstaller mockZipInstaller = EasyMock.createMock(ITestsZipInstaller.class); 247 mFlasher.setTestsZipInstaller(mockZipInstaller); 248 // expect 249 mockZipInstaller.pushTestsZipOntoData(EasyMock.eq(mMockDevice), EasyMock.eq(mMockBuildInfo)); 250 // expect 251 mMockDevice.rebootUntilOnline(); 252 mMockDevice.rebootIntoBootloader(); 253 EasyMock.expect(mMockDevice.isEncryptionSupported()).andReturn(Boolean.FALSE); 254 255 EasyMock.replay(mMockDevice, mockZipInstaller); 256 mFlasher.flashUserData(mMockDevice, mMockBuildInfo); 257 EasyMock.verify(mMockDevice, mockZipInstaller); 258 } 259 260 /** 261 * Verify that correct fastboot command is called with WIPE data option 262 * @throws DeviceNotAvailableException 263 * @throws TargetSetupError 264 */ 265 public void testFlashUserData_wipe() throws DeviceNotAvailableException, TargetSetupError { 266 mFlasher.setUserDataFlashOption(UserDataFlashOption.WIPE); 267 doTestFlashWithWipe(); 268 } 269 270 /** 271 * Verify that correct fastboot command is called with FORCE_WIPE data option 272 * @throws DeviceNotAvailableException 273 * @throws TargetSetupError 274 */ 275 public void testFlashUserData_forceWipe() throws DeviceNotAvailableException, TargetSetupError { 276 mFlasher.setUserDataFlashOption(UserDataFlashOption.FORCE_WIPE); 277 doTestFlashWithWipe(); 278 } 279 280 /** 281 * Verify call sequence when wiping cache on devices with cache partition 282 * @throws DeviceNotAvailableException 283 * @throws TargetSetupError 284 */ 285 public void testWipeCache_exists() throws DeviceNotAvailableException, TargetSetupError { 286 mFlasher.setUserDataFlashOption(UserDataFlashOption.WIPE); 287 CommandResult fastbootOutput = new CommandResult(); 288 fastbootOutput.setStatus(CommandStatus.SUCCESS); 289 fastbootOutput.setStderr("(bootloader) slot-count: not found\n" + 290 "(bootloader) slot-suffixes: not found\n" + 291 "(bootloader) slot-suffixes: not found\n" + 292 "partition-type:cache: ext4\n" + 293 "finished. total time: 0.002s\n"); 294 fastbootOutput.setStdout(""); 295 296 EasyMock.expect(mMockDevice.executeFastbootCommand("getvar", "partition-type:cache")). 297 andReturn(fastbootOutput); 298 EasyMock.expect(mMockDevice.getUseFastbootErase()).andReturn(false); 299 fastbootOutput = new CommandResult(); 300 fastbootOutput.setStatus(CommandStatus.SUCCESS); 301 fastbootOutput.setStderr("Creating filesystem with parameters:\n" + 302 " Size: 104857600\n" + 303 " Block size: 4096\n" + 304 " Blocks per group: 32768\n" + 305 " Inodes per group: 6400\n" + 306 " Inode size: 256\n" + 307 " Journal blocks: 1024\n" + 308 " Label: \n" + 309 " Blocks: 25600\n" + 310 " Block groups: 1\n" + 311 " Reserved block group size: 7\n" + 312 "Created filesystem with 11/6400 inodes and 1438/25600 blocks\n" + 313 "target reported max download size of 494927872 bytes\n" + 314 "erasing 'cache'...\n" + 315 "OKAY [ 0.024s]\n" + 316 "sending 'cache' (5752 KB)...\n" + 317 "OKAY [ 0.178s]\n" + 318 "writing 'cache'...\n" + 319 "OKAY [ 0.107s]\n" + 320 "finished. total time: 0.309s\n"); 321 EasyMock.expect(mMockDevice.fastbootWipePartition("cache")).andReturn(fastbootOutput); 322 EasyMock.replay(mMockDevice); 323 mFlasher.wipeCache(mMockDevice); 324 EasyMock.verify(mMockDevice); 325 } 326 327 /** 328 * Verify call sequence when wiping cache on devices without cache partition 329 * @throws DeviceNotAvailableException 330 * @throws TargetSetupError 331 */ 332 public void testWipeCache_not_exists() throws DeviceNotAvailableException, TargetSetupError { 333 mFlasher.setUserDataFlashOption(UserDataFlashOption.WIPE); 334 CommandResult fastbootOutput = new CommandResult(); 335 fastbootOutput.setStatus(CommandStatus.SUCCESS); 336 fastbootOutput.setStderr("(bootloader) slot-count: not found\n" + 337 "(bootloader) slot-suffixes: not found\n" + 338 "(bootloader) slot-suffixes: not found\n" + 339 "partition-type:cache: \n" + 340 "finished. total time: 0.002s\n"); 341 fastbootOutput.setStdout(""); 342 343 EasyMock.expect(mMockDevice.executeFastbootCommand("getvar", "partition-type:cache")). 344 andReturn(fastbootOutput); 345 EasyMock.replay(mMockDevice); 346 mFlasher.wipeCache(mMockDevice); 347 EasyMock.verify(mMockDevice); 348 } 349 350 /** 351 * Verify call sequence when wiping cache on devices without cache partition 352 * @throws DeviceNotAvailableException 353 * @throws TargetSetupError 354 */ 355 public void testWipeCache_not_exists_error() 356 throws DeviceNotAvailableException, TargetSetupError { 357 mFlasher.setUserDataFlashOption(UserDataFlashOption.WIPE); 358 CommandResult fastbootOutput = new CommandResult(); 359 fastbootOutput.setStatus(CommandStatus.SUCCESS); 360 fastbootOutput.setStderr("getvar:partition-type:cache FAILED (remote: unknown command)\n" + 361 "finished. total time: 0.051s\n"); 362 fastbootOutput.setStdout(""); 363 364 EasyMock.expect(mMockDevice.executeFastbootCommand("getvar", "partition-type:cache")). 365 andReturn(fastbootOutput); 366 EasyMock.replay(mMockDevice); 367 mFlasher.wipeCache(mMockDevice); 368 EasyMock.verify(mMockDevice); 369 } 370 371 /** 372 * Convenience function to set expectations for `fastboot -w` and execute test 373 * @throws DeviceNotAvailableException 374 * @throws TargetSetupError 375 */ 376 private void doTestFlashWithWipe() throws DeviceNotAvailableException, TargetSetupError { 377 CommandResult result = new CommandResult(); 378 result.setStatus(CommandStatus.SUCCESS); 379 result.setStderr(""); 380 result.setStdout(""); 381 EasyMock.expect(mMockDevice.executeFastbootCommand(EasyMock.anyLong(), 382 EasyMock.eq("-w"))).andReturn(result); 383 384 EasyMock.replay(mMockDevice); 385 mFlasher.handleUserDataFlashing(mMockDevice, mMockBuildInfo); 386 EasyMock.verify(mMockDevice); 387 } 388 389 /** 390 * Test doing a user data with with rm 391 * 392 * @throws TargetSetupError 393 */ 394 public void testFlashUserData_wipeRm() throws DeviceNotAvailableException, TargetSetupError { 395 mFlasher.setUserDataFlashOption(UserDataFlashOption.WIPE_RM); 396 397 ITestsZipInstaller mockZipInstaller = EasyMock.createMock(ITestsZipInstaller.class); 398 mFlasher.setTestsZipInstaller(mockZipInstaller); 399 // expect 400 mockZipInstaller.deleteData(EasyMock.eq(mMockDevice)); 401 // expect 402 mMockDevice.rebootUntilOnline(); 403 mMockDevice.rebootIntoBootloader(); 404 405 EasyMock.replay(mMockDevice, mockZipInstaller); 406 mFlasher.flashUserData(mMockDevice, mMockBuildInfo); 407 EasyMock.verify(mMockDevice, mockZipInstaller); 408 } 409 410 /** 411 * Test that 412 * {@link FastbootDeviceFlasher#downloadFlashingResources(ITestDevice, IDeviceBuildInfo)} 413 * throws an exception when device product is null. 414 */ 415 public void testDownloadFlashingResources_nullDeviceProduct() throws Exception { 416 mMockDevice = EasyMock.createMock(ITestDevice.class); 417 EasyMock.expect(mMockDevice.getDeviceDescriptor()).andStubReturn(null); 418 EasyMock.expect(mMockDevice.getProductType()).andReturn(null); 419 EasyMock.expect(mMockDevice.getSerialNumber()).andStubReturn(TEST_STRING); 420 EasyMock.expect(mMockParser.getRequiredBoards()).andReturn(new ArrayList<String>()); 421 EasyMock.replay(mMockDevice, mMockParser); 422 try { 423 mFlasher.downloadFlashingResources(mMockDevice, mMockBuildInfo); 424 fail("Should have thrown an exception"); 425 } catch (DeviceNotAvailableException expected) { 426 assertEquals(String.format("Could not determine product type for device %s", 427 TEST_STRING), expected.getMessage()); 428 } finally { 429 EasyMock.verify(mMockDevice, mMockParser); 430 } 431 } 432 433 /** 434 * Test that 435 * {@link FastbootDeviceFlasher#downloadFlashingResources(ITestDevice, IDeviceBuildInfo)} 436 * throws an exception when the device product is not found in required board. 437 */ 438 public void testDownloadFlashingResources_NotFindBoard() throws Exception { 439 final String boardName = "AWESOME_PRODUCT"; 440 mMockDevice = EasyMock.createMock(ITestDevice.class); 441 EasyMock.expect(mMockDevice.getDeviceDescriptor()).andStubReturn(null); 442 EasyMock.expect(mMockDevice.getProductType()).andReturn("NOT_FOUND"); 443 EasyMock.expect(mMockDevice.getSerialNumber()).andStubReturn(TEST_STRING); 444 EasyMock.expect(mMockParser.getRequiredBoards()).andStubReturn(ArrayUtil.list(boardName)); 445 EasyMock.replay(mMockDevice, mMockParser); 446 try { 447 mFlasher.downloadFlashingResources(mMockDevice, mMockBuildInfo); 448 fail("Should have thrown an exception"); 449 } catch (TargetSetupError expected) { 450 assertEquals(String.format("Device %s is NOT_FOUND. Expected %s null", 451 TEST_STRING, ArrayUtil.list(boardName)), expected.getMessage()); 452 } finally { 453 EasyMock.verify(mMockDevice, mMockParser); 454 } 455 } 456 457 /** 458 * Test that 459 * {@link FastbootDeviceFlasher#downloadFlashingResources(ITestDevice, IDeviceBuildInfo)} 460 * proceeds to the end without throwing. 461 */ 462 public void testDownloadFlashingResources() throws Exception { 463 final String boardName = "AWESOME_PRODUCT"; 464 mMockDevice = EasyMock.createMock(ITestDevice.class); 465 EasyMock.expect(mMockDevice.getDeviceDescriptor()).andStubReturn(null); 466 EasyMock.expect(mMockDevice.getProductType()).andReturn(boardName); 467 EasyMock.expect(mMockDevice.getSerialNumber()).andStubReturn(TEST_STRING); 468 EasyMock.expect(mMockParser.getRequiredBoards()).andStubReturn(ArrayUtil.list(boardName)); 469 EasyMock.replay(mMockDevice, mMockParser); 470 mFlasher.downloadFlashingResources(mMockDevice, mMockBuildInfo); 471 EasyMock.verify(mMockDevice, mMockParser); 472 } 473 474 /** 475 * Test that 476 * {@link FastbootDeviceFlasher#checkAndFlashBootloader(ITestDevice, IDeviceBuildInfo)} returns 477 * false because bootloader version is already good. 478 */ 479 public void testCheckAndFlashBootloader_SkippingFlashing() throws Exception { 480 final String version = "version 5"; 481 mFlasher = new FastbootDeviceFlasher() { 482 @Override 483 protected String getImageVersion(ITestDevice device, String imageName) 484 throws DeviceNotAvailableException, TargetSetupError { 485 return version; 486 } 487 }; 488 IDeviceBuildInfo mockBuild = EasyMock.createMock(IDeviceBuildInfo.class); 489 EasyMock.expect(mockBuild.getBootloaderVersion()).andStubReturn(version); 490 EasyMock.replay(mMockDevice, mockBuild); 491 assertFalse(mFlasher.checkAndFlashBootloader(mMockDevice, mockBuild)); 492 EasyMock.verify(mMockDevice, mockBuild); 493 } 494 495 /** 496 * Test that 497 * {@link FastbootDeviceFlasher#checkAndFlashBootloader(ITestDevice, IDeviceBuildInfo)} returns 498 * true after flashing the device bootloader. 499 */ 500 public void testCheckAndFlashBootloader() throws Exception { 501 final String version = "version 5"; 502 mFlasher = new FastbootDeviceFlasher() { 503 @Override 504 protected String getImageVersion(ITestDevice device, String imageName) 505 throws DeviceNotAvailableException, TargetSetupError { 506 return "version 6"; 507 } 508 }; 509 IDeviceBuildInfo mockBuild = EasyMock.createMock(IDeviceBuildInfo.class); 510 EasyMock.expect(mockBuild.getBootloaderVersion()).andStubReturn(version); 511 File bootloaderFake = FileUtil.createTempFile("fakeBootloader", ""); 512 try { 513 EasyMock.expect(mockBuild.getBootloaderImageFile()).andReturn(bootloaderFake); 514 CommandResult res = new CommandResult(CommandStatus.SUCCESS); 515 res.setStderr("flashing"); 516 EasyMock.expect(mMockDevice.executeFastbootCommand(EasyMock.eq("flash"), 517 EasyMock.eq("hboot"), EasyMock.eq(bootloaderFake.getAbsolutePath()))) 518 .andReturn(res); 519 mMockDevice.rebootIntoBootloader(); 520 EasyMock.expectLastCall(); 521 EasyMock.replay(mMockDevice, mockBuild); 522 assertTrue(mFlasher.checkAndFlashBootloader(mMockDevice, mockBuild)); 523 EasyMock.verify(mMockDevice, mockBuild); 524 } finally { 525 FileUtil.deleteFile(bootloaderFake); 526 } 527 } 528 529 /** 530 * Test {@link FastbootDeviceFlasher#checkAndFlashSystem(ITestDevice, 531 * String, String, IDeviceBuildInfo)} when there is no need to flash the system. 532 */ 533 public void testCheckAndFlashSystem_noFlashing() throws Exception { 534 final String buildId = "systemBuildId"; 535 final String buildFlavor = "systemBuildFlavor"; 536 IDeviceBuildInfo mockBuild = EasyMock.createMock(IDeviceBuildInfo.class); 537 EasyMock.expect(mockBuild.getDeviceBuildId()).andReturn(buildId); 538 EasyMock.expect(mockBuild.getBuildFlavor()).andReturn(buildFlavor); 539 mMockDevice.rebootUntilOnline(); 540 EasyMock.expectLastCall(); 541 EasyMock.replay(mMockDevice, mockBuild); 542 assertFalse(mFlasher.checkAndFlashSystem(mMockDevice, buildId, buildFlavor, mockBuild)); 543 EasyMock.verify(mMockDevice, mockBuild); 544 } 545 546 /** 547 * Test {@link FastbootDeviceFlasher#checkAndFlashSystem(ITestDevice, 548 * String, String, IDeviceBuildInfo)} when it needs to be flashed. 549 */ 550 public void testCheckAndFlashSystem_flashing() throws Exception { 551 final String buildId = "systemBuildId"; 552 IDeviceBuildInfo mockBuild = EasyMock.createMock(IDeviceBuildInfo.class); 553 EasyMock.expect(mockBuild.getDeviceBuildId()).andReturn(buildId); 554 File deviceImage = FileUtil.createTempFile("fakeDeviceImage", ""); 555 try { 556 EasyMock.expect(mockBuild.getDeviceImageFile()).andStubReturn(deviceImage); 557 CommandResult res = new CommandResult(CommandStatus.SUCCESS); 558 res.setStderr("flashing"); 559 EasyMock.expect(mMockDevice.executeLongFastbootCommand(EasyMock.eq("update"), 560 EasyMock.eq(deviceImage.getAbsolutePath()))) 561 .andReturn(res); 562 EasyMock.replay(mMockDevice, mockBuild); 563 assertTrue(mFlasher.checkAndFlashSystem(mMockDevice, buildId, null, mockBuild)); 564 EasyMock.verify(mMockDevice, mockBuild); 565 } finally { 566 FileUtil.deleteFile(deviceImage); 567 } 568 } 569 570 /** 571 * Set EasyMock expectations to simulate the response to some fastboot command 572 * 573 * @param mockDevice the EasyMock mock {@link ITestDevice} to configure 574 * @param response the fastboot command response to inject 575 */ 576 private static void setFastbootResponseExpectations(ITestDevice mockDevice, String response) 577 throws DeviceNotAvailableException { 578 CommandResult result = new CommandResult(); 579 result.setStatus(CommandStatus.SUCCESS); 580 result.setStderr(response); 581 result.setStdout(""); 582 EasyMock.expect( 583 mockDevice.executeFastbootCommand((String)EasyMock.anyObject(), 584 (String)EasyMock.anyObject())).andReturn(result); 585 } 586 587 /** 588 * Set EasyMock expectations to simulate the response to a fastboot flash command 589 * 590 * @param image the expected image name to flash 591 * @param mockDevice the EasyMock mock {@link ITestDevice} to configure 592 */ 593 private static void setFastbootFlashExpectations(ITestDevice mockDevice, String image) 594 throws DeviceNotAvailableException { 595 CommandResult result = new CommandResult(); 596 result.setStatus(CommandStatus.SUCCESS); 597 result.setStderr("success"); 598 result.setStdout(""); 599 EasyMock.expect( 600 mockDevice.executeLongFastbootCommand(EasyMock.eq("flash"), EasyMock.eq(image), 601 (String)EasyMock.anyObject())).andReturn(result); 602 } 603 604 private FastbootDeviceFlasher getFlasherWithParserData(final String androidInfoData) { 605 FastbootDeviceFlasher flasher = new FastbootDeviceFlasher() { 606 @Override 607 protected IFlashingResourcesParser createFlashingResourcesParser( 608 IDeviceBuildInfo localBuild, DeviceDescriptor desc) throws TargetSetupError { 609 BufferedReader reader = new BufferedReader(new StringReader(androidInfoData)); 610 try { 611 return new FlashingResourcesParser(reader); 612 } catch (IOException e) { 613 return null; 614 } 615 } 616 617 @Override 618 protected void flashBootloader(ITestDevice device, File bootloaderImageFile) 619 throws DeviceNotAvailableException, TargetSetupError { 620 throw new DeviceNotAvailableException("error", "fakeserial"); 621 } 622 }; 623 flasher.setFlashingResourcesRetriever(EasyMock.createNiceMock( 624 IFlashingResourcesRetriever.class)); 625 return flasher; 626 } 627 } 628