Roll src/third_party/WebKit 3529d49:06e8485 (svn 202554:202555)
[chromium-blink-merge.git] / google_apis / gcm / engine / gservices_settings_unittest.cc
blobfcf633ec55b5e42dcb93618cd6de7073c2cad59e
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.
5 #include "base/strings/string_number_conversions.h"
6 #include "google_apis/gcm/engine/gservices_settings.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 namespace gcm {
11 namespace {
13 const int64 kAlternativeCheckinInterval = 16 * 60 * 60;
14 const char kAlternativeCheckinURL[] = "http://alternative.url/checkin";
15 const char kAlternativeMCSHostname[] = "alternative.gcm.host";
16 const int kAlternativeMCSSecurePort = 7777;
17 const char kAlternativeRegistrationURL[] =
18 "http://alternative.url/registration";
20 const int64 kDefaultCheckinInterval = 2 * 24 * 60 * 60; // seconds = 2 days.
21 const char kDefaultCheckinURL[] = "https://android.clients.google.com/checkin";
22 const char kDefaultRegistrationURL[] =
23 "https://android.clients.google.com/c2dm/register3";
24 const char kDefaultSettingsDigest[] =
25 "1-da39a3ee5e6b4b0d3255bfef95601890afd80709";
26 const char kAlternativeSettingsDigest[] =
27 "1-7da4aa4eb38a8bd3e330e3751cc0899924499134";
29 void AddSettingsToResponse(
30 checkin_proto::AndroidCheckinResponse& checkin_response,
31 const GServicesSettings::SettingsMap& settings,
32 bool settings_diff) {
33 for (GServicesSettings::SettingsMap::const_iterator iter = settings.begin();
34 iter != settings.end();
35 ++iter) {
36 checkin_proto::GservicesSetting* setting = checkin_response.add_setting();
37 setting->set_name(iter->first);
38 setting->set_value(iter->second);
40 checkin_response.set_settings_diff(settings_diff);
43 } // namespace
45 class GServicesSettingsTest : public testing::Test {
46 public:
47 GServicesSettingsTest();
48 ~GServicesSettingsTest() override;
50 void CheckAllSetToDefault();
52 GServicesSettings& settings() {
53 return gservices_settings_;
56 private:
57 GServicesSettings gservices_settings_;
60 GServicesSettingsTest::GServicesSettingsTest()
61 : gservices_settings_() {
64 GServicesSettingsTest::~GServicesSettingsTest() {}
66 void GServicesSettingsTest::CheckAllSetToDefault() {
67 EXPECT_EQ(base::TimeDelta::FromSeconds(kDefaultCheckinInterval),
68 settings().GetCheckinInterval());
69 EXPECT_EQ(GURL(kDefaultCheckinURL), settings().GetCheckinURL());
70 EXPECT_EQ(GURL("https://mtalk.google.com:5228"),
71 settings().GetMCSMainEndpoint());
72 EXPECT_EQ(GURL("https://mtalk.google.com:443"),
73 settings().GetMCSFallbackEndpoint());
74 EXPECT_EQ(GURL(kDefaultRegistrationURL), settings().GetRegistrationURL());
77 // Verifies default values of the G-services settings and settings digest.
78 TEST_F(GServicesSettingsTest, DefaultSettingsAndDigest) {
79 CheckAllSetToDefault();
80 EXPECT_EQ(kDefaultSettingsDigest, settings().digest());
81 EXPECT_EQ(kDefaultSettingsDigest,
82 GServicesSettings::CalculateDigest(settings().settings_map()));
85 // Verifies digest calculation for the sample provided by protocol owners.
86 TEST_F(GServicesSettingsTest, CalculateDigest) {
87 GServicesSettings::SettingsMap settings_map;
88 settings_map["android_id"] = "55XXXXXXXXXXXXXXXX0";
89 settings_map["checkin_interval"] = "86400";
90 settings_map["checkin_url"] =
91 "https://fake.address.google.com/canary/checkin";
92 settings_map["chrome_device"] = "1";
93 settings_map["device_country"] = "us";
94 settings_map["gcm_hostname"] = "fake.address.google.com";
95 settings_map["gcm_secure_port"] = "443";
97 EXPECT_EQ("1-33381ccd1cf5791dc0e6dfa234266fa9f1259197",
98 GServicesSettings::CalculateDigest(settings_map));
101 // Verifies that settings are not updated when load result is empty.
102 TEST_F(GServicesSettingsTest, UpdateFromEmptyLoadResult) {
103 GCMStore::LoadResult result;
104 result.gservices_digest = "";
105 settings().UpdateFromLoadResult(result);
107 CheckAllSetToDefault();
108 EXPECT_EQ(kDefaultSettingsDigest, settings().digest());
111 // Verifies that settings are not when digest value does not match.
112 TEST_F(GServicesSettingsTest, UpdateFromLoadResultWithSettingMissing) {
113 GCMStore::LoadResult result;
114 result.gservices_settings["checkin_internval"] = "100000";
115 result.gservices_digest = "digest_value";
116 settings().UpdateFromLoadResult(result);
118 CheckAllSetToDefault();
119 EXPECT_EQ(kDefaultSettingsDigest, settings().digest());
122 // Verifies that the settings are set correctly based on the load result.
123 TEST_F(GServicesSettingsTest, UpdateFromLoadResult) {
124 GCMStore::LoadResult result;
125 result.gservices_settings["checkin_interval"] =
126 base::Int64ToString(kAlternativeCheckinInterval);
127 result.gservices_settings["checkin_url"] = kAlternativeCheckinURL;
128 result.gservices_settings["gcm_hostname"] = kAlternativeMCSHostname;
129 result.gservices_settings["gcm_secure_port"] =
130 base::IntToString(kAlternativeMCSSecurePort);
131 result.gservices_settings["gcm_registration_url"] =
132 kAlternativeRegistrationURL;
133 result.gservices_digest = kAlternativeSettingsDigest;
134 settings().UpdateFromLoadResult(result);
136 EXPECT_EQ(base::TimeDelta::FromSeconds(kAlternativeCheckinInterval),
137 settings().GetCheckinInterval());
138 EXPECT_EQ(GURL(kAlternativeCheckinURL), settings().GetCheckinURL());
139 EXPECT_EQ(GURL("https://alternative.gcm.host:7777"),
140 settings().GetMCSMainEndpoint());
141 EXPECT_EQ(GURL("https://alternative.gcm.host:443"),
142 settings().GetMCSFallbackEndpoint());
143 EXPECT_EQ(GURL(kAlternativeRegistrationURL), settings().GetRegistrationURL());
144 EXPECT_EQ(GServicesSettings::CalculateDigest(result.gservices_settings),
145 settings().digest());
148 // Verifies that the checkin interval is updated to minimum if the original
149 // value is less than minimum.
150 TEST_F(GServicesSettingsTest, CheckinResponseMinimumCheckinInterval) {
151 // Setting the checkin interval to less than minimum.
152 checkin_proto::AndroidCheckinResponse checkin_response;
153 GServicesSettings::SettingsMap new_settings;
154 new_settings["checkin_interval"] = "3600";
155 AddSettingsToResponse(checkin_response, new_settings, false);
157 EXPECT_TRUE(settings().UpdateFromCheckinResponse(checkin_response));
159 EXPECT_EQ(GServicesSettings::MinimumCheckinInterval(),
160 settings().GetCheckinInterval());
161 EXPECT_EQ(GServicesSettings::CalculateDigest(new_settings),
162 settings().digest());
165 // Verifies that default checkin interval can be selectively overwritten.
166 TEST_F(GServicesSettingsTest, CheckinResponseUpdateCheckinInterval) {
167 checkin_proto::AndroidCheckinResponse checkin_response;
168 GServicesSettings::SettingsMap new_settings;
169 new_settings["checkin_interval"] = "86400";
170 AddSettingsToResponse(checkin_response, new_settings, false);
172 EXPECT_TRUE(settings().UpdateFromCheckinResponse(checkin_response));
174 // Only the checkin interval was updated:
175 EXPECT_EQ(base::TimeDelta::FromSeconds(86400),
176 settings().GetCheckinInterval());
178 // Other settings still set to default.
179 EXPECT_EQ(GURL("https://mtalk.google.com:5228"),
180 settings().GetMCSMainEndpoint());
181 EXPECT_EQ(GURL("https://mtalk.google.com:443"),
182 settings().GetMCSFallbackEndpoint());
183 EXPECT_EQ(GURL(kDefaultCheckinURL), settings().GetCheckinURL());
184 EXPECT_EQ(GURL(kDefaultRegistrationURL), settings().GetRegistrationURL());
186 EXPECT_EQ(GServicesSettings::CalculateDigest(new_settings),
187 settings().digest());
190 // Verifies that default registration URL can be selectively overwritten.
191 TEST_F(GServicesSettingsTest, CheckinResponseUpdateRegistrationURL) {
192 checkin_proto::AndroidCheckinResponse checkin_response;
193 GServicesSettings::SettingsMap new_settings;
194 new_settings["gcm_registration_url"] = "https://new.registration.url";
195 AddSettingsToResponse(checkin_response, new_settings, false);
197 EXPECT_TRUE(settings().UpdateFromCheckinResponse(checkin_response));
199 // Only the registration URL was updated:
200 EXPECT_EQ(GURL("https://new.registration.url"),
201 settings().GetRegistrationURL());
203 // Other settings still set to default.
204 EXPECT_EQ(base::TimeDelta::FromSeconds(kDefaultCheckinInterval),
205 settings().GetCheckinInterval());
206 EXPECT_EQ(GURL("https://mtalk.google.com:5228"),
207 settings().GetMCSMainEndpoint());
208 EXPECT_EQ(GURL("https://mtalk.google.com:443"),
209 settings().GetMCSFallbackEndpoint());
210 EXPECT_EQ(GURL(kDefaultCheckinURL), settings().GetCheckinURL());
212 EXPECT_EQ(GServicesSettings::CalculateDigest(new_settings),
213 settings().digest());
216 // Verifies that default checkin URL can be selectively overwritten.
217 TEST_F(GServicesSettingsTest, CheckinResponseUpdateCheckinURL) {
218 checkin_proto::AndroidCheckinResponse checkin_response;
219 GServicesSettings::SettingsMap new_settings;
220 new_settings["checkin_url"] = "https://new.checkin.url";
221 AddSettingsToResponse(checkin_response, new_settings, false);
223 EXPECT_TRUE(settings().UpdateFromCheckinResponse(checkin_response));
225 // Only the checkin URL was updated:
226 EXPECT_EQ(GURL("https://new.checkin.url"), settings().GetCheckinURL());
228 // Other settings still set to default.
229 EXPECT_EQ(base::TimeDelta::FromSeconds(kDefaultCheckinInterval),
230 settings().GetCheckinInterval());
231 EXPECT_EQ(GURL("https://mtalk.google.com:5228"),
232 settings().GetMCSMainEndpoint());
233 EXPECT_EQ(GURL("https://mtalk.google.com:443"),
234 settings().GetMCSFallbackEndpoint());
235 EXPECT_EQ(GURL(kDefaultRegistrationURL), settings().GetRegistrationURL());
237 EXPECT_EQ(GServicesSettings::CalculateDigest(new_settings),
238 settings().digest());
241 // Verifies that default MCS hostname can be selectively overwritten.
242 TEST_F(GServicesSettingsTest, CheckinResponseUpdateMCSHostname) {
243 checkin_proto::AndroidCheckinResponse checkin_response;
244 GServicesSettings::SettingsMap new_settings;
245 new_settings["gcm_hostname"] = "new.gcm.hostname";
246 AddSettingsToResponse(checkin_response, new_settings, false);
248 EXPECT_TRUE(settings().UpdateFromCheckinResponse(checkin_response));
250 // Only the MCS endpoints were updated:
251 EXPECT_EQ(GURL("https://new.gcm.hostname:5228"),
252 settings().GetMCSMainEndpoint());
253 EXPECT_EQ(GURL("https://new.gcm.hostname:443"),
254 settings().GetMCSFallbackEndpoint());
256 // Other settings still set to default.
257 EXPECT_EQ(base::TimeDelta::FromSeconds(kDefaultCheckinInterval),
258 settings().GetCheckinInterval());
259 EXPECT_EQ(GURL(kDefaultCheckinURL), settings().GetCheckinURL());
260 EXPECT_EQ(GURL(kDefaultRegistrationURL), settings().GetRegistrationURL());
262 EXPECT_EQ(GServicesSettings::CalculateDigest(new_settings),
263 settings().digest());
266 // Verifies that default MCS secure port can be selectively overwritten.
267 TEST_F(GServicesSettingsTest, CheckinResponseUpdateMCSSecurePort) {
268 checkin_proto::AndroidCheckinResponse checkin_response;
269 GServicesSettings::SettingsMap new_settings;
270 new_settings["gcm_secure_port"] = "5229";
271 AddSettingsToResponse(checkin_response, new_settings, false);
273 EXPECT_TRUE(settings().UpdateFromCheckinResponse(checkin_response));
275 // Only the main MCS endpoint was updated:
276 EXPECT_EQ(GURL("https://mtalk.google.com:5229"),
277 settings().GetMCSMainEndpoint());
279 // Other settings still set to default.
280 EXPECT_EQ(base::TimeDelta::FromSeconds(kDefaultCheckinInterval),
281 settings().GetCheckinInterval());
282 EXPECT_EQ(GURL(kDefaultCheckinURL), settings().GetCheckinURL());
283 EXPECT_EQ(GURL("https://mtalk.google.com:443"),
284 settings().GetMCSFallbackEndpoint());
285 EXPECT_EQ(GURL(kDefaultRegistrationURL), settings().GetRegistrationURL());
287 EXPECT_EQ(GServicesSettings::CalculateDigest(new_settings),
288 settings().digest());
291 // Update from checkin response should also do incremental update for both cases
292 // where some settings are removed or added.
293 TEST_F(GServicesSettingsTest, UpdateFromCheckinResponseSettingsDiff) {
294 checkin_proto::AndroidCheckinResponse checkin_response;
296 // Only the new settings will be included in the response with settings diff.
297 GServicesSettings::SettingsMap settings_diff;
298 settings_diff["new_setting_1"] = "new_setting_1_value";
299 settings_diff["new_setting_2"] = "new_setting_2_value";
300 settings_diff["gcm_secure_port"] = "5229";
302 // Full settings are necessary to calculate digest.
303 GServicesSettings::SettingsMap full_settings(settings_diff);
304 std::string digest = GServicesSettings::CalculateDigest(full_settings);
306 checkin_response.Clear();
307 AddSettingsToResponse(checkin_response, settings_diff, true);
308 EXPECT_TRUE(settings().UpdateFromCheckinResponse(checkin_response));
309 EXPECT_EQ(full_settings, settings().settings_map());
310 // Default setting overwritten by settings diff.
311 EXPECT_EQ(GURL("https://mtalk.google.com:5229"),
312 settings().GetMCSMainEndpoint());
314 // Setting up diff removing some of the values (including default setting).
315 settings_diff.clear();
316 settings_diff["delete_new_setting_1"] = "";
317 settings_diff["delete_gcm_secure_port"] = "";
318 settings_diff["new_setting_3"] = "new_setting_3_value";
320 // Updating full settings to calculate digest.
321 full_settings.erase(full_settings.find("new_setting_1"));
322 full_settings.erase(full_settings.find("gcm_secure_port"));
323 full_settings["new_setting_3"] = "new_setting_3_value";
324 digest = GServicesSettings::CalculateDigest(full_settings);
326 checkin_response.Clear();
327 AddSettingsToResponse(checkin_response, settings_diff, true);
328 EXPECT_TRUE(settings().UpdateFromCheckinResponse(checkin_response));
329 EXPECT_EQ(full_settings, settings().settings_map());
330 // Default setting back to norm.
331 EXPECT_EQ(GURL("https://mtalk.google.com:5228"),
332 settings().GetMCSMainEndpoint());
335 } // namespace gcm