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 "base/sha1.h" | 9 #include "base/sha1.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
13 #include "components/variations/proto/study.pb.h" | 13 #include "components/variations/proto/study.pb.h" |
14 #include "components/variations/proto/variations_seed.pb.h" | 14 #include "components/variations/proto/variations_seed.pb.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 namespace chrome_variations { | 17 namespace chrome_variations { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 class TestVariationsSeedStore : public VariationsSeedStore { | 21 class TestVariationsSeedStore : public VariationsSeedStore { |
22 public: | 22 public: |
23 explicit TestVariationsSeedStore(PrefService* local_state) | 23 explicit TestVariationsSeedStore(PrefService* local_state) |
24 : VariationsSeedStore(local_state) {} | 24 : VariationsSeedStore(local_state) {} |
25 virtual ~TestVariationsSeedStore() {} | 25 virtual ~TestVariationsSeedStore() {} |
26 | 26 |
| 27 bool StoreSeedForTesting(const std::string& seed_data) { |
| 28 VariationsSeed seed; |
| 29 return StoreSeedData(seed_data, std::string(), base::Time::Now(), &seed); |
| 30 } |
| 31 |
27 virtual VariationsSeedStore::VerifySignatureResult VerifySeedSignature( | 32 virtual VariationsSeedStore::VerifySignatureResult VerifySeedSignature( |
28 const std::string& seed_bytes, | 33 const std::string& seed_bytes, |
29 const std::string& base64_seed_signature) OVERRIDE { | 34 const std::string& base64_seed_signature) OVERRIDE { |
30 return VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_ENUM_SIZE; | 35 return VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_ENUM_SIZE; |
31 } | 36 } |
32 | 37 |
33 private: | 38 private: |
34 DISALLOW_COPY_AND_ASSIGN(TestVariationsSeedStore); | 39 DISALLOW_COPY_AND_ASSIGN(TestVariationsSeedStore); |
35 }; | 40 }; |
36 | 41 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 EXPECT_TRUE(PrefHasDefaultValue(prefs, prefs::kVariationsSeed)); | 120 EXPECT_TRUE(PrefHasDefaultValue(prefs, prefs::kVariationsSeed)); |
116 EXPECT_TRUE(PrefHasDefaultValue(prefs, prefs::kVariationsSeedDate)); | 121 EXPECT_TRUE(PrefHasDefaultValue(prefs, prefs::kVariationsSeedDate)); |
117 EXPECT_TRUE(PrefHasDefaultValue(prefs, prefs::kVariationsSeedSignature)); | 122 EXPECT_TRUE(PrefHasDefaultValue(prefs, prefs::kVariationsSeedSignature)); |
118 | 123 |
119 // Check that having no seed in prefs results in a return value of false. | 124 // Check that having no seed in prefs results in a return value of false. |
120 prefs.ClearPref(prefs::kVariationsSeed); | 125 prefs.ClearPref(prefs::kVariationsSeed); |
121 EXPECT_FALSE(seed_store.LoadSeed(&loaded_seed)); | 126 EXPECT_FALSE(seed_store.LoadSeed(&loaded_seed)); |
122 } | 127 } |
123 | 128 |
124 TEST(VariationsSeedStoreTest, StoreSeedData) { | 129 TEST(VariationsSeedStoreTest, StoreSeedData) { |
125 const base::Time now = base::Time::Now(); | |
126 const VariationsSeed seed = CreateTestSeed(); | 130 const VariationsSeed seed = CreateTestSeed(); |
127 const std::string serialized_seed = SerializeSeed(seed); | 131 const std::string serialized_seed = SerializeSeed(seed); |
128 | 132 |
129 TestingPrefServiceSimple prefs; | 133 TestingPrefServiceSimple prefs; |
130 VariationsSeedStore::RegisterPrefs(prefs.registry()); | 134 VariationsSeedStore::RegisterPrefs(prefs.registry()); |
131 | 135 |
132 TestVariationsSeedStore seed_store(&prefs); | 136 TestVariationsSeedStore seed_store(&prefs); |
133 | 137 |
134 EXPECT_TRUE(seed_store.StoreSeedData(serialized_seed, std::string(), now)); | 138 EXPECT_TRUE(seed_store.StoreSeedForTesting(serialized_seed)); |
135 // Make sure the pref was actually set. | 139 // Make sure the pref was actually set. |
136 EXPECT_FALSE(PrefHasDefaultValue(prefs, prefs::kVariationsSeed)); | 140 EXPECT_FALSE(PrefHasDefaultValue(prefs, prefs::kVariationsSeed)); |
137 | 141 |
138 std::string loaded_serialized_seed = prefs.GetString(prefs::kVariationsSeed); | 142 std::string loaded_serialized_seed = prefs.GetString(prefs::kVariationsSeed); |
139 std::string decoded_serialized_seed; | 143 std::string decoded_serialized_seed; |
140 ASSERT_TRUE(base::Base64Decode(loaded_serialized_seed, | 144 ASSERT_TRUE(base::Base64Decode(loaded_serialized_seed, |
141 &decoded_serialized_seed)); | 145 &decoded_serialized_seed)); |
142 // Make sure the stored seed from pref is the same as the seed we created. | 146 // Make sure the stored seed from pref is the same as the seed we created. |
143 EXPECT_EQ(serialized_seed, decoded_serialized_seed); | 147 EXPECT_EQ(serialized_seed, decoded_serialized_seed); |
144 | 148 |
145 // Check if trying to store a bad seed leaves the pref unchanged. | 149 // Check if trying to store a bad seed leaves the pref unchanged. |
146 prefs.ClearPref(prefs::kVariationsSeed); | 150 prefs.ClearPref(prefs::kVariationsSeed); |
147 EXPECT_FALSE(seed_store.StoreSeedData("should fail", std::string(), now)); | 151 EXPECT_FALSE(seed_store.StoreSeedForTesting("should fail")); |
148 EXPECT_TRUE(PrefHasDefaultValue(prefs, prefs::kVariationsSeed)); | 152 EXPECT_TRUE(PrefHasDefaultValue(prefs, prefs::kVariationsSeed)); |
149 } | 153 } |
150 | 154 |
151 TEST(VariationsSeedStoreTest, VerifySeedSignature) { | 155 TEST(VariationsSeedStoreTest, VerifySeedSignature) { |
152 // The below seed and signature pair were generated using the server's | 156 // The below seed and signature pair were generated using the server's |
153 // private key. | 157 // private key. |
154 const std::string base64_seed_data = | 158 const std::string base64_seed_data = |
155 "CigxZDI5NDY0ZmIzZDc4ZmYxNTU2ZTViNTUxYzY0NDdjYmM3NGU1ZmQwEr0BCh9VTUEtVW5p" | 159 "CigxZDI5NDY0ZmIzZDc4ZmYxNTU2ZTViNTUxYzY0NDdjYmM3NGU1ZmQwEr0BCh9VTUEtVW5p" |
156 "Zm9ybWl0eS1UcmlhbC0xMC1QZXJjZW50GICckqUFOAFCB2RlZmF1bHRKCwoHZGVmYXVsdBAB" | 160 "Zm9ybWl0eS1UcmlhbC0xMC1QZXJjZW50GICckqUFOAFCB2RlZmF1bHRKCwoHZGVmYXVsdBAB" |
157 "SgwKCGdyb3VwXzAxEAFKDAoIZ3JvdXBfMDIQAUoMCghncm91cF8wMxABSgwKCGdyb3VwXzA0" | 161 "SgwKCGdyb3VwXzAxEAFKDAoIZ3JvdXBfMDIQAUoMCghncm91cF8wMxABSgwKCGdyb3VwXzA0" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 EXPECT_EQ(VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_INVALID_SIGNATURE, | 194 EXPECT_EQ(VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_INVALID_SIGNATURE, |
191 seed_store.VerifySeedSignature(seed_data, base64_seed_data)); | 195 seed_store.VerifySeedSignature(seed_data, base64_seed_data)); |
192 | 196 |
193 // Using a different seed should not match the signature. | 197 // Using a different seed should not match the signature. |
194 seed_data[0] = 'x'; | 198 seed_data[0] = 'x'; |
195 EXPECT_EQ(VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_INVALID_SEED, | 199 EXPECT_EQ(VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_INVALID_SEED, |
196 seed_store.VerifySeedSignature(seed_data, base64_seed_signature)); | 200 seed_store.VerifySeedSignature(seed_data, base64_seed_signature)); |
197 } | 201 } |
198 | 202 |
199 } // namespace chrome_variations | 203 } // namespace chrome_variations |
OLD | NEW |