File: known_interception_disclosure_infobar_browsertest.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 (165 lines) | stat: -rw-r--r-- 6,407 bytes parent folder | download | duplicates (4)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/files/file_util.h"
#include "base/test/simple_test_clock.h"
#include "base/threading/thread_restrictions.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ssl/known_interception_disclosure_infobar_delegate.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_enums.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/infobars/content/content_infobar_manager.h"
#include "components/infobars/core/confirm_infobar_delegate.h"
#include "components/infobars/core/infobar.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "net/cert/crl_set.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/test_data_directory.h"
#include "services/cert_verifier/public/mojom/cert_verifier_service_factory.mojom.h"
#include "ui/base/window_open_disposition.h"

namespace {

size_t GetInfobarCount(content::WebContents* contents) {
  infobars::ContentInfoBarManager* infobar_manager =
      infobars::ContentInfoBarManager::FromWebContents(contents);
  return infobar_manager ? infobar_manager->infobars().size() : 0;
}

infobars::InfoBar* GetInfobar(content::WebContents* contents) {
  infobars::ContentInfoBarManager* infobar_manager =
      infobars::ContentInfoBarManager::FromWebContents(contents);
  DCHECK(infobar_manager);
  return infobar_manager->infobars()[0];
}

// Follows same logic as clicking the "Continue" button would.
void CloseInfobar(content::WebContents* contents) {
  infobars::InfoBar* infobar = GetInfobar(contents);
  if (!infobar)
    return;

  ASSERT_TRUE(
      static_cast<ConfirmInfoBarDelegate*>(infobar->delegate())->Accept());
  infobar->RemoveSelf();
}

}  // namespace

class KnownInterceptionDisclosureInfobarTest : public InProcessBrowserTest {
 public:
  KnownInterceptionDisclosureInfobarTest()
      : https_server_(net::EmbeddedTestServer::TYPE_HTTPS) {
    https_server_.AddDefaultHandlers(GetChromeTestDataDir());
  }

  KnownInterceptionDisclosureInfobarTest(
      const KnownInterceptionDisclosureInfobarTest&) = delete;
  KnownInterceptionDisclosureInfobarTest& operator=(
      const KnownInterceptionDisclosureInfobarTest&) = delete;

  void SetUpOnMainThread() override {
    ASSERT_TRUE(https_server_.Start());

    // Load a CRLSet that marks the root as a known MITM.
    std::string crl_set_bytes;
    {
      base::ScopedAllowBlockingForTesting allow_blocking;
      base::ReadFileToString(net::GetTestCertsDirectory().AppendASCII(
                                 "crlset_known_interception_by_root.raw"),
                             &crl_set_bytes);
    }
    base::RunLoop run_loop;
    content::GetCertVerifierServiceFactory()->UpdateCRLSet(
        base::as_byte_span(crl_set_bytes), run_loop.QuitClosure());
    run_loop.Run();
  }

 protected:
  net::EmbeddedTestServer https_server_;
};

IN_PROC_BROWSER_TEST_F(KnownInterceptionDisclosureInfobarTest,
                       OnlyShowDisclosureOncePerSession) {
  const GURL kInterceptedUrl(https_server_.GetURL("/ssl/google.html"));

  TabStripModel* tab_strip_model = browser()->tab_strip_model();
  content::WebContents* tab1 = tab_strip_model->GetActiveWebContents();

  auto* clock = new base::SimpleTestClock();
  clock->SetNow(base::Time::Now());
  KnownInterceptionDisclosureCooldown::GetInstance()->SetClockForTesting(
      std::unique_ptr<base::Clock>(clock));

  // Trigger the disclosure infobar by navigating to a page served by a root
  // marked as known interception.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), kInterceptedUrl));
  EXPECT_EQ(1u, GetInfobarCount(tab1));

  // Test that the infobar is shown on new tabs after it has been triggered
  // once.
  ui_test_utils::NavigateToURLWithDisposition(
      browser(), GURL("about:blank"), WindowOpenDisposition::NEW_FOREGROUND_TAB,
      ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
  content::WebContents* tab2 = tab_strip_model->GetActiveWebContents();
  EXPECT_EQ(1u, GetInfobarCount(tab2));

  // Close the new tab.
  tab_strip_model->CloseWebContentsAt(tab_strip_model->active_index(),
                                      TabCloseTypes::CLOSE_USER_GESTURE);

  // Reload the first page -- infobar should still show.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), kInterceptedUrl));
  EXPECT_EQ(1u, GetInfobarCount(tab1));

  // Dismiss the infobar.
  CloseInfobar(tab1);
  EXPECT_EQ(0u, GetInfobarCount(tab1));

  // Try to trigger again by reloading the page -- infobar should not show.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), kInterceptedUrl));
  EXPECT_EQ(0u, GetInfobarCount(tab1));

  // Move clock ahead 8 days.
  clock->Advance(base::Days(8));

  // Trigger the infobar again -- infobar should show again.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), kInterceptedUrl));
  EXPECT_EQ(1u, GetInfobarCount(tab1));
}

IN_PROC_BROWSER_TEST_F(KnownInterceptionDisclosureInfobarTest,
                       PRE_CooldownResetsOnBrowserRestartDesktop) {
  const GURL kInterceptedUrl(https_server_.GetURL("/ssl/google.html"));

  // Trigger the disclosure infobar.
  content::WebContents* tab =
      browser()->tab_strip_model()->GetActiveWebContents();
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), kInterceptedUrl));
  EXPECT_EQ(1u, GetInfobarCount(tab));

  // Dismiss the infobar.
  CloseInfobar(tab);
  EXPECT_EQ(0u, GetInfobarCount(tab));
}

IN_PROC_BROWSER_TEST_F(KnownInterceptionDisclosureInfobarTest,
                       CooldownResetsOnBrowserRestartDesktop) {
  const GURL kInterceptedUrl(https_server_.GetURL("/ssl/google.html"));

  // On restart, no infobar should be shown initially.
  content::WebContents* tab =
      browser()->tab_strip_model()->GetActiveWebContents();
  EXPECT_EQ(0u, GetInfobarCount(tab));

  // Triggering the disclosure infobar again after browser restart should show
  // the infobar (the cooldown period should no longer apply on Desktop).
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), kInterceptedUrl));
  EXPECT_EQ(1u, GetInfobarCount(tab));
}