Home | History | Annotate | Download | only in tests
      1 #!/usr/bin/env python3.4
      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 
     17 import time
     18 import unittest
     19 import enum
     20 
     21 from acts import utils
     22 from acts.controllers.adb import AdbError
     23 
     24 
     25 class ActsUtilsTest(unittest.TestCase):
     26     """This test class has unit tests for the implementation of everything
     27     under acts.utils.
     28     """
     29 
     30     def test_start_standing_subproc(self):
     31         with self.assertRaisesRegexp(utils.ActsUtilsError,
     32                                      "Process .* has terminated"):
     33             utils.start_standing_subprocess("sleep 0", check_health_delay=0.1)
     34 
     35     def test_stop_standing_subproc(self):
     36         p = utils.start_standing_subprocess("sleep 0")
     37         time.sleep(0.1)
     38         with self.assertRaisesRegexp(utils.ActsUtilsError,
     39                                      "Process .* has terminated"):
     40             utils.stop_standing_subprocess(p)
     41 
     42     def test_bypass_setup_wizard_no_complications(self):
     43         ad = MockAd()
     44         ad.adb.return_state = BypassSetupWizardReturn.NO_COMPLICATIONS
     45         self.assertTrue(utils.bypass_setup_wizard(ad, 0))
     46         self.assertFalse(ad.adb.root_adb_called)
     47 
     48     def test_bypass_setup_wizard_unrecognized_error(self):
     49         ad = MockAd()
     50         ad.adb.return_state = BypassSetupWizardReturn.UNRECOGNIZED_ERR
     51         with self.assertRaises(AdbError):
     52             utils.bypass_setup_wizard(ad, 0)
     53         self.assertFalse(ad.adb.root_adb_called)
     54 
     55     def test_bypass_setup_wizard_need_root_access(self):
     56         ad = MockAd()
     57         ad.adb.return_state = BypassSetupWizardReturn.ROOT_ADB_NO_COMP
     58         self.assertTrue(utils.bypass_setup_wizard(ad, 0))
     59         self.assertTrue(ad.adb.root_adb_called)
     60 
     61     def test_bypass_setup_wizard_need_root_already_skipped(self):
     62         ad = MockAd()
     63         ad.adb.return_state = BypassSetupWizardReturn.ROOT_ADB_SKIPPED
     64         self.assertTrue(utils.bypass_setup_wizard(ad, 0))
     65         self.assertTrue(ad.adb.root_adb_called)
     66 
     67     def test_bypass_setup_wizard_root_access_still_fails(self):
     68         ad = MockAd()
     69         ad.adb.return_state = BypassSetupWizardReturn.ROOT_ADB_FAILS
     70         with self.assertRaises(AdbError):
     71             utils.bypass_setup_wizard(ad, 0)
     72         self.assertTrue(ad.adb.root_adb_called)
     73 
     74 
     75 class BypassSetupWizardReturn:
     76     # No complications. Bypass works the first time without issues.
     77     NO_COMPLICATIONS = AdbError("", "", "", 1)
     78     # Fail with doesn't need to be skipped/was skipped already.
     79     ALREADY_BYPASSED = AdbError("", "ADB_CMD_OUTPUT:0", "Error type 3\n"
     80                                 "Error: Activity class", 1)
     81     # Fail with different error.
     82     UNRECOGNIZED_ERR = AdbError("", "ADB_CMD_OUTPUT:0", "Error type 4\n"
     83                                 "Error: Activity class", 0)
     84     # Fail, get root access, then no complications arise.
     85     ROOT_ADB_NO_COMP = AdbError("", "ADB_CMD_OUTPUT:255",
     86                                 "Security exception: Permission Denial: "
     87                                 "starting Intent { flg=0x10000000 "
     88                                 "cmp=com.google.android.setupwizard/"
     89                                 ".SetupWizardExitActivity } from null "
     90                                 "(pid=5045, uid=2000) not exported from uid "
     91                                 "10000", 0)
     92     # Even with root access, the bypass setup wizard doesn't need to be skipped.
     93     ROOT_ADB_SKIPPED = AdbError("", "ADB_CMD_OUTPUT:255",
     94                                 "Security exception: Permission Denial: "
     95                                 "starting Intent { flg=0x10000000 "
     96                                 "cmp=com.google.android.setupwizard/"
     97                                 ".SetupWizardExitActivity } from null "
     98                                 "(pid=5045, uid=2000) not exported from "
     99                                 "uid 10000", 0)
    100     # Even with root access, the bypass setup wizard fails
    101     ROOT_ADB_FAILS = AdbError(
    102         "", "ADB_CMD_OUTPUT:255", "Security exception: Permission Denial: "
    103         "starting Intent { flg=0x10000000 "
    104         "cmp=com.google.android.setupwizard/"
    105         ".SetupWizardExitActivity } from null (pid=5045, "
    106         "uid=2000) not exported from uid 10000", 0)
    107 
    108 
    109 class MockAd:
    110     def __init__(self):
    111         self.adb = MockAdb()
    112 
    113 
    114 class MockAdb:
    115     def __init__(self):
    116         self.return_state = BypassSetupWizardReturn.NO_COMPLICATIONS
    117         self.root_adb_called = False
    118 
    119     def shell(self, string):
    120         if string == "settings get global device_provisioned":
    121             return self.return_state.ret_code
    122         raise self.return_state
    123 
    124     def root_adb(self):
    125         self.root_adb_called = True
    126         if self.return_state is BypassSetupWizardReturn.ROOT_ADB_FAILS:
    127             self.return_state = BypassSetupWizardReturn.UNRECOGNIZED_ERR
    128         elif self.return_state is not BypassSetupWizardReturn.ROOT_ADB_SKIPPED:
    129             self.return_state = BypassSetupWizardReturn.ALREADY_BYPASSED
    130         else:
    131             self.return_state = BypassSetupWizardReturn.NO_COMPLICATIONS
    132 
    133 
    134 if __name__ == "__main__":
    135     unittest.main()
    136