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

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

Issue 2719833002: Convert TrackedPreferenceValidationDelegate into a mojo interface. (Closed)
Patch Set: Created 3 years, 9 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>
11 #include <utility> 11 #include <utility>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/bind.h" 14 #include "base/bind.h"
15 #include "base/compiler_specific.h" 15 #include "base/compiler_specific.h"
16 #include "base/values.h" 16 #include "base/values.h"
17 #include "chrome/browser/safe_browsing/incident_reporting/incident.h" 17 #include "chrome/browser/safe_browsing/incident_reporting/incident.h"
18 #include "chrome/browser/safe_browsing/incident_reporting/mock_incident_receiver .h" 18 #include "chrome/browser/safe_browsing/incident_reporting/mock_incident_receiver .h"
19 #include "chrome/common/safe_browsing/csd.pb.h" 19 #include "chrome/common/safe_browsing/csd.pb.h"
20 #include "testing/gmock/include/gmock/gmock.h" 20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
22 22
23 using ::testing::_; 23 using ::testing::_;
24 using ::testing::IsNull; 24 using ::testing::IsNull;
25 using ::testing::NiceMock; 25 using ::testing::NiceMock;
26 using ::testing::WithArg; 26 using ::testing::WithArg;
27 27
28 namespace {
29 const char kPrefPath[] = "atomic.pref";
30 }
31
28 // A basic test harness that creates a delegate instance for which it stores all 32 // A basic test harness that creates a delegate instance for which it stores all
29 // incidents. Tests can push data to the delegate and verify that the test 33 // incidents. Tests can push data to the delegate and verify that the test
30 // instance was provided with the expected data. 34 // instance was provided with the expected data.
31 class PreferenceValidationDelegateTest : public testing::Test { 35 class PreferenceValidationDelegateTest : public testing::Test {
32 protected: 36 protected:
33 typedef std::vector<std::unique_ptr<safe_browsing::Incident>> IncidentVector; 37 typedef std::vector<std::unique_ptr<safe_browsing::Incident>> IncidentVector;
34 38
35 PreferenceValidationDelegateTest()
36 : kPrefPath_("atomic.pref"),
37 null_value_(base::Value::CreateNullValue()) {}
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 external_validation_invalid_keys_.push_back(std::string("three"));
44 std::unique_ptr<safe_browsing::MockIncidentReceiver> receiver( 44 std::unique_ptr<safe_browsing::MockIncidentReceiver> receiver(
45 new NiceMock<safe_browsing::MockIncidentReceiver>()); 45 new NiceMock<safe_browsing::MockIncidentReceiver>());
46 ON_CALL(*receiver, DoAddIncidentForProfile(IsNull(), _)) 46 ON_CALL(*receiver, DoAddIncidentForProfile(IsNull(), _))
47 .WillByDefault(WithArg<1>(TakeIncidentToVector(&incidents_))); 47 .WillByDefault(WithArg<1>(TakeIncidentToVector(&incidents_)));
48 instance_.reset(new safe_browsing::PreferenceValidationDelegate( 48 instance_.reset(new safe_browsing::PreferenceValidationDelegate(
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 83
84 static void ExpectKeysEquate( 84 static void ExpectKeysEquate(
85 const std::vector<std::string>& store_keys, 85 const std::vector<std::string>& store_keys,
86 const google::protobuf::RepeatedPtrField<std::string>& incident_keys) { 86 const google::protobuf::RepeatedPtrField<std::string>& incident_keys) {
87 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()));
88 for (int i = 0; i < incident_keys.size(); ++i) { 88 for (int i = 0; i < incident_keys.size(); ++i) {
89 EXPECT_EQ(store_keys[i], incident_keys.Get(i)); 89 EXPECT_EQ(store_keys[i], incident_keys.Get(i));
90 } 90 }
91 } 91 }
92 92
93 const std::string kPrefPath_;
94 IncidentVector incidents_; 93 IncidentVector incidents_;
95 std::unique_ptr<base::Value> null_value_;
96 base::DictionaryValue dict_value_;
97 std::vector<std::string> invalid_keys_; 94 std::vector<std::string> invalid_keys_;
98 std::vector<std::string> external_validation_invalid_keys_; 95 std::vector<std::string> external_validation_invalid_keys_;
99 std::unique_ptr<TrackedPreferenceValidationDelegate> instance_; 96 std::unique_ptr<prefs::mojom::TrackedPreferenceValidationDelegate> instance_;
100 }; 97 };
101 98
102 // Tests that a NULL value results in an incident with no value. 99 // Tests that a NULL value results in an incident with no value.
103 TEST_F(PreferenceValidationDelegateTest, NullValue) { 100 TEST_F(PreferenceValidationDelegateTest, NullValue) {
104 instance_->OnAtomicPreferenceValidation( 101 instance_->OnAtomicPreferenceValidation(
105 kPrefPath_, NULL, PrefHashStoreTransaction::CLEARED, 102 kPrefPath, NULL, PrefHashStoreTransaction::CLEARED,
106 PrefHashStoreTransaction::UNSUPPORTED, false /* is_personal */); 103 PrefHashStoreTransaction::UNSUPPORTED, false /* is_personal */);
107 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( 104 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident(
108 incidents_.back()->TakePayload()); 105 incidents_.back()->TakePayload());
109 EXPECT_FALSE(incident->tracked_preference().has_atomic_value()); 106 EXPECT_FALSE(incident->tracked_preference().has_atomic_value());
110 EXPECT_EQ( 107 EXPECT_EQ(
111 safe_browsing:: 108 safe_browsing::
112 ClientIncidentReport_IncidentData_TrackedPreferenceIncident::CLEARED, 109 ClientIncidentReport_IncidentData_TrackedPreferenceIncident::CLEARED,
113 incident->tracked_preference().value_state()); 110 incident->tracked_preference().value_state());
114 } 111 }
115 112
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 } 155 }
159 return std::unique_ptr<Value>(); 156 return std::unique_ptr<Value>();
160 } 157 }
161 158
162 base::Value::Type value_type_; 159 base::Value::Type value_type_;
163 const char* expected_value_; 160 const char* expected_value_;
164 }; 161 };
165 162
166 TEST_P(PreferenceValidationDelegateValues, Value) { 163 TEST_P(PreferenceValidationDelegateValues, Value) {
167 instance_->OnAtomicPreferenceValidation( 164 instance_->OnAtomicPreferenceValidation(
168 kPrefPath_, MakeValue(value_type_).get(), 165 kPrefPath, MakeValue(value_type_), PrefHashStoreTransaction::CLEARED,
169 PrefHashStoreTransaction::CLEARED, PrefHashStoreTransaction::UNSUPPORTED, 166 PrefHashStoreTransaction::UNSUPPORTED, false /* is_personal */);
170 false /* is_personal */);
171 ASSERT_EQ(1U, incidents_.size()); 167 ASSERT_EQ(1U, incidents_.size());
172 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( 168 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident(
173 incidents_.back()->TakePayload()); 169 incidents_.back()->TakePayload());
174 EXPECT_EQ(std::string(expected_value_), 170 EXPECT_EQ(std::string(expected_value_),
175 incident->tracked_preference().atomic_value()); 171 incident->tracked_preference().atomic_value());
176 } 172 }
177 173
178 INSTANTIATE_TEST_CASE_P( 174 INSTANTIATE_TEST_CASE_P(
179 Values, 175 Values,
180 PreferenceValidationDelegateValues, 176 PreferenceValidationDelegateValues,
(...skipping 28 matching lines...) Expand all
209 value_state_ = std::tr1::get<0>(GetParam()); 205 value_state_ = std::tr1::get<0>(GetParam());
210 external_validation_value_state_ = std::tr1::get<1>(GetParam()); 206 external_validation_value_state_ = std::tr1::get<1>(GetParam());
211 } 207 }
212 208
213 PrefHashStoreTransaction::ValueState value_state_; 209 PrefHashStoreTransaction::ValueState value_state_;
214 PrefHashStoreTransaction::ValueState external_validation_value_state_; 210 PrefHashStoreTransaction::ValueState external_validation_value_state_;
215 }; 211 };
216 212
217 TEST_P(PreferenceValidationDelegateNoIncident, Atomic) { 213 TEST_P(PreferenceValidationDelegateNoIncident, Atomic) {
218 instance_->OnAtomicPreferenceValidation( 214 instance_->OnAtomicPreferenceValidation(
219 kPrefPath_, null_value_.get(), value_state_, 215 kPrefPath, base::Value::CreateNullValue(), value_state_,
220 external_validation_value_state_, false /* is_personal */); 216 external_validation_value_state_, false /* is_personal */);
221 EXPECT_EQ(0U, incidents_.size()); 217 EXPECT_EQ(0U, incidents_.size());
222 } 218 }
223 219
224 TEST_P(PreferenceValidationDelegateNoIncident, Split) { 220 TEST_P(PreferenceValidationDelegateNoIncident, Split) {
225 instance_->OnSplitPreferenceValidation( 221 instance_->OnSplitPreferenceValidation(
226 kPrefPath_, &dict_value_, invalid_keys_, 222 kPrefPath, invalid_keys_, external_validation_invalid_keys_, value_state_,
227 external_validation_invalid_keys_, value_state_,
228 external_validation_value_state_, false /* is_personal */); 223 external_validation_value_state_, false /* is_personal */);
229 EXPECT_EQ(0U, incidents_.size()); 224 EXPECT_EQ(0U, incidents_.size());
230 } 225 }
231 226
232 INSTANTIATE_TEST_CASE_P( 227 INSTANTIATE_TEST_CASE_P(
233 NoIncident, 228 NoIncident,
234 PreferenceValidationDelegateNoIncident, 229 PreferenceValidationDelegateNoIncident,
235 testing::Combine( 230 testing::Combine(
236 testing::Values(PrefHashStoreTransaction::UNCHANGED, 231 testing::Values(PrefHashStoreTransaction::UNCHANGED,
237 PrefHashStoreTransaction::SECURE_LEGACY, 232 PrefHashStoreTransaction::SECURE_LEGACY,
(...skipping 18 matching lines...) Expand all
256 is_personal_ = std::tr1::get<2>(GetParam()); 251 is_personal_ = std::tr1::get<2>(GetParam());
257 } 252 }
258 253
259 PrefHashStoreTransaction::ValueState value_state_; 254 PrefHashStoreTransaction::ValueState value_state_;
260 PrefHashStoreTransaction::ValueState external_validation_value_state_; 255 PrefHashStoreTransaction::ValueState external_validation_value_state_;
261 bool is_personal_; 256 bool is_personal_;
262 }; 257 };
263 258
264 TEST_P(PreferenceValidationDelegateWithIncident, Atomic) { 259 TEST_P(PreferenceValidationDelegateWithIncident, Atomic) {
265 instance_->OnAtomicPreferenceValidation( 260 instance_->OnAtomicPreferenceValidation(
266 kPrefPath_, null_value_.get(), value_state_, 261 kPrefPath, base::Value::CreateNullValue(), value_state_,
267 external_validation_value_state_, is_personal_); 262 external_validation_value_state_, is_personal_);
268 ASSERT_EQ(1U, incidents_.size()); 263 ASSERT_EQ(1U, incidents_.size());
269 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( 264 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident(
270 incidents_.back()->TakePayload()); 265 incidents_.back()->TakePayload());
271 EXPECT_TRUE(incident->has_tracked_preference()); 266 EXPECT_TRUE(incident->has_tracked_preference());
272 const safe_browsing:: 267 const safe_browsing::
273 ClientIncidentReport_IncidentData_TrackedPreferenceIncident& tp_incident = 268 ClientIncidentReport_IncidentData_TrackedPreferenceIncident& tp_incident =
274 incident->tracked_preference(); 269 incident->tracked_preference();
275 EXPECT_EQ(kPrefPath_, tp_incident.path()); 270 EXPECT_EQ(kPrefPath, tp_incident.path());
276 EXPECT_EQ(0, tp_incident.split_key_size()); 271 EXPECT_EQ(0, tp_incident.split_key_size());
277 if (!is_personal_) { 272 if (!is_personal_) {
278 EXPECT_TRUE(tp_incident.has_atomic_value()); 273 EXPECT_TRUE(tp_incident.has_atomic_value());
279 EXPECT_EQ(std::string("null"), tp_incident.atomic_value()); 274 EXPECT_EQ(std::string("null"), tp_incident.atomic_value());
280 } else { 275 } else {
281 EXPECT_FALSE(tp_incident.has_atomic_value()); 276 EXPECT_FALSE(tp_incident.has_atomic_value());
282 } 277 }
283 EXPECT_TRUE(tp_incident.has_value_state()); 278 EXPECT_TRUE(tp_incident.has_value_state());
284 ExpectValueStatesEquate(value_state_, external_validation_value_state_, 279 ExpectValueStatesEquate(value_state_, external_validation_value_state_,
285 tp_incident.value_state()); 280 tp_incident.value_state());
286 } 281 }
287 282
288 TEST_P(PreferenceValidationDelegateWithIncident, Split) { 283 TEST_P(PreferenceValidationDelegateWithIncident, Split) {
289 instance_->OnSplitPreferenceValidation( 284 instance_->OnSplitPreferenceValidation(
290 kPrefPath_, &dict_value_, invalid_keys_, 285 kPrefPath, invalid_keys_, external_validation_invalid_keys_, value_state_,
291 external_validation_invalid_keys_, value_state_,
292 external_validation_value_state_, is_personal_); 286 external_validation_value_state_, is_personal_);
293 ASSERT_EQ(1U, incidents_.size()); 287 ASSERT_EQ(1U, incidents_.size());
294 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( 288 std::unique_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident(
295 incidents_.back()->TakePayload()); 289 incidents_.back()->TakePayload());
296 EXPECT_TRUE(incident->has_tracked_preference()); 290 EXPECT_TRUE(incident->has_tracked_preference());
297 const safe_browsing:: 291 const safe_browsing::
298 ClientIncidentReport_IncidentData_TrackedPreferenceIncident& tp_incident = 292 ClientIncidentReport_IncidentData_TrackedPreferenceIncident& tp_incident =
299 incident->tracked_preference(); 293 incident->tracked_preference();
300 EXPECT_EQ(kPrefPath_, tp_incident.path()); 294 EXPECT_EQ(kPrefPath, tp_incident.path());
301 EXPECT_FALSE(tp_incident.has_atomic_value()); 295 EXPECT_FALSE(tp_incident.has_atomic_value());
302 if (!is_personal_) { 296 if (!is_personal_) {
303 if (value_state_ == PrefHashStoreTransaction::CLEARED || 297 if (value_state_ == PrefHashStoreTransaction::CLEARED ||
304 value_state_ == PrefHashStoreTransaction::CHANGED || 298 value_state_ == PrefHashStoreTransaction::CHANGED ||
305 value_state_ == PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE) { 299 value_state_ == PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE) {
306 ExpectKeysEquate(invalid_keys_, tp_incident.split_key()); 300 ExpectKeysEquate(invalid_keys_, tp_incident.split_key());
307 } else { 301 } else {
308 ExpectKeysEquate(external_validation_invalid_keys_, 302 ExpectKeysEquate(external_validation_invalid_keys_,
309 tp_incident.split_key()); 303 tp_incident.split_key());
310 } 304 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 INSTANTIATE_TEST_CASE_P( 336 INSTANTIATE_TEST_CASE_P(
343 WithIncidentIgnoreBypass, 337 WithIncidentIgnoreBypass,
344 PreferenceValidationDelegateWithIncident, 338 PreferenceValidationDelegateWithIncident,
345 testing::Combine( 339 testing::Combine(
346 testing::Values(PrefHashStoreTransaction::CLEARED, 340 testing::Values(PrefHashStoreTransaction::CLEARED,
347 PrefHashStoreTransaction::CHANGED, 341 PrefHashStoreTransaction::CHANGED,
348 PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE), 342 PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE),
349 testing::Values(PrefHashStoreTransaction::CHANGED, 343 testing::Values(PrefHashStoreTransaction::CHANGED,
350 PrefHashStoreTransaction::CLEARED), 344 PrefHashStoreTransaction::CLEARED),
351 testing::Bool())); 345 testing::Bool()));
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698