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