OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 <string> | |
6 #include <vector> | |
7 | |
8 #include "base/command_line.h" | |
9 #include "base/strings/string_piece.h" | |
10 #include "base/strings/string_util.h" | |
11 #include "base/test/scoped_feature_list.h" | |
12 #include "components/prefs/pref_registry_simple.h" | |
13 #include "components/prefs/testing_pref_service.h" | |
14 #include "components/safe_browsing_db/safe_browsing_prefs.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 namespace safe_browsing { | |
18 | |
19 class SafeBrowsingPrefsTest : public ::testing::Test { | |
20 protected: | |
21 void SetUp() override { | |
22 prefs_.registry()->RegisterBooleanPref( | |
23 prefs::kSafeBrowsingExtendedReportingEnabled, false); | |
24 prefs_.registry()->RegisterBooleanPref( | |
25 prefs::kSafeBrowsingScoutReportingEnabled, false); | |
26 prefs_.registry()->RegisterBooleanPref( | |
27 prefs::kSafeBrowsingScoutGroupSelected, false); | |
28 prefs_.registry()->RegisterBooleanPref( | |
29 prefs::kSafeBrowsingSawInterstitialExtendedReporting, false); | |
30 prefs_.registry()->RegisterBooleanPref( | |
31 prefs::kSafeBrowsingSawInterstitialScoutReporting, false); | |
32 | |
33 ResetExperiments(/*can_show_scout=*/false, /*only_show_scout=*/false); | |
34 } | |
35 | |
36 void ResetPrefs(bool sber_reporting, bool scout_reporting, bool scout_group) { | |
37 prefs_.SetBoolean(prefs::kSafeBrowsingExtendedReportingEnabled, | |
38 sber_reporting); | |
39 prefs_.SetBoolean(prefs::kSafeBrowsingScoutReportingEnabled, | |
40 scout_reporting); | |
41 prefs_.SetBoolean(prefs::kSafeBrowsingScoutGroupSelected, scout_group); | |
42 } | |
43 | |
44 void ResetExperiments(bool can_show_scout, bool only_show_scout) { | |
45 std::vector<base::StringPiece> enabled_features; | |
46 std::vector<base::StringPiece> disabled_features; | |
47 | |
48 auto* target_vector = | |
49 can_show_scout ? &enabled_features : &disabled_features; | |
50 target_vector->push_back(kCanShowScoutOptIn.name); | |
51 | |
52 target_vector = only_show_scout ? &enabled_features : &disabled_features; | |
53 target_vector->push_back(kOnlyShowScoutOptIn.name); | |
54 | |
55 feature_list_.reset(new base::test::ScopedFeatureList); | |
56 feature_list_->InitFromCommandLine( | |
57 base::JoinString(enabled_features, ","), | |
58 base::JoinString(disabled_features, ",")); | |
59 } | |
60 | |
61 std::string GetActivePref() { return GetExtendedReportingPrefName(prefs_); } | |
62 | |
63 // Convenience method for explicitly setting up all combinations of prefs and | |
64 // experiments. | |
65 void TestGetPrefName(bool sber_reporting, | |
66 bool scout_reporting, | |
67 bool scout_group, | |
68 bool can_show_scout, | |
69 bool only_show_scout, | |
70 const std::string& expected_pref) { | |
71 ResetPrefs(sber_reporting, scout_reporting, scout_group); | |
72 ResetExperiments(can_show_scout, only_show_scout); | |
73 EXPECT_EQ(expected_pref, GetActivePref()) | |
74 << "sber=" << sber_reporting << " scout=" << scout_reporting | |
75 << " scout_group=" << scout_group | |
76 << " can_show_scout=" << can_show_scout | |
77 << " only_show_scout=" << only_show_scout; | |
78 } | |
79 | |
80 void InitPrefs() { InitializeSafeBrowsingPrefs(&prefs_); } | |
81 | |
82 bool IsScoutGroupSelected() { | |
83 return prefs_.GetBoolean(prefs::kSafeBrowsingScoutGroupSelected); | |
84 } | |
85 | |
86 void ExpectPrefs(bool sber_reporting, | |
87 bool scout_reporting, | |
88 bool scout_group) { | |
89 LOG(INFO) << "Pref values: sber=" << sber_reporting | |
90 << " scout=" << scout_reporting << " scout_group=" << scout_group; | |
91 EXPECT_EQ(sber_reporting, | |
92 prefs_.GetBoolean(prefs::kSafeBrowsingExtendedReportingEnabled)); | |
93 EXPECT_EQ(scout_reporting, | |
94 prefs_.GetBoolean(prefs::kSafeBrowsingScoutReportingEnabled)); | |
95 EXPECT_EQ(scout_group, | |
96 prefs_.GetBoolean(prefs::kSafeBrowsingScoutGroupSelected)); | |
97 } | |
98 | |
99 void ExpectPrefsExist(bool sber_reporting, | |
100 bool scout_reporting, | |
101 bool scout_group) { | |
102 LOG(INFO) << "Prefs exist: sber=" << sber_reporting | |
103 << " scout=" << scout_reporting << " scout_group=" << scout_group; | |
104 EXPECT_EQ(sber_reporting, | |
105 prefs_.HasPrefPath(prefs::kSafeBrowsingExtendedReportingEnabled)); | |
106 EXPECT_EQ(scout_reporting, | |
107 prefs_.HasPrefPath(prefs::kSafeBrowsingScoutReportingEnabled)); | |
108 EXPECT_EQ(scout_group, | |
109 prefs_.HasPrefPath(prefs::kSafeBrowsingScoutGroupSelected)); | |
110 } | |
111 TestingPrefServiceSimple prefs_; | |
112 | |
113 private: | |
114 std::unique_ptr<base::test::ScopedFeatureList> feature_list_; | |
115 }; | |
116 | |
117 // This test ensures that we correctly select between SBER and Scout as the | |
118 // active preference in a number of common scenarios. | |
119 TEST_F(SafeBrowsingPrefsTest, GetExtendedReportingPrefName_Common) { | |
120 const std::string& sber = prefs::kSafeBrowsingExtendedReportingEnabled; | |
121 const std::string& scout = prefs::kSafeBrowsingScoutReportingEnabled; | |
122 | |
123 // By default (all prefs and experiment features disabled), SBER pref is used. | |
124 TestGetPrefName(false, false, false, false, false, sber); | |
125 | |
126 // Changing any prefs (including ScoutGroupSelected) keeps SBER as the active | |
127 // pref because the experiment remains in the Control group. | |
128 TestGetPrefName(/*sber=*/true, false, false, false, false, sber); | |
129 TestGetPrefName(false, /*scout=*/true, false, false, false, sber); | |
130 TestGetPrefName(false, false, /*scout_group=*/true, false, false, sber); | |
131 | |
132 // Being in either experiment group with ScoutGroup selected makes Scout the | |
133 // active pref. | |
134 TestGetPrefName(false, false, /*scout_group=*/true, /*can_show_scout=*/true, | |
135 false, scout); | |
136 TestGetPrefName(false, false, /*scout_group=*/true, false, | |
137 /*only_show_scout=*/true, scout); | |
138 | |
139 // When ScoutGroup is not selected then SBER remains the active pref, | |
140 // regardless which experiment is enabled. | |
141 TestGetPrefName(false, false, false, /*can_show_scout=*/true, false, sber); | |
142 TestGetPrefName(false, false, false, false, /*only_show_scout=*/true, sber); | |
143 } | |
144 | |
145 // Here we exhaustively check all combinations of pref and experiment states. | |
146 // This should help catch regressions. | |
147 TEST_F(SafeBrowsingPrefsTest, GetExtendedReportingPrefName_Exhaustive) { | |
148 const std::string& sber = prefs::kSafeBrowsingExtendedReportingEnabled; | |
149 const std::string& scout = prefs::kSafeBrowsingScoutReportingEnabled; | |
150 TestGetPrefName(false, false, false, false, false, sber); | |
151 TestGetPrefName(false, false, false, false, true, sber); | |
152 TestGetPrefName(false, false, false, true, false, sber); | |
153 TestGetPrefName(false, false, false, true, true, sber); | |
154 TestGetPrefName(false, false, true, false, false, sber); | |
155 TestGetPrefName(false, false, true, false, true, scout); | |
156 TestGetPrefName(false, false, true, true, false, scout); | |
157 TestGetPrefName(false, false, true, true, true, scout); | |
158 TestGetPrefName(false, true, false, false, false, sber); | |
159 TestGetPrefName(false, true, false, false, true, sber); | |
160 TestGetPrefName(false, true, false, true, false, sber); | |
161 TestGetPrefName(false, true, false, true, true, sber); | |
162 TestGetPrefName(false, true, true, false, false, sber); | |
163 TestGetPrefName(false, true, true, false, true, scout); | |
164 TestGetPrefName(false, true, true, true, false, scout); | |
165 TestGetPrefName(false, true, true, true, true, scout); | |
166 TestGetPrefName(true, false, false, false, false, sber); | |
167 TestGetPrefName(true, false, false, false, true, sber); | |
168 TestGetPrefName(true, false, false, true, false, sber); | |
169 TestGetPrefName(true, false, false, true, true, sber); | |
170 TestGetPrefName(true, false, true, false, false, sber); | |
171 TestGetPrefName(true, false, true, false, true, scout); | |
172 TestGetPrefName(true, false, true, true, false, scout); | |
173 TestGetPrefName(true, false, true, true, true, scout); | |
174 TestGetPrefName(true, true, false, false, false, sber); | |
175 TestGetPrefName(true, true, false, false, true, sber); | |
176 TestGetPrefName(true, true, false, true, false, sber); | |
177 TestGetPrefName(true, true, false, true, true, sber); | |
178 TestGetPrefName(true, true, true, false, false, sber); | |
179 TestGetPrefName(true, true, true, false, true, scout); | |
180 TestGetPrefName(true, true, true, true, false, scout); | |
181 TestGetPrefName(true, true, true, true, true, scout); | |
182 } | |
183 | |
184 // Basic test that command-line flags can force the ScoutGroupSelected pref on | |
185 // or off. | |
186 TEST_F(SafeBrowsingPrefsTest, InitPrefs_ForceScoutGroupOnOff) { | |
187 // By default ScoutGroupSelected is off. | |
188 EXPECT_FALSE(IsScoutGroupSelected()); | |
189 | |
190 // Command-line flag can force it on during initialization. | |
191 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
192 kSwitchForceScoutGroup, "true"); | |
193 InitPrefs(); | |
194 EXPECT_TRUE(IsScoutGroupSelected()); | |
195 | |
196 // ScoutGroup remains on if switches are cleared, but only if an experiment | |
197 // is active (since being in the Control group automatically clears the | |
198 // Scout prefs). | |
199 base::CommandLine::StringVector empty; | |
200 base::CommandLine::ForCurrentProcess()->InitFromArgv(empty); | |
201 ResetExperiments(/*can_show_scout=*/true, /*only_show_scout=*/false); | |
202 EXPECT_TRUE(IsScoutGroupSelected()); | |
203 | |
204 // Nonsense values are ignored and ScoutGroup is unchanged. | |
205 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
206 kSwitchForceScoutGroup, "foo"); | |
207 InitPrefs(); | |
208 EXPECT_TRUE(IsScoutGroupSelected()); | |
209 | |
210 // ScoutGroup can also be forced off during initialization. | |
211 base::CommandLine::ForCurrentProcess()->InitFromArgv(empty); | |
212 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
213 kSwitchForceScoutGroup, "false"); | |
214 InitPrefs(); | |
215 EXPECT_FALSE(IsScoutGroupSelected()); | |
216 } | |
217 | |
218 // Test all combinations of prefs during initialization when neither experiment | |
219 // is on (ie: control group). In all cases the Scout prefs should be cleared, | |
220 // and the SBER pref may get switched. | |
221 TEST_F(SafeBrowsingPrefsTest, InitPrefs_Control) { | |
222 // Turn both experiments off. | |
223 ResetExperiments(/*can_show_scout=*/false, /*only_show_scout=*/false); | |
224 | |
225 // Default case (everything off) - no change on init. | |
226 ResetPrefs(false, false, false); | |
227 InitPrefs(); | |
228 ExpectPrefs(false, false, false); | |
229 // SBER pref exists because it was set to false above. | |
230 ExpectPrefsExist(true, false, false); | |
231 | |
232 // ScoutGroup on - SBER cleared since Scout opt-in was shown but Scout pref | |
233 // was not chosen. Scout prefs cleared. | |
234 ResetPrefs(false, false, true); | |
235 InitPrefs(); | |
236 ExpectPrefs(false, false, false); | |
237 ExpectPrefsExist(true, false, false); | |
238 | |
239 // ScoutReporting on without ScoutGroup - SBER turns on since user opted-in to | |
240 // broader Scout reporting, we can continue collecting the SBER subset. Scout | |
241 // prefs cleared. | |
242 ResetPrefs(false, true, false); | |
243 InitPrefs(); | |
244 ExpectPrefs(true, false, false); | |
245 ExpectPrefsExist(true, false, false); | |
246 | |
247 // ScoutReporting and ScoutGroup on - SBER turns on since user opted-in to | |
248 // broader Scout reporting, we can continue collecting the SBER subset. Scout | |
249 // prefs cleared. | |
250 ResetPrefs(false, true, true); | |
251 InitPrefs(); | |
252 ExpectPrefs(true, false, false); | |
253 ExpectPrefsExist(true, false, false); | |
254 | |
255 // SBER on - no change on init since ScoutGroup is off implying that user | |
256 // never saw Scout opt-in text. Scout prefs remain cleared. | |
257 ResetPrefs(true, false, false); | |
258 InitPrefs(); | |
259 ExpectPrefs(true, false, false); | |
260 ExpectPrefsExist(true, false, false); | |
261 | |
262 // SBER and ScoutGroup on - SBER cleared. User previously opted-in to SBER | |
263 // and they saw Scout opt-in text (ie. ScoutGroup on), but chose not to opt-in | |
264 // to Scout reporting. We want them to re-evaluate their choice of SBER since | |
265 // the lack of Scout opt-in was a conscious choice. Scout cleared. | |
266 ResetPrefs(true, false, true); | |
267 InitPrefs(); | |
268 ExpectPrefs(false, false, false); | |
269 ExpectPrefsExist(true, false, false); | |
270 | |
271 // SBER and ScoutReporting on. User has opted-in to broader level of reporting | |
272 // so SBER stays on. Scout prefs cleared. | |
273 ResetPrefs(true, true, false); | |
274 InitPrefs(); | |
275 ExpectPrefs(true, false, false); | |
276 ExpectPrefsExist(true, false, false); | |
277 | |
278 // Everything on. User has opted-in to broader level of reporting so SBER | |
279 // stays on. Scout prefs cleared. | |
280 ResetPrefs(true, true, true); | |
281 InitPrefs(); | |
282 ExpectPrefs(true, false, false); | |
283 ExpectPrefs(true, false, false); | |
284 } | |
285 | |
286 // Tests a unique case where the Extended Reporting pref will be Cleared instead | |
287 // of set to False in order to mimic the state of the Scout reporting pref. | |
288 // This happens when a user is in the OnlyShowScoutOptIn experiment but never | |
289 // encounters a security issue so never sees the Scout opt-in. This user then | |
290 // returns to the Control group having never seen the Scout opt-in, so their | |
291 // Scout Reporting pref is un-set. We want to return their SBER pref to the | |
292 // unset state as well. | |
293 TEST_F(SafeBrowsingPrefsTest, InitPrefs_Control_SberPrefCleared) { | |
294 // Turn both experiments off. | |
295 ResetExperiments(/*can_show_scout=*/false, /*only_show_scout=*/false); | |
296 | |
297 // Set the user's old SBER pref to on to be explicit. | |
298 prefs_.SetBoolean(prefs::kSafeBrowsingExtendedReportingEnabled, true); | |
299 // User is in the OnlyShowScoutOptIn experiment so they go directly to the | |
300 // Scout Group. | |
301 prefs_.SetBoolean(prefs::kSafeBrowsingScoutGroupSelected, true); | |
302 // But they never see a security popup or change the setting manually so the | |
303 // Scout pref remains unset. | |
304 prefs_.ClearPref(prefs::kSafeBrowsingScoutReportingEnabled); | |
305 | |
306 InitPrefs(); | |
307 | |
308 // All pref values should be false and unset. | |
309 ExpectPrefs(false, false, false); | |
310 ExpectPrefsExist(false, false, false); | |
311 } | |
312 | |
313 // Test all combinations of prefs during initialization when the CanShowScout | |
314 // experiment is on. | |
315 TEST_F(SafeBrowsingPrefsTest, InitPrefs_CanShowScout) { | |
316 // Turn the CanShowScout experiment on. | |
317 ResetExperiments(/*can_show_scout=*/true, /*only_show_scout=*/false); | |
318 | |
319 // Default case (everything off) - ScoutGroup turns on because SBER is off. | |
320 ResetPrefs(false, false, false); | |
321 InitPrefs(); | |
322 ExpectPrefs(false, false, true); | |
323 | |
324 // ScoutGroup on - no change on init since ScoutGroup is already on. | |
325 ResetPrefs(false, false, true); | |
326 InitPrefs(); | |
327 ExpectPrefs(false, false, true); | |
328 | |
329 // ScoutReporting on without ScoutGroup - ScoutGroup turns on because SBER is | |
330 // off. | |
331 ResetPrefs(false, true, false); | |
332 InitPrefs(); | |
333 ExpectPrefs(false, true, true); | |
334 | |
335 // ScoutReporting and ScoutGroup on - no change on init since ScoutGroup is | |
336 // already on. | |
337 ResetPrefs(false, true, true); | |
338 InitPrefs(); | |
339 ExpectPrefs(false, true, true); | |
340 | |
341 // SBER on - no change on init. Will wait for first security incident before | |
342 // turning on ScoutGroup and displaying the Scout opt-in. | |
343 ResetPrefs(true, false, false); | |
344 InitPrefs(); | |
345 ExpectPrefs(true, false, false); | |
346 | |
347 // SBER and ScoutGroup on - no change on init since ScoutGroup is already on. | |
348 ResetPrefs(true, false, true); | |
349 InitPrefs(); | |
350 ExpectPrefs(true, false, true); | |
351 | |
352 // SBER and ScoutReporting on - no change on init because SBER is on. | |
353 // ScoutGroup will turn on on next security incident. | |
354 ResetPrefs(true, true, false); | |
355 InitPrefs(); | |
356 ExpectPrefs(true, true, false); | |
357 | |
358 // Everything on - no change on init since ScoutGroup is already on. | |
359 ResetPrefs(true, true, true); | |
360 InitPrefs(); | |
361 ExpectPrefs(true, true, true); | |
362 } | |
363 | |
364 // Test all combinations of prefs during initialization when the OnlyShowScout | |
365 // experiment is on. | |
366 TEST_F(SafeBrowsingPrefsTest, InitPrefs_OnlyShowScout) { | |
367 // Turn the OnlyShowScout experiment on. | |
368 ResetExperiments(/*can_show_scout=*/false, /*only_show_scout=*/true); | |
369 | |
370 // Default case (everything off) - ScoutGroup turns on. | |
371 ResetPrefs(false, false, false); | |
372 InitPrefs(); | |
373 ExpectPrefs(false, false, true); | |
374 | |
375 // ScoutGroup on - no change on init since ScoutGroup is already on. | |
376 ResetPrefs(false, false, true); | |
377 InitPrefs(); | |
378 ExpectPrefs(false, false, true); | |
379 | |
380 // ScoutReporting on without ScoutGroup - ScoutGroup turns on. | |
381 ResetPrefs(false, true, false); | |
382 InitPrefs(); | |
383 ExpectPrefs(false, true, true); | |
384 | |
385 // ScoutReporting and ScoutGroup on - no change on init since ScoutGroup is | |
386 // already on. | |
387 ResetPrefs(false, true, true); | |
388 InitPrefs(); | |
389 ExpectPrefs(false, true, true); | |
390 | |
391 // SBER on - ScoutGroup turns on immediately, not waiting for first security | |
392 // incident. | |
393 ResetPrefs(true, false, false); | |
394 InitPrefs(); | |
395 ExpectPrefs(true, false, true); | |
396 | |
397 // SBER and ScoutGroup on - no change on init since ScoutGroup is already on. | |
398 ResetPrefs(true, false, true); | |
399 InitPrefs(); | |
400 ExpectPrefs(true, false, true); | |
401 | |
402 // SBER and ScoutReporting on - ScoutGroup turns on immediately, not waiting | |
403 // for first security incident. | |
404 ResetPrefs(true, true, false); | |
405 InitPrefs(); | |
406 ExpectPrefs(true, true, true); | |
407 | |
408 // Everything on - no change on init since ScoutGroup is already on. | |
409 ResetPrefs(true, true, true); | |
410 InitPrefs(); | |
411 ExpectPrefs(true, true, true); | |
412 } | |
413 | |
414 TEST_F(SafeBrowsingPrefsTest, ChooseOptInText) { | |
415 const int kSberResource = 100; | |
416 const int kScoutResource = 500; | |
417 // By default, SBER opt-in is used | |
418 EXPECT_EQ(kSberResource, | |
419 ChooseOptInTextResource(prefs_, kSberResource, kScoutResource)); | |
420 | |
421 // Enabling Scout switches to the Scout opt-in text. | |
422 ResetExperiments(/*can_show_scout=*/false, /*only_show_scout=*/true); | |
423 ResetPrefs(/*sber=*/false, /*scout=*/false, /*scout_group=*/true); | |
424 EXPECT_EQ(kScoutResource, | |
425 ChooseOptInTextResource(prefs_, kSberResource, kScoutResource)); | |
426 } | |
427 | |
428 TEST_F(SafeBrowsingPrefsTest, GetSafeBrowsingExtendedReportingLevel) { | |
429 // By Default, SBER is off | |
430 EXPECT_EQ(SBER_LEVEL_OFF, GetExtendedReportingLevel(prefs_)); | |
431 | |
432 // Opt-in to Legacy SBER gives Legacy reporting leve. | |
433 ResetPrefs(/*sber=*/true, /*scout_reporting=*/false, /*scout_group=*/false); | |
434 EXPECT_EQ(SBER_LEVEL_LEGACY, GetExtendedReportingLevel(prefs_)); | |
435 | |
436 // The value of the Scout pref doesn't change the reporting level if the user | |
437 // is outside of the Scout Group and/or no experiment is running. | |
438 // No scout group. | |
439 ResetPrefs(/*sber=*/true, /*scout_reporting=*/true, /*scout_group=*/false); | |
440 EXPECT_EQ(SBER_LEVEL_LEGACY, GetExtendedReportingLevel(prefs_)); | |
441 // Scout group but no experiment. | |
442 ResetPrefs(/*sber=*/true, /*scout_reporting=*/true, /*scout_group=*/true); | |
443 EXPECT_EQ(SBER_LEVEL_LEGACY, GetExtendedReportingLevel(prefs_)); | |
444 | |
445 // Remaining in the Scout Group and adding an experiment will switch to the | |
446 // Scout pref to determine reporting level. | |
447 ResetExperiments(/*can_show_scout=*/false, /*only_show_scout=*/true); | |
448 // Both reporting prefs off, so reporting is off. | |
449 ResetPrefs(/*sber=*/false, /*scout_reporting=*/false, /*scout_group=*/true); | |
450 EXPECT_EQ(SBER_LEVEL_OFF, GetExtendedReportingLevel(prefs_)); | |
451 // Legacy pref on when we're using Scout - reporting remains off. | |
452 ResetPrefs(/*sber=*/true, /*scout_reporting=*/false, /*scout_group=*/true); | |
453 EXPECT_EQ(SBER_LEVEL_OFF, GetExtendedReportingLevel(prefs_)); | |
454 // Turning on Scout gives us Scout level reporting | |
455 ResetPrefs(/*sber=*/false, /*scout_reporting=*/true, /*scout_group=*/true); | |
456 EXPECT_EQ(SBER_LEVEL_SCOUT, GetExtendedReportingLevel(prefs_)); | |
457 // .. and the legacy pref doesn't affect this. | |
458 ResetPrefs(/*sber=*/true, /*scout_reporting=*/true, /*scout_group=*/true); | |
459 EXPECT_EQ(SBER_LEVEL_SCOUT, GetExtendedReportingLevel(prefs_)); | |
460 } | |
461 | |
462 } // namespace safe_browsing | |
OLD | NEW |