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 "chrome/browser/metrics/variations/variations_seed_store.h" | 5 #include "chrome/browser/metrics/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 "chrome/common/pref_names.h" | 9 #include "chrome/common/pref_names.h" |
10 #include "components/metrics/compression_utils.h" | 10 #include "components/metrics/compression_utils.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 chrome_variations { | 15 namespace chrome_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(), base::Time::Now(), NULL); | 26 return StoreSeedData(seed_data, std::string(), std::string(), |
| 27 base::Time::Now(), false, nullptr); |
27 } | 28 } |
28 | 29 |
29 VariationsSeedStore::VerifySignatureResult VerifySeedSignature( | 30 VariationsSeedStore::VerifySignatureResult VerifySeedSignature( |
30 const std::string& seed_bytes, | 31 const std::string& seed_bytes, |
31 const std::string& base64_seed_signature) override { | 32 const std::string& base64_seed_signature) override { |
32 return VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_ENUM_SIZE; | 33 return VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_ENUM_SIZE; |
33 } | 34 } |
34 | 35 |
35 private: | 36 private: |
36 DISALLOW_COPY_AND_ASSIGN(TestVariationsSeedStore); | 37 DISALLOW_COPY_AND_ASSIGN(TestVariationsSeedStore); |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 TEST(VariationsSeedStoreTest, StoreSeedData_ParsedSeed) { | 203 TEST(VariationsSeedStoreTest, StoreSeedData_ParsedSeed) { |
203 const variations::VariationsSeed seed = CreateTestSeed(); | 204 const variations::VariationsSeed seed = CreateTestSeed(); |
204 const std::string serialized_seed = SerializeSeed(seed); | 205 const std::string serialized_seed = SerializeSeed(seed); |
205 | 206 |
206 TestingPrefServiceSimple prefs; | 207 TestingPrefServiceSimple prefs; |
207 VariationsSeedStore::RegisterPrefs(prefs.registry()); | 208 VariationsSeedStore::RegisterPrefs(prefs.registry()); |
208 TestVariationsSeedStore seed_store(&prefs); | 209 TestVariationsSeedStore seed_store(&prefs); |
209 | 210 |
210 variations::VariationsSeed parsed_seed; | 211 variations::VariationsSeed parsed_seed; |
211 EXPECT_TRUE(seed_store.StoreSeedData(serialized_seed, std::string(), | 212 EXPECT_TRUE(seed_store.StoreSeedData(serialized_seed, std::string(), |
212 base::Time::Now(), &parsed_seed)); | 213 std::string(), base::Time::Now(), false, |
| 214 &parsed_seed)); |
213 EXPECT_EQ(serialized_seed, SerializeSeed(parsed_seed)); | 215 EXPECT_EQ(serialized_seed, SerializeSeed(parsed_seed)); |
214 } | 216 } |
215 | 217 |
216 TEST(VariationsSeedStoreTest, StoreSeedData_CountryCode) { | 218 TEST(VariationsSeedStoreTest, StoreSeedData_CountryCode) { |
217 TestingPrefServiceSimple prefs; | 219 TestingPrefServiceSimple prefs; |
218 VariationsSeedStore::RegisterPrefs(prefs.registry()); | 220 VariationsSeedStore::RegisterPrefs(prefs.registry()); |
219 TestVariationsSeedStore seed_store(&prefs); | 221 TestVariationsSeedStore seed_store(&prefs); |
220 | 222 |
| 223 // Test with a seed country code and no header value. |
221 variations::VariationsSeed seed = CreateTestSeed(); | 224 variations::VariationsSeed seed = CreateTestSeed(); |
222 seed.set_country_code("test_country"); | 225 seed.set_country_code("test_country"); |
223 EXPECT_TRUE(seed_store.StoreSeedData(SerializeSeed(seed), std::string(), | 226 EXPECT_TRUE(seed_store.StoreSeedData(SerializeSeed(seed), std::string(), |
224 base::Time::Now(), nullptr)); | 227 std::string(), base::Time::Now(), false, |
| 228 nullptr)); |
225 EXPECT_EQ("test_country", prefs.GetString(prefs::kVariationsCountry)); | 229 EXPECT_EQ("test_country", prefs.GetString(prefs::kVariationsCountry)); |
| 230 |
| 231 // Test with a header value and no seed country. |
| 232 prefs.ClearPref(prefs::kVariationsCountry); |
| 233 seed.clear_country_code(); |
| 234 EXPECT_TRUE(seed_store.StoreSeedData(SerializeSeed(seed), std::string(), |
| 235 "test_country2", base::Time::Now(), |
| 236 false, nullptr)); |
| 237 EXPECT_EQ("test_country2", prefs.GetString(prefs::kVariationsCountry)); |
| 238 |
| 239 // Test with a seed country code and header value. |
| 240 prefs.ClearPref(prefs::kVariationsCountry); |
| 241 seed.set_country_code("test_country3"); |
| 242 EXPECT_TRUE(seed_store.StoreSeedData(SerializeSeed(seed), std::string(), |
| 243 "test_country4", base::Time::Now(), |
| 244 false, nullptr)); |
| 245 EXPECT_EQ("test_country4", prefs.GetString(prefs::kVariationsCountry)); |
| 246 |
| 247 // Test with no country code specified - which should preserve the old value. |
| 248 seed.clear_country_code(); |
| 249 EXPECT_TRUE(seed_store.StoreSeedData(SerializeSeed(seed), std::string(), |
| 250 std::string(), base::Time::Now(), false, |
| 251 nullptr)); |
| 252 EXPECT_EQ("test_country4", prefs.GetString(prefs::kVariationsCountry)); |
226 } | 253 } |
227 | 254 |
228 TEST(VariationsSeedStoreTest, VerifySeedSignature) { | 255 TEST(VariationsSeedStoreTest, VerifySeedSignature) { |
229 // The below seed and signature pair were generated using the server's | 256 // The below seed and signature pair were generated using the server's |
230 // private key. | 257 // private key. |
231 const std::string base64_seed_data = | 258 const std::string base64_seed_data = |
232 "CigxZDI5NDY0ZmIzZDc4ZmYxNTU2ZTViNTUxYzY0NDdjYmM3NGU1ZmQwEr0BCh9VTUEtVW5p" | 259 "CigxZDI5NDY0ZmIzZDc4ZmYxNTU2ZTViNTUxYzY0NDdjYmM3NGU1ZmQwEr0BCh9VTUEtVW5p" |
233 "Zm9ybWl0eS1UcmlhbC0xMC1QZXJjZW50GICckqUFOAFCB2RlZmF1bHRKCwoHZGVmYXVsdBAB" | 260 "Zm9ybWl0eS1UcmlhbC0xMC1QZXJjZW50GICckqUFOAFCB2RlZmF1bHRKCwoHZGVmYXVsdBAB" |
234 "SgwKCGdyb3VwXzAxEAFKDAoIZ3JvdXBfMDIQAUoMCghncm91cF8wMxABSgwKCGdyb3VwXzA0" | 261 "SgwKCGdyb3VwXzAxEAFKDAoIZ3JvdXBfMDIQAUoMCghncm91cF8wMxABSgwKCGdyb3VwXzA0" |
235 "EAFKDAoIZ3JvdXBfMDUQAUoMCghncm91cF8wNhABSgwKCGdyb3VwXzA3EAFKDAoIZ3JvdXBf" | 262 "EAFKDAoIZ3JvdXBfMDUQAUoMCghncm91cF8wNhABSgwKCGdyb3VwXzA3EAFKDAoIZ3JvdXBf" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 EXPECT_EQ(VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_INVALID_SIGNATURE, | 300 EXPECT_EQ(VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_INVALID_SIGNATURE, |
274 seed_store.VerifySeedSignature(seed_data, base64_seed_data)); | 301 seed_store.VerifySeedSignature(seed_data, base64_seed_data)); |
275 #endif | 302 #endif |
276 | 303 |
277 // Using a different seed should not match the signature. | 304 // Using a different seed should not match the signature. |
278 seed_data[0] = 'x'; | 305 seed_data[0] = 'x'; |
279 EXPECT_EQ(VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_INVALID_SEED, | 306 EXPECT_EQ(VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_INVALID_SEED, |
280 seed_store.VerifySeedSignature(seed_data, base64_seed_signature)); | 307 seed_store.VerifySeedSignature(seed_data, base64_seed_signature)); |
281 } | 308 } |
282 | 309 |
| 310 TEST(VariationsSeedStoreTest, ApplyDeltaPatch) { |
| 311 // Sample seeds and the server produced delta between them to verify that the |
| 312 // client code is able to decode the deltas produced by the server. |
| 313 const std::string base64_before_seed_data = |
| 314 "CigxN2E4ZGJiOTI4ODI0ZGU3ZDU2MGUyODRlODY1ZDllYzg2NzU1MTE0ElgKDFVNQVN0YWJp" |
| 315 "bGl0eRjEyomgBTgBQgtTZXBhcmF0ZUxvZ0oLCgdEZWZhdWx0EABKDwoLU2VwYXJhdGVMb2cQ" |
| 316 "ZFIVEgszNC4wLjE4MDEuMCAAIAEgAiADEkQKIFVNQS1Vbmlmb3JtaXR5LVRyaWFsLTEwMC1Q" |
| 317 "ZXJjZW50GIDjhcAFOAFCCGdyb3VwXzAxSgwKCGdyb3VwXzAxEAFgARJPCh9VTUEtVW5pZm9y" |
| 318 "bWl0eS1UcmlhbC01MC1QZXJjZW50GIDjhcAFOAFCB2RlZmF1bHRKDAoIZ3JvdXBfMDEQAUoL" |
| 319 "CgdkZWZhdWx0EAFgAQ=="; |
| 320 const std::string base64_after_seed_data = |
| 321 "CigyNGQzYTM3ZTAxYmViOWYwNWYzMjM4YjUzNWY3MDg1ZmZlZWI4NzQwElgKDFVNQVN0YWJp" |
| 322 "bGl0eRjEyomgBTgBQgtTZXBhcmF0ZUxvZ0oLCgdEZWZhdWx0EABKDwoLU2VwYXJhdGVMb2cQ" |
| 323 "ZFIVEgszNC4wLjE4MDEuMCAAIAEgAiADEpIBCh9VTUEtVW5pZm9ybWl0eS1UcmlhbC0yMC1Q" |
| 324 "ZXJjZW50GIDjhcAFOAFCB2RlZmF1bHRKEQoIZ3JvdXBfMDEQARijtskBShEKCGdyb3VwXzAy" |
| 325 "EAEYpLbJAUoRCghncm91cF8wMxABGKW2yQFKEQoIZ3JvdXBfMDQQARimtskBShAKB2RlZmF1" |
| 326 "bHQQARiitskBYAESWAofVU1BLVVuaWZvcm1pdHktVHJpYWwtNTAtUGVyY2VudBiA44XABTgB" |
| 327 "QgdkZWZhdWx0Sg8KC25vbl9kZWZhdWx0EAFKCwoHZGVmYXVsdBABUgQoACgBYAE="; |
| 328 const std::string base64_delta_data = |
| 329 "KgooMjRkM2EzN2UwMWJlYjlmMDVmMzIzOGI1MzVmNzA4NWZmZWViODc0MAAqW+4BkgEKH1VN" |
| 330 "QS1Vbmlmb3JtaXR5LVRyaWFsLTIwLVBlcmNlbnQYgOOFwAU4AUIHZGVmYXVsdEoRCghncm91" |
| 331 "cF8wMRABGKO2yQFKEQoIZ3JvdXBfMDIQARiktskBShEKCGdyb3VwXzAzEAEYpbbJAUoRCghn" |
| 332 "cm91cF8wNBABGKa2yQFKEAoHZGVmYXVsdBABGKK2yQFgARJYCh9VTUEtVW5pZm9ybWl0eS1U" |
| 333 "cmlhbC01MC1QZXJjZW50GIDjhcAFOAFCB2RlZmF1bHRKDwoLbm9uX2RlZmF1bHQQAUoLCgdk" |
| 334 "ZWZhdWx0EAFSBCgAKAFgAQ=="; |
| 335 |
| 336 std::string before_seed_data; |
| 337 std::string after_seed_data; |
| 338 std::string delta_data; |
| 339 EXPECT_TRUE(base::Base64Decode(base64_before_seed_data, &before_seed_data)); |
| 340 EXPECT_TRUE(base::Base64Decode(base64_after_seed_data, &after_seed_data)); |
| 341 EXPECT_TRUE(base::Base64Decode(base64_delta_data, &delta_data)); |
| 342 |
| 343 std::string output; |
| 344 EXPECT_TRUE(VariationsSeedStore::ApplyDeltaPatch(before_seed_data, delta_data, |
| 345 &output)); |
| 346 EXPECT_EQ(after_seed_data, output); |
| 347 } |
| 348 |
283 } // namespace chrome_variations | 349 } // namespace chrome_variations |
OLD | NEW |