File: safe_browsing_verdict_handler_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (511 lines) | stat: -rw-r--r-- 20,908 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/safe_browsing_verdict_handler.h"

#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_service_test_base.h"
#include "chrome/browser/extensions/test_blocklist.h"
#include "chrome/browser/profiles/profile.h"
#include "components/safe_browsing/buildflags.h"
#include "extensions/browser/blocklist_extension_prefs.h"
#include "extensions/browser/blocklist_state.h"
#include "extensions/browser/extension_registrar.h"
#include "extensions/test/extension_state_tester.h"

// The blocklist tests rely on the safe-browsing database.
#if BUILDFLAG(SAFE_BROWSING_DB_LOCAL)
#define ENABLE_BLOCKLIST_TESTS
#endif

namespace extensions {

namespace {

// Extension ids used during testing.
constexpr char kGood0[] = "behllobkkfkfnphdnhnkndlbkcpglgmj";
constexpr char kGood1[] = "hpiknbiabeeppbpihjehijgoemciehgk";
constexpr char kGood2[] = "bjafgdebaacbbbecmhlhpofkepfkgcpa";

}  // namespace

// Test suite to test safe browsing verdict handler.
class SafeBrowsingVerdictHandlerUnitTest : public ExtensionServiceTestBase {
 protected:
  void SetBlocklistStateForExtension(const std::string& extension_id,
                                     BlocklistState state,
                                     TestBlocklist& test_blocklist) {
    // Reset cache in blocklist to make sure the latest blocklist state is
    // fetched.
    service()->blocklist_->ResetBlocklistStateCacheForTest();
    test_blocklist.SetBlocklistState(extension_id, state, true);
    task_environment()->RunUntilIdle();
  }
};

#if defined(ENABLE_BLOCKLIST_TESTS)
// Extension is added to blocklist with BLOCKLISTED_POTENTIALLY_UNWANTED state
// after it is installed. It is then successfully re-enabled by the user.
TEST_F(SafeBrowsingVerdictHandlerUnitTest, GreylistedExtensionDisabled) {
  TestBlocklist test_blocklist;
  // A profile with 3 extensions installed: kGood0, kGood1, and kGood2.
  InitializeGoodInstalledExtensionService();
  test_blocklist.Attach(service()->blocklist_);
  service()->Init();

  ExtensionStateTester state_tester(profile());

  EXPECT_TRUE(state_tester.ExpectEnabled(kGood0));
  EXPECT_TRUE(state_tester.ExpectEnabled(kGood1));
  EXPECT_TRUE(state_tester.ExpectEnabled(kGood2));

  // Add kGood0 and kGood1 (and an invalid extension ID) to greylist.
  test_blocklist.SetBlocklistState(kGood0, BLOCKLISTED_CWS_POLICY_VIOLATION,
                                   true);
  test_blocklist.SetBlocklistState(kGood1, BLOCKLISTED_POTENTIALLY_UNWANTED,
                                   true);
  test_blocklist.SetBlocklistState("invalid_id", BLOCKLISTED_MALWARE, true);
  task_environment()->RunUntilIdle();

  EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
      kGood0, disable_reason::DISABLE_GREYLIST));
  EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
      kGood1, disable_reason::DISABLE_GREYLIST));
  EXPECT_TRUE(state_tester.ExpectEnabled(kGood2));

  ValidateIntegerPref(kGood0, "blacklist_state",
                      BLOCKLISTED_CWS_POLICY_VIOLATION);
  ValidateIntegerPref(kGood1, "blacklist_state",
                      BLOCKLISTED_POTENTIALLY_UNWANTED);

  // Now user enables kGood0.
  registrar()->EnableExtension(kGood0);

  EXPECT_TRUE(state_tester.ExpectEnabled(kGood0));
  EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
      kGood1, disable_reason::DISABLE_GREYLIST));
  EXPECT_TRUE(state_tester.ExpectEnabled(kGood2));

  // Remove extensions from blocklist.
  test_blocklist.SetBlocklistState(kGood0, NOT_BLOCKLISTED, true);
  test_blocklist.SetBlocklistState(kGood1, NOT_BLOCKLISTED, true);
  task_environment()->RunUntilIdle();

  // All extensions are enabled.
  EXPECT_TRUE(state_tester.ExpectEnabled(kGood0));
  EXPECT_TRUE(state_tester.ExpectEnabled(kGood1));
  EXPECT_TRUE(state_tester.ExpectEnabled(kGood2));
}

