[Android] Allow multiple --install in bb_device_steps.py.
[chromium-blink-merge.git] / chrome / browser / chromeos / settings / device_settings_service_unittest.cc
blobe0b4b79bfa87eb57eac22f1cf4fcec10763bd8c1
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/device_settings_service.h"
7 #include "base/basictypes.h"
8 #include "base/bind.h"
9 #include "base/bind_helpers.h"
10 #include "base/compiler_specific.h"
11 #include "base/time/time.h"
12 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos.h"
13 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos_factory.h"
14 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h"
15 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
18 #include "policy/proto/device_management_backend.pb.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 namespace em = enterprise_management;
24 using ::testing::Mock;
26 namespace chromeos {
28 namespace {
30 class MockDeviceSettingsObserver : public DeviceSettingsService::Observer {
31 public:
32 virtual ~MockDeviceSettingsObserver() {}
34 MOCK_METHOD0(OwnershipStatusChanged, void());
35 MOCK_METHOD0(DeviceSettingsUpdated, void());
36 MOCK_METHOD0(OnDeviceSettingsServiceShutdown, void());
39 } // namespace
41 class DeviceSettingsServiceTest : public DeviceSettingsTestBase {
42 public:
43 void SetOperationCompleted() {
44 operation_completed_ = true;
47 void SetOwnershipStatus(
48 DeviceSettingsService::OwnershipStatus ownership_status) {
49 ownership_status_ = ownership_status;
52 void OnIsOwner(bool is_owner) {
53 is_owner_ = is_owner;
54 is_owner_set_ = true;
57 protected:
58 DeviceSettingsServiceTest()
59 : operation_completed_(false),
60 is_owner_(true),
61 is_owner_set_(false),
62 ownership_status_(DeviceSettingsService::OWNERSHIP_UNKNOWN) {}
64 virtual void SetUp() override {
65 device_policy_.payload().mutable_device_policy_refresh_rate()->
66 set_device_policy_refresh_rate(120);
67 DeviceSettingsTestBase::SetUp();
70 void CheckPolicy() {
71 ASSERT_TRUE(device_settings_service_.policy_data());
72 EXPECT_EQ(device_policy_.policy_data().SerializeAsString(),
73 device_settings_service_.policy_data()->SerializeAsString());
74 ASSERT_TRUE(device_settings_service_.device_settings());
75 EXPECT_EQ(device_policy_.payload().SerializeAsString(),
76 device_settings_service_.device_settings()->SerializeAsString());
79 bool operation_completed_;
80 bool is_owner_;
81 bool is_owner_set_;
82 DeviceSettingsService::OwnershipStatus ownership_status_;
84 private:
85 DISALLOW_COPY_AND_ASSIGN(DeviceSettingsServiceTest);
88 TEST_F(DeviceSettingsServiceTest, LoadNoKey) {
89 owner_key_util_->Clear();
90 ReloadDeviceSettings();
92 EXPECT_EQ(DeviceSettingsService::STORE_KEY_UNAVAILABLE,
93 device_settings_service_.status());
94 EXPECT_FALSE(device_settings_service_.policy_data());
95 EXPECT_FALSE(device_settings_service_.device_settings());
98 TEST_F(DeviceSettingsServiceTest, LoadNoPolicy) {
99 device_settings_test_helper_.set_policy_blob(std::string());
100 ReloadDeviceSettings();
102 EXPECT_EQ(DeviceSettingsService::STORE_NO_POLICY,
103 device_settings_service_.status());
104 EXPECT_FALSE(device_settings_service_.policy_data());
105 EXPECT_FALSE(device_settings_service_.device_settings());
108 TEST_F(DeviceSettingsServiceTest, LoadValidationError) {
109 device_policy_.policy().set_policy_data_signature("bad");
110 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
111 owner_key_util_->SetPublicKeyFromPrivateKey(*device_policy_.GetSigningKey());
112 ReloadDeviceSettings();
114 EXPECT_EQ(DeviceSettingsService::STORE_VALIDATION_ERROR,
115 device_settings_service_.status());
116 EXPECT_FALSE(device_settings_service_.policy_data());
117 EXPECT_FALSE(device_settings_service_.device_settings());
120 TEST_F(DeviceSettingsServiceTest, LoadValidationErrorFutureTimestamp) {
121 base::Time timestamp(base::Time::NowFromSystemTime() +
122 base::TimeDelta::FromDays(5000));
123 device_policy_.policy_data().set_timestamp(
124 (timestamp - base::Time::UnixEpoch()).InMilliseconds());
125 device_policy_.Build();
126 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
127 owner_key_util_->SetPublicKeyFromPrivateKey(*device_policy_.GetSigningKey());
128 ReloadDeviceSettings();
130 // Loading a cached device policy with a timestamp in the future should work,
131 // since this may be due to a broken clock on the client device.
132 EXPECT_EQ(DeviceSettingsService::STORE_SUCCESS,
133 device_settings_service_.status());
134 CheckPolicy();
137 TEST_F(DeviceSettingsServiceTest, LoadSuccess) {
138 ReloadDeviceSettings();
140 EXPECT_EQ(DeviceSettingsService::STORE_SUCCESS,
141 device_settings_service_.status());
142 CheckPolicy();
145 TEST_F(DeviceSettingsServiceTest, SetManagementSettingsModeTransition) {
146 ReloadDeviceSettings();
147 EXPECT_EQ(DeviceSettingsService::STORE_SUCCESS,
148 device_settings_service_.status());
150 owner_key_util_->SetPrivateKey(device_policy_.GetSigningKey());
151 InitOwner(device_policy_.policy_data().username(), true);
152 FlushDeviceSettings();
154 // The initial management mode should be LOCAL_OWNER.
155 EXPECT_EQ(em::PolicyData::LOCAL_OWNER,
156 device_settings_service_.policy_data()->management_mode());
158 // LOCAL_OWNER -> CONSUMER_MANAGED: Okay.
159 device_settings_service_.SetManagementSettings(
160 em::PolicyData::CONSUMER_MANAGED,
161 "fake_request_token",
162 "fake_device_id",
163 base::Bind(&DeviceSettingsServiceTest::SetOperationCompleted,
164 base::Unretained(this)));
165 FlushDeviceSettings();
167 EXPECT_TRUE(operation_completed_);
168 EXPECT_EQ(DeviceSettingsService::STORE_SUCCESS,
169 device_settings_service_.status());
170 EXPECT_EQ(em::PolicyData::CONSUMER_MANAGED,
171 device_settings_service_.policy_data()->management_mode());
173 // CONSUMER_MANAGED -> ENTERPRISE_MANAGED: Invalid.
174 device_settings_service_.SetManagementSettings(
175 em::PolicyData::ENTERPRISE_MANAGED,
176 "fake_request_token",
177 "fake_device_id",
178 base::Bind(&DeviceSettingsServiceTest::SetOperationCompleted,
179 base::Unretained(this)));
180 FlushDeviceSettings();
182 EXPECT_TRUE(operation_completed_);
183 EXPECT_EQ(DeviceSettingsService::STORE_POLICY_ERROR,
184 device_settings_service_.status());
185 EXPECT_EQ(em::PolicyData::CONSUMER_MANAGED,
186 device_settings_service_.policy_data()->management_mode());
188 // CONSUMER_MANAGED -> LOCAL_OWNER: Okay.
189 device_settings_service_.SetManagementSettings(
190 em::PolicyData::LOCAL_OWNER,
191 "fake_request_token",
192 "fake_device_id",
193 base::Bind(&DeviceSettingsServiceTest::SetOperationCompleted,
194 base::Unretained(this)));
195 FlushDeviceSettings();
197 EXPECT_TRUE(operation_completed_);
198 EXPECT_EQ(DeviceSettingsService::STORE_SUCCESS,
199 device_settings_service_.status());
200 EXPECT_EQ(em::PolicyData::LOCAL_OWNER,
201 device_settings_service_.policy_data()->management_mode());
203 // LOCAL_OWNER -> ENTERPRISE_MANAGED: Invalid.
204 device_settings_service_.SetManagementSettings(
205 em::PolicyData::ENTERPRISE_MANAGED,
206 "fake_request_token",
207 "fake_device_id",
208 base::Bind(&DeviceSettingsServiceTest::SetOperationCompleted,
209 base::Unretained(this)));
210 FlushDeviceSettings();
212 EXPECT_TRUE(operation_completed_);
213 EXPECT_EQ(DeviceSettingsService::STORE_POLICY_ERROR,
214 device_settings_service_.status());
215 EXPECT_EQ(em::PolicyData::LOCAL_OWNER,
216 device_settings_service_.policy_data()->management_mode());
218 // Inject a policy data with management mode set to ENTERPRISE_MANAGED.
219 device_policy_.policy_data().set_management_mode(
220 em::PolicyData::ENTERPRISE_MANAGED);
221 device_policy_.Build();
222 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
223 ReloadDeviceSettings();
224 EXPECT_EQ(em::PolicyData::ENTERPRISE_MANAGED,
225 device_settings_service_.policy_data()->management_mode());
227 // ENTERPRISE_MANAGED -> LOCAL_OWNER: Invalid.
228 device_settings_service_.SetManagementSettings(
229 em::PolicyData::LOCAL_OWNER,
230 "fake_request_token",
231 "fake_device_id",
232 base::Bind(&DeviceSettingsServiceTest::SetOperationCompleted,
233 base::Unretained(this)));
234 FlushDeviceSettings();
236 EXPECT_TRUE(operation_completed_);
237 EXPECT_EQ(DeviceSettingsService::STORE_POLICY_ERROR,
238 device_settings_service_.status());
239 EXPECT_EQ(em::PolicyData::ENTERPRISE_MANAGED,
240 device_settings_service_.policy_data()->management_mode());
242 // ENTERPRISE_MANAGED -> CONSUMER_MANAGED: Invalid.
243 device_settings_service_.SetManagementSettings(
244 em::PolicyData::CONSUMER_MANAGED,
245 "fake_request_token",
246 "fake_device_id",
247 base::Bind(&DeviceSettingsServiceTest::SetOperationCompleted,
248 base::Unretained(this)));
249 FlushDeviceSettings();
251 EXPECT_TRUE(operation_completed_);
252 EXPECT_EQ(DeviceSettingsService::STORE_POLICY_ERROR,
253 device_settings_service_.status());
254 EXPECT_EQ(em::PolicyData::ENTERPRISE_MANAGED,
255 device_settings_service_.policy_data()->management_mode());
259 TEST_F(DeviceSettingsServiceTest, SetManagementSettingsSuccess) {
260 ReloadDeviceSettings();
261 EXPECT_EQ(DeviceSettingsService::STORE_SUCCESS,
262 device_settings_service_.status());
264 owner_key_util_->SetPrivateKey(device_policy_.GetSigningKey());
265 InitOwner(device_policy_.policy_data().username(), true);
266 FlushDeviceSettings();
268 device_settings_service_.SetManagementSettings(
269 em::PolicyData::CONSUMER_MANAGED,
270 "fake_request_token",
271 "fake_device_id",
272 base::Bind(&DeviceSettingsServiceTest::SetOperationCompleted,
273 base::Unretained(this)));
274 FlushDeviceSettings();
276 EXPECT_TRUE(operation_completed_);
277 EXPECT_EQ(DeviceSettingsService::STORE_SUCCESS,
278 device_settings_service_.status());
279 ASSERT_TRUE(device_settings_service_.device_settings());
281 // Check that the loaded policy_data contains the expected values.
282 const em::PolicyData* policy_data = device_settings_service_.policy_data();
283 EXPECT_EQ(policy::dm_protocol::kChromeDevicePolicyType,
284 policy_data->policy_type());
285 EXPECT_EQ(device_settings_service_.GetUsername(),
286 policy_data->username());
287 EXPECT_EQ(em::PolicyData::CONSUMER_MANAGED, policy_data->management_mode());
288 EXPECT_EQ("fake_request_token", policy_data->request_token());
289 EXPECT_EQ("fake_device_id", policy_data->device_id());
292 TEST_F(DeviceSettingsServiceTest, StoreFailure) {
293 owner_key_util_->Clear();
294 device_settings_test_helper_.set_policy_blob(std::string());
295 ReloadDeviceSettings();
296 EXPECT_EQ(DeviceSettingsService::STORE_KEY_UNAVAILABLE,
297 device_settings_service_.status());
299 device_settings_test_helper_.set_store_result(false);
300 device_settings_service_.Store(
301 device_policy_.GetCopy(),
302 base::Bind(&DeviceSettingsServiceTest::SetOperationCompleted,
303 base::Unretained(this)));
304 FlushDeviceSettings();
305 EXPECT_TRUE(operation_completed_);
306 EXPECT_EQ(DeviceSettingsService::STORE_OPERATION_FAILED,
307 device_settings_service_.status());
310 TEST_F(DeviceSettingsServiceTest, StoreSuccess) {
311 owner_key_util_->Clear();
312 device_settings_test_helper_.set_policy_blob(std::string());
313 ReloadDeviceSettings();
314 EXPECT_EQ(DeviceSettingsService::STORE_KEY_UNAVAILABLE,
315 device_settings_service_.status());
317 owner_key_util_->SetPublicKeyFromPrivateKey(*device_policy_.GetSigningKey());
318 InitOwner(device_policy_.policy_data().username(), true);
319 device_settings_service_.Store(
320 device_policy_.GetCopy(),
321 base::Bind(&DeviceSettingsServiceTest::SetOperationCompleted,
322 base::Unretained(this)));
323 FlushDeviceSettings();
324 EXPECT_TRUE(operation_completed_);
325 EXPECT_EQ(DeviceSettingsService::STORE_SUCCESS,
326 device_settings_service_.status());
327 CheckPolicy();
330 TEST_F(DeviceSettingsServiceTest, StoreRotation) {
331 ReloadDeviceSettings();
332 EXPECT_EQ(DeviceSettingsService::STORE_SUCCESS,
333 device_settings_service_.status());
335 device_policy_.payload().mutable_device_policy_refresh_rate()->
336 set_device_policy_refresh_rate(300);
337 device_policy_.SetDefaultNewSigningKey();
338 device_policy_.Build();
339 device_settings_service_.Store(device_policy_.GetCopy(), base::Closure());
340 FlushDeviceSettings();
341 owner_key_util_->SetPublicKeyFromPrivateKey(
342 *device_policy_.GetNewSigningKey());
343 device_settings_service_.OwnerKeySet(true);
344 FlushDeviceSettings();
345 EXPECT_EQ(DeviceSettingsService::STORE_SUCCESS,
346 device_settings_service_.status());
347 CheckPolicy();
349 // Check the new key has been loaded.
350 std::vector<uint8> key;
351 ASSERT_TRUE(device_policy_.GetNewSigningKey()->ExportPublicKey(&key));
352 EXPECT_EQ(device_settings_service_.GetPublicKey()->data(), key);
355 TEST_F(DeviceSettingsServiceTest, OwnershipStatus) {
356 owner_key_util_->Clear();
358 EXPECT_FALSE(device_settings_service_.HasPrivateOwnerKey());
359 EXPECT_FALSE(device_settings_service_.GetPublicKey().get());
360 EXPECT_EQ(DeviceSettingsService::OWNERSHIP_UNKNOWN,
361 device_settings_service_.GetOwnershipStatus());
363 device_settings_service_.GetOwnershipStatusAsync(
364 base::Bind(&DeviceSettingsServiceTest::SetOwnershipStatus,
365 base::Unretained(this)));
366 FlushDeviceSettings();
367 EXPECT_FALSE(device_settings_service_.HasPrivateOwnerKey());
368 ASSERT_TRUE(device_settings_service_.GetPublicKey().get());
369 EXPECT_FALSE(device_settings_service_.GetPublicKey()->is_loaded());
370 EXPECT_EQ(DeviceSettingsService::OWNERSHIP_NONE,
371 device_settings_service_.GetOwnershipStatus());
372 EXPECT_EQ(DeviceSettingsService::OWNERSHIP_NONE, ownership_status_);
374 owner_key_util_->SetPublicKeyFromPrivateKey(*device_policy_.GetSigningKey());
375 ReloadDeviceSettings();
376 device_settings_service_.GetOwnershipStatusAsync(
377 base::Bind(&DeviceSettingsServiceTest::SetOwnershipStatus,
378 base::Unretained(this)));
379 FlushDeviceSettings();
380 EXPECT_FALSE(device_settings_service_.HasPrivateOwnerKey());
381 ASSERT_TRUE(device_settings_service_.GetPublicKey().get());
382 ASSERT_TRUE(device_settings_service_.GetPublicKey()->is_loaded());
383 std::vector<uint8> key;
384 ASSERT_TRUE(device_policy_.GetSigningKey()->ExportPublicKey(&key));
385 EXPECT_EQ(device_settings_service_.GetPublicKey()->data(), key);
386 EXPECT_EQ(DeviceSettingsService::OWNERSHIP_TAKEN,
387 device_settings_service_.GetOwnershipStatus());
388 EXPECT_EQ(DeviceSettingsService::OWNERSHIP_TAKEN, ownership_status_);
390 owner_key_util_->SetPrivateKey(device_policy_.GetSigningKey());
391 InitOwner(device_policy_.policy_data().username(), true);
392 device_settings_service_.GetOwnershipStatusAsync(
393 base::Bind(&DeviceSettingsServiceTest::SetOwnershipStatus,
394 base::Unretained(this)));
395 FlushDeviceSettings();
396 EXPECT_TRUE(device_settings_service_.HasPrivateOwnerKey());
397 ASSERT_TRUE(device_settings_service_.GetPublicKey().get());
398 ASSERT_TRUE(device_settings_service_.GetPublicKey()->is_loaded());
399 ASSERT_TRUE(device_policy_.GetSigningKey()->ExportPublicKey(&key));
400 EXPECT_EQ(device_settings_service_.GetPublicKey()->data(), key);
401 EXPECT_EQ(DeviceSettingsService::OWNERSHIP_TAKEN,
402 device_settings_service_.GetOwnershipStatus());
403 EXPECT_EQ(DeviceSettingsService::OWNERSHIP_TAKEN, ownership_status_);
406 TEST_F(DeviceSettingsServiceTest, OnTPMTokenReadyForNonOwner) {
407 owner_key_util_->Clear();
409 EXPECT_FALSE(device_settings_service_.HasPrivateOwnerKey());
410 EXPECT_FALSE(device_settings_service_.GetPublicKey().get());
411 EXPECT_EQ(DeviceSettingsService::OWNERSHIP_UNKNOWN,
412 device_settings_service_.GetOwnershipStatus());
414 const std::string& user_id = device_policy_.policy_data().username();
415 InitOwner(user_id, false);
416 OwnerSettingsServiceChromeOS* service =
417 OwnerSettingsServiceChromeOSFactory::GetForBrowserContext(profile_.get());
418 ASSERT_TRUE(service);
419 service->IsOwnerAsync(base::Bind(&DeviceSettingsServiceTest::OnIsOwner,
420 base::Unretained(this)));
422 owner_key_util_->SetPublicKeyFromPrivateKey(*device_policy_.GetSigningKey());
423 ReloadDeviceSettings();
425 EXPECT_FALSE(device_settings_service_.HasPrivateOwnerKey());
426 ASSERT_TRUE(device_settings_service_.GetPublicKey().get());
427 ASSERT_TRUE(device_settings_service_.GetPublicKey()->is_loaded());
428 std::vector<uint8> key;
429 ASSERT_TRUE(device_policy_.GetSigningKey()->ExportPublicKey(&key));
430 EXPECT_EQ(device_settings_service_.GetPublicKey()->data(), key);
431 EXPECT_EQ(DeviceSettingsService::OWNERSHIP_TAKEN,
432 device_settings_service_.GetOwnershipStatus());
433 EXPECT_FALSE(is_owner_set_);
435 service->OnTPMTokenReady(true /* is ready */);
436 FlushDeviceSettings();
438 EXPECT_FALSE(device_settings_service_.HasPrivateOwnerKey());
439 ASSERT_TRUE(device_settings_service_.GetPublicKey().get());
440 ASSERT_TRUE(device_settings_service_.GetPublicKey()->is_loaded());
441 ASSERT_TRUE(device_policy_.GetSigningKey()->ExportPublicKey(&key));
442 EXPECT_EQ(device_settings_service_.GetPublicKey()->data(), key);
443 EXPECT_FALSE(device_settings_service_.HasPrivateOwnerKey());
444 EXPECT_EQ(DeviceSettingsService::OWNERSHIP_TAKEN,
445 device_settings_service_.GetOwnershipStatus());
446 EXPECT_TRUE(is_owner_set_);
447 EXPECT_FALSE(is_owner_);
450 TEST_F(DeviceSettingsServiceTest, OwnerPrivateKeyInTPMToken) {
451 owner_key_util_->Clear();
453 EXPECT_FALSE(device_settings_service_.HasPrivateOwnerKey());
454 EXPECT_FALSE(device_settings_service_.GetPublicKey().get());
455 EXPECT_EQ(DeviceSettingsService::OWNERSHIP_UNKNOWN,
456 device_settings_service_.GetOwnershipStatus());
458 const std::string& user_id = device_policy_.policy_data().username();
459 owner_key_util_->SetPublicKeyFromPrivateKey(*device_policy_.GetSigningKey());
460 InitOwner(user_id, false);
461 OwnerSettingsServiceChromeOS* service =
462 OwnerSettingsServiceChromeOSFactory::GetForBrowserContext(profile_.get());
463 ASSERT_TRUE(service);
464 ReloadDeviceSettings();
466 EXPECT_FALSE(device_settings_service_.HasPrivateOwnerKey());
467 ASSERT_TRUE(device_settings_service_.GetPublicKey().get());
468 ASSERT_TRUE(device_settings_service_.GetPublicKey()->is_loaded());
469 std::vector<uint8> key;
470 ASSERT_TRUE(device_policy_.GetSigningKey()->ExportPublicKey(&key));
471 EXPECT_EQ(device_settings_service_.GetPublicKey()->data(), key);
472 EXPECT_EQ(DeviceSettingsService::OWNERSHIP_TAKEN,
473 device_settings_service_.GetOwnershipStatus());
475 owner_key_util_->SetPrivateKey(device_policy_.GetSigningKey());
476 service->OnTPMTokenReady(true /* is ready */);
477 FlushDeviceSettings();
479 EXPECT_TRUE(device_settings_service_.HasPrivateOwnerKey());
480 ASSERT_TRUE(device_settings_service_.GetPublicKey().get());
481 ASSERT_TRUE(device_settings_service_.GetPublicKey()->is_loaded());
482 ASSERT_TRUE(device_policy_.GetSigningKey()->ExportPublicKey(&key));
483 EXPECT_EQ(device_settings_service_.GetPublicKey()->data(), key);
486 TEST_F(DeviceSettingsServiceTest, OnTPMTokenReadyForOwner) {
487 owner_key_util_->Clear();
489 EXPECT_FALSE(device_settings_service_.HasPrivateOwnerKey());
490 EXPECT_FALSE(device_settings_service_.GetPublicKey().get());
491 EXPECT_EQ(DeviceSettingsService::OWNERSHIP_UNKNOWN,
492 device_settings_service_.GetOwnershipStatus());
494 const std::string& user_id = device_policy_.policy_data().username();
495 owner_key_util_->SetPublicKeyFromPrivateKey(*device_policy_.GetSigningKey());
496 InitOwner(user_id, false);
497 OwnerSettingsServiceChromeOS* service =
498 OwnerSettingsServiceChromeOSFactory::GetForBrowserContext(profile_.get());
499 ASSERT_TRUE(service);
500 service->IsOwnerAsync(base::Bind(&DeviceSettingsServiceTest::OnIsOwner,
501 base::Unretained(this)));
502 ReloadDeviceSettings();
504 EXPECT_FALSE(device_settings_service_.HasPrivateOwnerKey());
505 ASSERT_TRUE(device_settings_service_.GetPublicKey().get());
506 ASSERT_TRUE(device_settings_service_.GetPublicKey()->is_loaded());
507 std::vector<uint8> key;
508 ASSERT_TRUE(device_policy_.GetSigningKey()->ExportPublicKey(&key));
509 EXPECT_EQ(device_settings_service_.GetPublicKey()->data(), key);
510 EXPECT_EQ(DeviceSettingsService::OWNERSHIP_TAKEN,
511 device_settings_service_.GetOwnershipStatus());
512 EXPECT_FALSE(is_owner_set_);
514 owner_key_util_->SetPrivateKey(device_policy_.GetSigningKey());
515 service->OnTPMTokenReady(true /* is ready */);
516 FlushDeviceSettings();
518 EXPECT_TRUE(device_settings_service_.HasPrivateOwnerKey());
519 ASSERT_TRUE(device_settings_service_.GetPublicKey().get());
520 ASSERT_TRUE(device_settings_service_.GetPublicKey()->is_loaded());
521 ASSERT_TRUE(device_policy_.GetSigningKey()->ExportPublicKey(&key));
522 EXPECT_EQ(device_settings_service_.GetPublicKey()->data(), key);
523 EXPECT_EQ(DeviceSettingsService::OWNERSHIP_TAKEN,
524 device_settings_service_.GetOwnershipStatus());
525 EXPECT_TRUE(is_owner_set_);
526 EXPECT_TRUE(is_owner_);
529 TEST_F(DeviceSettingsServiceTest, IsCurrentUserOwnerAsyncWithLoadedCerts) {
530 owner_key_util_->Clear();
532 EXPECT_FALSE(device_settings_service_.HasPrivateOwnerKey());
533 EXPECT_FALSE(device_settings_service_.GetPublicKey().get());
534 EXPECT_EQ(DeviceSettingsService::OWNERSHIP_UNKNOWN,
535 device_settings_service_.GetOwnershipStatus());
537 owner_key_util_->SetPublicKeyFromPrivateKey(*device_policy_.GetSigningKey());
538 owner_key_util_->SetPrivateKey(device_policy_.GetSigningKey());
540 InitOwner(device_policy_.policy_data().username(), true);
541 ReloadDeviceSettings();
542 FlushDeviceSettings();
544 EXPECT_TRUE(device_settings_service_.HasPrivateOwnerKey());
545 ASSERT_TRUE(device_settings_service_.GetPublicKey().get());
546 ASSERT_TRUE(device_settings_service_.GetPublicKey()->is_loaded());
547 std::vector<uint8> key;
548 ASSERT_TRUE(device_policy_.GetSigningKey()->ExportPublicKey(&key));
549 EXPECT_EQ(device_settings_service_.GetPublicKey()->data(), key);
550 EXPECT_EQ(DeviceSettingsService::OWNERSHIP_TAKEN,
551 device_settings_service_.GetOwnershipStatus());
552 EXPECT_FALSE(is_owner_set_);
554 OwnerSettingsServiceChromeOS* service =
555 OwnerSettingsServiceChromeOSFactory::GetForBrowserContext(profile_.get());
556 ASSERT_TRUE(service);
557 service->IsOwnerAsync(base::Bind(&DeviceSettingsServiceTest::OnIsOwner,
558 base::Unretained(this)));
559 // The callback should be called immediately.
560 base::MessageLoop::current()->RunUntilIdle();
562 EXPECT_TRUE(device_settings_service_.HasPrivateOwnerKey());
563 ASSERT_TRUE(device_settings_service_.GetPublicKey().get());
564 ASSERT_TRUE(device_settings_service_.GetPublicKey()->is_loaded());
565 ASSERT_TRUE(device_policy_.GetSigningKey()->ExportPublicKey(&key));
566 EXPECT_EQ(device_settings_service_.GetPublicKey()->data(), key);
567 EXPECT_EQ(DeviceSettingsService::OWNERSHIP_TAKEN,
568 device_settings_service_.GetOwnershipStatus());
569 EXPECT_TRUE(is_owner_set_);
570 EXPECT_TRUE(is_owner_);
573 TEST_F(DeviceSettingsServiceTest, Observer) {
574 owner_key_util_->Clear();
575 MockDeviceSettingsObserver observer_;
576 device_settings_service_.AddObserver(&observer_);
578 EXPECT_CALL(observer_, OwnershipStatusChanged()).Times(1);
579 EXPECT_CALL(observer_, DeviceSettingsUpdated()).Times(1);
580 ReloadDeviceSettings();
581 Mock::VerifyAndClearExpectations(&observer_);
583 EXPECT_CALL(observer_, OwnershipStatusChanged()).Times(1);
584 EXPECT_CALL(observer_, DeviceSettingsUpdated()).Times(1);
585 owner_key_util_->SetPublicKeyFromPrivateKey(*device_policy_.GetSigningKey());
586 InitOwner(device_policy_.policy_data().username(), true);
587 ReloadDeviceSettings();
588 Mock::VerifyAndClearExpectations(&observer_);
590 EXPECT_CALL(observer_, OwnershipStatusChanged()).Times(0);
591 EXPECT_CALL(observer_, DeviceSettingsUpdated()).Times(1);
592 device_settings_service_.Store(device_policy_.GetCopy(), base::Closure());
593 FlushDeviceSettings();
594 Mock::VerifyAndClearExpectations(&observer_);
596 EXPECT_CALL(observer_, OwnershipStatusChanged()).Times(0);
597 EXPECT_CALL(observer_, DeviceSettingsUpdated()).Times(1);
598 device_settings_service_.PropertyChangeComplete(true);
599 FlushDeviceSettings();
600 Mock::VerifyAndClearExpectations(&observer_);
602 device_settings_service_.RemoveObserver(&observer_);
605 } // namespace chromeos