client_side_detection_service_browsertest.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "weblayer/browser/safe_browsing/client_side_detection_service_factory.h"
  5. #include "base/test/bind.h"
  6. #include "base/test/scoped_feature_list.h"
  7. #include "components/prefs/pref_service.h"
  8. #include "components/safe_browsing/content/browser/client_side_detection_service.h"
  9. #include "components/safe_browsing/content/browser/client_side_phishing_model.h"
  10. #include "components/safe_browsing/content/common/safe_browsing.mojom.h"
  11. #include "components/safe_browsing/core/common/proto/client_model.pb.h"
  12. #include "components/safe_browsing/core/common/safe_browsing_prefs.h"
  13. #include "content/public/test/browser_test.h"
  14. #include "mojo/public/cpp/bindings/remote.h"
  15. #include "services/service_manager/public/cpp/interface_provider.h"
  16. #include "testing/gmock/include/gmock/gmock.h"
  17. #include "weblayer/browser/browser_context_impl.h"
  18. #include "weblayer/browser/profile_impl.h"
  19. #include "weblayer/browser/tab_impl.h"
  20. #include "weblayer/common/features.h"
  21. #include "weblayer/shell/browser/shell.h"
  22. #include "weblayer/test/weblayer_browser_test.h"
  23. #include "weblayer/test/weblayer_browser_test_utils.h"
  24. namespace weblayer {
  25. using safe_browsing::ClientSideDetectionService;
  26. using safe_browsing::ClientSideModel;
  27. using safe_browsing::ClientSidePhishingModel;
  28. using ::testing::_;
  29. using ::testing::ReturnRef;
  30. using ::testing::StrictMock;
  31. class ClientSideDetectionServiceBrowserTest : public WebLayerBrowserTest {
  32. public:
  33. ClientSideDetectionServiceBrowserTest() {
  34. feature_list_.InitAndEnableFeature(
  35. features::kWebLayerClientSidePhishingDetection);
  36. }
  37. content::WebContents* GetWebContents() {
  38. return static_cast<TabImpl*>(shell()->tab())->web_contents();
  39. }
  40. private:
  41. void SetUpOnMainThread() override {
  42. NavigateAndWaitForCompletion(GURL("about:blank"), shell());
  43. }
  44. base::test::ScopedFeatureList feature_list_;
  45. };
  46. // TODO(crbug.com/1217128): Re-enable this test, once we have a more reliable
  47. // method of ensuring the SetPhishingModel IPC comes before the
  48. // StartPhishingDetection IPC.
  49. IN_PROC_BROWSER_TEST_F(ClientSideDetectionServiceBrowserTest,
  50. DISABLED_NewHostGetsModel) {
  51. PrefService* prefs = GetProfile()->GetBrowserContext()->pref_service();
  52. prefs->SetBoolean(::prefs::kSafeBrowsingEnabled, false);
  53. ClientSideModel model;
  54. model.set_max_words_per_term(0);
  55. std::string model_str;
  56. model.SerializeToString(&model_str);
  57. ClientSidePhishingModel::GetInstance()->SetModelStrForTesting(model_str);
  58. // Enable Safe Browsing and the CSD service.
  59. prefs->SetBoolean(::prefs::kSafeBrowsingEnabled, true);
  60. base::RunLoop run_loop;
  61. content::RenderFrameHost* rfh = GetWebContents()->GetPrimaryMainFrame();
  62. mojo::Remote<safe_browsing::mojom::PhishingDetector> phishing_detector;
  63. rfh->GetRemoteInterfaces()->GetInterface(
  64. phishing_detector.BindNewPipeAndPassReceiver());
  65. safe_browsing::mojom::PhishingDetectorResult result;
  66. std::string verdict;
  67. phishing_detector->StartPhishingDetection(
  68. GURL("about:blank"),
  69. base::BindOnce(
  70. [](base::RepeatingClosure quit_closure,
  71. safe_browsing::mojom::PhishingDetectorResult* out_result,
  72. std::string* out_verdict,
  73. safe_browsing::mojom::PhishingDetectorResult result,
  74. const std::string& verdict) {
  75. *out_result = result;
  76. *out_verdict = verdict;
  77. quit_closure.Run();
  78. },
  79. run_loop.QuitClosure(), &result, &verdict));
  80. run_loop.Run();
  81. // The model classification will run, but will return an invalid score.
  82. EXPECT_EQ(result,
  83. safe_browsing::mojom::PhishingDetectorResult::INVALID_SCORE);
  84. }
  85. } // namespace weblayer