| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/policy/device_cloud_policy_manager_chromeos.h" | 5 #include "chrome/browser/chromeos/policy/device_cloud_policy_manager_chromeos.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 #include "chrome/test/base/testing_profile.h" | 33 #include "chrome/test/base/testing_profile.h" |
| 34 #include "chromeos/cryptohome/system_salt_getter.h" | 34 #include "chromeos/cryptohome/system_salt_getter.h" |
| 35 #include "chromeos/dbus/dbus_client_implementation_type.h" | 35 #include "chromeos/dbus/dbus_client_implementation_type.h" |
| 36 #include "chromeos/dbus/dbus_thread_manager.h" | 36 #include "chromeos/dbus/dbus_thread_manager.h" |
| 37 #include "chromeos/dbus/fake_cryptohome_client.h" | 37 #include "chromeos/dbus/fake_cryptohome_client.h" |
| 38 #include "chromeos/dbus/fake_session_manager_client.h" | 38 #include "chromeos/dbus/fake_session_manager_client.h" |
| 39 #include "chromeos/system/fake_statistics_provider.h" | 39 #include "chromeos/system/fake_statistics_provider.h" |
| 40 #include "chromeos/system/statistics_provider.h" | 40 #include "chromeos/system/statistics_provider.h" |
| 41 #include "components/policy/core/common/cloud/cloud_policy_client.h" | 41 #include "components/policy/core/common/cloud/cloud_policy_client.h" |
| 42 #include "components/policy/core/common/cloud/cloud_policy_constants.h" | 42 #include "components/policy/core/common/cloud/cloud_policy_constants.h" |
| 43 #include "components/policy/core/common/cloud/cloud_policy_core.h" | |
| 44 #include "components/policy/core/common/cloud/mock_device_management_service.h" | 43 #include "components/policy/core/common/cloud/mock_device_management_service.h" |
| 45 #include "components/policy/core/common/external_data_fetcher.h" | 44 #include "components/policy/core/common/external_data_fetcher.h" |
| 46 #include "components/policy/core/common/schema_registry.h" | 45 #include "components/policy/core/common/schema_registry.h" |
| 47 #include "google_apis/gaia/gaia_oauth_client.h" | 46 #include "google_apis/gaia/gaia_oauth_client.h" |
| 48 #include "net/url_request/test_url_fetcher_factory.h" | 47 #include "net/url_request/test_url_fetcher_factory.h" |
| 49 #include "net/url_request/url_request_test_util.h" | 48 #include "net/url_request/url_request_test_util.h" |
| 50 #include "policy/policy_constants.h" | 49 #include "policy/policy_constants.h" |
| 51 #include "policy/proto/device_management_backend.pb.h" | 50 #include "policy/proto/device_management_backend.pb.h" |
| 52 #include "testing/gmock/include/gmock/gmock.h" | 51 #include "testing/gmock/include/gmock/gmock.h" |
| 53 #include "testing/gtest/include/gtest/gtest.h" | 52 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) | 190 bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) |
| 192 .Set(key::kDeviceMetricsReportingEnabled, | 191 .Set(key::kDeviceMetricsReportingEnabled, |
| 193 POLICY_LEVEL_MANDATORY, | 192 POLICY_LEVEL_MANDATORY, |
| 194 POLICY_SCOPE_MACHINE, | 193 POLICY_SCOPE_MACHINE, |
| 195 new base::FundamentalValue(false), | 194 new base::FundamentalValue(false), |
| 196 NULL); | 195 NULL); |
| 197 EXPECT_TRUE(manager_->policies().Equals(bundle)); | 196 EXPECT_TRUE(manager_->policies().Equals(bundle)); |
| 198 } | 197 } |
| 199 | 198 |
| 200 MOCK_METHOD0(OnDeviceCloudPolicyManagerConnected, void()); | 199 MOCK_METHOD0(OnDeviceCloudPolicyManagerConnected, void()); |
| 201 MOCK_METHOD0(OnDeviceCloudPolicyManagerDisconnected, void()); | |
| 202 | 200 |
| 203 scoped_ptr<EnterpriseInstallAttributes> install_attributes_; | 201 scoped_ptr<EnterpriseInstallAttributes> install_attributes_; |
| 204 | 202 |
| 205 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 203 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 206 net::TestURLFetcherFactory url_fetcher_factory_; | 204 net::TestURLFetcherFactory url_fetcher_factory_; |
| 207 int url_fetcher_response_code_; | 205 int url_fetcher_response_code_; |
| 208 std::string url_fetcher_response_string_; | 206 std::string url_fetcher_response_string_; |
| 209 TestingPrefServiceSimple local_state_; | 207 TestingPrefServiceSimple local_state_; |
| 210 MockDeviceManagementService device_management_service_; | 208 MockDeviceManagementService device_management_service_; |
| 211 MockDeviceManagementService consumer_device_management_service_; | 209 MockDeviceManagementService consumer_device_management_service_; |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 | 319 |
| 322 ConnectManager(); | 320 ConnectManager(); |
| 323 EXPECT_TRUE(manager_->policies().Equals(bundle)); | 321 EXPECT_TRUE(manager_->policies().Equals(bundle)); |
| 324 // Should not create a status provider for reporting on consumer devices. | 322 // Should not create a status provider for reporting on consumer devices. |
| 325 EXPECT_FALSE(manager_->HasStatusProvider()); | 323 EXPECT_FALSE(manager_->HasStatusProvider()); |
| 326 | 324 |
| 327 manager_->Shutdown(); | 325 manager_->Shutdown(); |
| 328 EXPECT_TRUE(manager_->policies().Equals(bundle)); | 326 EXPECT_TRUE(manager_->policies().Equals(bundle)); |
| 329 } | 327 } |
| 330 | 328 |
| 331 TEST_F(DeviceCloudPolicyManagerChromeOSTest, ConnectAndDisconnect) { | 329 TEST_F(DeviceCloudPolicyManagerChromeOSTest, ObserverIsNotifiedOnConnected) { |
| 332 LockDevice(); | 330 LockDevice(); |
| 333 FlushDeviceSettings(); | 331 FlushDeviceSettings(); |
| 334 EXPECT_FALSE(manager_->core()->service()); // Not connected. | |
| 335 | 332 |
| 336 // Connect the manager. | |
| 337 MockDeviceManagementJob* policy_fetch_job = nullptr; | 333 MockDeviceManagementJob* policy_fetch_job = nullptr; |
| 338 EXPECT_CALL(device_management_service_, | 334 EXPECT_CALL(device_management_service_, |
| 339 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH, _)) | 335 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH, _)) |
| 340 .WillOnce(device_management_service_.CreateAsyncJob(&policy_fetch_job)); | 336 .WillOnce(device_management_service_.CreateAsyncJob(&policy_fetch_job)); |
| 341 EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _)); | 337 EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _)); |
| 342 EXPECT_CALL(*this, OnDeviceCloudPolicyManagerConnected()); | 338 EXPECT_CALL(*this, OnDeviceCloudPolicyManagerConnected()); |
| 339 |
| 343 ConnectManager(); | 340 ConnectManager(); |
| 344 base::RunLoop().RunUntilIdle(); | 341 base::RunLoop().RunUntilIdle(); |
| 345 Mock::VerifyAndClearExpectations(&device_management_service_); | |
| 346 Mock::VerifyAndClearExpectations(this); | |
| 347 EXPECT_TRUE(manager_->core()->service()); // Connected. | |
| 348 | |
| 349 // Disconnect the manager. | |
| 350 EXPECT_CALL(*this, OnDeviceCloudPolicyManagerDisconnected()); | |
| 351 manager_->Disconnect(); | |
| 352 EXPECT_FALSE(manager_->core()->service()); // Not connnected. | |
| 353 } | 342 } |
| 354 | 343 |
| 355 class DeviceCloudPolicyManagerChromeOSEnrollmentTest | 344 class DeviceCloudPolicyManagerChromeOSEnrollmentTest |
| 356 : public DeviceCloudPolicyManagerChromeOSTest { | 345 : public DeviceCloudPolicyManagerChromeOSTest { |
| 357 public: | 346 public: |
| 358 void Done(EnrollmentStatus status) { | 347 void Done(EnrollmentStatus status) { |
| 359 status_ = status; | 348 status_ = status; |
| 360 done_ = true; | 349 done_ = true; |
| 361 } | 350 } |
| 362 | 351 |
| 363 MOCK_METHOD1(OnUnregistered, void(bool)); | |
| 364 | |
| 365 protected: | 352 protected: |
| 366 DeviceCloudPolicyManagerChromeOSEnrollmentTest() | 353 DeviceCloudPolicyManagerChromeOSEnrollmentTest() |
| 367 : management_mode_(MANAGEMENT_MODE_ENTERPRISE_MANAGED), | 354 : management_mode_(MANAGEMENT_MODE_ENTERPRISE_MANAGED), |
| 368 register_status_(DM_STATUS_SUCCESS), | 355 register_status_(DM_STATUS_SUCCESS), |
| 369 policy_fetch_status_(DM_STATUS_SUCCESS), | 356 policy_fetch_status_(DM_STATUS_SUCCESS), |
| 370 robot_auth_fetch_status_(DM_STATUS_SUCCESS), | 357 robot_auth_fetch_status_(DM_STATUS_SUCCESS), |
| 371 store_result_(true), | 358 store_result_(true), |
| 372 status_(EnrollmentStatus::ForStatus(EnrollmentStatus::STATUS_SUCCESS)), | 359 status_(EnrollmentStatus::ForStatus(EnrollmentStatus::STATUS_SUCCESS)), |
| 373 done_(false) {} | 360 done_(false) {} |
| 374 | 361 |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 668 | 655 |
| 669 device_policy_.policy_data().set_management_mode(em::PolicyData::LOCAL_OWNER); | 656 device_policy_.policy_data().set_management_mode(em::PolicyData::LOCAL_OWNER); |
| 670 device_policy_.Build(); | 657 device_policy_.Build(); |
| 671 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob()); | 658 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob()); |
| 672 ReloadDeviceSettings(); | 659 ReloadDeviceSettings(); |
| 673 | 660 |
| 674 RunTest(); | 661 RunTest(); |
| 675 ExpectSuccessfulEnrollment(); | 662 ExpectSuccessfulEnrollment(); |
| 676 } | 663 } |
| 677 | 664 |
| 678 TEST_F(DeviceCloudPolicyManagerChromeOSEnrollmentTest, UnregisterSucceeds) { | |
| 679 // Enroll first. | |
| 680 RunTest(); | |
| 681 ExpectSuccessfulEnrollment(); | |
| 682 | |
| 683 // Set up mock objects for the upcoming unregistration job. | |
| 684 em::DeviceManagementResponse response; | |
| 685 response.mutable_unregister_response(); | |
| 686 EXPECT_CALL(device_management_service_, | |
| 687 CreateJob(DeviceManagementRequestJob::TYPE_UNREGISTRATION, _)) | |
| 688 .WillOnce(device_management_service_.SucceedJob(response)); | |
| 689 EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _)); | |
| 690 EXPECT_CALL(*this, OnUnregistered(true)); | |
| 691 | |
| 692 // Start unregistering. | |
| 693 manager_->Unregister(base::Bind( | |
| 694 &DeviceCloudPolicyManagerChromeOSEnrollmentTest::OnUnregistered, | |
| 695 base::Unretained(this))); | |
| 696 } | |
| 697 | |
| 698 TEST_F(DeviceCloudPolicyManagerChromeOSEnrollmentTest, UnregisterFails) { | |
| 699 // Enroll first. | |
| 700 RunTest(); | |
| 701 ExpectSuccessfulEnrollment(); | |
| 702 | |
| 703 // Set up mock objects for the upcoming unregistration job. | |
| 704 EXPECT_CALL(device_management_service_, | |
| 705 CreateJob(DeviceManagementRequestJob::TYPE_UNREGISTRATION, _)) | |
| 706 .WillOnce(device_management_service_.FailJob(DM_STATUS_REQUEST_FAILED)); | |
| 707 EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _)); | |
| 708 EXPECT_CALL(*this, OnUnregistered(false)); | |
| 709 | |
| 710 // Start unregistering. | |
| 711 manager_->Unregister(base::Bind( | |
| 712 &DeviceCloudPolicyManagerChromeOSEnrollmentTest::OnUnregistered, | |
| 713 base::Unretained(this))); | |
| 714 } | |
| 715 | |
| 716 // A subclass that runs with a blank system salt. | 665 // A subclass that runs with a blank system salt. |
| 717 class DeviceCloudPolicyManagerChromeOSEnrollmentBlankSystemSaltTest | 666 class DeviceCloudPolicyManagerChromeOSEnrollmentBlankSystemSaltTest |
| 718 : public DeviceCloudPolicyManagerChromeOSEnrollmentTest { | 667 : public DeviceCloudPolicyManagerChromeOSEnrollmentTest { |
| 719 protected: | 668 protected: |
| 720 DeviceCloudPolicyManagerChromeOSEnrollmentBlankSystemSaltTest() { | 669 DeviceCloudPolicyManagerChromeOSEnrollmentBlankSystemSaltTest() { |
| 721 // Set up a FakeCryptohomeClient with a blank system salt. | 670 // Set up a FakeCryptohomeClient with a blank system salt. |
| 722 fake_cryptohome_client_->set_system_salt(std::vector<uint8>()); | 671 fake_cryptohome_client_->set_system_salt(std::vector<uint8>()); |
| 723 } | 672 } |
| 724 }; | 673 }; |
| 725 | 674 |
| 726 TEST_F(DeviceCloudPolicyManagerChromeOSEnrollmentBlankSystemSaltTest, | 675 TEST_F(DeviceCloudPolicyManagerChromeOSEnrollmentBlankSystemSaltTest, |
| 727 RobotRefreshSaveFailed) { | 676 RobotRefreshSaveFailed) { |
| 728 // Without the system salt, the robot token can't be stored. | 677 // Without the system salt, the robot token can't be stored. |
| 729 RunTest(); | 678 RunTest(); |
| 730 ExpectFailedEnrollment(EnrollmentStatus::STATUS_ROBOT_REFRESH_STORE_FAILED); | 679 ExpectFailedEnrollment(EnrollmentStatus::STATUS_ROBOT_REFRESH_STORE_FAILED); |
| 731 } | 680 } |
| 732 | 681 |
| 733 } // namespace | 682 } // namespace |
| 734 } // namespace policy | 683 } // namespace policy |
| OLD | NEW |