1#!/usr/bin/env python3 2# 3# Copyright 2016 - The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17import logging 18import mock 19import os 20import shutil 21import tempfile 22import unittest 23 24from acts import logger 25from acts.controllers import android_device 26from acts.controllers.android_lib import errors 27 28# Mock log path for a test run. 29MOCK_LOG_PATH = "/tmp/logs/MockTest/xx-xx-xx_xx-xx-xx/" 30 31# Mock start and end time of the adb cat. 32MOCK_ADB_EPOCH_BEGIN_TIME = 191000123 33MOCK_ADB_LOGCAT_BEGIN_TIME = logger.normalize_log_line_timestamp( 34 logger.epoch_to_log_line_timestamp(MOCK_ADB_EPOCH_BEGIN_TIME)) 35MOCK_ADB_LOGCAT_END_TIME = "1970-01-02 21:22:02.000" 36 37MOCK_SERIAL = 1 38MOCK_RELEASE_BUILD_ID = "ABC1.123456.007" 39MOCK_DEV_BUILD_ID = "ABC-MR1" 40MOCK_NYC_BUILD_ID = "N4F27P" 41 42 43def get_mock_ads(num): 44 """Generates a list of mock AndroidDevice objects. 45 46 The serial number of each device will be integer 0 through num - 1. 47 48 Args: 49 num: An integer that is the number of mock AndroidDevice objects to 50 create. 51 """ 52 ads = [] 53 for i in range(num): 54 ad = mock.MagicMock(name="AndroidDevice", serial=i, h_port=None) 55 ad.ensure_screen_on = mock.MagicMock(return_value=True) 56 ads.append(ad) 57 return ads 58 59 60def mock_get_all_instances(): 61 return get_mock_ads(5) 62 63 64def mock_list_adb_devices(): 65 return [ad.serial for ad in get_mock_ads(5)] 66 67 68class MockAdbProxy(object): 69 """Mock class that swaps out calls to adb with mock calls.""" 70 71 def __init__(self, 72 serial, 73 fail_br=False, 74 fail_br_before_N=False, 75 build_id=MOCK_RELEASE_BUILD_ID, 76 return_value=None): 77 self.serial = serial 78 self.fail_br = fail_br 79 self.fail_br_before_N = fail_br_before_N 80 self.return_value = return_value 81 self.return_multiple = False 82 self.build_id = build_id 83 84 def shell(self, params, ignore_status=False, timeout=60): 85 if params == "id -u": 86 return "root" 87 elif params == "bugreportz": 88 if self.fail_br: 89 return "OMG I died!\n" 90 return "OK:/path/bugreport.zip\n" 91 elif params == "bugreportz -v": 92 if self.fail_br_before_N: 93 return "/system/bin/sh: bugreportz: not found" 94 return "1.1" 95 else: 96 if self.return_multiple: 97 return self.return_value.pop(0) 98 else: 99 return self.return_value 100 101 def getprop(self, params): 102 if params == "ro.build.id": 103 return self.build_id 104 elif params == "ro.build.version.incremental": 105 return "123456789" 106 elif params == "ro.build.type": 107 return "userdebug" 108 elif params == "ro.build.product" or params == "ro.product.name": 109 return "FakeModel" 110 elif params == "sys.boot_completed": 111 return "1" 112 113 def devices(self): 114 return "\t".join([str(self.serial), "device"]) 115 116 def bugreport(self, params, timeout=android_device.BUG_REPORT_TIMEOUT): 117 expected = os.path.join( 118 logging.log_path, "AndroidDevice%s" % self.serial, 119 "AndroidDevice%s_%s.txt" % 120 (self.serial, 121 logger.normalize_log_line_timestamp(MOCK_ADB_LOGCAT_BEGIN_TIME))) 122 assert expected in params, "Expected '%s', got '%s'." % (expected, 123 params) 124 125 def __getattr__(self, name): 126 """All calls to the none-existent functions in adb proxy would 127 simply return the adb command string. 128 """ 129 130 def adb_call(*args, **kwargs): 131 arg_str = ' '.join(str(elem) for elem in args) 132 return arg_str 133 134 return adb_call 135 136 137class MockFastbootProxy(): 138 """Mock class that swaps out calls to adb with mock calls.""" 139 140 def __init__(self, serial): 141 self.serial = serial 142 143 def devices(self): 144 return "xxxx\tdevice\nyyyy\tdevice" 145 146 def __getattr__(self, name): 147 def fastboot_call(*args): 148 arg_str = ' '.join(str(elem) for elem in args) 149 return arg_str 150 151 return fastboot_call 152 153 154class ActsAndroidDeviceTest(unittest.TestCase): 155 """This test class has unit tests for the implementation of everything 156 under acts.controllers.android_device. 157 """ 158 159 def setUp(self): 160 # Set log_path to logging since acts logger setup is not called. 161 if not hasattr(logging, "log_path"): 162 setattr(logging, "log_path", "/tmp/logs") 163 # Creates a temp dir to be used by tests in this test class. 164 self.tmp_dir = tempfile.mkdtemp() 165 166 def tearDown(self): 167 """Removes the temp dir. 168 """ 169 shutil.rmtree(self.tmp_dir) 170 171 # Tests for android_device module functions. 172 # These tests use mock AndroidDevice instances. 173 174 @mock.patch.object( 175 android_device, "get_all_instances", new=mock_get_all_instances) 176 @mock.patch.object( 177 android_device, "list_adb_devices", new=mock_list_adb_devices) 178 def test_create_with_pickup_all(self): 179 pick_all_token = android_device.ANDROID_DEVICE_PICK_ALL_TOKEN 180 actual_ads = android_device.create(pick_all_token) 181 for actual, expected in zip(actual_ads, get_mock_ads(5)): 182 self.assertEqual(actual.serial, expected.serial) 183 184 def test_create_with_empty_config(self): 185 expected_msg = android_device.ANDROID_DEVICE_EMPTY_CONFIG_MSG 186 with self.assertRaisesRegex(errors.AndroidDeviceConfigError, 187 expected_msg): 188 android_device.create([]) 189 190 def test_create_with_not_list_config(self): 191 expected_msg = android_device.ANDROID_DEVICE_NOT_LIST_CONFIG_MSG 192 with self.assertRaisesRegex(errors.AndroidDeviceConfigError, 193 expected_msg): 194 android_device.create("HAHA") 195 196 def test_get_device_success_with_serial(self): 197 ads = get_mock_ads(5) 198 expected_serial = 0 199 ad = android_device.get_device(ads, serial=expected_serial) 200 self.assertEqual(ad.serial, expected_serial) 201 202 def test_get_device_success_with_serial_and_extra_field(self): 203 ads = get_mock_ads(5) 204 expected_serial = 1 205 expected_h_port = 5555 206 ads[1].h_port = expected_h_port 207 ad = android_device.get_device( 208 ads, serial=expected_serial, h_port=expected_h_port) 209 self.assertEqual(ad.serial, expected_serial) 210 self.assertEqual(ad.h_port, expected_h_port) 211 212 def test_get_device_no_match(self): 213 ads = get_mock_ads(5) 214 expected_msg = ("Could not find a target device that matches condition" 215 ": {'serial': 5}.") 216 with self.assertRaisesRegex(ValueError, expected_msg): 217 ad = android_device.get_device(ads, serial=len(ads)) 218 219 def test_get_device_too_many_matches(self): 220 ads = get_mock_ads(5) 221 target_serial = ads[1].serial = ads[0].serial 222 expected_msg = "More than one device matched: \[0, 0\]" 223 with self.assertRaisesRegex(ValueError, expected_msg): 224 ad = android_device.get_device(ads, serial=target_serial) 225 226 def test_start_services_on_ads(self): 227 """Makes sure when an AndroidDevice fails to start some services, all 228 AndroidDevice objects get cleaned up. 229 """ 230 msg = "Some error happened." 231 ads = get_mock_ads(3) 232 ads[0].start_services = mock.MagicMock() 233 ads[0].clean_up = mock.MagicMock() 234 ads[1].start_services = mock.MagicMock() 235 ads[1].clean_up = mock.MagicMock() 236 ads[2].start_services = mock.MagicMock( 237 side_effect=errors.AndroidDeviceError(msg)) 238 ads[2].clean_up = mock.MagicMock() 239 with self.assertRaisesRegex(errors.AndroidDeviceError, msg): 240 android_device._start_services_on_ads(ads) 241 ads[0].clean_up.assert_called_once_with() 242 ads[1].clean_up.assert_called_once_with() 243 ads[2].clean_up.assert_called_once_with() 244 245 # Tests for android_device.AndroidDevice class. 246 # These tests mock out any interaction with the OS and real android device 247 # in AndroidDeivce. 248 249 @mock.patch( 250 'acts.controllers.adb.AdbProxy', 251 return_value=MockAdbProxy(MOCK_SERIAL)) 252 @mock.patch( 253 'acts.controllers.fastboot.FastbootProxy', 254 return_value=MockFastbootProxy(MOCK_SERIAL)) 255 def test_AndroidDevice_instantiation(self, MockFastboot, MockAdbProxy): 256 """Verifies the AndroidDevice object's basic attributes are correctly 257 set after instantiation. 258 """ 259 ad = android_device.AndroidDevice(serial=MOCK_SERIAL) 260 self.assertEqual(ad.serial, 1) 261 self.assertEqual(ad.model, "fakemodel") 262 self.assertIsNone(ad.adb_logcat_process) 263 expected_lp = os.path.join(logging.log_path, 264 "AndroidDevice%s" % MOCK_SERIAL) 265 self.assertEqual(ad.log_path, expected_lp) 266 267 @mock.patch( 268 'acts.controllers.adb.AdbProxy', 269 return_value=MockAdbProxy(MOCK_SERIAL)) 270 @mock.patch( 271 'acts.controllers.fastboot.FastbootProxy', 272 return_value=MockFastbootProxy(MOCK_SERIAL)) 273 def test_AndroidDevice_build_info_release(self, MockFastboot, 274 MockAdbProxy): 275 """Verifies the AndroidDevice object's basic attributes are correctly 276 set after instantiation. 277 """ 278 ad = android_device.AndroidDevice(serial=1) 279 build_info = ad.build_info 280 self.assertEqual(build_info["build_id"], "ABC1.123456.007") 281 self.assertEqual(build_info["build_type"], "userdebug") 282 283 @mock.patch( 284 'acts.controllers.adb.AdbProxy', 285 return_value=MockAdbProxy(MOCK_SERIAL, build_id=MOCK_DEV_BUILD_ID)) 286 @mock.patch( 287 'acts.controllers.fastboot.FastbootProxy', 288 return_value=MockFastbootProxy(MOCK_SERIAL)) 289 def test_AndroidDevice_build_info_dev(self, MockFastboot, MockAdbProxy): 290 """Verifies the AndroidDevice object's basic attributes are correctly 291 set after instantiation. 292 """ 293 ad = android_device.AndroidDevice(serial=1) 294 build_info = ad.build_info 295 self.assertEqual(build_info["build_id"], "123456789") 296 self.assertEqual(build_info["build_type"], "userdebug") 297 298 @mock.patch( 299 'acts.controllers.adb.AdbProxy', 300 return_value=MockAdbProxy(MOCK_SERIAL, build_id=MOCK_NYC_BUILD_ID)) 301 @mock.patch( 302 'acts.controllers.fastboot.FastbootProxy', 303 return_value=MockFastbootProxy(MOCK_SERIAL)) 304 def test_AndroidDevice_build_info_nyc(self, MockFastboot, MockAdbProxy): 305 """Verifies the AndroidDevice object's build id is set correctly for 306 NYC releases. 307 """ 308 ad = android_device.AndroidDevice(serial=1) 309 build_info = ad.build_info 310 self.assertEqual(build_info["build_id"], MOCK_NYC_BUILD_ID) 311 312 @mock.patch( 313 'acts.controllers.adb.AdbProxy', 314 return_value=MockAdbProxy(MOCK_SERIAL)) 315 @mock.patch( 316 'acts.controllers.fastboot.FastbootProxy', 317 return_value=MockFastbootProxy(MOCK_SERIAL)) 318 @mock.patch('os.makedirs') 319 @mock.patch('acts.utils.exe_cmd') 320 @mock.patch( 321 'acts.controllers.android_device.AndroidDevice.device_log_path', 322 new_callable=mock.PropertyMock) 323 def test_AndroidDevice_take_bug_report(self, mock_log_path, exe_mock, 324 mock_makedirs, FastbootProxy, 325 MockAdbProxy): 326 """Verifies AndroidDevice.take_bug_report calls the correct adb command 327 and writes the bugreport file to the correct path. 328 """ 329 ad = android_device.AndroidDevice(serial=MOCK_SERIAL) 330 mock_log_path.return_value = os.path.join( 331 logging.log_path, "AndroidDevice%s" % ad.serial) 332 ad.take_bug_report("test_something", 234325.32) 333 mock_makedirs.assert_called_with(mock_log_path(), exist_ok=True) 334 335 @mock.patch( 336 'acts.controllers.adb.AdbProxy', 337 return_value=MockAdbProxy(MOCK_SERIAL, fail_br=True)) 338 @mock.patch( 339 'acts.controllers.fastboot.FastbootProxy', 340 return_value=MockFastbootProxy(MOCK_SERIAL)) 341 @mock.patch('os.makedirs') 342 @mock.patch('acts.utils.exe_cmd') 343 @mock.patch( 344 'acts.controllers.android_device.AndroidDevice.device_log_path', 345 new_callable=mock.PropertyMock) 346 def test_AndroidDevice_take_bug_report_fail(self, mock_log_path, *_): 347 """Verifies AndroidDevice.take_bug_report writes out the correct message 348 when taking bugreport fails. 349 """ 350 ad = android_device.AndroidDevice(serial=MOCK_SERIAL) 351 mock_log_path.return_value = os.path.join( 352 logging.log_path, "AndroidDevice%s" % ad.serial) 353 expected_msg = "Failed to take bugreport on 1: OMG I died!" 354 with self.assertRaisesRegex(errors.AndroidDeviceError, expected_msg): 355 ad.take_bug_report("test_something", 4346343.23) 356 357 @mock.patch( 358 'acts.controllers.adb.AdbProxy', 359 return_value=MockAdbProxy(MOCK_SERIAL, fail_br_before_N=True)) 360 @mock.patch( 361 'acts.controllers.fastboot.FastbootProxy', 362 return_value=MockFastbootProxy(MOCK_SERIAL)) 363 @mock.patch('os.makedirs') 364 @mock.patch('acts.utils.exe_cmd') 365 @mock.patch( 366 'acts.controllers.android_device.AndroidDevice.device_log_path', 367 new_callable=mock.PropertyMock) 368 def test_AndroidDevice_take_bug_report_fallback( 369 self, mock_log_path, exe_mock, mock_makedirs, FastbootProxy, 370 MockAdbProxy): 371 """Verifies AndroidDevice.take_bug_report falls back to traditional 372 bugreport on builds that do not have bugreportz. 373 """ 374 ad = android_device.AndroidDevice(serial=MOCK_SERIAL) 375 mock_log_path.return_value = os.path.join( 376 logging.log_path, "AndroidDevice%s" % ad.serial) 377 ad.take_bug_report("test_something", MOCK_ADB_EPOCH_BEGIN_TIME) 378 mock_makedirs.assert_called_with(mock_log_path(), exist_ok=True) 379 380 @mock.patch( 381 'acts.controllers.adb.AdbProxy', 382 return_value=MockAdbProxy(MOCK_SERIAL)) 383 @mock.patch( 384 'acts.controllers.fastboot.FastbootProxy', 385 return_value=MockFastbootProxy(MOCK_SERIAL)) 386 @mock.patch('acts.libs.proc.process.Process') 387 def test_AndroidDevice_start_adb_logcat(self, proc_mock, FastbootProxy, 388 MockAdbProxy): 389 """Verifies the AndroidDevice method start_adb_logcat. Checks that the 390 underlying logcat process is started properly and correct warning msgs 391 are generated. 392 """ 393 with mock.patch(('acts.controllers.android_lib.logcat.' 394 'create_logcat_keepalive_process'), 395 return_value=proc_mock) as create_proc_mock: 396 ad = android_device.AndroidDevice(serial=MOCK_SERIAL) 397 ad.start_adb_logcat() 398 # Verify start did the correct operations. 399 self.assertTrue(ad.adb_logcat_process) 400 log_dir = "AndroidDevice%s" % ad.serial 401 create_proc_mock.assert_called_with(ad.serial, log_dir, '-b all') 402 proc_mock.start.assert_called_with() 403 # Expect warning msg if start is called back to back. 404 expected_msg = "Android device .* already has a running adb logcat" 405 proc_mock.is_running.return_value = True 406 with self.assertLogs(level='WARNING') as log: 407 ad.start_adb_logcat() 408 self.assertRegex(log.output[0], expected_msg) 409 410 @mock.patch( 411 'acts.controllers.adb.AdbProxy', 412 return_value=MockAdbProxy(MOCK_SERIAL)) 413 @mock.patch( 414 'acts.controllers.fastboot.FastbootProxy', 415 return_value=MockFastbootProxy(MOCK_SERIAL)) 416 @mock.patch('acts.controllers.android_lib.logcat.' 417 'create_logcat_keepalive_process') 418 def test_AndroidDevice_start_adb_logcat_with_user_param( 419 self, create_proc_mock, FastbootProxy, MockAdbProxy): 420 """Verifies that start_adb_logcat generates the correct adb logcat 421 command if adb_logcat_param is specified. 422 """ 423 ad = android_device.AndroidDevice(serial=MOCK_SERIAL) 424 ad.adb_logcat_param = "-b radio" 425 ad.start_adb_logcat() 426 # Verify that create_logcat_keepalive_process is called with the 427 # correct command. 428 log_dir = "AndroidDevice%s" % ad.serial 429 create_proc_mock.assert_called_with(ad.serial, log_dir, '-b radio') 430 431 @mock.patch( 432 'acts.controllers.adb.AdbProxy', 433 return_value=MockAdbProxy(MOCK_SERIAL)) 434 @mock.patch( 435 'acts.controllers.fastboot.FastbootProxy', 436 return_value=MockFastbootProxy(MOCK_SERIAL)) 437 @mock.patch('acts.libs.proc.process.Process') 438 def test_AndroidDevice_stop_adb_logcat(self, proc_mock, FastbootProxy, 439 MockAdbProxy): 440 """Verifies the AndroidDevice method stop_adb_logcat. Checks that the 441 underlying logcat process is stopped properly and correct warning msgs 442 are generated. 443 """ 444 ad = android_device.AndroidDevice(serial=MOCK_SERIAL) 445 ad.adb_logcat_process = proc_mock 446 # Expect warning msg if stop is called before start. 447 expected_msg = ( 448 "Android device .* does not have an ongoing adb logcat") 449 proc_mock.is_running.return_value = False 450 with self.assertLogs(level='WARNING') as log: 451 ad.stop_adb_logcat() 452 self.assertRegex(log.output[0], expected_msg) 453 454 # Verify the underlying process is stopped. 455 proc_mock.is_running.return_value = True 456 ad.stop_adb_logcat() 457 proc_mock.stop.assert_called_with() 458 459 @mock.patch( 460 'acts.controllers.adb.AdbProxy', 461 return_value=MockAdbProxy(MOCK_SERIAL)) 462 @mock.patch( 463 'acts.controllers.fastboot.FastbootProxy', 464 return_value=MockFastbootProxy(MOCK_SERIAL)) 465 def test_get_apk_process_id_process_cannot_find(self, fastboot_proxy, 466 adb_proxy): 467 ad = android_device.AndroidDevice(serial=MOCK_SERIAL) 468 ad.adb.return_value = "does_not_contain_value" 469 self.assertEqual(None, ad.get_package_pid("some_package")) 470 471 @mock.patch( 472 'acts.controllers.adb.AdbProxy', 473 return_value=MockAdbProxy(MOCK_SERIAL)) 474 @mock.patch( 475 'acts.controllers.fastboot.FastbootProxy', 476 return_value=MockFastbootProxy(MOCK_SERIAL)) 477 def test_get_apk_process_id_process_exists_second_try(self, fastboot_proxy, 478 adb_proxy): 479 ad = android_device.AndroidDevice(serial=MOCK_SERIAL) 480 ad.adb.return_multiple = True 481 ad.adb.return_value = ["", "system 1 2 3 4 S com.some_package"] 482 self.assertEqual(1, ad.get_package_pid("some_package")) 483 484 @mock.patch( 485 'acts.controllers.adb.AdbProxy', 486 return_value=MockAdbProxy(MOCK_SERIAL)) 487 @mock.patch( 488 'acts.controllers.fastboot.FastbootProxy', 489 return_value=MockFastbootProxy(MOCK_SERIAL)) 490 def test_get_apk_process_id_bad_return(self, fastboot_proxy, adb_proxy): 491 ad = android_device.AndroidDevice(serial=MOCK_SERIAL) 492 ad.adb.return_value = "bad_return_index_error" 493 self.assertEqual(None, ad.get_package_pid("some_package")) 494 495 @mock.patch( 496 'acts.controllers.adb.AdbProxy', 497 return_value=MockAdbProxy(MOCK_SERIAL)) 498 @mock.patch( 499 'acts.controllers.fastboot.FastbootProxy', 500 return_value=MockFastbootProxy(MOCK_SERIAL)) 501 def test_get_apk_process_id_bad_return(self, fastboot_proxy, adb_proxy): 502 ad = android_device.AndroidDevice(serial=MOCK_SERIAL) 503 ad.adb.return_value = "bad return value error" 504 self.assertEqual(None, ad.get_package_pid("some_package")) 505 506 @mock.patch( 507 'acts.controllers.adb.AdbProxy', 508 return_value=MockAdbProxy(MOCK_SERIAL)) 509 @mock.patch( 510 'acts.controllers.fastboot.FastbootProxy', 511 return_value=MockFastbootProxy(MOCK_SERIAL)) 512 def test_ensure_verity_enabled_only_system_enabled(self, fastboot_proxy, 513 adb_proxy): 514 ad = android_device.AndroidDevice(serial=MOCK_SERIAL) 515 root_user_id = '0' 516 517 ad.adb.get_user_id = mock.MagicMock() 518 ad.adb.get_user_id.return_value = root_user_id 519 520 ad.adb.getprop = mock.MagicMock(side_effect=[ 521 '', # system.verified 522 '2' 523 ]) # vendor.verified 524 ad.adb.ensure_user = mock.MagicMock() 525 ad.reboot = mock.MagicMock() 526 ad.ensure_verity_enabled() 527 ad.reboot.assert_called_once() 528 529 ad.adb.ensure_user.assert_called_with(root_user_id) 530 531 @mock.patch( 532 'acts.controllers.adb.AdbProxy', 533 return_value=MockAdbProxy(MOCK_SERIAL)) 534 @mock.patch( 535 'acts.controllers.fastboot.FastbootProxy', 536 return_value=MockFastbootProxy(MOCK_SERIAL)) 537 def test_ensure_verity_enabled_only_vendor_enabled(self, fastboot_proxy, 538 adb_proxy): 539 ad = android_device.AndroidDevice(serial=MOCK_SERIAL) 540 root_user_id = '0' 541 542 ad.adb.get_user_id = mock.MagicMock() 543 ad.adb.get_user_id.return_value = root_user_id 544 545 ad.adb.getprop = mock.MagicMock(side_effect=[ 546 '2', # system.verified 547 '' 548 ]) # vendor.verified 549 ad.adb.ensure_user = mock.MagicMock() 550 ad.reboot = mock.MagicMock() 551 552 ad.ensure_verity_enabled() 553 554 ad.reboot.assert_called_once() 555 ad.adb.ensure_user.assert_called_with(root_user_id) 556 557 @mock.patch( 558 'acts.controllers.adb.AdbProxy', 559 return_value=MockAdbProxy(MOCK_SERIAL)) 560 @mock.patch( 561 'acts.controllers.fastboot.FastbootProxy', 562 return_value=MockFastbootProxy(MOCK_SERIAL)) 563 def test_ensure_verity_enabled_both_enabled_at_start(self, fastboot_proxy, 564 adb_proxy): 565 ad = android_device.AndroidDevice(serial=MOCK_SERIAL) 566 root_user_id = '0' 567 568 ad.adb.get_user_id = mock.MagicMock() 569 ad.adb.get_user_id.return_value = root_user_id 570 571 ad.adb.getprop = mock.MagicMock(side_effect=[ 572 '2', # system.verified 573 '2' 574 ]) # vendor.verified 575 ad.adb.ensure_user = mock.MagicMock() 576 ad.reboot = mock.MagicMock() 577 ad.ensure_verity_enabled() 578 579 assert not ad.reboot.called 580 581 @mock.patch( 582 'acts.controllers.adb.AdbProxy', 583 return_value=MockAdbProxy(MOCK_SERIAL)) 584 @mock.patch( 585 'acts.controllers.fastboot.FastbootProxy', 586 return_value=MockFastbootProxy(MOCK_SERIAL)) 587 def test_ensure_verity_disabled_system_already_disabled( 588 self, fastboot_proxy, adb_proxy): 589 ad = android_device.AndroidDevice(serial=MOCK_SERIAL) 590 root_user_id = '0' 591 592 ad.adb.get_user_id = mock.MagicMock() 593 ad.adb.get_user_id.return_value = root_user_id 594 595 ad.adb.getprop = mock.MagicMock(side_effect=[ 596 '2', # system.verified 597 '' 598 ]) # vendor.verified 599 ad.adb.ensure_user = mock.MagicMock() 600 ad.reboot = mock.MagicMock() 601 ad.ensure_verity_disabled() 602 603 ad.reboot.assert_called_once() 604 605 @mock.patch( 606 'acts.controllers.adb.AdbProxy', 607 return_value=MockAdbProxy(MOCK_SERIAL)) 608 @mock.patch( 609 'acts.controllers.fastboot.FastbootProxy', 610 return_value=MockFastbootProxy(MOCK_SERIAL)) 611 def test_ensure_verity_disabled_vendor_already_disabled( 612 self, fastboot_proxy, adb_proxy): 613 ad = android_device.AndroidDevice(serial=MOCK_SERIAL) 614 root_user_id = '0' 615 616 ad.adb.get_user_id = mock.MagicMock() 617 ad.adb.get_user_id.return_value = root_user_id 618 619 ad.adb.getprop = mock.MagicMock(side_effect=[ 620 '', # system.verified 621 '2' 622 ]) # vendor.verified 623 ad.adb.ensure_user = mock.MagicMock() 624 ad.reboot = mock.MagicMock() 625 626 ad.ensure_verity_disabled() 627 628 ad.reboot.assert_called_once() 629 ad.adb.ensure_user.assert_called_with(root_user_id) 630 631 @mock.patch( 632 'acts.controllers.adb.AdbProxy', 633 return_value=MockAdbProxy(MOCK_SERIAL)) 634 @mock.patch( 635 'acts.controllers.fastboot.FastbootProxy', 636 return_value=MockFastbootProxy(MOCK_SERIAL)) 637 def test_ensure_verity_disabled_disabled_at_start( 638 self, fastboot_proxy, adb_proxy): 639 ad = android_device.AndroidDevice(serial=MOCK_SERIAL) 640 root_user_id = '0' 641 642 ad.adb.get_user_id = mock.MagicMock() 643 ad.adb.get_user_id.return_value = root_user_id 644 645 ad.adb.getprop = mock.MagicMock(side_effect=[ 646 '', # system.verified 647 '' 648 ]) # vendor.verified 649 ad.adb.ensure_user = mock.MagicMock() 650 ad.reboot = mock.MagicMock() 651 652 ad.ensure_verity_disabled() 653 654 assert not ad.reboot.called 655 656 @mock.patch( 657 'acts.controllers.adb.AdbProxy', 658 return_value=MockAdbProxy(MOCK_SERIAL)) 659 @mock.patch( 660 'acts.controllers.fastboot.FastbootProxy', 661 return_value=MockFastbootProxy(MOCK_SERIAL)) 662 def test_push_system_file(self, fastboot_proxy, adb_proxy): 663 ad = android_device.AndroidDevice(serial=MOCK_SERIAL) 664 ad.ensure_verity_disabled = mock.MagicMock() 665 ad.adb.remount = mock.MagicMock() 666 ad.adb.push = mock.MagicMock() 667 668 ret = ad.push_system_file('asdf', 'jkl') 669 self.assertTrue(ret) 670 671 @mock.patch( 672 'acts.controllers.adb.AdbProxy', 673 return_value=MockAdbProxy(MOCK_SERIAL)) 674 @mock.patch( 675 'acts.controllers.fastboot.FastbootProxy', 676 return_value=MockFastbootProxy(MOCK_SERIAL)) 677 def test_push_system_file_returns_false_on_error(self, fastboot_proxy, 678 adb_proxy): 679 ad = android_device.AndroidDevice(serial=MOCK_SERIAL) 680 ad.ensure_verity_disabled = mock.MagicMock() 681 ad.adb.remount = mock.MagicMock() 682 ad.adb.push = mock.MagicMock(return_value='error') 683 684 ret = ad.push_system_file('asdf', 'jkl') 685 self.assertFalse(ret) 686 687 688if __name__ == "__main__": 689 unittest.main() 690