| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/engine/mcs_client.h" | 5 #include "google_apis/gcm/engine/mcs_client.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/strings/string_number_conversions.h" |
| 11 #include "components/webdata/encryptor/encryptor.h" | 11 #include "components/webdata/encryptor/encryptor.h" |
| 12 #include "google_apis/gcm/base/mcs_util.h" | 12 #include "google_apis/gcm/base/mcs_util.h" |
| 13 #include "google_apis/gcm/engine/fake_connection_factory.h" | 13 #include "google_apis/gcm/engine/fake_connection_factory.h" |
| 14 #include "google_apis/gcm/engine/fake_connection_handler.h" | 14 #include "google_apis/gcm/engine/fake_connection_handler.h" |
| 15 #include "google_apis/gcm/engine/gcm_store_impl.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 17 namespace gcm { | 18 namespace gcm { |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 const uint64 kAndroidId = 54321; | 22 const uint64 kAndroidId = 54321; |
| 22 const uint64 kSecurityToken = 12345; | 23 const uint64 kSecurityToken = 12345; |
| 23 | 24 |
| 24 // Number of messages to send when testing batching. | 25 // Number of messages to send when testing batching. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 40 data_message.set_category(category); | 41 data_message.set_category(category); |
| 41 data_message.set_last_stream_id_received(last_stream_id_received); | 42 data_message.set_last_stream_id_received(last_stream_id_received); |
| 42 if (!persistent_id.empty()) | 43 if (!persistent_id.empty()) |
| 43 data_message.set_persistent_id(persistent_id); | 44 data_message.set_persistent_id(persistent_id); |
| 44 return MCSMessage(kDataMessageStanzaTag, data_message); | 45 return MCSMessage(kDataMessageStanzaTag, data_message); |
| 45 } | 46 } |
| 46 | 47 |
| 47 // MCSClient with overriden exposed persistent id logic. | 48 // MCSClient with overriden exposed persistent id logic. |
| 48 class TestMCSClient : public MCSClient { | 49 class TestMCSClient : public MCSClient { |
| 49 public: | 50 public: |
| 50 TestMCSClient(ConnectionFactory* connection_factory, | 51 TestMCSClient(ConnectionFactory* connection_factory, GCMStore* gcm_store) |
| 51 RMQStore* rmq_store) | 52 : MCSClient(connection_factory, gcm_store), next_id_(0) {} |
| 52 : MCSClient(connection_factory, rmq_store), | |
| 53 next_id_(0) { | |
| 54 } | |
| 55 | 53 |
| 56 virtual std::string GetNextPersistentId() OVERRIDE { | 54 virtual std::string GetNextPersistentId() OVERRIDE { |
| 57 return base::UintToString(++next_id_); | 55 return base::UintToString(++next_id_); |
| 58 } | 56 } |
| 59 | 57 |
| 60 private: | 58 private: |
| 61 uint32 next_id_; | 59 uint32 next_id_; |
| 62 }; | 60 }; |
| 63 | 61 |
| 64 class MCSClientTest : public testing::Test { | 62 class MCSClientTest : public testing::Test { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 88 private: | 86 private: |
| 89 void InitializationCallback(bool success, | 87 void InitializationCallback(bool success, |
| 90 uint64 restored_android_id, | 88 uint64 restored_android_id, |
| 91 uint64 restored_security_token); | 89 uint64 restored_security_token); |
| 92 void MessageReceivedCallback(const MCSMessage& message); | 90 void MessageReceivedCallback(const MCSMessage& message); |
| 93 void MessageSentCallback(const std::string& message_id); | 91 void MessageSentCallback(const std::string& message_id); |
| 94 | 92 |
| 95 base::ScopedTempDir temp_directory_; | 93 base::ScopedTempDir temp_directory_; |
| 96 base::MessageLoop message_loop_; | 94 base::MessageLoop message_loop_; |
| 97 scoped_ptr<base::RunLoop> run_loop_; | 95 scoped_ptr<base::RunLoop> run_loop_; |
| 98 scoped_ptr<RMQStore> rmq_store_; | 96 scoped_ptr<GCMStore> gcm_store_; |
| 99 | 97 |
| 100 FakeConnectionFactory connection_factory_; | 98 FakeConnectionFactory connection_factory_; |
| 101 scoped_ptr<TestMCSClient> mcs_client_; | 99 scoped_ptr<TestMCSClient> mcs_client_; |
| 102 bool init_success_; | 100 bool init_success_; |
| 103 uint64 restored_android_id_; | 101 uint64 restored_android_id_; |
| 104 uint64 restored_security_token_; | 102 uint64 restored_security_token_; |
| 105 scoped_ptr<MCSMessage> received_message_; | 103 scoped_ptr<MCSMessage> received_message_; |
| 106 std::string sent_message_id_; | 104 std::string sent_message_id_; |
| 107 }; | 105 }; |
| 108 | 106 |
| 109 MCSClientTest::MCSClientTest() | 107 MCSClientTest::MCSClientTest() |
| 110 : run_loop_(new base::RunLoop()), | 108 : run_loop_(new base::RunLoop()), |
| 111 init_success_(false), | 109 init_success_(false), |
| 112 restored_android_id_(0), | 110 restored_android_id_(0), |
| 113 restored_security_token_(0) { | 111 restored_security_token_(0) { |
| 114 EXPECT_TRUE(temp_directory_.CreateUniqueTempDir()); | 112 EXPECT_TRUE(temp_directory_.CreateUniqueTempDir()); |
| 115 run_loop_.reset(new base::RunLoop()); | 113 run_loop_.reset(new base::RunLoop()); |
| 116 | 114 |
| 117 // On OSX, prevent the Keychain permissions popup during unit tests. | 115 // On OSX, prevent the Keychain permissions popup during unit tests. |
| 118 #if defined(OS_MACOSX) | 116 #if defined(OS_MACOSX) |
| 119 Encryptor::UseMockKeychain(true); | 117 Encryptor::UseMockKeychain(true); |
| 120 #endif | 118 #endif |
| 121 } | 119 } |
| 122 | 120 |
| 123 MCSClientTest::~MCSClientTest() {} | 121 MCSClientTest::~MCSClientTest() {} |
| 124 | 122 |
| 125 void MCSClientTest::BuildMCSClient() { | 123 void MCSClientTest::BuildMCSClient() { |
| 126 rmq_store_.reset(new RMQStore(temp_directory_.path(), | 124 gcm_store_.reset(new GCMStoreImpl(temp_directory_.path(), |
| 127 message_loop_.message_loop_proxy())); | 125 message_loop_.message_loop_proxy())); |
| 128 mcs_client_.reset(new TestMCSClient(&connection_factory_, rmq_store_.get())); | 126 mcs_client_.reset(new TestMCSClient(&connection_factory_, gcm_store_.get())); |
| 129 } | 127 } |
| 130 | 128 |
| 131 void MCSClientTest::InitializeClient() { | 129 void MCSClientTest::InitializeClient() { |
| 132 rmq_store_->Load( | 130 gcm_store_->Load(base::Bind( |
| 133 base::Bind(&MCSClient::Initialize, | 131 &MCSClient::Initialize, |
| 134 base::Unretained(mcs_client_.get()), | 132 base::Unretained(mcs_client_.get()), |
| 135 base::Bind(&MCSClientTest::InitializationCallback, | 133 base::Bind(&MCSClientTest::InitializationCallback, |
| 136 base::Unretained(this)), | 134 base::Unretained(this)), |
| 137 base::Bind(&MCSClientTest::MessageReceivedCallback, | 135 base::Bind(&MCSClientTest::MessageReceivedCallback, |
| 138 base::Unretained(this)), | 136 base::Unretained(this)), |
| 139 base::Bind(&MCSClientTest::MessageSentCallback, | 137 base::Bind(&MCSClientTest::MessageSentCallback, base::Unretained(this)))); |
| 140 base::Unretained(this)))); | |
| 141 run_loop_->Run(); | 138 run_loop_->Run(); |
| 142 run_loop_.reset(new base::RunLoop()); | 139 run_loop_.reset(new base::RunLoop()); |
| 143 } | 140 } |
| 144 | 141 |
| 145 void MCSClientTest::LoginClient( | 142 void MCSClientTest::LoginClient( |
| 146 const std::vector<std::string>& acknowledged_ids) { | 143 const std::vector<std::string>& acknowledged_ids) { |
| 147 scoped_ptr<mcs_proto::LoginRequest> login_request = | 144 scoped_ptr<mcs_proto::LoginRequest> login_request = |
| 148 BuildLoginRequest(kAndroidId, kSecurityToken); | 145 BuildLoginRequest(kAndroidId, kSecurityToken); |
| 149 for (size_t i = 0; i < acknowledged_ids.size(); ++i) | 146 for (size_t i = 0; i < acknowledged_ids.size(); ++i) |
| 150 login_request->add_received_persistent_id(acknowledged_ids[i]); | 147 login_request->add_received_persistent_id(acknowledged_ids[i]); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 EXPECT_TRUE(init_success()); | 198 EXPECT_TRUE(init_success()); |
| 202 } | 199 } |
| 203 | 200 |
| 204 // Initialize a new client, shut it down, then restart the client. Should | 201 // Initialize a new client, shut it down, then restart the client. Should |
| 205 // reload the existing device credentials. | 202 // reload the existing device credentials. |
| 206 TEST_F(MCSClientTest, InitializeExisting) { | 203 TEST_F(MCSClientTest, InitializeExisting) { |
| 207 BuildMCSClient(); | 204 BuildMCSClient(); |
| 208 InitializeClient(); | 205 InitializeClient(); |
| 209 LoginClient(std::vector<std::string>()); | 206 LoginClient(std::vector<std::string>()); |
| 210 | 207 |
| 211 // Rebuild the client, to reload from the RMQ. | 208 // Rebuild the client, to reload from the GCM store. |
| 212 BuildMCSClient(); | 209 BuildMCSClient(); |
| 213 InitializeClient(); | 210 InitializeClient(); |
| 214 EXPECT_EQ(kAndroidId, restored_android_id()); | 211 EXPECT_EQ(kAndroidId, restored_android_id()); |
| 215 EXPECT_EQ(kSecurityToken, restored_security_token()); | 212 EXPECT_EQ(kSecurityToken, restored_security_token()); |
| 216 EXPECT_TRUE(init_success()); | 213 EXPECT_TRUE(init_success()); |
| 217 } | 214 } |
| 218 | 215 |
| 219 // Log in successfully to the MCS endpoint. | 216 // Log in successfully to the MCS endpoint. |
| 220 TEST_F(MCSClientTest, LoginSuccess) { | 217 TEST_F(MCSClientTest, LoginSuccess) { |
| 221 BuildMCSClient(); | 218 BuildMCSClient(); |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 533 BuildMCSClient(); | 530 BuildMCSClient(); |
| 534 InitializeClient(); | 531 InitializeClient(); |
| 535 LoginClient(std::vector<std::string>()); | 532 LoginClient(std::vector<std::string>()); |
| 536 EXPECT_TRUE(GetFakeHandler()-> | 533 EXPECT_TRUE(GetFakeHandler()-> |
| 537 AllOutgoingMessagesReceived()); | 534 AllOutgoingMessagesReceived()); |
| 538 } | 535 } |
| 539 | 536 |
| 540 } // namespace | 537 } // namespace |
| 541 | 538 |
| 542 } // namespace gcm | 539 } // namespace gcm |
| OLD | NEW |