File: security_interstitial_tab_helper_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (546 lines) | stat: -rw-r--r-- 23,506 bytes parent folder | download | duplicates (6)
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/security_interstitials/content/security_interstitial_tab_helper.h"

#include <memory>

#include "base/functional/bind.h"
#include "base/i18n/rtl.h"
#include "base/memory/raw_ptr.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "components/security_interstitials/content/security_interstitial_controller_client.h"
#include "components/security_interstitials/content/security_interstitial_page.h"
#include "components/security_interstitials/content/settings_page_helper.h"
#include "components/security_interstitials/core/controller_client.h"
#include "components/security_interstitials/core/metrics_helper.h"
#include "content/public/browser/certificate_request_result_type.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/test/mock_navigation_handle.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/test_renderer_host.h"
#include "net/base/net_errors.h"
#include "net/test/cert_test_util.h"
#include "net/test/test_data_directory.h"
#include "third_party/blink/public/common/features.h"
#include "url/gurl.h"

namespace security_interstitials {

const char kTestSslMetricsName[] = "test_blocking_page";

std::unique_ptr<security_interstitials::MetricsHelper> CreateTestMetricsHelper(
    content::WebContents* web_contents) {
  MetricsHelper::ReportDetails report_details;
  report_details.metric_prefix = kTestSslMetricsName;
  return std::make_unique<security_interstitials::MetricsHelper>(
      GURL(), report_details, nullptr);
}

class TestInterstitialPage : public SecurityInterstitialPage {
 public:
  // |*destroyed_tracker| is set to true in the destructor.
  TestInterstitialPage(content::WebContents* web_contents,
                       const GURL& request_url,
                       bool* destroyed_tracker)
      : SecurityInterstitialPage(
            web_contents,
            request_url,
            std::make_unique<SecurityInterstitialControllerClient>(
                web_contents,
                CreateTestMetricsHelper(web_contents),
                nullptr,
                base::i18n::GetConfiguredLocale(),
                GURL(),
                /* settings_page_helper*/ nullptr)),
        destroyed_tracker_(destroyed_tracker) {}

  ~TestInterstitialPage() override { *destroyed_tracker_ = true; }

  void OnInterstitialClosing() override {}

 protected:
  void PopulateInterstitialStrings(base::Value::Dict& load_time_data) override {
  }

