OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/component_updater/origin_trials_component_installer.h" | 5 #include "chrome/browser/component_updater/origin_trials_component_installer.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 namespace { | 23 namespace { |
24 | 24 |
25 // Mirror the constants used in the component installer. Do not share the | 25 // Mirror the constants used in the component installer. Do not share the |
26 // constants, as want to catch inadvertent changes in the tests. The keys will | 26 // constants, as want to catch inadvertent changes in the tests. The keys will |
27 // will be generated server-side, so any changes need to be intentional and | 27 // will be generated server-side, so any changes need to be intentional and |
28 // coordinated. | 28 // coordinated. |
29 static const char kManifestOriginTrialsKey[] = "origin-trials"; | 29 static const char kManifestOriginTrialsKey[] = "origin-trials"; |
30 static const char kManifestPublicKeyPath[] = "origin-trials.public-key"; | 30 static const char kManifestPublicKeyPath[] = "origin-trials.public-key"; |
31 static const char kManifestDisabledFeaturesPath[] = | 31 static const char kManifestDisabledFeaturesPath[] = |
32 "origin-trials.disabled-features"; | 32 "origin-trials.disabled-features"; |
| 33 static const char kManifestDisabledTokensPath[] = |
| 34 "origin-trials.disabled-tokens"; |
33 | 35 |
34 static const char kTestUpdateVersion[] = "1.0"; | 36 static const char kTestUpdateVersion[] = "1.0"; |
35 static const char kExistingPublicKey[] = "existing public key"; | 37 static const char kExistingPublicKey[] = "existing public key"; |
36 static const char kNewPublicKey[] = "new public key"; | 38 static const char kNewPublicKey[] = "new public key"; |
37 static const char kExistingDisabledFeature[] = "already disabled"; | 39 static const char kExistingDisabledFeature[] = "already disabled"; |
38 static const std::vector<std::string> kExistingDisabledFeatures = { | 40 static const std::vector<std::string> kExistingDisabledFeatures = { |
39 kExistingDisabledFeature}; | 41 kExistingDisabledFeature}; |
40 static const char kNewDisabledFeature1[] = "newly disabled 1"; | 42 static const char kNewDisabledFeature1[] = "newly disabled 1"; |
41 static const char kNewDisabledFeature2[] = "newly disabled 2"; | 43 static const char kNewDisabledFeature2[] = "newly disabled 2"; |
42 static const std::vector<std::string> kNewDisabledFeatures = { | 44 static const std::vector<std::string> kNewDisabledFeatures = { |
43 kNewDisabledFeature1, kNewDisabledFeature2}; | 45 kNewDisabledFeature1, kNewDisabledFeature2}; |
| 46 static const char kExistingDisabledToken[] = "already disabled token"; |
| 47 static const std::vector<std::string> kExistingDisabledTokens = { |
| 48 kExistingDisabledToken}; |
| 49 static const char kNewDisabledToken1[] = "newly disabled token 1"; |
| 50 static const char kNewDisabledToken2[] = "newly disabled token 2"; |
| 51 static const std::vector<std::string> kNewDisabledTokens = {kNewDisabledToken1, |
| 52 kNewDisabledToken2}; |
44 | 53 |
45 } // namespace | 54 } // namespace |
46 | 55 |
47 namespace component_updater { | 56 namespace component_updater { |
48 | 57 |
49 class OriginTrialsComponentInstallerTest : public PlatformTest { | 58 class OriginTrialsComponentInstallerTest : public PlatformTest { |
50 public: | 59 public: |
51 OriginTrialsComponentInstallerTest() | 60 OriginTrialsComponentInstallerTest() |
52 : testing_local_state_(TestingBrowserProcess::GetGlobal()) {} | 61 : testing_local_state_(TestingBrowserProcess::GetGlobal()) {} |
53 | 62 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 const bool found = disabled_feature_list->GetString(i, &disabled_feature); | 103 const bool found = disabled_feature_list->GetString(i, &disabled_feature); |
95 EXPECT_TRUE(found) << "Entry not found or not a string at index " << i; | 104 EXPECT_TRUE(found) << "Entry not found or not a string at index " << i; |
96 if (!found) { | 105 if (!found) { |
97 continue; | 106 continue; |
98 } | 107 } |
99 EXPECT_EQ(features[i], disabled_feature) | 108 EXPECT_EQ(features[i], disabled_feature) |
100 << "Feature lists differ at index " << i; | 109 << "Feature lists differ at index " << i; |
101 } | 110 } |
102 } | 111 } |
103 | 112 |
| 113 void AddDisabledTokensToPrefs(const std::vector<std::string>& tokens) { |
| 114 base::ListValue disabled_token_list; |
| 115 disabled_token_list.AppendStrings(tokens); |
| 116 ListPrefUpdate update(local_state(), prefs::kOriginTrialDisabledTokens); |
| 117 update->Swap(&disabled_token_list); |
| 118 } |
| 119 |
| 120 void CheckDisabledTokensPrefs(const std::vector<std::string>& tokens) { |
| 121 ASSERT_FALSE(tokens.empty()); |
| 122 |
| 123 ASSERT_TRUE(local_state()->HasPrefPath(prefs::kOriginTrialDisabledTokens)); |
| 124 |
| 125 const base::ListValue* disabled_token_list = |
| 126 local_state()->GetList(prefs::kOriginTrialDisabledTokens); |
| 127 ASSERT_TRUE(disabled_token_list); |
| 128 |
| 129 ASSERT_EQ(tokens.size(), disabled_token_list->GetSize()); |
| 130 |
| 131 std::string disabled_token; |
| 132 for (size_t i = 0; i < tokens.size(); ++i) { |
| 133 const bool found = disabled_token_list->GetString(i, &disabled_token); |
| 134 EXPECT_TRUE(found) << "Entry not found or not a string at index " << i; |
| 135 if (!found) { |
| 136 continue; |
| 137 } |
| 138 EXPECT_EQ(tokens[i], disabled_token) |
| 139 << "Token lists differ at index " << i; |
| 140 } |
| 141 } |
| 142 |
104 PrefService* local_state() { return g_browser_process->local_state(); } | 143 PrefService* local_state() { return g_browser_process->local_state(); } |
105 | 144 |
106 protected: | 145 protected: |
107 base::ScopedTempDir temp_dir_; | 146 base::ScopedTempDir temp_dir_; |
108 ScopedTestingLocalState testing_local_state_; | 147 ScopedTestingLocalState testing_local_state_; |
109 std::unique_ptr<ComponentInstallerTraits> traits_; | 148 std::unique_ptr<ComponentInstallerTraits> traits_; |
110 | 149 |
111 private: | 150 private: |
112 DISALLOW_COPY_AND_ASSIGN(OriginTrialsComponentInstallerTest); | 151 DISALLOW_COPY_AND_ASSIGN(OriginTrialsComponentInstallerTest); |
113 }; | 152 }; |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 auto disabled_feature_list = base::MakeUnique<base::ListValue>(); | 225 auto disabled_feature_list = base::MakeUnique<base::ListValue>(); |
187 disabled_feature_list->AppendStrings(kNewDisabledFeatures); | 226 disabled_feature_list->AppendStrings(kNewDisabledFeatures); |
188 manifest->Set(kManifestDisabledFeaturesPath, | 227 manifest->Set(kManifestDisabledFeaturesPath, |
189 std::move(disabled_feature_list)); | 228 std::move(disabled_feature_list)); |
190 | 229 |
191 LoadUpdates(std::move(manifest)); | 230 LoadUpdates(std::move(manifest)); |
192 | 231 |
193 CheckDisabledFeaturesPrefs(kNewDisabledFeatures); | 232 CheckDisabledFeaturesPrefs(kNewDisabledFeatures); |
194 } | 233 } |
195 | 234 |
| 235 TEST_F(OriginTrialsComponentInstallerTest, |
| 236 DisabledTokensResetToDefaultWhenListMissing) { |
| 237 AddDisabledTokensToPrefs(kExistingDisabledTokens); |
| 238 ASSERT_TRUE(local_state()->HasPrefPath(prefs::kOriginTrialDisabledTokens)); |
| 239 |
| 240 // Load with empty section in manifest |
| 241 LoadUpdates(nullptr); |
| 242 |
| 243 EXPECT_FALSE(local_state()->HasPrefPath(prefs::kOriginTrialDisabledTokens)); |
| 244 } |
| 245 |
| 246 TEST_F(OriginTrialsComponentInstallerTest, |
| 247 DisabledTokensResetToDefaultWhenListEmpty) { |
| 248 AddDisabledTokensToPrefs(kExistingDisabledTokens); |
| 249 ASSERT_TRUE(local_state()->HasPrefPath(prefs::kOriginTrialDisabledTokens)); |
| 250 |
| 251 auto manifest = base::MakeUnique<base::DictionaryValue>(); |
| 252 auto disabled_token_list = base::MakeUnique<base::ListValue>(); |
| 253 manifest->Set(kManifestDisabledTokensPath, std::move(disabled_token_list)); |
| 254 |
| 255 LoadUpdates(std::move(manifest)); |
| 256 |
| 257 EXPECT_FALSE(local_state()->HasPrefPath(prefs::kOriginTrialDisabledTokens)); |
| 258 } |
| 259 |
| 260 TEST_F(OriginTrialsComponentInstallerTest, DisabledTokensSetWhenListExists) { |
| 261 ASSERT_FALSE(local_state()->HasPrefPath(prefs::kOriginTrialDisabledTokens)); |
| 262 |
| 263 auto manifest = base::MakeUnique<base::DictionaryValue>(); |
| 264 auto disabled_token_list = base::MakeUnique<base::ListValue>(); |
| 265 disabled_token_list->AppendString(kNewDisabledToken1); |
| 266 manifest->Set(kManifestDisabledTokensPath, std::move(disabled_token_list)); |
| 267 |
| 268 LoadUpdates(std::move(manifest)); |
| 269 |
| 270 std::vector<std::string> tokens = {kNewDisabledToken1}; |
| 271 CheckDisabledTokensPrefs(tokens); |
| 272 } |
| 273 |
| 274 TEST_F(OriginTrialsComponentInstallerTest, |
| 275 DisabledTokensReplacedWhenListExists) { |
| 276 AddDisabledTokensToPrefs(kExistingDisabledTokens); |
| 277 ASSERT_TRUE(local_state()->HasPrefPath(prefs::kOriginTrialDisabledTokens)); |
| 278 |
| 279 auto manifest = base::MakeUnique<base::DictionaryValue>(); |
| 280 auto disabled_token_list = base::MakeUnique<base::ListValue>(); |
| 281 disabled_token_list->AppendStrings(kNewDisabledTokens); |
| 282 manifest->Set(kManifestDisabledTokensPath, std::move(disabled_token_list)); |
| 283 |
| 284 LoadUpdates(std::move(manifest)); |
| 285 |
| 286 CheckDisabledTokensPrefs(kNewDisabledTokens); |
| 287 } |
| 288 |
196 } // namespace component_updater | 289 } // namespace component_updater |
OLD | NEW |