Xinghui Lu | 259be4d | 2021-04-21 20:03:35 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/extensions/omaha_attributes_handler.h" |
| 6 | |
| 7 | #include "base/test/metrics/histogram_tester.h" |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 8 | #include "base/test/scoped_feature_list.h" |
Xinghui Lu | 259be4d | 2021-04-21 20:03:35 | [diff] [blame] | 9 | #include "base/values.h" |
Xinghui Lu | 259be4d | 2021-04-21 20:03:35 | [diff] [blame] | 10 | #include "chrome/browser/extensions/extension_service.h" |
| 11 | #include "chrome/browser/extensions/extension_service_test_base.h" |
Xinghui Lu | 574a067 | 2021-07-15 19:54:49 | [diff] [blame^] | 12 | #include "extensions/browser/blocklist_extension_prefs.h" |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 13 | #include "extensions/browser/disable_reason.h" |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 14 | #include "extensions/browser/extension_prefs.h" |
| 15 | #include "extensions/common/extension_features.h" |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 16 | #include "extensions/test/extension_state_tester.h" |
Xinghui Lu | 259be4d | 2021-04-21 20:03:35 | [diff] [blame] | 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | |
| 19 | namespace extensions { |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | // Extension ids used during testing. |
| 24 | constexpr char kTestExtensionId[] = "behllobkkfkfnphdnhnkndlbkcpglgmj"; |
| 25 | |
| 26 | } // namespace |
| 27 | |
| 28 | // Test suite to test Omaha attribute handler. |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 29 | class OmahaAttributesHandlerUnitTest : public ExtensionServiceTestBase { |
| 30 | public: |
| 31 | OmahaAttributesHandlerUnitTest() { |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 32 | feature_list_.InitWithFeatures( |
| 33 | /*enabled_features=*/ |
| 34 | {extensions_features::kDisablePolicyViolationExtensionsRemotely, |
| 35 | extensions_features::kDisablePotentiallyUwsExtensionsRemotely}, |
| 36 | /*disabled_features=*/{}); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 37 | } |
| 38 | }; |
Xinghui Lu | 259be4d | 2021-04-21 20:03:35 | [diff] [blame] | 39 | |
| 40 | TEST_F(OmahaAttributesHandlerUnitTest, LogPolicyViolationUWSMetrics) { |
| 41 | base::HistogramTester histograms; |
| 42 | base::Value attributes(base::Value::Type::DICTIONARY); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 43 | attributes.SetBoolKey("_policy_violation", true); |
| 44 | attributes.SetBoolKey("_potentially_uws", true); |
Xinghui Lu | 259be4d | 2021-04-21 20:03:35 | [diff] [blame] | 45 | InitializeEmptyExtensionService(); |
| 46 | |
| 47 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
| 48 | |
| 49 | histograms.ExpectBucketCount( |
Xinghui Lu | 09c221f | 2021-06-12 02:43:41 | [diff] [blame] | 50 | "Extensions.ExtensionDisabledRemotely2", |
| 51 | /*sample=*/ExtensionUpdateCheckDataKey::kPotentiallyUWS, |
| 52 | /*expected_count=*/1); |
Xinghui Lu | 259be4d | 2021-04-21 20:03:35 | [diff] [blame] | 53 | histograms.ExpectBucketCount( |
Xinghui Lu | 09c221f | 2021-06-12 02:43:41 | [diff] [blame] | 54 | "Extensions.ExtensionAddDisabledRemotelyReason2", |
| 55 | /*sample=*/ExtensionUpdateCheckDataKey::kPotentiallyUWS, |
| 56 | /*expected_count=*/1); |
| 57 | histograms.ExpectBucketCount( |
| 58 | "Extensions.ExtensionDisabledRemotely2", |
| 59 | /*sample=*/ExtensionUpdateCheckDataKey::kPolicyViolation, |
| 60 | /*expected_count=*/1); |
| 61 | histograms.ExpectBucketCount( |
| 62 | "Extensions.ExtensionAddDisabledRemotelyReason2", |
| 63 | /*sample=*/ExtensionUpdateCheckDataKey::kPolicyViolation, |
| 64 | /*expected_count=*/1); |
Xinghui Lu | 259be4d | 2021-04-21 20:03:35 | [diff] [blame] | 65 | } |
| 66 | |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 67 | TEST_F(OmahaAttributesHandlerUnitTest, DisableRemotelyForPolicyViolation) { |
Xinghui Lu | 09c221f | 2021-06-12 02:43:41 | [diff] [blame] | 68 | base::HistogramTester histograms; |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 69 | InitializeGoodInstalledExtensionService(); |
| 70 | service()->Init(); |
| 71 | |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 72 | ExtensionStateTester state_tester(profile()); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 73 | |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 74 | EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId)); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 75 | |
| 76 | base::Value attributes(base::Value::Type::DICTIONARY); |
| 77 | attributes.SetBoolKey("_policy_violation", true); |
| 78 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
| 79 | |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 80 | ExtensionPrefs* prefs = ExtensionPrefs::Get(profile()); |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 81 | EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason( |
| 82 | kTestExtensionId, disable_reason::DISABLE_GREYLIST)); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 83 | EXPECT_TRUE(blocklist_prefs::HasOmahaBlocklistState( |
| 84 | kTestExtensionId, BitMapBlocklistState::BLOCKLISTED_CWS_POLICY_VIOLATION, |
| 85 | prefs)); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 86 | |
| 87 | // Remove extensions from greylist. |
| 88 | attributes.SetBoolKey("_policy_violation", false); |
| 89 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 90 | |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 91 | // The extension is re-enabled. |
| 92 | EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId)); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 93 | EXPECT_FALSE(blocklist_prefs::HasOmahaBlocklistState( |
| 94 | kTestExtensionId, BitMapBlocklistState::BLOCKLISTED_CWS_POLICY_VIOLATION, |
| 95 | prefs)); |
Xinghui Lu | 09c221f | 2021-06-12 02:43:41 | [diff] [blame] | 96 | histograms.ExpectBucketCount( |
| 97 | "Extensions.ExtensionReenabledRemotelyForPolicyViolation", |
| 98 | /*sample=*/1, |
| 99 | /*expected_count=*/1); |
| 100 | histograms.ExpectBucketCount( |
| 101 | "Extensions.ExtensionReenabledRemotelyForPotentiallyUWS", |
| 102 | /*sample=*/1, |
| 103 | /*expected_count=*/0); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 104 | } |
| 105 | |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 106 | TEST_F(OmahaAttributesHandlerUnitTest, DisableRemotelyForPotentiallyUws) { |
Xinghui Lu | 09c221f | 2021-06-12 02:43:41 | [diff] [blame] | 107 | base::HistogramTester histograms; |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 108 | InitializeGoodInstalledExtensionService(); |
| 109 | service()->Init(); |
| 110 | |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 111 | ExtensionStateTester state_tester(profile()); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 112 | |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 113 | EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId)); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 114 | |
| 115 | base::Value attributes(base::Value::Type::DICTIONARY); |
| 116 | attributes.SetBoolKey("_potentially_uws", true); |
| 117 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
| 118 | |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 119 | ExtensionPrefs* prefs = ExtensionPrefs::Get(profile()); |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 120 | EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason( |
| 121 | kTestExtensionId, disable_reason::DISABLE_GREYLIST)); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 122 | EXPECT_TRUE(blocklist_prefs::HasOmahaBlocklistState( |
| 123 | kTestExtensionId, BitMapBlocklistState::BLOCKLISTED_POTENTIALLY_UNWANTED, |
| 124 | prefs)); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 125 | |
| 126 | // Remove extensions from greylist. |
| 127 | attributes.SetBoolKey("_potentially_uws", false); |
| 128 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 129 | |
| 130 | // The extension is re-enabled. |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 131 | EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId)); |
| 132 | EXPECT_FALSE(blocklist_prefs::HasOmahaBlocklistState( |
| 133 | kTestExtensionId, BitMapBlocklistState::BLOCKLISTED_POTENTIALLY_UNWANTED, |
| 134 | prefs)); |
Xinghui Lu | 09c221f | 2021-06-12 02:43:41 | [diff] [blame] | 135 | histograms.ExpectBucketCount( |
| 136 | "Extensions.ExtensionReenabledRemotelyForPotentiallyUWS", |
| 137 | /*sample=*/1, |
| 138 | /*expected_count=*/1); |
| 139 | histograms.ExpectBucketCount( |
| 140 | "Extensions.ExtensionReenabledRemotelyForPolicyViolation", |
| 141 | /*sample=*/1, |
| 142 | /*expected_count=*/0); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | TEST_F(OmahaAttributesHandlerUnitTest, MultipleGreylistStates) { |
| 146 | InitializeGoodInstalledExtensionService(); |
| 147 | service()->Init(); |
| 148 | |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 149 | ExtensionStateTester state_tester(profile()); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 150 | |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 151 | EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId)); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 152 | |
| 153 | base::Value attributes(base::Value::Type::DICTIONARY); |
| 154 | attributes.SetBoolKey("_policy_violation", true); |
| 155 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
| 156 | |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 157 | EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason( |
| 158 | kTestExtensionId, disable_reason::DISABLE_GREYLIST)); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 159 | |
| 160 | // Now user enables kTestExtensionId. |
| 161 | service()->EnableExtension(kTestExtensionId); |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 162 | EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId)); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 163 | |
| 164 | // Another greylist state is added to Omaha attribute. |
| 165 | attributes.SetBoolKey("_potentially_uws", true); |
| 166 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
| 167 | |
| 168 | // The extension should be disabled again. |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 169 | EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason( |
| 170 | kTestExtensionId, disable_reason::DISABLE_GREYLIST)); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 171 | |
| 172 | // Remove extensions from the first greylist state. |
| 173 | attributes.SetBoolKey("_policy_violation", false); |
| 174 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
| 175 | |
| 176 | // The extension should still be disabled, because it is still in the |
| 177 | // potentially unwanted state. |
| 178 | ExtensionPrefs* prefs = ExtensionPrefs::Get(profile()); |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 179 | EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason( |
| 180 | kTestExtensionId, disable_reason::DISABLE_GREYLIST)); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 181 | EXPECT_FALSE(blocklist_prefs::HasOmahaBlocklistState( |
| 182 | kTestExtensionId, BitMapBlocklistState::BLOCKLISTED_CWS_POLICY_VIOLATION, |
| 183 | prefs)); |
| 184 | EXPECT_TRUE(blocklist_prefs::HasOmahaBlocklistState( |
| 185 | kTestExtensionId, BitMapBlocklistState::BLOCKLISTED_POTENTIALLY_UNWANTED, |
| 186 | prefs)); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 187 | |
| 188 | // Remove the other greylist state. |
| 189 | attributes.SetBoolKey("_potentially_uws", false); |
| 190 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
| 191 | |
| 192 | // The extension is re-enabled. |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 193 | EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId)); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 194 | EXPECT_FALSE(blocklist_prefs::HasOmahaBlocklistState( |
| 195 | kTestExtensionId, BitMapBlocklistState::BLOCKLISTED_POTENTIALLY_UNWANTED, |
| 196 | prefs)); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 197 | } |
| 198 | |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 199 | TEST_F(OmahaAttributesHandlerUnitTest, KeepDisabledWhenMalwareRemoved) { |
| 200 | InitializeGoodInstalledExtensionService(); |
| 201 | service()->Init(); |
| 202 | |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 203 | ExtensionStateTester state_tester(profile()); |
| 204 | EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId)); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 205 | |
| 206 | base::Value attributes(base::Value::Type::DICTIONARY); |
| 207 | attributes.SetBoolKey("_malware", true); |
| 208 | attributes.SetBoolKey("_policy_violation", true); |
| 209 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
| 210 | |
| 211 | ExtensionPrefs* prefs = ExtensionPrefs::Get(profile()); |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 212 | EXPECT_TRUE(state_tester.ExpectBlocklisted(kTestExtensionId)); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 213 | EXPECT_EQ(disable_reason::DISABLE_REMOTELY_FOR_MALWARE | |
| 214 | disable_reason::DISABLE_GREYLIST, |
| 215 | prefs->GetDisableReasons(kTestExtensionId)); |
| 216 | |
| 217 | // Remove malware. |
| 218 | attributes.SetBoolKey("_malware", false); |
| 219 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
| 220 | |
| 221 | // The extension is not enabled because the policy violation bit is not |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 222 | // cleared, but it is no longer blocklisted (instead just disabled). |
| 223 | EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason( |
| 224 | kTestExtensionId, disable_reason::DISABLE_GREYLIST)); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | // Test suite to test Omaha attribute handler when features are disabled. |
| 228 | class OmahaAttributesHandlerWithFeatureDisabledUnitTest |
| 229 | : public ExtensionServiceTestBase { |
| 230 | public: |
| 231 | OmahaAttributesHandlerWithFeatureDisabledUnitTest() { |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 232 | feature_list_.InitWithFeatures( |
| 233 | /*enabled_features=*/ |
| 234 | {}, |
| 235 | /*disabled_features=*/{ |
| 236 | extensions_features::kDisablePolicyViolationExtensionsRemotely, |
| 237 | extensions_features::kDisablePotentiallyUwsExtensionsRemotely}); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 238 | } |
| 239 | }; |
| 240 | |
| 241 | TEST_F(OmahaAttributesHandlerWithFeatureDisabledUnitTest, |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 242 | DoNotDisableRemotelyWhenFlagsDisabled) { |
Xinghui Lu | 09c221f | 2021-06-12 02:43:41 | [diff] [blame] | 243 | base::HistogramTester histograms; |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 244 | InitializeGoodInstalledExtensionService(); |
| 245 | service()->Init(); |
| 246 | |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 247 | base::Value attributes(base::Value::Type::DICTIONARY); |
| 248 | attributes.SetBoolKey("_policy_violation", true); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 249 | attributes.SetBoolKey("_potentially_uws", true); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 250 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
| 251 | |
| 252 | // Since the flag is disabled, we don't expect the extension to be affected. |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 253 | ExtensionStateTester state_tester(profile()); |
| 254 | EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId)); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 255 | EXPECT_FALSE(blocklist_prefs::HasOmahaBlocklistState( |
| 256 | kTestExtensionId, BitMapBlocklistState::BLOCKLISTED_CWS_POLICY_VIOLATION, |
| 257 | ExtensionPrefs::Get(profile()))); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 258 | EXPECT_FALSE(blocklist_prefs::HasOmahaBlocklistState( |
| 259 | kTestExtensionId, BitMapBlocklistState::BLOCKLISTED_POTENTIALLY_UNWANTED, |
| 260 | ExtensionPrefs::Get(profile()))); |
Xinghui Lu | 09c221f | 2021-06-12 02:43:41 | [diff] [blame] | 261 | // Histograms should not be logged when the flag is disabled. |
| 262 | histograms.ExpectTotalCount("Extensions.ExtensionAddDisabledRemotelyReason2", |
| 263 | /*expected_count=*/0); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 264 | } |
| 265 | |
Xinghui Lu | 259be4d | 2021-04-21 20:03:35 | [diff] [blame] | 266 | } // namespace extensions |