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 variations::VariationsSeed loaded_seed; | |
Alexei Svitkine (slow)
2014/10/15 14:53:07
Nit: Move this closer to where it's used - i.e. ab
Georges Khalil
2014/10/15 16:53:19
Done.
| |
139 | |
140 // The below seed and signature pair were generated using the server's | |
141 // private key. | |
142 const std::string base64_seed_data = | |
143 "CigxZDI5NDY0ZmIzZDc4ZmYxNTU2ZTViNTUxYzY0NDdjYmM3NGU1ZmQwEr0BCh9VTUEtVW5p" | |
144 "Zm9ybWl0eS1UcmlhbC0xMC1QZXJjZW50GICckqUFOAFCB2RlZmF1bHRKCwoHZGVmYXVsdBAB" | |
145 "SgwKCGdyb3VwXzAxEAFKDAoIZ3JvdXBfMDIQAUoMCghncm91cF8wMxABSgwKCGdyb3VwXzA0" | |
146 "EAFKDAoIZ3JvdXBfMDUQAUoMCghncm91cF8wNhABSgwKCGdyb3VwXzA3EAFKDAoIZ3JvdXBf" | |
147 "MDgQAUoMCghncm91cF8wORAB"; | |
148 const std::string base64_seed_signature = | |
149 "MEQCIDD1IVxjzWYncun+9IGzqYjZvqxxujQEayJULTlbTGA/AiAr0oVmEgVUQZBYq5VLOSvy" | |
150 "96JkMYgzTkHPwbv7K/CmgA=="; | |
151 const std::string base64_seed_signature_invalid = | |
152 "AEQCIDD1IVxjzWYncun+9IGzqYjZvqxxujQEayJULTlbTGA/AiAr0oVmEgVUQZBYq5VLOSvy" | |
153 "96JkMYgzTkHPwbv7K/CmgA=="; | |
154 | |
155 // Set seed and valid signature in prefs. | |
156 prefs.SetString(prefs::kVariationsSeed, base64_seed_data); | |
157 prefs.SetString(prefs::kVariationsSeedSignature, base64_seed_signature); | |
158 | |
159 VariationsSeedStore seed_store(&prefs); | |
160 seed_store.LoadSeed(&loaded_seed); | |
161 std::string invalid_signature = seed_store.GetInvalidSignature(); | |
162 // Valid signature so we get an empty string. | |
163 EXPECT_EQ("", invalid_signature); | |
164 | |
165 prefs.SetString(prefs::kVariationsSeedSignature, | |
166 base64_seed_signature_invalid); | |
167 seed_store.LoadSeed(&loaded_seed); | |
168 // Invalid signature, so we should get the signature itself. | |
169 invalid_signature = seed_store.GetInvalidSignature(); | |
170 EXPECT_EQ(base64_seed_signature_invalid, invalid_signature); | |
171 | |
172 prefs.SetString(prefs::kVariationsSeedSignature, std::string()); | |
173 seed_store.LoadSeed(&loaded_seed); | |
174 invalid_signature = seed_store.GetInvalidSignature(); | |
175 // Empty signature, not considered invalid. | |
176 EXPECT_EQ("", invalid_signature); | |
Alexei Svitkine (slow)
2014/10/15 14:53:07
Nit: std::string() instead of "". Same thing above
Georges Khalil
2014/10/15 16:53:19
Done.
| |
177 } | |
178 | |
129 TEST(VariationsSeedStoreTest, StoreSeedData) { | 179 TEST(VariationsSeedStoreTest, StoreSeedData) { |
130 const variations::VariationsSeed seed = CreateTestSeed(); | 180 const variations::VariationsSeed seed = CreateTestSeed(); |
131 const std::string serialized_seed = SerializeSeed(seed); | 181 const std::string serialized_seed = SerializeSeed(seed); |
132 | 182 |
133 TestingPrefServiceSimple prefs; | 183 TestingPrefServiceSimple prefs; |
134 VariationsSeedStore::RegisterPrefs(prefs.registry()); | 184 VariationsSeedStore::RegisterPrefs(prefs.registry()); |
135 | 185 |
136 TestVariationsSeedStore seed_store(&prefs); | 186 TestVariationsSeedStore seed_store(&prefs); |
137 | 187 |
138 EXPECT_TRUE(seed_store.StoreSeedForTesting(serialized_seed)); | 188 EXPECT_TRUE(seed_store.StoreSeedForTesting(serialized_seed)); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 seed_store.VerifySeedSignature(seed_data, base64_seed_data)); | 265 seed_store.VerifySeedSignature(seed_data, base64_seed_data)); |
216 #endif | 266 #endif |
217 | 267 |
218 // Using a different seed should not match the signature. | 268 // Using a different seed should not match the signature. |
219 seed_data[0] = 'x'; | 269 seed_data[0] = 'x'; |
220 EXPECT_EQ(VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_INVALID_SEED, | 270 EXPECT_EQ(VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_INVALID_SEED, |
221 seed_store.VerifySeedSignature(seed_data, base64_seed_signature)); | 271 seed_store.VerifySeedSignature(seed_data, base64_seed_signature)); |
222 } | 272 } |
223 | 273 |
224 } // namespace chrome_variations | 274 } // namespace chrome_variations |
OLD | NEW |