Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / auto_enrollment_client_unittest.cc
blob1813430cf00b233152cb13532416e76faa65249a
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"
7 #include <stdint.h>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/prefs/testing_pref_service.h"
14 #include "base/run_loop.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/values.h"
17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/chromeos/policy/server_backed_device_state.h"
19 #include "chrome/common/pref_names.h"
20 #include "chrome/test/base/scoped_testing_local_state.h"
21 #include "chrome/test/base/testing_browser_process.h"
22 #include "components/policy/core/common/cloud/mock_device_management_service.h"
23 #include "content/public/test/test_browser_thread_bundle.h"
24 #include "crypto/sha2.h"
25 #include "net/url_request/url_request_context_getter.h"
26 #include "net/url_request/url_request_test_util.h"
27 #include "testing/gmock/include/gmock/gmock.h"
28 #include "testing/gtest/include/gtest/gtest.h"
30 namespace em = enterprise_management;
32 namespace policy {
34 namespace {
36 const char kStateKey[] = "state_key";
37 const char kStateKeyHash[] =
38 "\xde\x74\xcd\xf0\x03\x36\x8c\x21\x79\xba\xb1\x5a\xc4\x32\xee\xd6"
39 "\xb3\x4a\x5e\xff\x73\x7e\x92\xd9\xf8\x6e\x72\x44\xd0\x97\xc3\xe6";
40 const char kDisabledMessage[] = "This device has been disabled.";
42 using ::testing::InSequence;
43 using ::testing::Mock;
44 using ::testing::SaveArg;
45 using ::testing::_;
47 class AutoEnrollmentClientTest : public testing::Test {
48 protected:
49 AutoEnrollmentClientTest()
50 : scoped_testing_local_state_(
51 TestingBrowserProcess::GetGlobal()),
52 local_state_(scoped_testing_local_state_.Get()),
53 state_(AUTO_ENROLLMENT_STATE_PENDING) {}
55 void SetUp() override {
56 CreateClient(kStateKey, 4, 8);
57 ASSERT_FALSE(local_state_->GetUserPref(prefs::kShouldAutoEnroll));
58 ASSERT_FALSE(local_state_->GetUserPref(prefs::kAutoEnrollmentPowerLimit));
61 void TearDown() override {
62 // Flush any deletion tasks.
63 base::RunLoop().RunUntilIdle();
66 void CreateClient(const std::string& state_key,
67 int power_initial,
68 int power_limit) {
69 state_ = AUTO_ENROLLMENT_STATE_PENDING;
70 service_.reset(new MockDeviceManagementService());
71 EXPECT_CALL(*service_, StartJob(_, _, _, _, _, _))
72 .WillRepeatedly(SaveArg<5>(&last_request_));
73 client_.reset(new AutoEnrollmentClient(
74 base::Bind(&AutoEnrollmentClientTest::ProgressCallback,
75 base::Unretained(this)),
76 service_.get(), local_state_,
77 new net::TestURLRequestContextGetter(
78 base::MessageLoop::current()->task_runner()),
79 state_key, power_initial, power_limit));
82 void ProgressCallback(AutoEnrollmentState state) {
83 state_ = state;
86 void ServerWillFail(DeviceManagementStatus error) {
87 em::DeviceManagementResponse dummy_response;
88 EXPECT_CALL(*service_,
89 CreateJob(DeviceManagementRequestJob::TYPE_AUTO_ENROLLMENT, _))
90 .WillOnce(service_->FailJob(error));
93 void ServerWillReply(int64 modulus, bool with_hashes, bool with_id_hash) {
94 em::DeviceManagementResponse response;
95 em::DeviceAutoEnrollmentResponse* enrollment_response =
96 response.mutable_auto_enrollment_response();
97 if (modulus >= 0)
98 enrollment_response->set_expected_modulus(modulus);
99 if (with_hashes) {
100 for (int i = 0; i < 10; ++i) {
101 std::string state_key = base::StringPrintf("state_key %d", i);
102 std::string hash = crypto::SHA256HashString(state_key);
103 enrollment_response->mutable_hash()->Add()->assign(hash);
106 if (with_id_hash) {
107 enrollment_response->mutable_hash()->Add()->assign(kStateKeyHash,
108 crypto::kSHA256Length);
110 EXPECT_CALL(*service_,
111 CreateJob(DeviceManagementRequestJob::TYPE_AUTO_ENROLLMENT, _))
112 .WillOnce(service_->SucceedJob(response));
115 void ServerWillSendState(
116 const std::string& management_domain,
117 em::DeviceStateRetrievalResponse::RestoreMode restore_mode,
118 const std::string& device_disabled_message) {
119 em::DeviceManagementResponse response;
120 em::DeviceStateRetrievalResponse* state_response =
121 response.mutable_device_state_retrieval_response();
122 state_response->set_restore_mode(restore_mode);
123 state_response->set_management_domain(management_domain);
124 state_response->mutable_disabled_state()->set_message(
125 device_disabled_message);
126 EXPECT_CALL(
127 *service_,
128 CreateJob(DeviceManagementRequestJob::TYPE_DEVICE_STATE_RETRIEVAL, _))
129 .WillOnce(service_->SucceedJob(response));
132 void ServerWillReplyAsync(MockDeviceManagementJob** job) {
133 EXPECT_CALL(*service_,
134 CreateJob(DeviceManagementRequestJob::TYPE_AUTO_ENROLLMENT, _))
135 .WillOnce(service_->CreateAsyncJob(job));
138 bool HasCachedDecision() {
139 return local_state_->GetUserPref(prefs::kShouldAutoEnroll);
142 void VerifyCachedResult(bool should_enroll, int power_limit) {
143 base::FundamentalValue value_should_enroll(should_enroll);
144 base::FundamentalValue value_power_limit(power_limit);
145 EXPECT_TRUE(base::Value::Equals(
146 &value_should_enroll,
147 local_state_->GetUserPref(prefs::kShouldAutoEnroll)));
148 EXPECT_TRUE(base::Value::Equals(
149 &value_power_limit,
150 local_state_->GetUserPref(prefs::kAutoEnrollmentPowerLimit)));
153 bool HasServerBackedState() {
154 return local_state_->GetUserPref(prefs::kServerBackedDeviceState);
157 void VerifyServerBackedState(
158 const std::string& expected_management_domain,
159 const std::string& expected_restore_mode,
160 const std::string& expected_disabled_message) {
161 const base::Value* state =
162 local_state_->GetUserPref(prefs::kServerBackedDeviceState);
163 ASSERT_TRUE(state);
164 const base::DictionaryValue* state_dict = nullptr;
165 ASSERT_TRUE(state->GetAsDictionary(&state_dict));
167 std::string actual_management_domain;
168 EXPECT_TRUE(state_dict->GetString(kDeviceStateManagementDomain,
169 &actual_management_domain));
170 EXPECT_EQ(expected_management_domain, actual_management_domain);
172 if (!expected_restore_mode.empty()) {
173 std::string actual_restore_mode;
174 EXPECT_TRUE(state_dict->GetString(kDeviceStateRestoreMode,
175 &actual_restore_mode));
176 EXPECT_EQ(expected_restore_mode, actual_restore_mode);
177 } else {
178 EXPECT_FALSE(state_dict->HasKey(kDeviceStateRestoreMode));
181 std::string actual_disabled_message;
182 EXPECT_TRUE(state_dict->GetString(kDeviceStateDisabledMessage,
183 &actual_disabled_message));
184 EXPECT_EQ(expected_disabled_message, actual_disabled_message);
187 const em::DeviceAutoEnrollmentRequest& auto_enrollment_request() {
188 return last_request_.auto_enrollment_request();
191 content::TestBrowserThreadBundle browser_threads_;
192 ScopedTestingLocalState scoped_testing_local_state_;
193 TestingPrefServiceSimple* local_state_;
194 scoped_ptr<MockDeviceManagementService> service_;
195 scoped_ptr<AutoEnrollmentClient> client_;
196 em::DeviceManagementRequest last_request_;
197 AutoEnrollmentState state_;
199 private:
200 DISALLOW_COPY_AND_ASSIGN(AutoEnrollmentClientTest);
203 TEST_F(AutoEnrollmentClientTest, NetworkFailure) {
204 ServerWillFail(DM_STATUS_TEMPORARY_UNAVAILABLE);
205 client_->Start();
206 EXPECT_EQ(AUTO_ENROLLMENT_STATE_SERVER_ERROR, state_);
207 EXPECT_FALSE(HasCachedDecision());
208 EXPECT_FALSE(HasServerBackedState());
211 TEST_F(AutoEnrollmentClientTest, EmptyReply) {
212 ServerWillReply(-1, false, false);
213 client_->Start();
214 EXPECT_EQ(AUTO_ENROLLMENT_STATE_NO_ENROLLMENT, state_);
215 VerifyCachedResult(false, 8);
216 EXPECT_FALSE(HasServerBackedState());
219 TEST_F(AutoEnrollmentClientTest, ClientUploadsRightBits) {
220 ServerWillReply(-1, false, false);
221 client_->Start();
222 EXPECT_EQ(AUTO_ENROLLMENT_STATE_NO_ENROLLMENT, state_);
224 EXPECT_TRUE(auto_enrollment_request().has_remainder());
225 EXPECT_TRUE(auto_enrollment_request().has_modulus());
226 EXPECT_EQ(16, auto_enrollment_request().modulus());
227 EXPECT_EQ(kStateKeyHash[31] & 0xf, auto_enrollment_request().remainder());
228 VerifyCachedResult(false, 8);
229 EXPECT_FALSE(HasServerBackedState());
232 TEST_F(AutoEnrollmentClientTest, AskForMoreThenFail) {
233 InSequence sequence;
234 ServerWillReply(32, false, false);
235 ServerWillFail(DM_STATUS_TEMPORARY_UNAVAILABLE);
236 client_->Start();
237 EXPECT_EQ(AUTO_ENROLLMENT_STATE_SERVER_ERROR, state_);
238 EXPECT_FALSE(HasCachedDecision());
239 EXPECT_FALSE(HasServerBackedState());
242 TEST_F(AutoEnrollmentClientTest, AskForMoreThenEvenMore) {
243 InSequence sequence;
244 ServerWillReply(32, false, false);
245 ServerWillReply(64, false, false);
246 client_->Start();
247 EXPECT_EQ(AUTO_ENROLLMENT_STATE_SERVER_ERROR, state_);
248 EXPECT_FALSE(HasCachedDecision());
249 EXPECT_FALSE(HasServerBackedState());
252 TEST_F(AutoEnrollmentClientTest, AskForLess) {
253 InSequence sequence;
254 ServerWillReply(8, false, false);
255 ServerWillReply(-1, true, true);
256 ServerWillSendState(
257 "example.com",
258 em::DeviceStateRetrievalResponse::RESTORE_MODE_REENROLLMENT_ENFORCED,
259 kDisabledMessage);
260 client_->Start();
261 EXPECT_EQ(AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT, state_);
262 VerifyCachedResult(true, 8);
263 VerifyServerBackedState("example.com",
264 kDeviceStateRestoreModeReEnrollmentEnforced,
265 kDisabledMessage);
268 TEST_F(AutoEnrollmentClientTest, AskForSame) {
269 InSequence sequence;
270 ServerWillReply(16, false, false);
271 ServerWillReply(-1, true, true);
272 ServerWillSendState(
273 "example.com",
274 em::DeviceStateRetrievalResponse::RESTORE_MODE_REENROLLMENT_ENFORCED,
275 kDisabledMessage);
276 client_->Start();
277 EXPECT_EQ(AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT, state_);
278 VerifyCachedResult(true, 8);
279 VerifyServerBackedState("example.com",
280 kDeviceStateRestoreModeReEnrollmentEnforced,
281 kDisabledMessage);
284 TEST_F(AutoEnrollmentClientTest, AskForSameTwice) {
285 InSequence sequence;
286 ServerWillReply(16, false, false);
287 ServerWillReply(16, false, false);
288 client_->Start();
289 EXPECT_EQ(AUTO_ENROLLMENT_STATE_SERVER_ERROR, state_);
290 EXPECT_FALSE(HasCachedDecision());
291 EXPECT_FALSE(HasServerBackedState());
294 TEST_F(AutoEnrollmentClientTest, AskForTooMuch) {
295 ServerWillReply(512, false, false);
296 client_->Start();
297 EXPECT_EQ(AUTO_ENROLLMENT_STATE_SERVER_ERROR, state_);
298 EXPECT_FALSE(HasCachedDecision());
299 EXPECT_FALSE(HasServerBackedState());
302 TEST_F(AutoEnrollmentClientTest, AskNonPowerOf2) {
303 InSequence sequence;
304 ServerWillReply(100, false, false);
305 ServerWillReply(-1, false, false);
306 client_->Start();
307 EXPECT_EQ(AUTO_ENROLLMENT_STATE_NO_ENROLLMENT, state_);
308 EXPECT_TRUE(auto_enrollment_request().has_remainder());
309 EXPECT_TRUE(auto_enrollment_request().has_modulus());
310 EXPECT_EQ(128, auto_enrollment_request().modulus());
311 EXPECT_EQ(kStateKeyHash[31] & 0x7f, auto_enrollment_request().remainder());
312 VerifyCachedResult(false, 8);
313 EXPECT_FALSE(HasServerBackedState());
316 TEST_F(AutoEnrollmentClientTest, ConsumerDevice) {
317 ServerWillReply(-1, true, false);
318 client_->Start();
319 EXPECT_EQ(AUTO_ENROLLMENT_STATE_NO_ENROLLMENT, state_);
320 VerifyCachedResult(false, 8);
321 EXPECT_FALSE(HasServerBackedState());
323 // Network changes don't trigger retries after obtaining a response from
324 // the server.
325 client_->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_ETHERNET);
326 EXPECT_EQ(AUTO_ENROLLMENT_STATE_NO_ENROLLMENT, state_);
329 TEST_F(AutoEnrollmentClientTest, ForcedReEnrollment) {
330 ServerWillReply(-1, true, true);
331 ServerWillSendState(
332 "example.com",
333 em::DeviceStateRetrievalResponse::RESTORE_MODE_REENROLLMENT_ENFORCED,
334 kDisabledMessage);
335 client_->Start();
336 EXPECT_EQ(AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT, state_);
337 VerifyCachedResult(true, 8);
338 VerifyServerBackedState("example.com",
339 kDeviceStateRestoreModeReEnrollmentEnforced,
340 kDisabledMessage);
342 // Network changes don't trigger retries after obtaining a response from
343 // the server.
344 client_->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_ETHERNET);
345 EXPECT_EQ(AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT, state_);
348 TEST_F(AutoEnrollmentClientTest, RequestedReEnrollment) {
349 ServerWillReply(-1, true, true);
350 ServerWillSendState(
351 "example.com",
352 em::DeviceStateRetrievalResponse::RESTORE_MODE_REENROLLMENT_REQUESTED,
353 kDisabledMessage);
354 client_->Start();
355 EXPECT_EQ(AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT, state_);
356 VerifyCachedResult(true, 8);
357 VerifyServerBackedState("example.com",
358 kDeviceStateRestoreModeReEnrollmentRequested,
359 kDisabledMessage);
362 TEST_F(AutoEnrollmentClientTest, DeviceDisabled) {
363 ServerWillReply(-1, true, true);
364 ServerWillSendState(
365 "example.com",
366 em::DeviceStateRetrievalResponse::RESTORE_MODE_DISABLED,
367 kDisabledMessage);
368 client_->Start();
369 EXPECT_EQ(AUTO_ENROLLMENT_STATE_NO_ENROLLMENT, state_);
370 VerifyCachedResult(true, 8);
371 VerifyServerBackedState("example.com",
372 kDeviceStateRestoreModeDisabled,
373 kDisabledMessage);
376 TEST_F(AutoEnrollmentClientTest, NoBitsUploaded) {
377 CreateClient(kStateKey, 0, 0);
378 ServerWillReply(-1, false, false);
379 client_->Start();
380 EXPECT_EQ(AUTO_ENROLLMENT_STATE_NO_ENROLLMENT, state_);
381 EXPECT_TRUE(auto_enrollment_request().has_remainder());
382 EXPECT_TRUE(auto_enrollment_request().has_modulus());
383 EXPECT_EQ(1, auto_enrollment_request().modulus());
384 EXPECT_EQ(0, auto_enrollment_request().remainder());
385 VerifyCachedResult(false, 0);
386 EXPECT_FALSE(HasServerBackedState());
389 TEST_F(AutoEnrollmentClientTest, ManyBitsUploaded) {
390 int64 bottom62 = INT64_C(0x386e7244d097c3e6);
391 for (int i = 0; i <= 62; ++i) {
392 CreateClient(kStateKey, i, i);
393 ServerWillReply(-1, false, false);
394 client_->Start();
395 EXPECT_EQ(AUTO_ENROLLMENT_STATE_NO_ENROLLMENT, state_);
396 EXPECT_TRUE(auto_enrollment_request().has_remainder());
397 EXPECT_TRUE(auto_enrollment_request().has_modulus());
398 EXPECT_EQ(INT64_C(1) << i, auto_enrollment_request().modulus());
399 EXPECT_EQ(bottom62 % (INT64_C(1) << i),
400 auto_enrollment_request().remainder());
401 VerifyCachedResult(false, i);
402 EXPECT_FALSE(HasServerBackedState());
406 TEST_F(AutoEnrollmentClientTest, MoreThan32BitsUploaded) {
407 CreateClient(kStateKey, 10, 37);
408 InSequence sequence;
409 ServerWillReply(INT64_C(1) << 37, false, false);
410 ServerWillReply(-1, true, true);
411 ServerWillSendState(
412 "example.com",
413 em::DeviceStateRetrievalResponse::RESTORE_MODE_REENROLLMENT_ENFORCED,
414 kDisabledMessage);
415 client_->Start();
416 EXPECT_EQ(AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT, state_);
417 VerifyCachedResult(true, 37);
418 VerifyServerBackedState("example.com",
419 kDeviceStateRestoreModeReEnrollmentEnforced,
420 kDisabledMessage);
423 TEST_F(AutoEnrollmentClientTest, ReuseCachedDecision) {
424 // No bucket download requests should be issued.
425 EXPECT_CALL(*service_,
426 CreateJob(DeviceManagementRequestJob::TYPE_AUTO_ENROLLMENT, _))
427 .Times(0);
428 local_state_->SetUserPref(prefs::kShouldAutoEnroll,
429 new base::FundamentalValue(true));
430 local_state_->SetUserPref(prefs::kAutoEnrollmentPowerLimit,
431 new base::FundamentalValue(8));
433 // Note that device state will be retrieved every time, regardless of any
434 // cached information. This is intentional, the idea is that device state on
435 // the server may change.
436 ServerWillSendState(
437 "example.com",
438 em::DeviceStateRetrievalResponse::RESTORE_MODE_REENROLLMENT_ENFORCED,
439 kDisabledMessage);
441 client_->Start();
442 EXPECT_EQ(AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT, state_);
443 VerifyServerBackedState("example.com",
444 kDeviceStateRestoreModeReEnrollmentEnforced,
445 kDisabledMessage);
448 TEST_F(AutoEnrollmentClientTest, RetryIfPowerLargerThanCached) {
449 local_state_->SetUserPref(prefs::kShouldAutoEnroll,
450 new base::FundamentalValue(false));
451 local_state_->SetUserPref(prefs::kAutoEnrollmentPowerLimit,
452 new base::FundamentalValue(8));
453 CreateClient(kStateKey, 5, 10);
454 ServerWillReply(-1, true, true);
455 ServerWillSendState(
456 "example.com",
457 em::DeviceStateRetrievalResponse::RESTORE_MODE_REENROLLMENT_ENFORCED,
458 kDisabledMessage);
459 client_->Start();
460 EXPECT_EQ(AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT, state_);
461 VerifyServerBackedState("example.com",
462 kDeviceStateRestoreModeReEnrollmentEnforced,
463 kDisabledMessage);
466 TEST_F(AutoEnrollmentClientTest, NetworkChangeRetryAfterErrors) {
467 ServerWillFail(DM_STATUS_TEMPORARY_UNAVAILABLE);
468 client_->Start();
469 // Don't invoke the callback if there was a network failure.
470 EXPECT_EQ(AUTO_ENROLLMENT_STATE_SERVER_ERROR, state_);
471 EXPECT_FALSE(HasCachedDecision());
472 EXPECT_FALSE(HasServerBackedState());
474 // The client doesn't retry if no new connection became available.
475 client_->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_NONE);
476 EXPECT_EQ(AUTO_ENROLLMENT_STATE_SERVER_ERROR, state_);
477 EXPECT_FALSE(HasCachedDecision());
478 EXPECT_FALSE(HasServerBackedState());
480 // Retry once the network is back.
481 ServerWillReply(-1, true, true);
482 ServerWillSendState(
483 "example.com",
484 em::DeviceStateRetrievalResponse::RESTORE_MODE_REENROLLMENT_ENFORCED,
485 kDisabledMessage);
486 client_->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_ETHERNET);
487 EXPECT_EQ(AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT, state_);
488 EXPECT_TRUE(HasCachedDecision());
489 VerifyServerBackedState("example.com",
490 kDeviceStateRestoreModeReEnrollmentEnforced,
491 kDisabledMessage);
493 // Subsequent network changes don't trigger retries.
494 client_->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_NONE);
495 client_->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_ETHERNET);
496 EXPECT_EQ(AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT, state_);
497 EXPECT_TRUE(HasCachedDecision());
498 VerifyServerBackedState("example.com",
499 kDeviceStateRestoreModeReEnrollmentEnforced,
500 kDisabledMessage);
503 TEST_F(AutoEnrollmentClientTest, CancelAndDeleteSoonWithPendingRequest) {
504 MockDeviceManagementJob* job = NULL;
505 ServerWillReplyAsync(&job);
506 EXPECT_FALSE(job);
507 client_->Start();
508 ASSERT_TRUE(job);
509 EXPECT_EQ(AUTO_ENROLLMENT_STATE_PENDING, state_);
511 // Cancel while a request is in flight.
512 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
513 client_.release()->CancelAndDeleteSoon();
514 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
516 // The client cleans itself up once a reply is received.
517 job->SendResponse(DM_STATUS_TEMPORARY_UNAVAILABLE,
518 em::DeviceManagementResponse());
519 // The DeleteSoon task has been posted:
520 EXPECT_FALSE(base::MessageLoop::current()->IsIdleForTesting());
521 EXPECT_EQ(AUTO_ENROLLMENT_STATE_PENDING, state_);
524 TEST_F(AutoEnrollmentClientTest, NetworkChangedAfterCancelAndDeleteSoon) {
525 MockDeviceManagementJob* job = NULL;
526 ServerWillReplyAsync(&job);
527 EXPECT_FALSE(job);
528 client_->Start();
529 ASSERT_TRUE(job);
530 EXPECT_EQ(AUTO_ENROLLMENT_STATE_PENDING, state_);
532 // Cancel while a request is in flight.
533 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
534 AutoEnrollmentClient* client = client_.release();
535 client->CancelAndDeleteSoon();
536 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
538 // Network change events are ignored while a request is pending.
539 client->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_ETHERNET);
540 EXPECT_EQ(AUTO_ENROLLMENT_STATE_PENDING, state_);
542 // The client cleans itself up once a reply is received.
543 job->SendResponse(DM_STATUS_TEMPORARY_UNAVAILABLE,
544 em::DeviceManagementResponse());
545 // The DeleteSoon task has been posted:
546 EXPECT_FALSE(base::MessageLoop::current()->IsIdleForTesting());
547 EXPECT_EQ(AUTO_ENROLLMENT_STATE_PENDING, state_);
549 // Network changes that have been posted before are also ignored:
550 client->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_ETHERNET);
551 EXPECT_EQ(AUTO_ENROLLMENT_STATE_PENDING, state_);
554 TEST_F(AutoEnrollmentClientTest, CancelAndDeleteSoonAfterCompletion) {
555 ServerWillReply(-1, true, true);
556 ServerWillSendState(
557 "example.com",
558 em::DeviceStateRetrievalResponse::RESTORE_MODE_REENROLLMENT_ENFORCED,
559 kDisabledMessage);
560 client_->Start();
561 EXPECT_EQ(AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT, state_);
562 VerifyServerBackedState("example.com",
563 kDeviceStateRestoreModeReEnrollmentEnforced,
564 kDisabledMessage);
566 // The client will delete itself immediately if there are no pending
567 // requests.
568 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
569 client_.release()->CancelAndDeleteSoon();
570 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
573 TEST_F(AutoEnrollmentClientTest, CancelAndDeleteSoonAfterNetworkFailure) {
574 ServerWillFail(DM_STATUS_TEMPORARY_UNAVAILABLE);
575 client_->Start();
576 EXPECT_EQ(AUTO_ENROLLMENT_STATE_SERVER_ERROR, state_);
578 // The client will delete itself immediately if there are no pending
579 // requests.
580 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
581 client_.release()->CancelAndDeleteSoon();
582 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
585 TEST_F(AutoEnrollmentClientTest, NetworkFailureThenRequireUpdatedModulus) {
586 // This test verifies that if the first request fails due to a network
587 // problem then the second request will correctly handle an updated
588 // modulus request from the server.
590 ServerWillFail(DM_STATUS_REQUEST_FAILED);
591 client_->Start();
592 // Callback should signal the connection error.
593 EXPECT_EQ(AUTO_ENROLLMENT_STATE_CONNECTION_ERROR, state_);
594 EXPECT_FALSE(HasCachedDecision());
595 EXPECT_FALSE(HasServerBackedState());
596 Mock::VerifyAndClearExpectations(service_.get());
598 InSequence sequence;
599 // The default client uploads 4 bits. Make the server ask for 5.
600 ServerWillReply(1 << 5, false, false);
601 EXPECT_CALL(*service_, StartJob(_, _, _, _, _, _));
602 // Then reply with a valid response and include the hash.
603 ServerWillReply(-1, true, true);
604 EXPECT_CALL(*service_, StartJob(_, _, _, _, _, _));
605 // State download triggers.
606 ServerWillSendState(
607 "example.com",
608 em::DeviceStateRetrievalResponse::RESTORE_MODE_REENROLLMENT_ENFORCED,
609 kDisabledMessage);
610 EXPECT_CALL(*service_, StartJob(_, _, _, _, _, _));
612 // Trigger a network change event.
613 client_->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_ETHERNET);
614 EXPECT_EQ(AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT, state_);
615 EXPECT_TRUE(HasCachedDecision());
616 VerifyServerBackedState("example.com",
617 kDeviceStateRestoreModeReEnrollmentEnforced,
618 kDisabledMessage);
619 Mock::VerifyAndClearExpectations(service_.get());
622 } // namespace
623 } // namespace policy