• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5from autotest_lib.client.common_lib import error
6from autotest_lib.client.common_lib import utils
7
8from autotest_lib.client.common_lib.cros import arc
9from autotest_lib.client.cros.enterprise import enterprise_policy_base
10
11
12class policy_EnterpriseForceInstallCustom(
13        enterprise_policy_base.EnterprisePolicyTest):
14    """
15    Test effect of policy_AlternateErrorPages policy on Chrome OS.
16
17    """
18    version = 1
19
20    POLICY_NAME = 'ArcPolicy'
21    PACKAGE1 = "com.coffeebeanventures.easyvoicerecorder"
22
23    def _get_actual_policy_apps(self):
24        """
25        Return the apps listed in the ArcPolicy value with the value of
26        "FORCE_INSTALLED" or "BLOCKED".
27
28        @raises error.TestError if no apps are found.
29
30        @returns: App & policies that are set to force_install, or blocked.
31
32        """
33        policy_value = self._get_policy_value_from_new_tab(self.POLICY_NAME)
34        if not policy_value:
35            raise error.TestError('No value for ArcPolicy found!')
36
37        check_apps = []
38        checklist = set(['FORCE_INSTALLED', 'BLOCKED'])
39        app_settings = 'applications'
40        if app_settings not in policy_value:
41            raise error.TestError('ArcPolicy has no application settings!')
42
43        for app in policy_value[app_settings]:
44            if app['installType'] in checklist:
45                check_apps.append(app)
46        return check_apps
47
48    def _verify_force_apps_list(self):
49        """
50        Verify that the expected force-installed apps match the policy value.
51
52        """
53        controlled_apps = self._get_actual_policy_apps()
54        for pkg in controlled_apps:
55            self._verify_package_status(pkg['packageName'], pkg['installType'])
56
57    def _verify_package_status(self, pkg, installType):
58        """
59        Test that the package is installed/not installed as expected
60
61        @param pkg: Name of the package to check.
62        @param installType: Policy Setting of the app.
63
64        @raises error.TestError if any expected apps are not found.
65
66        """
67        type_lookup = {'FORCE_INSTALLED': True,
68                       'BLOCKED': False}
69        status = type_lookup[installType]
70
71        utils.poll_for_condition(
72            lambda: self._check_pkg(pkg, status),
73            exception=error.TestFail('Package {} not installed!'.format(pkg)),
74            timeout=240,
75            sleep_interval=1,
76            desc='Polling for Package to be installed')
77
78    def _check_pkg(self, pkg, status):
79        """
80        Checks to see if the package state is proper.
81
82        @param pkg: Name of the package to check.
83        @param status: If the package should be installed or not.
84
85        @returns: True if the status is correct, else False.
86
87        """
88        if status:
89            return arc._is_in_installed_packages_list(pkg)
90        else:
91            return not arc._is_in_installed_packages_list(pkg)
92
93    def run_once(self):
94        """
95        Verify that the FORCE_INSTALLED and BLOCKED app settings work properly.
96
97        Test will itterate twice. First will run the test with the app
98        installed, and verify it is loaded. The second will block the app, and
99        verify that the app is un-installed.
100
101        """
102        cases = ['FORCE_INSTALLED', 'BLOCKED']
103        for case in cases:
104
105            pol = self.policy_creator(case)
106            self.setup_case(user_policies=pol,
107                            arc_mode='enabled',
108                            use_clouddpc_test=False)
109
110            self._verify_force_apps_list()
111
112    def policy_creator(self, case):
113        """
114        Generates the policy value.
115
116        @param case: If the app should installed or removed.
117
118        @returns: Policy value.
119        """
120        pol = {'ArcEnabled': True,
121               'ArcPolicy':
122                   {"installUnknownSourcesDisabled": False,
123                    "playDeviceLocalPolicyEnabled": True,
124                    "availableAppSetPolicy": "WHITELIST",
125                    "applications":
126                        [{"packageName": self.PACKAGE1,
127                          "defaultPermissionPolicy": "GRANT",
128                          "installType": case}]
129                    }
130               }
131        return pol
132