Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: chrome/browser/chromeos/policy/device_cloud_policy_manager_chromeos_unittest.cc

Issue 751703003: Implemented consumer management unenrollment. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@dcpm
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
11 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
12 #include "base/compiler_specific.h" 12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop/message_loop.h" 14 #include "base/message_loop/message_loop.h"
15 #include "base/prefs/pref_registry_simple.h" 15 #include "base/prefs/pref_registry_simple.h"
16 #include "base/prefs/testing_pref_service.h" 16 #include "base/prefs/testing_pref_service.h"
17 #include "base/run_loop.h" 17 #include "base/run_loop.h"
18 #include "chrome/browser/chromeos/policy/device_cloud_policy_initializer.h" 18 #include "chrome/browser/chromeos/policy/device_cloud_policy_initializer.h"
19 #include "chrome/browser/chromeos/policy/device_cloud_policy_store_chromeos.h" 19 #include "chrome/browser/chromeos/policy/device_cloud_policy_store_chromeos.h"
20 #include "chrome/browser/chromeos/policy/enterprise_install_attributes.h" 20 #include "chrome/browser/chromeos/policy/enterprise_install_attributes.h"
21 #include "chrome/browser/chromeos/policy/fake_consumer_management_service.h"
21 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h" 22 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h"
22 #include "chrome/browser/chromeos/settings/cros_settings.h" 23 #include "chrome/browser/chromeos/settings/cros_settings.h"
23 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h" 24 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h"
24 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h " 25 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h "
25 #include "chrome/browser/chromeos/settings/device_settings_service.h" 26 #include "chrome/browser/chromeos/settings/device_settings_service.h"
26 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h" 27 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
27 #include "chrome/browser/prefs/browser_prefs.h" 28 #include "chrome/browser/prefs/browser_prefs.h"
28 #include "chrome/test/base/testing_browser_process.h" 29 #include "chrome/test/base/testing_browser_process.h"
29 #include "chromeos/cryptohome/system_salt_getter.h" 30 #include "chromeos/cryptohome/system_salt_getter.h"
30 #include "chromeos/dbus/dbus_client_implementation_type.h" 31 #include "chromeos/dbus/dbus_client_implementation_type.h"
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 manager_->AddObserver(this); 153 manager_->AddObserver(this);
153 initializer_.reset(new DeviceCloudPolicyInitializer( 154 initializer_.reset(new DeviceCloudPolicyInitializer(
154 &local_state_, 155 &local_state_,
155 &device_management_service_, 156 &device_management_service_,
156 &consumer_device_management_service_, 157 &consumer_device_management_service_,
157 base::MessageLoopProxy::current(), 158 base::MessageLoopProxy::current(),
158 install_attributes_.get(), 159 install_attributes_.get(),
159 &state_keys_broker_, 160 &state_keys_broker_,
160 store_, 161 store_,
161 manager_.get(), 162 manager_.get(),
162 &device_settings_service_)); 163 &device_settings_service_,
164 &consumer_management_service_));
163 initializer_->Init(); 165 initializer_->Init();
164 } 166 }
165 167
166 void VerifyPolicyPopulated() { 168 void VerifyPolicyPopulated() {
167 PolicyBundle bundle; 169 PolicyBundle bundle;
168 bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) 170 bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
169 .Set(key::kDeviceMetricsReportingEnabled, 171 .Set(key::kDeviceMetricsReportingEnabled,
170 POLICY_LEVEL_MANDATORY, 172 POLICY_LEVEL_MANDATORY,
171 POLICY_SCOPE_MACHINE, 173 POLICY_SCOPE_MACHINE,
172 new base::FundamentalValue(false), 174 new base::FundamentalValue(false),
173 NULL); 175 NULL);
174 EXPECT_TRUE(manager_->policies().Equals(bundle)); 176 EXPECT_TRUE(manager_->policies().Equals(bundle));
175 } 177 }
176 178
177 MOCK_METHOD0(OnDeviceCloudPolicyManagerConnected, void()); 179 MOCK_METHOD0(OnDeviceCloudPolicyManagerConnected, void());
180 MOCK_METHOD0(OnDeviceCloudPolicyManagerDisconnected, void());
178 181
179 scoped_ptr<EnterpriseInstallAttributes> install_attributes_; 182 scoped_ptr<EnterpriseInstallAttributes> install_attributes_;
180 183
181 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; 184 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
182 net::TestURLFetcherFactory url_fetcher_factory_; 185 net::TestURLFetcherFactory url_fetcher_factory_;
183 int url_fetcher_response_code_; 186 int url_fetcher_response_code_;
184 string url_fetcher_response_string_; 187 string url_fetcher_response_string_;
185 TestingPrefServiceSimple local_state_; 188 TestingPrefServiceSimple local_state_;
186 MockDeviceManagementService device_management_service_; 189 MockDeviceManagementService device_management_service_;
187 MockDeviceManagementService consumer_device_management_service_; 190 MockDeviceManagementService consumer_device_management_service_;
188 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; 191 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
189 chromeos::ScopedTestCrosSettings test_cros_settings_; 192 chromeos::ScopedTestCrosSettings test_cros_settings_;
190 chromeos::system::ScopedFakeStatisticsProvider fake_statistics_provider_; 193 chromeos::system::ScopedFakeStatisticsProvider fake_statistics_provider_;
191 chromeos::FakeSessionManagerClient fake_session_manager_client_; 194 chromeos::FakeSessionManagerClient fake_session_manager_client_;
192 chromeos::FakeCryptohomeClient* fake_cryptohome_client_; 195 chromeos::FakeCryptohomeClient* fake_cryptohome_client_;
193 ServerBackedStateKeysBroker state_keys_broker_; 196 ServerBackedStateKeysBroker state_keys_broker_;
194 197
195 DeviceCloudPolicyStoreChromeOS* store_; 198 DeviceCloudPolicyStoreChromeOS* store_;
196 SchemaRegistry schema_registry_; 199 SchemaRegistry schema_registry_;
197 scoped_ptr<DeviceCloudPolicyManagerChromeOS> manager_; 200 scoped_ptr<DeviceCloudPolicyManagerChromeOS> manager_;
201 FakeConsumerManagementService consumer_management_service_;
198 scoped_ptr<DeviceCloudPolicyInitializer> initializer_; 202 scoped_ptr<DeviceCloudPolicyInitializer> initializer_;
199 203
200 private: 204 private:
201 DISALLOW_COPY_AND_ASSIGN(DeviceCloudPolicyManagerChromeOSTest); 205 DISALLOW_COPY_AND_ASSIGN(DeviceCloudPolicyManagerChromeOSTest);
202 }; 206 };
203 207
204 TEST_F(DeviceCloudPolicyManagerChromeOSTest, FreshDevice) { 208 TEST_F(DeviceCloudPolicyManagerChromeOSTest, FreshDevice) {
205 owner_key_util_->Clear(); 209 owner_key_util_->Clear();
206 FlushDeviceSettings(); 210 FlushDeviceSettings();
207 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); 211 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 device_policy_.policy()); 269 device_policy_.policy());
266 policy_fetch_job->SendResponse(DM_STATUS_SUCCESS, policy_fetch_response); 270 policy_fetch_job->SendResponse(DM_STATUS_SUCCESS, policy_fetch_response);
267 FlushDeviceSettings(); 271 FlushDeviceSettings();
268 272
269 // Policy state should now be active and the policy map should be populated. 273 // Policy state should now be active and the policy map should be populated.
270 EXPECT_TRUE(store_->is_managed()); 274 EXPECT_TRUE(store_->is_managed());
271 VerifyPolicyPopulated(); 275 VerifyPolicyPopulated();
272 } 276 }
273 277
274 TEST_F(DeviceCloudPolicyManagerChromeOSTest, ConsumerDevice) { 278 TEST_F(DeviceCloudPolicyManagerChromeOSTest, ConsumerDevice) {
279 consumer_management_service_.SetStatusAndStage(
280 ConsumerManagementService::STATUS_ENROLLED,
bartfab (slow) 2014/11/28 13:25:18 Nit: #include "chrome/browser/chromeos/policy/cons
davidyu 2014/12/01 17:05:22 Done.
281 ConsumerManagementStage(ConsumerManagementStage::NONE));
bartfab (slow) 2014/11/28 13:25:18 Nit: #include "chrome/browser/chromeos/policy/cons
davidyu 2014/12/01 17:05:22 Done.
275 FlushDeviceSettings(); 282 FlushDeviceSettings();
276 EXPECT_EQ(CloudPolicyStore::STATUS_BAD_STATE, store_->status()); 283 EXPECT_EQ(CloudPolicyStore::STATUS_BAD_STATE, store_->status());
277 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); 284 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
278 285
279 PolicyBundle bundle; 286 PolicyBundle bundle;
280 EXPECT_TRUE(manager_->policies().Equals(bundle)); 287 EXPECT_TRUE(manager_->policies().Equals(bundle));
288 EXPECT_FALSE(manager_->core()->service()); // Not connected.
bartfab (slow) 2014/11/28 13:25:18 Nit: #include "components/policy/core/common/cloud
davidyu 2014/12/01 17:05:22 Done.
281 289
282 ConnectManager(); 290 ConnectManager();
291 base::RunLoop().RunUntilIdle();
283 EXPECT_TRUE(manager_->policies().Equals(bundle)); 292 EXPECT_TRUE(manager_->policies().Equals(bundle));
293 EXPECT_TRUE(manager_->core()->service()); // Connected.
294
295 manager_->Disconnect();
296 EXPECT_FALSE(manager_->core()->service()); // Not connected.
284 297
285 manager_->Shutdown(); 298 manager_->Shutdown();
286 EXPECT_TRUE(manager_->policies().Equals(bundle)); 299 EXPECT_TRUE(manager_->policies().Equals(bundle));
287 } 300 }
288 301
289 TEST_F(DeviceCloudPolicyManagerChromeOSTest, ObserverIsNotifiedWhenConnected) { 302 TEST_F(DeviceCloudPolicyManagerChromeOSTest,
303 ObserverIsNotifiedWhenConnectedAndDisconnected) {
290 LockDevice(); 304 LockDevice();
291 FlushDeviceSettings(); 305 FlushDeviceSettings();
292 306
293 MockDeviceManagementJob* policy_fetch_job = NULL; 307 MockDeviceManagementJob* policy_fetch_job = NULL;
294 EXPECT_CALL(device_management_service_, 308 EXPECT_CALL(device_management_service_,
295 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH, _)) 309 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH, _))
296 .WillOnce(device_management_service_.CreateAsyncJob(&policy_fetch_job)); 310 .WillOnce(device_management_service_.CreateAsyncJob(&policy_fetch_job));
297 EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _)); 311 EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _));
298 EXPECT_CALL(*this, OnDeviceCloudPolicyManagerConnected()); 312 EXPECT_CALL(*this, OnDeviceCloudPolicyManagerConnected());
299 313
300 ConnectManager(); 314 ConnectManager();
301 base::RunLoop().RunUntilIdle(); 315 base::RunLoop().RunUntilIdle();
316 Mock::VerifyAndClearExpectations(&device_management_service_);
317 Mock::VerifyAndClearExpectations(this);
318
319 EXPECT_CALL(*this, OnDeviceCloudPolicyManagerDisconnected());
320 manager_->Disconnect();
302 } 321 }
303 322
304 class DeviceCloudPolicyManagerChromeOSEnrollmentTest 323 class DeviceCloudPolicyManagerChromeOSEnrollmentTest
305 : public DeviceCloudPolicyManagerChromeOSTest { 324 : public DeviceCloudPolicyManagerChromeOSTest {
306 public: 325 public:
307 void Done(EnrollmentStatus status) { 326 void Done(EnrollmentStatus status) {
308 status_ = status; 327 status_ = status;
309 done_ = true; 328 done_ = true;
310 } 329 }
311 330
331 MOCK_METHOD1(OnUnregistered, void(bool));
332
312 protected: 333 protected:
313 DeviceCloudPolicyManagerChromeOSEnrollmentTest() 334 DeviceCloudPolicyManagerChromeOSEnrollmentTest()
314 : is_auto_enrollment_(false), 335 : is_auto_enrollment_(false),
315 management_mode_(em::PolicyData::ENTERPRISE_MANAGED), 336 management_mode_(em::PolicyData::ENTERPRISE_MANAGED),
316 register_status_(DM_STATUS_SUCCESS), 337 register_status_(DM_STATUS_SUCCESS),
317 policy_fetch_status_(DM_STATUS_SUCCESS), 338 policy_fetch_status_(DM_STATUS_SUCCESS),
318 robot_auth_fetch_status_(DM_STATUS_SUCCESS), 339 robot_auth_fetch_status_(DM_STATUS_SUCCESS),
319 store_result_(true), 340 store_result_(true),
320 status_(EnrollmentStatus::ForStatus(EnrollmentStatus::STATUS_SUCCESS)), 341 status_(EnrollmentStatus::ForStatus(EnrollmentStatus::STATUS_SUCCESS)),
321 done_(false) {} 342 done_(false) {}
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
612 SuccessfulConsumerManagementEnrollment) { 633 SuccessfulConsumerManagementEnrollment) {
613 management_mode_ = em::PolicyData::CONSUMER_MANAGED; 634 management_mode_ = em::PolicyData::CONSUMER_MANAGED;
614 owner_key_util_->SetPrivateKey(device_policy_.GetSigningKey()); 635 owner_key_util_->SetPrivateKey(device_policy_.GetSigningKey());
615 InitOwner(device_policy_.policy_data().username(), true); 636 InitOwner(device_policy_.policy_data().username(), true);
616 FlushDeviceSettings(); 637 FlushDeviceSettings();
617 638
618 RunTest(); 639 RunTest();
619 ExpectSuccessfulEnrollment(); 640 ExpectSuccessfulEnrollment();
620 } 641 }
621 642
643 TEST_F(DeviceCloudPolicyManagerChromeOSEnrollmentTest, UnregisterSucceeds) {
644 // Enroll first.
645 RunTest();
646 ExpectSuccessfulEnrollment();
647
648 // Set up mock objects for the upcoming unregistration job.
649 em::DeviceManagementResponse response;
650 response.mutable_unregister_response();
651 EXPECT_CALL(device_management_service_,
652 CreateJob(DeviceManagementRequestJob::TYPE_UNREGISTRATION, _))
653 .WillOnce(device_management_service_.SucceedJob(response));
654 EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _));
655 EXPECT_CALL(*this, OnUnregistered(true));
656
657 // Start unregistering.
658 manager_->Unregister(base::Bind(
659 &DeviceCloudPolicyManagerChromeOSEnrollmentTest::OnUnregistered,
660 base::Unretained(this)));
661 }
662
663 TEST_F(DeviceCloudPolicyManagerChromeOSEnrollmentTest, UnregisterFails) {
664 // Enroll first.
665 RunTest();
666 ExpectSuccessfulEnrollment();
667
668 // Set up mock objects for the upcoming unregistration job.
669 EXPECT_CALL(device_management_service_,
670 CreateJob(DeviceManagementRequestJob::TYPE_UNREGISTRATION, _))
671 .WillOnce(device_management_service_.FailJob(DM_STATUS_REQUEST_FAILED));
672 EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _));
673 EXPECT_CALL(*this, OnUnregistered(false));
674
675 // Start unregistering.
676 manager_->Unregister(base::Bind(
677 &DeviceCloudPolicyManagerChromeOSEnrollmentTest::OnUnregistered,
678 base::Unretained(this)));
679 }
680
622 // A subclass that runs with a blank system salt. 681 // A subclass that runs with a blank system salt.
623 class DeviceCloudPolicyManagerChromeOSEnrollmentBlankSystemSaltTest 682 class DeviceCloudPolicyManagerChromeOSEnrollmentBlankSystemSaltTest
624 : public DeviceCloudPolicyManagerChromeOSEnrollmentTest { 683 : public DeviceCloudPolicyManagerChromeOSEnrollmentTest {
625 protected: 684 protected:
626 DeviceCloudPolicyManagerChromeOSEnrollmentBlankSystemSaltTest() { 685 DeviceCloudPolicyManagerChromeOSEnrollmentBlankSystemSaltTest() {
627 // Set up a FakeCryptohomeClient with a blank system salt. 686 // Set up a FakeCryptohomeClient with a blank system salt.
628 fake_cryptohome_client_->set_system_salt(std::vector<uint8>()); 687 fake_cryptohome_client_->set_system_salt(std::vector<uint8>());
629 } 688 }
630 }; 689 };
631 690
632 TEST_F(DeviceCloudPolicyManagerChromeOSEnrollmentBlankSystemSaltTest, 691 TEST_F(DeviceCloudPolicyManagerChromeOSEnrollmentBlankSystemSaltTest,
633 RobotRefreshSaveFailed) { 692 RobotRefreshSaveFailed) {
634 // Without the system salt, the robot token can't be stored. 693 // Without the system salt, the robot token can't be stored.
635 RunTest(); 694 RunTest();
636 ExpectFailedEnrollment(EnrollmentStatus::STATUS_ROBOT_REFRESH_STORE_FAILED); 695 ExpectFailedEnrollment(EnrollmentStatus::STATUS_ROBOT_REFRESH_STORE_FAILED);
637 } 696 }
638 697
639 } // namespace 698 } // namespace
640 } // namespace policy 699 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698