Commit dcfe32a7 authored by fgorski@chromium.org's avatar fgorski@chromium.org

[GCM] fixing G-settings initialization from an empty store

Adding a test for the problem
Fixing the problem
Adding a minimum checkin interval

BUG=359254

Review URL: https://codereview.chromium.org/252933002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@267049 0039d316-1c4b-4281-b951-d872f2087c98
parent 62579169
...@@ -30,6 +30,8 @@ const char kDefaultRegistrationURL[] = ...@@ -30,6 +30,8 @@ const char kDefaultRegistrationURL[] =
namespace gcm { namespace gcm {
const int64 GServicesSettings::kMinimumCheckinInterval = 12 * 60 * 60;
GServicesSettings::GServicesSettings(GCMStore* gcm_store) GServicesSettings::GServicesSettings(GCMStore* gcm_store)
: gcm_store_(gcm_store), : gcm_store_(gcm_store),
checkin_interval_(kDefaultCheckinInterval), checkin_interval_(kDefaultCheckinInterval),
...@@ -77,61 +79,72 @@ void GServicesSettings::UpdateFromLoadResult( ...@@ -77,61 +79,72 @@ void GServicesSettings::UpdateFromLoadResult(
bool GServicesSettings::UpdateSettings( bool GServicesSettings::UpdateSettings(
const std::map<std::string, std::string>& settings) { const std::map<std::string, std::string>& settings) {
int64 new_checkin_interval = 0LL; int64 new_checkin_interval = kMinimumCheckinInterval;
std::map<std::string, std::string>::const_iterator iter = std::map<std::string, std::string>::const_iterator iter =
settings.find(kCheckinIntervalKey); settings.find(kCheckinIntervalKey);
if (iter != settings.end()) { if (iter == settings.end()) {
if (!base::StringToInt64(iter->second, &new_checkin_interval)) { LOG(ERROR) << "Setting not found: " << kCheckinIntervalKey;
LOG(ERROR) << "Failed to parse checkin interval: " << iter->second; return false;
return false; }
} if (!base::StringToInt64(iter->second, &new_checkin_interval)) {
if (new_checkin_interval <= 0LL) { LOG(ERROR) << "Failed to parse checkin interval: " << iter->second;
LOG(ERROR) << "Checkin interval not positive: " << new_checkin_interval; return false;
return false; }
} if (new_checkin_interval < kMinimumCheckinInterval) {
LOG(ERROR) << "Checkin interval: " << new_checkin_interval
<< " is less than allowed minimum: " << kMinimumCheckinInterval;
new_checkin_interval = kMinimumCheckinInterval;
} }
std::string new_mcs_hostname; std::string new_mcs_hostname;
int new_mcs_secure_port = -1;
iter = settings.find(kMCSHostnameKey); iter = settings.find(kMCSHostnameKey);
if (iter != settings.end()) { if (iter == settings.end()) {
new_mcs_hostname = iter->second; LOG(ERROR) << "Setting not found: " << kMCSHostnameKey;
if (new_mcs_hostname.empty()) { return false;
LOG(ERROR) << "Empty MCS hostname provided."; }
return false; new_mcs_hostname = iter->second;
} if (new_mcs_hostname.empty()) {
LOG(ERROR) << "Empty MCS hostname provided.";
iter = settings.find(kMCSSecurePortKey); return false;
if (iter != settings.end()) { }
if (!base::StringToInt(iter->second, &new_mcs_secure_port)) {
LOG(ERROR) << "Failed to parse MCS secure port: " << iter->second; int new_mcs_secure_port = -1;
return false; iter = settings.find(kMCSSecurePortKey);
} if (iter == settings.end()) {
if (new_mcs_secure_port < 0 || 65535 < new_mcs_secure_port) { LOG(ERROR) << "Setting not found: " << kMCSSecurePortKey;
LOG(ERROR) << "Incorrect port value: " << new_mcs_secure_port; return false;
return false; }
} if (!base::StringToInt(iter->second, &new_mcs_secure_port)) {
} LOG(ERROR) << "Failed to parse MCS secure port: " << iter->second;
return false;
}
if (new_mcs_secure_port < 0 || 65535 < new_mcs_secure_port) {
LOG(ERROR) << "Incorrect port value: " << new_mcs_secure_port;
return false;
} }
std::string new_checkin_url; std::string new_checkin_url;
iter = settings.find(kCheckinURLKey); iter = settings.find(kCheckinURLKey);
if (iter != settings.end()) { if (iter == settings.end()) {
new_checkin_url = iter->second; LOG(ERROR) << "Setting not found: " << kCheckinURLKey;
if (new_checkin_url.empty()) { return false;
LOG(ERROR) << "Empty checkin URL provided."; }
return false; new_checkin_url = iter->second;
} if (new_checkin_url.empty()) {
LOG(ERROR) << "Empty checkin URL provided.";
return false;
} }
std::string new_registration_url; std::string new_registration_url;
iter = settings.find(kRegistrationURLKey); iter = settings.find(kRegistrationURLKey);
if (iter != settings.end()) { if (iter == settings.end()) {
new_registration_url = iter->second; LOG(ERROR) << "Setting not found: " << kRegistrationURLKey;
if (new_registration_url.empty()) { return false;
LOG(ERROR) << "Empty registration URL provided."; }
return false; new_registration_url = iter->second;
} if (new_registration_url.empty()) {
LOG(ERROR) << "Empty registration URL provided.";
return false;
} }
// We only update the settings once all of them are correct. // We only update the settings once all of them are correct.
......
...@@ -19,6 +19,9 @@ namespace gcm { ...@@ -19,6 +19,9 @@ namespace gcm {
// extracting them from checkin response and storing in GCMStore. // extracting them from checkin response and storing in GCMStore.
class GCM_EXPORT GServicesSettings { class GCM_EXPORT GServicesSettings {
public: public:
// Minimum periodic checkin interval in seconds.
static const int64 kMinimumCheckinInterval;
// Create an instance of GServicesSettings class. |gcm_store| is used to store // Create an instance of GServicesSettings class. |gcm_store| is used to store
// the settings after they are extracted from checkin response. // the settings after they are extracted from checkin response.
explicit GServicesSettings(GCMStore* gcm_store); explicit GServicesSettings(GCMStore* gcm_store);
......
...@@ -12,7 +12,7 @@ namespace gcm { ...@@ -12,7 +12,7 @@ namespace gcm {
namespace { namespace {
const int64 kAlternativeCheckinInterval = 2000LL; const int64 kAlternativeCheckinInterval = 16 * 60 * 60;
const char kAlternativeCheckinURL[] = "http://alternative.url/checkin"; const char kAlternativeCheckinURL[] = "http://alternative.url/checkin";
const char kAlternativeMCSHostname[] = "http://alternative.gcm.host"; const char kAlternativeMCSHostname[] = "http://alternative.gcm.host";
const int kAlternativeMCSSecurePort = 443; const int kAlternativeMCSSecurePort = 443;
...@@ -121,10 +121,11 @@ class GServicesSettingsTest : public testing::Test { ...@@ -121,10 +121,11 @@ class GServicesSettingsTest : public testing::Test {
FakeGCMStore& gcm_store() { return gcm_store_; } FakeGCMStore& gcm_store() { return gcm_store_; }
std::map<std::string, std::string> alternative_settings_;
private: private:
FakeGCMStore gcm_store_; FakeGCMStore gcm_store_;
GServicesSettings gserivces_settings_; GServicesSettings gserivces_settings_;
std::map<std::string, std::string> alternative_settings_;
}; };
GServicesSettingsTest::GServicesSettingsTest() GServicesSettingsTest::GServicesSettingsTest()
...@@ -176,6 +177,28 @@ TEST_F(GServicesSettingsTest, DefaultSettingsAndDigest) { ...@@ -176,6 +177,28 @@ TEST_F(GServicesSettingsTest, DefaultSettingsAndDigest) {
EXPECT_EQ(std::string(), settings().digest()); EXPECT_EQ(std::string(), settings().digest());
} }
// Verifies that settings are not updated when load result is empty.
TEST_F(GServicesSettingsTest, UpdateFromEmptyLoadResult) {
GCMStore::LoadResult result;
result.gservices_digest = "digest_value";
settings().UpdateFromLoadResult(result);
CheckAllSetToDefault();
EXPECT_EQ(std::string(), settings().digest());
}
// Verifies that settings are not updated when one of them is missing.
TEST_F(GServicesSettingsTest, UpdateFromLoadResultWithSettingMissing) {
GCMStore::LoadResult result;
result.gservices_settings = alternative_settings();
result.gservices_digest = "digest_value";
result.gservices_settings.erase("gcm_hostname");
settings().UpdateFromLoadResult(result);
CheckAllSetToDefault();
EXPECT_EQ(std::string(), settings().digest());
}
// Verifies that the settings are set correctly based on the load result. // Verifies that the settings are set correctly based on the load result.
TEST_F(GServicesSettingsTest, UpdateFromLoadResult) { TEST_F(GServicesSettingsTest, UpdateFromLoadResult) {
GCMStore::LoadResult result; GCMStore::LoadResult result;
...@@ -202,6 +225,39 @@ TEST_F(GServicesSettingsTest, UpdateFromCheckinResponse) { ...@@ -202,6 +225,39 @@ TEST_F(GServicesSettingsTest, UpdateFromCheckinResponse) {
EXPECT_EQ("digest_value", settings().digest()); EXPECT_EQ("digest_value", settings().digest());
} }
// Verifies that the checkin interval is updated to minimum if the original
// value is less than minimum.
TEST_F(GServicesSettingsTest, UpdateFromCheckinResponseMinimumCheckinInterval) {
checkin_proto::AndroidCheckinResponse checkin_response;
checkin_response.set_digest("digest_value");
// Setting the checkin interval to less than minimum.
alternative_settings_["checkin_interval"] = base::IntToString(3600);
SetWithAlternativeSettings(checkin_response);
settings().UpdateFromCheckinResponse(checkin_response);
EXPECT_TRUE(gcm_store().settings_saved());
EXPECT_EQ(GServicesSettings::kMinimumCheckinInterval,
settings().checkin_interval());
EXPECT_EQ("digest_value", settings().digest());
}
// Verifies that settings are not updated when one of them is missing.
TEST_F(GServicesSettingsTest, UpdateFromCheckinResponseWithSettingMissing) {
checkin_proto::AndroidCheckinResponse checkin_response;
checkin_response.set_digest("digest_value");
alternative_settings_.erase("gcm_hostname");
SetWithAlternativeSettings(checkin_response);
settings().UpdateFromCheckinResponse(checkin_response);
EXPECT_FALSE(gcm_store().settings_saved());
CheckAllSetToDefault();
EXPECT_EQ(std::string(), settings().digest());
}
// Verifies that no update is done, when a checkin response misses digest. // Verifies that no update is done, when a checkin response misses digest.
TEST_F(GServicesSettingsTest, UpdateFromCheckinResponseNoDigest) { TEST_F(GServicesSettingsTest, UpdateFromCheckinResponseNoDigest) {
checkin_proto::AndroidCheckinResponse checkin_response; checkin_proto::AndroidCheckinResponse checkin_response;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment