• 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.
4import copy
5import unittest
6
7from autotest_lib.client.common_lib import error
8
9from autotest_lib.client.cros.enterprise import policy
10
11"""
12This is the unittest file for policy.py.
13If you modify that file, you should be at minimum re-running this file.
14
15Add and correct tests as changes are made to the utils file.
16
17To run the tests, use the following command from your DEV box (outside_chroot):
18
19src/third_party/autotest/files/utils$ python unittest_suite.py \
20autotest_lib.client.cros.enterprise.test_policy --debug
21
22"""
23
24
25class test_policyManager(unittest.TestCase):
26
27    dict_value = {'scope': 'testScope', 'source': 'testSource',
28                  'level': 'testLevel', 'value': 'test_value'}
29    dict_value2 = {'scope': 'testScope', 'source': 'testSource',
30                   'level': 'testLevel', 'value': 'test_value2'}
31
32    def test_setting_params(self):
33        test_policy = policy.Policy()
34        test_policy.name = 'Test'
35        test_policy.level = 1
36        test_policy.value = 'TheValue'
37        test_policy.scope = 'test_value'
38        test_policy.source = 'Cloud'
39        self.assertEqual(test_policy.name, 'Test')
40        self.assertEqual(test_policy.level, 1)
41        self.assertEqual(test_policy.scope, 'test_value')
42        self.assertEqual(test_policy.source, 'Cloud')
43        self.assertEqual(test_policy.value, 'TheValue')
44
45    def test_setting_group_user(self):
46        test_policy = policy.Policy()
47        test_policy.name = 'Test'
48        test_policy.group = 'user'
49        self.assertEqual(test_policy.name, 'Test')
50        self.assertEqual(test_policy.source, 'cloud')
51        self.assertEqual(test_policy.level, 'mandatory')
52        self.assertEqual(test_policy.scope, 'user')
53
54    def test_setting_group_device(self):
55        test_policy = policy.Policy()
56        test_policy.name = 'Test'
57        test_policy.group = 'device'
58        self.assertEqual(test_policy.name, 'Test')
59        self.assertEqual(test_policy.source, 'cloud')
60        self.assertEqual(test_policy.level, 'mandatory')
61        self.assertEqual(test_policy.scope, 'machine')
62
63    def test_setting_group_suggested_user(self):
64        test_policy = policy.Policy()
65        test_policy.name = 'Test'
66        test_policy.group = 'suggested_user'
67        self.assertEqual(test_policy.name, 'Test')
68        self.assertEqual(test_policy.source, 'cloud')
69        self.assertEqual(test_policy.level, 'recommended')
70        self.assertEqual(test_policy.scope, 'user')
71
72    def test_setting_value(self):
73        test_policy = policy.Policy()
74        test_policy.name = 'Test'
75        test_policydict = copy.deepcopy(self.dict_value)
76        test_policydict['error'] = 'An error'
77        test_policy.set_policy_from_dict(test_policydict)
78
79        self.assertEqual(test_policy.name, 'Test')
80        self.assertEqual(test_policy.level, 'testLevel')
81        self.assertEqual(test_policy.scope, 'testScope')
82        self.assertEqual(test_policy.source, 'testSource')
83        self.assertEqual(test_policy.value, 'test_value')
84
85    def test_get_policy_as_dict(self):
86        test_policy = policy.Policy()
87        test_policy.name = 'Test'
88        test_policy.level = 1
89        test_policy.value = 'TheValue'
90        test_policy.scope = 'test_value'
91        test_policy.source = 'Cloud'
92
93        expected_dict = {
94            'Test': {
95                'scope': 'test_value',
96                'level': 1,
97                'value': 'TheValue',
98                'source': 'Cloud'}}
99        self.assertEqual(expected_dict, test_policy.get_policy_as_dict())
100
101    def test_policy_eq_ne(self):
102        test_policy = policy.Policy()
103        test_policy.name = 'Test'
104        test_policy.set_policy_from_dict(self.dict_value)
105
106        test_policy2 = policy.Policy()
107        test_policy2.name = 'Test2'
108        test_policy2.set_policy_from_dict(self.dict_value)
109        self.assertTrue(test_policy == test_policy2)
110        self.assertFalse(test_policy != test_policy2)
111
112        test_policy3 = policy.Policy()
113        test_policy3.name = 'Test3'
114        test_policy3.set_policy_from_dict(self.dict_value2)
115        self.assertFalse(test_policy == test_policy3)
116
117    def test_policy_eq_obfuscated(self):
118        test_policy = policy.Policy()
119        test_policy.name = 'Test'
120        test_value = {
121            'scope': 'testScope', 'source': 'testSource',
122            'level': 'testLevel',
123            'value': {'NetworkConfigurations': [
124                {'WiFi': {'Passphrase': 'Secret'}}]}}
125        test_policy.set_policy_from_dict(test_value)
126
127        test_policy2 = policy.Policy()
128        test_policy2.name = 'TestOpenNetworkConfiguration'
129        obfuscated_value = {
130            'scope': 'testScope', 'source': 'testSource',
131            'level': 'testLevel',
132            'value': {'NetworkConfigurations': [
133                {'WiFi': {'Passphrase': '********'}}]}}
134        test_policy2.set_policy_from_dict(obfuscated_value)
135        self.assertTrue(test_policy == test_policy2)
136        self.assertFalse(test_policy != test_policy2)
137
138    def test_is_network_policy(self):
139        self.assertTrue(
140            policy.is_network_policy('TestOpenNetworkConfiguration'))
141
142        self.assertFalse(policy.is_network_policy('Test'))
143
144    def test_check_obfuscation(self):
145        test_value = {
146            'NetworkConfigurations': [
147                {'WiFi': {'Passphrase': '********'}}]}
148        self.assertTrue(policy.check_obfuscation(test_value))
149
150        test_value2 = {
151            'NetworkConfigurations': [
152                {'WiFi': {'Passphrase': 'Bad'}}]}
153        self.assertFalse(policy.check_obfuscation(test_value2))
154
155        test_value3 = {
156            'NetworkConfigurations': [
157                {'WiFi': {'EAP': {'Password': '********'}}}]}
158        self.assertTrue(policy.check_obfuscation(test_value3))
159
160        test_value4 = {
161            'NetworkConfigurations': [
162                {'WiFi': {'EAP': {'Password': 'Bad'}}}]}
163        self.assertFalse(policy.check_obfuscation(test_value4))
164
165        test_value5 = {
166            'Certificates': [
167                {'PKCS12': '********'}]}
168        self.assertTrue(policy.check_obfuscation(test_value5))
169
170        test_value6 = {
171            'Certificates': [
172                {'PKCS12': 'BAD'}]}
173        self.assertFalse(policy.check_obfuscation(test_value6))
174
175    def test_invalid_policy_dict(self):
176        with self.assertRaises(error.TestError) as context:
177            test_policy = policy.Policy()
178            test_policy.name = 'Bad Policy'
179            test_policy.set_policy_from_dict({'Invalid Keys': 'Invalid Value'})
180        self.assertIn('Incorrect input data provided', str(context.exception))
181
182
183if __name__ == '__main__':
184    unittest.main()
185