| 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 "base/test/simple_test_clock.h" | 11 #include "base/test/simple_test_clock.h" |
| 12 #include "components/webdata/encryptor/encryptor.h" | 12 #include "components/webdata/encryptor/encryptor.h" |
| 13 #include "google_apis/gcm/base/mcs_util.h" | 13 #include "google_apis/gcm/base/mcs_util.h" |
| 14 #include "google_apis/gcm/engine/fake_connection_factory.h" | 14 #include "google_apis/gcm/engine/fake_connection_factory.h" |
| 15 #include "google_apis/gcm/engine/fake_connection_handler.h" | 15 #include "google_apis/gcm/engine/fake_connection_handler.h" |
| 16 #include "google_apis/gcm/engine/gcm_store_impl.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 18 |
| 18 namespace gcm { | 19 namespace gcm { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 const uint64 kAndroidId = 54321; | 23 const uint64 kAndroidId = 54321; |
| 23 const uint64 kSecurityToken = 12345; | 24 const uint64 kSecurityToken = 12345; |
| 24 | 25 |
| 25 // Number of messages to send when testing batching. | 26 // Number of messages to send when testing batching. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 50 data_message.set_ttl(ttl); | 51 data_message.set_ttl(ttl); |
| 51 data_message.set_sent(sent); | 52 data_message.set_sent(sent); |
| 52 return MCSMessage(kDataMessageStanzaTag, data_message); | 53 return MCSMessage(kDataMessageStanzaTag, data_message); |
| 53 } | 54 } |
| 54 | 55 |
| 55 // MCSClient with overriden exposed persistent id logic. | 56 // MCSClient with overriden exposed persistent id logic. |
| 56 class TestMCSClient : public MCSClient { | 57 class TestMCSClient : public MCSClient { |
| 57 public: | 58 public: |
| 58 TestMCSClient(base::Clock* clock, | 59 TestMCSClient(base::Clock* clock, |
| 59 ConnectionFactory* connection_factory, | 60 ConnectionFactory* connection_factory, |
| 60 RMQStore* rmq_store) | 61 GCMStore* gcm_store) |
| 61 : MCSClient(clock, connection_factory, rmq_store), | 62 : MCSClient(clock, connection_factory, gcm_store), |
| 62 next_id_(0) { | 63 next_id_(0) { |
| 63 } | 64 } |
| 64 | 65 |
| 65 virtual std::string GetNextPersistentId() OVERRIDE { | 66 virtual std::string GetNextPersistentId() OVERRIDE { |
| 66 return base::UintToString(++next_id_); | 67 return base::UintToString(++next_id_); |
| 67 } | 68 } |
| 68 | 69 |
| 69 private: | 70 private: |
| 70 uint32 next_id_; | 71 uint32 next_id_; |
| 71 }; | 72 }; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 100 uint64 restored_android_id, | 101 uint64 restored_android_id, |
| 101 uint64 restored_security_token); | 102 uint64 restored_security_token); |
| 102 void MessageReceivedCallback(const MCSMessage& message); | 103 void MessageReceivedCallback(const MCSMessage& message); |
| 103 void MessageSentCallback(const std::string& message_id); | 104 void MessageSentCallback(const std::string& message_id); |
| 104 | 105 |
| 105 base::SimpleTestClock clock_; | 106 base::SimpleTestClock clock_; |
| 106 | 107 |
| 107 base::ScopedTempDir temp_directory_; | 108 base::ScopedTempDir temp_directory_; |
| 108 base::MessageLoop message_loop_; | 109 base::MessageLoop message_loop_; |
| 109 scoped_ptr<base::RunLoop> run_loop_; | 110 scoped_ptr<base::RunLoop> run_loop_; |
| 110 scoped_ptr<RMQStore> rmq_store_; | 111 scoped_ptr<GCMStore> gcm_store_; |
| 111 | 112 |
| 112 FakeConnectionFactory connection_factory_; | 113 FakeConnectionFactory connection_factory_; |
| 113 scoped_ptr<TestMCSClient> mcs_client_; | 114 scoped_ptr<TestMCSClient> mcs_client_; |
| 114 bool init_success_; | 115 bool init_success_; |
| 115 uint64 restored_android_id_; | 116 uint64 restored_android_id_; |
| 116 uint64 restored_security_token_; | 117 uint64 restored_security_token_; |
| 117 scoped_ptr<MCSMessage> received_message_; | 118 scoped_ptr<MCSMessage> received_message_; |
| 118 std::string sent_message_id_; | 119 std::string sent_message_id_; |
| 119 }; | 120 }; |
| 120 | 121 |
| 121 MCSClientTest::MCSClientTest() | 122 MCSClientTest::MCSClientTest() |
| 122 : run_loop_(new base::RunLoop()), | 123 : run_loop_(new base::RunLoop()), |
| 123 init_success_(false), | 124 init_success_(false), |
| 124 restored_android_id_(0), | 125 restored_android_id_(0), |
| 125 restored_security_token_(0) { | 126 restored_security_token_(0) { |
| 126 EXPECT_TRUE(temp_directory_.CreateUniqueTempDir()); | 127 EXPECT_TRUE(temp_directory_.CreateUniqueTempDir()); |
| 127 run_loop_.reset(new base::RunLoop()); | 128 run_loop_.reset(new base::RunLoop()); |
| 128 | 129 |
| 129 // On OSX, prevent the Keychain permissions popup during unit tests. | 130 // On OSX, prevent the Keychain permissions popup during unit tests. |
| 130 #if defined(OS_MACOSX) | 131 #if defined(OS_MACOSX) |
| 131 Encryptor::UseMockKeychain(true); | 132 Encryptor::UseMockKeychain(true); |
| 132 #endif | 133 #endif |
| 133 } | 134 } |
| 134 | 135 |
| 135 MCSClientTest::~MCSClientTest() {} | 136 MCSClientTest::~MCSClientTest() {} |
| 136 | 137 |
| 137 void MCSClientTest::BuildMCSClient() { | 138 void MCSClientTest::BuildMCSClient() { |
| 138 rmq_store_.reset(new RMQStore(temp_directory_.path(), | 139 gcm_store_.reset(new GCMStoreImpl(temp_directory_.path(), |
| 139 message_loop_.message_loop_proxy())); | 140 message_loop_.message_loop_proxy())); |
| 140 mcs_client_.reset(new TestMCSClient(&clock_, | 141 mcs_client_.reset(new TestMCSClient(&clock_, |
| 141 &connection_factory_, | 142 &connection_factory_, |
| 142 rmq_store_.get())); | 143 gcm_store_.get())); |
| 143 } | 144 } |
| 144 | 145 |
| 145 void MCSClientTest::InitializeClient() { | 146 void MCSClientTest::InitializeClient() { |
| 146 rmq_store_->Load( | 147 gcm_store_->Load(base::Bind( |
| 147 base::Bind(&MCSClient::Initialize, | 148 &MCSClient::Initialize, |
| 148 base::Unretained(mcs_client_.get()), | 149 base::Unretained(mcs_client_.get()), |
| 149 base::Bind(&MCSClientTest::InitializationCallback, | 150 base::Bind(&MCSClientTest::InitializationCallback, |
| 150 base::Unretained(this)), | 151 base::Unretained(this)), |
| 151 base::Bind(&MCSClientTest::MessageReceivedCallback, | 152 base::Bind(&MCSClientTest::MessageReceivedCallback, |
| 152 base::Unretained(this)), | 153 base::Unretained(this)), |
| 153 base::Bind(&MCSClientTest::MessageSentCallback, | 154 base::Bind(&MCSClientTest::MessageSentCallback, base::Unretained(this)))); |
| 154 base::Unretained(this)))); | |
| 155 run_loop_->Run(); | 155 run_loop_->Run(); |
| 156 run_loop_.reset(new base::RunLoop()); | 156 run_loop_.reset(new base::RunLoop()); |
| 157 } | 157 } |
| 158 | 158 |
| 159 void MCSClientTest::LoginClient( | 159 void MCSClientTest::LoginClient( |
| 160 const std::vector<std::string>& acknowledged_ids) { | 160 const std::vector<std::string>& acknowledged_ids) { |
| 161 scoped_ptr<mcs_proto::LoginRequest> login_request = | 161 scoped_ptr<mcs_proto::LoginRequest> login_request = |
| 162 BuildLoginRequest(kAndroidId, kSecurityToken); | 162 BuildLoginRequest(kAndroidId, kSecurityToken); |
| 163 for (size_t i = 0; i < acknowledged_ids.size(); ++i) | 163 for (size_t i = 0; i < acknowledged_ids.size(); ++i) |
| 164 login_request->add_received_persistent_id(acknowledged_ids[i]); | 164 login_request->add_received_persistent_id(acknowledged_ids[i]); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 EXPECT_TRUE(init_success()); | 216 EXPECT_TRUE(init_success()); |
| 217 } | 217 } |
| 218 | 218 |
| 219 // Initialize a new client, shut it down, then restart the client. Should | 219 // Initialize a new client, shut it down, then restart the client. Should |
| 220 // reload the existing device credentials. | 220 // reload the existing device credentials. |
| 221 TEST_F(MCSClientTest, InitializeExisting) { | 221 TEST_F(MCSClientTest, InitializeExisting) { |
| 222 BuildMCSClient(); | 222 BuildMCSClient(); |
| 223 InitializeClient(); | 223 InitializeClient(); |
| 224 LoginClient(std::vector<std::string>()); | 224 LoginClient(std::vector<std::string>()); |
| 225 | 225 |
| 226 // Rebuild the client, to reload from the RMQ. | 226 // Rebuild the client, to reload from the GCM store. |
| 227 BuildMCSClient(); | 227 BuildMCSClient(); |
| 228 InitializeClient(); | 228 InitializeClient(); |
| 229 EXPECT_EQ(kAndroidId, restored_android_id()); | 229 EXPECT_EQ(kAndroidId, restored_android_id()); |
| 230 EXPECT_EQ(kSecurityToken, restored_security_token()); | 230 EXPECT_EQ(kSecurityToken, restored_security_token()); |
| 231 EXPECT_TRUE(init_success()); | 231 EXPECT_TRUE(init_success()); |
| 232 } | 232 } |
| 233 | 233 |
| 234 // Log in successfully to the MCS endpoint. | 234 // Log in successfully to the MCS endpoint. |
| 235 TEST_F(MCSClientTest, LoginSuccess) { | 235 TEST_F(MCSClientTest, LoginSuccess) { |
| 236 BuildMCSClient(); | 236 BuildMCSClient(); |
| (...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 608 InitializeClient(); | 608 InitializeClient(); |
| 609 LoginClient(std::vector<std::string>()); | 609 LoginClient(std::vector<std::string>()); |
| 610 PumpLoop(); | 610 PumpLoop(); |
| 611 EXPECT_FALSE(sent_message_id().empty()); | 611 EXPECT_FALSE(sent_message_id().empty()); |
| 612 EXPECT_TRUE(GetFakeHandler()->AllOutgoingMessagesReceived()); | 612 EXPECT_TRUE(GetFakeHandler()->AllOutgoingMessagesReceived()); |
| 613 } | 613 } |
| 614 | 614 |
| 615 } // namespace | 615 } // namespace |
| 616 | 616 |
| 617 } // namespace gcm | 617 } // namespace gcm |
| OLD | NEW |