Commit 03ae4059 authored by jwd@chromium.org's avatar jwd@chromium.org

Added tests for storing and loading seed data to and from prefs.


BUG=136396
TEST=New VariationsServiceTest.StoreSeed and VariationsServiceTest.LoadSeed tests


Review URL: https://chromiumcodereview.appspot.com/10764005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@145980 0039d316-1c4b-4281-b951-d872f2087c98
parent 75bfcc7b
......@@ -190,7 +190,7 @@ void VariationsService::RegisterPrefs(PrefService* prefs) {
base::Time().ToInternalValue());
}
void VariationsService::StoreSeedData(const std::string& seed_data,
bool VariationsService::StoreSeedData(const std::string& seed_data,
const base::Time& seed_date,
PrefService* local_prefs) {
// Only store the seed data if it parses correctly.
......@@ -198,19 +198,20 @@ void VariationsService::StoreSeedData(const std::string& seed_data,
if (!seed.ParseFromString(seed_data)) {
VLOG(1) << "Variations Seed data from server is not in valid proto format, "
<< "rejecting the seed.";
return;
return false;
}
std::string base64_seed_data;
if (!base::Base64Encode(seed_data, &base64_seed_data)) {
VLOG(1) << "Variations Seed data from server fails Base64Encode, rejecting "
<< "the seed.";
return;
return false;
}
local_prefs->SetString(prefs::kVariationsSeed, base64_seed_data);
local_prefs->SetInt64(prefs::kVariationsSeedDate,
seed_date.ToInternalValue());
return true;
}
// static
......
......@@ -58,13 +58,15 @@ class VariationsService : public net::URLFetcherDelegate {
FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyVersionWildcards);
FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyStartDate);
FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, IsStudyExpired);
FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, LoadSeed);
FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, StoreSeed);
FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, ValidateStudy);
// Store the given seed data to the given local prefs. Note that |seed_data|
// is assumed to be the raw serialized protobuf data stored in a string. It
// will be Base64Encoded for storage. If the string is invalid or the encoding
// fails, the |local_prefs| is left as is.
void StoreSeedData(const std::string& seed_data,
// fails, the |local_prefs| is left as is and the function returns false.
bool StoreSeedData(const std::string& seed_data,
const base::Time& seed_date,
PrefService* local_prefs);
......
......@@ -2,10 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/base64.h"
#include "base/string_split.h"
#include "chrome/browser/metrics/proto/study.pb.h"
#include "chrome/browser/metrics/variations_service.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_pref_service.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chrome_variations {
......@@ -17,6 +20,21 @@ int64 TimeToProtoTime(const base::Time& time) {
return (time - base::Time::UnixEpoch()).InSeconds();
}
// Populates |seed| with simple test data. The resulting seed will contain one
// study called "test", which contains one experiment called "abc" with
// probability weight 100. |seed|'s study field will be cleared before adding
// the new study.
chrome_variations::TrialsSeed CreateTestSeed() {
chrome_variations::TrialsSeed seed;
chrome_variations::Study* study = seed.add_study();
study->set_name("test");
study->set_default_experiment_name("abc");
chrome_variations::Study_Experiment* experiment = study->add_experiment();
experiment->set_name("abc");
experiment->set_probability_weight(100);
return seed;
}
} // namespace
TEST(VariationsServiceTest, CheckStudyChannel) {
......@@ -290,6 +308,79 @@ TEST(VariationsServiceTest, IsStudyExpired) {
}
}
TEST(VariationsServiceTest, LoadSeed) {
TestingPrefService pref_service;
VariationsService::RegisterPrefs(&pref_service);
// Store good seed data to test if loading from prefs works.
chrome_variations::TrialsSeed seed = CreateTestSeed();
std::string serialized_seed;
seed.SerializeToString(&serialized_seed);
std::string base64_serialized_seed;
ASSERT_TRUE(base::Base64Encode(serialized_seed, &base64_serialized_seed));
pref_service.SetString(prefs::kVariationsSeed, base64_serialized_seed);
VariationsService variations_service;
chrome_variations::TrialsSeed loaded_seed;
EXPECT_TRUE(
variations_service.LoadTrialsSeedFromPref(&pref_service, &loaded_seed));
std::string serialized_loaded_seed;
loaded_seed.SerializeToString(&serialized_loaded_seed);
// Check that the loaded data is the same as the original.
EXPECT_EQ(serialized_seed, serialized_loaded_seed);
// Make sure the pref hasn't been changed.
EXPECT_FALSE(
pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue());
EXPECT_EQ(base64_serialized_seed,
pref_service.GetString(prefs::kVariationsSeed));
// Check that loading a bad seed returns false and clears the pref.
pref_service.ClearPref(prefs::kVariationsSeed);
pref_service.SetString(prefs::kVariationsSeed, "this should fail");
EXPECT_FALSE(
pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue());
EXPECT_FALSE(
variations_service.LoadTrialsSeedFromPref(&pref_service, &loaded_seed));
EXPECT_TRUE(
pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue());
}
TEST(VariationsServiceTest, StoreSeed) {
TestingPrefService pref_service;
VariationsService::RegisterPrefs(&pref_service);
const base::Time now = base::Time::Now();
chrome_variations::TrialsSeed seed = CreateTestSeed();
VariationsService variations_service;
std::string serialized_seed;
seed.SerializeToString(&serialized_seed);
EXPECT_TRUE(
variations_service.StoreSeedData(serialized_seed, now, &pref_service));
// Make sure the pref was actually set.
EXPECT_FALSE(
pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue());
std::string loaded_serialized_seed =
pref_service.GetString(prefs::kVariationsSeed);
std::string decoded_serialized_seed;
ASSERT_TRUE(base::Base64Decode(loaded_serialized_seed,
&decoded_serialized_seed));
// Make sure the stored seed from pref is the same as the seed we created.
EXPECT_EQ(serialized_seed, decoded_serialized_seed);
// Check if trying to store a bad seed leaves the pref unchanged.
pref_service.ClearPref(prefs::kVariationsSeed);
EXPECT_FALSE(
variations_service.StoreSeedData("this should fail", now, &pref_service));
EXPECT_TRUE(
pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue());
}
TEST(VariationsServiceTest, ValidateStudy) {
Study study;
study.set_default_experiment_name("def");
......
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