// When extension is removed from greylist, do not re-enable it if it is
// disabled by user.
TEST_F(SafeBrowsingVerdictHandlerUnitTest, GreylistDontEnableManuallyDisabled) {
  TestBlocklist test_blocklist;
  // A profile with 3 extensions installed: kGood0, kGood1, and kGood2.
  InitializeGoodInstalledExtensionService();
  test_blocklist.Attach(service()->blocklist_);
  service()->Init();

  // Manually disable.
  registrar()->DisableExtension(kGood0, {disable_reason::DISABLE_USER_ACTION});

  test_blocklist.SetBlocklistState(kGood0, BLOCKLISTED_CWS_POLICY_VIOLATION,
                                   true);
  test_blocklist.SetBlocklistState(kGood1, BLOCKLISTED_POTENTIALLY_UNWANTED,
                                   true);
  test_blocklist.SetBlocklistState(kGood2, BLOCKLISTED_SECURITY_VULNERABILITY,
                                   true);
  task_environment()->RunUntilIdle();

  ExtensionStateTester state_tester(profile());

  // All extensions disabled.
  EXPECT_TRUE(state_tester.ExpectDisabledWithReasons(
      kGood0,
      {disable_reason::DISABLE_GREYLIST, disable_reason::DISABLE_USER_ACTION}));
  EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
      kGood1, disable_reason::DISABLE_GREYLIST));
  EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
      kGood2, disable_reason::DISABLE_GREYLIST));

  // Greylisted extension can be enabled.
  registrar()->EnableExtension(kGood1);
  EXPECT_TRUE(state_tester.ExpectEnabled(kGood1));

  // kGood1 is now manually disabled.
  registrar()->DisableExtension(kGood1, {disable_reason::DISABLE_USER_ACTION});
  EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
      kGood1, disable_reason::DISABLE_USER_ACTION));

  // Remove extensions from blocklist.
  test_blocklist.SetBlocklistState(kGood0, NOT_BLOCKLISTED, true);
  test_blocklist.SetBlocklistState(kGood1, NOT_BLOCKLISTED, true);
  test_blocklist.SetBlocklistState(kGood2, NOT_BLOCKLISTED, true);
  task_environment()->RunUntilIdle();

  // kGood0 and kGood1 remain disabled.
  EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
      kGood0, disable_reason::DISABLE_USER_ACTION));
  EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
      kGood1, disable_reason::DISABLE_USER_ACTION));
  EXPECT_TRUE(state_tester.ExpectEnabled(kGood2));
}

