OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/proximity_auth/cryptauth/cryptauth_enrollment_manager.h" | 5 #include "components/proximity_auth/cryptauth/cryptauth_enrollment_manager.h" |
6 | 6 |
7 #include "base/memory/weak_ptr.h" | 7 #include "base/memory/weak_ptr.h" |
8 #include "base/prefs/testing_pref_service.h" | 8 #include "base/prefs/testing_pref_service.h" |
9 #include "base/test/simple_test_clock.h" | 9 #include "base/test/simple_test_clock.h" |
10 #include "base/time/clock.h" | 10 #include "base/time/clock.h" |
11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
12 #include "components/proximity_auth/cryptauth/cryptauth_enroller.h" | 12 #include "components/proximity_auth/cryptauth/cryptauth_enroller.h" |
| 13 #include "components/proximity_auth/cryptauth/fake_cryptauth_gcm_manager.h" |
13 #include "components/proximity_auth/cryptauth/mock_sync_scheduler.h" | 14 #include "components/proximity_auth/cryptauth/mock_sync_scheduler.h" |
14 #include "components/proximity_auth/cryptauth/pref_names.h" | 15 #include "components/proximity_auth/cryptauth/pref_names.h" |
15 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 | 18 |
18 using ::testing::_; | 19 using ::testing::_; |
19 using ::testing::NiceMock; | 20 using ::testing::NiceMock; |
20 using ::testing::Return; | 21 using ::testing::Return; |
21 using ::testing::SaveArg; | 22 using ::testing::SaveArg; |
22 | 23 |
23 namespace proximity_auth { | 24 namespace proximity_auth { |
24 | 25 |
25 namespace { | 26 namespace { |
26 | 27 |
| 28 // The GCM registration id from a successful registration. |
| 29 const char kGCMRegistrationId[] = "new gcm registration id"; |
| 30 |
27 // The user's persistent key pair identifying the local device. | 31 // The user's persistent key pair identifying the local device. |
28 const char kUserPublicKey[] = "user public key"; | 32 const char kUserPublicKey[] = "user public key"; |
29 const char kUserPrivateKey[] = "user private key"; | 33 const char kUserPrivateKey[] = "user private key"; |
30 | 34 |
31 // The initial "Now" time for testing. | 35 // The initial "Now" time for testing. |
32 const double kInitialTimeNowSeconds = 20000000; | 36 const double kInitialTimeNowSeconds = 20000000; |
33 | 37 |
34 // A later "Now" time for testing. | 38 // A later "Now" time for testing. |
35 const double kLaterTimeNow = kInitialTimeNowSeconds + 30; | 39 const double kLaterTimeNow = kInitialTimeNowSeconds + 30; |
36 | 40 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 DISALLOW_COPY_AND_ASSIGN(MockCryptAuthEnrollerFactory); | 90 DISALLOW_COPY_AND_ASSIGN(MockCryptAuthEnrollerFactory); |
87 }; | 91 }; |
88 | 92 |
89 // Harness for testing CryptAuthEnrollmentManager. | 93 // Harness for testing CryptAuthEnrollmentManager. |
90 class TestCryptAuthEnrollmentManager : public CryptAuthEnrollmentManager { | 94 class TestCryptAuthEnrollmentManager : public CryptAuthEnrollmentManager { |
91 public: | 95 public: |
92 TestCryptAuthEnrollmentManager( | 96 TestCryptAuthEnrollmentManager( |
93 scoped_ptr<base::Clock> clock, | 97 scoped_ptr<base::Clock> clock, |
94 scoped_ptr<CryptAuthEnrollerFactory> enroller_factory, | 98 scoped_ptr<CryptAuthEnrollerFactory> enroller_factory, |
95 const cryptauth::GcmDeviceInfo& device_info, | 99 const cryptauth::GcmDeviceInfo& device_info, |
| 100 CryptAuthGCMManager* gcm_manager, |
96 PrefService* pref_service) | 101 PrefService* pref_service) |
97 : CryptAuthEnrollmentManager(clock.Pass(), | 102 : CryptAuthEnrollmentManager(clock.Pass(), |
98 enroller_factory.Pass(), | 103 enroller_factory.Pass(), |
99 kUserPublicKey, | 104 kUserPublicKey, |
100 kUserPrivateKey, | 105 kUserPrivateKey, |
101 device_info, | 106 device_info, |
| 107 gcm_manager, |
102 pref_service), | 108 pref_service), |
103 scoped_sync_scheduler_(new NiceMock<MockSyncScheduler>()), | 109 scoped_sync_scheduler_(new NiceMock<MockSyncScheduler>()), |
104 weak_sync_scheduler_factory_(scoped_sync_scheduler_.get()) {} | 110 weak_sync_scheduler_factory_(scoped_sync_scheduler_.get()) {} |
105 | 111 |
106 ~TestCryptAuthEnrollmentManager() override {} | 112 ~TestCryptAuthEnrollmentManager() override {} |
107 | 113 |
108 scoped_ptr<SyncScheduler> CreateSyncScheduler() override { | 114 scoped_ptr<SyncScheduler> CreateSyncScheduler() override { |
109 EXPECT_TRUE(scoped_sync_scheduler_); | 115 EXPECT_TRUE(scoped_sync_scheduler_); |
110 return scoped_sync_scheduler_.Pass(); | 116 return scoped_sync_scheduler_.Pass(); |
111 } | 117 } |
(...skipping 18 matching lines...) Expand all Loading... |
130 | 136 |
131 } // namespace | 137 } // namespace |
132 | 138 |
133 class ProximityAuthCryptAuthEnrollmentManagerTest | 139 class ProximityAuthCryptAuthEnrollmentManagerTest |
134 : public testing::Test, | 140 : public testing::Test, |
135 public CryptAuthEnrollmentManager::Observer { | 141 public CryptAuthEnrollmentManager::Observer { |
136 protected: | 142 protected: |
137 ProximityAuthCryptAuthEnrollmentManagerTest() | 143 ProximityAuthCryptAuthEnrollmentManagerTest() |
138 : clock_(new base::SimpleTestClock()), | 144 : clock_(new base::SimpleTestClock()), |
139 enroller_factory_(new MockCryptAuthEnrollerFactory()), | 145 enroller_factory_(new MockCryptAuthEnrollerFactory()), |
| 146 gcm_manager_(kGCMRegistrationId), |
140 enrollment_manager_(make_scoped_ptr(clock_), | 147 enrollment_manager_(make_scoped_ptr(clock_), |
141 make_scoped_ptr(enroller_factory_), | 148 make_scoped_ptr(enroller_factory_), |
142 device_info_, | 149 device_info_, |
| 150 &gcm_manager_, |
143 &pref_service_) {} | 151 &pref_service_) {} |
144 | 152 |
145 // testing::Test: | 153 // testing::Test: |
146 void SetUp() override { | 154 void SetUp() override { |
147 clock_->SetNow(base::Time::FromDoubleT(kInitialTimeNowSeconds)); | 155 clock_->SetNow(base::Time::FromDoubleT(kInitialTimeNowSeconds)); |
148 enrollment_manager_.AddObserver(this); | 156 enrollment_manager_.AddObserver(this); |
149 | 157 |
150 CryptAuthEnrollmentManager::RegisterPrefs(pref_service_.registry()); | 158 CryptAuthEnrollmentManager::RegisterPrefs(pref_service_.registry()); |
151 pref_service_.SetUserPref( | 159 pref_service_.SetUserPref( |
152 prefs::kCryptAuthEnrollmentIsRecoveringFromFailure, | 160 prefs::kCryptAuthEnrollmentIsRecoveringFromFailure, |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 // Owned by |enrollment_manager_|. | 219 // Owned by |enrollment_manager_|. |
212 base::SimpleTestClock* clock_; | 220 base::SimpleTestClock* clock_; |
213 | 221 |
214 // Owned by |enrollment_manager_|. | 222 // Owned by |enrollment_manager_|. |
215 MockCryptAuthEnrollerFactory* enroller_factory_; | 223 MockCryptAuthEnrollerFactory* enroller_factory_; |
216 | 224 |
217 cryptauth::GcmDeviceInfo device_info_; | 225 cryptauth::GcmDeviceInfo device_info_; |
218 | 226 |
219 TestingPrefServiceSimple pref_service_; | 227 TestingPrefServiceSimple pref_service_; |
220 | 228 |
| 229 FakeCryptAuthGCMManager gcm_manager_; |
| 230 |
221 TestCryptAuthEnrollmentManager enrollment_manager_; | 231 TestCryptAuthEnrollmentManager enrollment_manager_; |
222 | 232 |
223 DISALLOW_COPY_AND_ASSIGN(ProximityAuthCryptAuthEnrollmentManagerTest); | 233 DISALLOW_COPY_AND_ASSIGN(ProximityAuthCryptAuthEnrollmentManagerTest); |
224 }; | 234 }; |
225 | 235 |
226 TEST_F(ProximityAuthCryptAuthEnrollmentManagerTest, RegisterPrefs) { | 236 TEST_F(ProximityAuthCryptAuthEnrollmentManagerTest, RegisterPrefs) { |
227 TestingPrefServiceSimple pref_service; | 237 TestingPrefServiceSimple pref_service; |
228 CryptAuthEnrollmentManager::RegisterPrefs(pref_service.registry()); | 238 CryptAuthEnrollmentManager::RegisterPrefs(pref_service.registry()); |
229 EXPECT_TRUE(pref_service.FindPreference( | 239 EXPECT_TRUE(pref_service.FindPreference( |
230 prefs::kCryptAuthEnrollmentLastEnrollmentTimeSeconds)); | 240 prefs::kCryptAuthEnrollmentLastEnrollmentTimeSeconds)); |
(...skipping 30 matching lines...) Expand all Loading... |
261 TEST_F(ProximityAuthCryptAuthEnrollmentManagerTest, InitWithDefaultPrefs) { | 271 TEST_F(ProximityAuthCryptAuthEnrollmentManagerTest, InitWithDefaultPrefs) { |
262 scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock()); | 272 scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock()); |
263 clock->SetNow(base::Time::FromDoubleT(kInitialTimeNowSeconds)); | 273 clock->SetNow(base::Time::FromDoubleT(kInitialTimeNowSeconds)); |
264 base::TimeDelta elapsed_time = clock->Now() - base::Time::FromDoubleT(0); | 274 base::TimeDelta elapsed_time = clock->Now() - base::Time::FromDoubleT(0); |
265 | 275 |
266 TestingPrefServiceSimple pref_service; | 276 TestingPrefServiceSimple pref_service; |
267 CryptAuthEnrollmentManager::RegisterPrefs(pref_service.registry()); | 277 CryptAuthEnrollmentManager::RegisterPrefs(pref_service.registry()); |
268 | 278 |
269 TestCryptAuthEnrollmentManager enrollment_manager( | 279 TestCryptAuthEnrollmentManager enrollment_manager( |
270 clock.Pass(), make_scoped_ptr(new MockCryptAuthEnrollerFactory()), | 280 clock.Pass(), make_scoped_ptr(new MockCryptAuthEnrollerFactory()), |
271 device_info_, &pref_service); | 281 device_info_, &gcm_manager_, &pref_service); |
272 | 282 |
273 EXPECT_CALL( | 283 EXPECT_CALL( |
274 *enrollment_manager.GetSyncScheduler(), | 284 *enrollment_manager.GetSyncScheduler(), |
275 Start(elapsed_time, SyncScheduler::Strategy::AGGRESSIVE_RECOVERY)); | 285 Start(elapsed_time, SyncScheduler::Strategy::AGGRESSIVE_RECOVERY)); |
276 enrollment_manager.Start(); | 286 enrollment_manager.Start(); |
277 | 287 |
278 EXPECT_FALSE(enrollment_manager.IsEnrollmentValid()); | 288 EXPECT_FALSE(enrollment_manager.IsEnrollmentValid()); |
279 EXPECT_TRUE(enrollment_manager.GetLastEnrollmentTime().is_null()); | 289 EXPECT_TRUE(enrollment_manager.GetLastEnrollmentTime().is_null()); |
280 } | 290 } |
281 | 291 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 completion_callback = | 373 completion_callback = |
364 FireSchedulerForEnrollment(cryptauth::INVOCATION_REASON_FAILURE_RECOVERY); | 374 FireSchedulerForEnrollment(cryptauth::INVOCATION_REASON_FAILURE_RECOVERY); |
365 clock_->SetNow(base::Time::FromDoubleT(kLaterTimeNow + 30)); | 375 clock_->SetNow(base::Time::FromDoubleT(kLaterTimeNow + 30)); |
366 EXPECT_CALL(*this, OnEnrollmentFinishedProxy(true)); | 376 EXPECT_CALL(*this, OnEnrollmentFinishedProxy(true)); |
367 completion_callback.Run(true); | 377 completion_callback.Run(true); |
368 EXPECT_EQ(clock_->Now(), enrollment_manager_.GetLastEnrollmentTime()); | 378 EXPECT_EQ(clock_->Now(), enrollment_manager_.GetLastEnrollmentTime()); |
369 EXPECT_FALSE(pref_service_.GetBoolean( | 379 EXPECT_FALSE(pref_service_.GetBoolean( |
370 prefs::kCryptAuthEnrollmentIsRecoveringFromFailure)); | 380 prefs::kCryptAuthEnrollmentIsRecoveringFromFailure)); |
371 } | 381 } |
372 | 382 |
| 383 TEST_F(ProximityAuthCryptAuthEnrollmentManagerTest, |
| 384 EnrollWithoutGCMRegistrationId) { |
| 385 // Initialize |enrollment_manager_|. |
| 386 ON_CALL(*sync_scheduler(), GetStrategy()) |
| 387 .WillByDefault(Return(SyncScheduler::Strategy::PERIODIC_REFRESH)); |
| 388 gcm_manager_.set_registration_id(std::string()); |
| 389 enrollment_manager_.Start(); |
| 390 |
| 391 // Trigger a sync request. |
| 392 EXPECT_CALL(*this, OnEnrollmentStartedProxy()); |
| 393 auto sync_request = make_scoped_ptr( |
| 394 new SyncScheduler::SyncRequest(enrollment_manager_.GetSyncScheduler())); |
| 395 static_cast<SyncScheduler::Delegate*>(&enrollment_manager_) |
| 396 ->OnSyncRequested(sync_request.Pass()); |
| 397 |
| 398 // Complete GCM registration successfully. |
| 399 CryptAuthEnroller::EnrollmentFinishedCallback enrollment_callback; |
| 400 EXPECT_CALL(*next_cryptauth_enroller(), |
| 401 Enroll(kUserPublicKey, kUserPrivateKey, _, |
| 402 cryptauth::INVOCATION_REASON_PERIODIC, _)) |
| 403 .WillOnce(SaveArg<4>(&enrollment_callback)); |
| 404 ASSERT_TRUE(gcm_manager_.registration_in_progress()); |
| 405 gcm_manager_.CompleteRegistration(kGCMRegistrationId); |
| 406 |
| 407 // Complete CryptAuth enrollment. |
| 408 ASSERT_FALSE(enrollment_callback.is_null()); |
| 409 EXPECT_CALL(*this, OnEnrollmentFinishedProxy(true)); |
| 410 enrollment_callback.Run(true); |
| 411 } |
| 412 |
| 413 TEST_F(ProximityAuthCryptAuthEnrollmentManagerTest, GCMRegistrationFails) { |
| 414 // Initialize |enrollment_manager_|. |
| 415 ON_CALL(*sync_scheduler(), GetStrategy()) |
| 416 .WillByDefault(Return(SyncScheduler::Strategy::PERIODIC_REFRESH)); |
| 417 gcm_manager_.set_registration_id(std::string()); |
| 418 enrollment_manager_.Start(); |
| 419 |
| 420 // Trigger a sync request. |
| 421 EXPECT_CALL(*this, OnEnrollmentStartedProxy()); |
| 422 auto sync_request = make_scoped_ptr( |
| 423 new SyncScheduler::SyncRequest(enrollment_manager_.GetSyncScheduler())); |
| 424 static_cast<SyncScheduler::Delegate*>(&enrollment_manager_) |
| 425 ->OnSyncRequested(sync_request.Pass()); |
| 426 |
| 427 // Complete GCM registration with failure. |
| 428 EXPECT_CALL(*this, OnEnrollmentFinishedProxy(false)); |
| 429 gcm_manager_.CompleteRegistration(std::string()); |
| 430 } |
| 431 |
| 432 TEST_F(ProximityAuthCryptAuthEnrollmentManagerTest, ReenrollOnGCMPushMessage) { |
| 433 enrollment_manager_.Start(); |
| 434 |
| 435 // Simulate receiving a GCM push message, forcing the device to re-enroll. |
| 436 gcm_manager_.PushReenrollMessage(); |
| 437 auto completion_callback = |
| 438 FireSchedulerForEnrollment(cryptauth::INVOCATION_REASON_SERVER_INITIATED); |
| 439 |
| 440 EXPECT_CALL(*this, OnEnrollmentFinishedProxy(true)); |
| 441 completion_callback.Run(true); |
| 442 } |
| 443 |
373 } // namespace proximity_auth | 444 } // namespace proximity_auth |
OLD | NEW |