File: download_danger_prompt_browsertest.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 (301 lines) | stat: -rw-r--r-- 11,841 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
// Copyright 2012 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/command_line.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "build/build_config.h"
#include "chrome/browser/download/download_danger_prompt.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/safe_browsing/download_protection/download_protection_service.h"
#include "chrome/browser/safe_browsing/test_safe_browsing_service.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/hats/mock_trust_safety_sentiment_service.h"
#include "chrome/browser/ui/hats/trust_safety_sentiment_service_factory.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/test/test_browser_dialog.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/download/public/common/mock_download_item.h"
#include "components/prefs/pref_service.h"
#include "components/safe_browsing/core/browser/db/database_manager.h"
#include "components/safe_browsing/core/common/proto/csd.pb.h"
#include "content/public/browser/download_item_utils.h"
#include "content/public/test/browser_test.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

using ::testing::_;
using ::testing::ByRef;
using ::testing::Eq;
using ::testing::Return;
using ::testing::ReturnRef;
using ::testing::SaveArg;

namespace safe_browsing {

namespace {

const char kTestDownloadUrl[] = "http://evildownload.com";
const char kDownloadResponseToken[] = "default_token";

}  // namespace

class DownloadDangerPromptTest : public InProcessBrowserTest {
 public:
  DownloadDangerPromptTest()
      : prompt_(nullptr),
        expected_action_(DownloadDangerPrompt::CANCEL),
        did_receive_callback_(false),
        test_safe_browsing_factory_(
            std::make_unique<TestSafeBrowsingServiceFactory>()) {}

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

  ~DownloadDangerPromptTest() override = default;

  void SetUp() override {
    SafeBrowsingService::RegisterFactory(test_safe_browsing_factory_.get());
    InProcessBrowserTest::SetUp();
  }

  void TearDown() override {
    SafeBrowsingService::RegisterFactory(nullptr);
    InProcessBrowserTest::TearDown();
  }

