Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Side by Side Diff: chrome/browser/safe_browsing/notification_image_reporter_unittest.cc

Issue 2637153002: Submit a sample of notification images to Safe Browsing (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 SetReportingEnabled(bool reporting_enabled) {
45 reporting_enabled_ = reporting_enabled;
46 }
47
48 protected:
49 bool IsReportingEnabled() const override { return reporting_enabled_; }
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 bool reporting_enabled_ = true;
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().png_data().size(), 0U);
213 ASSERT_TRUE(report.image().has_dimensions());
214 EXPECT_EQ(1, report.image().dimensions().width());
215 EXPECT_EQ(1, report.image().dimensions().height());
216 EXPECT_FALSE(report.image().has_original_dimensions());
217 }
218
219 TEST_F(NotificationImageReporterTest, ImageDownscaling) {
220 SetExtendedReportingLevel(SBER_LEVEL_SCOUT);
221
222 image_ = CreateBitmap(640 /* w */, 360 /* h */);
223
224 ReportNotificationImage();
225 mock_report_sender_->WaitForReportSent();
226
227 NotificationImageReportRequest report;
228 ASSERT_TRUE(report.ParseFromString(mock_report_sender_->latest_report()));
229 ASSERT_TRUE(report.has_image());
230 EXPECT_GT(report.image().png_data().size(), 0U);
231 ASSERT_TRUE(report.image().has_dimensions());
232 EXPECT_EQ(512, report.image().dimensions().width());
233 EXPECT_EQ(288, report.image().dimensions().height());
234 ASSERT_TRUE(report.image().has_original_dimensions());
235 EXPECT_EQ(640, report.image().original_dimensions().width());
236 EXPECT_EQ(360, report.image().original_dimensions().height());
237 }
238
239 TEST_F(NotificationImageReporterTest, NoReportWithoutSBER) {
240 SetExtendedReportingLevel(SBER_LEVEL_OFF);
241
242 ReportNotificationImage();
243 notification_image_reporter_->WaitForReportSkipped();
244
245 EXPECT_EQ(0, mock_report_sender_->GetAndResetNumberOfReportsSent());
246 }
247
248 TEST_F(NotificationImageReporterTest, NoReportWithoutScout) {
249 SetExtendedReportingLevel(SBER_LEVEL_LEGACY);
250
251 ReportNotificationImage();
252 notification_image_reporter_->WaitForReportSkipped();
253
254 EXPECT_EQ(0, mock_report_sender_->GetAndResetNumberOfReportsSent());
255 }
256
257 TEST_F(NotificationImageReporterTest, NoReportWithoutReportingEnabled) {
258 SetExtendedReportingLevel(SBER_LEVEL_SCOUT);
259 notification_image_reporter_->SetReportingEnabled(false);
260
261 ReportNotificationImage();
262 notification_image_reporter_->WaitForReportSkipped();
263
264 EXPECT_EQ(0, mock_report_sender_->GetAndResetNumberOfReportsSent());
265 }
266
267 TEST_F(NotificationImageReporterTest, MaxReportsPerDay) {
268 SetExtendedReportingLevel(SBER_LEVEL_SCOUT);
269
270 const int kMaxReportsPerDay = 5;
271
272 for (int i = 0; i < kMaxReportsPerDay; i++) {
273 ReportNotificationImage();
274 mock_report_sender_->WaitForReportSent();
275 }
276 ReportNotificationImage();
277 notification_image_reporter_->WaitForReportSkipped();
278
279 EXPECT_EQ(kMaxReportsPerDay,
280 mock_report_sender_->GetAndResetNumberOfReportsSent());
281 }
282
283 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698