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

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: Addressing feedback from Nicolas 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
« no previous file with comments | « google_apis/gcm/gcm_client_impl.cc ('k') | google_apis/gcm/tools/mcs_probe.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 }
227 241
228 int64 CurrentTime(); 242 int64 CurrentTime();
229 243
230 private: 244 protected:
Nicolas Zea 2014/04/02 18:36:53 nit: just leave these public
231 // Tooling. 245 // Tooling.
232 void PumpLoop(); 246 void PumpLoop();
233 void PumpLoopUntilIdle(); 247 void PumpLoopUntilIdle();
234 void QuitLoop(); 248 void QuitLoop();
249 void ResetLoop();
250
251 bool CreateUniqueTempDir();
235 252
236 base::SimpleTestClock* clock() const { 253 base::SimpleTestClock* clock() const {
237 return reinterpret_cast<base::SimpleTestClock*>(gcm_client_->clock_.get()); 254 return reinterpret_cast<base::SimpleTestClock*>(gcm_client_->clock_.get());
238 } 255 }
239 256
257 private:
240 // Variables used for verification. 258 // Variables used for verification.
241 LastEvent last_event_; 259 LastEvent last_event_;
242 std::string last_app_id_; 260 std::string last_app_id_;
243 std::string last_registration_id_; 261 std::string last_registration_id_;
244 std::string last_message_id_; 262 std::string last_message_id_;
245 GCMClient::Result last_result_; 263 GCMClient::Result last_result_;
246 GCMClient::IncomingMessage last_message_; 264 GCMClient::IncomingMessage last_message_;
247 GCMClient::SendErrorDetails last_error_details_; 265 GCMClient::SendErrorDetails last_error_details_;
248 266
249 scoped_ptr<GCMClientImpl> gcm_client_; 267 scoped_ptr<GCMClientImpl> gcm_client_;
(...skipping 10 matching lines...) Expand all
260 GCMClientImplTest::GCMClientImplTest() 278 GCMClientImplTest::GCMClientImplTest()
261 : last_event_(NONE), 279 : last_event_(NONE),
262 last_result_(GCMClient::UNKNOWN_ERROR), 280 last_result_(GCMClient::UNKNOWN_ERROR),
263 url_request_context_getter_(new net::TestURLRequestContextGetter( 281 url_request_context_getter_(new net::TestURLRequestContextGetter(
264 message_loop_.message_loop_proxy())) { 282 message_loop_.message_loop_proxy())) {
265 } 283 }
266 284
267 GCMClientImplTest::~GCMClientImplTest() {} 285 GCMClientImplTest::~GCMClientImplTest() {}
268 286
269 void GCMClientImplTest::SetUp() { 287 void GCMClientImplTest::SetUp() {
270 ASSERT_TRUE(temp_directory_.CreateUniqueTempDir()); 288 ASSERT_TRUE(CreateUniqueTempDir());
271 run_loop_.reset(new base::RunLoop); 289 ResetLoop();
272 BuildGCMClient(); 290 BuildGCMClient();
273 InitializeGCMClient(); 291 InitializeGCMClient();
274 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken); 292 CompleteCheckin(kDeviceAndroidId,
293 kDeviceSecurityToken,
294 kSettingsDefaultDigest,
295 std::map<std::string, std::string>());
275 } 296 }
276 297
277 void GCMClientImplTest::PumpLoop() { 298 void GCMClientImplTest::PumpLoop() {
278 run_loop_->Run(); 299 run_loop_->Run();
279 run_loop_.reset(new base::RunLoop()); 300 run_loop_.reset(new base::RunLoop());
280 } 301 }
281 302
282 void GCMClientImplTest::PumpLoopUntilIdle() { 303 void GCMClientImplTest::PumpLoopUntilIdle() {
283 run_loop_->RunUntilIdle(); 304 run_loop_->RunUntilIdle();
284 run_loop_.reset(new base::RunLoop()); 305 run_loop_.reset(new base::RunLoop());
285 } 306 }
286 307
287 void GCMClientImplTest::QuitLoop() { 308 void GCMClientImplTest::QuitLoop() {
288 if (run_loop_ && run_loop_->running()) 309 if (run_loop_ && run_loop_->running())
289 run_loop_->Quit(); 310 run_loop_->Quit();
290 } 311 }
291 312
313 void GCMClientImplTest::ResetLoop() {
314 run_loop_.reset(new base::RunLoop);
315 }
316
317 bool GCMClientImplTest::CreateUniqueTempDir() {
318 return temp_directory_.CreateUniqueTempDir();
319 }
320
292 void GCMClientImplTest::BuildGCMClient() { 321 void GCMClientImplTest::BuildGCMClient() {
293 gcm_client_.reset(new GCMClientImpl( 322 gcm_client_.reset(new GCMClientImpl(
294 make_scoped_ptr<GCMInternalsBuilder>(new FakeGCMInternalsBuilder()))); 323 make_scoped_ptr<GCMInternalsBuilder>(new FakeGCMInternalsBuilder())));
295 } 324 }
296 325
297 void GCMClientImplTest::CompleteCheckin(uint64 android_id, 326 void GCMClientImplTest::CompleteCheckin(
298 uint64 security_token) { 327 uint64 android_id,
328 uint64 security_token,
329 const std::string& digest,
330 const std::map<std::string, std::string>& settings) {
299 checkin_proto::AndroidCheckinResponse response; 331 checkin_proto::AndroidCheckinResponse response;
300 response.set_stats_ok(true); 332 response.set_stats_ok(true);
301 response.set_android_id(android_id); 333 response.set_android_id(android_id);
302 response.set_security_token(security_token); 334 response.set_security_token(security_token);
303 335
336 // For testing GServices settings.
337 response.set_digest(digest);
338 for (std::map<std::string, std::string>::const_iterator it = settings.begin();
339 it != settings.end(); ++it) {
340 checkin_proto::GservicesSetting* setting = response.add_setting();
341 setting->set_name(it->first);
342 setting->set_value(it->second);
343 }
344
304 std::string response_string; 345 std::string response_string;
305 response.SerializeToString(&response_string); 346 response.SerializeToString(&response_string);
306 347
307 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 348 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
308 ASSERT_TRUE(fetcher); 349 ASSERT_TRUE(fetcher);
309 fetcher->set_response_code(net::HTTP_OK); 350 fetcher->set_response_code(net::HTTP_OK);
310 fetcher->SetResponseString(response_string); 351 fetcher->SetResponseString(response_string);
311 fetcher->delegate()->OnURLFetchComplete(fetcher); 352 fetcher->delegate()->OnURLFetchComplete(fetcher);
312 url_fetcher_factory_.RemoveFetcherFromMap(0); 353 url_fetcher_factory_.RemoveFetcherFromMap(0);
313 } 354 }
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after
588 EXPECT_EQ(500, mcs_client()->last_data_message_stanza().ttl()); 629 EXPECT_EQ(500, mcs_client()->last_data_message_stanza().ttl());
589 EXPECT_EQ(CurrentTime(), mcs_client()->last_data_message_stanza().sent()); 630 EXPECT_EQ(CurrentTime(), mcs_client()->last_data_message_stanza().sent());
590 EXPECT_EQ("007", mcs_client()->last_data_message_stanza().id()); 631 EXPECT_EQ("007", mcs_client()->last_data_message_stanza().id());
591 EXPECT_EQ("gcm@chrome.com", mcs_client()->last_data_message_stanza().from()); 632 EXPECT_EQ("gcm@chrome.com", mcs_client()->last_data_message_stanza().from());
592 EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to()); 633 EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to());
593 EXPECT_EQ("key", mcs_client()->last_data_message_stanza().app_data(0).key()); 634 EXPECT_EQ("key", mcs_client()->last_data_message_stanza().app_data(0).key());
594 EXPECT_EQ("value", 635 EXPECT_EQ("value",
595 mcs_client()->last_data_message_stanza().app_data(0).value()); 636 mcs_client()->last_data_message_stanza().app_data(0).value());
596 } 637 }
597 638
639 class GCMClientImplCheckinTest : public GCMClientImplTest {
640 public:
641 GCMClientImplCheckinTest();
642 virtual ~GCMClientImplCheckinTest();
643
644 virtual void SetUp() OVERRIDE;
645
646 std::map<std::string, std::string> GenerateSettings();
647 };
648
649 GCMClientImplCheckinTest::GCMClientImplCheckinTest() {}
650
651 GCMClientImplCheckinTest::~GCMClientImplCheckinTest() {}
652
653 void GCMClientImplCheckinTest::SetUp() {
654 ASSERT_TRUE(CreateUniqueTempDir());
655 ResetLoop();
656 BuildGCMClient();
657 InitializeGCMClient();
658 }
659
660 std::map<std::string, std::string>
661 GCMClientImplCheckinTest::GenerateSettings() {
662 std::map<std::string, std::string> settings;
663 settings[kSettingsCheckinIntervalKey] =
664 base::Int64ToString(kSettingsCheckinInterval);
665 return settings;
666 }
667
668 TEST_F(GCMClientImplCheckinTest, GServicesSettingsAfterInitialCheckin) {
669 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
670 kSettingsDefaultDigest, GenerateSettings());
671 EXPECT_EQ(base::Int64ToString(kSettingsCheckinInterval),
672 services_settings()[kSettingsCheckinIntervalKey]);
673 }
674
675 // This test only checks that periodic checkin happens.
676 TEST_F(GCMClientImplCheckinTest, PeriodicCheckin) {
677 std::map<std::string, std::string> settings = GenerateSettings();
678 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
679 kSettingsDefaultDigest, settings);
680 PumpLoopUntilIdle();
681
682 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
683 kSettingsDefaultDigest, settings);
684 }
685
686 // This test checks that checkin reponse with the same digest will not update
687 // G-services settings.
688 TEST_F(GCMClientImplCheckinTest, GServicesSettingsSameDigest) {
689 std::map<std::string, std::string> settings = GenerateSettings();
690 settings["checkin_url"] = "http://checkin.google.com";
691 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
692 kSettingsDefaultDigest, settings);
693 EXPECT_EQ(settings, services_settings());
694 EXPECT_EQ(kSettingsDefaultDigest, services_digest());
695 PumpLoopUntilIdle();
696
697 // Response will carry same digest and no settings.
698 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
699 kSettingsDefaultDigest, std::map<std::string, std::string>());
700 EXPECT_EQ(settings, services_settings());
701 EXPECT_EQ(kSettingsDefaultDigest, services_digest());
702 }
703
704 // Test that checkin response with a different digest will also update the
705 // G-services settings.
706 TEST_F(GCMClientImplCheckinTest, GServicesSettingsDifferentDigest) {
707 std::map<std::string, std::string> settings = GenerateSettings();
708 settings["checkin_url"] = "http://checkin.google.com";
709 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
710 kSettingsDefaultDigest, settings);
711 EXPECT_EQ(settings, services_settings());
712 EXPECT_EQ(kSettingsDefaultDigest, services_digest());
713 PumpLoopUntilIdle();
714
715 settings.clear();
716 settings["some_settings"] = "on second checkin";
717 settings[kSettingsCheckinIntervalKey] = "2100";
718 settings["checkin_url"] = "http://checkin.google.com";
719 std::string new_digest = "some_other_digest";
720
721 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, new_digest, settings);
722 EXPECT_EQ(settings, services_settings());
723 EXPECT_EQ(new_digest, services_digest());
724 }
725
598 } // namespace gcm 726 } // namespace gcm
OLDNEW
« no previous file with comments | « google_apis/gcm/gcm_client_impl.cc ('k') | google_apis/gcm/tools/mcs_probe.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698