  // Opens a new tab and waits for navigations to finish. If there are pending
  // navigations, the constrained prompt might be dismissed when the navigation
  // completes.
  void OpenNewTab(Browser* browser_to_use) {
    ui_test_utils::NavigateToURLWithDisposition(
        browser_to_use, GURL("about:blank"),
        WindowOpenDisposition::NEW_FOREGROUND_TAB,
        ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB |
            ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
  }

  // Opens a new window and waits for navigations to finish. If there are
  // pending navigations, the constrained prompt might be dismissed when the
  // navigation completes.
  void OpenNewWindow(Browser* browser_to_use) {
    ui_test_utils::NavigateToURLWithDisposition(
        browser_to_use, GURL("about:blank"), WindowOpenDisposition::NEW_WINDOW,
        ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
  }

  void SetUpExpectations(
      const DownloadDangerPrompt::Action& expected_action,
      const download::DownloadDangerType& danger_type,
      const ClientDownloadResponse::Verdict& download_verdict,
      const std::string& token,
      Browser* browser_to_use) {
    content::DownloadItemUtils::AttachInfoForTesting(
        &download(), browser_to_use->profile(), nullptr);
    did_receive_callback_ = false;
    expected_action_ = expected_action;
    SetUpDownloadItemExpectations(danger_type, token, download_verdict);
    SetUpSafeBrowsingReportExpectations(
        expected_action == DownloadDangerPrompt::ACCEPT, download_verdict,
        token, browser_to_use);
    CreatePrompt(browser_to_use);
  }

  void VerifyExpectations(bool should_send_report) {
    content::RunAllPendingInMessageLoop();
    // At the end of each test, we expect no more activity from the prompt. The
    // prompt shouldn't exist anymore either.
    EXPECT_TRUE(did_receive_callback_);
    EXPECT_FALSE(prompt_);

    if (should_send_report) {
      EXPECT_EQ(expected_serialized_report_,
                test_safe_browsing_factory_->test_safe_browsing_service()
                    ->serialized_download_report());
    } else {
      EXPECT_TRUE(test_safe_browsing_factory_->test_safe_browsing_service()
                      ->serialized_download_report()
                      .empty());
    }
    testing::Mock::VerifyAndClearExpectations(&download_);
    test_safe_browsing_factory_->test_safe_browsing_service()
        ->ClearDownloadReport();
  }

  void SimulatePromptAction(DownloadDangerPrompt::Action action) {
    prompt_->InvokeActionForTesting(action);
  }

  download::MockDownloadItem& download() { return download_; }

  DownloadDangerPrompt* prompt() { return prompt_; }

 private:
  void SetUpDownloadItemExpectations(
      const download::DownloadDangerType& danger_type,
      const std::string& token,
      const ClientDownloadResponse::Verdict& download_verdict) {
    EXPECT_CALL(download_, GetFileNameToReportUser())
        .WillRepeatedly(Return(base::FilePath(FILE_PATH_LITERAL("evil.exe"))));
    EXPECT_CALL(download_, GetDangerType()).WillRepeatedly(Return(danger_type));
    auto token_obj =
        std::make_unique<DownloadProtectionService::DownloadProtectionData>(
            token, download_verdict, ClientDownloadResponse::TailoredVerdict());
    download_.SetUserData(DownloadProtectionService::kDownloadProtectionDataKey,
                          std::move(token_obj));
  }

  void SetUpSafeBrowsingReportExpectations(
      bool did_proceed,
      const ClientDownloadResponse::Verdict& download_verdict,
      const std::string& token,
      Browser* browser_to_use) {
    ClientSafeBrowsingReportRequest expected_report;
    expected_report.set_url(GURL(kTestDownloadUrl).spec());
    expected_report.set_type(
        ClientSafeBrowsingReportRequest::DANGEROUS_DOWNLOAD_BY_API);
    expected_report.set_download_verdict(download_verdict);
    expected_report.set_did_proceed(did_proceed);
    if (!token.empty())
      expected_report.set_token(token);
    expected_report.SerializeToString(&expected_serialized_report_);
  }

  void CreatePrompt(Browser* browser_to_use) {
    prompt_ = DownloadDangerPrompt::Create(
        &download_, browser_to_use->tab_strip_model()->GetActiveWebContents(),
        base::BindOnce(&DownloadDangerPromptTest::PromptCallback,
                       base::Unretained(this)));
    content::RunAllPendingInMessageLoop();
  }

  void PromptCallback(DownloadDangerPrompt::Action action) {
    EXPECT_FALSE(did_receive_callback_);
    EXPECT_EQ(expected_action_, action);
    did_receive_callback_ = true;
    prompt_ = nullptr;
  }

  download::MockDownloadItem download_;
  raw_ptr<DownloadDangerPrompt> prompt_;
  DownloadDangerPrompt::Action expected_action_;
  bool did_receive_callback_;
  std::unique_ptr<TestSafeBrowsingServiceFactory> test_safe_browsing_factory_;
  std::string expected_serialized_report_;
};

// Disabled for flaky timeouts on Windows. crbug.com/446696
#if BUILDFLAG(IS_WIN)
#define MAYBE_TestAll DISABLED_TestAll
#else
#define MAYBE_TestAll TestAll
#endif
IN_PROC_BROWSER_TEST_F(DownloadDangerPromptTest, MAYBE_TestAll) {
  GURL download_url(kTestDownloadUrl);
  ON_CALL(download(), GetURL()).WillByDefault(ReturnRef(download_url));
  ON_CALL(download(), GetReferrerUrl())
      .WillByDefault(ReturnRef(GURL::EmptyGURL()));
  base::FilePath empty_file_path;
  ON_CALL(download(), GetTargetFilePath())
      .WillByDefault(ReturnRef(empty_file_path));

  OpenNewTab(browser());

  // If file is downloaded through download api, a confirm download dialog
  // instead of a recovery dialog is shown. Clicking the Accept button should
  // invoke the ACCEPT action, a report will be sent with type
  // DANGEROUS_DOWNLOAD_BY_API.
  SetUpExpectations(DownloadDangerPrompt::ACCEPT,
                    download::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
                    ClientDownloadResponse::DANGEROUS, kDownloadResponseToken,
                    browser());
  EXPECT_CALL(download(), IsDangerous()).WillRepeatedly(Return(true));
  SimulatePromptAction(DownloadDangerPrompt::ACCEPT);
  VerifyExpectations(true);

  // If file is downloaded through download api, a confirm download dialog
  // instead of a recovery dialog is shown. Clicking the Cancel button should
  // invoke the CANCEL action, a report will NOT be sent with type
  // DANGEROUS_DOWNLOAD_BY_API.
  SetUpExpectations(DownloadDangerPrompt::CANCEL,
                    download::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT,
                    ClientDownloadResponse::UNCOMMON, std::string(), browser());
  EXPECT_CALL(download(), IsDangerous()).WillRepeatedly(Return(true));
  SimulatePromptAction(DownloadDangerPrompt::CANCEL);
  VerifyExpectations(false);
}

// Class for testing interactive dialogs.
class DownloadDangerPromptBrowserTest : public DialogBrowserTest {
 protected:
  DownloadDangerPromptBrowserTest() : download_url_(kTestDownloadUrl) {}

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

  void RunTest(download::DownloadDangerType danger_type) {
    danger_type_ = danger_type;
    ShowAndVerifyUi();
  }

 private:
  void ShowUi(const std::string& name) override {
    ON_CALL(download_, GetURL()).WillByDefault(ReturnRef(download_url_));
    ON_CALL(download_, GetReferrerUrl())
        .WillByDefault(ReturnRef(GURL::EmptyGURL()));
    ON_CALL(download_, GetTargetFilePath())
        .WillByDefault(ReturnRef(empty_file_path_));
    ON_CALL(download_, IsDangerous()).WillByDefault(Return(true));
    ON_CALL(download_, GetFileNameToReportUser())
        .WillByDefault(Return(base::FilePath(FILE_PATH_LITERAL("evil.exe"))));

    // Set up test-specific parameters
    ON_CALL(download_, GetDangerType()).WillByDefault(Return(danger_type_));
    content::DownloadItemUtils::AttachInfoForTesting(
        &download_, browser()->profile(), nullptr);
    DownloadDangerPrompt::Create(
        &download_, browser()->tab_strip_model()->GetActiveWebContents(),
        DownloadDangerPrompt::OnDone());
  }

  const GURL download_url_;
  const base::FilePath empty_file_path_;

  download::DownloadDangerType danger_type_;
  download::MockDownloadItem download_;
};

IN_PROC_BROWSER_TEST_F(DownloadDangerPromptBrowserTest,
                       InvokeUi_DangerousFileFromApi) {
  RunTest(download::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE);
}

IN_PROC_BROWSER_TEST_F(DownloadDangerPromptBrowserTest,
                       InvokeUi_DangerousUrlFromApi) {
  RunTest(download::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL);
}

IN_PROC_BROWSER_TEST_F(DownloadDangerPromptBrowserTest,
                       InvokeUi_UncommonContentFromApi) {
  RunTest(download::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT);
}

IN_PROC_BROWSER_TEST_F(DownloadDangerPromptBrowserTest,
                       InvokeUi_PotentiallyUnwantedFromApi) {
  RunTest(download::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED);
}

IN_PROC_BROWSER_TEST_F(DownloadDangerPromptBrowserTest,
                       InvokeUi_AccountCompromiseFromApi) {
  RunTest(download::DOWNLOAD_DANGER_TYPE_DANGEROUS_ACCOUNT_COMPROMISE);
}

}  // namespace safe_browsing