 private:
  raw_ptr<bool> destroyed_tracker_;
};

class SecurityInterstitialTabHelperTest
    : public content::RenderViewHostTestHarness {
 protected:
  void SetUp() override {
    RenderViewHostTestHarness::SetUp();
    content::RenderFrameHostTester::For(main_rfh())
        ->InitializeRenderFrameIfNeeded();
  }

  // Creates a navigation handle for the main frame.
  std::unique_ptr<content::MockNavigationHandle> CreateHandle(
      bool committed,
      bool is_same_document) {
    return CreateHandle(main_rfh(), committed, is_same_document);
  }

  // Creates a navigation handle for a subframe.
  std::unique_ptr<content::MockNavigationHandle> CreateSubframeHandle(
      bool committed,
      bool is_same_document) {
    content::RenderFrameHost* subframe =
        content::RenderFrameHostTester::For(main_rfh())
            ->AppendChild("subframe");
    return CreateHandle(subframe, committed, is_same_document);
  }

  // Creates a navigation handle for the given frame.
  std::unique_ptr<content::MockNavigationHandle> CreateHandle(
      content::RenderFrameHost* frame,
      bool committed,
      bool is_same_document) {
    std::unique_ptr<content::MockNavigationHandle> handle =
        std::make_unique<content::MockNavigationHandle>(GURL(), frame);
    handle->set_has_committed(committed);
    handle->set_is_same_document(is_same_document);
    return handle;
  }

  // The lifetime of the blocking page is managed by the
  // SecurityInterstitialTabHelper for the test's web_contents.
  // |destroyed_tracker| will be set to true when the corresponding blocking
  // page is destroyed.
  void CreateAssociatedBlockingPage(content::NavigationHandle* handle,
                                    bool* destroyed_tracker) {
    SecurityInterstitialTabHelper::AssociateBlockingPage(
        handle, std::make_unique<TestInterstitialPage>(web_contents(), GURL(),
                                                       destroyed_tracker));
  }
};

// Tests that the helper properly handles the lifetime of a single blocking
// page, interleaved with other navigations.
TEST_F(SecurityInterstitialTabHelperTest, SingleBlockingPage) {
  std::unique_ptr<content::MockNavigationHandle> blocking_page_handle =
      CreateHandle(true, false);
  bool blocking_page_destroyed = false;
  CreateAssociatedBlockingPage(blocking_page_handle.get(),
                               &blocking_page_destroyed);
  SecurityInterstitialTabHelper* helper =
      SecurityInterstitialTabHelper::FromWebContents(web_contents());

  // Test that a same-document navigation doesn't destroy the blocking page if
  // its navigation hasn't committed yet.
  std::unique_ptr<content::MockNavigationHandle> same_document_handle =
      CreateHandle(true, true);
  helper->DidFinishNavigation(same_document_handle.get());
  EXPECT_FALSE(blocking_page_destroyed);

  // Test that a committed (non-same-document) navigation doesn't destroy the
  // blocking page if its navigation hasn't committed yet.
  std::unique_ptr<content::MockNavigationHandle> committed_handle1 =
      CreateHandle(true, false);
  helper->DidFinishNavigation(committed_handle1.get());
  EXPECT_FALSE(blocking_page_destroyed);

  // Simulate comitting the interstitial.
  helper->DidFinishNavigation(blocking_page_handle.get());
  EXPECT_FALSE(blocking_page_destroyed);

  // Test that a subsequent committed navigation releases the blocking page
  // stored for the currently committed navigation.
  std::unique_ptr<content::MockNavigationHandle> committed_handle2 =
      CreateHandle(true, false);
  helper->DidFinishNavigation(committed_handle2.get());
  EXPECT_TRUE(blocking_page_destroyed);
}

// Tests that the helper properly handles the lifetime of multiple blocking
// pages for a single FrameTreeNode, committed in a different order than they
// are created.
TEST_F(SecurityInterstitialTabHelperTest, MultipleBlockingPages) {
  // Simulate associating the first interstitial.
  std::unique_ptr<content::MockNavigationHandle> handle1 =
      CreateHandle(true, false);
  bool blocking_page1_destroyed = false;
  CreateAssociatedBlockingPage(handle1.get(), &blocking_page1_destroyed);

  // We can directly retrieve the helper for testing once
  // CreateAssociatedBlockingPage() was called.
  SecurityInterstitialTabHelper* helper =
      SecurityInterstitialTabHelper::FromWebContents(web_contents());

  // Simulate committing the first interstitial.
  helper->DidFinishNavigation(handle1.get());
  EXPECT_FALSE(blocking_page1_destroyed);
  EXPECT_TRUE(helper->IsDisplayingInterstitial());

  // Associate the second interstitial.
  std::unique_ptr<content::MockNavigationHandle> handle2 =
      CreateHandle(true, false);
  bool blocking_page2_destroyed = false;
  CreateAssociatedBlockingPage(handle2.get(), &blocking_page2_destroyed);
  EXPECT_FALSE(blocking_page1_destroyed);
  EXPECT_FALSE(blocking_page2_destroyed);

  // Associate the third interstitial.
  std::unique_ptr<content::MockNavigationHandle> handle3 =
      CreateHandle(true, false);
  bool blocking_page3_destroyed = false;
  CreateAssociatedBlockingPage(handle3.get(), &blocking_page3_destroyed);
  EXPECT_FALSE(blocking_page1_destroyed);
  EXPECT_FALSE(blocking_page2_destroyed);
  EXPECT_FALSE(blocking_page3_destroyed);

  // Simulate committing the third interstitial.
  helper->DidFinishNavigation(handle3.get());
  EXPECT_TRUE(blocking_page1_destroyed);
  EXPECT_FALSE(blocking_page2_destroyed);
  EXPECT_FALSE(blocking_page3_destroyed);

  // Simulate commiting the second interstitial.
  helper->DidFinishNavigation(handle2.get());
  EXPECT_TRUE(blocking_page1_destroyed);
  EXPECT_FALSE(blocking_page2_destroyed);
  EXPECT_TRUE(blocking_page3_destroyed);

  // Test that a subsequent committed navigation releases the last blocking
  // page.
  std::unique_ptr<content::MockNavigationHandle> committed_handle4 =
      CreateHandle(true, false);
  helper->DidFinishNavigation(committed_handle4.get());
  EXPECT_TRUE(blocking_page2_destroyed);
}

// Tests that the helper properly handles a navigation that finishes without
// committing.
TEST_F(SecurityInterstitialTabHelperTest, NavigationDoesNotCommit) {
  std::unique_ptr<content::MockNavigationHandle> committed_handle =
      CreateHandle(true, false);
  bool committed_blocking_page_destroyed = false;
  CreateAssociatedBlockingPage(committed_handle.get(),
                               &committed_blocking_page_destroyed);
  SecurityInterstitialTabHelper* helper =
      SecurityInterstitialTabHelper::FromWebContents(web_contents());
  helper->DidFinishNavigation(committed_handle.get());
  EXPECT_FALSE(committed_blocking_page_destroyed);

  // Simulate a navigation that does not commit.
  std::unique_ptr<content::MockNavigationHandle> non_committed_handle =
      CreateHandle(false, false);
  helper->DidFinishNavigation(non_committed_handle.get());

  // The blocking page for the non-committed navigation should have been cleaned
  // up, but the one for the previous committed navigation should still be
  // around.
  EXPECT_FALSE(committed_blocking_page_destroyed);

  // When a navigation does commit, the previous one should be cleaned up.
  std::unique_ptr<content::MockNavigationHandle> next_committed_handle =
      CreateHandle(true, false);
  helper->DidFinishNavigation(next_committed_handle.get());
  EXPECT_TRUE(committed_blocking_page_destroyed);
}

// Tests that main frame committed blocking interstitial is cleaned up on tab
// closing.
TEST_F(SecurityInterstitialTabHelperTest, ClosingTabForMainFrameInterstitial) {
  std::unique_ptr<content::MockNavigationHandle> committed_handle =
      CreateHandle(true, false);
  bool committed_blocking_page_destroyed = false;
  CreateAssociatedBlockingPage(committed_handle.get(),
                               &committed_blocking_page_destroyed);
  SecurityInterstitialTabHelper* helper =
      SecurityInterstitialTabHelper::FromWebContents(web_contents());
  helper->DidFinishNavigation(committed_handle.get());
  EXPECT_FALSE(committed_blocking_page_destroyed);

  // Only subframe interstitials are destroyed on frame deletion.
  helper->FrameDeleted(main_rfh()->GetFrameTreeNodeId());
  EXPECT_FALSE(committed_blocking_page_destroyed);

  helper->WebContentsDestroyed();
  EXPECT_TRUE(committed_blocking_page_destroyed);
}

// Tests that multiple FrameTreeNodes can display blocking interstitials.
TEST_F(SecurityInterstitialTabHelperTest, MultipleFramesBlocked) {
  // Simulate associating the first interstitial.
  std::unique_ptr<content::MockNavigationHandle> handle1 =
      CreateSubframeHandle(true, false);
  bool blocking_page1_destroyed = false;
  CreateAssociatedBlockingPage(handle1.get(), &blocking_page1_destroyed);

  // Simulate associating the second interstitial to another frame.
  std::unique_ptr<content::MockNavigationHandle> handle2 =
      CreateSubframeHandle(true, false);
  bool blocking_page2_destroyed = false;
  CreateAssociatedBlockingPage(handle2.get(), &blocking_page2_destroyed);

  // We can directly retrieve the helper for testing once
  // CreateAssociatedBlockingPage() was called.
  SecurityInterstitialTabHelper* helper =
      SecurityInterstitialTabHelper::FromWebContents(web_contents());

  // Simulate committing the first interstitial.
  helper->DidFinishNavigation(handle1.get());
  EXPECT_FALSE(blocking_page1_destroyed);
  EXPECT_FALSE(blocking_page2_destroyed);

  // The interstitial at frame 1 moved from pending to commit state.
  EXPECT_TRUE(
      helper->IsInterstitialCommittedForFrame(handle1->GetFrameTreeNodeId()));
  EXPECT_FALSE(
      helper->IsInterstitialCommittedForFrame(handle2->GetFrameTreeNodeId()));
  EXPECT_FALSE(
      helper->IsInterstitialPendingForNavigation(handle1->GetNavigationId()));
  EXPECT_TRUE(
      helper->IsInterstitialPendingForNavigation(handle2->GetNavigationId()));

  // Simulate committing the second interstitial.
  helper->DidFinishNavigation(handle2.get());
  EXPECT_FALSE(blocking_page1_destroyed);
  EXPECT_FALSE(blocking_page2_destroyed);

  // The interstitial at frame 2 moved from pending to commit state.
  EXPECT_TRUE(
      helper->IsInterstitialCommittedForFrame(handle1->GetFrameTreeNodeId()));
  EXPECT_TRUE(
      helper->IsInterstitialCommittedForFrame(handle2->GetFrameTreeNodeId()));
  EXPECT_FALSE(
      helper->IsInterstitialPendingForNavigation(handle1->GetNavigationId()));
  EXPECT_FALSE(
      helper->IsInterstitialPendingForNavigation(handle2->GetNavigationId()));

  helper->FrameDeleted(handle1->GetFrameTreeNodeId());
  EXPECT_TRUE(blocking_page1_destroyed);

  helper->FrameDeleted(handle2->GetFrameTreeNodeId());
  EXPECT_TRUE(blocking_page2_destroyed);
}

// Tests that navigation events associated to one FrameTreeNode do not affect
// the pending interstitial for another FrameTreeNode.
TEST_F(SecurityInterstitialTabHelperTest,
       NavigationForSubframeWithPendingInterstitial) {
  content::RenderFrameHost* subframe1 =
      content::RenderFrameHostTester::For(main_rfh())
          ->AppendChild("subframe 1");
  content::RenderFrameHost* subframe2 =
      content::RenderFrameHostTester::For(main_rfh())
          ->AppendChild("subframe 2");

  std::unique_ptr<content::MockNavigationHandle> committed_handle =
      CreateHandle(subframe1, true, false);
  bool committed_blocking_page_destroyed = false;
  CreateAssociatedBlockingPage(committed_handle.get(),
                               &committed_blocking_page_destroyed);

  std::unique_ptr<content::MockNavigationHandle> pending_handle =
      CreateHandle(subframe2, true, false);
  bool pending_blocking_page_destroyed = false;
  CreateAssociatedBlockingPage(pending_handle.get(),
                               &pending_blocking_page_destroyed);

  // Simulate committing the interstitial for frame 1.
  SecurityInterstitialTabHelper* helper =
      SecurityInterstitialTabHelper::FromWebContents(web_contents());
  helper->DidFinishNavigation(committed_handle.get());
  EXPECT_FALSE(committed_blocking_page_destroyed);
  EXPECT_FALSE(pending_blocking_page_destroyed);

  // The interstitial at frame 1 moved from pending to commit state.
  EXPECT_TRUE(helper->IsInterstitialCommittedForFrame(
      committed_handle->GetFrameTreeNodeId()));
  EXPECT_FALSE(helper->IsInterstitialCommittedForFrame(
      pending_handle->GetFrameTreeNodeId()));
  EXPECT_FALSE(helper->IsInterstitialPendingForNavigation(
      committed_handle->GetNavigationId()));
  EXPECT_TRUE(helper->IsInterstitialPendingForNavigation(
      pending_handle->GetNavigationId()));

  // Simulate a navigation that does not commit.
  std::unique_ptr<content::MockNavigationHandle> non_committed_handle =
      CreateHandle(subframe1, false, false);
  helper->DidFinishNavigation(non_committed_handle.get());

  // The non-committed navigation should not affect the state of committed or
  // pending blocking pages.
  EXPECT_FALSE(committed_blocking_page_destroyed);
  EXPECT_FALSE(pending_blocking_page_destroyed);
  EXPECT_TRUE(helper->IsInterstitialCommittedForFrame(
      committed_handle->GetFrameTreeNodeId()));
  EXPECT_FALSE(helper->IsInterstitialCommittedForFrame(
      pending_handle->GetFrameTreeNodeId()));
  EXPECT_FALSE(helper->IsInterstitialPendingForNavigation(
      committed_handle->GetNavigationId()));
  EXPECT_TRUE(helper->IsInterstitialPendingForNavigation(
      pending_handle->GetNavigationId()));

  // When a navigation does commit, the previous one should be cleaned up.
  std::unique_ptr<content::MockNavigationHandle> next_committed_handle =
      CreateHandle(subframe1, true, false);
  helper->DidFinishNavigation(next_committed_handle.get());
  EXPECT_TRUE(committed_blocking_page_destroyed);
  EXPECT_FALSE(helper->IsInterstitialCommittedForFrame(
      committed_handle->GetFrameTreeNodeId()));
  EXPECT_FALSE(helper->IsInterstitialPendingForNavigation(
      committed_handle->GetNavigationId()));

  // The pending interstitial should still be present.
  EXPECT_FALSE(pending_blocking_page_destroyed);
  EXPECT_TRUE(helper->IsInterstitialPendingForNavigation(
      pending_handle->GetNavigationId()));
  EXPECT_FALSE(helper->IsInterstitialCommittedForFrame(
      pending_handle->GetFrameTreeNodeId()));

  // Frame deletions only affect committed interstitials.
  helper->FrameDeleted(pending_handle->GetFrameTreeNodeId());
  EXPECT_FALSE(pending_blocking_page_destroyed);

  // Delete pending interstitials via web contents destruction.
  DeleteContents();
  EXPECT_TRUE(pending_blocking_page_destroyed);
}

// Tests that navigation events associated to one FrameTreeNode do not affect
// the committed interstitial for another FrameTreeNode.
TEST_F(SecurityInterstitialTabHelperTest,
       NavigationForSubframeWithCommittedInterstitial) {
  content::RenderFrameHost* subframe1 =
      content::RenderFrameHostTester::For(main_rfh())
          ->AppendChild("subframe 1");
  content::RenderFrameHost* subframe2 =
      content::RenderFrameHostTester::For(main_rfh())
          ->AppendChild("subframe 2");

  std::unique_ptr<content::MockNavigationHandle> committed_handle =
      CreateHandle(subframe1, true, false);
  bool committed_blocking_page_destroyed = false;
  CreateAssociatedBlockingPage(committed_handle.get(),
                               &committed_blocking_page_destroyed);

  std::unique_ptr<content::MockNavigationHandle> committed_handle2 =
      CreateHandle(subframe2, true, false);
  bool committed_blocking_page2_destroyed = false;
  CreateAssociatedBlockingPage(committed_handle2.get(),
                               &committed_blocking_page2_destroyed);

  // Simulate committing the interstitial for frame 1.
  SecurityInterstitialTabHelper* helper =
      SecurityInterstitialTabHelper::FromWebContents(web_contents());
  helper->DidFinishNavigation(committed_handle.get());
  EXPECT_FALSE(committed_blocking_page_destroyed);
  EXPECT_FALSE(committed_blocking_page2_destroyed);

  // Simulate committing the interstitial for frame 2.
  helper->DidFinishNavigation(committed_handle2.get());
  EXPECT_FALSE(committed_blocking_page_destroyed);
  EXPECT_FALSE(committed_blocking_page2_destroyed);

  // Both interstitials have moved from pending to commit state.
  EXPECT_TRUE(helper->IsInterstitialCommittedForFrame(
      committed_handle->GetFrameTreeNodeId()));
  EXPECT_TRUE(helper->IsInterstitialCommittedForFrame(
      committed_handle2->GetFrameTreeNodeId()));
  EXPECT_FALSE(helper->IsInterstitialPendingForNavigation(
      committed_handle->GetNavigationId()));
  EXPECT_FALSE(helper->IsInterstitialPendingForNavigation(
      committed_handle2->GetNavigationId()));

  // Simulate a navigation that does not commit.
  std::unique_ptr<content::MockNavigationHandle> non_committed_handle =
      CreateHandle(subframe1, false, false);
  helper->DidFinishNavigation(non_committed_handle.get());

  // The non-committed navigation should not affect the state of committed or
  // pending blocking pages.
  EXPECT_FALSE(committed_blocking_page_destroyed);
  EXPECT_FALSE(committed_blocking_page2_destroyed);
  EXPECT_TRUE(helper->IsInterstitialCommittedForFrame(
      committed_handle->GetFrameTreeNodeId()));
  EXPECT_TRUE(helper->IsInterstitialCommittedForFrame(
      committed_handle2->GetFrameTreeNodeId()));
  EXPECT_FALSE(helper->IsInterstitialPendingForNavigation(
      committed_handle->GetNavigationId()));
  EXPECT_FALSE(helper->IsInterstitialPendingForNavigation(
      committed_handle2->GetNavigationId()));

  // When a navigation does commit, the previous one should be cleaned up.
  std::unique_ptr<content::MockNavigationHandle> next_committed_handle =
      CreateHandle(subframe1, true, false);
  helper->DidFinishNavigation(next_committed_handle.get());
  EXPECT_TRUE(committed_blocking_page_destroyed);
  EXPECT_FALSE(helper->IsInterstitialCommittedForFrame(
      committed_handle->GetFrameTreeNodeId()));
  EXPECT_FALSE(helper->IsInterstitialPendingForNavigation(
      committed_handle->GetNavigationId()));

  // The pending interstitial should still be present.
  EXPECT_FALSE(committed_blocking_page2_destroyed);
  EXPECT_TRUE(helper->IsInterstitialCommittedForFrame(
      committed_handle2->GetFrameTreeNodeId()));

  helper->FrameDeleted(committed_handle2->GetFrameTreeNodeId());
  EXPECT_TRUE(committed_blocking_page2_destroyed);
}

class SecurityInterstitialTabHelperFencedFrameTest
    : public SecurityInterstitialTabHelperTest {
 public:
  SecurityInterstitialTabHelperFencedFrameTest() {
    scoped_feature_list_.InitAndEnableFeatureWithParameters(
        blink::features::kFencedFrames, {{"implementation_type", "mparch"}});
  }
  ~SecurityInterstitialTabHelperFencedFrameTest() override = default;

  content::RenderFrameHost* CreateFencedFrame(
      content::RenderFrameHost* parent) {
    content::RenderFrameHost* fenced_frame =
        content::RenderFrameHostTester::For(parent)->AppendFencedFrame();
    return fenced_frame;
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

// Tests that a FencedFrame does not close the interstitial page.
TEST_F(SecurityInterstitialTabHelperFencedFrameTest,
       FencedFrameDoesNotCloseInterstitialPage) {
  std::unique_ptr<content::NavigationHandle> blocking_page_handle =
      CreateHandle(true, false);
  bool blocking_page_destroyed = false;
  CreateAssociatedBlockingPage(blocking_page_handle.get(),
                               &blocking_page_destroyed);
  SecurityInterstitialTabHelper* helper =
      SecurityInterstitialTabHelper::FromWebContents(web_contents());
  helper->DidFinishNavigation(blocking_page_handle.get());
  EXPECT_FALSE(blocking_page_destroyed);
  EXPECT_TRUE(helper->IsDisplayingInterstitial());

  // Navigate a fenced frame and the interstitial page should be kept visible on
  // the fenced frame.
  GURL fenced_frame_url = GURL("https://fencedframe.com");
  content::RenderFrameHostTester::For(main_rfh())
      ->InitializeRenderFrameIfNeeded();
  content::RenderFrameHost* fenced_frame_rfh = CreateFencedFrame(main_rfh());
  std::unique_ptr<content::NavigationSimulator> navigation_simulator =
      content::NavigationSimulator::CreateRendererInitiated(fenced_frame_url,
                                                            fenced_frame_rfh);
  navigation_simulator->Commit();
  fenced_frame_rfh = navigation_simulator->GetFinalRenderFrameHost();
  EXPECT_TRUE(fenced_frame_rfh->IsFencedFrameRoot());
  EXPECT_FALSE(blocking_page_destroyed);
  EXPECT_TRUE(helper->IsDisplayingInterstitial());

  // When a navigation does commit, the fenced frame one should be cleaned up.
  std::unique_ptr<content::NavigationHandle> next_committed_handle =
      CreateHandle(true, false);
  helper->DidFinishNavigation(next_committed_handle.get());
  EXPECT_TRUE(blocking_page_destroyed);
  EXPECT_FALSE(helper->IsDisplayingInterstitial());
}

}  // namespace security_interstitials