| 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 <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/safe_browsing/incident_reporting/incident.h" |
| 14 #include "chrome/common/safe_browsing/csd.pb.h" | 15 #include "chrome/common/safe_browsing/csd.pb.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 17 // A basic test harness that creates a delegate instance for which it stores all | 18 // A basic test harness that creates a delegate instance for which it stores all |
| 18 // incidents. Tests can push data to the delegate and verify that the test | 19 // incidents. Tests can push data to the delegate and verify that the test |
| 19 // instance was provided with the expected data. | 20 // instance was provided with the expected data. |
| 20 class PreferenceValidationDelegateTest : public testing::Test { | 21 class PreferenceValidationDelegateTest : public testing::Test { |
| 21 protected: | 22 protected: |
| 22 typedef ScopedVector<safe_browsing::ClientIncidentReport_IncidentData> | 23 typedef ScopedVector<safe_browsing::Incident> IncidentVector; |
| 23 IncidentVector; | |
| 24 | 24 |
| 25 PreferenceValidationDelegateTest() | 25 PreferenceValidationDelegateTest() |
| 26 : kPrefPath_("atomic.pref"), | 26 : kPrefPath_("atomic.pref"), |
| 27 null_value_(base::Value::CreateNullValue()) {} | 27 null_value_(base::Value::CreateNullValue()) {} |
| 28 | 28 |
| 29 void SetUp() override { | 29 void SetUp() override { |
| 30 testing::Test::SetUp(); | 30 testing::Test::SetUp(); |
| 31 invalid_keys_.push_back(std::string("one")); | 31 invalid_keys_.push_back(std::string("one")); |
| 32 invalid_keys_.push_back(std::string("two")); | 32 invalid_keys_.push_back(std::string("two")); |
| 33 instance_.reset(new safe_browsing::PreferenceValidationDelegate( | 33 instance_.reset(new safe_browsing::PreferenceValidationDelegate( |
| 34 base::Bind(&PreferenceValidationDelegateTest::AddIncident, | 34 base::Bind(&PreferenceValidationDelegateTest::AddIncident, |
| 35 base::Unretained(this)))); | 35 base::Unretained(this)))); |
| 36 } | 36 } |
| 37 | 37 |
| 38 void AddIncident( | 38 void AddIncident(scoped_ptr<safe_browsing::Incident> incident) { |
| 39 scoped_ptr<safe_browsing::ClientIncidentReport_IncidentData> data) { | 39 incidents_.push_back(incident.release()); |
| 40 incidents_.push_back(data.release()); | |
| 41 } | 40 } |
| 42 | 41 |
| 43 static void ExpectValueStatesEquate( | 42 static void ExpectValueStatesEquate( |
| 44 PrefHashStoreTransaction::ValueState store_state, | 43 PrefHashStoreTransaction::ValueState store_state, |
| 45 safe_browsing:: | 44 safe_browsing:: |
| 46 ClientIncidentReport_IncidentData_TrackedPreferenceIncident_ValueState | 45 ClientIncidentReport_IncidentData_TrackedPreferenceIncident_ValueState |
| 47 incident_state) { | 46 incident_state) { |
| 48 typedef safe_browsing:: | 47 typedef safe_browsing:: |
| 49 ClientIncidentReport_IncidentData_TrackedPreferenceIncident TPIncident; | 48 ClientIncidentReport_IncidentData_TrackedPreferenceIncident TPIncident; |
| 50 switch (store_state) { | 49 switch (store_state) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 79 std::vector<std::string> invalid_keys_; | 78 std::vector<std::string> invalid_keys_; |
| 80 scoped_ptr<TrackedPreferenceValidationDelegate> instance_; | 79 scoped_ptr<TrackedPreferenceValidationDelegate> instance_; |
| 81 }; | 80 }; |
| 82 | 81 |
| 83 // Tests that a NULL value results in an incident with no value. | 82 // Tests that a NULL value results in an incident with no value. |
| 84 TEST_F(PreferenceValidationDelegateTest, NullValue) { | 83 TEST_F(PreferenceValidationDelegateTest, NullValue) { |
| 85 instance_->OnAtomicPreferenceValidation(kPrefPath_, | 84 instance_->OnAtomicPreferenceValidation(kPrefPath_, |
| 86 NULL, | 85 NULL, |
| 87 PrefHashStoreTransaction::CLEARED, | 86 PrefHashStoreTransaction::CLEARED, |
| 88 TrackedPreferenceHelper::DONT_RESET); | 87 TrackedPreferenceHelper::DONT_RESET); |
| 89 safe_browsing::ClientIncidentReport_IncidentData* incident = | 88 scoped_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( |
| 90 incidents_.back(); | 89 incidents_.back()->TakePayload()); |
| 91 EXPECT_FALSE(incident->tracked_preference().has_atomic_value()); | 90 EXPECT_FALSE(incident->tracked_preference().has_atomic_value()); |
| 92 EXPECT_EQ( | 91 EXPECT_EQ( |
| 93 safe_browsing:: | 92 safe_browsing:: |
| 94 ClientIncidentReport_IncidentData_TrackedPreferenceIncident::CLEARED, | 93 ClientIncidentReport_IncidentData_TrackedPreferenceIncident::CLEARED, |
| 95 incident->tracked_preference().value_state()); | 94 incident->tracked_preference().value_state()); |
| 96 } | 95 } |
| 97 | 96 |
| 98 // Tests that all supported value types can be stringified into an incident. The | 97 // Tests that all supported value types can be stringified into an incident. The |
| 99 // parameters for the test are the type of value to test and the expected value | 98 // parameters for the test are the type of value to test and the expected value |
| 100 // string. | 99 // string. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 base::Value::Type value_type_; | 142 base::Value::Type value_type_; |
| 144 const char* expected_value_; | 143 const char* expected_value_; |
| 145 }; | 144 }; |
| 146 | 145 |
| 147 TEST_P(PreferenceValidationDelegateValues, Value) { | 146 TEST_P(PreferenceValidationDelegateValues, Value) { |
| 148 instance_->OnAtomicPreferenceValidation(kPrefPath_, | 147 instance_->OnAtomicPreferenceValidation(kPrefPath_, |
| 149 MakeValue(value_type_).get(), | 148 MakeValue(value_type_).get(), |
| 150 PrefHashStoreTransaction::CLEARED, | 149 PrefHashStoreTransaction::CLEARED, |
| 151 TrackedPreferenceHelper::DONT_RESET); | 150 TrackedPreferenceHelper::DONT_RESET); |
| 152 ASSERT_EQ(1U, incidents_.size()); | 151 ASSERT_EQ(1U, incidents_.size()); |
| 153 safe_browsing::ClientIncidentReport_IncidentData* incident = | 152 scoped_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( |
| 154 incidents_.back(); | 153 incidents_.back()->TakePayload()); |
| 155 EXPECT_EQ(std::string(expected_value_), | 154 EXPECT_EQ(std::string(expected_value_), |
| 156 incident->tracked_preference().atomic_value()); | 155 incident->tracked_preference().atomic_value()); |
| 157 } | 156 } |
| 158 | 157 |
| 159 INSTANTIATE_TEST_CASE_P( | 158 INSTANTIATE_TEST_CASE_P( |
| 160 Values, | 159 Values, |
| 161 PreferenceValidationDelegateValues, | 160 PreferenceValidationDelegateValues, |
| 162 // On Android, make_tuple(..., "null") doesn't compile due to the error: | 161 // On Android, make_tuple(..., "null") doesn't compile due to the error: |
| 163 // testing/gtest/include/gtest/internal/gtest-tuple.h:246:48: | 162 // testing/gtest/include/gtest/internal/gtest-tuple.h:246:48: |
| 164 // error: array used as initializer | 163 // error: array used as initializer |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 } | 229 } |
| 231 | 230 |
| 232 PrefHashStoreTransaction::ValueState value_state_; | 231 PrefHashStoreTransaction::ValueState value_state_; |
| 233 TrackedPreferenceHelper::ResetAction reset_action_; | 232 TrackedPreferenceHelper::ResetAction reset_action_; |
| 234 }; | 233 }; |
| 235 | 234 |
| 236 TEST_P(PreferenceValidationDelegateWithIncident, Atomic) { | 235 TEST_P(PreferenceValidationDelegateWithIncident, Atomic) { |
| 237 instance_->OnAtomicPreferenceValidation( | 236 instance_->OnAtomicPreferenceValidation( |
| 238 kPrefPath_, null_value_.get(), value_state_, reset_action_); | 237 kPrefPath_, null_value_.get(), value_state_, reset_action_); |
| 239 ASSERT_EQ(1U, incidents_.size()); | 238 ASSERT_EQ(1U, incidents_.size()); |
| 240 safe_browsing::ClientIncidentReport_IncidentData* incident = | 239 scoped_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( |
| 241 incidents_.back(); | 240 incidents_.back()->TakePayload()); |
| 242 EXPECT_TRUE(incident->has_tracked_preference()); | 241 EXPECT_TRUE(incident->has_tracked_preference()); |
| 243 const safe_browsing:: | 242 const safe_browsing:: |
| 244 ClientIncidentReport_IncidentData_TrackedPreferenceIncident& tp_incident = | 243 ClientIncidentReport_IncidentData_TrackedPreferenceIncident& tp_incident = |
| 245 incident->tracked_preference(); | 244 incident->tracked_preference(); |
| 246 EXPECT_EQ(kPrefPath_, tp_incident.path()); | 245 EXPECT_EQ(kPrefPath_, tp_incident.path()); |
| 247 EXPECT_EQ(0, tp_incident.split_key_size()); | 246 EXPECT_EQ(0, tp_incident.split_key_size()); |
| 248 EXPECT_TRUE(tp_incident.has_atomic_value()); | 247 EXPECT_TRUE(tp_incident.has_atomic_value()); |
| 249 EXPECT_EQ(std::string("null"), tp_incident.atomic_value()); | 248 EXPECT_EQ(std::string("null"), tp_incident.atomic_value()); |
| 250 EXPECT_TRUE(tp_incident.has_value_state()); | 249 EXPECT_TRUE(tp_incident.has_value_state()); |
| 251 ExpectValueStatesEquate(value_state_, tp_incident.value_state()); | 250 ExpectValueStatesEquate(value_state_, tp_incident.value_state()); |
| 252 } | 251 } |
| 253 | 252 |
| 254 TEST_P(PreferenceValidationDelegateWithIncident, Split) { | 253 TEST_P(PreferenceValidationDelegateWithIncident, Split) { |
| 255 instance_->OnSplitPreferenceValidation( | 254 instance_->OnSplitPreferenceValidation( |
| 256 kPrefPath_, &dict_value_, invalid_keys_, value_state_, reset_action_); | 255 kPrefPath_, &dict_value_, invalid_keys_, value_state_, reset_action_); |
| 257 ASSERT_EQ(1U, incidents_.size()); | 256 ASSERT_EQ(1U, incidents_.size()); |
| 258 safe_browsing::ClientIncidentReport_IncidentData* incident = | 257 scoped_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( |
| 259 incidents_.back(); | 258 incidents_.back()->TakePayload()); |
| 260 EXPECT_TRUE(incident->has_tracked_preference()); | 259 EXPECT_TRUE(incident->has_tracked_preference()); |
| 261 const safe_browsing:: | 260 const safe_browsing:: |
| 262 ClientIncidentReport_IncidentData_TrackedPreferenceIncident& tp_incident = | 261 ClientIncidentReport_IncidentData_TrackedPreferenceIncident& tp_incident = |
| 263 incident->tracked_preference(); | 262 incident->tracked_preference(); |
| 264 EXPECT_EQ(kPrefPath_, tp_incident.path()); | 263 EXPECT_EQ(kPrefPath_, tp_incident.path()); |
| 265 EXPECT_FALSE(tp_incident.has_atomic_value()); | 264 EXPECT_FALSE(tp_incident.has_atomic_value()); |
| 266 ExpectKeysEquate(invalid_keys_, tp_incident.split_key()); | 265 ExpectKeysEquate(invalid_keys_, tp_incident.split_key()); |
| 267 EXPECT_TRUE(tp_incident.has_value_state()); | 266 EXPECT_TRUE(tp_incident.has_value_state()); |
| 268 ExpectValueStatesEquate(value_state_, tp_incident.value_state()); | 267 ExpectValueStatesEquate(value_state_, tp_incident.value_state()); |
| 269 } | 268 } |
| 270 | 269 |
| 271 INSTANTIATE_TEST_CASE_P( | 270 INSTANTIATE_TEST_CASE_P( |
| 272 WithIncident, | 271 WithIncident, |
| 273 PreferenceValidationDelegateWithIncident, | 272 PreferenceValidationDelegateWithIncident, |
| 274 testing::Combine( | 273 testing::Combine( |
| 275 testing::Values(PrefHashStoreTransaction::CLEARED, | 274 testing::Values(PrefHashStoreTransaction::CLEARED, |
| 276 PrefHashStoreTransaction::CHANGED, | 275 PrefHashStoreTransaction::CHANGED, |
| 277 PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE), | 276 PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE), |
| 278 testing::Values(TrackedPreferenceHelper::WANTED_RESET, | 277 testing::Values(TrackedPreferenceHelper::WANTED_RESET, |
| 279 TrackedPreferenceHelper::DO_RESET))); | 278 TrackedPreferenceHelper::DO_RESET))); |
| OLD | NEW |