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 return StoreSeedData(seed_data, std::string(), base::Time::Now(), NULL); |
| 29 } |
| 30 |
27 virtual VariationsSeedStore::VerifySignatureResult VerifySeedSignature( | 31 virtual VariationsSeedStore::VerifySignatureResult VerifySeedSignature( |
28 const std::string& seed_bytes, | 32 const std::string& seed_bytes, |
29 const std::string& base64_seed_signature) OVERRIDE { | 33 const std::string& base64_seed_signature) OVERRIDE { |
30 return VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_ENUM_SIZE; | 34 return VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_ENUM_SIZE; |
31 } | 35 } |
32 | 36 |
33 private: | 37 private: |
34 DISALLOW_COPY_AND_ASSIGN(TestVariationsSeedStore); | 38 DISALLOW_COPY_AND_ASSIGN(TestVariationsSeedStore); |
35 }; | 39 }; |
36 | 40 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 EXPECT_TRUE(PrefHasDefaultValue(prefs, prefs::kVariationsSeed)); | 119 EXPECT_TRUE(PrefHasDefaultValue(prefs, prefs::kVariationsSeed)); |
116 EXPECT_TRUE(PrefHasDefaultValue(prefs, prefs::kVariationsSeedDate)); | 120 EXPECT_TRUE(PrefHasDefaultValue(prefs, prefs::kVariationsSeedDate)); |
117 EXPECT_TRUE(PrefHasDefaultValue(prefs, prefs::kVariationsSeedSignature)); | 121 EXPECT_TRUE(PrefHasDefaultValue(prefs, prefs::kVariationsSeedSignature)); |
118 | 122 |
119 // Check that having no seed in prefs results in a return value of false. | 123 // Check that having no seed in prefs results in a return value of false. |
120 prefs.ClearPref(prefs::kVariationsSeed); | 124 prefs.ClearPref(prefs::kVariationsSeed); |
121 EXPECT_FALSE(seed_store.LoadSeed(&loaded_seed)); | 125 EXPECT_FALSE(seed_store.LoadSeed(&loaded_seed)); |
122 } | 126 } |
123 | 127 |
124 TEST(VariationsSeedStoreTest, StoreSeedData) { | 128 TEST(VariationsSeedStoreTest, StoreSeedData) { |
125 const base::Time now = base::Time::Now(); | |
126 const VariationsSeed seed = CreateTestSeed(); | 129 const VariationsSeed seed = CreateTestSeed(); |
127 const std::string serialized_seed = SerializeSeed(seed); | 130 const std::string serialized_seed = SerializeSeed(seed); |
128 | 131 |
129 TestingPrefServiceSimple prefs; | 132 TestingPrefServiceSimple prefs; |
130 VariationsSeedStore::RegisterPrefs(prefs.registry()); | 133 VariationsSeedStore::RegisterPrefs(prefs.registry()); |
131 | 134 |
132 TestVariationsSeedStore seed_store(&prefs); | 135 TestVariationsSeedStore seed_store(&prefs); |
133 | 136 |
134 EXPECT_TRUE(seed_store.StoreSeedData(serialized_seed, std::string(), now)); | 137 EXPECT_TRUE(seed_store.StoreSeedForTesting(serialized_seed)); |
135 // Make sure the pref was actually set. | 138 // Make sure the pref was actually set. |
136 EXPECT_FALSE(PrefHasDefaultValue(prefs, prefs::kVariationsSeed)); | 139 EXPECT_FALSE(PrefHasDefaultValue(prefs, prefs::kVariationsSeed)); |
137 | 140 |
138 std::string loaded_serialized_seed = prefs.GetString(prefs::kVariationsSeed); | 141 std::string loaded_serialized_seed = prefs.GetString(prefs::kVariationsSeed); |
139 std::string decoded_serialized_seed; | 142 std::string decoded_serialized_seed; |
140 ASSERT_TRUE(base::Base64Decode(loaded_serialized_seed, | 143 ASSERT_TRUE(base::Base64Decode(loaded_serialized_seed, |
141 &decoded_serialized_seed)); | 144 &decoded_serialized_seed)); |
142 // Make sure the stored seed from pref is the same as the seed we created. | 145 // Make sure the stored seed from pref is the same as the seed we created. |
143 EXPECT_EQ(serialized_seed, decoded_serialized_seed); | 146 EXPECT_EQ(serialized_seed, decoded_serialized_seed); |
144 | 147 |
145 // Check if trying to store a bad seed leaves the pref unchanged. | 148 // Check if trying to store a bad seed leaves the pref unchanged. |
146 prefs.ClearPref(prefs::kVariationsSeed); | 149 prefs.ClearPref(prefs::kVariationsSeed); |
147 EXPECT_FALSE(seed_store.StoreSeedData("should fail", std::string(), now)); | 150 EXPECT_FALSE(seed_store.StoreSeedForTesting("should fail")); |
148 EXPECT_TRUE(PrefHasDefaultValue(prefs, prefs::kVariationsSeed)); | 151 EXPECT_TRUE(PrefHasDefaultValue(prefs, prefs::kVariationsSeed)); |
149 } | 152 } |
150 | 153 |
| 154 TEST(VariationsSeedStoreTest, StoreSeedData_ParsedSeed) { |
| 155 const VariationsSeed seed = CreateTestSeed(); |
| 156 const std::string serialized_seed = SerializeSeed(seed); |
| 157 |
| 158 TestingPrefServiceSimple prefs; |
| 159 VariationsSeedStore::RegisterPrefs(prefs.registry()); |
| 160 TestVariationsSeedStore seed_store(&prefs); |
| 161 |
| 162 VariationsSeed parsed_seed; |
| 163 EXPECT_TRUE(seed_store.StoreSeedData(serialized_seed, std::string(), |
| 164 base::Time::Now(), &parsed_seed)); |
| 165 EXPECT_EQ(serialized_seed, SerializeSeed(parsed_seed)); |
| 166 } |
| 167 |
151 TEST(VariationsSeedStoreTest, VerifySeedSignature) { | 168 TEST(VariationsSeedStoreTest, VerifySeedSignature) { |
152 // The below seed and signature pair were generated using the server's | 169 // The below seed and signature pair were generated using the server's |
153 // private key. | 170 // private key. |
154 const std::string base64_seed_data = | 171 const std::string base64_seed_data = |
155 "CigxZDI5NDY0ZmIzZDc4ZmYxNTU2ZTViNTUxYzY0NDdjYmM3NGU1ZmQwEr0BCh9VTUEtVW5p" | 172 "CigxZDI5NDY0ZmIzZDc4ZmYxNTU2ZTViNTUxYzY0NDdjYmM3NGU1ZmQwEr0BCh9VTUEtVW5p" |
156 "Zm9ybWl0eS1UcmlhbC0xMC1QZXJjZW50GICckqUFOAFCB2RlZmF1bHRKCwoHZGVmYXVsdBAB" | 173 "Zm9ybWl0eS1UcmlhbC0xMC1QZXJjZW50GICckqUFOAFCB2RlZmF1bHRKCwoHZGVmYXVsdBAB" |
157 "SgwKCGdyb3VwXzAxEAFKDAoIZ3JvdXBfMDIQAUoMCghncm91cF8wMxABSgwKCGdyb3VwXzA0" | 174 "SgwKCGdyb3VwXzAxEAFKDAoIZ3JvdXBfMDIQAUoMCghncm91cF8wMxABSgwKCGdyb3VwXzA0" |
158 "EAFKDAoIZ3JvdXBfMDUQAUoMCghncm91cF8wNhABSgwKCGdyb3VwXzA3EAFKDAoIZ3JvdXBf" | 175 "EAFKDAoIZ3JvdXBfMDUQAUoMCghncm91cF8wNhABSgwKCGdyb3VwXzA3EAFKDAoIZ3JvdXBf" |
159 "MDgQAUoMCghncm91cF8wORAB"; | 176 "MDgQAUoMCghncm91cF8wORAB"; |
160 const std::string base64_seed_signature = | 177 const std::string base64_seed_signature = |
(...skipping 29 matching lines...) Expand all Loading... |
190 EXPECT_EQ(VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_INVALID_SIGNATURE, | 207 EXPECT_EQ(VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_INVALID_SIGNATURE, |
191 seed_store.VerifySeedSignature(seed_data, base64_seed_data)); | 208 seed_store.VerifySeedSignature(seed_data, base64_seed_data)); |
192 | 209 |
193 // Using a different seed should not match the signature. | 210 // Using a different seed should not match the signature. |
194 seed_data[0] = 'x'; | 211 seed_data[0] = 'x'; |
195 EXPECT_EQ(VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_INVALID_SEED, | 212 EXPECT_EQ(VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_INVALID_SEED, |
196 seed_store.VerifySeedSignature(seed_data, base64_seed_signature)); | 213 seed_store.VerifySeedSignature(seed_data, base64_seed_signature)); |
197 } | 214 } |
198 | 215 |
199 } // namespace chrome_variations | 216 } // namespace chrome_variations |
OLD | NEW |