Chromium Code Reviews| 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 void SetReportingChance(bool reporting_chance) { | |
| 45 reporting_chance_ = reporting_chance; | |
| 46 } | |
| 47 | |
| 48 protected: | |
| 49 double GetReportChance() const override { return reporting_chance_; } | |
| 50 void SkippedReporting() override { | |
| 51 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 52 BrowserThread::PostTask( | |
| 53 BrowserThread::UI, FROM_HERE, | |
| 54 base::Bind(&TestingNotificationImageReporter::SkippedReportingOnUI, | |
| 55 base::Unretained(this))); | |
| 56 } | |
| 57 | |
| 58 private: | |
| 59 void SkippedReportingOnUI() { | |
| 60 if (quit_closure_) { | |
| 61 quit_closure_.Run(); | |
| 62 quit_closure_.Reset(); | |
| 63 } | |
| 64 } | |
| 65 base::Closure quit_closure_; | |
| 66 double reporting_chance_ = 1; | |
|
johnme
2017/01/23 11:34:34
Micro-nit: 1.0
harkness
2017/01/23 11:45:56
Done.
| |
| 67 }; | |
| 68 | |
| 69 class FakeSafeBrowsingDatabaseManager : public TestSafeBrowsingDatabaseManager { | |
| 70 public: | |
| 71 bool MatchCsdWhitelistUrl(const GURL& url) override { return false; } | |
| 72 | |
| 73 private: | |
| 74 ~FakeSafeBrowsingDatabaseManager() override {} | |
| 75 }; | |
| 76 | |
| 77 SkBitmap CreateBitmap(int width, int height) { | |
| 78 SkBitmap bitmap; | |
| 79 bitmap.allocN32Pixels(width, height); | |
| 80 bitmap.eraseColor(SK_ColorGREEN); | |
| 81 return bitmap; | |
| 82 } | |
| 83 | |
| 84 } // namespace | |
| 85 | |
| 86 class NotificationImageReporterTest : public ::testing::Test { | |
| 87 public: | |
| 88 NotificationImageReporterTest(); | |
| 89 | |
| 90 void SetUp() override; | |
| 91 void TearDown() override; | |
| 92 | |
| 93 private: | |
| 94 content::TestBrowserThreadBundle thread_bundle_; // Should be first member. | |
| 95 | |
| 96 protected: | |
| 97 void SetExtendedReportingLevel(ExtendedReportingLevel level); | |
| 98 void ReportNotificationImage(); | |
| 99 | |
| 100 scoped_refptr<SafeBrowsingService> safe_browsing_service_; | |
| 101 | |
| 102 std::unique_ptr<TestingProfile> profile_; // Written on UI, read on IO. | |
| 103 | |
| 104 // Owned by |notification_image_reporter_|. | |
| 105 MockPermissionReportSender* mock_report_sender_; | |
| 106 | |
| 107 TestingNotificationImageReporter* notification_image_reporter_; | |
| 108 | |
| 109 std::unique_ptr<base::test::ScopedFeatureList> feature_list_; | |
| 110 | |
| 111 GURL origin_; // Written on UI, read on IO. | |
| 112 SkBitmap image_; // Written on UI, read on IO. | |
| 113 | |
| 114 private: | |
| 115 void SetUpOnIO(); | |
| 116 | |
| 117 void ReportNotificationImageOnIO(); | |
| 118 }; | |
| 119 | |
| 120 NotificationImageReporterTest::NotificationImageReporterTest() | |
| 121 // Use REAL_IO_THREAD so DCHECK_CURRENTLY_ON distinguishes IO from UI. | |
| 122 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD), | |
| 123 origin_("https://example.com") { | |
| 124 image_ = CreateBitmap(1 /* w */, 1 /* h */); | |
| 125 } | |
| 126 | |
| 127 void NotificationImageReporterTest::SetUp() { | |
| 128 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 129 | |
| 130 // Initialize SafeBrowsingService with FakeSafeBrowsingDatabaseManager. | |
| 131 TestSafeBrowsingServiceFactory sb_service_factory; | |
| 132 sb_service_factory.SetTestDatabaseManager( | |
| 133 new FakeSafeBrowsingDatabaseManager()); | |
| 134 SafeBrowsingService::RegisterFactory(&sb_service_factory); | |
| 135 safe_browsing_service_ = sb_service_factory.CreateSafeBrowsingService(); | |
| 136 SafeBrowsingService::RegisterFactory(nullptr); | |
| 137 TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService( | |
| 138 safe_browsing_service_.get()); | |
| 139 g_browser_process->safe_browsing_service()->Initialize(); | |
| 140 base::RunLoop().RunUntilIdle(); // TODO(johnme): Might still be tasks on IO. | |
| 141 | |
| 142 profile_ = base::MakeUnique<TestingProfile>(); | |
| 143 | |
| 144 base::RunLoop run_loop; | |
| 145 BrowserThread::PostTaskAndReply( | |
| 146 BrowserThread::IO, FROM_HERE, | |
| 147 base::Bind(&NotificationImageReporterTest::SetUpOnIO, | |
| 148 base::Unretained(this)), | |
| 149 run_loop.QuitClosure()); | |
| 150 run_loop.Run(); | |
| 151 } | |
| 152 | |
| 153 void NotificationImageReporterTest::TearDown() { | |
| 154 TestingBrowserProcess::GetGlobal()->safe_browsing_service()->ShutDown(); | |
| 155 base::RunLoop().RunUntilIdle(); // TODO(johnme): Might still be tasks on IO. | |
| 156 TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService(nullptr); | |
| 157 } | |
| 158 | |
| 159 void NotificationImageReporterTest::SetUpOnIO() { | |
| 160 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 161 | |
| 162 mock_report_sender_ = new MockPermissionReportSender; | |
| 163 notification_image_reporter_ = new TestingNotificationImageReporter( | |
| 164 base::WrapUnique(mock_report_sender_)); | |
| 165 safe_browsing_service_->ping_manager()->notification_image_reporter_ = | |
| 166 base::WrapUnique(notification_image_reporter_); | |
| 167 } | |
| 168 | |
| 169 void NotificationImageReporterTest::SetExtendedReportingLevel( | |
| 170 ExtendedReportingLevel level) { | |
| 171 feature_list_ = base::MakeUnique<base::test::ScopedFeatureList>(); | |
| 172 if (level == SBER_LEVEL_SCOUT) | |
| 173 feature_list_->InitWithFeatures({safe_browsing::kOnlyShowScoutOptIn}, {}); | |
| 174 | |
| 175 InitializeSafeBrowsingPrefs(profile_->GetPrefs()); | |
| 176 SetExtendedReportingPref(profile_->GetPrefs(), level != SBER_LEVEL_OFF); | |
| 177 } | |
| 178 | |
| 179 void NotificationImageReporterTest::ReportNotificationImage() { | |
| 180 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 181 BrowserThread::PostTask( | |
| 182 BrowserThread::IO, FROM_HERE, | |
| 183 base::Bind(&NotificationImageReporterTest::ReportNotificationImageOnIO, | |
| 184 base::Unretained(this))); | |
| 185 } | |
| 186 | |
| 187 void NotificationImageReporterTest::ReportNotificationImageOnIO() { | |
| 188 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 189 if (!safe_browsing_service_->enabled()) | |
| 190 return; | |
| 191 safe_browsing_service_->ping_manager()->ReportNotificationImage( | |
| 192 profile_.get(), safe_browsing_service_->database_manager(), origin_, | |
| 193 image_); | |
| 194 } | |
| 195 | |
| 196 TEST_F(NotificationImageReporterTest, ReportSuccess) { | |
| 197 SetExtendedReportingLevel(SBER_LEVEL_SCOUT); | |
| 198 | |
| 199 ReportNotificationImage(); | |
| 200 mock_report_sender_->WaitForReportSent(); | |
| 201 | |
| 202 ASSERT_EQ(1, mock_report_sender_->GetAndResetNumberOfReportsSent()); | |
| 203 EXPECT_EQ(GURL(NotificationImageReporter::kReportingUploadUrl), | |
| 204 mock_report_sender_->latest_report_uri()); | |
| 205 EXPECT_EQ("application/octet-stream", | |
| 206 mock_report_sender_->latest_content_type()); | |
| 207 | |
| 208 NotificationImageReportRequest report; | |
| 209 ASSERT_TRUE(report.ParseFromString(mock_report_sender_->latest_report())); | |
| 210 EXPECT_EQ(origin_.spec(), report.notification_origin()); | |
| 211 ASSERT_TRUE(report.has_image()); | |
| 212 EXPECT_GT(report.image().data().size(), 0U); | |
| 213 ASSERT_TRUE(report.image().has_mime_type()); | |
| 214 EXPECT_EQ(report.image().mime_type(), "image/png"); | |
| 215 ASSERT_TRUE(report.image().has_dimensions()); | |
| 216 EXPECT_EQ(1, report.image().dimensions().width()); | |
| 217 EXPECT_EQ(1, report.image().dimensions().height()); | |
| 218 EXPECT_FALSE(report.image().has_original_dimensions()); | |
| 219 } | |
| 220 | |
| 221 TEST_F(NotificationImageReporterTest, ImageDownscaling) { | |
| 222 SetExtendedReportingLevel(SBER_LEVEL_SCOUT); | |
| 223 | |
| 224 image_ = CreateBitmap(640 /* w */, 360 /* h */); | |
| 225 | |
| 226 ReportNotificationImage(); | |
| 227 mock_report_sender_->WaitForReportSent(); | |
| 228 | |
| 229 NotificationImageReportRequest report; | |
| 230 ASSERT_TRUE(report.ParseFromString(mock_report_sender_->latest_report())); | |
| 231 ASSERT_TRUE(report.has_image()); | |
| 232 EXPECT_GT(report.image().data().size(), 0U); | |
| 233 ASSERT_TRUE(report.image().has_dimensions()); | |
| 234 EXPECT_EQ(512, report.image().dimensions().width()); | |
| 235 EXPECT_EQ(288, report.image().dimensions().height()); | |
| 236 ASSERT_TRUE(report.image().has_original_dimensions()); | |
| 237 EXPECT_EQ(640, report.image().original_dimensions().width()); | |
| 238 EXPECT_EQ(360, report.image().original_dimensions().height()); | |
| 239 } | |
| 240 | |
| 241 TEST_F(NotificationImageReporterTest, NoReportWithoutSBER) { | |
| 242 SetExtendedReportingLevel(SBER_LEVEL_OFF); | |
| 243 | |
| 244 ReportNotificationImage(); | |
| 245 notification_image_reporter_->WaitForReportSkipped(); | |
| 246 | |
| 247 EXPECT_EQ(0, mock_report_sender_->GetAndResetNumberOfReportsSent()); | |
| 248 } | |
| 249 | |
| 250 TEST_F(NotificationImageReporterTest, NoReportWithoutScout) { | |
| 251 SetExtendedReportingLevel(SBER_LEVEL_LEGACY); | |
| 252 | |
| 253 ReportNotificationImage(); | |
| 254 notification_image_reporter_->WaitForReportSkipped(); | |
| 255 | |
| 256 EXPECT_EQ(0, mock_report_sender_->GetAndResetNumberOfReportsSent()); | |
| 257 } | |
| 258 | |
| 259 TEST_F(NotificationImageReporterTest, NoReportWithoutReportingEnabled) { | |
| 260 SetExtendedReportingLevel(SBER_LEVEL_SCOUT); | |
| 261 notification_image_reporter_->SetReportingChance(0); | |
|
johnme
2017/01/23 11:34:34
Micro-nit: 0.0 (https://google.github.io/styleguid
harkness
2017/01/23 11:45:56
Done.
| |
| 262 | |
| 263 ReportNotificationImage(); | |
| 264 notification_image_reporter_->WaitForReportSkipped(); | |
| 265 | |
| 266 EXPECT_EQ(0, mock_report_sender_->GetAndResetNumberOfReportsSent()); | |
| 267 } | |
| 268 | |
| 269 TEST_F(NotificationImageReporterTest, MaxReportsPerDay) { | |
| 270 SetExtendedReportingLevel(SBER_LEVEL_SCOUT); | |
| 271 | |
| 272 const int kMaxReportsPerDay = 5; | |
| 273 | |
| 274 for (int i = 0; i < kMaxReportsPerDay; i++) { | |
| 275 ReportNotificationImage(); | |
| 276 mock_report_sender_->WaitForReportSent(); | |
| 277 } | |
| 278 ReportNotificationImage(); | |
| 279 notification_image_reporter_->WaitForReportSkipped(); | |
| 280 | |
| 281 EXPECT_EQ(kMaxReportsPerDay, | |
| 282 mock_report_sender_->GetAndResetNumberOfReportsSent()); | |
| 283 } | |
| 284 | |
| 285 } // namespace safe_browsing | |
| OLD | NEW |