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, VerifySeedSignature) { | 218 TEST(VariationsSeedStoreTest, VerifySeedSignature) { |
217 // The below seed and signature pair were generated using the server's | 219 // The below seed and signature pair were generated using the server's |
218 // private key. | 220 // private key. |
219 const std::string base64_seed_data = | 221 const std::string base64_seed_data = |
220 "CigxZDI5NDY0ZmIzZDc4ZmYxNTU2ZTViNTUxYzY0NDdjYmM3NGU1ZmQwEr0BCh9VTUEtVW5p" | 222 "CigxZDI5NDY0ZmIzZDc4ZmYxNTU2ZTViNTUxYzY0NDdjYmM3NGU1ZmQwEr0BCh9VTUEtVW5p" |
221 "Zm9ybWl0eS1UcmlhbC0xMC1QZXJjZW50GICckqUFOAFCB2RlZmF1bHRKCwoHZGVmYXVsdBAB" | 223 "Zm9ybWl0eS1UcmlhbC0xMC1QZXJjZW50GICckqUFOAFCB2RlZmF1bHRKCwoHZGVmYXVsdBAB" |
222 "SgwKCGdyb3VwXzAxEAFKDAoIZ3JvdXBfMDIQAUoMCghncm91cF8wMxABSgwKCGdyb3VwXzA0" | 224 "SgwKCGdyb3VwXzAxEAFKDAoIZ3JvdXBfMDIQAUoMCghncm91cF8wMxABSgwKCGdyb3VwXzA0" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 EXPECT_EQ(VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_INVALID_SIGNATURE, | 263 EXPECT_EQ(VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_INVALID_SIGNATURE, |
262 seed_store.VerifySeedSignature(seed_data, base64_seed_data)); | 264 seed_store.VerifySeedSignature(seed_data, base64_seed_data)); |
263 #endif | 265 #endif |
264 | 266 |
265 // Using a different seed should not match the signature. | 267 // Using a different seed should not match the signature. |
266 seed_data[0] = 'x'; | 268 seed_data[0] = 'x'; |
267 EXPECT_EQ(VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_INVALID_SEED, | 269 EXPECT_EQ(VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_INVALID_SEED, |
268 seed_store.VerifySeedSignature(seed_data, base64_seed_signature)); | 270 seed_store.VerifySeedSignature(seed_data, base64_seed_signature)); |
269 } | 271 } |
270 | 272 |
| 273 TEST(VariationsSeedStoreTest, ApplyDeltaPatch) { |
| 274 // Sample seeds and the server produced delta between them to verify that the |
| 275 // client code is able to decode the deltas produced by the server. |
| 276 const std::string base64_before_seed_data = |
| 277 "CigxN2E4ZGJiOTI4ODI0ZGU3ZDU2MGUyODRlODY1ZDllYzg2NzU1MTE0ElgKDFVNQVN0YWJp" |
| 278 "bGl0eRjEyomgBTgBQgtTZXBhcmF0ZUxvZ0oLCgdEZWZhdWx0EABKDwoLU2VwYXJhdGVMb2cQ" |
| 279 "ZFIVEgszNC4wLjE4MDEuMCAAIAEgAiADEkQKIFVNQS1Vbmlmb3JtaXR5LVRyaWFsLTEwMC1Q" |
| 280 "ZXJjZW50GIDjhcAFOAFCCGdyb3VwXzAxSgwKCGdyb3VwXzAxEAFgARJPCh9VTUEtVW5pZm9y" |
| 281 "bWl0eS1UcmlhbC01MC1QZXJjZW50GIDjhcAFOAFCB2RlZmF1bHRKDAoIZ3JvdXBfMDEQAUoL" |
| 282 "CgdkZWZhdWx0EAFgAQ=="; |
| 283 const std::string base64_after_seed_data = |
| 284 "CigyNGQzYTM3ZTAxYmViOWYwNWYzMjM4YjUzNWY3MDg1ZmZlZWI4NzQwElgKDFVNQVN0YWJp" |
| 285 "bGl0eRjEyomgBTgBQgtTZXBhcmF0ZUxvZ0oLCgdEZWZhdWx0EABKDwoLU2VwYXJhdGVMb2cQ" |
| 286 "ZFIVEgszNC4wLjE4MDEuMCAAIAEgAiADEpIBCh9VTUEtVW5pZm9ybWl0eS1UcmlhbC0yMC1Q" |
| 287 "ZXJjZW50GIDjhcAFOAFCB2RlZmF1bHRKEQoIZ3JvdXBfMDEQARijtskBShEKCGdyb3VwXzAy" |
| 288 "EAEYpLbJAUoRCghncm91cF8wMxABGKW2yQFKEQoIZ3JvdXBfMDQQARimtskBShAKB2RlZmF1" |
| 289 "bHQQARiitskBYAESWAofVU1BLVVuaWZvcm1pdHktVHJpYWwtNTAtUGVyY2VudBiA44XABTgB" |
| 290 "QgdkZWZhdWx0Sg8KC25vbl9kZWZhdWx0EAFKCwoHZGVmYXVsdBABUgQoACgBYAE="; |
| 291 const std::string base64_delta_data = |
| 292 "KgooMjRkM2EzN2UwMWJlYjlmMDVmMzIzOGI1MzVmNzA4NWZmZWViODc0MAAqW+4BkgEKH1VN" |
| 293 "QS1Vbmlmb3JtaXR5LVRyaWFsLTIwLVBlcmNlbnQYgOOFwAU4AUIHZGVmYXVsdEoRCghncm91" |
| 294 "cF8wMRABGKO2yQFKEQoIZ3JvdXBfMDIQARiktskBShEKCGdyb3VwXzAzEAEYpbbJAUoRCghn" |
| 295 "cm91cF8wNBABGKa2yQFKEAoHZGVmYXVsdBABGKK2yQFgARJYCh9VTUEtVW5pZm9ybWl0eS1U" |
| 296 "cmlhbC01MC1QZXJjZW50GIDjhcAFOAFCB2RlZmF1bHRKDwoLbm9uX2RlZmF1bHQQAUoLCgdk" |
| 297 "ZWZhdWx0EAFSBCgAKAFgAQ=="; |
| 298 |
| 299 std::string before_seed_data; |
| 300 std::string after_seed_data; |
| 301 std::string delta_data; |
| 302 EXPECT_TRUE(base::Base64Decode(base64_before_seed_data, &before_seed_data)); |
| 303 EXPECT_TRUE(base::Base64Decode(base64_after_seed_data, &after_seed_data)); |
| 304 EXPECT_TRUE(base::Base64Decode(base64_delta_data, &delta_data)); |
| 305 |
| 306 std::string output; |
| 307 EXPECT_TRUE(VariationsSeedStore::ApplyDeltaPatch(before_seed_data, delta_data, |
| 308 &output)); |
| 309 EXPECT_EQ(after_seed_data, output); |
| 310 } |
| 311 |
271 } // namespace chrome_variations | 312 } // namespace chrome_variations |
OLD | NEW |