// Greylisted extension with unknown state are not enabled/disabled.
TEST_F(SafeBrowsingVerdictHandlerUnitTest, GreylistUnknownDontChange) {
  TestBlocklist test_blocklist;
  // A profile with 3 extensions installed: kGood0, kGood1, and kGood2.
  InitializeGoodInstalledExtensionService();
  test_blocklist.Attach(service()->blocklist_);
  service()->Init();

  test_blocklist.SetBlocklistState(kGood0, BLOCKLISTED_CWS_POLICY_VIOLATION,
                                   true);
  test_blocklist.SetBlocklistState(kGood1, BLOCKLISTED_POTENTIALLY_UNWANTED,
                                   true);
  task_environment()->RunUntilIdle();

  ExtensionStateTester state_tester(profile());

  EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
      kGood0, disable_reason::DISABLE_GREYLIST));
  EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
      kGood1, disable_reason::DISABLE_GREYLIST));
  EXPECT_TRUE(state_tester.ExpectEnabled(kGood2));

  test_blocklist.SetBlocklistState(kGood0, NOT_BLOCKLISTED, true);
  test_blocklist.SetBlocklistState(kGood1, BLOCKLISTED_UNKNOWN, true);
  test_blocklist.SetBlocklistState(kGood2, BLOCKLISTED_UNKNOWN, true);
  task_environment()->RunUntilIdle();

  // kGood0 re-enabled, other remain as they were.
  EXPECT_TRUE(state_tester.ExpectEnabled(kGood0));
  EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
      kGood1, disable_reason::DISABLE_GREYLIST));
  EXPECT_TRUE(state_tester.ExpectEnabled(kGood2));
}

// The extension is loaded but kept disabled when it is downgraded from
// blocklist to greylist.
TEST_F(SafeBrowsingVerdictHandlerUnitTest,
       UnblocklistedExtensionStillGreylisted) {
  TestBlocklist test_blocklist;
  // A profile with 3 extensions installed: kGood0, kGood1, and kGood2.
  InitializeGoodInstalledExtensionService();
  test_blocklist.Attach(service()->blocklist_);
  service()->Init();

  // Add the extension to blocklist
  SetBlocklistStateForExtension(kGood0, BLOCKLISTED_MALWARE, test_blocklist);

  ExtensionStateTester state_tester(profile());

  EXPECT_TRUE(state_tester.ExpectBlocklisted(kGood0));

  // Remove the extension from blocklist and add it to greylist
  SetBlocklistStateForExtension(kGood0, BLOCKLISTED_CWS_POLICY_VIOLATION,
                                test_blocklist);
  content::RunAllTasksUntilIdle();

  // The extension is reloaded, but remains disabled.
  EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
      kGood0, disable_reason::DISABLE_GREYLIST));
}

// When extension is on the greylist, do not disable it if it is re-enabled by
// user.
TEST_F(SafeBrowsingVerdictHandlerUnitTest,
       GreylistedExtensionDoesNotDisableAgain) {
  TestBlocklist test_blocklist;
  // A profile with 3 extensions installed: kGood0, kGood1, and kGood2.
  InitializeGoodInstalledExtensionService();
  test_blocklist.Attach(service()->blocklist_);
  service()->Init();
  ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile());

  SetBlocklistStateForExtension(kGood0, BLOCKLISTED_CWS_POLICY_VIOLATION,
                                test_blocklist);

  ExtensionStateTester state_tester(profile());

  EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
      kGood0, disable_reason::DISABLE_GREYLIST));
  EXPECT_TRUE(blocklist_prefs::HasAcknowledgedBlocklistState(
      kGood0, BitMapBlocklistState::BLOCKLISTED_CWS_POLICY_VIOLATION,
      extension_prefs));

  // Now user enables kGood0.
  registrar()->EnableExtension(kGood0);
  EXPECT_TRUE(state_tester.ExpectEnabled(kGood0));
  // The acknowledged state should not be cleared when the extension is
  // re-enabled.
  EXPECT_TRUE(blocklist_prefs::HasAcknowledgedBlocklistState(
      kGood0, BitMapBlocklistState::BLOCKLISTED_CWS_POLICY_VIOLATION,
      extension_prefs));

  // Set the blocklist to the same greylist state.
  SetBlocklistStateForExtension(kGood0, BLOCKLISTED_CWS_POLICY_VIOLATION,
                                test_blocklist);

  // kGood0 should still be enabled.
  EXPECT_TRUE(state_tester.ExpectEnabled(kGood0));
  // The acknowledged state should not be cleared.
  EXPECT_TRUE(blocklist_prefs::HasAcknowledgedBlocklistState(
      kGood0, BitMapBlocklistState::BLOCKLISTED_CWS_POLICY_VIOLATION,
      extension_prefs));
}

