OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/strings/string_number_conversions.h" | 5 #include "base/strings/string_number_conversions.h" |
6 #include "google_apis/gcm/engine/gservices_settings.h" | 6 #include "google_apis/gcm/engine/gservices_settings.h" |
7 #include "google_apis/gcm/engine/registration_info.h" | 7 #include "google_apis/gcm/engine/registration_info.h" |
8 #include "google_apis/gcm/gcm_client.h" | 8 #include "google_apis/gcm/gcm_client.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
11 namespace gcm { | 11 namespace gcm { |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 const int64 kAlternativeCheckinInterval = 2000LL; | 15 const int64 kAlternativeCheckinInterval = 16 * 60 * 60; |
16 const char kAlternativeCheckinURL[] = "http://alternative.url/checkin"; | 16 const char kAlternativeCheckinURL[] = "http://alternative.url/checkin"; |
17 const char kAlternativeMCSHostname[] = "http://alternative.gcm.host"; | 17 const char kAlternativeMCSHostname[] = "http://alternative.gcm.host"; |
18 const int kAlternativeMCSSecurePort = 443; | 18 const int kAlternativeMCSSecurePort = 443; |
19 const char kAlternativeRegistrationURL[] = | 19 const char kAlternativeRegistrationURL[] = |
20 "http://alternative.url/registration"; | 20 "http://alternative.url/registration"; |
21 | 21 |
22 const int64 kDefaultCheckinInterval = 2 * 24 * 60 * 60; // seconds = 2 days. | 22 const int64 kDefaultCheckinInterval = 2 * 24 * 60 * 60; // seconds = 2 days. |
23 const char kDefaultCheckinURL[] = "https://android.clients.google.com/checkin"; | 23 const char kDefaultCheckinURL[] = "https://android.clients.google.com/checkin"; |
24 const char kDefaultMCSHostname[] = "https://mtalk.google.com"; | 24 const char kDefaultMCSHostname[] = "https://mtalk.google.com"; |
25 const int kDefaultMCSSecurePort = 5228; | 25 const int kDefaultMCSSecurePort = 5228; |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 GServicesSettings& settings() { | 114 GServicesSettings& settings() { |
115 return gserivces_settings_; | 115 return gserivces_settings_; |
116 } | 116 } |
117 | 117 |
118 const std::map<std::string, std::string>& alternative_settings() { | 118 const std::map<std::string, std::string>& alternative_settings() { |
119 return alternative_settings_; | 119 return alternative_settings_; |
120 } | 120 } |
121 | 121 |
122 FakeGCMStore& gcm_store() { return gcm_store_; } | 122 FakeGCMStore& gcm_store() { return gcm_store_; } |
123 | 123 |
| 124 std::map<std::string, std::string> alternative_settings_; |
| 125 |
124 private: | 126 private: |
125 FakeGCMStore gcm_store_; | 127 FakeGCMStore gcm_store_; |
126 GServicesSettings gserivces_settings_; | 128 GServicesSettings gserivces_settings_; |
127 std::map<std::string, std::string> alternative_settings_; | |
128 }; | 129 }; |
129 | 130 |
130 GServicesSettingsTest::GServicesSettingsTest() | 131 GServicesSettingsTest::GServicesSettingsTest() |
131 : gserivces_settings_(&gcm_store_) { | 132 : gserivces_settings_(&gcm_store_) { |
132 } | 133 } |
133 | 134 |
134 GServicesSettingsTest::~GServicesSettingsTest() {} | 135 GServicesSettingsTest::~GServicesSettingsTest() {} |
135 | 136 |
136 void GServicesSettingsTest::SetUp() { | 137 void GServicesSettingsTest::SetUp() { |
137 alternative_settings_["checkin_interval"] = | 138 alternative_settings_["checkin_interval"] = |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 setting->set_value(iter->second); | 170 setting->set_value(iter->second); |
170 } | 171 } |
171 } | 172 } |
172 | 173 |
173 // Verifies default values of the G-services settings and settings digest. | 174 // Verifies default values of the G-services settings and settings digest. |
174 TEST_F(GServicesSettingsTest, DefaultSettingsAndDigest) { | 175 TEST_F(GServicesSettingsTest, DefaultSettingsAndDigest) { |
175 CheckAllSetToDefault(); | 176 CheckAllSetToDefault(); |
176 EXPECT_EQ(std::string(), settings().digest()); | 177 EXPECT_EQ(std::string(), settings().digest()); |
177 } | 178 } |
178 | 179 |
| 180 // Verifies that settings are not updated when load result is empty. |
| 181 TEST_F(GServicesSettingsTest, UpdateFromEmptyLoadResult) { |
| 182 GCMStore::LoadResult result; |
| 183 result.gservices_digest = "digest_value"; |
| 184 settings().UpdateFromLoadResult(result); |
| 185 |
| 186 CheckAllSetToDefault(); |
| 187 EXPECT_EQ(std::string(), settings().digest()); |
| 188 } |
| 189 |
| 190 // Verifies that settings are not updated when one of them is missing. |
| 191 TEST_F(GServicesSettingsTest, UpdateFromLoadResultWithSettingMissing) { |
| 192 GCMStore::LoadResult result; |
| 193 result.gservices_settings = alternative_settings(); |
| 194 result.gservices_digest = "digest_value"; |
| 195 result.gservices_settings.erase("gcm_hostname"); |
| 196 settings().UpdateFromLoadResult(result); |
| 197 |
| 198 CheckAllSetToDefault(); |
| 199 EXPECT_EQ(std::string(), settings().digest()); |
| 200 } |
| 201 |
179 // Verifies that the settings are set correctly based on the load result. | 202 // Verifies that the settings are set correctly based on the load result. |
180 TEST_F(GServicesSettingsTest, UpdateFromLoadResult) { | 203 TEST_F(GServicesSettingsTest, UpdateFromLoadResult) { |
181 GCMStore::LoadResult result; | 204 GCMStore::LoadResult result; |
182 result.gservices_settings = alternative_settings(); | 205 result.gservices_settings = alternative_settings(); |
183 result.gservices_digest = "digest_value"; | 206 result.gservices_digest = "digest_value"; |
184 settings().UpdateFromLoadResult(result); | 207 settings().UpdateFromLoadResult(result); |
185 | 208 |
186 CheckAllSetToAlternative(); | 209 CheckAllSetToAlternative(); |
187 EXPECT_EQ("digest_value", settings().digest()); | 210 EXPECT_EQ("digest_value", settings().digest()); |
188 } | 211 } |
189 | 212 |
190 // Verifies that the settings are set correctly after parsing a checkin | 213 // Verifies that the settings are set correctly after parsing a checkin |
191 // response. | 214 // response. |
192 TEST_F(GServicesSettingsTest, UpdateFromCheckinResponse) { | 215 TEST_F(GServicesSettingsTest, UpdateFromCheckinResponse) { |
193 checkin_proto::AndroidCheckinResponse checkin_response; | 216 checkin_proto::AndroidCheckinResponse checkin_response; |
194 | 217 |
195 checkin_response.set_digest("digest_value"); | 218 checkin_response.set_digest("digest_value"); |
196 SetWithAlternativeSettings(checkin_response); | 219 SetWithAlternativeSettings(checkin_response); |
197 | 220 |
198 settings().UpdateFromCheckinResponse(checkin_response); | 221 settings().UpdateFromCheckinResponse(checkin_response); |
199 EXPECT_TRUE(gcm_store().settings_saved()); | 222 EXPECT_TRUE(gcm_store().settings_saved()); |
200 | 223 |
201 CheckAllSetToAlternative(); | 224 CheckAllSetToAlternative(); |
202 EXPECT_EQ("digest_value", settings().digest()); | 225 EXPECT_EQ("digest_value", settings().digest()); |
203 } | 226 } |
204 | 227 |
| 228 // Verifies that the checkin interval is updated to minimum if the original |
| 229 // value is less than minimum. |
| 230 TEST_F(GServicesSettingsTest, UpdateFromCheckinResponseMinimumCheckinInterval) { |
| 231 checkin_proto::AndroidCheckinResponse checkin_response; |
| 232 |
| 233 checkin_response.set_digest("digest_value"); |
| 234 // Setting the checkin interval to less than minimum. |
| 235 alternative_settings_["checkin_interval"] = base::IntToString(3600); |
| 236 SetWithAlternativeSettings(checkin_response); |
| 237 |
| 238 settings().UpdateFromCheckinResponse(checkin_response); |
| 239 EXPECT_TRUE(gcm_store().settings_saved()); |
| 240 |
| 241 EXPECT_EQ(GServicesSettings::kMinimumCheckinInterval, |
| 242 settings().checkin_interval()); |
| 243 EXPECT_EQ("digest_value", settings().digest()); |
| 244 } |
| 245 |
| 246 // Verifies that settings are not updated when one of them is missing. |
| 247 TEST_F(GServicesSettingsTest, UpdateFromCheckinResponseWithSettingMissing) { |
| 248 checkin_proto::AndroidCheckinResponse checkin_response; |
| 249 |
| 250 checkin_response.set_digest("digest_value"); |
| 251 alternative_settings_.erase("gcm_hostname"); |
| 252 SetWithAlternativeSettings(checkin_response); |
| 253 |
| 254 settings().UpdateFromCheckinResponse(checkin_response); |
| 255 EXPECT_FALSE(gcm_store().settings_saved()); |
| 256 |
| 257 CheckAllSetToDefault(); |
| 258 EXPECT_EQ(std::string(), settings().digest()); |
| 259 } |
| 260 |
205 // Verifies that no update is done, when a checkin response misses digest. | 261 // Verifies that no update is done, when a checkin response misses digest. |
206 TEST_F(GServicesSettingsTest, UpdateFromCheckinResponseNoDigest) { | 262 TEST_F(GServicesSettingsTest, UpdateFromCheckinResponseNoDigest) { |
207 checkin_proto::AndroidCheckinResponse checkin_response; | 263 checkin_proto::AndroidCheckinResponse checkin_response; |
208 | 264 |
209 SetWithAlternativeSettings(checkin_response); | 265 SetWithAlternativeSettings(checkin_response); |
210 settings().UpdateFromCheckinResponse(checkin_response); | 266 settings().UpdateFromCheckinResponse(checkin_response); |
211 EXPECT_FALSE(gcm_store().settings_saved()); | 267 EXPECT_FALSE(gcm_store().settings_saved()); |
212 | 268 |
213 CheckAllSetToDefault(); | 269 CheckAllSetToDefault(); |
214 EXPECT_EQ(std::string(), settings().digest()); | 270 EXPECT_EQ(std::string(), settings().digest()); |
(...skipping 10 matching lines...) Expand all Loading... |
225 checkin_response.set_digest("old_digest"); | 281 checkin_response.set_digest("old_digest"); |
226 SetWithAlternativeSettings(checkin_response); | 282 SetWithAlternativeSettings(checkin_response); |
227 settings().UpdateFromCheckinResponse(checkin_response); | 283 settings().UpdateFromCheckinResponse(checkin_response); |
228 EXPECT_FALSE(gcm_store().settings_saved()); | 284 EXPECT_FALSE(gcm_store().settings_saved()); |
229 | 285 |
230 CheckAllSetToAlternative(); | 286 CheckAllSetToAlternative(); |
231 EXPECT_EQ("old_digest", settings().digest()); | 287 EXPECT_EQ("old_digest", settings().digest()); |
232 } | 288 } |
233 | 289 |
234 } // namespace gcm | 290 } // namespace gcm |
OLD | NEW |