| 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 |
| 9 #include <memory> |
| 8 #include <string> | 10 #include <string> |
| 9 #include <utility> | 11 #include <utility> |
| 10 #include <vector> | 12 #include <vector> |
| 11 | 13 |
| 12 #include "base/bind.h" | 14 #include "base/bind.h" |
| 13 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/values.h" | 16 #include "base/values.h" |
| 16 #include "chrome/browser/safe_browsing/incident_reporting/incident.h" | 17 #include "chrome/browser/safe_browsing/incident_reporting/incident.h" |
| 17 #include "chrome/browser/safe_browsing/incident_reporting/mock_incident_receiver
.h" | 18 #include "chrome/browser/safe_browsing/incident_reporting/mock_incident_receiver
.h" |
| 18 #include "chrome/common/safe_browsing/csd.pb.h" | 19 #include "chrome/common/safe_browsing/csd.pb.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 22 |
| 22 using ::testing::_; | 23 using ::testing::_; |
| 23 using ::testing::IsNull; | 24 using ::testing::IsNull; |
| 24 using ::testing::NiceMock; | 25 using ::testing::NiceMock; |
| 25 using ::testing::WithArg; | 26 using ::testing::WithArg; |
| 26 | 27 |
| 27 // A basic test harness that creates a delegate instance for which it stores all | 28 // A basic test harness that creates a delegate instance for which it stores all |
| 28 // incidents. Tests can push data to the delegate and verify that the test | 29 // incidents. Tests can push data to the delegate and verify that the test |
| 29 // instance was provided with the expected data. | 30 // instance was provided with the expected data. |
| 30 class PreferenceValidationDelegateTest : public testing::Test { | 31 class PreferenceValidationDelegateTest : public testing::Test { |
| 31 protected: | 32 protected: |
| 32 typedef std::vector<scoped_ptr<safe_browsing::Incident>> IncidentVector; | 33 typedef std::vector<std::unique_ptr<safe_browsing::Incident>> IncidentVector; |
| 33 | 34 |
| 34 PreferenceValidationDelegateTest() | 35 PreferenceValidationDelegateTest() |
| 35 : kPrefPath_("atomic.pref"), | 36 : kPrefPath_("atomic.pref"), |
| 36 null_value_(base::Value::CreateNullValue()) {} | 37 null_value_(base::Value::CreateNullValue()) {} |
| 37 | 38 |
| 38 void SetUp() override { | 39 void SetUp() override { |
| 39 testing::Test::SetUp(); | 40 testing::Test::SetUp(); |
| 40 invalid_keys_.push_back(std::string("one")); | 41 invalid_keys_.push_back(std::string("one")); |
| 41 invalid_keys_.push_back(std::string("two")); | 42 invalid_keys_.push_back(std::string("two")); |
| 42 scoped_ptr<safe_browsing::MockIncidentReceiver> receiver( | 43 std::unique_ptr<safe_browsing::MockIncidentReceiver> receiver( |
| 43 new NiceMock<safe_browsing::MockIncidentReceiver>()); | 44 new NiceMock<safe_browsing::MockIncidentReceiver>()); |
| 44 ON_CALL(*receiver, DoAddIncidentForProfile(IsNull(), _)) | 45 ON_CALL(*receiver, DoAddIncidentForProfile(IsNull(), _)) |
| 45 .WillByDefault(WithArg<1>(TakeIncidentToVector(&incidents_))); | 46 .WillByDefault(WithArg<1>(TakeIncidentToVector(&incidents_))); |
| 46 instance_.reset(new safe_browsing::PreferenceValidationDelegate( | 47 instance_.reset(new safe_browsing::PreferenceValidationDelegate( |
| 47 nullptr, std::move(receiver))); | 48 nullptr, std::move(receiver))); |
| 48 } | 49 } |
| 49 | 50 |
| 50 static void ExpectValueStatesEquate( | 51 static void ExpectValueStatesEquate( |
| 51 PrefHashStoreTransaction::ValueState store_state, | 52 PrefHashStoreTransaction::ValueState store_state, |
| 52 safe_browsing:: | 53 safe_browsing:: |
| (...skipping 21 matching lines...) Expand all Loading... |
| 74 const std::vector<std::string>& store_keys, | 75 const std::vector<std::string>& store_keys, |
| 75 const google::protobuf::RepeatedPtrField<std::string>& incident_keys) { | 76 const google::protobuf::RepeatedPtrField<std::string>& incident_keys) { |
| 76 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())); |
| 77 for (int i = 0; i < incident_keys.size(); ++i) { | 78 for (int i = 0; i < incident_keys.size(); ++i) { |
| 78 EXPECT_EQ(store_keys[i], incident_keys.Get(i)); | 79 EXPECT_EQ(store_keys[i], incident_keys.Get(i)); |
| 79 } | 80 } |
| 80 } | 81 } |
| 81 | 82 |
| 82 const std::string kPrefPath_; | 83 const std::string kPrefPath_; |
| 83 IncidentVector incidents_; | 84 IncidentVector incidents_; |
| 84 scoped_ptr<base::Value> null_value_; | 85 std::unique_ptr<base::Value> null_value_; |
| 85 base::DictionaryValue dict_value_; | 86 base::DictionaryValue dict_value_; |
| 86 std::vector<std::string> invalid_keys_; | 87 std::vector<std::string> invalid_keys_; |
| 87 scoped_ptr<TrackedPreferenceValidationDelegate> instance_; | 88 std::unique_ptr<TrackedPreferenceValidationDelegate> instance_; |
| 88 }; | 89 }; |
| 89 | 90 |
| 90 // 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. |
| 91 TEST_F(PreferenceValidationDelegateTest, NullValue) { | 92 TEST_F(PreferenceValidationDelegateTest, NullValue) { |
| 92 instance_->OnAtomicPreferenceValidation(kPrefPath_, | 93 instance_->OnAtomicPreferenceValidation(kPrefPath_, |
| 93 NULL, | 94 NULL, |
| 94 PrefHashStoreTransaction::CLEARED, | 95 PrefHashStoreTransaction::CLEARED, |
| 95 false /* is_personal */); | 96 false /* is_personal */); |
| 96 scoped_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( | 97 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( |
| 97 incidents_.back()->TakePayload()); | 98 incidents_.back()->TakePayload()); |
| 98 EXPECT_FALSE(incident->tracked_preference().has_atomic_value()); | 99 EXPECT_FALSE(incident->tracked_preference().has_atomic_value()); |
| 99 EXPECT_EQ( | 100 EXPECT_EQ( |
| 100 safe_browsing:: | 101 safe_browsing:: |
| 101 ClientIncidentReport_IncidentData_TrackedPreferenceIncident::CLEARED, | 102 ClientIncidentReport_IncidentData_TrackedPreferenceIncident::CLEARED, |
| 102 incident->tracked_preference().value_state()); | 103 incident->tracked_preference().value_state()); |
| 103 } | 104 } |
| 104 | 105 |
| 105 // 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 |
| 106 // parameters for the test are the type of value to test and the expected value | 107 // parameters for the test are the type of value to test and the expected value |
| 107 // string. | 108 // string. |
| 108 class PreferenceValidationDelegateValues | 109 class PreferenceValidationDelegateValues |
| 109 : public PreferenceValidationDelegateTest, | 110 : public PreferenceValidationDelegateTest, |
| 110 public testing::WithParamInterface< | 111 public testing::WithParamInterface< |
| 111 std::tr1::tuple<base::Value::Type, const char*> > { | 112 std::tr1::tuple<base::Value::Type, const char*> > { |
| 112 protected: | 113 protected: |
| 113 void SetUp() override { | 114 void SetUp() override { |
| 114 PreferenceValidationDelegateTest::SetUp(); | 115 PreferenceValidationDelegateTest::SetUp(); |
| 115 value_type_ = std::tr1::get<0>(GetParam()); | 116 value_type_ = std::tr1::get<0>(GetParam()); |
| 116 expected_value_ = std::tr1::get<1>(GetParam()); | 117 expected_value_ = std::tr1::get<1>(GetParam()); |
| 117 } | 118 } |
| 118 | 119 |
| 119 static scoped_ptr<base::Value> MakeValue(base::Value::Type value_type) { | 120 static std::unique_ptr<base::Value> MakeValue(base::Value::Type value_type) { |
| 120 using base::Value; | 121 using base::Value; |
| 121 switch (value_type) { | 122 switch (value_type) { |
| 122 case Value::TYPE_NULL: | 123 case Value::TYPE_NULL: |
| 123 return Value::CreateNullValue(); | 124 return Value::CreateNullValue(); |
| 124 case Value::TYPE_BOOLEAN: | 125 case Value::TYPE_BOOLEAN: |
| 125 return scoped_ptr<Value>(new base::FundamentalValue(false)); | 126 return std::unique_ptr<Value>(new base::FundamentalValue(false)); |
| 126 case Value::TYPE_INTEGER: | 127 case Value::TYPE_INTEGER: |
| 127 return scoped_ptr<Value>(new base::FundamentalValue(47)); | 128 return std::unique_ptr<Value>(new base::FundamentalValue(47)); |
| 128 case Value::TYPE_DOUBLE: | 129 case Value::TYPE_DOUBLE: |
| 129 return scoped_ptr<Value>(new base::FundamentalValue(0.47)); | 130 return std::unique_ptr<Value>(new base::FundamentalValue(0.47)); |
| 130 case Value::TYPE_STRING: | 131 case Value::TYPE_STRING: |
| 131 return scoped_ptr<Value>(new base::StringValue("i have a spleen")); | 132 return std::unique_ptr<Value>(new base::StringValue("i have a spleen")); |
| 132 case Value::TYPE_DICTIONARY: { | 133 case Value::TYPE_DICTIONARY: { |
| 133 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); | 134 std::unique_ptr<base::DictionaryValue> value( |
| 135 new base::DictionaryValue()); |
| 134 value->SetInteger("twenty-two", 22); | 136 value->SetInteger("twenty-two", 22); |
| 135 value->SetInteger("forty-seven", 47); | 137 value->SetInteger("forty-seven", 47); |
| 136 return std::move(value); | 138 return std::move(value); |
| 137 } | 139 } |
| 138 case Value::TYPE_LIST: { | 140 case Value::TYPE_LIST: { |
| 139 scoped_ptr<base::ListValue> value(new base::ListValue()); | 141 std::unique_ptr<base::ListValue> value(new base::ListValue()); |
| 140 value->AppendInteger(22); | 142 value->AppendInteger(22); |
| 141 value->AppendInteger(47); | 143 value->AppendInteger(47); |
| 142 return std::move(value); | 144 return std::move(value); |
| 143 } | 145 } |
| 144 default: | 146 default: |
| 145 ADD_FAILURE() << "unsupported value type " << value_type; | 147 ADD_FAILURE() << "unsupported value type " << value_type; |
| 146 } | 148 } |
| 147 return scoped_ptr<Value>(); | 149 return std::unique_ptr<Value>(); |
| 148 } | 150 } |
| 149 | 151 |
| 150 base::Value::Type value_type_; | 152 base::Value::Type value_type_; |
| 151 const char* expected_value_; | 153 const char* expected_value_; |
| 152 }; | 154 }; |
| 153 | 155 |
| 154 TEST_P(PreferenceValidationDelegateValues, Value) { | 156 TEST_P(PreferenceValidationDelegateValues, Value) { |
| 155 instance_->OnAtomicPreferenceValidation(kPrefPath_, | 157 instance_->OnAtomicPreferenceValidation(kPrefPath_, |
| 156 MakeValue(value_type_).get(), | 158 MakeValue(value_type_).get(), |
| 157 PrefHashStoreTransaction::CLEARED, | 159 PrefHashStoreTransaction::CLEARED, |
| 158 false /* is_personal */); | 160 false /* is_personal */); |
| 159 ASSERT_EQ(1U, incidents_.size()); | 161 ASSERT_EQ(1U, incidents_.size()); |
| 160 scoped_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( | 162 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( |
| 161 incidents_.back()->TakePayload()); | 163 incidents_.back()->TakePayload()); |
| 162 EXPECT_EQ(std::string(expected_value_), | 164 EXPECT_EQ(std::string(expected_value_), |
| 163 incident->tracked_preference().atomic_value()); | 165 incident->tracked_preference().atomic_value()); |
| 164 } | 166 } |
| 165 | 167 |
| 166 INSTANTIATE_TEST_CASE_P( | 168 INSTANTIATE_TEST_CASE_P( |
| 167 Values, | 169 Values, |
| 168 PreferenceValidationDelegateValues, | 170 PreferenceValidationDelegateValues, |
| 169 // On Android, make_tuple(..., "null") doesn't compile due to the error: | 171 // On Android, make_tuple(..., "null") doesn't compile due to the error: |
| 170 // testing/gtest/include/gtest/internal/gtest-tuple.h:246:48: | 172 // testing/gtest/include/gtest/internal/gtest-tuple.h:246:48: |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 } | 238 } |
| 237 | 239 |
| 238 PrefHashStoreTransaction::ValueState value_state_; | 240 PrefHashStoreTransaction::ValueState value_state_; |
| 239 bool is_personal_; | 241 bool is_personal_; |
| 240 }; | 242 }; |
| 241 | 243 |
| 242 TEST_P(PreferenceValidationDelegateWithIncident, Atomic) { | 244 TEST_P(PreferenceValidationDelegateWithIncident, Atomic) { |
| 243 instance_->OnAtomicPreferenceValidation( | 245 instance_->OnAtomicPreferenceValidation( |
| 244 kPrefPath_, null_value_.get(), value_state_, is_personal_); | 246 kPrefPath_, null_value_.get(), value_state_, is_personal_); |
| 245 ASSERT_EQ(1U, incidents_.size()); | 247 ASSERT_EQ(1U, incidents_.size()); |
| 246 scoped_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( | 248 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( |
| 247 incidents_.back()->TakePayload()); | 249 incidents_.back()->TakePayload()); |
| 248 EXPECT_TRUE(incident->has_tracked_preference()); | 250 EXPECT_TRUE(incident->has_tracked_preference()); |
| 249 const safe_browsing:: | 251 const safe_browsing:: |
| 250 ClientIncidentReport_IncidentData_TrackedPreferenceIncident& tp_incident = | 252 ClientIncidentReport_IncidentData_TrackedPreferenceIncident& tp_incident = |
| 251 incident->tracked_preference(); | 253 incident->tracked_preference(); |
| 252 EXPECT_EQ(kPrefPath_, tp_incident.path()); | 254 EXPECT_EQ(kPrefPath_, tp_incident.path()); |
| 253 EXPECT_EQ(0, tp_incident.split_key_size()); | 255 EXPECT_EQ(0, tp_incident.split_key_size()); |
| 254 if (!is_personal_) { | 256 if (!is_personal_) { |
| 255 EXPECT_TRUE(tp_incident.has_atomic_value()); | 257 EXPECT_TRUE(tp_incident.has_atomic_value()); |
| 256 EXPECT_EQ(std::string("null"), tp_incident.atomic_value()); | 258 EXPECT_EQ(std::string("null"), tp_incident.atomic_value()); |
| 257 } else { | 259 } else { |
| 258 EXPECT_FALSE(tp_incident.has_atomic_value()); | 260 EXPECT_FALSE(tp_incident.has_atomic_value()); |
| 259 } | 261 } |
| 260 EXPECT_TRUE(tp_incident.has_value_state()); | 262 EXPECT_TRUE(tp_incident.has_value_state()); |
| 261 ExpectValueStatesEquate(value_state_, tp_incident.value_state()); | 263 ExpectValueStatesEquate(value_state_, tp_incident.value_state()); |
| 262 } | 264 } |
| 263 | 265 |
| 264 TEST_P(PreferenceValidationDelegateWithIncident, Split) { | 266 TEST_P(PreferenceValidationDelegateWithIncident, Split) { |
| 265 instance_->OnSplitPreferenceValidation( | 267 instance_->OnSplitPreferenceValidation( |
| 266 kPrefPath_, &dict_value_, invalid_keys_, value_state_, is_personal_); | 268 kPrefPath_, &dict_value_, invalid_keys_, value_state_, is_personal_); |
| 267 ASSERT_EQ(1U, incidents_.size()); | 269 ASSERT_EQ(1U, incidents_.size()); |
| 268 scoped_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( | 270 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( |
| 269 incidents_.back()->TakePayload()); | 271 incidents_.back()->TakePayload()); |
| 270 EXPECT_TRUE(incident->has_tracked_preference()); | 272 EXPECT_TRUE(incident->has_tracked_preference()); |
| 271 const safe_browsing:: | 273 const safe_browsing:: |
| 272 ClientIncidentReport_IncidentData_TrackedPreferenceIncident& tp_incident = | 274 ClientIncidentReport_IncidentData_TrackedPreferenceIncident& tp_incident = |
| 273 incident->tracked_preference(); | 275 incident->tracked_preference(); |
| 274 EXPECT_EQ(kPrefPath_, tp_incident.path()); | 276 EXPECT_EQ(kPrefPath_, tp_incident.path()); |
| 275 EXPECT_FALSE(tp_incident.has_atomic_value()); | 277 EXPECT_FALSE(tp_incident.has_atomic_value()); |
| 276 if (!is_personal_) | 278 if (!is_personal_) |
| 277 ExpectKeysEquate(invalid_keys_, tp_incident.split_key()); | 279 ExpectKeysEquate(invalid_keys_, tp_incident.split_key()); |
| 278 else | 280 else |
| 279 EXPECT_EQ(0, tp_incident.split_key_size()); | 281 EXPECT_EQ(0, tp_incident.split_key_size()); |
| 280 EXPECT_TRUE(tp_incident.has_value_state()); | 282 EXPECT_TRUE(tp_incident.has_value_state()); |
| 281 ExpectValueStatesEquate(value_state_, tp_incident.value_state()); | 283 ExpectValueStatesEquate(value_state_, tp_incident.value_state()); |
| 282 } | 284 } |
| 283 | 285 |
| 284 INSTANTIATE_TEST_CASE_P( | 286 INSTANTIATE_TEST_CASE_P( |
| 285 WithIncident, | 287 WithIncident, |
| 286 PreferenceValidationDelegateWithIncident, | 288 PreferenceValidationDelegateWithIncident, |
| 287 testing::Combine( | 289 testing::Combine( |
| 288 testing::Values(PrefHashStoreTransaction::CLEARED, | 290 testing::Values(PrefHashStoreTransaction::CLEARED, |
| 289 PrefHashStoreTransaction::CHANGED, | 291 PrefHashStoreTransaction::CHANGED, |
| 290 PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE), | 292 PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE), |
| 291 testing::Bool())); | 293 testing::Bool())); |
| OLD | NEW |