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" |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 EXPECT_FALSE(seed_store.LoadSeed(&loaded_seed)); | 119 EXPECT_FALSE(seed_store.LoadSeed(&loaded_seed)); |
120 EXPECT_TRUE(PrefHasDefaultValue(prefs, prefs::kVariationsSeed)); | 120 EXPECT_TRUE(PrefHasDefaultValue(prefs, prefs::kVariationsSeed)); |
121 EXPECT_TRUE(PrefHasDefaultValue(prefs, prefs::kVariationsSeedDate)); | 121 EXPECT_TRUE(PrefHasDefaultValue(prefs, prefs::kVariationsSeedDate)); |
122 EXPECT_TRUE(PrefHasDefaultValue(prefs, prefs::kVariationsSeedSignature)); | 122 EXPECT_TRUE(PrefHasDefaultValue(prefs, prefs::kVariationsSeedSignature)); |
123 | 123 |
124 // 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. |
125 prefs.ClearPref(prefs::kVariationsSeed); | 125 prefs.ClearPref(prefs::kVariationsSeed); |
126 EXPECT_FALSE(seed_store.LoadSeed(&loaded_seed)); | 126 EXPECT_FALSE(seed_store.LoadSeed(&loaded_seed)); |
127 } | 127 } |
128 | 128 |
129 TEST(VariationsSeedStoreTest, GetInvalidSignature) { | |
130 const variations::VariationsSeed seed = CreateTestSeed(); | |
131 std::string seed_hash; | |
132 const std::string base64_seed = SerializeSeedBase64(seed, &seed_hash); | |
133 | |
134 TestingPrefServiceSimple prefs; | |
135 VariationsSeedStore::RegisterPrefs(prefs.registry()); | |
136 prefs.SetString(prefs::kVariationsSeed, base64_seed); | |
137 | |
138 // The below seed and signature pair were generated using the server's | |
139 // private key. | |
140 const std::string base64_seed_data = | |
141 "CigxZDI5NDY0ZmIzZDc4ZmYxNTU2ZTViNTUxYzY0NDdjYmM3NGU1ZmQwEr0BCh9VTUEtVW5p" | |
142 "Zm9ybWl0eS1UcmlhbC0xMC1QZXJjZW50GICckqUFOAFCB2RlZmF1bHRKCwoHZGVmYXVsdBAB" | |
143 "SgwKCGdyb3VwXzAxEAFKDAoIZ3JvdXBfMDIQAUoMCghncm91cF8wMxABSgwKCGdyb3VwXzA0" | |
144 "EAFKDAoIZ3JvdXBfMDUQAUoMCghncm91cF8wNhABSgwKCGdyb3VwXzA3EAFKDAoIZ3JvdXBf" | |
145 "MDgQAUoMCghncm91cF8wORAB"; | |
146 const std::string base64_seed_signature = | |
147 "MEQCIDD1IVxjzWYncun+9IGzqYjZvqxxujQEayJULTlbTGA/AiAr0oVmEgVUQZBYq5VLOSvy" | |
148 "96JkMYgzTkHPwbv7K/CmgA=="; | |
149 const std::string base64_seed_signature_invalid = | |
150 "AEQCIDD1IVxjzWYncun+9IGzqYjZvqxxujQEayJULTlbTGA/AiAr0oVmEgVUQZBYq5VLOSvy" | |
151 "96JkMYgzTkHPwbv7K/CmgA=="; | |
152 | |
153 // Set seed and valid signature in prefs. | |
154 prefs.SetString(prefs::kVariationsSeed, base64_seed_data); | |
155 prefs.SetString(prefs::kVariationsSeedSignature, base64_seed_signature); | |
156 | |
157 VariationsSeedStore seed_store(&prefs); | |
158 variations::VariationsSeed loaded_seed; | |
159 seed_store.LoadSeed(&loaded_seed); | |
160 std::string invalid_signature = seed_store.GetInvalidSignature(); | |
161 // Valid signature so we get an empty string. | |
162 EXPECT_EQ(std::string(), invalid_signature); | |
163 | |
164 prefs.SetString(prefs::kVariationsSeedSignature, | |
165 base64_seed_signature_invalid); | |
166 seed_store.LoadSeed(&loaded_seed); | |
167 // Invalid signature, so we should get the signature itself, except on mobile | |
168 // where we should get an empty string because verification is not enabled. | |
169 invalid_signature = seed_store.GetInvalidSignature(); | |
170 #if defined(OS_IOS) || defined(OS_ANDROID) | |
171 EXPECT_EQ(std::string(), invalid_signature); | |
172 #else | |
173 EXPECT_EQ(base64_seed_signature_invalid, invalid_signature); | |
174 #endif | |
175 | |
176 prefs.SetString(prefs::kVariationsSeedSignature, std::string()); | |
177 seed_store.LoadSeed(&loaded_seed); | |
178 invalid_signature = seed_store.GetInvalidSignature(); | |
179 // Empty signature, not considered invalid. | |
180 EXPECT_EQ(std::string(), invalid_signature); | |
181 } | |
182 | |
183 TEST(VariationsSeedStoreTest, StoreSeedData) { | 129 TEST(VariationsSeedStoreTest, StoreSeedData) { |
184 const variations::VariationsSeed seed = CreateTestSeed(); | 130 const variations::VariationsSeed seed = CreateTestSeed(); |
185 const std::string serialized_seed = SerializeSeed(seed); | 131 const std::string serialized_seed = SerializeSeed(seed); |
186 | 132 |
187 TestingPrefServiceSimple prefs; | 133 TestingPrefServiceSimple prefs; |
188 VariationsSeedStore::RegisterPrefs(prefs.registry()); | 134 VariationsSeedStore::RegisterPrefs(prefs.registry()); |
189 | 135 |
190 TestVariationsSeedStore seed_store(&prefs); | 136 TestVariationsSeedStore seed_store(&prefs); |
191 | 137 |
192 EXPECT_TRUE(seed_store.StoreSeedForTesting(serialized_seed)); | 138 EXPECT_TRUE(seed_store.StoreSeedForTesting(serialized_seed)); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 seed_store.VerifySeedSignature(seed_data, base64_seed_data)); | 215 seed_store.VerifySeedSignature(seed_data, base64_seed_data)); |
270 #endif | 216 #endif |
271 | 217 |
272 // Using a different seed should not match the signature. | 218 // Using a different seed should not match the signature. |
273 seed_data[0] = 'x'; | 219 seed_data[0] = 'x'; |
274 EXPECT_EQ(VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_INVALID_SEED, | 220 EXPECT_EQ(VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_INVALID_SEED, |
275 seed_store.VerifySeedSignature(seed_data, base64_seed_signature)); | 221 seed_store.VerifySeedSignature(seed_data, base64_seed_signature)); |
276 } | 222 } |
277 | 223 |
278 } // namespace chrome_variations | 224 } // namespace chrome_variations |
OLD | NEW |