// When extension is removed from the greylist and re-added, disable the
// extension again.
TEST_F(SafeBrowsingVerdictHandlerUnitTest,
       GreylistedExtensionDisableAgainIfReAdded) {
  TestBlocklist test_blocklist;
  // A profile with 3 extensions installed: kGood0, kGood1, and kGood2.
  InitializeGoodInstalledExtensionService();
  test_blocklist.Attach(service()->blocklist_);
  service()->Init();
  ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile());

  SetBlocklistStateForExtension(kGood0, BLOCKLISTED_CWS_POLICY_VIOLATION,
                                test_blocklist);

  ExtensionStateTester state_tester(profile());

  // kGood0 is disabled.
  EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
      kGood0, disable_reason::DISABLE_GREYLIST));
  EXPECT_TRUE(blocklist_prefs::HasAcknowledgedBlocklistState(
      kGood0, BitMapBlocklistState::BLOCKLISTED_CWS_POLICY_VIOLATION,
      extension_prefs));

  // Now user enables kGood0.
  registrar()->EnableExtension(kGood0);
  EXPECT_TRUE(state_tester.ExpectEnabled(kGood0));
  // The acknowledged state should not be cleared when the extension is
  // re-enabled.
  EXPECT_TRUE(blocklist_prefs::HasAcknowledgedBlocklistState(
      kGood0, BitMapBlocklistState::BLOCKLISTED_CWS_POLICY_VIOLATION,
      extension_prefs));

  // Remove kGood0 from blocklist.
  SetBlocklistStateForExtension(kGood0, NOT_BLOCKLISTED, test_blocklist);

  // kGood0 should still be enabled.
  EXPECT_TRUE(state_tester.ExpectEnabled(kGood0));
  // The acknowledged state should be cleared when the extension is removed from
  // the blocklist.
  EXPECT_FALSE(blocklist_prefs::HasAcknowledgedBlocklistState(
      kGood0, BitMapBlocklistState::BLOCKLISTED_CWS_POLICY_VIOLATION,
      extension_prefs));

  // Set the blocklist to the same greylist state.
  SetBlocklistStateForExtension(kGood0, BLOCKLISTED_CWS_POLICY_VIOLATION,
                                test_blocklist);

  // kGood0 is disabled again.
  EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
      kGood0, disable_reason::DISABLE_GREYLIST));
  // The acknowledged state should be set again.
  EXPECT_TRUE(blocklist_prefs::HasAcknowledgedBlocklistState(
      kGood0, BitMapBlocklistState::BLOCKLISTED_CWS_POLICY_VIOLATION,
      extension_prefs));
}

// When extension is on the greylist, disable it again if the greylist state
// changes, even if the user has re-enabled it.
TEST_F(SafeBrowsingVerdictHandlerUnitTest,
       DisableExtensionForDifferentGreylistState) {
  TestBlocklist test_blocklist;
  // A profile with 3 extensions installed: kGood0, kGood1, and kGood2.
  InitializeGoodInstalledExtensionService();
  test_blocklist.Attach(service()->blocklist_);
  service()->Init();
  ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile());

  SetBlocklistStateForExtension(kGood0, BLOCKLISTED_CWS_POLICY_VIOLATION,
                                test_blocklist);

  ExtensionStateTester state_tester(profile());

  // kGood0 is disabled.
  EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
      kGood0, disable_reason::DISABLE_GREYLIST));
  EXPECT_TRUE(blocklist_prefs::HasAcknowledgedBlocklistState(
      kGood0, BitMapBlocklistState::BLOCKLISTED_CWS_POLICY_VIOLATION,
      extension_prefs));

  // Now user enables kGood0.
  registrar()->EnableExtension(kGood0);
  EXPECT_TRUE(state_tester.ExpectEnabled(kGood0));

  // Set the blocklist to another greylist state.
  SetBlocklistStateForExtension(kGood0, BLOCKLISTED_POTENTIALLY_UNWANTED,
                                test_blocklist);

  // The extension should be disabled again.
  EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
      kGood0, disable_reason::DISABLE_GREYLIST));
  // The old acknowledged state should be cleared and the new one should be set.
  EXPECT_FALSE(blocklist_prefs::HasAcknowledgedBlocklistState(
      kGood0, BitMapBlocklistState::BLOCKLISTED_CWS_POLICY_VIOLATION,
      extension_prefs));
  EXPECT_TRUE(blocklist_prefs::HasAcknowledgedBlocklistState(
      kGood0, BitMapBlocklistState::BLOCKLISTED_POTENTIALLY_UNWANTED,
      extension_prefs));
}

