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/policy/auto_enrollment_client.h"
8 #include "base/bind_helpers.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/prefs/testing_pref_service.h"
12 #include "base/run_loop.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/values.h"
15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/chromeos/policy/server_backed_device_state.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/test/base/scoped_testing_local_state.h"
19 #include "chrome/test/base/testing_browser_process.h"
20 #include "components/policy/core/common/cloud/mock_device_management_service.h"
21 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "crypto/sha2.h"
23 #include "net/url_request/url_request_context_getter.h"
24 #include "net/url_request/url_request_test_util.h"
25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h"
28 namespace em
= enterprise_management
;
34 const char kStateKey
[] = "state_key";
35 const char kStateKeyHash
[] =
36 "\xde\x74\xcd\xf0\x03\x36\x8c\x21\x79\xba\xb1\x5a\xc4\x32\xee\xd6"
37 "\xb3\x4a\x5e\xff\x73\x7e\x92\xd9\xf8\x6e\x72\x44\xd0\x97\xc3\xe6";
38 const char kDisabledMessage
[] = "This device has been disabled.";
40 using ::testing::InSequence
;
41 using ::testing::Mock
;
42 using ::testing::SaveArg
;
45 class AutoEnrollmentClientTest
: public testing::Test
{
47 AutoEnrollmentClientTest()
48 : scoped_testing_local_state_(
49 TestingBrowserProcess::GetGlobal()),
50 local_state_(scoped_testing_local_state_
.Get()),
51 state_(AUTO_ENROLLMENT_STATE_PENDING
) {}
53 void SetUp() override
{
54 CreateClient(kStateKey
, 4, 8);
55 ASSERT_FALSE(local_state_
->GetUserPref(prefs::kShouldAutoEnroll
));
56 ASSERT_FALSE(local_state_
->GetUserPref(prefs::kAutoEnrollmentPowerLimit
));
59 void TearDown() override
{
60 // Flush any deletion tasks.
61 base::RunLoop().RunUntilIdle();
64 void CreateClient(const std::string
& state_key
,
67 state_
= AUTO_ENROLLMENT_STATE_PENDING
;
68 service_
.reset(new MockDeviceManagementService());
69 EXPECT_CALL(*service_
, StartJob(_
, _
, _
, _
, _
, _
, _
))
70 .WillRepeatedly(SaveArg
<6>(&last_request_
));
71 client_
.reset(new AutoEnrollmentClient(
72 base::Bind(&AutoEnrollmentClientTest::ProgressCallback
,
73 base::Unretained(this)),
76 new net::TestURLRequestContextGetter(
77 base::MessageLoop::current()->message_loop_proxy()),
83 void ProgressCallback(AutoEnrollmentState state
) {
87 void ServerWillFail(DeviceManagementStatus error
) {
88 em::DeviceManagementResponse dummy_response
;
89 EXPECT_CALL(*service_
,
90 CreateJob(DeviceManagementRequestJob::TYPE_AUTO_ENROLLMENT
, _
))
91 .WillOnce(service_
->FailJob(error
));
94 void ServerWillReply(int64 modulus
, bool with_hashes
, bool with_id_hash
) {
95 em::DeviceManagementResponse response
;
96 em::DeviceAutoEnrollmentResponse
* enrollment_response
=
97 response
.mutable_auto_enrollment_response();
99 enrollment_response
->set_expected_modulus(modulus
);
101 for (int i
= 0; i
< 10; ++i
) {
102 std::string state_key
= base::StringPrintf("state_key %d", i
);
103 std::string hash
= crypto::SHA256HashString(state_key
);
104 enrollment_response
->mutable_hash()->Add()->assign(hash
);
108 enrollment_response
->mutable_hash()->Add()->assign(kStateKeyHash
,
109 crypto::kSHA256Length
);
111 EXPECT_CALL(*service_
,
112 CreateJob(DeviceManagementRequestJob::TYPE_AUTO_ENROLLMENT
, _
))
113 .WillOnce(service_
->SucceedJob(response
));
116 void ServerWillSendState(
117 const std::string
& management_domain
,
118 em::DeviceStateRetrievalResponse::RestoreMode restore_mode
,
119 const std::string
& device_disabled_message
) {
120 em::DeviceManagementResponse response
;
121 em::DeviceStateRetrievalResponse
* state_response
=
122 response
.mutable_device_state_retrieval_response();
123 state_response
->set_restore_mode(restore_mode
);
124 state_response
->set_management_domain(management_domain
);
125 state_response
->mutable_disabled_state()->set_message(
126 device_disabled_message
);
129 CreateJob(DeviceManagementRequestJob::TYPE_DEVICE_STATE_RETRIEVAL
, _
))
130 .WillOnce(service_
->SucceedJob(response
));
133 void ServerWillReplyAsync(MockDeviceManagementJob
** job
) {
134 EXPECT_CALL(*service_
,
135 CreateJob(DeviceManagementRequestJob::TYPE_AUTO_ENROLLMENT
, _
))
136 .WillOnce(service_
->CreateAsyncJob(job
));
139 bool HasCachedDecision() {
140 return local_state_
->GetUserPref(prefs::kShouldAutoEnroll
);
143 void VerifyCachedResult(bool should_enroll
, int power_limit
) {
144 base::FundamentalValue
value_should_enroll(should_enroll
);
145 base::FundamentalValue
value_power_limit(power_limit
);
146 EXPECT_TRUE(base::Value::Equals(
147 &value_should_enroll
,
148 local_state_
->GetUserPref(prefs::kShouldAutoEnroll
)));
149 EXPECT_TRUE(base::Value::Equals(
151 local_state_
->GetUserPref(prefs::kAutoEnrollmentPowerLimit
)));
154 bool HasServerBackedState() {
155 return local_state_
->GetUserPref(prefs::kServerBackedDeviceState
);
158 void VerifyServerBackedState(
159 const std::string
& expected_management_domain
,
160 const std::string
& expected_restore_mode
,
161 const std::string
& expected_disabled_message
) {
162 const base::Value
* state
=
163 local_state_
->GetUserPref(prefs::kServerBackedDeviceState
);
165 const base::DictionaryValue
* state_dict
= nullptr;
166 ASSERT_TRUE(state
->GetAsDictionary(&state_dict
));
168 std::string actual_management_domain
;
169 EXPECT_TRUE(state_dict
->GetString(kDeviceStateManagementDomain
,
170 &actual_management_domain
));
171 EXPECT_EQ(expected_management_domain
, actual_management_domain
);
173 if (!expected_restore_mode
.empty()) {
174 std::string actual_restore_mode
;
175 EXPECT_TRUE(state_dict
->GetString(kDeviceStateRestoreMode
,
176 &actual_restore_mode
));
177 EXPECT_EQ(expected_restore_mode
, actual_restore_mode
);
179 EXPECT_FALSE(state_dict
->HasKey(kDeviceStateRestoreMode
));
182 std::string actual_disabled_message
;
183 EXPECT_TRUE(state_dict
->GetString(kDeviceStateDisabledMessage
,
184 &actual_disabled_message
));
185 EXPECT_EQ(expected_disabled_message
, actual_disabled_message
);
188 const em::DeviceAutoEnrollmentRequest
& auto_enrollment_request() {
189 return last_request_
.auto_enrollment_request();
192 content::TestBrowserThreadBundle browser_threads_
;
193 ScopedTestingLocalState scoped_testing_local_state_
;
194 TestingPrefServiceSimple
* local_state_
;
195 scoped_ptr
<MockDeviceManagementService
> service_
;
196 scoped_ptr
<AutoEnrollmentClient
> client_
;
197 em::DeviceManagementRequest last_request_
;
198 AutoEnrollmentState state_
;
201 DISALLOW_COPY_AND_ASSIGN(AutoEnrollmentClientTest
);
204 TEST_F(AutoEnrollmentClientTest
, NetworkFailure
) {
205 ServerWillFail(DM_STATUS_TEMPORARY_UNAVAILABLE
);
207 EXPECT_EQ(AUTO_ENROLLMENT_STATE_SERVER_ERROR
, state_
);
208 EXPECT_FALSE(HasCachedDecision());
209 EXPECT_FALSE(HasServerBackedState());
212 TEST_F(AutoEnrollmentClientTest
, EmptyReply
) {
213 ServerWillReply(-1, false, false);
215 EXPECT_EQ(AUTO_ENROLLMENT_STATE_NO_ENROLLMENT
, state_
);
216 VerifyCachedResult(false, 8);
217 EXPECT_FALSE(HasServerBackedState());
220 TEST_F(AutoEnrollmentClientTest
, ClientUploadsRightBits
) {
221 ServerWillReply(-1, false, false);
223 EXPECT_EQ(AUTO_ENROLLMENT_STATE_NO_ENROLLMENT
, state_
);
225 EXPECT_TRUE(auto_enrollment_request().has_remainder());
226 EXPECT_TRUE(auto_enrollment_request().has_modulus());
227 EXPECT_EQ(16, auto_enrollment_request().modulus());
228 EXPECT_EQ(kStateKeyHash
[31] & 0xf, auto_enrollment_request().remainder());
229 VerifyCachedResult(false, 8);
230 EXPECT_FALSE(HasServerBackedState());
233 TEST_F(AutoEnrollmentClientTest
, AskForMoreThenFail
) {
235 ServerWillReply(32, false, false);
236 ServerWillFail(DM_STATUS_TEMPORARY_UNAVAILABLE
);
238 EXPECT_EQ(AUTO_ENROLLMENT_STATE_SERVER_ERROR
, state_
);
239 EXPECT_FALSE(HasCachedDecision());
240 EXPECT_FALSE(HasServerBackedState());
243 TEST_F(AutoEnrollmentClientTest
, AskForMoreThenEvenMore
) {
245 ServerWillReply(32, false, false);
246 ServerWillReply(64, false, false);
248 EXPECT_EQ(AUTO_ENROLLMENT_STATE_SERVER_ERROR
, state_
);
249 EXPECT_FALSE(HasCachedDecision());
250 EXPECT_FALSE(HasServerBackedState());
253 TEST_F(AutoEnrollmentClientTest
, AskForLess
) {
255 ServerWillReply(8, false, false);
256 ServerWillReply(-1, true, true);
259 em::DeviceStateRetrievalResponse::RESTORE_MODE_REENROLLMENT_ENFORCED
,
262 EXPECT_EQ(AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT
, state_
);
263 VerifyCachedResult(true, 8);
264 VerifyServerBackedState("example.com",
265 kDeviceStateRestoreModeReEnrollmentEnforced
,
269 TEST_F(AutoEnrollmentClientTest
, AskForSame
) {
271 ServerWillReply(16, false, false);
272 ServerWillReply(-1, true, true);
275 em::DeviceStateRetrievalResponse::RESTORE_MODE_REENROLLMENT_ENFORCED
,
278 EXPECT_EQ(AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT
, state_
);
279 VerifyCachedResult(true, 8);
280 VerifyServerBackedState("example.com",
281 kDeviceStateRestoreModeReEnrollmentEnforced
,
285 TEST_F(AutoEnrollmentClientTest
, AskForSameTwice
) {
287 ServerWillReply(16, false, false);
288 ServerWillReply(16, false, false);
290 EXPECT_EQ(AUTO_ENROLLMENT_STATE_SERVER_ERROR
, state_
);
291 EXPECT_FALSE(HasCachedDecision());
292 EXPECT_FALSE(HasServerBackedState());
295 TEST_F(AutoEnrollmentClientTest
, AskForTooMuch
) {
296 ServerWillReply(512, false, false);
298 EXPECT_EQ(AUTO_ENROLLMENT_STATE_SERVER_ERROR
, state_
);
299 EXPECT_FALSE(HasCachedDecision());
300 EXPECT_FALSE(HasServerBackedState());
303 TEST_F(AutoEnrollmentClientTest
, AskNonPowerOf2
) {
305 ServerWillReply(100, false, false);
306 ServerWillReply(-1, false, false);
308 EXPECT_EQ(AUTO_ENROLLMENT_STATE_NO_ENROLLMENT
, state_
);
309 EXPECT_TRUE(auto_enrollment_request().has_remainder());
310 EXPECT_TRUE(auto_enrollment_request().has_modulus());
311 EXPECT_EQ(128, auto_enrollment_request().modulus());
312 EXPECT_EQ(kStateKeyHash
[31] & 0x7f, auto_enrollment_request().remainder());
313 VerifyCachedResult(false, 8);
314 EXPECT_FALSE(HasServerBackedState());
317 TEST_F(AutoEnrollmentClientTest
, ConsumerDevice
) {
318 ServerWillReply(-1, true, false);
320 EXPECT_EQ(AUTO_ENROLLMENT_STATE_NO_ENROLLMENT
, state_
);
321 VerifyCachedResult(false, 8);
322 EXPECT_FALSE(HasServerBackedState());
324 // Network changes don't trigger retries after obtaining a response from
326 client_
->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_ETHERNET
);
327 EXPECT_EQ(AUTO_ENROLLMENT_STATE_NO_ENROLLMENT
, state_
);
330 TEST_F(AutoEnrollmentClientTest
, ForcedReEnrollment
) {
331 ServerWillReply(-1, true, true);
334 em::DeviceStateRetrievalResponse::RESTORE_MODE_REENROLLMENT_ENFORCED
,
337 EXPECT_EQ(AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT
, state_
);
338 VerifyCachedResult(true, 8);
339 VerifyServerBackedState("example.com",
340 kDeviceStateRestoreModeReEnrollmentEnforced
,
343 // Network changes don't trigger retries after obtaining a response from
345 client_
->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_ETHERNET
);
346 EXPECT_EQ(AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT
, state_
);
349 TEST_F(AutoEnrollmentClientTest
, RequestedReEnrollment
) {
350 ServerWillReply(-1, true, true);
353 em::DeviceStateRetrievalResponse::RESTORE_MODE_REENROLLMENT_REQUESTED
,
356 EXPECT_EQ(AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT
, state_
);
357 VerifyCachedResult(true, 8);
358 VerifyServerBackedState("example.com",
359 kDeviceStateRestoreModeReEnrollmentRequested
,
363 TEST_F(AutoEnrollmentClientTest
, DeviceDisabled
) {
364 ServerWillReply(-1, true, true);
367 em::DeviceStateRetrievalResponse::RESTORE_MODE_DISABLED
,
370 EXPECT_EQ(AUTO_ENROLLMENT_STATE_NO_ENROLLMENT
, state_
);
371 VerifyCachedResult(true, 8);
372 VerifyServerBackedState("example.com",
373 kDeviceStateRestoreModeDisabled
,
377 TEST_F(AutoEnrollmentClientTest
, NoBitsUploaded
) {
378 CreateClient(kStateKey
, 0, 0);
379 ServerWillReply(-1, false, false);
381 EXPECT_EQ(AUTO_ENROLLMENT_STATE_NO_ENROLLMENT
, state_
);
382 EXPECT_TRUE(auto_enrollment_request().has_remainder());
383 EXPECT_TRUE(auto_enrollment_request().has_modulus());
384 EXPECT_EQ(1, auto_enrollment_request().modulus());
385 EXPECT_EQ(0, auto_enrollment_request().remainder());
386 VerifyCachedResult(false, 0);
387 EXPECT_FALSE(HasServerBackedState());
390 TEST_F(AutoEnrollmentClientTest
, ManyBitsUploaded
) {
391 int64 bottom62
= GG_INT64_C(0x386e7244d097c3e6);
392 for (int i
= 0; i
<= 62; ++i
) {
393 CreateClient(kStateKey
, i
, i
);
394 ServerWillReply(-1, false, false);
396 EXPECT_EQ(AUTO_ENROLLMENT_STATE_NO_ENROLLMENT
, state_
);
397 EXPECT_TRUE(auto_enrollment_request().has_remainder());
398 EXPECT_TRUE(auto_enrollment_request().has_modulus());
399 EXPECT_EQ(GG_INT64_C(1) << i
, auto_enrollment_request().modulus());
400 EXPECT_EQ(bottom62
% (GG_INT64_C(1) << i
),
401 auto_enrollment_request().remainder());
402 VerifyCachedResult(false, i
);
403 EXPECT_FALSE(HasServerBackedState());
407 TEST_F(AutoEnrollmentClientTest
, MoreThan32BitsUploaded
) {
408 CreateClient(kStateKey
, 10, 37);
410 ServerWillReply(GG_INT64_C(1) << 37, false, false);
411 ServerWillReply(-1, true, true);
414 em::DeviceStateRetrievalResponse::RESTORE_MODE_REENROLLMENT_ENFORCED
,
417 EXPECT_EQ(AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT
, state_
);
418 VerifyCachedResult(true, 37);
419 VerifyServerBackedState("example.com",
420 kDeviceStateRestoreModeReEnrollmentEnforced
,
424 TEST_F(AutoEnrollmentClientTest
, ReuseCachedDecision
) {
425 // No bucket download requests should be issued.
426 EXPECT_CALL(*service_
,
427 CreateJob(DeviceManagementRequestJob::TYPE_AUTO_ENROLLMENT
, _
))
429 local_state_
->SetUserPref(prefs::kShouldAutoEnroll
,
430 new base::FundamentalValue(true));
431 local_state_
->SetUserPref(prefs::kAutoEnrollmentPowerLimit
,
432 new base::FundamentalValue(8));
434 // Note that device state will be retrieved every time, regardless of any
435 // cached information. This is intentional, the idea is that device state on
436 // the server may change.
439 em::DeviceStateRetrievalResponse::RESTORE_MODE_REENROLLMENT_ENFORCED
,
443 EXPECT_EQ(AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT
, state_
);
444 VerifyServerBackedState("example.com",
445 kDeviceStateRestoreModeReEnrollmentEnforced
,
449 TEST_F(AutoEnrollmentClientTest
, RetryIfPowerLargerThanCached
) {
450 local_state_
->SetUserPref(prefs::kShouldAutoEnroll
,
451 new base::FundamentalValue(false));
452 local_state_
->SetUserPref(prefs::kAutoEnrollmentPowerLimit
,
453 new base::FundamentalValue(8));
454 CreateClient(kStateKey
, 5, 10);
455 ServerWillReply(-1, true, true);
458 em::DeviceStateRetrievalResponse::RESTORE_MODE_REENROLLMENT_ENFORCED
,
461 EXPECT_EQ(AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT
, state_
);
462 VerifyServerBackedState("example.com",
463 kDeviceStateRestoreModeReEnrollmentEnforced
,
467 TEST_F(AutoEnrollmentClientTest
, NetworkChangeRetryAfterErrors
) {
468 ServerWillFail(DM_STATUS_TEMPORARY_UNAVAILABLE
);
470 // Don't invoke the callback if there was a network failure.
471 EXPECT_EQ(AUTO_ENROLLMENT_STATE_SERVER_ERROR
, state_
);
472 EXPECT_FALSE(HasCachedDecision());
473 EXPECT_FALSE(HasServerBackedState());
475 // The client doesn't retry if no new connection became available.
476 client_
->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_NONE
);
477 EXPECT_EQ(AUTO_ENROLLMENT_STATE_SERVER_ERROR
, state_
);
478 EXPECT_FALSE(HasCachedDecision());
479 EXPECT_FALSE(HasServerBackedState());
481 // Retry once the network is back.
482 ServerWillReply(-1, true, true);
485 em::DeviceStateRetrievalResponse::RESTORE_MODE_REENROLLMENT_ENFORCED
,
487 client_
->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_ETHERNET
);
488 EXPECT_EQ(AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT
, state_
);
489 EXPECT_TRUE(HasCachedDecision());
490 VerifyServerBackedState("example.com",
491 kDeviceStateRestoreModeReEnrollmentEnforced
,
494 // Subsequent network changes don't trigger retries.
495 client_
->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_NONE
);
496 client_
->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_ETHERNET
);
497 EXPECT_EQ(AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT
, state_
);
498 EXPECT_TRUE(HasCachedDecision());
499 VerifyServerBackedState("example.com",
500 kDeviceStateRestoreModeReEnrollmentEnforced
,
504 TEST_F(AutoEnrollmentClientTest
, CancelAndDeleteSoonWithPendingRequest
) {
505 MockDeviceManagementJob
* job
= NULL
;
506 ServerWillReplyAsync(&job
);
510 EXPECT_EQ(AUTO_ENROLLMENT_STATE_PENDING
, state_
);
512 // Cancel while a request is in flight.
513 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
514 client_
.release()->CancelAndDeleteSoon();
515 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
517 // The client cleans itself up once a reply is received.
518 job
->SendResponse(DM_STATUS_TEMPORARY_UNAVAILABLE
,
519 em::DeviceManagementResponse());
520 // The DeleteSoon task has been posted:
521 EXPECT_FALSE(base::MessageLoop::current()->IsIdleForTesting());
522 EXPECT_EQ(AUTO_ENROLLMENT_STATE_PENDING
, state_
);
525 TEST_F(AutoEnrollmentClientTest
, NetworkChangedAfterCancelAndDeleteSoon
) {
526 MockDeviceManagementJob
* job
= NULL
;
527 ServerWillReplyAsync(&job
);
531 EXPECT_EQ(AUTO_ENROLLMENT_STATE_PENDING
, state_
);
533 // Cancel while a request is in flight.
534 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
535 AutoEnrollmentClient
* client
= client_
.release();
536 client
->CancelAndDeleteSoon();
537 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
539 // Network change events are ignored while a request is pending.
540 client
->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_ETHERNET
);
541 EXPECT_EQ(AUTO_ENROLLMENT_STATE_PENDING
, state_
);
543 // The client cleans itself up once a reply is received.
544 job
->SendResponse(DM_STATUS_TEMPORARY_UNAVAILABLE
,
545 em::DeviceManagementResponse());
546 // The DeleteSoon task has been posted:
547 EXPECT_FALSE(base::MessageLoop::current()->IsIdleForTesting());
548 EXPECT_EQ(AUTO_ENROLLMENT_STATE_PENDING
, state_
);
550 // Network changes that have been posted before are also ignored:
551 client
->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_ETHERNET
);
552 EXPECT_EQ(AUTO_ENROLLMENT_STATE_PENDING
, state_
);
555 TEST_F(AutoEnrollmentClientTest
, CancelAndDeleteSoonAfterCompletion
) {
556 ServerWillReply(-1, true, true);
559 em::DeviceStateRetrievalResponse::RESTORE_MODE_REENROLLMENT_ENFORCED
,
562 EXPECT_EQ(AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT
, state_
);
563 VerifyServerBackedState("example.com",
564 kDeviceStateRestoreModeReEnrollmentEnforced
,
567 // The client will delete itself immediately if there are no pending
569 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
570 client_
.release()->CancelAndDeleteSoon();
571 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
574 TEST_F(AutoEnrollmentClientTest
, CancelAndDeleteSoonAfterNetworkFailure
) {
575 ServerWillFail(DM_STATUS_TEMPORARY_UNAVAILABLE
);
577 EXPECT_EQ(AUTO_ENROLLMENT_STATE_SERVER_ERROR
, state_
);
579 // The client will delete itself immediately if there are no pending
581 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
582 client_
.release()->CancelAndDeleteSoon();
583 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
586 TEST_F(AutoEnrollmentClientTest
, NetworkFailureThenRequireUpdatedModulus
) {
587 // This test verifies that if the first request fails due to a network
588 // problem then the second request will correctly handle an updated
589 // modulus request from the server.
591 ServerWillFail(DM_STATUS_REQUEST_FAILED
);
593 // Callback should signal the connection error.
594 EXPECT_EQ(AUTO_ENROLLMENT_STATE_CONNECTION_ERROR
, state_
);
595 EXPECT_FALSE(HasCachedDecision());
596 EXPECT_FALSE(HasServerBackedState());
597 Mock::VerifyAndClearExpectations(service_
.get());
600 // The default client uploads 4 bits. Make the server ask for 5.
601 ServerWillReply(1 << 5, false, false);
602 EXPECT_CALL(*service_
, StartJob(_
, _
, _
, _
, _
, _
, _
));
603 // Then reply with a valid response and include the hash.
604 ServerWillReply(-1, true, true);
605 EXPECT_CALL(*service_
, StartJob(_
, _
, _
, _
, _
, _
, _
));
606 // State download triggers.
609 em::DeviceStateRetrievalResponse::RESTORE_MODE_REENROLLMENT_ENFORCED
,
611 EXPECT_CALL(*service_
, StartJob(_
, _
, _
, _
, _
, _
, _
));
613 // Trigger a network change event.
614 client_
->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_ETHERNET
);
615 EXPECT_EQ(AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT
, state_
);
616 EXPECT_TRUE(HasCachedDecision());
617 VerifyServerBackedState("example.com",
618 kDeviceStateRestoreModeReEnrollmentEnforced
,
620 Mock::VerifyAndClearExpectations(service_
.get());
624 } // namespace policy