1# Copyright (c) 2012 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 5import dbus, gobject, os, sys 6 7import common 8from autotest_lib.client.common_lib import error 9from autotest_lib.client.common_lib.cros import session_manager 10from autotest_lib.client.cros import ownership 11 12"""Utility class for tests that generate, push and fetch policies. 13 14As the python bindings for the protobufs used in policies are built as a part 15of tests that use them, callers must pass in their location at call time.""" 16 17 18def install_protobufs(autodir, job): 19 """Installs policy protobuf dependencies and set import path. 20 21 After calling this, you can simply import any policy pb2.py file directly, 22 e.g. import chrome_device_policy_pb2. 23 24 @param autodir: Autotest directory (usually the caller's self.autodir). 25 @param job: Job instance (usually the caller's self.job). 26 """ 27 # TODO(crbug.com/807950): Change the installation process so that policy 28 # proto imports can be moved to the top. 29 dep = 'policy_protos' 30 dep_dir = os.path.join(autodir, 'deps', dep) 31 job.install_pkg(dep, 'dep', dep_dir) 32 sys.path.append(dep_dir) 33 34 35def compare_policy_response(policy_response, owner=None, guests=None, 36 new_users=None, roaming=None): 37 """Check the contents of |policy_response| against given args. 38 39 Deserializes |policy_response| into a PolicyFetchResponse protobuf, 40 with an embedded (serialized) PolicyData protobuf that embeds a 41 (serialized) ChromeDeviceSettingsProto, and checks to see if this 42 protobuf turducken contains the information passed in. 43 44 @param policy_response: string serialization of a PolicyData protobuf. 45 @param owner: string representing the owner's name/account. 46 @param guests: boolean indicating whether guests should be allowed. 47 @param new_users: boolean indicating if user pods are on login screen. 48 @param roaming: boolean indicating whether data roaming is enabled. 49 50 @return True if |policy_response| has all the provided data, else False. 51 """ 52 import chrome_device_policy_pb2 53 import device_management_backend_pb2 54 55 response_proto = device_management_backend_pb2.PolicyFetchResponse() 56 response_proto.ParseFromString(policy_response) 57 ownership.assert_has_policy_data(response_proto) 58 59 data_proto = device_management_backend_pb2.PolicyData() 60 data_proto.ParseFromString(response_proto.policy_data) 61 ownership.assert_has_device_settings(data_proto) 62 if owner: ownership.assert_username(data_proto, owner) 63 64 settings = chrome_device_policy_pb2.ChromeDeviceSettingsProto() 65 settings.ParseFromString(data_proto.policy_value) 66 if guests: ownership.assert_guest_setting(settings, guests) 67 if new_users: ownership.assert_show_users(settings, new_users) 68 if roaming: ownership.assert_roaming(settings, roaming) 69 70 71def build_policy_data(): 72 """Generate and serialize a populated device policy protobuffer. 73 74 Creates a PolicyData protobuf, with an embedded 75 ChromeDeviceSettingsProto, containing the information passed in. 76 77 @return serialization of the PolicyData proto that we build. 78 """ 79 import chrome_device_policy_pb2 80 import device_management_backend_pb2 81 82 data_proto = device_management_backend_pb2.PolicyData() 83 data_proto.policy_type = ownership.POLICY_TYPE 84 85 settings = chrome_device_policy_pb2.ChromeDeviceSettingsProto() 86 87 data_proto.policy_value = settings.SerializeToString() 88 return data_proto.SerializeToString() 89 90 91def generate_policy(key, pubkey, policy, old_key=None): 92 """Generate and serialize a populated, signed device policy protobuffer. 93 94 Creates a protobuf containing the device policy |policy|, signed with 95 |key|. Also includes the public key |pubkey|, signed with |old_key| 96 if provided. If not, |pubkey| is signed with |key|. The protobuf 97 is serialized to a string and returned. 98 99 @param key: new policy signing key. 100 @param pubkey: new public key to be signed and embedded in generated 101 PolicyFetchResponse. 102 @param policy: policy data to be embedded in generated PolicyFetchResponse. 103 @param old_key: if provided, this implies the generated PolicyFetchRespone 104 is intended to represent a key rotation. pubkey will be 105 signed with this key before embedding. 106 107 @return serialization of the PolicyFetchResponse proto that we build. 108 """ 109 import device_management_backend_pb2 110 111 if old_key == None: 112 old_key = key 113 policy_proto = device_management_backend_pb2.PolicyFetchResponse() 114 policy_proto.policy_data = policy 115 policy_proto.policy_data_signature = ownership.sign(key, policy) 116 policy_proto.new_public_key = pubkey 117 policy_proto.new_public_key_signature = ownership.sign(old_key, pubkey) 118 return policy_proto.SerializeToString() 119 120 121def push_policy_and_verify(policy_string, sm): 122 """Push a device policy to the session manager over DBus. 123 124 The serialized device policy |policy_string| is sent to the session 125 manager with the StorePolicyEx DBus call. Success of the store is 126 validated by fetching the policy again and comparing. 127 128 @param policy_string: serialized policy to push to the session manager. 129 @param sm: a connected SessionManagerInterface. 130 131 @raises error.TestFail if policy push failed. 132 """ 133 listener = session_manager.OwnershipSignalListener(gobject.MainLoop()) 134 listener.listen_for_new_policy() 135 descriptor = session_manager.make_device_policy_descriptor() 136 sm.StorePolicyEx(descriptor, 137 dbus.ByteArray(policy_string), byte_arrays=True) 138 listener.wait_for_signals(desc='Policy push.') 139 140 retrieved_policy = sm.RetrievePolicyEx(descriptor, byte_arrays=True) 141 if retrieved_policy != policy_string: 142 raise error.TestFail('Policy should not be %s' % retrieved_policy) 143 144 145def get_policy(sm): 146 """Get a device policy from the session manager over DBus. 147 148 Provided mainly for symmetry with push_policy_and_verify(). 149 150 @param sm: a connected SessionManagerInterface. 151 152 @return Serialized PolicyFetchResponse. 153 """ 154 return sm.RetrievePolicyEx(session_manager.make_device_policy_descriptor(), 155 byte_arrays=True) 156