[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 5 | #include "components/gcm_driver/gcm_client_impl.h" |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 6 | |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 9 | #include <initializer_list> |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 10 | #include <memory> |
| 11 | |
[email protected] | fdeaf973 | 2014-04-11 21:02:15 | [diff] [blame] | 12 | #include "base/command_line.h" |
jianli | 00b4600f | 2015-02-10 23:32:49 | [diff] [blame] | 13 | #include "base/files/file_path.h" |
| 14 | #include "base/files/file_util.h" |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 15 | #include "base/files/scoped_temp_dir.h" |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 16 | #include "base/macros.h" |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 17 | #include "base/memory/ptr_util.h" |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 18 | #include "base/strings/string_number_conversions.h" |
johnme | 6d6c780 | 2016-09-29 22:27:16 | [diff] [blame] | 19 | #include "base/test/histogram_tester.h" |
jianli | 78b5604 | 2015-06-17 01:21:22 | [diff] [blame] | 20 | #include "base/test/test_mock_time_task_runner.h" |
gab | 7966d31 | 2016-05-11 20:35:01 | [diff] [blame] | 21 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 22 | #include "base/time/clock.h" |
chirantan | 26436e40 | 2014-10-24 19:44:42 | [diff] [blame] | 23 | #include "base/timer/timer.h" |
[email protected] | 446f73c2 | 2014-05-14 20:47:18 | [diff] [blame] | 24 | #include "google_apis/gcm/base/fake_encryptor.h" |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 25 | #include "google_apis/gcm/base/mcs_message.h" |
| 26 | #include "google_apis/gcm/base/mcs_util.h" |
| 27 | #include "google_apis/gcm/engine/fake_connection_factory.h" |
| 28 | #include "google_apis/gcm/engine/fake_connection_handler.h" |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 29 | #include "google_apis/gcm/engine/gservices_settings.h" |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 30 | #include "google_apis/gcm/monitoring/gcm_stats_recorder.h" |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 31 | #include "google_apis/gcm/protocol/android_checkin.pb.h" |
| 32 | #include "google_apis/gcm/protocol/checkin.pb.h" |
| 33 | #include "google_apis/gcm/protocol/mcs.pb.h" |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 34 | #include "net/test/gtest_util.h" |
| 35 | #include "net/test/scoped_disable_exit_on_dfatal.h" |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 36 | #include "net/url_request/test_url_fetcher_factory.h" |
| 37 | #include "net/url_request/url_fetcher_delegate.h" |
| 38 | #include "net/url_request/url_request_test_util.h" |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 39 | #include "testing/gtest/include/gtest/gtest-spi.h" |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 40 | #include "testing/gtest/include/gtest/gtest.h" |
Chris Mumford | a884aa1f | 2017-10-24 22:46:32 | [diff] [blame^] | 41 | #include "third_party/leveldatabase/leveldb_chrome.h" |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 42 | |
| 43 | namespace gcm { |
| 44 | |
| 45 | namespace { |
| 46 | |
| 47 | enum LastEvent { |
| 48 | NONE, |
| 49 | LOADING_COMPLETED, |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 50 | REGISTRATION_COMPLETED, |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 51 | UNREGISTRATION_COMPLETED, |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 52 | MESSAGE_SEND_ERROR, |
[email protected] | 292af2b2 | 2014-08-06 19:42:45 | [diff] [blame] | 53 | MESSAGE_SEND_ACK, |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 54 | MESSAGE_RECEIVED, |
| 55 | MESSAGES_DELETED, |
| 56 | }; |
| 57 | |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 58 | const char kChromeVersion[] = "45.0.0.1"; |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 59 | const uint64_t kDeviceAndroidId = 54321; |
| 60 | const uint64_t kDeviceSecurityToken = 12345; |
| 61 | const uint64_t kDeviceAndroidId2 = 11111; |
| 62 | const uint64_t kDeviceSecurityToken2 = 2222; |
| 63 | const int64_t kSettingsCheckinInterval = 16 * 60 * 60; |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 64 | const char kProductCategoryForSubtypes[] = "com.chrome.macosx"; |
| 65 | const char kExtensionAppId[] = "abcdefghijklmnopabcdefghijklmnop"; |
| 66 | const char kSubtypeAppId[] = "app_id"; |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 67 | const char kSender[] = "project_id"; |
| 68 | const char kSender2[] = "project_id2"; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 69 | const char kRegistrationResponsePrefix[] = "token="; |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 70 | const char kUnregistrationResponsePrefix[] = "deleted="; |
peter | a5aedc5 | 2015-07-22 10:47:30 | [diff] [blame] | 71 | const char kRawData[] = "example raw data"; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 72 | |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 73 | const char kInstanceID[] = "iid_1"; |
| 74 | const char kScope[] = "GCM"; |
| 75 | const char kDeleteTokenResponse[] = "token=foo"; |
| 76 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 77 | // Helper for building arbitrary data messages. |
| 78 | MCSMessage BuildDownstreamMessage( |
| 79 | const std::string& project_id, |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 80 | const std::string& category, |
| 81 | const std::string& subtype, |
peter | a5aedc5 | 2015-07-22 10:47:30 | [diff] [blame] | 82 | const std::map<std::string, std::string>& data, |
| 83 | const std::string& raw_data) { |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 84 | mcs_proto::DataMessageStanza data_message; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 85 | data_message.set_from(project_id); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 86 | data_message.set_category(category); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 87 | for (std::map<std::string, std::string>::const_iterator iter = data.begin(); |
| 88 | iter != data.end(); |
| 89 | ++iter) { |
| 90 | mcs_proto::AppData* app_data = data_message.add_app_data(); |
| 91 | app_data->set_key(iter->first); |
| 92 | app_data->set_value(iter->second); |
| 93 | } |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 94 | if (!subtype.empty()) { |
| 95 | mcs_proto::AppData* app_data = data_message.add_app_data(); |
| 96 | app_data->set_key("subtype"); |
| 97 | app_data->set_value(subtype); |
| 98 | } |
peter | a5aedc5 | 2015-07-22 10:47:30 | [diff] [blame] | 99 | data_message.set_raw_data(raw_data); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 100 | return MCSMessage(kDataMessageStanzaTag, data_message); |
| 101 | } |
| 102 | |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 103 | GCMClient::AccountTokenInfo MakeAccountToken(const std::string& email, |
| 104 | const std::string& token) { |
| 105 | GCMClient::AccountTokenInfo account_token; |
| 106 | account_token.email = email; |
| 107 | account_token.access_token = token; |
| 108 | return account_token; |
| 109 | } |
| 110 | |
| 111 | std::map<std::string, std::string> MakeEmailToTokenMap( |
| 112 | const std::vector<GCMClient::AccountTokenInfo>& account_tokens) { |
| 113 | std::map<std::string, std::string> email_token_map; |
| 114 | for (std::vector<GCMClient::AccountTokenInfo>::const_iterator iter = |
| 115 | account_tokens.begin(); iter != account_tokens.end(); ++iter) { |
| 116 | email_token_map[iter->email] = iter->access_token; |
| 117 | } |
| 118 | return email_token_map; |
| 119 | } |
| 120 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 121 | class FakeMCSClient : public MCSClient { |
| 122 | public: |
| 123 | FakeMCSClient(base::Clock* clock, |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 124 | ConnectionFactory* connection_factory, |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 125 | GCMStore* gcm_store, |
| 126 | GCMStatsRecorder* recorder); |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 127 | ~FakeMCSClient() override; |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 128 | void Login(uint64_t android_id, uint64_t security_token) override; |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 129 | void SendMessage(const MCSMessage& message) override; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 130 | |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 131 | uint64_t last_android_id() const { return last_android_id_; } |
| 132 | uint64_t last_security_token() const { return last_security_token_; } |
| 133 | uint8_t last_message_tag() const { return last_message_tag_; } |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 134 | const mcs_proto::DataMessageStanza& last_data_message_stanza() const { |
| 135 | return last_data_message_stanza_; |
| 136 | } |
| 137 | |
| 138 | private: |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 139 | uint64_t last_android_id_; |
| 140 | uint64_t last_security_token_; |
| 141 | uint8_t last_message_tag_; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 142 | mcs_proto::DataMessageStanza last_data_message_stanza_; |
| 143 | }; |
| 144 | |
| 145 | FakeMCSClient::FakeMCSClient(base::Clock* clock, |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 146 | ConnectionFactory* connection_factory, |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 147 | GCMStore* gcm_store, |
| 148 | GCMStatsRecorder* recorder) |
chirantan | 192a921 | 2014-12-06 03:30:45 | [diff] [blame] | 149 | : MCSClient("", clock, connection_factory, gcm_store, recorder), |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 150 | last_android_id_(0u), |
| 151 | last_security_token_(0u), |
| 152 | last_message_tag_(kNumProtoTypes) { |
| 153 | } |
| 154 | |
| 155 | FakeMCSClient::~FakeMCSClient() { |
| 156 | } |
| 157 | |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 158 | void FakeMCSClient::Login(uint64_t android_id, uint64_t security_token) { |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 159 | last_android_id_ = android_id; |
| 160 | last_security_token_ = security_token; |
| 161 | } |
| 162 | |
| 163 | void FakeMCSClient::SendMessage(const MCSMessage& message) { |
| 164 | last_message_tag_ = message.tag(); |
| 165 | if (last_message_tag_ == kDataMessageStanzaTag) { |
| 166 | last_data_message_stanza_.CopyFrom( |
| 167 | reinterpret_cast<const mcs_proto::DataMessageStanza&>( |
| 168 | message.GetProtobuf())); |
| 169 | } |
| 170 | } |
| 171 | |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 172 | class AutoAdvancingTestClock : public base::Clock { |
| 173 | public: |
| 174 | explicit AutoAdvancingTestClock(base::TimeDelta auto_increment_time_delta); |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 175 | ~AutoAdvancingTestClock() override; |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 176 | |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 177 | base::Time Now() override; |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 178 | void Advance(TimeDelta delta); |
| 179 | int call_count() const { return call_count_; } |
| 180 | |
| 181 | private: |
| 182 | int call_count_; |
| 183 | base::TimeDelta auto_increment_time_delta_; |
| 184 | base::Time now_; |
| 185 | |
| 186 | DISALLOW_COPY_AND_ASSIGN(AutoAdvancingTestClock); |
| 187 | }; |
| 188 | |
| 189 | AutoAdvancingTestClock::AutoAdvancingTestClock( |
| 190 | base::TimeDelta auto_increment_time_delta) |
| 191 | : call_count_(0), auto_increment_time_delta_(auto_increment_time_delta) { |
| 192 | } |
| 193 | |
| 194 | AutoAdvancingTestClock::~AutoAdvancingTestClock() { |
| 195 | } |
| 196 | |
| 197 | base::Time AutoAdvancingTestClock::Now() { |
| 198 | call_count_++; |
| 199 | now_ += auto_increment_time_delta_; |
| 200 | return now_; |
| 201 | } |
| 202 | |
| 203 | void AutoAdvancingTestClock::Advance(base::TimeDelta delta) { |
| 204 | now_ += delta; |
| 205 | } |
| 206 | |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 207 | class FakeGCMInternalsBuilder : public GCMInternalsBuilder { |
| 208 | public: |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 209 | FakeGCMInternalsBuilder(base::TimeDelta clock_step); |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 210 | ~FakeGCMInternalsBuilder() override; |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 211 | |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 212 | std::unique_ptr<base::Clock> BuildClock() override; |
| 213 | std::unique_ptr<MCSClient> BuildMCSClient( |
| 214 | const std::string& version, |
| 215 | base::Clock* clock, |
| 216 | ConnectionFactory* connection_factory, |
| 217 | GCMStore* gcm_store, |
| 218 | GCMStatsRecorder* recorder) override; |
| 219 | std::unique_ptr<ConnectionFactory> BuildConnectionFactory( |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 220 | const std::vector<GURL>& endpoints, |
| 221 | const net::BackoffEntry::Policy& backoff_policy, |
mmenke | e65e7af | 2015-10-13 17:16:42 | [diff] [blame] | 222 | net::HttpNetworkSession* gcm_network_session, |
| 223 | net::HttpNetworkSession* http_network_session, |
mostynb | fe59f48 | 2014-10-06 15:04:46 | [diff] [blame] | 224 | GCMStatsRecorder* recorder) override; |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 225 | |
| 226 | private: |
| 227 | base::TimeDelta clock_step_; |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 228 | }; |
| 229 | |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 230 | FakeGCMInternalsBuilder::FakeGCMInternalsBuilder(base::TimeDelta clock_step) |
| 231 | : clock_step_(clock_step) { |
| 232 | } |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 233 | |
| 234 | FakeGCMInternalsBuilder::~FakeGCMInternalsBuilder() {} |
| 235 | |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 236 | std::unique_ptr<base::Clock> FakeGCMInternalsBuilder::BuildClock() { |
| 237 | return base::WrapUnique<base::Clock>(new AutoAdvancingTestClock(clock_step_)); |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 238 | } |
| 239 | |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 240 | std::unique_ptr<MCSClient> FakeGCMInternalsBuilder::BuildMCSClient( |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 241 | const std::string& version, |
| 242 | base::Clock* clock, |
| 243 | ConnectionFactory* connection_factory, |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 244 | GCMStore* gcm_store, |
| 245 | GCMStatsRecorder* recorder) { |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 246 | return base::WrapUnique<MCSClient>( |
| 247 | new FakeMCSClient(clock, connection_factory, gcm_store, recorder)); |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 248 | } |
| 249 | |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 250 | std::unique_ptr<ConnectionFactory> |
| 251 | FakeGCMInternalsBuilder::BuildConnectionFactory( |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 252 | const std::vector<GURL>& endpoints, |
| 253 | const net::BackoffEntry::Policy& backoff_policy, |
mmenke | e65e7af | 2015-10-13 17:16:42 | [diff] [blame] | 254 | net::HttpNetworkSession* gcm_network_session, |
| 255 | net::HttpNetworkSession* http_network_session, |
[email protected] | 9df5b93 | 2014-04-30 00:39:06 | [diff] [blame] | 256 | GCMStatsRecorder* recorder) { |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 257 | return base::WrapUnique<ConnectionFactory>(new FakeConnectionFactory()); |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 258 | } |
| 259 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 260 | } // namespace |
| 261 | |
| 262 | class GCMClientImplTest : public testing::Test, |
| 263 | public GCMClient::Delegate { |
| 264 | public: |
| 265 | GCMClientImplTest(); |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 266 | ~GCMClientImplTest() override; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 267 | |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 268 | void SetUp() override; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 269 | |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 270 | void SetUpUrlFetcherFactory(); |
| 271 | |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 272 | void BuildGCMClient(base::TimeDelta clock_step); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 273 | void InitializeGCMClient(); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 274 | void StartGCMClient(); |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 275 | void Register(const std::string& app_id, |
| 276 | const std::vector<std::string>& senders); |
| 277 | void Unregister(const std::string& app_id); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 278 | void ReceiveMessageFromMCS(const MCSMessage& message); |
[email protected] | 292af2b2 | 2014-08-06 19:42:45 | [diff] [blame] | 279 | void ReceiveOnMessageSentToMCS( |
| 280 | const std::string& app_id, |
| 281 | const std::string& message_id, |
| 282 | const MCSClient::MessageSendStatus status); |
zea | 76342abf | 2016-11-01 17:26:04 | [diff] [blame] | 283 | void FailCheckin(net::HttpStatusCode response_code); |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 284 | void CompleteCheckin(uint64_t android_id, |
| 285 | uint64_t security_token, |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 286 | const std::string& digest, |
| 287 | const std::map<std::string, std::string>& settings); |
zea | 76342abf | 2016-11-01 17:26:04 | [diff] [blame] | 288 | void CompleteCheckinImpl(uint64_t android_id, |
| 289 | uint64_t security_token, |
| 290 | const std::string& digest, |
| 291 | const std::map<std::string, std::string>& settings, |
| 292 | net::HttpStatusCode response_code); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 293 | void CompleteRegistration(const std::string& registration_id); |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 294 | void CompleteUnregistration(const std::string& app_id); |
fgorski | e45a34f | 2014-10-08 17:37:46 | [diff] [blame] | 295 | void VerifyPendingRequestFetcherDeleted(); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 296 | |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 297 | bool ExistsRegistration(const std::string& app_id) const; |
| 298 | void AddRegistration(const std::string& app_id, |
| 299 | const std::vector<std::string>& sender_ids, |
| 300 | const std::string& registration_id); |
| 301 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 302 | // GCMClient::Delegate overrides (for verification). |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 303 | void OnRegisterFinished(const linked_ptr<RegistrationInfo>& registration_info, |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 304 | const std::string& registration_id, |
| 305 | GCMClient::Result result) override; |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 306 | void OnUnregisterFinished( |
| 307 | const linked_ptr<RegistrationInfo>& registration_info, |
| 308 | GCMClient::Result result) override; |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 309 | void OnSendFinished(const std::string& app_id, |
| 310 | const std::string& message_id, |
| 311 | GCMClient::Result result) override {} |
| 312 | void OnMessageReceived(const std::string& registration_id, |
mvanouwerkerk | f8633deb | 2015-07-13 11:04:06 | [diff] [blame] | 313 | const IncomingMessage& message) override; |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 314 | void OnMessagesDeleted(const std::string& app_id) override; |
| 315 | void OnMessageSendError( |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 316 | const std::string& app_id, |
mostynb | fe59f48 | 2014-10-06 15:04:46 | [diff] [blame] | 317 | const gcm::GCMClient::SendErrorDetails& send_error_details) override; |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 318 | void OnSendAcknowledged(const std::string& app_id, |
| 319 | const std::string& message_id) override; |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 320 | void OnGCMReady(const std::vector<AccountMapping>& account_mappings, |
| 321 | const base::Time& last_token_fetch_time) override; |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 322 | void OnActivityRecorded() override {} |
| 323 | void OnConnected(const net::IPEndPoint& ip_endpoint) override {} |
| 324 | void OnDisconnected() override {} |
johnme | 93ec793 | 2016-11-17 14:26:58 | [diff] [blame] | 325 | void OnStoreReset() override {} |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 326 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 327 | GCMClientImpl* gcm_client() const { return gcm_client_.get(); } |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 328 | GCMClientImpl::State gcm_client_state() const { |
| 329 | return gcm_client_->state_; |
| 330 | } |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 331 | FakeMCSClient* mcs_client() const { |
| 332 | return reinterpret_cast<FakeMCSClient*>(gcm_client_->mcs_client_.get()); |
| 333 | } |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 334 | ConnectionFactory* connection_factory() const { |
| 335 | return gcm_client_->connection_factory_.get(); |
| 336 | } |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 337 | |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 338 | const GCMClientImpl::CheckinInfo& device_checkin_info() const { |
| 339 | return gcm_client_->device_checkin_info_; |
| 340 | } |
| 341 | |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 342 | void reset_last_event() { |
| 343 | last_event_ = NONE; |
| 344 | last_app_id_.clear(); |
| 345 | last_registration_id_.clear(); |
| 346 | last_message_id_.clear(); |
| 347 | last_result_ = GCMClient::UNKNOWN_ERROR; |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 348 | last_account_mappings_.clear(); |
| 349 | last_token_fetch_time_ = base::Time(); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 350 | } |
| 351 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 352 | LastEvent last_event() const { return last_event_; } |
| 353 | const std::string& last_app_id() const { return last_app_id_; } |
| 354 | const std::string& last_registration_id() const { |
| 355 | return last_registration_id_; |
| 356 | } |
| 357 | const std::string& last_message_id() const { return last_message_id_; } |
| 358 | GCMClient::Result last_result() const { return last_result_; } |
mvanouwerkerk | f8633deb | 2015-07-13 11:04:06 | [diff] [blame] | 359 | const IncomingMessage& last_message() const { return last_message_; } |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 360 | const GCMClient::SendErrorDetails& last_error_details() const { |
| 361 | return last_error_details_; |
| 362 | } |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 363 | const base::Time& last_token_fetch_time() const { |
| 364 | return last_token_fetch_time_; |
| 365 | } |
| 366 | const std::vector<AccountMapping>& last_account_mappings() { |
| 367 | return last_account_mappings_; |
| 368 | } |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 369 | |
[email protected] | 06e4527 | 2014-05-06 03:41:34 | [diff] [blame] | 370 | const GServicesSettings& gservices_settings() const { |
| 371 | return gcm_client_->gservices_settings_; |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 372 | } |
| 373 | |
jianli | 00b4600f | 2015-02-10 23:32:49 | [diff] [blame] | 374 | const base::FilePath& temp_directory_path() const { |
vabr | 0c237ae | 2016-09-14 09:24:28 | [diff] [blame] | 375 | return temp_directory_.GetPath(); |
jianli | 00b4600f | 2015-02-10 23:32:49 | [diff] [blame] | 376 | } |
| 377 | |
jianli | 9f9a7367 | 2015-06-16 19:39:34 | [diff] [blame] | 378 | base::FilePath gcm_store_path() const { |
| 379 | // Pass an non-existent directory as store path to match the exact |
| 380 | // behavior in the production code. Currently GCMStoreImpl checks if |
| 381 | // the directory exist or not to determine the store existence. |
vabr | 0c237ae | 2016-09-14 09:24:28 | [diff] [blame] | 382 | return temp_directory_.GetPath().Append(FILE_PATH_LITERAL("GCM Store")); |
jianli | 9f9a7367 | 2015-06-16 19:39:34 | [diff] [blame] | 383 | } |
| 384 | |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 385 | int64_t CurrentTime(); |
[email protected] | d3ba08d9 | 2014-04-04 23:03:55 | [diff] [blame] | 386 | |
| 387 | // Tooling. |
[email protected] | d3ba08d9 | 2014-04-04 23:03:55 | [diff] [blame] | 388 | void PumpLoopUntilIdle(); |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 389 | bool CreateUniqueTempDir(); |
| 390 | AutoAdvancingTestClock* clock() const { |
| 391 | return reinterpret_cast<AutoAdvancingTestClock*>(gcm_client_->clock_.get()); |
[email protected] | a0144ceb | 2014-04-04 23:49:34 | [diff] [blame] | 392 | } |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 393 | net::TestURLFetcherFactory* url_fetcher_factory() { |
| 394 | return &url_fetcher_factory_; |
| 395 | } |
jianli | 78b5604 | 2015-06-17 01:21:22 | [diff] [blame] | 396 | base::TestMockTimeTaskRunner* task_runner() { |
| 397 | return task_runner_.get(); |
| 398 | } |
[email protected] | d3ba08d9 | 2014-04-04 23:03:55 | [diff] [blame] | 399 | |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 400 | private: |
wkorman | c1e71b6 | 2015-11-09 22:56:04 | [diff] [blame] | 401 | // Must be declared first so that it is destroyed last. Injected to |
| 402 | // GCM client. |
| 403 | base::ScopedTempDir temp_directory_; |
| 404 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 405 | // Variables used for verification. |
| 406 | LastEvent last_event_; |
| 407 | std::string last_app_id_; |
| 408 | std::string last_registration_id_; |
| 409 | std::string last_message_id_; |
| 410 | GCMClient::Result last_result_; |
mvanouwerkerk | f8633deb | 2015-07-13 11:04:06 | [diff] [blame] | 411 | IncomingMessage last_message_; |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 412 | GCMClient::SendErrorDetails last_error_details_; |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 413 | base::Time last_token_fetch_time_; |
| 414 | std::vector<AccountMapping> last_account_mappings_; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 415 | |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 416 | std::unique_ptr<GCMClientImpl> gcm_client_; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 417 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 418 | net::TestURLFetcherFactory url_fetcher_factory_; |
| 419 | |
jianli | 78b5604 | 2015-06-17 01:21:22 | [diff] [blame] | 420 | scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; |
| 421 | base::ThreadTaskRunnerHandle task_runner_handle_; |
| 422 | |
wkorman | c1e71b6 | 2015-11-09 22:56:04 | [diff] [blame] | 423 | // Injected to GCM client. |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 424 | scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; |
| 425 | }; |
| 426 | |
| 427 | GCMClientImplTest::GCMClientImplTest() |
| 428 | : last_event_(NONE), |
| 429 | last_result_(GCMClient::UNKNOWN_ERROR), |
jianli | 78b5604 | 2015-06-17 01:21:22 | [diff] [blame] | 430 | task_runner_(new base::TestMockTimeTaskRunner), |
| 431 | task_runner_handle_(task_runner_), |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 432 | url_request_context_getter_( |
jianli | 78b5604 | 2015-06-17 01:21:22 | [diff] [blame] | 433 | new net::TestURLRequestContextGetter(task_runner_)) { |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | GCMClientImplTest::~GCMClientImplTest() {} |
| 437 | |
| 438 | void GCMClientImplTest::SetUp() { |
[email protected] | fdeaf973 | 2014-04-11 21:02:15 | [diff] [blame] | 439 | testing::Test::SetUp(); |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 440 | ASSERT_TRUE(CreateUniqueTempDir()); |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 441 | BuildGCMClient(base::TimeDelta()); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 442 | InitializeGCMClient(); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 443 | StartGCMClient(); |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 444 | SetUpUrlFetcherFactory(); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 445 | ASSERT_NO_FATAL_FAILURE( |
| 446 | CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, std::string(), |
| 447 | std::map<std::string, std::string>())); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 448 | } |
| 449 | |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 450 | void GCMClientImplTest::SetUpUrlFetcherFactory() { |
| 451 | url_fetcher_factory_.set_remove_fetcher_on_delete(true); |
| 452 | } |
| 453 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 454 | void GCMClientImplTest::PumpLoopUntilIdle() { |
jianli | 78b5604 | 2015-06-17 01:21:22 | [diff] [blame] | 455 | task_runner_->RunUntilIdle(); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 456 | } |
| 457 | |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 458 | bool GCMClientImplTest::CreateUniqueTempDir() { |
| 459 | return temp_directory_.CreateUniqueTempDir(); |
| 460 | } |
| 461 | |
| 462 | void GCMClientImplTest::BuildGCMClient(base::TimeDelta clock_step) { |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 463 | gcm_client_.reset(new GCMClientImpl(base::WrapUnique<GCMInternalsBuilder>( |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 464 | new FakeGCMInternalsBuilder(clock_step)))); |
| 465 | } |
| 466 | |
zea | 76342abf | 2016-11-01 17:26:04 | [diff] [blame] | 467 | void GCMClientImplTest::FailCheckin(net::HttpStatusCode response_code) { |
| 468 | std::map<std::string, std::string> settings; |
| 469 | CompleteCheckinImpl(0, 0, GServicesSettings::CalculateDigest(settings), |
| 470 | settings, response_code); |
| 471 | } |
| 472 | |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 473 | void GCMClientImplTest::CompleteCheckin( |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 474 | uint64_t android_id, |
| 475 | uint64_t security_token, |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 476 | const std::string& digest, |
| 477 | const std::map<std::string, std::string>& settings) { |
zea | 76342abf | 2016-11-01 17:26:04 | [diff] [blame] | 478 | CompleteCheckinImpl(android_id, security_token, digest, settings, |
| 479 | net::HTTP_OK); |
| 480 | } |
| 481 | |
| 482 | void GCMClientImplTest::CompleteCheckinImpl( |
| 483 | uint64_t android_id, |
| 484 | uint64_t security_token, |
| 485 | const std::string& digest, |
| 486 | const std::map<std::string, std::string>& settings, |
| 487 | net::HttpStatusCode response_code) { |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 488 | checkin_proto::AndroidCheckinResponse response; |
| 489 | response.set_stats_ok(true); |
| 490 | response.set_android_id(android_id); |
| 491 | response.set_security_token(security_token); |
| 492 | |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 493 | // For testing G-services settings. |
| 494 | if (!digest.empty()) { |
| 495 | response.set_digest(digest); |
| 496 | for (std::map<std::string, std::string>::const_iterator it = |
| 497 | settings.begin(); |
| 498 | it != settings.end(); |
| 499 | ++it) { |
| 500 | checkin_proto::GservicesSetting* setting = response.add_setting(); |
| 501 | setting->set_name(it->first); |
| 502 | setting->set_value(it->second); |
| 503 | } |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 504 | response.set_settings_diff(false); |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 505 | } |
| 506 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 507 | std::string response_string; |
| 508 | response.SerializeToString(&response_string); |
| 509 | |
| 510 | net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 511 | ASSERT_TRUE(fetcher); |
zea | 76342abf | 2016-11-01 17:26:04 | [diff] [blame] | 512 | fetcher->set_response_code(response_code); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 513 | fetcher->SetResponseString(response_string); |
| 514 | fetcher->delegate()->OnURLFetchComplete(fetcher); |
jianli | d7e80f2 | 2015-06-18 22:21:31 | [diff] [blame] | 515 | // Give a chance for GCMStoreImpl::Backend to finish persisting data. |
| 516 | PumpLoopUntilIdle(); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | void GCMClientImplTest::CompleteRegistration( |
| 520 | const std::string& registration_id) { |
| 521 | std::string response(kRegistrationResponsePrefix); |
| 522 | response.append(registration_id); |
| 523 | net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 524 | ASSERT_TRUE(fetcher); |
| 525 | fetcher->set_response_code(net::HTTP_OK); |
| 526 | fetcher->SetResponseString(response); |
| 527 | fetcher->delegate()->OnURLFetchComplete(fetcher); |
jianli | d7e80f2 | 2015-06-18 22:21:31 | [diff] [blame] | 528 | // Give a chance for GCMStoreImpl::Backend to finish persisting data. |
| 529 | PumpLoopUntilIdle(); |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | void GCMClientImplTest::CompleteUnregistration( |
| 533 | const std::string& app_id) { |
| 534 | std::string response(kUnregistrationResponsePrefix); |
| 535 | response.append(app_id); |
| 536 | net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 537 | ASSERT_TRUE(fetcher); |
| 538 | fetcher->set_response_code(net::HTTP_OK); |
| 539 | fetcher->SetResponseString(response); |
| 540 | fetcher->delegate()->OnURLFetchComplete(fetcher); |
jianli | d7e80f2 | 2015-06-18 22:21:31 | [diff] [blame] | 541 | // Give a chance for GCMStoreImpl::Backend to finish persisting data. |
| 542 | PumpLoopUntilIdle(); |
fgorski | e45a34f | 2014-10-08 17:37:46 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | void GCMClientImplTest::VerifyPendingRequestFetcherDeleted() { |
| 546 | net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 547 | EXPECT_FALSE(fetcher); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | bool GCMClientImplTest::ExistsRegistration(const std::string& app_id) const { |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 551 | return ExistsGCMRegistrationInMap(gcm_client_->registrations_, app_id); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | void GCMClientImplTest::AddRegistration( |
| 555 | const std::string& app_id, |
| 556 | const std::vector<std::string>& sender_ids, |
| 557 | const std::string& registration_id) { |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 558 | linked_ptr<GCMRegistrationInfo> registration(new GCMRegistrationInfo); |
| 559 | registration->app_id = app_id; |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 560 | registration->sender_ids = sender_ids; |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 561 | gcm_client_->registrations_[registration] = registration_id; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | void GCMClientImplTest::InitializeGCMClient() { |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 565 | clock()->Advance(base::TimeDelta::FromMilliseconds(1)); |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 566 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 567 | // Actual initialization. |
[email protected] | 8ad8051 | 2014-05-23 09:40:47 | [diff] [blame] | 568 | GCMClient::ChromeBuildInfo chrome_build_info; |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 569 | chrome_build_info.version = kChromeVersion; |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 570 | chrome_build_info.product_category_for_subtypes = kProductCategoryForSubtypes; |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 571 | gcm_client_->Initialize(chrome_build_info, gcm_store_path(), task_runner_, |
| 572 | url_request_context_getter_, |
| 573 | base::WrapUnique<Encryptor>(new FakeEncryptor), this); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 574 | } |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 575 | |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 576 | void GCMClientImplTest::StartGCMClient() { |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 577 | // Start loading and check-in. |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 578 | gcm_client_->Start(GCMClient::IMMEDIATE_START); |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 579 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 580 | PumpLoopUntilIdle(); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 581 | } |
| 582 | |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 583 | void GCMClientImplTest::Register(const std::string& app_id, |
| 584 | const std::vector<std::string>& senders) { |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 585 | std::unique_ptr<GCMRegistrationInfo> gcm_info(new GCMRegistrationInfo); |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 586 | gcm_info->app_id = app_id; |
| 587 | gcm_info->sender_ids = senders; |
| 588 | gcm_client()->Register(make_linked_ptr<RegistrationInfo>(gcm_info.release())); |
| 589 | } |
| 590 | |
| 591 | void GCMClientImplTest::Unregister(const std::string& app_id) { |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 592 | std::unique_ptr<GCMRegistrationInfo> gcm_info(new GCMRegistrationInfo); |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 593 | gcm_info->app_id = app_id; |
| 594 | gcm_client()->Unregister( |
| 595 | make_linked_ptr<RegistrationInfo>(gcm_info.release())); |
| 596 | } |
| 597 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 598 | void GCMClientImplTest::ReceiveMessageFromMCS(const MCSMessage& message) { |
[email protected] | dd47c4ce | 2014-08-05 23:38:50 | [diff] [blame] | 599 | gcm_client_->recorder_.RecordConnectionInitiated(std::string()); |
| 600 | gcm_client_->recorder_.RecordConnectionSuccess(); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 601 | gcm_client_->OnMessageReceivedFromMCS(message); |
| 602 | } |
| 603 | |
[email protected] | 292af2b2 | 2014-08-06 19:42:45 | [diff] [blame] | 604 | void GCMClientImplTest::ReceiveOnMessageSentToMCS( |
| 605 | const std::string& app_id, |
| 606 | const std::string& message_id, |
| 607 | const MCSClient::MessageSendStatus status) { |
| 608 | gcm_client_->OnMessageSentToMCS(0LL, app_id, message_id, status); |
| 609 | } |
| 610 | |
fgorski | d578c18b | 2014-09-24 23:40:17 | [diff] [blame] | 611 | void GCMClientImplTest::OnGCMReady( |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 612 | const std::vector<AccountMapping>& account_mappings, |
| 613 | const base::Time& last_token_fetch_time) { |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 614 | last_event_ = LOADING_COMPLETED; |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 615 | last_account_mappings_ = account_mappings; |
| 616 | last_token_fetch_time_ = last_token_fetch_time; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 617 | } |
| 618 | |
mvanouwerkerk | f8633deb | 2015-07-13 11:04:06 | [diff] [blame] | 619 | void GCMClientImplTest::OnMessageReceived(const std::string& registration_id, |
| 620 | const IncomingMessage& message) { |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 621 | last_event_ = MESSAGE_RECEIVED; |
| 622 | last_app_id_ = registration_id; |
| 623 | last_message_ = message; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 624 | } |
| 625 | |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 626 | void GCMClientImplTest::OnRegisterFinished( |
| 627 | const linked_ptr<RegistrationInfo>& registration_info, |
| 628 | const std::string& registration_id, |
| 629 | GCMClient::Result result) { |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 630 | last_event_ = REGISTRATION_COMPLETED; |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 631 | last_app_id_ = registration_info->app_id; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 632 | last_registration_id_ = registration_id; |
| 633 | last_result_ = result; |
| 634 | } |
| 635 | |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 636 | void GCMClientImplTest::OnUnregisterFinished( |
| 637 | const linked_ptr<RegistrationInfo>& registration_info, |
| 638 | GCMClient::Result result) { |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 639 | last_event_ = UNREGISTRATION_COMPLETED; |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 640 | last_app_id_ = registration_info->app_id; |
[email protected] | 0e88e1d1 | 2014-03-19 06:53:08 | [diff] [blame] | 641 | last_result_ = result; |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 642 | } |
| 643 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 644 | void GCMClientImplTest::OnMessagesDeleted(const std::string& app_id) { |
| 645 | last_event_ = MESSAGES_DELETED; |
| 646 | last_app_id_ = app_id; |
| 647 | } |
| 648 | |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 649 | void GCMClientImplTest::OnMessageSendError( |
| 650 | const std::string& app_id, |
| 651 | const gcm::GCMClient::SendErrorDetails& send_error_details) { |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 652 | last_event_ = MESSAGE_SEND_ERROR; |
| 653 | last_app_id_ = app_id; |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 654 | last_error_details_ = send_error_details; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 655 | } |
| 656 | |
[email protected] | 292af2b2 | 2014-08-06 19:42:45 | [diff] [blame] | 657 | void GCMClientImplTest::OnSendAcknowledged(const std::string& app_id, |
| 658 | const std::string& message_id) { |
| 659 | last_event_ = MESSAGE_SEND_ACK; |
| 660 | last_app_id_ = app_id; |
| 661 | last_message_id_ = message_id; |
| 662 | } |
| 663 | |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 664 | int64_t GCMClientImplTest::CurrentTime() { |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 665 | return clock()->Now().ToInternalValue() / base::Time::kMicrosecondsPerSecond; |
| 666 | } |
| 667 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 668 | TEST_F(GCMClientImplTest, LoadingCompleted) { |
| 669 | EXPECT_EQ(LOADING_COMPLETED, last_event()); |
| 670 | EXPECT_EQ(kDeviceAndroidId, mcs_client()->last_android_id()); |
| 671 | EXPECT_EQ(kDeviceSecurityToken, mcs_client()->last_security_token()); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 672 | |
| 673 | // Checking freshly loaded CheckinInfo. |
| 674 | EXPECT_EQ(kDeviceAndroidId, device_checkin_info().android_id); |
| 675 | EXPECT_EQ(kDeviceSecurityToken, device_checkin_info().secret); |
| 676 | EXPECT_TRUE(device_checkin_info().last_checkin_accounts.empty()); |
| 677 | EXPECT_TRUE(device_checkin_info().accounts_set); |
| 678 | EXPECT_TRUE(device_checkin_info().account_tokens.empty()); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 679 | } |
| 680 | |
jianli | 00b4600f | 2015-02-10 23:32:49 | [diff] [blame] | 681 | TEST_F(GCMClientImplTest, LoadingBusted) { |
| 682 | // Close the GCM store. |
| 683 | gcm_client()->Stop(); |
| 684 | PumpLoopUntilIdle(); |
| 685 | |
| 686 | // Mess up the store. |
Chris Mumford | a884aa1f | 2017-10-24 22:46:32 | [diff] [blame^] | 687 | EXPECT_TRUE(leveldb_chrome::CorruptClosedDBForTesting(gcm_store_path())); |
jianli | 00b4600f | 2015-02-10 23:32:49 | [diff] [blame] | 688 | |
| 689 | // Restart GCM client. The store should be reset and the loading should |
| 690 | // complete successfully. |
| 691 | reset_last_event(); |
| 692 | BuildGCMClient(base::TimeDelta()); |
| 693 | InitializeGCMClient(); |
| 694 | StartGCMClient(); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 695 | ASSERT_NO_FATAL_FAILURE( |
| 696 | CompleteCheckin(kDeviceAndroidId2, kDeviceSecurityToken2, std::string(), |
| 697 | std::map<std::string, std::string>())); |
jianli | 00b4600f | 2015-02-10 23:32:49 | [diff] [blame] | 698 | |
| 699 | EXPECT_EQ(LOADING_COMPLETED, last_event()); |
| 700 | EXPECT_EQ(kDeviceAndroidId2, mcs_client()->last_android_id()); |
| 701 | EXPECT_EQ(kDeviceSecurityToken2, mcs_client()->last_security_token()); |
| 702 | } |
| 703 | |
johnme | 6d6c780 | 2016-09-29 22:27:16 | [diff] [blame] | 704 | TEST_F(GCMClientImplTest, LoadingWithEmptyDirectory) { |
| 705 | // Close the GCM store. |
| 706 | gcm_client()->Stop(); |
| 707 | PumpLoopUntilIdle(); |
| 708 | |
| 709 | // Make the store directory empty, to simulate a previous destroy store |
| 710 | // operation failing to delete the store directory. |
| 711 | ASSERT_TRUE(base::DeleteFile(gcm_store_path(), true /* recursive */)); |
| 712 | ASSERT_TRUE(base::CreateDirectory(gcm_store_path())); |
| 713 | |
| 714 | base::HistogramTester histogram_tester; |
| 715 | |
| 716 | // Restart GCM client. The store should be considered to not exist. |
| 717 | BuildGCMClient(base::TimeDelta()); |
| 718 | InitializeGCMClient(); |
| 719 | gcm_client()->Start(GCMClient::DELAYED_START); |
| 720 | PumpLoopUntilIdle(); |
| 721 | histogram_tester.ExpectUniqueSample("GCM.LoadStatus", |
| 722 | 13 /* STORE_DOES_NOT_EXIST */, 1); |
| 723 | // Since the store does not exist, the database should not have been opened. |
| 724 | histogram_tester.ExpectTotalCount("GCM.Database.Open", 0); |
| 725 | // Without a store, DELAYED_START loading should only reach INITIALIZED state. |
| 726 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 727 | |
| 728 | // The store directory should still exist (and be empty). If not, then the |
| 729 | // DELAYED_START load has probably reset the store, rather than leaving that |
| 730 | // to the next IMMEDIATE_START load as expected. |
| 731 | ASSERT_TRUE(base::DirectoryExists(gcm_store_path())); |
| 732 | ASSERT_FALSE( |
| 733 | base::PathExists(gcm_store_path().Append(FILE_PATH_LITERAL("CURRENT")))); |
| 734 | |
| 735 | // IMMEDIATE_START loading should successfully create a new store despite the |
| 736 | // empty directory. |
| 737 | reset_last_event(); |
| 738 | StartGCMClient(); |
| 739 | ASSERT_NO_FATAL_FAILURE( |
| 740 | CompleteCheckin(kDeviceAndroidId2, kDeviceSecurityToken2, std::string(), |
| 741 | std::map<std::string, std::string>())); |
| 742 | EXPECT_EQ(LOADING_COMPLETED, last_event()); |
| 743 | EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); |
| 744 | EXPECT_EQ(kDeviceAndroidId2, mcs_client()->last_android_id()); |
| 745 | EXPECT_EQ(kDeviceSecurityToken2, mcs_client()->last_security_token()); |
| 746 | } |
| 747 | |
jianli | 78b5604 | 2015-06-17 01:21:22 | [diff] [blame] | 748 | TEST_F(GCMClientImplTest, DestroyStoreWhenNotNeeded) { |
| 749 | // Close the GCM store. |
| 750 | gcm_client()->Stop(); |
| 751 | PumpLoopUntilIdle(); |
| 752 | |
| 753 | // Restart GCM client. The store is loaded successfully. |
| 754 | reset_last_event(); |
| 755 | BuildGCMClient(base::TimeDelta()); |
| 756 | InitializeGCMClient(); |
| 757 | gcm_client()->Start(GCMClient::DELAYED_START); |
| 758 | PumpLoopUntilIdle(); |
| 759 | |
| 760 | EXPECT_EQ(GCMClientImpl::LOADED, gcm_client_state()); |
| 761 | EXPECT_TRUE(device_checkin_info().android_id); |
| 762 | EXPECT_TRUE(device_checkin_info().secret); |
| 763 | |
| 764 | // Fast forward the clock to trigger the store destroying logic. |
| 765 | task_runner()->FastForwardBy(base::TimeDelta::FromMilliseconds(300000)); |
| 766 | PumpLoopUntilIdle(); |
| 767 | |
| 768 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 769 | EXPECT_FALSE(device_checkin_info().android_id); |
| 770 | EXPECT_FALSE(device_checkin_info().secret); |
| 771 | } |
| 772 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 773 | TEST_F(GCMClientImplTest, RegisterApp) { |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 774 | EXPECT_FALSE(ExistsRegistration(kExtensionAppId)); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 775 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 776 | std::vector<std::string> senders; |
| 777 | senders.push_back("sender"); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 778 | Register(kExtensionAppId, senders); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 779 | ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id")); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 780 | |
| 781 | EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 782 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 783 | EXPECT_EQ("reg_id", last_registration_id()); |
| 784 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 785 | EXPECT_TRUE(ExistsRegistration(kExtensionAppId)); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 786 | } |
| 787 | |
[email protected] | 3904d2b | 2014-03-22 01:34:31 | [diff] [blame] | 788 | TEST_F(GCMClientImplTest, DISABLED_RegisterAppFromCache) { |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 789 | EXPECT_FALSE(ExistsRegistration(kExtensionAppId)); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 790 | |
| 791 | std::vector<std::string> senders; |
| 792 | senders.push_back("sender"); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 793 | Register(kExtensionAppId, senders); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 794 | ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id")); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 795 | EXPECT_TRUE(ExistsRegistration(kExtensionAppId)); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 796 | |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 797 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 798 | EXPECT_EQ("reg_id", last_registration_id()); |
| 799 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 800 | EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
| 801 | |
| 802 | // Recreate GCMClient in order to load from the persistent store. |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 803 | BuildGCMClient(base::TimeDelta()); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 804 | InitializeGCMClient(); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 805 | StartGCMClient(); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 806 | |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 807 | EXPECT_TRUE(ExistsRegistration(kExtensionAppId)); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 808 | } |
| 809 | |
jianli | 055634b | 2015-06-29 21:32:12 | [diff] [blame] | 810 | TEST_F(GCMClientImplTest, RegisterPreviousSenderAgain) { |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 811 | EXPECT_FALSE(ExistsRegistration(kExtensionAppId)); |
jianli | 055634b | 2015-06-29 21:32:12 | [diff] [blame] | 812 | |
| 813 | // Register a sender. |
| 814 | std::vector<std::string> senders; |
| 815 | senders.push_back("sender"); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 816 | Register(kExtensionAppId, senders); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 817 | ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id")); |
jianli | 055634b | 2015-06-29 21:32:12 | [diff] [blame] | 818 | |
| 819 | EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 820 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
jianli | 055634b | 2015-06-29 21:32:12 | [diff] [blame] | 821 | EXPECT_EQ("reg_id", last_registration_id()); |
| 822 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 823 | EXPECT_TRUE(ExistsRegistration(kExtensionAppId)); |
jianli | 055634b | 2015-06-29 21:32:12 | [diff] [blame] | 824 | |
| 825 | reset_last_event(); |
| 826 | |
| 827 | // Register a different sender. Different registration ID from previous one |
| 828 | // should be returned. |
| 829 | std::vector<std::string> senders2; |
| 830 | senders2.push_back("sender2"); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 831 | Register(kExtensionAppId, senders2); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 832 | ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id2")); |
jianli | 055634b | 2015-06-29 21:32:12 | [diff] [blame] | 833 | |
| 834 | EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 835 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
jianli | 055634b | 2015-06-29 21:32:12 | [diff] [blame] | 836 | EXPECT_EQ("reg_id2", last_registration_id()); |
| 837 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 838 | EXPECT_TRUE(ExistsRegistration(kExtensionAppId)); |
jianli | 055634b | 2015-06-29 21:32:12 | [diff] [blame] | 839 | |
| 840 | reset_last_event(); |
| 841 | |
| 842 | // Register the 1st sender again. Different registration ID from previous one |
| 843 | // should be returned. |
| 844 | std::vector<std::string> senders3; |
| 845 | senders3.push_back("sender"); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 846 | Register(kExtensionAppId, senders3); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 847 | ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id")); |
jianli | 055634b | 2015-06-29 21:32:12 | [diff] [blame] | 848 | |
| 849 | EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 850 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
jianli | 055634b | 2015-06-29 21:32:12 | [diff] [blame] | 851 | EXPECT_EQ("reg_id", last_registration_id()); |
| 852 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 853 | EXPECT_TRUE(ExistsRegistration(kExtensionAppId)); |
jianli | 055634b | 2015-06-29 21:32:12 | [diff] [blame] | 854 | } |
| 855 | |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 856 | TEST_F(GCMClientImplTest, UnregisterApp) { |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 857 | EXPECT_FALSE(ExistsRegistration(kExtensionAppId)); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 858 | |
| 859 | std::vector<std::string> senders; |
| 860 | senders.push_back("sender"); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 861 | Register(kExtensionAppId, senders); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 862 | ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id")); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 863 | EXPECT_TRUE(ExistsRegistration(kExtensionAppId)); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 864 | |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 865 | Unregister(kExtensionAppId); |
| 866 | ASSERT_NO_FATAL_FAILURE(CompleteUnregistration(kExtensionAppId)); |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 867 | |
| 868 | EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 869 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 870 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 871 | EXPECT_FALSE(ExistsRegistration(kExtensionAppId)); |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 872 | } |
| 873 | |
fgorski | e45a34f | 2014-10-08 17:37:46 | [diff] [blame] | 874 | // Tests that stopping the GCMClient also deletes pending registration requests. |
| 875 | // This is tested by checking that url fetcher contained in the request was |
| 876 | // deleted. |
| 877 | TEST_F(GCMClientImplTest, DeletePendingRequestsWhenStopping) { |
| 878 | std::vector<std::string> senders; |
| 879 | senders.push_back("sender"); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 880 | Register(kExtensionAppId, senders); |
fgorski | e45a34f | 2014-10-08 17:37:46 | [diff] [blame] | 881 | |
| 882 | gcm_client()->Stop(); |
jianli | d7e80f2 | 2015-06-18 22:21:31 | [diff] [blame] | 883 | PumpLoopUntilIdle(); |
fgorski | e45a34f | 2014-10-08 17:37:46 | [diff] [blame] | 884 | VerifyPendingRequestFetcherDeleted(); |
| 885 | } |
| 886 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 887 | TEST_F(GCMClientImplTest, DispatchDownstreamMessage) { |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 888 | // Register to receive messages from kSender and kSender2 only. |
| 889 | std::vector<std::string> senders; |
| 890 | senders.push_back(kSender); |
| 891 | senders.push_back(kSender2); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 892 | AddRegistration(kExtensionAppId, senders, "reg_id"); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 893 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 894 | std::map<std::string, std::string> expected_data; |
| 895 | expected_data["message_type"] = "gcm"; |
| 896 | expected_data["key"] = "value"; |
| 897 | expected_data["key2"] = "value2"; |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 898 | |
| 899 | // Message for kSender will be received. |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 900 | MCSMessage message(BuildDownstreamMessage( |
| 901 | kSender, kExtensionAppId, std::string() /* subtype */, expected_data, |
| 902 | std::string() /* raw_data */)); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 903 | EXPECT_TRUE(message.IsValid()); |
| 904 | ReceiveMessageFromMCS(message); |
| 905 | |
| 906 | expected_data.erase(expected_data.find("message_type")); |
| 907 | EXPECT_EQ(MESSAGE_RECEIVED, last_event()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 908 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 909 | EXPECT_EQ(expected_data.size(), last_message().data.size()); |
| 910 | EXPECT_EQ(expected_data, last_message().data); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 911 | EXPECT_EQ(kSender, last_message().sender_id); |
| 912 | |
| 913 | reset_last_event(); |
| 914 | |
| 915 | // Message for kSender2 will be received. |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 916 | MCSMessage message2(BuildDownstreamMessage( |
| 917 | kSender2, kExtensionAppId, std::string() /* subtype */, expected_data, |
| 918 | std::string() /* raw_data */)); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 919 | EXPECT_TRUE(message2.IsValid()); |
| 920 | ReceiveMessageFromMCS(message2); |
| 921 | |
| 922 | EXPECT_EQ(MESSAGE_RECEIVED, last_event()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 923 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 924 | EXPECT_EQ(expected_data.size(), last_message().data.size()); |
| 925 | EXPECT_EQ(expected_data, last_message().data); |
| 926 | EXPECT_EQ(kSender2, last_message().sender_id); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 927 | } |
| 928 | |
peter | a5aedc5 | 2015-07-22 10:47:30 | [diff] [blame] | 929 | TEST_F(GCMClientImplTest, DispatchDownstreamMessageRawData) { |
| 930 | std::vector<std::string> senders(1, kSender); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 931 | AddRegistration(kExtensionAppId, senders, "reg_id"); |
peter | a5aedc5 | 2015-07-22 10:47:30 | [diff] [blame] | 932 | |
| 933 | std::map<std::string, std::string> expected_data; |
| 934 | |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 935 | MCSMessage message(BuildDownstreamMessage(kSender, kExtensionAppId, |
| 936 | std::string() /* subtype */, |
| 937 | expected_data, kRawData)); |
peter | a5aedc5 | 2015-07-22 10:47:30 | [diff] [blame] | 938 | EXPECT_TRUE(message.IsValid()); |
| 939 | ReceiveMessageFromMCS(message); |
| 940 | |
| 941 | EXPECT_EQ(MESSAGE_RECEIVED, last_event()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 942 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
peter | a5aedc5 | 2015-07-22 10:47:30 | [diff] [blame] | 943 | EXPECT_EQ(expected_data.size(), last_message().data.size()); |
| 944 | EXPECT_EQ(kSender, last_message().sender_id); |
| 945 | EXPECT_EQ(kRawData, last_message().raw_data); |
| 946 | } |
| 947 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 948 | TEST_F(GCMClientImplTest, DispatchDownstreamMessageSendError) { |
| 949 | std::map<std::string, std::string> expected_data; |
| 950 | expected_data["message_type"] = "send_error"; |
| 951 | expected_data["google.message_id"] = "007"; |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 952 | expected_data["error_details"] = "some details"; |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 953 | MCSMessage message(BuildDownstreamMessage( |
| 954 | kSender, kExtensionAppId, std::string() /* subtype */, expected_data, |
| 955 | std::string() /* raw_data */)); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 956 | EXPECT_TRUE(message.IsValid()); |
| 957 | ReceiveMessageFromMCS(message); |
| 958 | |
| 959 | EXPECT_EQ(MESSAGE_SEND_ERROR, last_event()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 960 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 961 | EXPECT_EQ("007", last_error_details().message_id); |
| 962 | EXPECT_EQ(1UL, last_error_details().additional_data.size()); |
mvanouwerkerk | f8633deb | 2015-07-13 11:04:06 | [diff] [blame] | 963 | MessageData::const_iterator iter = |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 964 | last_error_details().additional_data.find("error_details"); |
| 965 | EXPECT_TRUE(iter != last_error_details().additional_data.end()); |
| 966 | EXPECT_EQ("some details", iter->second); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | TEST_F(GCMClientImplTest, DispatchDownstreamMessgaesDeleted) { |
| 970 | std::map<std::string, std::string> expected_data; |
| 971 | expected_data["message_type"] = "deleted_messages"; |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 972 | MCSMessage message(BuildDownstreamMessage( |
| 973 | kSender, kExtensionAppId, std::string() /* subtype */, expected_data, |
| 974 | std::string() /* raw_data */)); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 975 | EXPECT_TRUE(message.IsValid()); |
| 976 | ReceiveMessageFromMCS(message); |
| 977 | |
| 978 | EXPECT_EQ(MESSAGES_DELETED, last_event()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 979 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | TEST_F(GCMClientImplTest, SendMessage) { |
mvanouwerkerk | f8633deb | 2015-07-13 11:04:06 | [diff] [blame] | 983 | OutgoingMessage message; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 984 | message.id = "007"; |
| 985 | message.time_to_live = 500; |
| 986 | message.data["key"] = "value"; |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 987 | gcm_client()->Send(kExtensionAppId, kSender, message); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 988 | |
| 989 | EXPECT_EQ(kDataMessageStanzaTag, mcs_client()->last_message_tag()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 990 | EXPECT_EQ(kExtensionAppId, |
| 991 | mcs_client()->last_data_message_stanza().category()); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 992 | EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to()); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 993 | EXPECT_EQ(500, mcs_client()->last_data_message_stanza().ttl()); |
| 994 | EXPECT_EQ(CurrentTime(), mcs_client()->last_data_message_stanza().sent()); |
| 995 | EXPECT_EQ("007", mcs_client()->last_data_message_stanza().id()); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 996 | EXPECT_EQ("[email protected]", mcs_client()->last_data_message_stanza().from()); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 997 | EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to()); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 998 | EXPECT_EQ("key", mcs_client()->last_data_message_stanza().app_data(0).key()); |
| 999 | EXPECT_EQ("value", |
| 1000 | mcs_client()->last_data_message_stanza().app_data(0).value()); |
| 1001 | } |
| 1002 | |
[email protected] | 292af2b2 | 2014-08-06 19:42:45 | [diff] [blame] | 1003 | TEST_F(GCMClientImplTest, SendMessageAcknowledged) { |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1004 | ReceiveOnMessageSentToMCS(kExtensionAppId, "007", MCSClient::SENT); |
[email protected] | 292af2b2 | 2014-08-06 19:42:45 | [diff] [blame] | 1005 | EXPECT_EQ(MESSAGE_SEND_ACK, last_event()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1006 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
[email protected] | 292af2b2 | 2014-08-06 19:42:45 | [diff] [blame] | 1007 | EXPECT_EQ("007", last_message_id()); |
| 1008 | } |
| 1009 | |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 1010 | class GCMClientImplCheckinTest : public GCMClientImplTest { |
| 1011 | public: |
| 1012 | GCMClientImplCheckinTest(); |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 1013 | ~GCMClientImplCheckinTest() override; |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 1014 | |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 1015 | void SetUp() override; |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 1016 | }; |
| 1017 | |
| 1018 | GCMClientImplCheckinTest::GCMClientImplCheckinTest() { |
| 1019 | } |
| 1020 | |
| 1021 | GCMClientImplCheckinTest::~GCMClientImplCheckinTest() { |
| 1022 | } |
| 1023 | |
| 1024 | void GCMClientImplCheckinTest::SetUp() { |
| 1025 | testing::Test::SetUp(); |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 1026 | // Creating unique temp directory that will be used by GCMStore shared between |
| 1027 | // GCM Client and G-services settings. |
| 1028 | ASSERT_TRUE(CreateUniqueTempDir()); |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 1029 | // Time will be advancing one hour every time it is checked. |
| 1030 | BuildGCMClient(base::TimeDelta::FromSeconds(kSettingsCheckinInterval)); |
| 1031 | InitializeGCMClient(); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1032 | StartGCMClient(); |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | TEST_F(GCMClientImplCheckinTest, GServicesSettingsAfterInitialCheckin) { |
| 1036 | std::map<std::string, std::string> settings; |
| 1037 | settings["checkin_interval"] = base::Int64ToString(kSettingsCheckinInterval); |
| 1038 | settings["checkin_url"] = "http://alternative.url/checkin"; |
[email protected] | 8ab0c4b2 | 2014-05-10 20:40:13 | [diff] [blame] | 1039 | settings["gcm_hostname"] = "alternative.gcm.host"; |
| 1040 | settings["gcm_secure_port"] = "7777"; |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 1041 | settings["gcm_registration_url"] = "http://alternative.url/registration"; |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1042 | ASSERT_NO_FATAL_FAILURE( |
| 1043 | CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, |
| 1044 | GServicesSettings::CalculateDigest(settings), settings)); |
[email protected] | f0935451 | 2014-05-02 00:51:13 | [diff] [blame] | 1045 | EXPECT_EQ(base::TimeDelta::FromSeconds(kSettingsCheckinInterval), |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 1046 | gservices_settings().GetCheckinInterval()); |
[email protected] | 06e4527 | 2014-05-06 03:41:34 | [diff] [blame] | 1047 | EXPECT_EQ(GURL("http://alternative.url/checkin"), |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 1048 | gservices_settings().GetCheckinURL()); |
[email protected] | 06e4527 | 2014-05-06 03:41:34 | [diff] [blame] | 1049 | EXPECT_EQ(GURL("http://alternative.url/registration"), |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 1050 | gservices_settings().GetRegistrationURL()); |
[email protected] | 8ab0c4b2 | 2014-05-10 20:40:13 | [diff] [blame] | 1051 | EXPECT_EQ(GURL("https://alternative.gcm.host:7777"), |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 1052 | gservices_settings().GetMCSMainEndpoint()); |
[email protected] | 8ab0c4b2 | 2014-05-10 20:40:13 | [diff] [blame] | 1053 | EXPECT_EQ(GURL("https://alternative.gcm.host:443"), |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 1054 | gservices_settings().GetMCSFallbackEndpoint()); |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | // This test only checks that periodic checkin happens. |
| 1058 | TEST_F(GCMClientImplCheckinTest, PeriodicCheckin) { |
| 1059 | std::map<std::string, std::string> settings; |
ricea | f44cda2 | 2015-09-24 14:08:51 | [diff] [blame] | 1060 | settings["checkin_interval"] = base::Int64ToString(kSettingsCheckinInterval); |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 1061 | settings["checkin_url"] = "http://alternative.url/checkin"; |
[email protected] | 8ab0c4b2 | 2014-05-10 20:40:13 | [diff] [blame] | 1062 | settings["gcm_hostname"] = "alternative.gcm.host"; |
| 1063 | settings["gcm_secure_port"] = "7777"; |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 1064 | settings["gcm_registration_url"] = "http://alternative.url/registration"; |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1065 | ASSERT_NO_FATAL_FAILURE( |
| 1066 | CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, |
| 1067 | GServicesSettings::CalculateDigest(settings), settings)); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1068 | |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 1069 | EXPECT_EQ(2, clock()->call_count()); |
| 1070 | |
| 1071 | PumpLoopUntilIdle(); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1072 | ASSERT_NO_FATAL_FAILURE( |
| 1073 | CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, |
| 1074 | GServicesSettings::CalculateDigest(settings), settings)); |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 1075 | } |
| 1076 | |
[email protected] | 06e4527 | 2014-05-06 03:41:34 | [diff] [blame] | 1077 | TEST_F(GCMClientImplCheckinTest, LoadGSettingsFromStore) { |
| 1078 | std::map<std::string, std::string> settings; |
ricea | f44cda2 | 2015-09-24 14:08:51 | [diff] [blame] | 1079 | settings["checkin_interval"] = base::Int64ToString(kSettingsCheckinInterval); |
[email protected] | 06e4527 | 2014-05-06 03:41:34 | [diff] [blame] | 1080 | settings["checkin_url"] = "http://alternative.url/checkin"; |
[email protected] | 8ab0c4b2 | 2014-05-10 20:40:13 | [diff] [blame] | 1081 | settings["gcm_hostname"] = "alternative.gcm.host"; |
| 1082 | settings["gcm_secure_port"] = "7777"; |
[email protected] | 06e4527 | 2014-05-06 03:41:34 | [diff] [blame] | 1083 | settings["gcm_registration_url"] = "http://alternative.url/registration"; |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1084 | ASSERT_NO_FATAL_FAILURE( |
| 1085 | CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, |
| 1086 | GServicesSettings::CalculateDigest(settings), settings)); |
[email protected] | 06e4527 | 2014-05-06 03:41:34 | [diff] [blame] | 1087 | |
| 1088 | BuildGCMClient(base::TimeDelta()); |
| 1089 | InitializeGCMClient(); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1090 | StartGCMClient(); |
[email protected] | 06e4527 | 2014-05-06 03:41:34 | [diff] [blame] | 1091 | |
| 1092 | EXPECT_EQ(base::TimeDelta::FromSeconds(kSettingsCheckinInterval), |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 1093 | gservices_settings().GetCheckinInterval()); |
[email protected] | 06e4527 | 2014-05-06 03:41:34 | [diff] [blame] | 1094 | EXPECT_EQ(GURL("http://alternative.url/checkin"), |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 1095 | gservices_settings().GetCheckinURL()); |
[email protected] | 06e4527 | 2014-05-06 03:41:34 | [diff] [blame] | 1096 | EXPECT_EQ(GURL("http://alternative.url/registration"), |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 1097 | gservices_settings().GetRegistrationURL()); |
[email protected] | 8ab0c4b2 | 2014-05-10 20:40:13 | [diff] [blame] | 1098 | EXPECT_EQ(GURL("https://alternative.gcm.host:7777"), |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 1099 | gservices_settings().GetMCSMainEndpoint()); |
[email protected] | 8ab0c4b2 | 2014-05-10 20:40:13 | [diff] [blame] | 1100 | EXPECT_EQ(GURL("https://alternative.gcm.host:443"), |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 1101 | gservices_settings().GetMCSFallbackEndpoint()); |
[email protected] | 06e4527 | 2014-05-06 03:41:34 | [diff] [blame] | 1102 | } |
| 1103 | |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1104 | // This test only checks that periodic checkin happens. |
| 1105 | TEST_F(GCMClientImplCheckinTest, CheckinWithAccounts) { |
| 1106 | std::map<std::string, std::string> settings; |
ricea | f44cda2 | 2015-09-24 14:08:51 | [diff] [blame] | 1107 | settings["checkin_interval"] = base::Int64ToString(kSettingsCheckinInterval); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1108 | settings["checkin_url"] = "http://alternative.url/checkin"; |
| 1109 | settings["gcm_hostname"] = "alternative.gcm.host"; |
| 1110 | settings["gcm_secure_port"] = "7777"; |
| 1111 | settings["gcm_registration_url"] = "http://alternative.url/registration"; |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1112 | ASSERT_NO_FATAL_FAILURE( |
| 1113 | CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, |
| 1114 | GServicesSettings::CalculateDigest(settings), settings)); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1115 | |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 1116 | std::vector<GCMClient::AccountTokenInfo> account_tokens; |
| 1117 | account_tokens.push_back(MakeAccountToken("[email protected]", "token1")); |
| 1118 | account_tokens.push_back(MakeAccountToken("[email protected]", "token2")); |
| 1119 | gcm_client()->SetAccountTokens(account_tokens); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1120 | |
| 1121 | EXPECT_TRUE(device_checkin_info().last_checkin_accounts.empty()); |
| 1122 | EXPECT_TRUE(device_checkin_info().accounts_set); |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 1123 | EXPECT_EQ(MakeEmailToTokenMap(account_tokens), |
| 1124 | device_checkin_info().account_tokens); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1125 | |
| 1126 | PumpLoopUntilIdle(); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1127 | ASSERT_NO_FATAL_FAILURE( |
| 1128 | CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, |
| 1129 | GServicesSettings::CalculateDigest(settings), settings)); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1130 | |
| 1131 | std::set<std::string> accounts; |
| 1132 | accounts.insert("[email protected]"); |
| 1133 | accounts.insert("[email protected]"); |
| 1134 | EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts); |
| 1135 | EXPECT_TRUE(device_checkin_info().accounts_set); |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 1136 | EXPECT_EQ(MakeEmailToTokenMap(account_tokens), |
| 1137 | device_checkin_info().account_tokens); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | // This test only checks that periodic checkin happens. |
| 1141 | TEST_F(GCMClientImplCheckinTest, CheckinWhenAccountRemoved) { |
| 1142 | std::map<std::string, std::string> settings; |
ricea | f44cda2 | 2015-09-24 14:08:51 | [diff] [blame] | 1143 | settings["checkin_interval"] = base::Int64ToString(kSettingsCheckinInterval); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1144 | settings["checkin_url"] = "http://alternative.url/checkin"; |
| 1145 | settings["gcm_hostname"] = "alternative.gcm.host"; |
| 1146 | settings["gcm_secure_port"] = "7777"; |
| 1147 | settings["gcm_registration_url"] = "http://alternative.url/registration"; |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1148 | ASSERT_NO_FATAL_FAILURE( |
| 1149 | CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, |
| 1150 | GServicesSettings::CalculateDigest(settings), settings)); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1151 | |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 1152 | std::vector<GCMClient::AccountTokenInfo> account_tokens; |
| 1153 | account_tokens.push_back(MakeAccountToken("[email protected]", "token1")); |
| 1154 | account_tokens.push_back(MakeAccountToken("[email protected]", "token2")); |
| 1155 | gcm_client()->SetAccountTokens(account_tokens); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1156 | PumpLoopUntilIdle(); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1157 | ASSERT_NO_FATAL_FAILURE( |
| 1158 | CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, |
| 1159 | GServicesSettings::CalculateDigest(settings), settings)); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1160 | |
| 1161 | EXPECT_EQ(2UL, device_checkin_info().last_checkin_accounts.size()); |
| 1162 | EXPECT_TRUE(device_checkin_info().accounts_set); |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 1163 | EXPECT_EQ(MakeEmailToTokenMap(account_tokens), |
| 1164 | device_checkin_info().account_tokens); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1165 | |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 1166 | account_tokens.erase(account_tokens.begin() + 1); |
| 1167 | gcm_client()->SetAccountTokens(account_tokens); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1168 | |
| 1169 | PumpLoopUntilIdle(); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1170 | ASSERT_NO_FATAL_FAILURE( |
| 1171 | CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, |
| 1172 | GServicesSettings::CalculateDigest(settings), settings)); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1173 | |
| 1174 | std::set<std::string> accounts; |
| 1175 | accounts.insert("[email protected]"); |
| 1176 | EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts); |
| 1177 | EXPECT_TRUE(device_checkin_info().accounts_set); |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 1178 | EXPECT_EQ(MakeEmailToTokenMap(account_tokens), |
| 1179 | device_checkin_info().account_tokens); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | // This test only checks that periodic checkin happens. |
| 1183 | TEST_F(GCMClientImplCheckinTest, CheckinWhenAccountReplaced) { |
| 1184 | std::map<std::string, std::string> settings; |
ricea | f44cda2 | 2015-09-24 14:08:51 | [diff] [blame] | 1185 | settings["checkin_interval"] = base::Int64ToString(kSettingsCheckinInterval); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1186 | settings["checkin_url"] = "http://alternative.url/checkin"; |
| 1187 | settings["gcm_hostname"] = "alternative.gcm.host"; |
| 1188 | settings["gcm_secure_port"] = "7777"; |
| 1189 | settings["gcm_registration_url"] = "http://alternative.url/registration"; |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1190 | ASSERT_NO_FATAL_FAILURE( |
| 1191 | CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, |
| 1192 | GServicesSettings::CalculateDigest(settings), settings)); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1193 | |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 1194 | std::vector<GCMClient::AccountTokenInfo> account_tokens; |
| 1195 | account_tokens.push_back(MakeAccountToken("[email protected]", "token1")); |
| 1196 | gcm_client()->SetAccountTokens(account_tokens); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1197 | |
| 1198 | PumpLoopUntilIdle(); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1199 | ASSERT_NO_FATAL_FAILURE( |
| 1200 | CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, |
| 1201 | GServicesSettings::CalculateDigest(settings), settings)); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1202 | |
| 1203 | std::set<std::string> accounts; |
| 1204 | accounts.insert("[email protected]"); |
| 1205 | EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts); |
| 1206 | |
| 1207 | // This should trigger another checkin, because the list of accounts is |
| 1208 | // different. |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 1209 | account_tokens.clear(); |
| 1210 | account_tokens.push_back(MakeAccountToken("[email protected]", "token2")); |
| 1211 | gcm_client()->SetAccountTokens(account_tokens); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1212 | |
| 1213 | PumpLoopUntilIdle(); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1214 | ASSERT_NO_FATAL_FAILURE( |
| 1215 | CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, |
| 1216 | GServicesSettings::CalculateDigest(settings), settings)); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1217 | |
| 1218 | accounts.clear(); |
| 1219 | accounts.insert("[email protected]"); |
| 1220 | EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts); |
| 1221 | EXPECT_TRUE(device_checkin_info().accounts_set); |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 1222 | EXPECT_EQ(MakeEmailToTokenMap(account_tokens), |
| 1223 | device_checkin_info().account_tokens); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1224 | } |
| 1225 | |
zea | 76342abf | 2016-11-01 17:26:04 | [diff] [blame] | 1226 | TEST_F(GCMClientImplCheckinTest, ResetStoreWhenCheckinRejected) { |
| 1227 | base::HistogramTester histogram_tester; |
| 1228 | std::map<std::string, std::string> settings; |
| 1229 | ASSERT_NO_FATAL_FAILURE(FailCheckin(net::HTTP_UNAUTHORIZED)); |
| 1230 | PumpLoopUntilIdle(); |
| 1231 | |
| 1232 | // Store should have been destroyed. Restart client and verify the initial |
| 1233 | // checkin response is persisted. |
| 1234 | BuildGCMClient(base::TimeDelta()); |
| 1235 | InitializeGCMClient(); |
| 1236 | StartGCMClient(); |
| 1237 | ASSERT_NO_FATAL_FAILURE( |
| 1238 | CompleteCheckin(kDeviceAndroidId2, kDeviceSecurityToken2, |
| 1239 | GServicesSettings::CalculateDigest(settings), settings)); |
| 1240 | |
| 1241 | EXPECT_EQ(LOADING_COMPLETED, last_event()); |
| 1242 | EXPECT_EQ(kDeviceAndroidId2, mcs_client()->last_android_id()); |
| 1243 | EXPECT_EQ(kDeviceSecurityToken2, mcs_client()->last_security_token()); |
| 1244 | } |
| 1245 | |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1246 | class GCMClientImplStartAndStopTest : public GCMClientImplTest { |
jianli | 78b5604 | 2015-06-17 01:21:22 | [diff] [blame] | 1247 | public: |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1248 | GCMClientImplStartAndStopTest(); |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 1249 | ~GCMClientImplStartAndStopTest() override; |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1250 | |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 1251 | void SetUp() override; |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 1252 | |
| 1253 | void DefaultCompleteCheckin(); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1254 | }; |
| 1255 | |
| 1256 | GCMClientImplStartAndStopTest::GCMClientImplStartAndStopTest() { |
| 1257 | } |
| 1258 | |
| 1259 | GCMClientImplStartAndStopTest::~GCMClientImplStartAndStopTest() { |
| 1260 | } |
| 1261 | |
| 1262 | void GCMClientImplStartAndStopTest::SetUp() { |
| 1263 | testing::Test::SetUp(); |
| 1264 | ASSERT_TRUE(CreateUniqueTempDir()); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1265 | BuildGCMClient(base::TimeDelta()); |
| 1266 | InitializeGCMClient(); |
| 1267 | } |
| 1268 | |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 1269 | void GCMClientImplStartAndStopTest::DefaultCompleteCheckin() { |
| 1270 | SetUpUrlFetcherFactory(); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1271 | ASSERT_NO_FATAL_FAILURE( |
| 1272 | CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, std::string(), |
| 1273 | std::map<std::string, std::string>())); |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 1274 | PumpLoopUntilIdle(); |
| 1275 | } |
| 1276 | |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1277 | TEST_F(GCMClientImplStartAndStopTest, StartStopAndRestart) { |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1278 | // GCMClientImpl should be in INITIALIZED state at first. |
| 1279 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1280 | |
| 1281 | // Delay start the GCM. |
| 1282 | gcm_client()->Start(GCMClient::DELAYED_START); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1283 | PumpLoopUntilIdle(); |
jianli | 9f9a7367 | 2015-06-16 19:39:34 | [diff] [blame] | 1284 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1285 | |
| 1286 | // Stop the GCM. |
| 1287 | gcm_client()->Stop(); |
| 1288 | PumpLoopUntilIdle(); |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1289 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1290 | |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1291 | // Restart the GCM without delay. |
| 1292 | gcm_client()->Start(GCMClient::IMMEDIATE_START); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1293 | PumpLoopUntilIdle(); |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1294 | EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1295 | } |
| 1296 | |
jianli | 9f9a7367 | 2015-06-16 19:39:34 | [diff] [blame] | 1297 | TEST_F(GCMClientImplStartAndStopTest, DelayedStartAndStopImmediately) { |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1298 | // GCMClientImpl should be in INITIALIZED state at first. |
| 1299 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1300 | |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1301 | // Delay start the GCM and then stop it immediately. |
| 1302 | gcm_client()->Start(GCMClient::DELAYED_START); |
| 1303 | gcm_client()->Stop(); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1304 | PumpLoopUntilIdle(); |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1305 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
jianli | 9f9a7367 | 2015-06-16 19:39:34 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | TEST_F(GCMClientImplStartAndStopTest, ImmediateStartAndStopImmediately) { |
| 1309 | // GCMClientImpl should be in INITIALIZED state at first. |
| 1310 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1311 | |
| 1312 | // Start the GCM and then stop it immediately. |
| 1313 | gcm_client()->Start(GCMClient::IMMEDIATE_START); |
| 1314 | gcm_client()->Stop(); |
| 1315 | PumpLoopUntilIdle(); |
| 1316 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1317 | } |
| 1318 | |
jianli | 9f9a7367 | 2015-06-16 19:39:34 | [diff] [blame] | 1319 | TEST_F(GCMClientImplStartAndStopTest, DelayedStartStopAndRestart) { |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1320 | // GCMClientImpl should be in INITIALIZED state at first. |
| 1321 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1322 | |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1323 | // Delay start the GCM and then stop and restart it immediately. |
| 1324 | gcm_client()->Start(GCMClient::DELAYED_START); |
| 1325 | gcm_client()->Stop(); |
| 1326 | gcm_client()->Start(GCMClient::DELAYED_START); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1327 | PumpLoopUntilIdle(); |
jianli | 9f9a7367 | 2015-06-16 19:39:34 | [diff] [blame] | 1328 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1329 | } |
| 1330 | |
| 1331 | TEST_F(GCMClientImplStartAndStopTest, ImmediateStartStopAndRestart) { |
| 1332 | // GCMClientImpl should be in INITIALIZED state at first. |
| 1333 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1334 | |
| 1335 | // Start the GCM and then stop and restart it immediately. |
| 1336 | gcm_client()->Start(GCMClient::IMMEDIATE_START); |
| 1337 | gcm_client()->Stop(); |
| 1338 | gcm_client()->Start(GCMClient::IMMEDIATE_START); |
| 1339 | PumpLoopUntilIdle(); |
| 1340 | EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); |
| 1341 | } |
| 1342 | |
jianli | 9f9a7367 | 2015-06-16 19:39:34 | [diff] [blame] | 1343 | TEST_F(GCMClientImplStartAndStopTest, ImmediateStartAndThenImmediateStart) { |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1344 | // GCMClientImpl should be in INITIALIZED state at first. |
| 1345 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1346 | |
jianli | 9f9a7367 | 2015-06-16 19:39:34 | [diff] [blame] | 1347 | // Start the GCM immediately and complete the checkin. |
| 1348 | gcm_client()->Start(GCMClient::IMMEDIATE_START); |
| 1349 | PumpLoopUntilIdle(); |
| 1350 | EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1351 | ASSERT_NO_FATAL_FAILURE(DefaultCompleteCheckin()); |
jianli | 9f9a7367 | 2015-06-16 19:39:34 | [diff] [blame] | 1352 | EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); |
| 1353 | |
| 1354 | // Stop the GCM. |
| 1355 | gcm_client()->Stop(); |
| 1356 | PumpLoopUntilIdle(); |
| 1357 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1358 | |
| 1359 | // Start the GCM immediately. GCMClientImpl should be in READY state. |
| 1360 | BuildGCMClient(base::TimeDelta()); |
| 1361 | InitializeGCMClient(); |
| 1362 | gcm_client()->Start(GCMClient::IMMEDIATE_START); |
| 1363 | PumpLoopUntilIdle(); |
| 1364 | EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); |
| 1365 | } |
| 1366 | |
| 1367 | TEST_F(GCMClientImplStartAndStopTest, ImmediateStartAndThenDelayStart) { |
| 1368 | // GCMClientImpl should be in INITIALIZED state at first. |
| 1369 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1370 | |
| 1371 | // Start the GCM immediately and complete the checkin. |
| 1372 | gcm_client()->Start(GCMClient::IMMEDIATE_START); |
| 1373 | PumpLoopUntilIdle(); |
| 1374 | EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1375 | ASSERT_NO_FATAL_FAILURE(DefaultCompleteCheckin()); |
jianli | 9f9a7367 | 2015-06-16 19:39:34 | [diff] [blame] | 1376 | EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); |
| 1377 | |
| 1378 | // Stop the GCM. |
| 1379 | gcm_client()->Stop(); |
| 1380 | PumpLoopUntilIdle(); |
| 1381 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1382 | |
| 1383 | // Delay start the GCM. GCMClientImpl should be in LOADED state. |
| 1384 | BuildGCMClient(base::TimeDelta()); |
| 1385 | InitializeGCMClient(); |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1386 | gcm_client()->Start(GCMClient::DELAYED_START); |
| 1387 | PumpLoopUntilIdle(); |
| 1388 | EXPECT_EQ(GCMClientImpl::LOADED, gcm_client_state()); |
jianli | 9f9a7367 | 2015-06-16 19:39:34 | [diff] [blame] | 1389 | } |
| 1390 | |
johnme | 902c406 | 2015-08-21 22:06:17 | [diff] [blame] | 1391 | TEST_F(GCMClientImplStartAndStopTest, DelayedStartRace) { |
| 1392 | // GCMClientImpl should be in INITIALIZED state at first. |
| 1393 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1394 | |
| 1395 | // Delay start the GCM, then start it immediately while it's still loading. |
| 1396 | gcm_client()->Start(GCMClient::DELAYED_START); |
| 1397 | gcm_client()->Start(GCMClient::IMMEDIATE_START); |
| 1398 | PumpLoopUntilIdle(); |
| 1399 | EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1400 | ASSERT_NO_FATAL_FAILURE(DefaultCompleteCheckin()); |
johnme | 902c406 | 2015-08-21 22:06:17 | [diff] [blame] | 1401 | EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); |
| 1402 | } |
| 1403 | |
jianli | 9f9a7367 | 2015-06-16 19:39:34 | [diff] [blame] | 1404 | TEST_F(GCMClientImplStartAndStopTest, DelayedStart) { |
| 1405 | // GCMClientImpl should be in INITIALIZED state at first. |
| 1406 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1407 | |
| 1408 | // Delay start the GCM. The store will not be loaded and GCMClientImpl should |
| 1409 | // still be in INITIALIZED state. |
| 1410 | gcm_client()->Start(GCMClient::DELAYED_START); |
| 1411 | PumpLoopUntilIdle(); |
| 1412 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1413 | |
| 1414 | // Start the GCM immediately and complete the checkin. |
| 1415 | gcm_client()->Start(GCMClient::IMMEDIATE_START); |
| 1416 | PumpLoopUntilIdle(); |
| 1417 | EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1418 | ASSERT_NO_FATAL_FAILURE(DefaultCompleteCheckin()); |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1419 | EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); |
| 1420 | |
| 1421 | // Registration. |
| 1422 | std::vector<std::string> senders; |
| 1423 | senders.push_back("sender"); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1424 | Register(kExtensionAppId, senders); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1425 | ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id")); |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1426 | EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); |
| 1427 | |
| 1428 | // Stop the GCM. |
| 1429 | gcm_client()->Stop(); |
| 1430 | PumpLoopUntilIdle(); |
| 1431 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1432 | |
| 1433 | // Delay start the GCM. GCM is indeed started without delay because the |
| 1434 | // registration record has been found. |
jianli | 9f9a7367 | 2015-06-16 19:39:34 | [diff] [blame] | 1435 | BuildGCMClient(base::TimeDelta()); |
| 1436 | InitializeGCMClient(); |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1437 | gcm_client()->Start(GCMClient::DELAYED_START); |
| 1438 | PumpLoopUntilIdle(); |
| 1439 | EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1440 | } |
| 1441 | |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 1442 | // Test for known account mappings and last token fetching time being passed |
| 1443 | // to OnGCMReady. |
| 1444 | TEST_F(GCMClientImplStartAndStopTest, OnGCMReadyAccountsAndTokenFetchingTime) { |
| 1445 | // Start the GCM and wait until it is ready. |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1446 | gcm_client()->Start(GCMClient::IMMEDIATE_START); |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 1447 | PumpLoopUntilIdle(); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1448 | ASSERT_NO_FATAL_FAILURE(DefaultCompleteCheckin()); |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 1449 | |
| 1450 | base::Time expected_time = base::Time::Now(); |
| 1451 | gcm_client()->SetLastTokenFetchTime(expected_time); |
| 1452 | AccountMapping expected_mapping; |
| 1453 | expected_mapping.account_id = "accId"; |
| 1454 | expected_mapping.email = "[email protected]"; |
| 1455 | expected_mapping.status = AccountMapping::MAPPED; |
| 1456 | expected_mapping.status_change_timestamp = expected_time; |
| 1457 | gcm_client()->UpdateAccountMapping(expected_mapping); |
| 1458 | PumpLoopUntilIdle(); |
| 1459 | |
| 1460 | // Stop the GCM. |
| 1461 | gcm_client()->Stop(); |
| 1462 | PumpLoopUntilIdle(); |
| 1463 | |
| 1464 | // Restart the GCM. |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1465 | gcm_client()->Start(GCMClient::IMMEDIATE_START); |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 1466 | PumpLoopUntilIdle(); |
| 1467 | |
| 1468 | EXPECT_EQ(LOADING_COMPLETED, last_event()); |
| 1469 | EXPECT_EQ(expected_time, last_token_fetch_time()); |
| 1470 | ASSERT_EQ(1UL, last_account_mappings().size()); |
| 1471 | const AccountMapping& actual_mapping = last_account_mappings()[0]; |
| 1472 | EXPECT_EQ(expected_mapping.account_id, actual_mapping.account_id); |
| 1473 | EXPECT_EQ(expected_mapping.email, actual_mapping.email); |
| 1474 | EXPECT_EQ(expected_mapping.status, actual_mapping.status); |
| 1475 | EXPECT_EQ(expected_mapping.status_change_timestamp, |
| 1476 | actual_mapping.status_change_timestamp); |
| 1477 | } |
| 1478 | |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1479 | |
| 1480 | class GCMClientInstanceIDTest : public GCMClientImplTest { |
| 1481 | public: |
| 1482 | GCMClientInstanceIDTest(); |
| 1483 | ~GCMClientInstanceIDTest() override; |
| 1484 | |
| 1485 | void AddInstanceID(const std::string& app_id, |
| 1486 | const std::string& instance_id); |
| 1487 | void RemoveInstanceID(const std::string& app_id); |
| 1488 | void GetToken(const std::string& app_id, |
| 1489 | const std::string& authorized_entity, |
| 1490 | const std::string& scope); |
| 1491 | void DeleteToken(const std::string& app_id, |
| 1492 | const std::string& authorized_entity, |
| 1493 | const std::string& scope); |
| 1494 | void CompleteDeleteToken(); |
| 1495 | bool ExistsToken(const std::string& app_id, |
| 1496 | const std::string& authorized_entity, |
| 1497 | const std::string& scope) const; |
| 1498 | }; |
| 1499 | |
| 1500 | GCMClientInstanceIDTest::GCMClientInstanceIDTest() { |
| 1501 | } |
| 1502 | |
| 1503 | GCMClientInstanceIDTest::~GCMClientInstanceIDTest() { |
| 1504 | } |
| 1505 | |
| 1506 | void GCMClientInstanceIDTest::AddInstanceID(const std::string& app_id, |
| 1507 | const std::string& instance_id) { |
| 1508 | gcm_client()->AddInstanceIDData(app_id, instance_id, "123"); |
| 1509 | } |
| 1510 | |
| 1511 | void GCMClientInstanceIDTest::RemoveInstanceID(const std::string& app_id) { |
| 1512 | gcm_client()->RemoveInstanceIDData(app_id); |
| 1513 | } |
| 1514 | |
| 1515 | void GCMClientInstanceIDTest::GetToken(const std::string& app_id, |
| 1516 | const std::string& authorized_entity, |
| 1517 | const std::string& scope) { |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 1518 | std::unique_ptr<InstanceIDTokenInfo> instance_id_info( |
| 1519 | new InstanceIDTokenInfo); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1520 | instance_id_info->app_id = app_id; |
| 1521 | instance_id_info->authorized_entity = authorized_entity; |
| 1522 | instance_id_info->scope = scope; |
| 1523 | gcm_client()->Register( |
| 1524 | make_linked_ptr<RegistrationInfo>(instance_id_info.release())); |
| 1525 | } |
| 1526 | |
| 1527 | void GCMClientInstanceIDTest::DeleteToken(const std::string& app_id, |
| 1528 | const std::string& authorized_entity, |
| 1529 | const std::string& scope) { |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 1530 | std::unique_ptr<InstanceIDTokenInfo> instance_id_info( |
| 1531 | new InstanceIDTokenInfo); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1532 | instance_id_info->app_id = app_id; |
| 1533 | instance_id_info->authorized_entity = authorized_entity; |
| 1534 | instance_id_info->scope = scope; |
| 1535 | gcm_client()->Unregister( |
| 1536 | make_linked_ptr<RegistrationInfo>(instance_id_info.release())); |
| 1537 | } |
| 1538 | |
| 1539 | void GCMClientInstanceIDTest::CompleteDeleteToken() { |
| 1540 | std::string response(kDeleteTokenResponse); |
| 1541 | net::TestURLFetcher* fetcher = url_fetcher_factory()->GetFetcherByID(0); |
| 1542 | ASSERT_TRUE(fetcher); |
| 1543 | fetcher->set_response_code(net::HTTP_OK); |
| 1544 | fetcher->SetResponseString(response); |
| 1545 | fetcher->delegate()->OnURLFetchComplete(fetcher); |
jianli | d7e80f2 | 2015-06-18 22:21:31 | [diff] [blame] | 1546 | // Give a chance for GCMStoreImpl::Backend to finish persisting data. |
| 1547 | PumpLoopUntilIdle(); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | bool GCMClientInstanceIDTest::ExistsToken(const std::string& app_id, |
| 1551 | const std::string& authorized_entity, |
| 1552 | const std::string& scope) const { |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 1553 | std::unique_ptr<InstanceIDTokenInfo> instance_id_info( |
| 1554 | new InstanceIDTokenInfo); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1555 | instance_id_info->app_id = app_id; |
| 1556 | instance_id_info->authorized_entity = authorized_entity; |
| 1557 | instance_id_info->scope = scope; |
| 1558 | return gcm_client()->registrations_.count( |
| 1559 | make_linked_ptr<RegistrationInfo>(instance_id_info.release())) > 0; |
| 1560 | } |
| 1561 | |
| 1562 | TEST_F(GCMClientInstanceIDTest, GetToken) { |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1563 | AddInstanceID(kExtensionAppId, kInstanceID); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1564 | |
| 1565 | // Get a token. |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1566 | EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope)); |
| 1567 | GetToken(kExtensionAppId, kSender, kScope); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1568 | ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1")); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1569 | |
| 1570 | EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1571 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1572 | EXPECT_EQ("token1", last_registration_id()); |
| 1573 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1574 | EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope)); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1575 | |
| 1576 | // Get another token. |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1577 | EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender2, kScope)); |
| 1578 | GetToken(kExtensionAppId, kSender2, kScope); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1579 | ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2")); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1580 | |
| 1581 | EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1582 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1583 | EXPECT_EQ("token2", last_registration_id()); |
| 1584 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1585 | EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender2, kScope)); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1586 | // The 1st token still exists. |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1587 | EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope)); |
| 1588 | } |
| 1589 | |
| 1590 | // Most tests in this file use kExtensionAppId which is special-cased by |
| 1591 | // InstanceIDUsesSubtypeForAppId in gcm_client_impl.cc. This test uses |
| 1592 | // kSubtypeAppId to cover the alternate case. |
| 1593 | TEST_F(GCMClientInstanceIDTest, GetTokenWithSubtype) { |
| 1594 | ASSERT_EQ(GCMClientImpl::READY, gcm_client_state()); |
| 1595 | |
| 1596 | AddInstanceID(kSubtypeAppId, kInstanceID); |
| 1597 | |
| 1598 | EXPECT_FALSE(ExistsToken(kSubtypeAppId, kSender, kScope)); |
| 1599 | |
| 1600 | // Get a token. |
| 1601 | GetToken(kSubtypeAppId, kSender, kScope); |
| 1602 | ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1")); |
| 1603 | EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
| 1604 | EXPECT_EQ(kSubtypeAppId, last_app_id()); |
| 1605 | EXPECT_EQ("token1", last_registration_id()); |
| 1606 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 1607 | EXPECT_TRUE(ExistsToken(kSubtypeAppId, kSender, kScope)); |
| 1608 | |
| 1609 | // Delete the token. |
| 1610 | DeleteToken(kSubtypeAppId, kSender, kScope); |
| 1611 | ASSERT_NO_FATAL_FAILURE(CompleteDeleteToken()); |
| 1612 | EXPECT_FALSE(ExistsToken(kSubtypeAppId, kSender, kScope)); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1613 | } |
| 1614 | |
jianli | 70715cc | 2015-06-23 21:46:48 | [diff] [blame] | 1615 | TEST_F(GCMClientInstanceIDTest, DeleteInvalidToken) { |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1616 | AddInstanceID(kExtensionAppId, kInstanceID); |
jianli | 70715cc | 2015-06-23 21:46:48 | [diff] [blame] | 1617 | |
| 1618 | // Delete an invalid token. |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1619 | DeleteToken(kExtensionAppId, "Foo@#$", kScope); |
jianli | 70715cc | 2015-06-23 21:46:48 | [diff] [blame] | 1620 | PumpLoopUntilIdle(); |
| 1621 | |
| 1622 | EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1623 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
jianli | 70715cc | 2015-06-23 21:46:48 | [diff] [blame] | 1624 | EXPECT_EQ(GCMClient::INVALID_PARAMETER, last_result()); |
| 1625 | |
| 1626 | reset_last_event(); |
| 1627 | |
| 1628 | // Delete a non-existing token. |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1629 | DeleteToken(kExtensionAppId, kSender, kScope); |
jianli | 70715cc | 2015-06-23 21:46:48 | [diff] [blame] | 1630 | PumpLoopUntilIdle(); |
| 1631 | |
| 1632 | EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1633 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
jianli | 70715cc | 2015-06-23 21:46:48 | [diff] [blame] | 1634 | EXPECT_EQ(GCMClient::INVALID_PARAMETER, last_result()); |
| 1635 | } |
| 1636 | |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1637 | TEST_F(GCMClientInstanceIDTest, DeleteSingleToken) { |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1638 | AddInstanceID(kExtensionAppId, kInstanceID); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1639 | |
| 1640 | // Get a token. |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1641 | EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope)); |
| 1642 | GetToken(kExtensionAppId, kSender, kScope); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1643 | ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1")); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1644 | |
| 1645 | EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1646 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1647 | EXPECT_EQ("token1", last_registration_id()); |
| 1648 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1649 | EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope)); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1650 | |
jianli | 70715cc | 2015-06-23 21:46:48 | [diff] [blame] | 1651 | reset_last_event(); |
| 1652 | |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1653 | // Get another token. |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1654 | EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender2, kScope)); |
| 1655 | GetToken(kExtensionAppId, kSender2, kScope); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1656 | ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2")); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1657 | |
| 1658 | EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1659 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1660 | EXPECT_EQ("token2", last_registration_id()); |
| 1661 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1662 | EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender2, kScope)); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1663 | // The 1st token still exists. |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1664 | EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope)); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1665 | |
jianli | 70715cc | 2015-06-23 21:46:48 | [diff] [blame] | 1666 | reset_last_event(); |
| 1667 | |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1668 | // Delete the 2nd token. |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1669 | DeleteToken(kExtensionAppId, kSender2, kScope); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1670 | ASSERT_NO_FATAL_FAILURE(CompleteDeleteToken()); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1671 | |
| 1672 | EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1673 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1674 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 1675 | // The 2nd token is gone while the 1st token still exists. |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1676 | EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope)); |
| 1677 | EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender2, kScope)); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1678 | |
jianli | 70715cc | 2015-06-23 21:46:48 | [diff] [blame] | 1679 | reset_last_event(); |
| 1680 | |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1681 | // Delete the 1st token. |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1682 | DeleteToken(kExtensionAppId, kSender, kScope); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1683 | ASSERT_NO_FATAL_FAILURE(CompleteDeleteToken()); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1684 | |
| 1685 | EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1686 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1687 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 1688 | // Both tokens are gone now. |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1689 | EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope)); |
| 1690 | EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope)); |
jianli | 70715cc | 2015-06-23 21:46:48 | [diff] [blame] | 1691 | |
| 1692 | reset_last_event(); |
| 1693 | |
| 1694 | // Trying to delete the token again will get an error. |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1695 | DeleteToken(kExtensionAppId, kSender, kScope); |
jianli | 70715cc | 2015-06-23 21:46:48 | [diff] [blame] | 1696 | PumpLoopUntilIdle(); |
| 1697 | |
| 1698 | EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1699 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
jianli | 70715cc | 2015-06-23 21:46:48 | [diff] [blame] | 1700 | EXPECT_EQ(GCMClient::INVALID_PARAMETER, last_result()); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1701 | } |
| 1702 | |
jianli | ea853487 | 2015-06-22 21:06:22 | [diff] [blame] | 1703 | TEST_F(GCMClientInstanceIDTest, DeleteAllTokens) { |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1704 | AddInstanceID(kExtensionAppId, kInstanceID); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1705 | |
| 1706 | // Get a token. |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1707 | EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope)); |
| 1708 | GetToken(kExtensionAppId, kSender, kScope); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1709 | ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1")); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1710 | |
| 1711 | EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1712 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1713 | EXPECT_EQ("token1", last_registration_id()); |
| 1714 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1715 | EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope)); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1716 | |
jianli | 70715cc | 2015-06-23 21:46:48 | [diff] [blame] | 1717 | reset_last_event(); |
| 1718 | |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1719 | // Get another token. |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1720 | EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender2, kScope)); |
| 1721 | GetToken(kExtensionAppId, kSender2, kScope); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1722 | ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2")); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1723 | |
| 1724 | EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1725 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1726 | EXPECT_EQ("token2", last_registration_id()); |
| 1727 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1728 | EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender2, kScope)); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1729 | // The 1st token still exists. |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1730 | EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope)); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1731 | |
jianli | 70715cc | 2015-06-23 21:46:48 | [diff] [blame] | 1732 | reset_last_event(); |
| 1733 | |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1734 | // Delete all tokens. |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1735 | DeleteToken(kExtensionAppId, "*", "*"); |
johnme | 0fb2fc0 | 2016-07-14 14:47:44 | [diff] [blame] | 1736 | ASSERT_NO_FATAL_FAILURE(CompleteDeleteToken()); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1737 | |
| 1738 | EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1739 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1740 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 1741 | // All tokens are gone now. |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1742 | EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope)); |
| 1743 | EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope)); |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame] | 1744 | } |
| 1745 | |
jianli | ea853487 | 2015-06-22 21:06:22 | [diff] [blame] | 1746 | TEST_F(GCMClientInstanceIDTest, DeleteAllTokensBeforeGetAnyToken) { |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1747 | AddInstanceID(kExtensionAppId, kInstanceID); |
jianli | ea853487 | 2015-06-22 21:06:22 | [diff] [blame] | 1748 | |
| 1749 | // Delete all tokens without getting a token first. |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1750 | DeleteToken(kExtensionAppId, "*", "*"); |
jianli | ea853487 | 2015-06-22 21:06:22 | [diff] [blame] | 1751 | // No need to call CompleteDeleteToken since unregistration request should |
| 1752 | // not be triggered. |
| 1753 | PumpLoopUntilIdle(); |
| 1754 | |
| 1755 | EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1756 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
jianli | ea853487 | 2015-06-22 21:06:22 | [diff] [blame] | 1757 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 1758 | } |
| 1759 | |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1760 | TEST_F(GCMClientInstanceIDTest, DispatchDownstreamMessageWithoutSubtype) { |
| 1761 | AddInstanceID(kExtensionAppId, kInstanceID); |
| 1762 | GetToken(kExtensionAppId, kSender, kScope); |
| 1763 | ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1")); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1764 | |
| 1765 | std::map<std::string, std::string> expected_data; |
| 1766 | |
Peter Beverloo | c66fc63c | 2017-07-07 22:58:21 | [diff] [blame] | 1767 | MCSMessage message(BuildDownstreamMessage( |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1768 | kSender, kExtensionAppId, std::string() /* subtype */, expected_data, |
| 1769 | std::string() /* raw_data */)); |
Peter Beverloo | c66fc63c | 2017-07-07 22:58:21 | [diff] [blame] | 1770 | EXPECT_TRUE(message.IsValid()); |
| 1771 | ReceiveMessageFromMCS(message); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1772 | |
| 1773 | EXPECT_EQ(MESSAGE_RECEIVED, last_event()); |
| 1774 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
| 1775 | EXPECT_EQ(expected_data.size(), last_message().data.size()); |
| 1776 | EXPECT_EQ(expected_data, last_message().data); |
| 1777 | EXPECT_EQ(kSender, last_message().sender_id); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | TEST_F(GCMClientInstanceIDTest, DispatchDownstreamMessageWithSubtype) { |
| 1781 | AddInstanceID(kSubtypeAppId, kInstanceID); |
| 1782 | GetToken(kSubtypeAppId, kSender, kScope); |
| 1783 | ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1")); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1784 | |
| 1785 | std::map<std::string, std::string> expected_data; |
| 1786 | |
Peter Beverloo | c66fc63c | 2017-07-07 22:58:21 | [diff] [blame] | 1787 | MCSMessage message(BuildDownstreamMessage( |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1788 | kSender, kProductCategoryForSubtypes, kSubtypeAppId /* subtype */, |
| 1789 | expected_data, std::string() /* raw_data */)); |
Peter Beverloo | c66fc63c | 2017-07-07 22:58:21 | [diff] [blame] | 1790 | EXPECT_TRUE(message.IsValid()); |
| 1791 | ReceiveMessageFromMCS(message); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1792 | |
| 1793 | EXPECT_EQ(MESSAGE_RECEIVED, last_event()); |
| 1794 | EXPECT_EQ(kSubtypeAppId, last_app_id()); |
| 1795 | EXPECT_EQ(expected_data.size(), last_message().data.size()); |
| 1796 | EXPECT_EQ(expected_data, last_message().data); |
| 1797 | EXPECT_EQ(kSender, last_message().sender_id); |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 1798 | } |
| 1799 | |
| 1800 | TEST_F(GCMClientInstanceIDTest, DispatchDownstreamMessageWithFakeSubtype) { |
| 1801 | // Victim non-extension registration. |
| 1802 | AddInstanceID(kSubtypeAppId, "iid_1"); |
| 1803 | GetToken(kSubtypeAppId, kSender, kScope); |
| 1804 | ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1")); |
| 1805 | |
| 1806 | // Malicious extension registration. |
| 1807 | AddInstanceID(kExtensionAppId, "iid_2"); |
| 1808 | GetToken(kExtensionAppId, kSender, kScope); |
| 1809 | ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2")); |
| 1810 | |
| 1811 | std::map<std::string, std::string> expected_data; |
| 1812 | |
| 1813 | // Message for kExtensionAppId should be delivered to the extension rather |
| 1814 | // than the victim app, despite the malicious subtype property attempting to |
| 1815 | // impersonate victim app. |
| 1816 | MCSMessage message(BuildDownstreamMessage( |
| 1817 | kSender, kExtensionAppId /* category */, kSubtypeAppId /* subtype */, |
| 1818 | expected_data, std::string() /* raw_data */)); |
| 1819 | EXPECT_TRUE(message.IsValid()); |
| 1820 | ReceiveMessageFromMCS(message); |
| 1821 | |
| 1822 | EXPECT_EQ(MESSAGE_RECEIVED, last_event()); |
| 1823 | EXPECT_EQ(kExtensionAppId, last_app_id()); |
| 1824 | EXPECT_EQ(expected_data.size(), last_message().data.size()); |
| 1825 | EXPECT_EQ(expected_data, last_message().data); |
| 1826 | EXPECT_EQ(kSender, last_message().sender_id); |
| 1827 | } |
| 1828 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 1829 | } // namespace gcm |