Chromium Code Reviews| 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 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 114 | 114 |
| 115 // Check that loading a bad seed returns false and clears the pref. | 115 // Check that loading a bad seed returns false and clears the pref. |
| 116 prefs.ClearPref(prefs::kVariationsSeed); | 116 prefs.ClearPref(prefs::kVariationsSeed); |
| 117 prefs.SetString(prefs::kVariationsSeed, "this should fail"); | 117 prefs.SetString(prefs::kVariationsSeed, "this should fail"); |
| 118 EXPECT_FALSE(PrefHasDefaultValue(prefs, prefs::kVariationsSeed)); | 118 EXPECT_FALSE(PrefHasDefaultValue(prefs, prefs::kVariationsSeed)); |
| 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 // The below seed and signature pair were generated using the server's | |
|
grt (UTC plus 2)
2014/10/14 01:28:30
how about moving this new test into its own "GetIn
Georges Khalil
2014/10/14 13:51:09
Done.
| |
| 125 // private key. | |
| 126 const std::string base64_seed_data = | |
| 127 "CigxZDI5NDY0ZmIzZDc4ZmYxNTU2ZTViNTUxYzY0NDdjYmM3NGU1ZmQwEr0BCh9VTUEtVW5p" | |
| 128 "Zm9ybWl0eS1UcmlhbC0xMC1QZXJjZW50GICckqUFOAFCB2RlZmF1bHRKCwoHZGVmYXVsdBAB" | |
| 129 "SgwKCGdyb3VwXzAxEAFKDAoIZ3JvdXBfMDIQAUoMCghncm91cF8wMxABSgwKCGdyb3VwXzA0" | |
| 130 "EAFKDAoIZ3JvdXBfMDUQAUoMCghncm91cF8wNhABSgwKCGdyb3VwXzA3EAFKDAoIZ3JvdXBf" | |
| 131 "MDgQAUoMCghncm91cF8wORAB"; | |
| 132 const std::string base64_seed_signature = | |
| 133 "MEQCIDD1IVxjzWYncun+9IGzqYjZvqxxujQEayJULTlbTGA/AiAr0oVmEgVUQZBYq5VLOSvy" | |
| 134 "96JkMYgzTkHPwbv7K/CmgA=="; | |
| 135 const std::string base64_seed_signature_invalid = | |
| 136 "AEQCIDD1IVxjzWYncun+9IGzqYjZvqxxujQEayJULTlbTGA/AiAr0oVmEgVUQZBYq5VLOSvy" | |
| 137 "96JkMYgzTkHPwbv7K/CmgA=="; | |
| 138 | |
| 139 // Set seed and valid signature in prefs. | |
| 140 prefs.SetString(prefs::kVariationsSeed, base64_seed_data); | |
| 141 prefs.SetString(prefs::kVariationsSeedSignature, base64_seed_signature); | |
| 142 | |
| 143 // We need a real VariationsSeedStore to test invalid signatures. | |
| 144 VariationsSeedStore seed_store_reel(&prefs); | |
| 145 seed_store_reel.LoadSeed(&loaded_seed); | |
| 146 std::string invalid_signature; | |
| 147 // Valid signature, so we should get an empty string. | |
|
grt (UTC plus 2)
2014/10/14 01:28:30
update comment
Georges Khalil
2014/10/14 13:51:09
Done.
| |
| 148 EXPECT_EQ(false, seed_store_reel.GetInvalidSignature(&invalid_signature)); | |
|
grt (UTC plus 2)
2014/10/14 01:28:30
EXPECT_FALSE
Georges Khalil
2014/10/14 13:51:09
Done.
| |
| 149 | |
| 150 prefs.SetString(prefs::kVariationsSeedSignature, | |
| 151 base64_seed_signature_invalid); | |
| 152 seed_store_reel.LoadSeed(&loaded_seed); | |
| 153 // Invalid signature, so we should get the signature itself. | |
| 154 EXPECT_EQ(true, seed_store_reel.GetInvalidSignature(&invalid_signature)); | |
|
grt (UTC plus 2)
2014/10/14 01:28:30
EXPECT_FALSE
Georges Khalil
2014/10/14 13:51:09
Done.
| |
| 155 EXPECT_EQ(invalid_signature, base64_seed_signature_invalid); | |
| 156 | |
| 157 prefs.SetString(prefs::kVariationsSeedSignature, std::string()); | |
| 158 seed_store_reel.LoadSeed(&loaded_seed); | |
| 159 // Empty signature, not considered invalid | |
| 160 EXPECT_EQ(false, seed_store_reel.GetInvalidSignature(&invalid_signature)); | |
|
grt (UTC plus 2)
2014/10/14 01:28:30
EXPECT_TRUE
Georges Khalil
2014/10/14 13:51:09
Done.
| |
| 161 | |
| 124 // Check that having no seed in prefs results in a return value of false. | 162 // Check that having no seed in prefs results in a return value of false. |
| 125 prefs.ClearPref(prefs::kVariationsSeed); | 163 prefs.ClearPref(prefs::kVariationsSeed); |
| 126 EXPECT_FALSE(seed_store.LoadSeed(&loaded_seed)); | 164 EXPECT_FALSE(seed_store.LoadSeed(&loaded_seed)); |
| 127 } | 165 } |
| 128 | 166 |
| 129 TEST(VariationsSeedStoreTest, StoreSeedData) { | 167 TEST(VariationsSeedStoreTest, StoreSeedData) { |
| 130 const variations::VariationsSeed seed = CreateTestSeed(); | 168 const variations::VariationsSeed seed = CreateTestSeed(); |
| 131 const std::string serialized_seed = SerializeSeed(seed); | 169 const std::string serialized_seed = SerializeSeed(seed); |
| 132 | 170 |
| 133 TestingPrefServiceSimple prefs; | 171 TestingPrefServiceSimple prefs; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 seed_store.VerifySeedSignature(seed_data, base64_seed_data)); | 253 seed_store.VerifySeedSignature(seed_data, base64_seed_data)); |
| 216 #endif | 254 #endif |
| 217 | 255 |
| 218 // Using a different seed should not match the signature. | 256 // Using a different seed should not match the signature. |
| 219 seed_data[0] = 'x'; | 257 seed_data[0] = 'x'; |
| 220 EXPECT_EQ(VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_INVALID_SEED, | 258 EXPECT_EQ(VariationsSeedStore::VARIATIONS_SEED_SIGNATURE_INVALID_SEED, |
| 221 seed_store.VerifySeedSignature(seed_data, base64_seed_signature)); | 259 seed_store.VerifySeedSignature(seed_data, base64_seed_signature)); |
| 222 } | 260 } |
| 223 | 261 |
| 224 } // namespace chrome_variations | 262 } // namespace chrome_variations |
| OLD | NEW |