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

Side by Side Diff: chrome/browser/safe_browsing/incident_reporting/preference_validation_delegate_unittest.cc

Issue 2384213002: Send a TrackedPreference incident when registry pref validation fails. (Closed)
Patch Set: Address comments on patch set 4 Created 4 years, 2 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/safe_browsing/incident_reporting/preference_validation_ delegate.h" 5 #include "chrome/browser/safe_browsing/incident_reporting/preference_validation_ delegate.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <string> 10 #include <string>
(...skipping 22 matching lines...) Expand all
33 typedef std::vector<std::unique_ptr<safe_browsing::Incident>> IncidentVector; 33 typedef std::vector<std::unique_ptr<safe_browsing::Incident>> IncidentVector;
34 34
35 PreferenceValidationDelegateTest() 35 PreferenceValidationDelegateTest()
36 : kPrefPath_("atomic.pref"), 36 : kPrefPath_("atomic.pref"),
37 null_value_(base::Value::CreateNullValue()) {} 37 null_value_(base::Value::CreateNullValue()) {}
38 38
39 void SetUp() override { 39 void SetUp() override {
40 testing::Test::SetUp(); 40 testing::Test::SetUp();
41 invalid_keys_.push_back(std::string("one")); 41 invalid_keys_.push_back(std::string("one"));
42 invalid_keys_.push_back(std::string("two")); 42 invalid_keys_.push_back(std::string("two"));
43 external_validation_invalid_keys_.push_back(std::string("three"));
43 std::unique_ptr<safe_browsing::MockIncidentReceiver> receiver( 44 std::unique_ptr<safe_browsing::MockIncidentReceiver> receiver(
44 new NiceMock<safe_browsing::MockIncidentReceiver>()); 45 new NiceMock<safe_browsing::MockIncidentReceiver>());
45 ON_CALL(*receiver, DoAddIncidentForProfile(IsNull(), _)) 46 ON_CALL(*receiver, DoAddIncidentForProfile(IsNull(), _))
46 .WillByDefault(WithArg<1>(TakeIncidentToVector(&incidents_))); 47 .WillByDefault(WithArg<1>(TakeIncidentToVector(&incidents_)));
47 instance_.reset(new safe_browsing::PreferenceValidationDelegate( 48 instance_.reset(new safe_browsing::PreferenceValidationDelegate(
48 nullptr, std::move(receiver))); 49 nullptr, std::move(receiver)));
49 } 50 }
50 51
51 static void ExpectValueStatesEquate( 52 static void ExpectValueStatesEquate(
52 PrefHashStoreTransaction::ValueState store_state, 53 PrefHashStoreTransaction::ValueState store_state,
54 PrefHashStoreTransaction::ValueState external_validation_value_state,
53 safe_browsing:: 55 safe_browsing::
54 ClientIncidentReport_IncidentData_TrackedPreferenceIncident_ValueState 56 ClientIncidentReport_IncidentData_TrackedPreferenceIncident_ValueState
55 incident_state) { 57 incident_state) {
56 typedef safe_browsing:: 58 typedef safe_browsing::
57 ClientIncidentReport_IncidentData_TrackedPreferenceIncident TPIncident; 59 ClientIncidentReport_IncidentData_TrackedPreferenceIncident TPIncident;
58 switch (store_state) { 60 switch (store_state) {
59 case PrefHashStoreTransaction::CLEARED: 61 case PrefHashStoreTransaction::CLEARED:
60 EXPECT_EQ(TPIncident::CLEARED, incident_state); 62 EXPECT_EQ(TPIncident::CLEARED, incident_state);
61 break; 63 break;
62 case PrefHashStoreTransaction::CHANGED: 64 case PrefHashStoreTransaction::CHANGED:
63 EXPECT_EQ(TPIncident::CHANGED, incident_state); 65 EXPECT_EQ(TPIncident::CHANGED, incident_state);
64 break; 66 break;
65 case PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE: 67 case PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE:
66 EXPECT_EQ(TPIncident::UNTRUSTED_UNKNOWN_VALUE, incident_state); 68 EXPECT_EQ(TPIncident::UNTRUSTED_UNKNOWN_VALUE, incident_state);
67 break; 69 break;
68 default: 70 default:
69 FAIL() << "unexpected store state"; 71 if (external_validation_value_state ==
72 PrefHashStoreTransaction::CLEARED) {
73 EXPECT_EQ(TPIncident::BYPASS_CLEARED, incident_state);
74 } else if (external_validation_value_state ==
75 PrefHashStoreTransaction::CHANGED) {
76 EXPECT_EQ(TPIncident::BYPASS_CHANGED, incident_state);
77 } else {
78 FAIL() << "unexpected store state";
79 }
70 break; 80 break;
71 } 81 }
72 } 82 }
73 83
74 static void ExpectKeysEquate( 84 static void ExpectKeysEquate(
75 const std::vector<std::string>& store_keys, 85 const std::vector<std::string>& store_keys,
76 const google::protobuf::RepeatedPtrField<std::string>& incident_keys) { 86 const google::protobuf::RepeatedPtrField<std::string>& incident_keys) {
77 ASSERT_EQ(store_keys.size(), static_cast<size_t>(incident_keys.size())); 87 ASSERT_EQ(store_keys.size(), static_cast<size_t>(incident_keys.size()));
78 for (int i = 0; i < incident_keys.size(); ++i) { 88 for (int i = 0; i < incident_keys.size(); ++i) {
79 EXPECT_EQ(store_keys[i], incident_keys.Get(i)); 89 EXPECT_EQ(store_keys[i], incident_keys.Get(i));
80 } 90 }
81 } 91 }
82 92
83 const std::string kPrefPath_; 93 const std::string kPrefPath_;
84 IncidentVector incidents_; 94 IncidentVector incidents_;
85 std::unique_ptr<base::Value> null_value_; 95 std::unique_ptr<base::Value> null_value_;
86 base::DictionaryValue dict_value_; 96 base::DictionaryValue dict_value_;
87 std::vector<std::string> invalid_keys_; 97 std::vector<std::string> invalid_keys_;
98 std::vector<std::string> external_validation_invalid_keys_;
88 std::unique_ptr<TrackedPreferenceValidationDelegate> instance_; 99 std::unique_ptr<TrackedPreferenceValidationDelegate> instance_;
89 }; 100 };
90 101
91 // Tests that a NULL value results in an incident with no value. 102 // Tests that a NULL value results in an incident with no value.
92 TEST_F(PreferenceValidationDelegateTest, NullValue) { 103 TEST_F(PreferenceValidationDelegateTest, NullValue) {
93 instance_->OnAtomicPreferenceValidation(kPrefPath_, 104 instance_->OnAtomicPreferenceValidation(
94 NULL, 105 kPrefPath_, NULL, PrefHashStoreTransaction::CLEARED,
95 PrefHashStoreTransaction::CLEARED, 106 PrefHashStoreTransaction::UNSUPPORTED, false /* is_personal */);
96 false /* is_personal */);
97 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( 107 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident(
98 incidents_.back()->TakePayload()); 108 incidents_.back()->TakePayload());
99 EXPECT_FALSE(incident->tracked_preference().has_atomic_value()); 109 EXPECT_FALSE(incident->tracked_preference().has_atomic_value());
100 EXPECT_EQ( 110 EXPECT_EQ(
101 safe_browsing:: 111 safe_browsing::
102 ClientIncidentReport_IncidentData_TrackedPreferenceIncident::CLEARED, 112 ClientIncidentReport_IncidentData_TrackedPreferenceIncident::CLEARED,
103 incident->tracked_preference().value_state()); 113 incident->tracked_preference().value_state());
104 } 114 }
105 115
106 // Tests that all supported value types can be stringified into an incident. The 116 // Tests that all supported value types can be stringified into an incident. The
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 ADD_FAILURE() << "unsupported value type " << value_type; 157 ADD_FAILURE() << "unsupported value type " << value_type;
148 } 158 }
149 return std::unique_ptr<Value>(); 159 return std::unique_ptr<Value>();
150 } 160 }
151 161
152 base::Value::Type value_type_; 162 base::Value::Type value_type_;
153 const char* expected_value_; 163 const char* expected_value_;
154 }; 164 };
155 165
156 TEST_P(PreferenceValidationDelegateValues, Value) { 166 TEST_P(PreferenceValidationDelegateValues, Value) {
157 instance_->OnAtomicPreferenceValidation(kPrefPath_, 167 instance_->OnAtomicPreferenceValidation(
158 MakeValue(value_type_).get(), 168 kPrefPath_, MakeValue(value_type_).get(),
159 PrefHashStoreTransaction::CLEARED, 169 PrefHashStoreTransaction::CLEARED, PrefHashStoreTransaction::UNSUPPORTED,
160 false /* is_personal */); 170 false /* is_personal */);
161 ASSERT_EQ(1U, incidents_.size()); 171 ASSERT_EQ(1U, incidents_.size());
162 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( 172 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident(
163 incidents_.back()->TakePayload()); 173 incidents_.back()->TakePayload());
164 EXPECT_EQ(std::string(expected_value_), 174 EXPECT_EQ(std::string(expected_value_),
165 incident->tracked_preference().atomic_value()); 175 incident->tracked_preference().atomic_value());
166 } 176 }
167 177
168 INSTANTIATE_TEST_CASE_P( 178 INSTANTIATE_TEST_CASE_P(
169 Values, 179 Values,
170 PreferenceValidationDelegateValues, 180 PreferenceValidationDelegateValues,
(...skipping 12 matching lines...) Expand all
183 std::tr1::make_tuple(base::Value::TYPE_STRING, 193 std::tr1::make_tuple(base::Value::TYPE_STRING,
184 const_cast<char*>("i have a spleen")), 194 const_cast<char*>("i have a spleen")),
185 std::tr1::make_tuple(base::Value::TYPE_DICTIONARY, 195 std::tr1::make_tuple(base::Value::TYPE_DICTIONARY,
186 const_cast<char*>("{\"forty-seven\":47,\"twenty-two\":22}")), 196 const_cast<char*>("{\"forty-seven\":47,\"twenty-two\":22}")),
187 std::tr1::make_tuple(base::Value::TYPE_LIST, 197 std::tr1::make_tuple(base::Value::TYPE_LIST,
188 const_cast<char*>("[22,47]")))); 198 const_cast<char*>("[22,47]"))));
189 199
190 // Tests that no incidents are reported for relevant combinations of ValueState. 200 // Tests that no incidents are reported for relevant combinations of ValueState.
191 class PreferenceValidationDelegateNoIncident 201 class PreferenceValidationDelegateNoIncident
192 : public PreferenceValidationDelegateTest, 202 : public PreferenceValidationDelegateTest,
193 public testing::WithParamInterface<PrefHashStoreTransaction::ValueState> { 203 public testing::WithParamInterface<
204 std::tr1::tuple<PrefHashStoreTransaction::ValueState,
205 PrefHashStoreTransaction::ValueState>> {
194 protected: 206 protected:
195 void SetUp() override { 207 void SetUp() override {
196 PreferenceValidationDelegateTest::SetUp(); 208 PreferenceValidationDelegateTest::SetUp();
197 value_state_ = GetParam(); 209 value_state_ = std::tr1::get<0>(GetParam());
210 external_validation_value_state_ = std::tr1::get<1>(GetParam());
198 } 211 }
199 212
200 PrefHashStoreTransaction::ValueState value_state_; 213 PrefHashStoreTransaction::ValueState value_state_;
214 PrefHashStoreTransaction::ValueState external_validation_value_state_;
201 }; 215 };
202 216
203 TEST_P(PreferenceValidationDelegateNoIncident, Atomic) { 217 TEST_P(PreferenceValidationDelegateNoIncident, Atomic) {
204 instance_->OnAtomicPreferenceValidation(kPrefPath_, 218 instance_->OnAtomicPreferenceValidation(
205 null_value_.get(), 219 kPrefPath_, null_value_.get(), value_state_,
206 value_state_, 220 external_validation_value_state_, false /* is_personal */);
207 false /* is_personal */);
208 EXPECT_EQ(0U, incidents_.size()); 221 EXPECT_EQ(0U, incidents_.size());
209 } 222 }
210 223
211 TEST_P(PreferenceValidationDelegateNoIncident, Split) { 224 TEST_P(PreferenceValidationDelegateNoIncident, Split) {
212 instance_->OnSplitPreferenceValidation(kPrefPath_, 225 instance_->OnSplitPreferenceValidation(
213 &dict_value_, 226 kPrefPath_, &dict_value_, invalid_keys_,
214 invalid_keys_, 227 external_validation_invalid_keys_, value_state_,
215 value_state_, 228 external_validation_value_state_, false /* is_personal */);
216 false /* is_personal */);
217 EXPECT_EQ(0U, incidents_.size()); 229 EXPECT_EQ(0U, incidents_.size());
218 } 230 }
219 231
220 INSTANTIATE_TEST_CASE_P( 232 INSTANTIATE_TEST_CASE_P(
221 NoIncident, 233 NoIncident,
222 PreferenceValidationDelegateNoIncident, 234 PreferenceValidationDelegateNoIncident,
223 testing::Values(PrefHashStoreTransaction::UNCHANGED, 235 testing::Combine(
224 PrefHashStoreTransaction::SECURE_LEGACY, 236 testing::Values(PrefHashStoreTransaction::UNCHANGED,
225 PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE)); 237 PrefHashStoreTransaction::SECURE_LEGACY,
238 PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE),
239 testing::Values(PrefHashStoreTransaction::UNCHANGED,
240 PrefHashStoreTransaction::UNSUPPORTED,
241 PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE)));
226 242
227 // Tests that incidents are reported for relevant combinations of ValueState and 243 // Tests that incidents are reported for relevant combinations of ValueState and
228 // impersonal/personal. 244 // impersonal/personal.
229 class PreferenceValidationDelegateWithIncident 245 class PreferenceValidationDelegateWithIncident
230 : public PreferenceValidationDelegateTest, 246 : public PreferenceValidationDelegateTest,
231 public testing::WithParamInterface< 247 public testing::WithParamInterface<
232 std::tr1::tuple<PrefHashStoreTransaction::ValueState, bool>> { 248 std::tr1::tuple<PrefHashStoreTransaction::ValueState,
249 PrefHashStoreTransaction::ValueState,
250 bool>> {
233 protected: 251 protected:
234 void SetUp() override { 252 void SetUp() override {
235 PreferenceValidationDelegateTest::SetUp(); 253 PreferenceValidationDelegateTest::SetUp();
236 value_state_ = std::tr1::get<0>(GetParam()); 254 value_state_ = std::tr1::get<0>(GetParam());
237 is_personal_ = std::tr1::get<1>(GetParam()); 255 external_validation_value_state_ = std::tr1::get<1>(GetParam());
256 is_personal_ = std::tr1::get<2>(GetParam());
238 } 257 }
239 258
240 PrefHashStoreTransaction::ValueState value_state_; 259 PrefHashStoreTransaction::ValueState value_state_;
260 PrefHashStoreTransaction::ValueState external_validation_value_state_;
241 bool is_personal_; 261 bool is_personal_;
242 }; 262 };
243 263
244 TEST_P(PreferenceValidationDelegateWithIncident, Atomic) { 264 TEST_P(PreferenceValidationDelegateWithIncident, Atomic) {
245 instance_->OnAtomicPreferenceValidation( 265 instance_->OnAtomicPreferenceValidation(
246 kPrefPath_, null_value_.get(), value_state_, is_personal_); 266 kPrefPath_, null_value_.get(), value_state_,
267 external_validation_value_state_, is_personal_);
247 ASSERT_EQ(1U, incidents_.size()); 268 ASSERT_EQ(1U, incidents_.size());
248 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( 269 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident(
249 incidents_.back()->TakePayload()); 270 incidents_.back()->TakePayload());
250 EXPECT_TRUE(incident->has_tracked_preference()); 271 EXPECT_TRUE(incident->has_tracked_preference());
251 const safe_browsing:: 272 const safe_browsing::
252 ClientIncidentReport_IncidentData_TrackedPreferenceIncident& tp_incident = 273 ClientIncidentReport_IncidentData_TrackedPreferenceIncident& tp_incident =
253 incident->tracked_preference(); 274 incident->tracked_preference();
254 EXPECT_EQ(kPrefPath_, tp_incident.path()); 275 EXPECT_EQ(kPrefPath_, tp_incident.path());
255 EXPECT_EQ(0, tp_incident.split_key_size()); 276 EXPECT_EQ(0, tp_incident.split_key_size());
256 if (!is_personal_) { 277 if (!is_personal_) {
257 EXPECT_TRUE(tp_incident.has_atomic_value()); 278 EXPECT_TRUE(tp_incident.has_atomic_value());
258 EXPECT_EQ(std::string("null"), tp_incident.atomic_value()); 279 EXPECT_EQ(std::string("null"), tp_incident.atomic_value());
259 } else { 280 } else {
260 EXPECT_FALSE(tp_incident.has_atomic_value()); 281 EXPECT_FALSE(tp_incident.has_atomic_value());
261 } 282 }
262 EXPECT_TRUE(tp_incident.has_value_state()); 283 EXPECT_TRUE(tp_incident.has_value_state());
263 ExpectValueStatesEquate(value_state_, tp_incident.value_state()); 284 ExpectValueStatesEquate(value_state_, external_validation_value_state_,
285 tp_incident.value_state());
264 } 286 }
265 287
266 TEST_P(PreferenceValidationDelegateWithIncident, Split) { 288 TEST_P(PreferenceValidationDelegateWithIncident, Split) {
267 instance_->OnSplitPreferenceValidation( 289 instance_->OnSplitPreferenceValidation(
268 kPrefPath_, &dict_value_, invalid_keys_, value_state_, is_personal_); 290 kPrefPath_, &dict_value_, invalid_keys_,
291 external_validation_invalid_keys_, value_state_,
292 external_validation_value_state_, is_personal_);
269 ASSERT_EQ(1U, incidents_.size()); 293 ASSERT_EQ(1U, incidents_.size());
270 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( 294 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident(
271 incidents_.back()->TakePayload()); 295 incidents_.back()->TakePayload());
272 EXPECT_TRUE(incident->has_tracked_preference()); 296 EXPECT_TRUE(incident->has_tracked_preference());
273 const safe_browsing:: 297 const safe_browsing::
274 ClientIncidentReport_IncidentData_TrackedPreferenceIncident& tp_incident = 298 ClientIncidentReport_IncidentData_TrackedPreferenceIncident& tp_incident =
275 incident->tracked_preference(); 299 incident->tracked_preference();
276 EXPECT_EQ(kPrefPath_, tp_incident.path()); 300 EXPECT_EQ(kPrefPath_, tp_incident.path());
277 EXPECT_FALSE(tp_incident.has_atomic_value()); 301 EXPECT_FALSE(tp_incident.has_atomic_value());
278 if (!is_personal_) 302 if (!is_personal_) {
279 ExpectKeysEquate(invalid_keys_, tp_incident.split_key()); 303 if (value_state_ == PrefHashStoreTransaction::CLEARED ||
280 else 304 value_state_ == PrefHashStoreTransaction::CHANGED ||
305 value_state_ == PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE) {
306 ExpectKeysEquate(invalid_keys_, tp_incident.split_key());
307 } else {
308 ExpectKeysEquate(external_validation_invalid_keys_,
309 tp_incident.split_key());
310 }
311 } else {
281 EXPECT_EQ(0, tp_incident.split_key_size()); 312 EXPECT_EQ(0, tp_incident.split_key_size());
gab 2016/10/05 19:22:26 Just like this verifies size is zero when not repo
proberge 2016/10/05 19:54:05 I think it already does so implicitly; tp_incident
gab 2016/10/05 20:08:16 Ah, indeed :-), lgtm++
313 }
282 EXPECT_TRUE(tp_incident.has_value_state()); 314 EXPECT_TRUE(tp_incident.has_value_state());
283 ExpectValueStatesEquate(value_state_, tp_incident.value_state()); 315 ExpectValueStatesEquate(value_state_, external_validation_value_state_,
316 tp_incident.value_state());
284 } 317 }
285 318
286 INSTANTIATE_TEST_CASE_P( 319 INSTANTIATE_TEST_CASE_P(
287 WithIncident, 320 WithIncident,
288 PreferenceValidationDelegateWithIncident, 321 PreferenceValidationDelegateWithIncident,
289 testing::Combine( 322 testing::Combine(
290 testing::Values(PrefHashStoreTransaction::CLEARED, 323 testing::Values(PrefHashStoreTransaction::CLEARED,
291 PrefHashStoreTransaction::CHANGED, 324 PrefHashStoreTransaction::CHANGED,
292 PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE), 325 PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE),
326 testing::Values(PrefHashStoreTransaction::UNCHANGED,
327 PrefHashStoreTransaction::UNSUPPORTED,
328 PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE),
293 testing::Bool())); 329 testing::Bool()));
330
331 INSTANTIATE_TEST_CASE_P(
332 WithBypassIncident,
333 PreferenceValidationDelegateWithIncident,
334 testing::Combine(
335 testing::Values(PrefHashStoreTransaction::UNCHANGED,
336 PrefHashStoreTransaction::SECURE_LEGACY,
337 PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE),
338 testing::Values(PrefHashStoreTransaction::CHANGED,
339 PrefHashStoreTransaction::CLEARED),
340 testing::Bool()));
341
342 INSTANTIATE_TEST_CASE_P(
343 WithIncidentIgnoreBypass,
344 PreferenceValidationDelegateWithIncident,
345 testing::Combine(
346 testing::Values(PrefHashStoreTransaction::CLEARED,
347 PrefHashStoreTransaction::CHANGED,
348 PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE),
349 testing::Values(PrefHashStoreTransaction::CHANGED,
350 PrefHashStoreTransaction::CLEARED),
351 testing::Bool()));
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698