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

Side by Side Diff: google_apis/gcm/gcm_client_impl_unittest.cc

Issue 215363007: [GCM] Adding basic G-services handling (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebasing Created 6 years, 8 months 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "google_apis/gcm/gcm_client_impl.h" 5 #include "google_apis/gcm/gcm_client_impl.h"
6 6
7 #include "base/files/scoped_temp_dir.h" 7 #include "base/files/scoped_temp_dir.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "base/strings/string_number_conversions.h"
10 #include "base/test/simple_test_clock.h" 11 #include "base/test/simple_test_clock.h"
11 #include "components/os_crypt/os_crypt.h" 12 #include "components/os_crypt/os_crypt.h"
12 #include "google_apis/gcm/base/mcs_message.h" 13 #include "google_apis/gcm/base/mcs_message.h"
13 #include "google_apis/gcm/base/mcs_util.h" 14 #include "google_apis/gcm/base/mcs_util.h"
14 #include "google_apis/gcm/engine/fake_connection_factory.h" 15 #include "google_apis/gcm/engine/fake_connection_factory.h"
15 #include "google_apis/gcm/engine/fake_connection_handler.h" 16 #include "google_apis/gcm/engine/fake_connection_handler.h"
16 #include "google_apis/gcm/protocol/android_checkin.pb.h" 17 #include "google_apis/gcm/protocol/android_checkin.pb.h"
17 #include "google_apis/gcm/protocol/checkin.pb.h" 18 #include "google_apis/gcm/protocol/checkin.pb.h"
18 #include "google_apis/gcm/protocol/mcs.pb.h" 19 #include "google_apis/gcm/protocol/mcs.pb.h"
19 #include "net/url_request/test_url_fetcher_factory.h" 20 #include "net/url_request/test_url_fetcher_factory.h"
(...skipping 10 matching lines...) Expand all
30 LOADING_COMPLETED, 31 LOADING_COMPLETED,
31 REGISTRATION_COMPLETED, 32 REGISTRATION_COMPLETED,
32 UNREGISTRATION_COMPLETED, 33 UNREGISTRATION_COMPLETED,
33 MESSAGE_SEND_ERROR, 34 MESSAGE_SEND_ERROR,
34 MESSAGE_RECEIVED, 35 MESSAGE_RECEIVED,
35 MESSAGES_DELETED, 36 MESSAGES_DELETED,
36 }; 37 };
37 38
38 const uint64 kDeviceAndroidId = 54321; 39 const uint64 kDeviceAndroidId = 54321;
39 const uint64 kDeviceSecurityToken = 12345; 40 const uint64 kDeviceSecurityToken = 12345;
41 const int64 kSettingsCheckinInterval = 0;
42 const char kSettingsCheckinIntervalKey[] = "checkin_interval";
43 const char kSettingsDefaultDigest[] = "default_digest";
40 const char kAppId[] = "app_id"; 44 const char kAppId[] = "app_id";
41 const char kSender[] = "project_id"; 45 const char kSender[] = "project_id";
42 const char kSender2[] = "project_id2"; 46 const char kSender2[] = "project_id2";
43 const char kSender3[] = "project_id3"; 47 const char kSender3[] = "project_id3";
44 const char kRegistrationResponsePrefix[] = "token="; 48 const char kRegistrationResponsePrefix[] = "token=";
45 const char kUnregistrationResponsePrefix[] = "deleted="; 49 const char kUnregistrationResponsePrefix[] = "deleted=";
46 50
47 // Helper for building arbitrary data messages. 51 // Helper for building arbitrary data messages.
48 MCSMessage BuildDownstreamMessage( 52 MCSMessage BuildDownstreamMessage(
49 const std::string& project_id, 53 const std::string& project_id,
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 public GCMClient::Delegate { 165 public GCMClient::Delegate {
162 public: 166 public:
163 GCMClientImplTest(); 167 GCMClientImplTest();
164 virtual ~GCMClientImplTest(); 168 virtual ~GCMClientImplTest();
165 169
166 virtual void SetUp() OVERRIDE; 170 virtual void SetUp() OVERRIDE;
167 171
168 void BuildGCMClient(); 172 void BuildGCMClient();
169 void InitializeGCMClient(); 173 void InitializeGCMClient();
170 void ReceiveMessageFromMCS(const MCSMessage& message); 174 void ReceiveMessageFromMCS(const MCSMessage& message);
171 void CompleteCheckin(uint64 android_id, uint64 security_token); 175 void CompleteCheckin(
176 uint64 android_id,
177 uint64 security_token,
178 const std::string& digest,
179 const std::map<std::string, std::string>& settings);
172 void CompleteRegistration(const std::string& registration_id); 180 void CompleteRegistration(const std::string& registration_id);
173 void CompleteUnregistration(const std::string& app_id); 181 void CompleteUnregistration(const std::string& app_id);
174 182
175 bool ExistsRegistration(const std::string& app_id) const; 183 bool ExistsRegistration(const std::string& app_id) const;
176 void AddRegistration(const std::string& app_id, 184 void AddRegistration(const std::string& app_id,
177 const std::vector<std::string>& sender_ids, 185 const std::vector<std::string>& sender_ids,
178 const std::string& registration_id); 186 const std::string& registration_id);
179 187
180 // GCMClient::Delegate overrides (for verification). 188 // GCMClient::Delegate overrides (for verification).
181 virtual void OnRegisterFinished(const std::string& app_id, 189 virtual void OnRegisterFinished(const std::string& app_id,
(...skipping 13 matching lines...) Expand all
195 const gcm::GCMClient::SendErrorDetails& send_error_details) OVERRIDE; 203 const gcm::GCMClient::SendErrorDetails& send_error_details) OVERRIDE;
196 virtual void OnGCMReady() OVERRIDE; 204 virtual void OnGCMReady() OVERRIDE;
197 205
198 GCMClientImpl* gcm_client() const { return gcm_client_.get(); } 206 GCMClientImpl* gcm_client() const { return gcm_client_.get(); }
199 FakeMCSClient* mcs_client() const { 207 FakeMCSClient* mcs_client() const {
200 return reinterpret_cast<FakeMCSClient*>(gcm_client_->mcs_client_.get()); 208 return reinterpret_cast<FakeMCSClient*>(gcm_client_->mcs_client_.get());
201 } 209 }
202 ConnectionFactory* connection_factory() const { 210 ConnectionFactory* connection_factory() const {
203 return gcm_client_->connection_factory_.get(); 211 return gcm_client_->connection_factory_.get();
204 } 212 }
213 GServicesSettingsMap& services_settings() {
214 return gcm_client_->gservices_settings_;
215 }
216 const std::string& services_digest() {
217 return gcm_client_->gservices_digest_;
218 }
205 219
206 void reset_last_event() { 220 void reset_last_event() {
207 last_event_ = NONE; 221 last_event_ = NONE;
208 last_app_id_.clear(); 222 last_app_id_.clear();
209 last_registration_id_.clear(); 223 last_registration_id_.clear();
210 last_message_id_.clear(); 224 last_message_id_.clear();
211 last_result_ = GCMClient::UNKNOWN_ERROR; 225 last_result_ = GCMClient::UNKNOWN_ERROR;
212 } 226 }
213 227
214 LastEvent last_event() const { return last_event_; } 228 LastEvent last_event() const { return last_event_; }
215 const std::string& last_app_id() const { return last_app_id_; } 229 const std::string& last_app_id() const { return last_app_id_; }
216 const std::string& last_registration_id() const { 230 const std::string& last_registration_id() const {
217 return last_registration_id_; 231 return last_registration_id_;
218 } 232 }
219 const std::string& last_message_id() const { return last_message_id_; } 233 const std::string& last_message_id() const { return last_message_id_; }
220 GCMClient::Result last_result() const { return last_result_; } 234 GCMClient::Result last_result() const { return last_result_; }
221 const GCMClient::IncomingMessage& last_message() const { 235 const GCMClient::IncomingMessage& last_message() const {
222 return last_message_; 236 return last_message_;
223 } 237 }
224 const GCMClient::SendErrorDetails& last_error_details() const { 238 const GCMClient::SendErrorDetails& last_error_details() const {
225 return last_error_details_; 239 return last_error_details_;
226 } 240 }
241 base::SimpleTestClock* clock() const {
242 return reinterpret_cast<base::SimpleTestClock*>(gcm_client_->clock_.get());
243 }
227 244
228 int64 CurrentTime(); 245 int64 CurrentTime();
229 246
230 private:
231 // Tooling. 247 // Tooling.
232 void PumpLoop(); 248 void PumpLoop();
233 void PumpLoopUntilIdle(); 249 void PumpLoopUntilIdle();
234 void QuitLoop(); 250 void QuitLoop();
251 void ResetLoop();
235 252
236 base::SimpleTestClock* clock() const { 253 bool CreateUniqueTempDir();
237 return reinterpret_cast<base::SimpleTestClock*>(gcm_client_->clock_.get());
238 }
239 254
255 private:
240 // Variables used for verification. 256 // Variables used for verification.
241 LastEvent last_event_; 257 LastEvent last_event_;
242 std::string last_app_id_; 258 std::string last_app_id_;
243 std::string last_registration_id_; 259 std::string last_registration_id_;
244 std::string last_message_id_; 260 std::string last_message_id_;
245 GCMClient::Result last_result_; 261 GCMClient::Result last_result_;
246 GCMClient::IncomingMessage last_message_; 262 GCMClient::IncomingMessage last_message_;
247 GCMClient::SendErrorDetails last_error_details_; 263 GCMClient::SendErrorDetails last_error_details_;
248 264
249 scoped_ptr<GCMClientImpl> gcm_client_; 265 scoped_ptr<GCMClientImpl> gcm_client_;
(...skipping 10 matching lines...) Expand all
260 GCMClientImplTest::GCMClientImplTest() 276 GCMClientImplTest::GCMClientImplTest()
261 : last_event_(NONE), 277 : last_event_(NONE),
262 last_result_(GCMClient::UNKNOWN_ERROR), 278 last_result_(GCMClient::UNKNOWN_ERROR),
263 url_request_context_getter_(new net::TestURLRequestContextGetter( 279 url_request_context_getter_(new net::TestURLRequestContextGetter(
264 message_loop_.message_loop_proxy())) { 280 message_loop_.message_loop_proxy())) {
265 } 281 }
266 282
267 GCMClientImplTest::~GCMClientImplTest() {} 283 GCMClientImplTest::~GCMClientImplTest() {}
268 284
269 void GCMClientImplTest::SetUp() { 285 void GCMClientImplTest::SetUp() {
270 ASSERT_TRUE(temp_directory_.CreateUniqueTempDir()); 286 ASSERT_TRUE(CreateUniqueTempDir());
271 run_loop_.reset(new base::RunLoop); 287 ResetLoop();
272 BuildGCMClient(); 288 BuildGCMClient();
273 InitializeGCMClient(); 289 InitializeGCMClient();
274 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken); 290 CompleteCheckin(kDeviceAndroidId,
291 kDeviceSecurityToken,
292 kSettingsDefaultDigest,
293 std::map<std::string, std::string>());
275 } 294 }
276 295
277 void GCMClientImplTest::PumpLoop() { 296 void GCMClientImplTest::PumpLoop() {
278 run_loop_->Run(); 297 run_loop_->Run();
279 run_loop_.reset(new base::RunLoop()); 298 run_loop_.reset(new base::RunLoop());
280 } 299 }
281 300
282 void GCMClientImplTest::PumpLoopUntilIdle() { 301 void GCMClientImplTest::PumpLoopUntilIdle() {
283 run_loop_->RunUntilIdle(); 302 run_loop_->RunUntilIdle();
284 run_loop_.reset(new base::RunLoop()); 303 run_loop_.reset(new base::RunLoop());
285 } 304 }
286 305
287 void GCMClientImplTest::QuitLoop() { 306 void GCMClientImplTest::QuitLoop() {
288 if (run_loop_ && run_loop_->running()) 307 if (run_loop_ && run_loop_->running())
289 run_loop_->Quit(); 308 run_loop_->Quit();
290 } 309 }
291 310
311 void GCMClientImplTest::ResetLoop() {
312 run_loop_.reset(new base::RunLoop);
313 }
314
315 bool GCMClientImplTest::CreateUniqueTempDir() {
316 return temp_directory_.CreateUniqueTempDir();
317 }
318
292 void GCMClientImplTest::BuildGCMClient() { 319 void GCMClientImplTest::BuildGCMClient() {
293 gcm_client_.reset(new GCMClientImpl( 320 gcm_client_.reset(new GCMClientImpl(
294 make_scoped_ptr<GCMInternalsBuilder>(new FakeGCMInternalsBuilder()))); 321 make_scoped_ptr<GCMInternalsBuilder>(new FakeGCMInternalsBuilder())));
295 } 322 }
296 323
297 void GCMClientImplTest::CompleteCheckin(uint64 android_id, 324 void GCMClientImplTest::CompleteCheckin(
298 uint64 security_token) { 325 uint64 android_id,
326 uint64 security_token,
327 const std::string& digest,
328 const std::map<std::string, std::string>& settings) {
299 checkin_proto::AndroidCheckinResponse response; 329 checkin_proto::AndroidCheckinResponse response;
300 response.set_stats_ok(true); 330 response.set_stats_ok(true);
301 response.set_android_id(android_id); 331 response.set_android_id(android_id);
302 response.set_security_token(security_token); 332 response.set_security_token(security_token);
303 333
334 // For testing GServices settings.
335 response.set_digest(digest);
336 for (std::map<std::string, std::string>::const_iterator it = settings.begin();
337 it != settings.end(); ++it) {
338 checkin_proto::GservicesSetting* setting = response.add_setting();
339 setting->set_name(it->first);
340 setting->set_value(it->second);
341 }
342
304 std::string response_string; 343 std::string response_string;
305 response.SerializeToString(&response_string); 344 response.SerializeToString(&response_string);
306 345
307 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 346 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
308 ASSERT_TRUE(fetcher); 347 ASSERT_TRUE(fetcher);
309 fetcher->set_response_code(net::HTTP_OK); 348 fetcher->set_response_code(net::HTTP_OK);
310 fetcher->SetResponseString(response_string); 349 fetcher->SetResponseString(response_string);
311 fetcher->delegate()->OnURLFetchComplete(fetcher); 350 fetcher->delegate()->OnURLFetchComplete(fetcher);
312 url_fetcher_factory_.RemoveFetcherFromMap(0); 351 url_fetcher_factory_.RemoveFetcherFromMap(0);
313 } 352 }
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after
588 EXPECT_EQ(500, mcs_client()->last_data_message_stanza().ttl()); 627 EXPECT_EQ(500, mcs_client()->last_data_message_stanza().ttl());
589 EXPECT_EQ(CurrentTime(), mcs_client()->last_data_message_stanza().sent()); 628 EXPECT_EQ(CurrentTime(), mcs_client()->last_data_message_stanza().sent());
590 EXPECT_EQ("007", mcs_client()->last_data_message_stanza().id()); 629 EXPECT_EQ("007", mcs_client()->last_data_message_stanza().id());
591 EXPECT_EQ("gcm@chrome.com", mcs_client()->last_data_message_stanza().from()); 630 EXPECT_EQ("gcm@chrome.com", mcs_client()->last_data_message_stanza().from());
592 EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to()); 631 EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to());
593 EXPECT_EQ("key", mcs_client()->last_data_message_stanza().app_data(0).key()); 632 EXPECT_EQ("key", mcs_client()->last_data_message_stanza().app_data(0).key());
594 EXPECT_EQ("value", 633 EXPECT_EQ("value",
595 mcs_client()->last_data_message_stanza().app_data(0).value()); 634 mcs_client()->last_data_message_stanza().app_data(0).value());
596 } 635 }
597 636
637 class GCMClientImplCheckinTest : public GCMClientImplTest {
638 public:
639 GCMClientImplCheckinTest();
640 virtual ~GCMClientImplCheckinTest();
641
642 virtual void SetUp() OVERRIDE;
643
644 std::map<std::string, std::string> GenerateSettings();
645 };
646
647 GCMClientImplCheckinTest::GCMClientImplCheckinTest() {}
648
649 GCMClientImplCheckinTest::~GCMClientImplCheckinTest() {}
650
651 void GCMClientImplCheckinTest::SetUp() {
652 ASSERT_TRUE(CreateUniqueTempDir());
653 ResetLoop();
654 BuildGCMClient();
655 InitializeGCMClient();
656 }
657
658 std::map<std::string, std::string>
659 GCMClientImplCheckinTest::GenerateSettings() {
660 std::map<std::string, std::string> settings;
661 settings[kSettingsCheckinIntervalKey] =
662 base::Int64ToString(kSettingsCheckinInterval);
663 return settings;
664 }
665
666 TEST_F(GCMClientImplCheckinTest, GServicesSettingsAfterInitialCheckin) {
667 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
668 kSettingsDefaultDigest, GenerateSettings());
669 EXPECT_EQ(base::Int64ToString(kSettingsCheckinInterval),
670 services_settings()[kSettingsCheckinIntervalKey]);
671 }
672
673 // This test only checks that periodic checkin happens.
674 TEST_F(GCMClientImplCheckinTest, PeriodicCheckin) {
675 std::map<std::string, std::string> settings = GenerateSettings();
676 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
677 kSettingsDefaultDigest, settings);
678 PumpLoopUntilIdle();
679
680 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
681 kSettingsDefaultDigest, settings);
682 }
683
684 // This test checks that checkin reponse with the same digest will not update
685 // G-services settings.
686 TEST_F(GCMClientImplCheckinTest, GServicesSettingsSameDigest) {
687 std::map<std::string, std::string> settings = GenerateSettings();
688 settings["checkin_url"] = "http://checkin.google.com";
689 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
690 kSettingsDefaultDigest, settings);
691 EXPECT_EQ(settings, services_settings());
692 EXPECT_EQ(kSettingsDefaultDigest, services_digest());
693 PumpLoopUntilIdle();
694
695 // Response will carry same digest and no settings.
696 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
697 kSettingsDefaultDigest, std::map<std::string, std::string>());
698 EXPECT_EQ(settings, services_settings());
699 EXPECT_EQ(kSettingsDefaultDigest, services_digest());
700 }
701
702 // Test that checkin response with a different digest will also update the
703 // G-services settings.
704 TEST_F(GCMClientImplCheckinTest, GServicesSettingsDifferentDigest) {
705 std::map<std::string, std::string> settings = GenerateSettings();
706 settings["checkin_url"] = "http://checkin.google.com";
707 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
708 kSettingsDefaultDigest, settings);
709 EXPECT_EQ(settings, services_settings());
710 EXPECT_EQ(kSettingsDefaultDigest, services_digest());
711 PumpLoopUntilIdle();
712
713 settings.clear();
714 settings["some_settings"] = "on second checkin";
715 settings[kSettingsCheckinIntervalKey] = "2100";
716 settings["checkin_url"] = "http://checkin.google.com";
717 std::string new_digest = "some_other_digest";
718
719 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, new_digest, settings);
720 EXPECT_EQ(settings, services_settings());
721 EXPECT_EQ(new_digest, services_digest());
722 }
723
598 } // namespace gcm 724 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698