// Add the extension to greylist state1, and then switch to greylist state2, and
// then the user re-enables the extension, and then the extension is switched
// back to greylist state1, the extension should be disabled again.
TEST_F(SafeBrowsingVerdictHandlerUnitTest,
       DisableExtensionWhenSwitchingBetweenGreylistStates) {
  TestBlocklist test_blocklist;
  // A profile with 3 extensions installed: kGood0, kGood1, and kGood2.
  InitializeGoodInstalledExtensionService();
  test_blocklist.Attach(service()->blocklist_);
  service()->Init();
  ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile());

  SetBlocklistStateForExtension(kGood0, BLOCKLISTED_CWS_POLICY_VIOLATION,
                                test_blocklist);

  // Set the blocklist to another greylist state.
  SetBlocklistStateForExtension(kGood0, BLOCKLISTED_POTENTIALLY_UNWANTED,
                                test_blocklist);

  ExtensionStateTester state_tester(profile());

  // kGood0 is disabled.
  EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
      kGood0, disable_reason::DISABLE_GREYLIST));
  EXPECT_FALSE(blocklist_prefs::HasAcknowledgedBlocklistState(
      kGood0, BitMapBlocklistState::BLOCKLISTED_CWS_POLICY_VIOLATION,
      extension_prefs));
  EXPECT_TRUE(blocklist_prefs::HasAcknowledgedBlocklistState(
      kGood0, BitMapBlocklistState::BLOCKLISTED_POTENTIALLY_UNWANTED,
      extension_prefs));

  // Now user enables kGood0.
  registrar()->EnableExtension(kGood0);
  EXPECT_TRUE(state_tester.ExpectEnabled(kGood0));

  // Set the blocklist to the original blocklist state.
  SetBlocklistStateForExtension(kGood0, BLOCKLISTED_CWS_POLICY_VIOLATION,
                                test_blocklist);

  // The extension should be disabled again.
  EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
      kGood0, disable_reason::DISABLE_GREYLIST));
  // The acknowledged state should be set to the current state.
  EXPECT_TRUE(blocklist_prefs::HasAcknowledgedBlocklistState(
      kGood0, BitMapBlocklistState::BLOCKLISTED_CWS_POLICY_VIOLATION,
      extension_prefs));
  EXPECT_FALSE(blocklist_prefs::HasAcknowledgedBlocklistState(
      kGood0, BitMapBlocklistState::BLOCKLISTED_POTENTIALLY_UNWANTED,
      extension_prefs));
}

