OLD | NEW |
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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 std::unique_ptr<safe_browsing::MockIncidentReceiver> receiver( | 43 std::unique_ptr<safe_browsing::MockIncidentReceiver> receiver( |
44 new NiceMock<safe_browsing::MockIncidentReceiver>()); | 44 new NiceMock<safe_browsing::MockIncidentReceiver>()); |
45 ON_CALL(*receiver, DoAddIncidentForProfile(IsNull(), _)) | 45 ON_CALL(*receiver, DoAddIncidentForProfile(IsNull(), _)) |
46 .WillByDefault(WithArg<1>(TakeIncidentToVector(&incidents_))); | 46 .WillByDefault(WithArg<1>(TakeIncidentToVector(&incidents_))); |
47 instance_.reset(new safe_browsing::PreferenceValidationDelegate( | 47 instance_.reset(new safe_browsing::PreferenceValidationDelegate( |
48 nullptr, std::move(receiver))); | 48 nullptr, std::move(receiver))); |
49 } | 49 } |
50 | 50 |
51 static void ExpectValueStatesEquate( | 51 static void ExpectValueStatesEquate( |
52 PrefHashStoreTransaction::ValueState store_state, | 52 PrefHashStoreTransaction::ValueState store_state, |
| 53 PrefHashStoreTransaction::ValueState external_validation_value_state, |
53 safe_browsing:: | 54 safe_browsing:: |
54 ClientIncidentReport_IncidentData_TrackedPreferenceIncident_ValueState | 55 ClientIncidentReport_IncidentData_TrackedPreferenceIncident_ValueState |
55 incident_state) { | 56 incident_state) { |
56 typedef safe_browsing:: | 57 typedef safe_browsing:: |
57 ClientIncidentReport_IncidentData_TrackedPreferenceIncident TPIncident; | 58 ClientIncidentReport_IncidentData_TrackedPreferenceIncident TPIncident; |
58 switch (store_state) { | 59 switch (store_state) { |
59 case PrefHashStoreTransaction::CLEARED: | 60 case PrefHashStoreTransaction::CLEARED: |
60 EXPECT_EQ(TPIncident::CLEARED, incident_state); | 61 EXPECT_EQ(TPIncident::CLEARED, incident_state); |
61 break; | 62 break; |
62 case PrefHashStoreTransaction::CHANGED: | 63 case PrefHashStoreTransaction::CHANGED: |
63 EXPECT_EQ(TPIncident::CHANGED, incident_state); | 64 EXPECT_EQ(TPIncident::CHANGED, incident_state); |
64 break; | 65 break; |
65 case PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE: | 66 case PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE: |
66 EXPECT_EQ(TPIncident::UNTRUSTED_UNKNOWN_VALUE, incident_state); | 67 EXPECT_EQ(TPIncident::UNTRUSTED_UNKNOWN_VALUE, incident_state); |
67 break; | 68 break; |
68 default: | 69 default: |
69 FAIL() << "unexpected store state"; | 70 if (external_validation_value_state == |
| 71 PrefHashStoreTransaction::CLEARED) { |
| 72 EXPECT_EQ(TPIncident::BYPASS_CLEARED, incident_state); |
| 73 } else if (external_validation_value_state == |
| 74 PrefHashStoreTransaction::CHANGED) { |
| 75 EXPECT_EQ(TPIncident::BYPASS_CHANGED, incident_state); |
| 76 } else { |
| 77 FAIL() << "unexpected store state"; |
| 78 } |
70 break; | 79 break; |
71 } | 80 } |
72 } | 81 } |
73 | 82 |
74 static void ExpectKeysEquate( | 83 static void ExpectKeysEquate( |
75 const std::vector<std::string>& store_keys, | 84 const std::vector<std::string>& store_keys, |
76 const google::protobuf::RepeatedPtrField<std::string>& incident_keys) { | 85 const google::protobuf::RepeatedPtrField<std::string>& incident_keys) { |
77 ASSERT_EQ(store_keys.size(), static_cast<size_t>(incident_keys.size())); | 86 ASSERT_EQ(store_keys.size(), static_cast<size_t>(incident_keys.size())); |
78 for (int i = 0; i < incident_keys.size(); ++i) { | 87 for (int i = 0; i < incident_keys.size(); ++i) { |
79 EXPECT_EQ(store_keys[i], incident_keys.Get(i)); | 88 EXPECT_EQ(store_keys[i], incident_keys.Get(i)); |
80 } | 89 } |
81 } | 90 } |
82 | 91 |
83 const std::string kPrefPath_; | 92 const std::string kPrefPath_; |
84 IncidentVector incidents_; | 93 IncidentVector incidents_; |
85 std::unique_ptr<base::Value> null_value_; | 94 std::unique_ptr<base::Value> null_value_; |
86 base::DictionaryValue dict_value_; | 95 base::DictionaryValue dict_value_; |
87 std::vector<std::string> invalid_keys_; | 96 std::vector<std::string> invalid_keys_; |
88 std::unique_ptr<TrackedPreferenceValidationDelegate> instance_; | 97 std::unique_ptr<TrackedPreferenceValidationDelegate> instance_; |
89 }; | 98 }; |
90 | 99 |
91 // Tests that a NULL value results in an incident with no value. | 100 // Tests that a NULL value results in an incident with no value. |
92 TEST_F(PreferenceValidationDelegateTest, NullValue) { | 101 TEST_F(PreferenceValidationDelegateTest, NullValue) { |
93 instance_->OnAtomicPreferenceValidation(kPrefPath_, | 102 instance_->OnAtomicPreferenceValidation( |
94 NULL, | 103 kPrefPath_, NULL, PrefHashStoreTransaction::CLEARED, |
95 PrefHashStoreTransaction::CLEARED, | 104 PrefHashStoreTransaction::UNSUPPORTED, false /* is_personal */); |
96 false /* is_personal */); | |
97 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( | 105 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( |
98 incidents_.back()->TakePayload()); | 106 incidents_.back()->TakePayload()); |
99 EXPECT_FALSE(incident->tracked_preference().has_atomic_value()); | 107 EXPECT_FALSE(incident->tracked_preference().has_atomic_value()); |
100 EXPECT_EQ( | 108 EXPECT_EQ( |
101 safe_browsing:: | 109 safe_browsing:: |
102 ClientIncidentReport_IncidentData_TrackedPreferenceIncident::CLEARED, | 110 ClientIncidentReport_IncidentData_TrackedPreferenceIncident::CLEARED, |
103 incident->tracked_preference().value_state()); | 111 incident->tracked_preference().value_state()); |
104 } | 112 } |
105 | 113 |
106 // Tests that all supported value types can be stringified into an incident. The | 114 // 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 Loading... |
147 ADD_FAILURE() << "unsupported value type " << value_type; | 155 ADD_FAILURE() << "unsupported value type " << value_type; |
148 } | 156 } |
149 return std::unique_ptr<Value>(); | 157 return std::unique_ptr<Value>(); |
150 } | 158 } |
151 | 159 |
152 base::Value::Type value_type_; | 160 base::Value::Type value_type_; |
153 const char* expected_value_; | 161 const char* expected_value_; |
154 }; | 162 }; |
155 | 163 |
156 TEST_P(PreferenceValidationDelegateValues, Value) { | 164 TEST_P(PreferenceValidationDelegateValues, Value) { |
157 instance_->OnAtomicPreferenceValidation(kPrefPath_, | 165 instance_->OnAtomicPreferenceValidation( |
158 MakeValue(value_type_).get(), | 166 kPrefPath_, MakeValue(value_type_).get(), |
159 PrefHashStoreTransaction::CLEARED, | 167 PrefHashStoreTransaction::CLEARED, PrefHashStoreTransaction::UNSUPPORTED, |
160 false /* is_personal */); | 168 false /* is_personal */); |
161 ASSERT_EQ(1U, incidents_.size()); | 169 ASSERT_EQ(1U, incidents_.size()); |
162 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( | 170 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( |
163 incidents_.back()->TakePayload()); | 171 incidents_.back()->TakePayload()); |
164 EXPECT_EQ(std::string(expected_value_), | 172 EXPECT_EQ(std::string(expected_value_), |
165 incident->tracked_preference().atomic_value()); | 173 incident->tracked_preference().atomic_value()); |
166 } | 174 } |
167 | 175 |
168 INSTANTIATE_TEST_CASE_P( | 176 INSTANTIATE_TEST_CASE_P( |
169 Values, | 177 Values, |
170 PreferenceValidationDelegateValues, | 178 PreferenceValidationDelegateValues, |
(...skipping 12 matching lines...) Expand all Loading... |
183 std::tr1::make_tuple(base::Value::TYPE_STRING, | 191 std::tr1::make_tuple(base::Value::TYPE_STRING, |
184 const_cast<char*>("i have a spleen")), | 192 const_cast<char*>("i have a spleen")), |
185 std::tr1::make_tuple(base::Value::TYPE_DICTIONARY, | 193 std::tr1::make_tuple(base::Value::TYPE_DICTIONARY, |
186 const_cast<char*>("{\"forty-seven\":47,\"twenty-two\":22}")), | 194 const_cast<char*>("{\"forty-seven\":47,\"twenty-two\":22}")), |
187 std::tr1::make_tuple(base::Value::TYPE_LIST, | 195 std::tr1::make_tuple(base::Value::TYPE_LIST, |
188 const_cast<char*>("[22,47]")))); | 196 const_cast<char*>("[22,47]")))); |
189 | 197 |
190 // Tests that no incidents are reported for relevant combinations of ValueState. | 198 // Tests that no incidents are reported for relevant combinations of ValueState. |
191 class PreferenceValidationDelegateNoIncident | 199 class PreferenceValidationDelegateNoIncident |
192 : public PreferenceValidationDelegateTest, | 200 : public PreferenceValidationDelegateTest, |
193 public testing::WithParamInterface<PrefHashStoreTransaction::ValueState> { | 201 public testing::WithParamInterface< |
| 202 std::tr1::tuple<PrefHashStoreTransaction::ValueState, |
| 203 PrefHashStoreTransaction::ValueState>> { |
194 protected: | 204 protected: |
195 void SetUp() override { | 205 void SetUp() override { |
196 PreferenceValidationDelegateTest::SetUp(); | 206 PreferenceValidationDelegateTest::SetUp(); |
197 value_state_ = GetParam(); | 207 value_state_ = std::tr1::get<0>(GetParam()); |
| 208 external_validation_value_state_ = std::tr1::get<1>(GetParam()); |
198 } | 209 } |
199 | 210 |
200 PrefHashStoreTransaction::ValueState value_state_; | 211 PrefHashStoreTransaction::ValueState value_state_; |
| 212 PrefHashStoreTransaction::ValueState external_validation_value_state_; |
201 }; | 213 }; |
202 | 214 |
203 TEST_P(PreferenceValidationDelegateNoIncident, Atomic) { | 215 TEST_P(PreferenceValidationDelegateNoIncident, Atomic) { |
204 instance_->OnAtomicPreferenceValidation(kPrefPath_, | 216 instance_->OnAtomicPreferenceValidation( |
205 null_value_.get(), | 217 kPrefPath_, null_value_.get(), value_state_, |
206 value_state_, | 218 external_validation_value_state_, false /* is_personal */); |
207 false /* is_personal */); | |
208 EXPECT_EQ(0U, incidents_.size()); | 219 EXPECT_EQ(0U, incidents_.size()); |
209 } | 220 } |
210 | 221 |
211 TEST_P(PreferenceValidationDelegateNoIncident, Split) { | 222 TEST_P(PreferenceValidationDelegateNoIncident, Split) { |
212 instance_->OnSplitPreferenceValidation(kPrefPath_, | 223 instance_->OnSplitPreferenceValidation( |
213 &dict_value_, | 224 kPrefPath_, &dict_value_, invalid_keys_, value_state_, |
214 invalid_keys_, | 225 external_validation_value_state_, false /* is_personal */); |
215 value_state_, | |
216 false /* is_personal */); | |
217 EXPECT_EQ(0U, incidents_.size()); | 226 EXPECT_EQ(0U, incidents_.size()); |
218 } | 227 } |
219 | 228 |
220 INSTANTIATE_TEST_CASE_P( | 229 INSTANTIATE_TEST_CASE_P( |
221 NoIncident, | 230 NoIncident, |
222 PreferenceValidationDelegateNoIncident, | 231 PreferenceValidationDelegateNoIncident, |
223 testing::Values(PrefHashStoreTransaction::UNCHANGED, | 232 testing::Combine( |
224 PrefHashStoreTransaction::SECURE_LEGACY, | 233 testing::Values(PrefHashStoreTransaction::UNCHANGED, |
225 PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE)); | 234 PrefHashStoreTransaction::SECURE_LEGACY, |
| 235 PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE), |
| 236 testing::Values(PrefHashStoreTransaction::UNCHANGED, |
| 237 PrefHashStoreTransaction::UNSUPPORTED, |
| 238 PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE))); |
226 | 239 |
227 // Tests that incidents are reported for relevant combinations of ValueState and | 240 // Tests that incidents are reported for relevant combinations of ValueState and |
228 // impersonal/personal. | 241 // impersonal/personal. |
229 class PreferenceValidationDelegateWithIncident | 242 class PreferenceValidationDelegateWithIncident |
230 : public PreferenceValidationDelegateTest, | 243 : public PreferenceValidationDelegateTest, |
231 public testing::WithParamInterface< | 244 public testing::WithParamInterface< |
232 std::tr1::tuple<PrefHashStoreTransaction::ValueState, bool>> { | 245 std::tr1::tuple<PrefHashStoreTransaction::ValueState, |
| 246 PrefHashStoreTransaction::ValueState, |
| 247 bool>> { |
233 protected: | 248 protected: |
234 void SetUp() override { | 249 void SetUp() override { |
235 PreferenceValidationDelegateTest::SetUp(); | 250 PreferenceValidationDelegateTest::SetUp(); |
236 value_state_ = std::tr1::get<0>(GetParam()); | 251 value_state_ = std::tr1::get<0>(GetParam()); |
237 is_personal_ = std::tr1::get<1>(GetParam()); | 252 external_validation_value_state_ = std::tr1::get<1>(GetParam()); |
| 253 is_personal_ = std::tr1::get<2>(GetParam()); |
238 } | 254 } |
239 | 255 |
240 PrefHashStoreTransaction::ValueState value_state_; | 256 PrefHashStoreTransaction::ValueState value_state_; |
| 257 PrefHashStoreTransaction::ValueState external_validation_value_state_; |
241 bool is_personal_; | 258 bool is_personal_; |
242 }; | 259 }; |
243 | 260 |
244 TEST_P(PreferenceValidationDelegateWithIncident, Atomic) { | 261 TEST_P(PreferenceValidationDelegateWithIncident, Atomic) { |
245 instance_->OnAtomicPreferenceValidation( | 262 instance_->OnAtomicPreferenceValidation( |
246 kPrefPath_, null_value_.get(), value_state_, is_personal_); | 263 kPrefPath_, null_value_.get(), value_state_, |
| 264 external_validation_value_state_, is_personal_); |
247 ASSERT_EQ(1U, incidents_.size()); | 265 ASSERT_EQ(1U, incidents_.size()); |
248 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( | 266 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( |
249 incidents_.back()->TakePayload()); | 267 incidents_.back()->TakePayload()); |
250 EXPECT_TRUE(incident->has_tracked_preference()); | 268 EXPECT_TRUE(incident->has_tracked_preference()); |
251 const safe_browsing:: | 269 const safe_browsing:: |
252 ClientIncidentReport_IncidentData_TrackedPreferenceIncident& tp_incident = | 270 ClientIncidentReport_IncidentData_TrackedPreferenceIncident& tp_incident = |
253 incident->tracked_preference(); | 271 incident->tracked_preference(); |
254 EXPECT_EQ(kPrefPath_, tp_incident.path()); | 272 EXPECT_EQ(kPrefPath_, tp_incident.path()); |
255 EXPECT_EQ(0, tp_incident.split_key_size()); | 273 EXPECT_EQ(0, tp_incident.split_key_size()); |
256 if (!is_personal_) { | 274 if (!is_personal_) { |
257 EXPECT_TRUE(tp_incident.has_atomic_value()); | 275 EXPECT_TRUE(tp_incident.has_atomic_value()); |
258 EXPECT_EQ(std::string("null"), tp_incident.atomic_value()); | 276 EXPECT_EQ(std::string("null"), tp_incident.atomic_value()); |
259 } else { | 277 } else { |
260 EXPECT_FALSE(tp_incident.has_atomic_value()); | 278 EXPECT_FALSE(tp_incident.has_atomic_value()); |
261 } | 279 } |
262 EXPECT_TRUE(tp_incident.has_value_state()); | 280 EXPECT_TRUE(tp_incident.has_value_state()); |
263 ExpectValueStatesEquate(value_state_, tp_incident.value_state()); | 281 ExpectValueStatesEquate(value_state_, external_validation_value_state_, |
| 282 tp_incident.value_state()); |
264 } | 283 } |
265 | 284 |
266 TEST_P(PreferenceValidationDelegateWithIncident, Split) { | 285 TEST_P(PreferenceValidationDelegateWithIncident, Split) { |
267 instance_->OnSplitPreferenceValidation( | 286 instance_->OnSplitPreferenceValidation( |
268 kPrefPath_, &dict_value_, invalid_keys_, value_state_, is_personal_); | 287 kPrefPath_, &dict_value_, invalid_keys_, value_state_, |
| 288 external_validation_value_state_, is_personal_); |
269 ASSERT_EQ(1U, incidents_.size()); | 289 ASSERT_EQ(1U, incidents_.size()); |
270 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( | 290 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( |
271 incidents_.back()->TakePayload()); | 291 incidents_.back()->TakePayload()); |
272 EXPECT_TRUE(incident->has_tracked_preference()); | 292 EXPECT_TRUE(incident->has_tracked_preference()); |
273 const safe_browsing:: | 293 const safe_browsing:: |
274 ClientIncidentReport_IncidentData_TrackedPreferenceIncident& tp_incident = | 294 ClientIncidentReport_IncidentData_TrackedPreferenceIncident& tp_incident = |
275 incident->tracked_preference(); | 295 incident->tracked_preference(); |
276 EXPECT_EQ(kPrefPath_, tp_incident.path()); | 296 EXPECT_EQ(kPrefPath_, tp_incident.path()); |
277 EXPECT_FALSE(tp_incident.has_atomic_value()); | 297 EXPECT_FALSE(tp_incident.has_atomic_value()); |
278 if (!is_personal_) | 298 if (!is_personal_) |
279 ExpectKeysEquate(invalid_keys_, tp_incident.split_key()); | 299 ExpectKeysEquate(invalid_keys_, tp_incident.split_key()); |
280 else | 300 else |
281 EXPECT_EQ(0, tp_incident.split_key_size()); | 301 EXPECT_EQ(0, tp_incident.split_key_size()); |
282 EXPECT_TRUE(tp_incident.has_value_state()); | 302 EXPECT_TRUE(tp_incident.has_value_state()); |
283 ExpectValueStatesEquate(value_state_, tp_incident.value_state()); | 303 ExpectValueStatesEquate(value_state_, external_validation_value_state_, |
| 304 tp_incident.value_state()); |
284 } | 305 } |
285 | 306 |
286 INSTANTIATE_TEST_CASE_P( | 307 INSTANTIATE_TEST_CASE_P( |
287 WithIncident, | 308 WithIncident, |
288 PreferenceValidationDelegateWithIncident, | 309 PreferenceValidationDelegateWithIncident, |
289 testing::Combine( | 310 testing::Combine( |
290 testing::Values(PrefHashStoreTransaction::CLEARED, | 311 testing::Values(PrefHashStoreTransaction::CLEARED, |
291 PrefHashStoreTransaction::CHANGED, | 312 PrefHashStoreTransaction::CHANGED, |
292 PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE), | 313 PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE), |
| 314 testing::Values(PrefHashStoreTransaction::UNCHANGED, |
| 315 PrefHashStoreTransaction::UNSUPPORTED, |
| 316 PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE), |
293 testing::Bool())); | 317 testing::Bool())); |
| 318 |
| 319 INSTANTIATE_TEST_CASE_P( |
| 320 WithBypassIncident, |
| 321 PreferenceValidationDelegateWithIncident, |
| 322 testing::Combine( |
| 323 testing::Values(PrefHashStoreTransaction::UNCHANGED, |
| 324 PrefHashStoreTransaction::SECURE_LEGACY, |
| 325 PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE), |
| 326 testing::Values(PrefHashStoreTransaction::CHANGED, |
| 327 PrefHashStoreTransaction::CLEARED), |
| 328 testing::Bool())); |
OLD | NEW |