| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 import logging | |
| 7 import os | |
| 8 | |
| 9 import pyauto_functional # must come before pyauto. | |
| 10 import policy_base | |
| 11 import policy_test_cases | |
| 12 import pyauto_errors | |
| 13 import pyauto | |
| 14 | |
| 15 | |
| 16 class PolicyTest(policy_base.PolicyTestBase): | |
| 17 """Tests that the effects of policies are being enforced as expected.""" | |
| 18 | |
| 19 def _GetPrefIsManagedError(self, pref, expected_value): | |
| 20 """Verify the managed preferences cannot be modified. | |
| 21 | |
| 22 Args: | |
| 23 pref: The preference key that you want to modify. | |
| 24 expected_value: Current value of the preference. | |
| 25 | |
| 26 Returns: | |
| 27 Error message if any, None if pref is successfully managed. | |
| 28 """ | |
| 29 # Check if the current value of the preference is set as expected. | |
| 30 local_state_pref_value = self.GetLocalStatePrefsInfo().Prefs(pref) | |
| 31 profile_pref_value = self.GetPrefsInfo().Prefs(pref) | |
| 32 actual_value = (profile_pref_value if profile_pref_value is not None else | |
| 33 local_state_pref_value) | |
| 34 if actual_value is None: | |
| 35 return 'Preference %s is not registered.' % pref | |
| 36 elif actual_value != expected_value: | |
| 37 return ('Preference value "%s" does not match policy value "%s".' % | |
| 38 (actual_value, expected_value)) | |
| 39 # If the preference is managed, this should throw an exception. | |
| 40 try: | |
| 41 if profile_pref_value is not None: | |
| 42 self.SetPrefs(pref, expected_value) | |
| 43 else: | |
| 44 self.SetLocalStatePrefs(pref, expected_value) | |
| 45 except pyauto_errors.JSONInterfaceError, e: | |
| 46 if str(e) != 'pref is managed. cannot be changed.': | |
| 47 return str(e) | |
| 48 else: | |
| 49 return None | |
| 50 else: | |
| 51 return 'Preference can be set even though a policy is in effect.' | |
| 52 | |
| 53 def setUp(self): | |
| 54 policy_base.PolicyTestBase.setUp(self) | |
| 55 if self.IsChromeOS(): | |
| 56 self.LoginWithTestAccount() | |
| 57 | |
| 58 def testPolicyToPrefMapping(self): | |
| 59 """Verify that simple user policies map to corresponding prefs. | |
| 60 | |
| 61 Also verify that once these policies are in effect, the prefs cannot be | |
| 62 modified by the user. | |
| 63 """ | |
| 64 total = 0 | |
| 65 fails = [] | |
| 66 policy_prefs = policy_test_cases.PolicyPrefsTestCases | |
| 67 for policy, values in policy_prefs.policies.iteritems(): | |
| 68 pref = values[policy_prefs.INDEX_PREF] | |
| 69 value = values[policy_prefs.INDEX_VALUE] | |
| 70 os = values[policy_prefs.INDEX_OS] | |
| 71 if not pref or self.GetPlatform() not in os: | |
| 72 continue | |
| 73 self.SetUserPolicy({policy: value}) | |
| 74 error = self._GetPrefIsManagedError(getattr(pyauto, pref), value) | |
| 75 if error: | |
| 76 fails.append('%s: %s' % (policy, error)) | |
| 77 total += 1 | |
| 78 self.assertFalse(fails, msg='%d of %d policies failed.\n%s' % | |
| 79 (len(fails), total, '\n'.join(fails))) | |
| 80 | |
| 81 | |
| 82 if __name__ == '__main__': | |
| 83 pyauto_functional.Main() | |
| OLD | NEW |