// Old greylisted extensions are not re-enabled.
// This test is for checking backward compatibility.
TEST_F(SafeBrowsingVerdictHandlerUnitTest, AcknowledgedStateBackFilled) {
  TestBlocklist test_blocklist;
  // A profile with 3 extensions installed: kGood0, kGood1, and kGood2.
  InitializeGoodInstalledExtensionService();
  test_blocklist.Attach(service()->blocklist_);
  service()->Init();
  ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile());

  SetBlocklistStateForExtension(kGood0, BLOCKLISTED_CWS_POLICY_VIOLATION,
                                test_blocklist);

  ExtensionStateTester state_tester(profile());

  // kGood0 is disabled.
  EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason(
      kGood0, disable_reason::DISABLE_GREYLIST));

  // Now user enables kGood0.
  registrar()->EnableExtension(kGood0);
  EXPECT_TRUE(state_tester.ExpectEnabled(kGood0));

  // To simulate an old Chrome version, the acknowledged state is cleared.
  blocklist_prefs::ClearAcknowledgedGreylistStates(
      kGood0, ExtensionPrefs::Get(profile()));
  // The browser is restarted.
  service()->safe_browsing_verdict_handler_.Init();

  // The acknowledged state should be restored.
  EXPECT_TRUE(blocklist_prefs::HasAcknowledgedBlocklistState(
      kGood0, BitMapBlocklistState::BLOCKLISTED_CWS_POLICY_VIOLATION,
      extension_prefs));

  // Set the blocklist to the same greylist state.
  SetBlocklistStateForExtension(kGood0, BLOCKLISTED_CWS_POLICY_VIOLATION,
                                test_blocklist);

  // kGood0 should remain enabled.
  EXPECT_TRUE(state_tester.ExpectEnabled(kGood0));
}

// Regression test for https://crbug.com/1267860. It should not crash if the
// extension is uninstalled before it is removed from the blocklist.
TEST_F(SafeBrowsingVerdictHandlerUnitTest,
       ExtensionUninstalledWhenBlocklisted) {
  TestBlocklist test_blocklist;
  // A profile with 3 extensions installed: kGood0, kGood1, and kGood2.
  InitializeGoodInstalledExtensionService();
  test_blocklist.Attach(service()->blocklist_);
  service()->Init();

  SetBlocklistStateForExtension(kGood0, BLOCKLISTED_MALWARE, test_blocklist);

  ExtensionStateTester state_tester(profile());

  // kGood0 is blocklisted.
  EXPECT_TRUE(state_tester.ExpectBlocklisted(kGood0));

  // Now uninstall kGood0.
  registrar()->UninstallExtension(kGood0, UNINSTALL_REASON_FOR_TESTING,
                                  nullptr);
  // kGood0 should be removed from the blocklist.
  EXPECT_EQ(0u, registry()->blocklisted_extensions().size());

  // Should not crash.
  SetBlocklistStateForExtension(kGood0, NOT_BLOCKLISTED, test_blocklist);
}

// Regression test for https://crbug.com/1267860. It should not crash if the
// extension is uninstalled during blocklist fetching.
TEST_F(SafeBrowsingVerdictHandlerUnitTest,
       ExtensionUninstalledWhenBlocklistFetching) {
  TestBlocklist test_blocklist;
  // A profile with 3 extensions installed: kGood0, kGood1, and kGood2.
  InitializeGoodInstalledExtensionService();
  test_blocklist.Attach(service()->blocklist_);
  service()->Init();

  SetBlocklistStateForExtension(kGood0, BLOCKLISTED_MALWARE, test_blocklist);

  ExtensionStateTester state_tester(profile());

  // kGood0 is blocklisted.
  EXPECT_TRUE(state_tester.ExpectBlocklisted(kGood0));

  service()->blocklist_->ResetBlocklistStateCacheForTest();
  // Use TestBlocklist::SetBlocklistState() here instead of
  // SetBlocklistStateForExtension(). This makes the blocklisting process
  // asynchronous, so that we can simulate uninstalling the extension
  // during a blocklist state fetch.
  test_blocklist.SetBlocklistState(kGood0, BLOCKLISTED_MALWARE, true);

  // Uninstalled the extension in the middle of the update.
  registrar()->UninstallExtension(kGood0, UNINSTALL_REASON_FOR_TESTING,
                                  nullptr);
  // Should not crash when the update finishes.
  task_environment()->RunUntilIdle();
}

#endif  // defined(ENABLE_BLOCKLIST_TESTS)

}  // namespace extensions