OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 |
| 5 #include "chrome/browser/safe_browsing/notification_image_reporter.h" |
| 6 |
| 7 #include "base/callback.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/run_loop.h" |
| 10 #include "base/test/scoped_feature_list.h" |
| 11 #include "chrome/browser/safe_browsing/mock_permission_report_sender.h" |
| 12 #include "chrome/browser/safe_browsing/ping_manager.h" |
| 13 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" |
| 14 #include "chrome/common/safe_browsing/csd.pb.h" |
| 15 #include "chrome/test/base/testing_browser_process.h" |
| 16 #include "chrome/test/base/testing_profile.h" |
| 17 #include "components/safe_browsing_db/safe_browsing_prefs.h" |
| 18 #include "components/safe_browsing_db/test_database_manager.h" |
| 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/test/test_browser_thread_bundle.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "third_party/skia/include/core/SkBitmap.h" |
| 23 #include "third_party/skia/include/core/SkColor.h" |
| 24 #include "url/gurl.h" |
| 25 |
| 26 using content::BrowserThread; |
| 27 |
| 28 namespace safe_browsing { |
| 29 |
| 30 namespace { |
| 31 |
| 32 class TestingNotificationImageReporter : public NotificationImageReporter { |
| 33 public: |
| 34 explicit TestingNotificationImageReporter( |
| 35 std::unique_ptr<net::ReportSender> report_sender) |
| 36 : NotificationImageReporter(std::move(report_sender)) {} |
| 37 |
| 38 void WaitForReportSkipped() { |
| 39 base::RunLoop run_loop; |
| 40 quit_closure_ = run_loop.QuitClosure(); |
| 41 run_loop.Run(); |
| 42 } |
| 43 |
| 44 protected: |
| 45 double GetReportChance() override { return 1.0; } |
| 46 void SkippedReporting() override { |
| 47 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 48 BrowserThread::PostTask( |
| 49 BrowserThread::UI, FROM_HERE, |
| 50 base::Bind(&TestingNotificationImageReporter::SkippedReportingOnUI, |
| 51 base::Unretained(this))); |
| 52 } |
| 53 |
| 54 private: |
| 55 void SkippedReportingOnUI() { |
| 56 if (quit_closure_) { |
| 57 quit_closure_.Run(); |
| 58 quit_closure_.Reset(); |
| 59 } |
| 60 } |
| 61 base::Closure quit_closure_; |
| 62 }; |
| 63 |
| 64 class FakeSafeBrowsingDatabaseManager : public TestSafeBrowsingDatabaseManager { |
| 65 public: |
| 66 bool MatchCsdWhitelistUrl(const GURL& url) override { return false; } |
| 67 |
| 68 private: |
| 69 ~FakeSafeBrowsingDatabaseManager() override {} |
| 70 }; |
| 71 |
| 72 SkBitmap CreateBitmap(int width, int height) { |
| 73 SkBitmap bitmap; |
| 74 bitmap.allocN32Pixels(width, height); |
| 75 bitmap.eraseColor(SK_ColorGREEN); |
| 76 return bitmap; |
| 77 } |
| 78 |
| 79 } // namespace |
| 80 |
| 81 class NotificationImageReporterTest : public ::testing::Test { |
| 82 public: |
| 83 NotificationImageReporterTest(); |
| 84 |
| 85 void SetUp() override; |
| 86 void TearDown() override; |
| 87 |
| 88 private: |
| 89 content::TestBrowserThreadBundle thread_bundle_; // Should be first member. |
| 90 |
| 91 protected: |
| 92 void SetExtendedReportingLevel(ExtendedReportingLevel level); |
| 93 void ReportNotificationImage(); |
| 94 |
| 95 scoped_refptr<SafeBrowsingService> safe_browsing_service_; |
| 96 |
| 97 std::unique_ptr<TestingProfile> profile_; // Written on UI, read on IO. |
| 98 |
| 99 // Owned by |notification_image_reporter_|. |
| 100 MockPermissionReportSender* mock_report_sender_; |
| 101 |
| 102 TestingNotificationImageReporter* notification_image_reporter_; |
| 103 |
| 104 std::unique_ptr<base::test::ScopedFeatureList> feature_list_; |
| 105 |
| 106 GURL origin_; // Written on UI, read on IO. |
| 107 SkBitmap image_; // Written on UI, read on IO. |
| 108 |
| 109 private: |
| 110 void SetUpOnIO(); |
| 111 |
| 112 void ReportNotificationImageOnIO(); |
| 113 }; |
| 114 |
| 115 NotificationImageReporterTest::NotificationImageReporterTest() |
| 116 // Use REAL_IO_THREAD so DCHECK_CURRENTLY_ON distinguishes IO from UI. |
| 117 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD), |
| 118 origin_("https://example.com") { |
| 119 image_ = CreateBitmap(1 /* w */, 1 /* h */); |
| 120 } |
| 121 |
| 122 void NotificationImageReporterTest::SetUp() { |
| 123 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 124 |
| 125 // Initialize SafeBrowsingService with FakeSafeBrowsingDatabaseManager. |
| 126 TestSafeBrowsingServiceFactory sb_service_factory; |
| 127 sb_service_factory.SetTestDatabaseManager( |
| 128 new FakeSafeBrowsingDatabaseManager()); |
| 129 SafeBrowsingService::RegisterFactory(&sb_service_factory); |
| 130 safe_browsing_service_ = sb_service_factory.CreateSafeBrowsingService(); |
| 131 SafeBrowsingService::RegisterFactory(nullptr); |
| 132 TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService( |
| 133 safe_browsing_service_.get()); |
| 134 g_browser_process->safe_browsing_service()->Initialize(); |
| 135 base::RunLoop().RunUntilIdle(); // TODO(johnme): Might still be tasks on IO. |
| 136 |
| 137 profile_ = base::MakeUnique<TestingProfile>(); |
| 138 |
| 139 base::RunLoop run_loop; |
| 140 BrowserThread::PostTaskAndReply( |
| 141 BrowserThread::IO, FROM_HERE, |
| 142 base::Bind(&NotificationImageReporterTest::SetUpOnIO, |
| 143 base::Unretained(this)), |
| 144 run_loop.QuitClosure()); |
| 145 run_loop.Run(); |
| 146 } |
| 147 |
| 148 void NotificationImageReporterTest::TearDown() { |
| 149 TestingBrowserProcess::GetGlobal()->safe_browsing_service()->ShutDown(); |
| 150 base::RunLoop().RunUntilIdle(); // TODO(johnme): Might still be tasks on IO. |
| 151 TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService(nullptr); |
| 152 } |
| 153 |
| 154 void NotificationImageReporterTest::SetUpOnIO() { |
| 155 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 156 |
| 157 mock_report_sender_ = new MockPermissionReportSender; |
| 158 notification_image_reporter_ = new TestingNotificationImageReporter( |
| 159 base::WrapUnique(mock_report_sender_)); |
| 160 safe_browsing_service_->ping_manager()->notification_image_reporter_ = |
| 161 base::WrapUnique(notification_image_reporter_); |
| 162 } |
| 163 |
| 164 void NotificationImageReporterTest::SetExtendedReportingLevel( |
| 165 ExtendedReportingLevel level) { |
| 166 feature_list_ = base::MakeUnique<base::test::ScopedFeatureList>(); |
| 167 if (level == SBER_LEVEL_SCOUT) |
| 168 feature_list_->InitWithFeatures({safe_browsing::kOnlyShowScoutOptIn}, {}); |
| 169 |
| 170 InitializeSafeBrowsingPrefs(profile_->GetPrefs()); |
| 171 SetExtendedReportingPref(profile_->GetPrefs(), level != SBER_LEVEL_OFF); |
| 172 } |
| 173 |
| 174 void NotificationImageReporterTest::ReportNotificationImage() { |
| 175 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 176 BrowserThread::PostTask( |
| 177 BrowserThread::IO, FROM_HERE, |
| 178 base::Bind(&NotificationImageReporterTest::ReportNotificationImageOnIO, |
| 179 base::Unretained(this))); |
| 180 } |
| 181 |
| 182 void NotificationImageReporterTest::ReportNotificationImageOnIO() { |
| 183 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 184 if (!safe_browsing_service_->enabled()) |
| 185 return; |
| 186 safe_browsing_service_->ping_manager()->ReportNotificationImage( |
| 187 profile_.get(), safe_browsing_service_->database_manager(), origin_, |
| 188 image_); |
| 189 } |
| 190 |
| 191 TEST_F(NotificationImageReporterTest, ReportSuccess) { |
| 192 SetExtendedReportingLevel(SBER_LEVEL_SCOUT); |
| 193 |
| 194 ReportNotificationImage(); |
| 195 mock_report_sender_->WaitForReportSent(); |
| 196 |
| 197 ASSERT_EQ(1, mock_report_sender_->GetAndResetNumberOfReportsSent()); |
| 198 EXPECT_EQ(GURL(NotificationImageReporter::kReportingUploadUrl), |
| 199 mock_report_sender_->latest_report_uri()); |
| 200 EXPECT_EQ("application/octet-stream", |
| 201 mock_report_sender_->latest_content_type()); |
| 202 |
| 203 NotificationImageReportRequest report; |
| 204 ASSERT_TRUE(report.ParseFromString(mock_report_sender_->latest_report())); |
| 205 EXPECT_EQ(origin_.spec(), report.notification_origin()); |
| 206 ASSERT_TRUE(report.has_image()); |
| 207 EXPECT_GT(report.image().png_data().size(), 0U); |
| 208 ASSERT_TRUE(report.image().has_dimensions()); |
| 209 EXPECT_EQ(1, report.image().dimensions().width()); |
| 210 EXPECT_EQ(1, report.image().dimensions().height()); |
| 211 EXPECT_FALSE(report.image().has_original_dimensions()); |
| 212 } |
| 213 |
| 214 TEST_F(NotificationImageReporterTest, ImageDownscaling) { |
| 215 SetExtendedReportingLevel(SBER_LEVEL_SCOUT); |
| 216 |
| 217 image_ = CreateBitmap(640 /* w */, 360 /* h */); |
| 218 |
| 219 ReportNotificationImage(); |
| 220 mock_report_sender_->WaitForReportSent(); |
| 221 |
| 222 NotificationImageReportRequest report; |
| 223 ASSERT_TRUE(report.ParseFromString(mock_report_sender_->latest_report())); |
| 224 ASSERT_TRUE(report.has_image()); |
| 225 EXPECT_GT(report.image().png_data().size(), 0U); |
| 226 ASSERT_TRUE(report.image().has_dimensions()); |
| 227 EXPECT_EQ(512, report.image().dimensions().width()); |
| 228 EXPECT_EQ(288, report.image().dimensions().height()); |
| 229 ASSERT_TRUE(report.image().has_original_dimensions()); |
| 230 EXPECT_EQ(640, report.image().original_dimensions().width()); |
| 231 EXPECT_EQ(360, report.image().original_dimensions().height()); |
| 232 } |
| 233 |
| 234 TEST_F(NotificationImageReporterTest, NoReportWithoutSBER) { |
| 235 SetExtendedReportingLevel(SBER_LEVEL_OFF); |
| 236 |
| 237 ReportNotificationImage(); |
| 238 notification_image_reporter_->WaitForReportSkipped(); |
| 239 |
| 240 EXPECT_EQ(0, mock_report_sender_->GetAndResetNumberOfReportsSent()); |
| 241 } |
| 242 |
| 243 TEST_F(NotificationImageReporterTest, NoReportWithoutScout) { |
| 244 SetExtendedReportingLevel(SBER_LEVEL_LEGACY); |
| 245 |
| 246 ReportNotificationImage(); |
| 247 notification_image_reporter_->WaitForReportSkipped(); |
| 248 |
| 249 EXPECT_EQ(0, mock_report_sender_->GetAndResetNumberOfReportsSent()); |
| 250 } |
| 251 |
| 252 TEST_F(NotificationImageReporterTest, MaxReportsPerDay) { |
| 253 SetExtendedReportingLevel(SBER_LEVEL_SCOUT); |
| 254 |
| 255 const int kMaxReportsPerDay = 5; |
| 256 |
| 257 for (int i = 0; i < kMaxReportsPerDay; i++) { |
| 258 ReportNotificationImage(); |
| 259 mock_report_sender_->WaitForReportSent(); |
| 260 } |
| 261 ReportNotificationImage(); |
| 262 notification_image_reporter_->WaitForReportSkipped(); |
| 263 |
| 264 EXPECT_EQ(kMaxReportsPerDay, |
| 265 mock_report_sender_->GetAndResetNumberOfReportsSent()); |
| 266 } |
| 267 |
| 268 } // namespace safe_browsing |
OLD | NEW |