summaryrefslogtreecommitdiff
path: root/chromium/weblayer/browser/safe_browsing/client_side_detection_service_browsertest.cc
blob: 6b5fa6af7c501e235e2b23cd4482681b6441c1d2 (plain)
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
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "weblayer/browser/safe_browsing/client_side_detection_service_factory.h"

#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "components/prefs/pref_service.h"
#include "components/safe_browsing/content/browser/client_side_detection_service.h"
#include "components/safe_browsing/content/browser/client_side_phishing_model.h"
#include "components/safe_browsing/content/common/safe_browsing.mojom.h"
#include "components/safe_browsing/core/common/proto/client_model.pb.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#include "content/public/test/browser_test.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "weblayer/browser/browser_context_impl.h"
#include "weblayer/browser/profile_impl.h"
#include "weblayer/browser/tab_impl.h"
#include "weblayer/common/features.h"
#include "weblayer/shell/browser/shell.h"
#include "weblayer/test/weblayer_browser_test.h"
#include "weblayer/test/weblayer_browser_test_utils.h"

namespace weblayer {

using safe_browsing::ClientSideDetectionService;
using safe_browsing::ClientSideModel;
using safe_browsing::ClientSidePhishingModel;
using ::testing::_;
using ::testing::ReturnRef;
using ::testing::StrictMock;

class ClientSideDetectionServiceBrowserTest : public WebLayerBrowserTest {
 public:
  ClientSideDetectionServiceBrowserTest() {
    feature_list_.InitAndEnableFeature(
        features::kWebLayerClientSidePhishingDetection);
  }
  content::WebContents* GetWebContents() {
    return static_cast<TabImpl*>(shell()->tab())->web_contents();
  }

 private:
  void SetUpOnMainThread() override {
    NavigateAndWaitForCompletion(GURL("about:blank"), shell());
  }
  base::test::ScopedFeatureList feature_list_;
};

// TODO(crbug.com/1217128): Re-enable this test, once we have a more reliable
// method of ensuring the SetPhishingModel IPC comes before the
// StartPhishingDetection IPC.
IN_PROC_BROWSER_TEST_F(ClientSideDetectionServiceBrowserTest,
                       DISABLED_NewHostGetsModel) {
  PrefService* prefs = GetProfile()->GetBrowserContext()->pref_service();
  prefs->SetBoolean(::prefs::kSafeBrowsingEnabled, false);

  ClientSideModel model;
  model.set_max_words_per_term(0);
  std::string model_str;
  model.SerializeToString(&model_str);

  ClientSidePhishingModel::GetInstance()->SetModelStrForTesting(model_str);

  // Enable Safe Browsing and the CSD service.
  prefs->SetBoolean(::prefs::kSafeBrowsingEnabled, true);

  base::RunLoop run_loop;

  content::RenderFrameHost* rfh = GetWebContents()->GetMainFrame();
  mojo::Remote<safe_browsing::mojom::PhishingDetector> phishing_detector;
  rfh->GetRemoteInterfaces()->GetInterface(
      phishing_detector.BindNewPipeAndPassReceiver());

  safe_browsing::mojom::PhishingDetectorResult result;
  std::string verdict;
  phishing_detector->StartPhishingDetection(
      GURL("about:blank"),
      base::BindOnce(
          [](base::RepeatingClosure quit_closure,
             safe_browsing::mojom::PhishingDetectorResult* out_result,
             std::string* out_verdict,
             safe_browsing::mojom::PhishingDetectorResult result,
             const std::string& verdict) {
            *out_result = result;
            *out_verdict = verdict;
            quit_closure.Run();
          },
          run_loop.QuitClosure(), &result, &verdict));

  run_loop.Run();

  // The model classification will run, but will return an invalid score.
  EXPECT_EQ(result,
            safe_browsing::mojom::PhishingDetectorResult::INVALID_SCORE);
}

}  // namespace weblayer