1 // Copyright (c) 2012 The Chromium 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.
5 #include "chrome/browser/chromeos/settings/session_manager_operation.h"
7 #include "base/basictypes.h"
9 #include "base/bind_helpers.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h"
14 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
15 #include "chrome/browser/chromeos/settings/mock_owner_key_util.h"
16 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
17 #include "components/policy/core/common/cloud/cloud_policy_validator.h"
18 #include "components/policy/core/common/cloud/policy_builder.h"
19 #include "content/public/test/test_browser_thread.h"
20 #include "crypto/rsa_private_key.h"
21 #include "policy/proto/device_management_backend.pb.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h"
25 namespace em
= enterprise_management
;
32 class SessionManagerOperationTest
: public testing::Test
{
34 SessionManagerOperationTest()
35 : ui_thread_(content::BrowserThread::UI
, &message_loop_
),
36 file_thread_(content::BrowserThread::FILE, &message_loop_
),
37 owner_key_util_(new MockOwnerKeyUtil()),
40 virtual void SetUp() OVERRIDE
{
41 policy_
.payload().mutable_pinned_apps()->add_app_id("fake-app");
45 MOCK_METHOD2(OnOperationCompleted
,
46 void(SessionManagerOperation
*, DeviceSettingsService::Status
));
48 void CheckSuccessfulValidation(
49 policy::DeviceCloudPolicyValidator
* validator
) {
50 EXPECT_TRUE(validator
->success());
51 EXPECT_TRUE(validator
->payload().get());
52 EXPECT_EQ(validator
->payload()->SerializeAsString(),
53 policy_
.payload().SerializeAsString());
57 void CheckPublicKeyLoaded(SessionManagerOperation
* op
) {
58 ASSERT_TRUE(op
->owner_key().get());
59 ASSERT_TRUE(op
->owner_key()->public_key());
60 std::vector
<uint8
> public_key
;
61 ASSERT_TRUE(policy_
.GetSigningKey()->ExportPublicKey(&public_key
));
62 EXPECT_EQ(public_key
, *op
->owner_key()->public_key());
65 void CheckPrivateKeyLoaded(SessionManagerOperation
* op
) {
66 ASSERT_TRUE(op
->owner_key().get());
67 ASSERT_TRUE(op
->owner_key()->private_key());
68 std::vector
<uint8
> expected_key
;
69 ASSERT_TRUE(policy_
.GetSigningKey()->ExportPrivateKey(&expected_key
));
70 std::vector
<uint8
> actual_key
;
71 ASSERT_TRUE(op
->owner_key()->private_key()->ExportPrivateKey(&actual_key
));
72 EXPECT_EQ(expected_key
, actual_key
);
76 base::MessageLoop message_loop_
;
77 content::TestBrowserThread ui_thread_
;
78 content::TestBrowserThread file_thread_
;
80 policy::DevicePolicyBuilder policy_
;
81 DeviceSettingsTestHelper device_settings_test_helper_
;
82 scoped_refptr
<MockOwnerKeyUtil
> owner_key_util_
;
87 DISALLOW_COPY_AND_ASSIGN(SessionManagerOperationTest
);
90 TEST_F(SessionManagerOperationTest
, LoadNoPolicyNoKey
) {
91 LoadSettingsOperation
op(
92 base::Bind(&SessionManagerOperationTest::OnOperationCompleted
,
93 base::Unretained(this)));
97 &op
, DeviceSettingsService::STORE_KEY_UNAVAILABLE
));
98 op
.Start(&device_settings_test_helper_
, owner_key_util_
, NULL
);
99 device_settings_test_helper_
.Flush();
100 Mock::VerifyAndClearExpectations(this);
102 EXPECT_FALSE(op
.policy_data().get());
103 EXPECT_FALSE(op
.device_settings().get());
104 ASSERT_TRUE(op
.owner_key().get());
105 EXPECT_FALSE(op
.owner_key()->public_key());
106 EXPECT_FALSE(op
.owner_key()->private_key());
109 TEST_F(SessionManagerOperationTest
, LoadOwnerKey
) {
110 owner_key_util_
->SetPublicKeyFromPrivateKey(*policy_
.GetSigningKey());
111 LoadSettingsOperation
op(
112 base::Bind(&SessionManagerOperationTest::OnOperationCompleted
,
113 base::Unretained(this)));
116 OnOperationCompleted(
117 &op
, DeviceSettingsService::STORE_NO_POLICY
));
118 op
.Start(&device_settings_test_helper_
, owner_key_util_
, NULL
);
119 device_settings_test_helper_
.Flush();
120 Mock::VerifyAndClearExpectations(this);
122 CheckPublicKeyLoaded(&op
);
125 TEST_F(SessionManagerOperationTest
, LoadPolicy
) {
126 owner_key_util_
->SetPublicKeyFromPrivateKey(*policy_
.GetSigningKey());
127 device_settings_test_helper_
.set_policy_blob(policy_
.GetBlob());
128 LoadSettingsOperation
op(
129 base::Bind(&SessionManagerOperationTest::OnOperationCompleted
,
130 base::Unretained(this)));
133 OnOperationCompleted(
134 &op
, DeviceSettingsService::STORE_SUCCESS
));
135 op
.Start(&device_settings_test_helper_
, owner_key_util_
, NULL
);
136 device_settings_test_helper_
.Flush();
137 Mock::VerifyAndClearExpectations(this);
139 ASSERT_TRUE(op
.policy_data().get());
140 EXPECT_EQ(policy_
.policy_data().SerializeAsString(),
141 op
.policy_data()->SerializeAsString());
142 ASSERT_TRUE(op
.device_settings().get());
143 EXPECT_EQ(policy_
.payload().SerializeAsString(),
144 op
.device_settings()->SerializeAsString());
147 TEST_F(SessionManagerOperationTest
, LoadPrivateOwnerKey
) {
148 owner_key_util_
->SetPrivateKey(policy_
.GetSigningKey());
149 LoadSettingsOperation
op(
150 base::Bind(&SessionManagerOperationTest::OnOperationCompleted
,
151 base::Unretained(this)));
154 OnOperationCompleted(
155 &op
, DeviceSettingsService::STORE_NO_POLICY
));
156 op
.Start(&device_settings_test_helper_
, owner_key_util_
, NULL
);
157 device_settings_test_helper_
.Flush();
158 Mock::VerifyAndClearExpectations(this);
160 CheckPublicKeyLoaded(&op
);
161 CheckPrivateKeyLoaded(&op
);
164 TEST_F(SessionManagerOperationTest
, RestartLoad
) {
165 owner_key_util_
->SetPrivateKey(policy_
.GetSigningKey());
166 device_settings_test_helper_
.set_policy_blob(policy_
.GetBlob());
167 LoadSettingsOperation
op(
168 base::Bind(&SessionManagerOperationTest::OnOperationCompleted
,
169 base::Unretained(this)));
171 EXPECT_CALL(*this, OnOperationCompleted(&op
, _
)).Times(0);
172 op
.Start(&device_settings_test_helper_
, owner_key_util_
, NULL
);
173 device_settings_test_helper_
.FlushLoops();
174 device_settings_test_helper_
.FlushRetrieve();
175 EXPECT_TRUE(op
.owner_key().get());
176 EXPECT_TRUE(op
.owner_key()->public_key());
177 Mock::VerifyAndClearExpectations(this);
179 // Now install a different key and policy and restart the operation.
180 policy_
.SetSigningKey(*policy::PolicyBuilder::CreateTestOtherSigningKey());
181 policy_
.payload().mutable_metrics_enabled()->set_metrics_enabled(true);
183 device_settings_test_helper_
.set_policy_blob(policy_
.GetBlob());
184 owner_key_util_
->SetPrivateKey(policy_
.GetSigningKey());
187 OnOperationCompleted(
188 &op
, DeviceSettingsService::STORE_SUCCESS
));
189 op
.RestartLoad(true);
190 device_settings_test_helper_
.Flush();
191 Mock::VerifyAndClearExpectations(this);
193 // Check that the new keys have been loaded.
194 CheckPublicKeyLoaded(&op
);
195 CheckPrivateKeyLoaded(&op
);
197 // Verify the new policy.
198 ASSERT_TRUE(op
.policy_data().get());
199 EXPECT_EQ(policy_
.policy_data().SerializeAsString(),
200 op
.policy_data()->SerializeAsString());
201 ASSERT_TRUE(op
.device_settings().get());
202 EXPECT_EQ(policy_
.payload().SerializeAsString(),
203 op
.device_settings()->SerializeAsString());
206 TEST_F(SessionManagerOperationTest
, StoreSettings
) {
207 owner_key_util_
->SetPublicKeyFromPrivateKey(*policy_
.GetSigningKey());
208 StoreSettingsOperation
op(
209 base::Bind(&SessionManagerOperationTest::OnOperationCompleted
,
210 base::Unretained(this)),
214 OnOperationCompleted(
215 &op
, DeviceSettingsService::STORE_SUCCESS
));
216 op
.Start(&device_settings_test_helper_
, owner_key_util_
, NULL
);
217 device_settings_test_helper_
.Flush();
218 Mock::VerifyAndClearExpectations(this);
220 EXPECT_EQ(device_settings_test_helper_
.policy_blob(),
222 ASSERT_TRUE(op
.policy_data().get());
223 EXPECT_EQ(policy_
.policy_data().SerializeAsString(),
224 op
.policy_data()->SerializeAsString());
225 ASSERT_TRUE(op
.device_settings().get());
226 EXPECT_EQ(policy_
.payload().SerializeAsString(),
227 op
.device_settings()->SerializeAsString());
230 TEST_F(SessionManagerOperationTest
, SignAndStoreSettings
) {
231 base::Time
before(base::Time::NowFromSystemTime());
232 owner_key_util_
->SetPrivateKey(policy_
.GetSigningKey());
233 SignAndStoreSettingsOperation
op(
234 base::Bind(&SessionManagerOperationTest::OnOperationCompleted
,
235 base::Unretained(this)),
236 scoped_ptr
<em::ChromeDeviceSettingsProto
>(
237 new em::ChromeDeviceSettingsProto(policy_
.payload())),
238 policy_
.policy_data().username());
241 OnOperationCompleted(
242 &op
, DeviceSettingsService::STORE_SUCCESS
));
243 op
.Start(&device_settings_test_helper_
, owner_key_util_
, NULL
);
244 device_settings_test_helper_
.Flush();
245 Mock::VerifyAndClearExpectations(this);
246 base::Time
after(base::Time::NowFromSystemTime());
248 // The blob should validate.
249 scoped_ptr
<em::PolicyFetchResponse
> policy_response(
250 new em::PolicyFetchResponse());
252 policy_response
->ParseFromString(
253 device_settings_test_helper_
.policy_blob()));
254 policy::DeviceCloudPolicyValidator
* validator
=
255 policy::DeviceCloudPolicyValidator::Create(
256 policy_response
.Pass(), message_loop_
.message_loop_proxy());
257 validator
->ValidateUsername(policy_
.policy_data().username());
258 validator
->ValidateTimestamp(
261 policy::CloudPolicyValidatorBase::TIMESTAMP_REQUIRED
);
262 validator
->ValidatePolicyType(policy::dm_protocol::kChromeDevicePolicyType
);
263 validator
->ValidatePayload();
264 std::vector
<uint8
> public_key
;
265 policy_
.GetSigningKey()->ExportPublicKey(&public_key
);
266 validator
->ValidateSignature(public_key
, false);
267 validator
->StartValidation(
268 base::Bind(&SessionManagerOperationTest::CheckSuccessfulValidation
,
269 base::Unretained(this)));
271 message_loop_
.RunUntilIdle();
272 EXPECT_TRUE(validated_
);
274 // Check that the loaded policy_data contains the expected values.
275 EXPECT_EQ(policy::dm_protocol::kChromeDevicePolicyType
,
276 op
.policy_data()->policy_type());
277 EXPECT_LE((before
- base::Time::UnixEpoch()).InMilliseconds(),
278 op
.policy_data()->timestamp());
279 EXPECT_GE((after
- base::Time::UnixEpoch()).InMilliseconds(),
280 op
.policy_data()->timestamp());
281 EXPECT_FALSE(op
.policy_data()->has_request_token());
282 EXPECT_EQ(policy_
.policy_data().username(), op
.policy_data()->username());
284 // Loaded device settings should match what the operation received.
285 ASSERT_TRUE(op
.device_settings().get());
286 EXPECT_EQ(policy_
.payload().SerializeAsString(),
287 op
.device_settings()->SerializeAsString());
290 } // namespace chromeos