blob: 06e4c2272a02624520c3a63d6a698e5e1a5944d7 [file] [log] [blame]
Xinghui Lu259be4d2021-04-21 20:03:351// 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 Lu778ea582021-05-18 23:47:318#include "base/test/scoped_feature_list.h"
Xinghui Lu259be4d2021-04-21 20:03:359#include "base/values.h"
Xinghui Lu259be4d2021-04-21 20:03:3510#include "chrome/browser/extensions/extension_service.h"
11#include "chrome/browser/extensions/extension_service_test_base.h"
Xinghui Lu574a0672021-07-15 19:54:4912#include "extensions/browser/blocklist_extension_prefs.h"
Xinghui Lubfcb30a2021-05-19 20:49:1613#include "extensions/browser/disable_reason.h"
Xinghui Lu778ea582021-05-18 23:47:3114#include "extensions/browser/extension_prefs.h"
15#include "extensions/common/extension_features.h"
Devlin Cronind025bf82021-05-20 19:42:2716#include "extensions/test/extension_state_tester.h"
Xinghui Lu259be4d2021-04-21 20:03:3517#include "testing/gtest/include/gtest/gtest.h"
18
19namespace extensions {
20
21namespace {
22
23// Extension ids used during testing.
24constexpr char kTestExtensionId[] = "behllobkkfkfnphdnhnkndlbkcpglgmj";
25
26} // namespace
27
28// Test suite to test Omaha attribute handler.
Xinghui Lu778ea582021-05-18 23:47:3129class OmahaAttributesHandlerUnitTest : public ExtensionServiceTestBase {
30 public:
31 OmahaAttributesHandlerUnitTest() {
Xinghui Lubfcb30a2021-05-19 20:49:1632 feature_list_.InitWithFeatures(
33 /*enabled_features=*/
34 {extensions_features::kDisablePolicyViolationExtensionsRemotely,
35 extensions_features::kDisablePotentiallyUwsExtensionsRemotely},
36 /*disabled_features=*/{});
Xinghui Lu778ea582021-05-18 23:47:3137 }
38};
Xinghui Lu259be4d2021-04-21 20:03:3539
40TEST_F(OmahaAttributesHandlerUnitTest, LogPolicyViolationUWSMetrics) {
41 base::HistogramTester histograms;
42 base::Value attributes(base::Value::Type::DICTIONARY);
Xinghui Lu778ea582021-05-18 23:47:3143 attributes.SetBoolKey("_policy_violation", true);
44 attributes.SetBoolKey("_potentially_uws", true);
Xinghui Lu259be4d2021-04-21 20:03:3545 InitializeEmptyExtensionService();
46
47 service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes);
48
49 histograms.ExpectBucketCount(
Xinghui Lu09c221f2021-06-12 02:43:4150 "Extensions.ExtensionDisabledRemotely2",
51 /*sample=*/ExtensionUpdateCheckDataKey::kPotentiallyUWS,
52 /*expected_count=*/1);
Xinghui Lu259be4d2021-04-21 20:03:3553 histograms.ExpectBucketCount(
Xinghui Lu09c221f2021-06-12 02:43:4154 "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 Lu259be4d2021-04-21 20:03:3565}
66
Xinghui Lu778ea582021-05-18 23:47:3167TEST_F(OmahaAttributesHandlerUnitTest, DisableRemotelyForPolicyViolation) {
Xinghui Lu09c221f2021-06-12 02:43:4168 base::HistogramTester histograms;
Xinghui Lu778ea582021-05-18 23:47:3169 InitializeGoodInstalledExtensionService();
70 service()->Init();
71
Devlin Cronind025bf82021-05-20 19:42:2772 ExtensionStateTester state_tester(profile());
Xinghui Lu778ea582021-05-18 23:47:3173
Devlin Cronind025bf82021-05-20 19:42:2774 EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId));
Xinghui Lu778ea582021-05-18 23:47:3175
76 base::Value attributes(base::Value::Type::DICTIONARY);
77 attributes.SetBoolKey("_policy_violation", true);
78 service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes);
79
Xinghui Lu778ea582021-05-18 23:47:3180 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
Devlin Cronind025bf82021-05-20 19:42:2781 EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
82 kTestExtensionId, disable_reason::DISABLE_GREYLIST));
Xinghui Lu778ea582021-05-18 23:47:3183 EXPECT_TRUE(blocklist_prefs::HasOmahaBlocklistState(
84 kTestExtensionId, BitMapBlocklistState::BLOCKLISTED_CWS_POLICY_VIOLATION,
85 prefs));
Xinghui Lu778ea582021-05-18 23:47:3186
87 // Remove extensions from greylist.
88 attributes.SetBoolKey("_policy_violation", false);
89 service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes);
Xinghui Lu778ea582021-05-18 23:47:3190
Devlin Cronind025bf82021-05-20 19:42:2791 // The extension is re-enabled.
92 EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId));
Xinghui Lu778ea582021-05-18 23:47:3193 EXPECT_FALSE(blocklist_prefs::HasOmahaBlocklistState(
94 kTestExtensionId, BitMapBlocklistState::BLOCKLISTED_CWS_POLICY_VIOLATION,
95 prefs));
Xinghui Lu09c221f2021-06-12 02:43:4196 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 Lu778ea582021-05-18 23:47:31104}
105
Xinghui Lubfcb30a2021-05-19 20:49:16106TEST_F(OmahaAttributesHandlerUnitTest, DisableRemotelyForPotentiallyUws) {
Xinghui Lu09c221f2021-06-12 02:43:41107 base::HistogramTester histograms;
Xinghui Lubfcb30a2021-05-19 20:49:16108 InitializeGoodInstalledExtensionService();
109 service()->Init();
110
Devlin Cronind025bf82021-05-20 19:42:27111 ExtensionStateTester state_tester(profile());
Xinghui Lubfcb30a2021-05-19 20:49:16112
Devlin Cronind025bf82021-05-20 19:42:27113 EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId));
Xinghui Lubfcb30a2021-05-19 20:49:16114
115 base::Value attributes(base::Value::Type::DICTIONARY);
116 attributes.SetBoolKey("_potentially_uws", true);
117 service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes);
118
Xinghui Lubfcb30a2021-05-19 20:49:16119 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
Devlin Cronind025bf82021-05-20 19:42:27120 EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
121 kTestExtensionId, disable_reason::DISABLE_GREYLIST));
Xinghui Lubfcb30a2021-05-19 20:49:16122 EXPECT_TRUE(blocklist_prefs::HasOmahaBlocklistState(
123 kTestExtensionId, BitMapBlocklistState::BLOCKLISTED_POTENTIALLY_UNWANTED,
124 prefs));
Xinghui Lubfcb30a2021-05-19 20:49:16125
126 // Remove extensions from greylist.
127 attributes.SetBoolKey("_potentially_uws", false);
128 service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes);
Xinghui Lubfcb30a2021-05-19 20:49:16129
130 // The extension is re-enabled.
Devlin Cronind025bf82021-05-20 19:42:27131 EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId));
132 EXPECT_FALSE(blocklist_prefs::HasOmahaBlocklistState(
133 kTestExtensionId, BitMapBlocklistState::BLOCKLISTED_POTENTIALLY_UNWANTED,
134 prefs));
Xinghui Lu09c221f2021-06-12 02:43:41135 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 Lubfcb30a2021-05-19 20:49:16143}
144
145TEST_F(OmahaAttributesHandlerUnitTest, MultipleGreylistStates) {
146 InitializeGoodInstalledExtensionService();
147 service()->Init();
148
Devlin Cronind025bf82021-05-20 19:42:27149 ExtensionStateTester state_tester(profile());
Xinghui Lubfcb30a2021-05-19 20:49:16150
Devlin Cronind025bf82021-05-20 19:42:27151 EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId));
Xinghui Lubfcb30a2021-05-19 20:49:16152
153 base::Value attributes(base::Value::Type::DICTIONARY);
154 attributes.SetBoolKey("_policy_violation", true);
155 service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes);
156
Devlin Cronind025bf82021-05-20 19:42:27157 EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
158 kTestExtensionId, disable_reason::DISABLE_GREYLIST));
Xinghui Lubfcb30a2021-05-19 20:49:16159
160 // Now user enables kTestExtensionId.
161 service()->EnableExtension(kTestExtensionId);
Devlin Cronind025bf82021-05-20 19:42:27162 EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId));
Xinghui Lubfcb30a2021-05-19 20:49:16163
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 Cronind025bf82021-05-20 19:42:27169 EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
170 kTestExtensionId, disable_reason::DISABLE_GREYLIST));
Xinghui Lubfcb30a2021-05-19 20:49:16171
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 Cronind025bf82021-05-20 19:42:27179 EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
180 kTestExtensionId, disable_reason::DISABLE_GREYLIST));
Xinghui Lubfcb30a2021-05-19 20:49:16181 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 Lubfcb30a2021-05-19 20:49:16187
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 Cronind025bf82021-05-20 19:42:27193 EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId));
Xinghui Lubfcb30a2021-05-19 20:49:16194 EXPECT_FALSE(blocklist_prefs::HasOmahaBlocklistState(
195 kTestExtensionId, BitMapBlocklistState::BLOCKLISTED_POTENTIALLY_UNWANTED,
196 prefs));
Xinghui Lubfcb30a2021-05-19 20:49:16197}
198
Xinghui Lu778ea582021-05-18 23:47:31199TEST_F(OmahaAttributesHandlerUnitTest, KeepDisabledWhenMalwareRemoved) {
200 InitializeGoodInstalledExtensionService();
201 service()->Init();
202
Devlin Cronind025bf82021-05-20 19:42:27203 ExtensionStateTester state_tester(profile());
204 EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId));
Xinghui Lu778ea582021-05-18 23:47:31205
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 Cronind025bf82021-05-20 19:42:27212 EXPECT_TRUE(state_tester.ExpectBlocklisted(kTestExtensionId));
Xinghui Lu778ea582021-05-18 23:47:31213 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 Cronind025bf82021-05-20 19:42:27222 // cleared, but it is no longer blocklisted (instead just disabled).
223 EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
224 kTestExtensionId, disable_reason::DISABLE_GREYLIST));
Xinghui Lu778ea582021-05-18 23:47:31225}
226
227// Test suite to test Omaha attribute handler when features are disabled.
228class OmahaAttributesHandlerWithFeatureDisabledUnitTest
229 : public ExtensionServiceTestBase {
230 public:
231 OmahaAttributesHandlerWithFeatureDisabledUnitTest() {
Xinghui Lubfcb30a2021-05-19 20:49:16232 feature_list_.InitWithFeatures(
233 /*enabled_features=*/
234 {},
235 /*disabled_features=*/{
236 extensions_features::kDisablePolicyViolationExtensionsRemotely,
237 extensions_features::kDisablePotentiallyUwsExtensionsRemotely});
Xinghui Lu778ea582021-05-18 23:47:31238 }
239};
240
241TEST_F(OmahaAttributesHandlerWithFeatureDisabledUnitTest,
Xinghui Lubfcb30a2021-05-19 20:49:16242 DoNotDisableRemotelyWhenFlagsDisabled) {
Xinghui Lu09c221f2021-06-12 02:43:41243 base::HistogramTester histograms;
Xinghui Lu778ea582021-05-18 23:47:31244 InitializeGoodInstalledExtensionService();
245 service()->Init();
246
Xinghui Lu778ea582021-05-18 23:47:31247 base::Value attributes(base::Value::Type::DICTIONARY);
248 attributes.SetBoolKey("_policy_violation", true);
Xinghui Lubfcb30a2021-05-19 20:49:16249 attributes.SetBoolKey("_potentially_uws", true);
Xinghui Lu778ea582021-05-18 23:47:31250 service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes);
251
252 // Since the flag is disabled, we don't expect the extension to be affected.
Devlin Cronind025bf82021-05-20 19:42:27253 ExtensionStateTester state_tester(profile());
254 EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId));
Xinghui Lu778ea582021-05-18 23:47:31255 EXPECT_FALSE(blocklist_prefs::HasOmahaBlocklistState(
256 kTestExtensionId, BitMapBlocklistState::BLOCKLISTED_CWS_POLICY_VIOLATION,
257 ExtensionPrefs::Get(profile())));
Xinghui Lubfcb30a2021-05-19 20:49:16258 EXPECT_FALSE(blocklist_prefs::HasOmahaBlocklistState(
259 kTestExtensionId, BitMapBlocklistState::BLOCKLISTED_POTENTIALLY_UNWANTED,
260 ExtensionPrefs::Get(profile())));
Xinghui Lu09c221f2021-06-12 02:43:41261 // Histograms should not be logged when the flag is disabled.
262 histograms.ExpectTotalCount("Extensions.ExtensionAddDisabledRemotelyReason2",
263 /*expected_count=*/0);
Xinghui Lu778ea582021-05-18 23:47:31264}
265
Xinghui Lu259be4d2021-04-21 20:03:35266} // namespace extensions