| 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 "components/variations/variations_seed_store.h" | 5 #include "components/variations/variations_seed_store.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/prefs/testing_pref_service.h" | 8 #include "base/prefs/testing_pref_service.h" |
| 9 #include "components/compression/compression_utils.h" | 9 #include "components/compression/compression_utils.h" |
| 10 #include "components/variations/pref_names.h" | 10 #include "components/variations/pref_names.h" |
| 11 #include "components/variations/proto/study.pb.h" | 11 #include "components/variations/proto/study.pb.h" |
| 12 #include "components/variations/proto/variations_seed.pb.h" | 12 #include "components/variations/proto/variations_seed.pb.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 namespace variations { | 15 namespace variations { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 class TestVariationsSeedStore : public VariationsSeedStore { | 19 class TestVariationsSeedStore : public VariationsSeedStore { |
| 20 public: | 20 public: |
| 21 explicit TestVariationsSeedStore(PrefService* local_state) | 21 explicit TestVariationsSeedStore(PrefService* local_state) |
| 22 : VariationsSeedStore(local_state) {} | 22 : VariationsSeedStore(local_state) {} |
| 23 ~TestVariationsSeedStore() override {} | 23 ~TestVariationsSeedStore() override {} |
| 24 | 24 |
| 25 bool StoreSeedForTesting(const std::string& seed_data) { | 25 bool StoreSeedForTesting(const std::string& seed_data) { |
| 26 return StoreSeedData(seed_data, std::string(), std::string(), | 26 return StoreSeedData(seed_data, std::string(), std::string(), |
| 27 base::Time::Now(), false, nullptr); | 27 base::Time::Now(), false, false, nullptr); |
| 28 } | 28 } |
| 29 | 29 |
| 30 VariationsSeedStore::VerifySignatureResult VerifySeedSignature( | 30 VariationsSeedStore::VerifySignatureResult VerifySeedSignature( |
| 31 const std::string& seed_bytes, | 31 const std::string& seed_bytes, |
| 32 const std::string& base64_seed_signature) override { | 32 const std::string& base64_seed_signature) override { |
| 33 return VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_ENUM_SIZE; | 33 return VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_ENUM_SIZE; |
| 34 } | 34 } |
| 35 | 35 |
| 36 private: | 36 private: |
| 37 DISALLOW_COPY_AND_ASSIGN(TestVariationsSeedStore); | 37 DISALLOW_COPY_AND_ASSIGN(TestVariationsSeedStore); |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 const variations::VariationsSeed seed = CreateTestSeed(); | 204 const variations::VariationsSeed seed = CreateTestSeed(); |
| 205 const std::string serialized_seed = SerializeSeed(seed); | 205 const std::string serialized_seed = SerializeSeed(seed); |
| 206 | 206 |
| 207 TestingPrefServiceSimple prefs; | 207 TestingPrefServiceSimple prefs; |
| 208 VariationsSeedStore::RegisterPrefs(prefs.registry()); | 208 VariationsSeedStore::RegisterPrefs(prefs.registry()); |
| 209 TestVariationsSeedStore seed_store(&prefs); | 209 TestVariationsSeedStore seed_store(&prefs); |
| 210 | 210 |
| 211 variations::VariationsSeed parsed_seed; | 211 variations::VariationsSeed parsed_seed; |
| 212 EXPECT_TRUE(seed_store.StoreSeedData(serialized_seed, std::string(), | 212 EXPECT_TRUE(seed_store.StoreSeedData(serialized_seed, std::string(), |
| 213 std::string(), base::Time::Now(), false, | 213 std::string(), base::Time::Now(), false, |
| 214 &parsed_seed)); | 214 false, &parsed_seed)); |
| 215 EXPECT_EQ(serialized_seed, SerializeSeed(parsed_seed)); | 215 EXPECT_EQ(serialized_seed, SerializeSeed(parsed_seed)); |
| 216 } | 216 } |
| 217 | 217 |
| 218 TEST(VariationsSeedStoreTest, StoreSeedData_CountryCode) { | 218 TEST(VariationsSeedStoreTest, StoreSeedData_CountryCode) { |
| 219 TestingPrefServiceSimple prefs; | 219 TestingPrefServiceSimple prefs; |
| 220 VariationsSeedStore::RegisterPrefs(prefs.registry()); | 220 VariationsSeedStore::RegisterPrefs(prefs.registry()); |
| 221 TestVariationsSeedStore seed_store(&prefs); | 221 TestVariationsSeedStore seed_store(&prefs); |
| 222 | 222 |
| 223 // Test with a seed country code and no header value. | 223 // Test with a seed country code and no header value. |
| 224 variations::VariationsSeed seed = CreateTestSeed(); | 224 variations::VariationsSeed seed = CreateTestSeed(); |
| 225 seed.set_country_code("test_country"); | 225 seed.set_country_code("test_country"); |
| 226 EXPECT_TRUE(seed_store.StoreSeedData(SerializeSeed(seed), std::string(), | 226 EXPECT_TRUE(seed_store.StoreSeedData(SerializeSeed(seed), std::string(), |
| 227 std::string(), base::Time::Now(), false, | 227 std::string(), base::Time::Now(), false, |
| 228 nullptr)); | 228 false, nullptr)); |
| 229 EXPECT_EQ("test_country", prefs.GetString(prefs::kVariationsCountry)); | 229 EXPECT_EQ("test_country", prefs.GetString(prefs::kVariationsCountry)); |
| 230 | 230 |
| 231 // Test with a header value and no seed country. | 231 // Test with a header value and no seed country. |
| 232 prefs.ClearPref(prefs::kVariationsCountry); | 232 prefs.ClearPref(prefs::kVariationsCountry); |
| 233 seed.clear_country_code(); | 233 seed.clear_country_code(); |
| 234 EXPECT_TRUE(seed_store.StoreSeedData(SerializeSeed(seed), std::string(), | 234 EXPECT_TRUE(seed_store.StoreSeedData(SerializeSeed(seed), std::string(), |
| 235 "test_country2", base::Time::Now(), | 235 "test_country2", base::Time::Now(), |
| 236 false, nullptr)); | 236 false, false, nullptr)); |
| 237 EXPECT_EQ("test_country2", prefs.GetString(prefs::kVariationsCountry)); | 237 EXPECT_EQ("test_country2", prefs.GetString(prefs::kVariationsCountry)); |
| 238 | 238 |
| 239 // Test with a seed country code and header value. | 239 // Test with a seed country code and header value. |
| 240 prefs.ClearPref(prefs::kVariationsCountry); | 240 prefs.ClearPref(prefs::kVariationsCountry); |
| 241 seed.set_country_code("test_country3"); | 241 seed.set_country_code("test_country3"); |
| 242 EXPECT_TRUE(seed_store.StoreSeedData(SerializeSeed(seed), std::string(), | 242 EXPECT_TRUE(seed_store.StoreSeedData(SerializeSeed(seed), std::string(), |
| 243 "test_country4", base::Time::Now(), | 243 "test_country4", base::Time::Now(), |
| 244 false, nullptr)); | 244 false, false, nullptr)); |
| 245 EXPECT_EQ("test_country4", prefs.GetString(prefs::kVariationsCountry)); | 245 EXPECT_EQ("test_country4", prefs.GetString(prefs::kVariationsCountry)); |
| 246 | 246 |
| 247 // Test with no country code specified - which should preserve the old value. | 247 // Test with no country code specified - which should preserve the old value. |
| 248 seed.clear_country_code(); | 248 seed.clear_country_code(); |
| 249 EXPECT_TRUE(seed_store.StoreSeedData(SerializeSeed(seed), std::string(), | 249 EXPECT_TRUE(seed_store.StoreSeedData(SerializeSeed(seed), std::string(), |
| 250 std::string(), base::Time::Now(), false, | 250 std::string(), base::Time::Now(), false, |
| 251 nullptr)); | 251 false, nullptr)); |
| 252 EXPECT_EQ("test_country4", prefs.GetString(prefs::kVariationsCountry)); | 252 EXPECT_EQ("test_country4", prefs.GetString(prefs::kVariationsCountry)); |
| 253 } | 253 } |
| 254 | 254 |
| 255 TEST(VariationsSeedStoreTest, StoreSeedData_GzippedSeed) { |
| 256 const variations::VariationsSeed seed = CreateTestSeed(); |
| 257 const std::string serialized_seed = SerializeSeed(seed); |
| 258 std::string compressed_seed; |
| 259 ASSERT_TRUE(compression::GzipCompress(serialized_seed, &compressed_seed)); |
| 260 |
| 261 TestingPrefServiceSimple prefs; |
| 262 VariationsSeedStore::RegisterPrefs(prefs.registry()); |
| 263 TestVariationsSeedStore seed_store(&prefs); |
| 264 |
| 265 variations::VariationsSeed parsed_seed; |
| 266 EXPECT_TRUE(seed_store.StoreSeedData(compressed_seed, std::string(), |
| 267 std::string(), base::Time::Now(), false, |
| 268 true, &parsed_seed)); |
| 269 EXPECT_EQ(serialized_seed, SerializeSeed(parsed_seed)); |
| 270 } |
| 271 |
| 255 TEST(VariationsSeedStoreTest, VerifySeedSignature) { | 272 TEST(VariationsSeedStoreTest, VerifySeedSignature) { |
| 256 // The below seed and signature pair were generated using the server's | 273 // The below seed and signature pair were generated using the server's |
| 257 // private key. | 274 // private key. |
| 258 const std::string base64_seed_data = | 275 const std::string base64_seed_data = |
| 259 "CigxZDI5NDY0ZmIzZDc4ZmYxNTU2ZTViNTUxYzY0NDdjYmM3NGU1ZmQwEr0BCh9VTUEtVW5p" | 276 "CigxZDI5NDY0ZmIzZDc4ZmYxNTU2ZTViNTUxYzY0NDdjYmM3NGU1ZmQwEr0BCh9VTUEtVW5p" |
| 260 "Zm9ybWl0eS1UcmlhbC0xMC1QZXJjZW50GICckqUFOAFCB2RlZmF1bHRKCwoHZGVmYXVsdBAB" | 277 "Zm9ybWl0eS1UcmlhbC0xMC1QZXJjZW50GICckqUFOAFCB2RlZmF1bHRKCwoHZGVmYXVsdBAB" |
| 261 "SgwKCGdyb3VwXzAxEAFKDAoIZ3JvdXBfMDIQAUoMCghncm91cF8wMxABSgwKCGdyb3VwXzA0" | 278 "SgwKCGdyb3VwXzAxEAFKDAoIZ3JvdXBfMDIQAUoMCghncm91cF8wMxABSgwKCGdyb3VwXzA0" |
| 262 "EAFKDAoIZ3JvdXBfMDUQAUoMCghncm91cF8wNhABSgwKCGdyb3VwXzA3EAFKDAoIZ3JvdXBf" | 279 "EAFKDAoIZ3JvdXBfMDUQAUoMCghncm91cF8wNhABSgwKCGdyb3VwXzA3EAFKDAoIZ3JvdXBf" |
| 263 "MDgQAUoMCghncm91cF8wORAB"; | 280 "MDgQAUoMCghncm91cF8wORAB"; |
| 264 const std::string base64_seed_signature = | 281 const std::string base64_seed_signature = |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 EXPECT_TRUE(base::Base64Decode(base64_after_seed_data, &after_seed_data)); | 357 EXPECT_TRUE(base::Base64Decode(base64_after_seed_data, &after_seed_data)); |
| 341 EXPECT_TRUE(base::Base64Decode(base64_delta_data, &delta_data)); | 358 EXPECT_TRUE(base::Base64Decode(base64_delta_data, &delta_data)); |
| 342 | 359 |
| 343 std::string output; | 360 std::string output; |
| 344 EXPECT_TRUE(VariationsSeedStore::ApplyDeltaPatch(before_seed_data, delta_data, | 361 EXPECT_TRUE(VariationsSeedStore::ApplyDeltaPatch(before_seed_data, delta_data, |
| 345 &output)); | 362 &output)); |
| 346 EXPECT_EQ(after_seed_data, output); | 363 EXPECT_EQ(after_seed_data, output); |
| 347 } | 364 } |
| 348 | 365 |
| 349 } // namespace variations | 366 } // namespace variations |
| OLD | NEW |