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

Side by Side Diff: components/safe_browsing_db/v4_update_protocol_manager_unittest.cc

Issue 2062013002: Fetch incremental updates. Store new state in V4Store. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Minor: Fix DCHECK Created 4 years, 5 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "components/safe_browsing_db/v4_update_protocol_manager.h" 5 #include "components/safe_browsing_db/v4_update_protocol_manager.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/base64.h" 10 #include "base/base64.h"
11 #include "base/memory/ptr_util.h"
11 #include "base/strings/stringprintf.h" 12 #include "base/strings/stringprintf.h"
12 #include "base/test/test_simple_task_runner.h" 13 #include "base/test/test_simple_task_runner.h"
13 #include "base/threading/thread_task_runner_handle.h" 14 #include "base/threading/thread_task_runner_handle.h"
14 #include "base/time/time.h" 15 #include "base/time/time.h"
15 #include "components/safe_browsing_db/safebrowsing.pb.h" 16 #include "components/safe_browsing_db/safebrowsing.pb.h"
16 #include "components/safe_browsing_db/util.h" 17 #include "components/safe_browsing_db/util.h"
17 #include "net/base/escape.h" 18 #include "net/base/escape.h"
18 #include "net/base/load_flags.h" 19 #include "net/base/load_flags.h"
19 #include "net/base/net_errors.h" 20 #include "net/base/net_errors.h"
20 #include "net/url_request/test_url_fetcher_factory.h" 21 #include "net/url_request/test_url_fetcher_factory.h"
21 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "testing/platform_test.h"
22 24
23 using base::Time; 25 using base::Time;
24 using base::TimeDelta; 26 using base::TimeDelta;
25 27
26 namespace { 28 namespace {
27 29
28 const char kClient[] = "unittest"; 30 const char kClient[] = "unittest";
29 const char kAppVer[] = "1.0"; 31 const char kAppVer[] = "1.0";
30 const char kKeyParam[] = "test_key_param"; 32 const char kKeyParam[] = "test_key_param";
31 33
32 } // namespace 34 } // namespace
33 35
34 namespace safe_browsing { 36 namespace safe_browsing {
35 37
36 class V4UpdateProtocolManagerTest : public testing::Test { 38 class V4UpdateProtocolManagerTest : public PlatformTest {
39 void SetUp() override {
40 PlatformTest::SetUp();
41
42 SetupStoreStates();
43 }
44
37 protected: 45 protected:
38 void ValidateGetUpdatesResults( 46 void ValidateGetUpdatesResults(
39 const std::vector<ListUpdateResponse>& expected_lurs, 47 const std::vector<ListUpdateResponse>& expected_lurs,
40 const std::vector<ListUpdateResponse>& list_update_responses) { 48 const std::vector<ListUpdateResponse>& list_update_responses) {
41 // The callback should never be called if expect_callback_to_be_called_ is 49 // The callback should never be called if expect_callback_to_be_called_ is
42 // false. 50 // false.
43 EXPECT_TRUE(expect_callback_to_be_called_); 51 EXPECT_TRUE(expect_callback_to_be_called_);
44 ASSERT_EQ(expected_lurs.size(), list_update_responses.size()); 52 ASSERT_EQ(expected_lurs.size(), list_update_responses.size());
45 53
46 for (unsigned int i = 0; i < list_update_responses.size(); ++i) { 54 for (unsigned int i = 0; i < list_update_responses.size(); ++i) {
47 const ListUpdateResponse& expected = expected_lurs[i]; 55 const ListUpdateResponse& expected = expected_lurs[i];
48 const ListUpdateResponse& actual = list_update_responses[i]; 56 const ListUpdateResponse& actual = list_update_responses[i];
49 57
50 EXPECT_EQ(expected.platform_type(), actual.platform_type()); 58 EXPECT_EQ(expected.platform_type(), actual.platform_type());
51 EXPECT_EQ(expected.response_type(), actual.response_type()); 59 EXPECT_EQ(expected.response_type(), actual.response_type());
52 EXPECT_EQ(expected.threat_entry_type(), actual.threat_entry_type()); 60 EXPECT_EQ(expected.threat_entry_type(), actual.threat_entry_type());
53 EXPECT_EQ(expected.threat_type(), actual.threat_type()); 61 EXPECT_EQ(expected.threat_type(), actual.threat_type());
54 EXPECT_EQ(expected.new_client_state(), actual.new_client_state()); 62 EXPECT_EQ(expected.new_client_state(), actual.new_client_state());
55 63
56 // TODO(vakh): Test more fields from the proto. 64 // TODO(vakh): Test more fields from the proto.
57 } 65 }
58 } 66 }
59 67
60 std::unique_ptr<V4UpdateProtocolManager> CreateProtocolManager( 68 std::unique_ptr<V4UpdateProtocolManager> CreateProtocolManager(
61 const base::hash_map<UpdateListIdentifier, std::string>
62 current_list_states,
63 const std::vector<ListUpdateResponse>& expected_lurs) { 69 const std::vector<ListUpdateResponse>& expected_lurs) {
64 V4ProtocolConfig config; 70 V4ProtocolConfig config;
65 config.client_name = kClient; 71 config.client_name = kClient;
66 config.version = kAppVer; 72 config.version = kAppVer;
67 config.key_param = kKeyParam; 73 config.key_param = kKeyParam;
68 config.disable_auto_update = false; 74 config.disable_auto_update = false;
69 return V4UpdateProtocolManager::Create( 75 return V4UpdateProtocolManager::Create(
70 NULL, config, current_list_states, 76 NULL, config,
71 base::Bind(&V4UpdateProtocolManagerTest::ValidateGetUpdatesResults, 77 base::Bind(&V4UpdateProtocolManagerTest::ValidateGetUpdatesResults,
72 base::Unretained(this), expected_lurs)); 78 base::Unretained(this), expected_lurs));
73 } 79 }
74 80
75 void SetupCurrentListStates( 81 void SetupStoreStates() {
76 base::hash_map<UpdateListIdentifier, std::string>* current_list_states) { 82 store_state_map_ = base::MakeUnique<StoreStateMap>();
77 UpdateListIdentifier list_identifier;
78 list_identifier.platform_type = WINDOWS_PLATFORM;
79 list_identifier.threat_entry_type = URL;
80 list_identifier.threat_type = MALWARE_THREAT;
81 current_list_states->insert({list_identifier, "initial_state_1"});
82 83
83 list_identifier.platform_type = WINDOWS_PLATFORM; 84 UpdateListIdentifier win_url_malware(WINDOWS_PLATFORM, URL, MALWARE_THREAT);
84 list_identifier.threat_entry_type = URL; 85 store_state_map_->insert({win_url_malware, "initial_state_1"});
85 list_identifier.threat_type = UNWANTED_SOFTWARE;
86 current_list_states->insert({list_identifier, "initial_state_2"});
87 86
88 list_identifier.platform_type = WINDOWS_PLATFORM; 87 UpdateListIdentifier win_url_uws(WINDOWS_PLATFORM, URL, UNWANTED_SOFTWARE);
89 list_identifier.threat_entry_type = EXECUTABLE; 88 store_state_map_->insert({win_url_uws, "initial_state_2"});
90 list_identifier.threat_type = MALWARE_THREAT; 89
91 current_list_states->insert({list_identifier, "initial_state_3"}); 90 UpdateListIdentifier win_exe_uws(WINDOWS_PLATFORM, EXECUTABLE,
91 UNWANTED_SOFTWARE);
92 store_state_map_->insert({win_exe_uws, "initial_state_3"});
92 } 93 }
93 94
94 void SetupExpectedListUpdateResponse( 95 void SetupExpectedListUpdateResponse(
95 std::vector<ListUpdateResponse>* expected_lurs) { 96 std::vector<ListUpdateResponse>* expected_lurs) {
96 ListUpdateResponse lur; 97 ListUpdateResponse lur;
97 lur.set_platform_type(WINDOWS_PLATFORM); 98 lur.set_platform_type(WINDOWS_PLATFORM);
98 lur.set_response_type(ListUpdateResponse::PARTIAL_UPDATE); 99 lur.set_response_type(ListUpdateResponse::PARTIAL_UPDATE);
99 lur.set_threat_entry_type(URL); 100 lur.set_threat_entry_type(URL);
100 lur.set_threat_type(MALWARE_THREAT); 101 lur.set_threat_type(MALWARE_THREAT);
101 lur.set_new_client_state("new_state_1"); 102 lur.set_new_client_state("new_state_1");
(...skipping 28 matching lines...) Expand all
130 } 131 }
131 132
132 // Serialize. 133 // Serialize.
133 std::string res_data; 134 std::string res_data;
134 response.SerializeToString(&res_data); 135 response.SerializeToString(&res_data);
135 136
136 return res_data; 137 return res_data;
137 } 138 }
138 139
139 bool expect_callback_to_be_called_; 140 bool expect_callback_to_be_called_;
141 std::unique_ptr<StoreStateMap> store_state_map_;
140 }; 142 };
141 143
142 // TODO(vakh): Add many more tests. 144 // TODO(vakh): Add many more tests.
143 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingNetwork) { 145 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingNetwork) {
144 scoped_refptr<base::TestSimpleTaskRunner> runner( 146 scoped_refptr<base::TestSimpleTaskRunner> runner(
145 new base::TestSimpleTaskRunner()); 147 new base::TestSimpleTaskRunner());
146 base::ThreadTaskRunnerHandle runner_handler(runner); 148 base::ThreadTaskRunnerHandle runner_handler(runner);
147 net::TestURLFetcherFactory factory; 149 net::TestURLFetcherFactory factory;
148 const base::hash_map<UpdateListIdentifier, std::string> current_list_states;
149 const std::vector<ListUpdateResponse> expected_lurs; 150 const std::vector<ListUpdateResponse> expected_lurs;
150 std::unique_ptr<V4UpdateProtocolManager> pm( 151 std::unique_ptr<V4UpdateProtocolManager> pm(
151 CreateProtocolManager(current_list_states, expected_lurs)); 152 CreateProtocolManager(expected_lurs));
152 runner->ClearPendingTasks(); 153 runner->ClearPendingTasks();
153 154
154 // Initial state. No errors. 155 // Initial state. No errors.
155 EXPECT_EQ(0ul, pm->update_error_count_); 156 EXPECT_EQ(0ul, pm->update_error_count_);
156 EXPECT_EQ(1ul, pm->update_back_off_mult_); 157 EXPECT_EQ(1ul, pm->update_back_off_mult_);
157 expect_callback_to_be_called_ = false; 158 expect_callback_to_be_called_ = false;
159 pm->store_state_map_ = std::move(store_state_map_);
158 pm->IssueUpdateRequest(); 160 pm->IssueUpdateRequest();
159 161
160 EXPECT_FALSE(pm->IsUpdateScheduled()); 162 EXPECT_FALSE(pm->IsUpdateScheduled());
161 163
162 runner->RunPendingTasks(); 164 runner->RunPendingTasks();
163 165
164 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 166 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
165 DCHECK(fetcher); 167 DCHECK(fetcher);
166 // Failed request status should result in error. 168 // Failed request status should result in error.
167 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, 169 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
168 net::ERR_CONNECTION_RESET)); 170 net::ERR_CONNECTION_RESET));
169 fetcher->delegate()->OnURLFetchComplete(fetcher); 171 fetcher->delegate()->OnURLFetchComplete(fetcher);
170 172
171 // Should have recorded one error, but back off multiplier is unchanged. 173 // Should have recorded one error, but back off multiplier is unchanged.
172 EXPECT_EQ(1ul, pm->update_error_count_); 174 EXPECT_EQ(1ul, pm->update_error_count_);
173 EXPECT_EQ(1ul, pm->update_back_off_mult_); 175 EXPECT_EQ(1ul, pm->update_back_off_mult_);
174 EXPECT_TRUE(pm->IsUpdateScheduled()); 176 EXPECT_TRUE(pm->IsUpdateScheduled());
175 } 177 }
176 178
177 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingResponseCode) { 179 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingResponseCode) {
178 scoped_refptr<base::TestSimpleTaskRunner> runner( 180 scoped_refptr<base::TestSimpleTaskRunner> runner(
179 new base::TestSimpleTaskRunner()); 181 new base::TestSimpleTaskRunner());
180 base::ThreadTaskRunnerHandle runner_handler(runner); 182 base::ThreadTaskRunnerHandle runner_handler(runner);
181 net::TestURLFetcherFactory factory; 183 net::TestURLFetcherFactory factory;
182 const std::vector<ListUpdateResponse> expected_lurs; 184 const std::vector<ListUpdateResponse> expected_lurs;
183 const base::hash_map<UpdateListIdentifier, std::string> current_list_states;
184 std::unique_ptr<V4UpdateProtocolManager> pm( 185 std::unique_ptr<V4UpdateProtocolManager> pm(
185 CreateProtocolManager(current_list_states, expected_lurs)); 186 CreateProtocolManager(expected_lurs));
186 runner->ClearPendingTasks(); 187 runner->ClearPendingTasks();
187 188
188 // Initial state. No errors. 189 // Initial state. No errors.
189 EXPECT_EQ(0ul, pm->update_error_count_); 190 EXPECT_EQ(0ul, pm->update_error_count_);
190 EXPECT_EQ(1ul, pm->update_back_off_mult_); 191 EXPECT_EQ(1ul, pm->update_back_off_mult_);
191 expect_callback_to_be_called_ = false; 192 expect_callback_to_be_called_ = false;
193 pm->store_state_map_ = std::move(store_state_map_);
192 pm->IssueUpdateRequest(); 194 pm->IssueUpdateRequest();
193 195
194 EXPECT_FALSE(pm->IsUpdateScheduled()); 196 EXPECT_FALSE(pm->IsUpdateScheduled());
195 197
196 runner->RunPendingTasks(); 198 runner->RunPendingTasks();
197 199
198 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 200 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
199 DCHECK(fetcher); 201 DCHECK(fetcher);
200 fetcher->set_status(net::URLRequestStatus()); 202 fetcher->set_status(net::URLRequestStatus());
201 // Response code of anything other than 200 should result in error. 203 // Response code of anything other than 200 should result in error.
202 fetcher->set_response_code(net::HTTP_NO_CONTENT); 204 fetcher->set_response_code(net::HTTP_NO_CONTENT);
203 fetcher->SetResponseString(""); 205 fetcher->SetResponseString("");
204 fetcher->delegate()->OnURLFetchComplete(fetcher); 206 fetcher->delegate()->OnURLFetchComplete(fetcher);
205 207
206 // Should have recorded one error, but back off multiplier is unchanged. 208 // Should have recorded one error, but back off multiplier is unchanged.
207 EXPECT_EQ(1ul, pm->update_error_count_); 209 EXPECT_EQ(1ul, pm->update_error_count_);
208 EXPECT_EQ(1ul, pm->update_back_off_mult_); 210 EXPECT_EQ(1ul, pm->update_back_off_mult_);
209 EXPECT_TRUE(pm->IsUpdateScheduled()); 211 EXPECT_TRUE(pm->IsUpdateScheduled());
210 } 212 }
211 213
212 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesNoError) { 214 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesNoError) {
213 scoped_refptr<base::TestSimpleTaskRunner> runner( 215 scoped_refptr<base::TestSimpleTaskRunner> runner(
214 new base::TestSimpleTaskRunner()); 216 new base::TestSimpleTaskRunner());
215 base::ThreadTaskRunnerHandle runner_handler(runner); 217 base::ThreadTaskRunnerHandle runner_handler(runner);
216 net::TestURLFetcherFactory factory; 218 net::TestURLFetcherFactory factory;
217 std::vector<ListUpdateResponse> expected_lurs; 219 std::vector<ListUpdateResponse> expected_lurs;
218 SetupExpectedListUpdateResponse(&expected_lurs); 220 SetupExpectedListUpdateResponse(&expected_lurs);
219 base::hash_map<UpdateListIdentifier, std::string> current_list_states;
220 SetupCurrentListStates(&current_list_states);
221 std::unique_ptr<V4UpdateProtocolManager> pm( 221 std::unique_ptr<V4UpdateProtocolManager> pm(
222 CreateProtocolManager(current_list_states, expected_lurs)); 222 CreateProtocolManager(expected_lurs));
223 runner->ClearPendingTasks(); 223 runner->ClearPendingTasks();
224 224
225 // Initial state. No errors. 225 // Initial state. No errors.
226 EXPECT_EQ(0ul, pm->update_error_count_); 226 EXPECT_EQ(0ul, pm->update_error_count_);
227 EXPECT_EQ(1ul, pm->update_back_off_mult_); 227 EXPECT_EQ(1ul, pm->update_back_off_mult_);
228 expect_callback_to_be_called_ = true; 228 expect_callback_to_be_called_ = true;
229 pm->store_state_map_ = std::move(store_state_map_);
229 pm->IssueUpdateRequest(); 230 pm->IssueUpdateRequest();
230 231
231 EXPECT_FALSE(pm->IsUpdateScheduled()); 232 EXPECT_FALSE(pm->IsUpdateScheduled());
232 233
233 runner->RunPendingTasks(); 234 runner->RunPendingTasks();
234 235
235 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 236 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
236 DCHECK(fetcher); 237 DCHECK(fetcher);
237 fetcher->set_status(net::URLRequestStatus()); 238 fetcher->set_status(net::URLRequestStatus());
238 fetcher->set_response_code(net::HTTP_OK); 239 fetcher->set_response_code(net::HTTP_OK);
239 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs)); 240 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs));
240 fetcher->delegate()->OnURLFetchComplete(fetcher); 241 fetcher->delegate()->OnURLFetchComplete(fetcher);
241 242
242 // No error, back off multiplier is unchanged. 243 // No error, back off multiplier is unchanged.
243 EXPECT_EQ(0ul, pm->update_error_count_); 244 EXPECT_EQ(0ul, pm->update_error_count_);
244 EXPECT_EQ(1ul, pm->update_back_off_mult_); 245 EXPECT_EQ(1ul, pm->update_back_off_mult_);
245 EXPECT_FALSE(pm->IsUpdateScheduled()); 246 EXPECT_FALSE(pm->IsUpdateScheduled());
246 } 247 }
247 248
248 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesWithOneBackoff) { 249 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesWithOneBackoff) {
249 scoped_refptr<base::TestSimpleTaskRunner> runner( 250 scoped_refptr<base::TestSimpleTaskRunner> runner(
250 new base::TestSimpleTaskRunner()); 251 new base::TestSimpleTaskRunner());
251 base::ThreadTaskRunnerHandle runner_handler(runner); 252 base::ThreadTaskRunnerHandle runner_handler(runner);
252 net::TestURLFetcherFactory factory; 253 net::TestURLFetcherFactory factory;
253 std::vector<ListUpdateResponse> expected_lurs; 254 std::vector<ListUpdateResponse> expected_lurs;
254 SetupExpectedListUpdateResponse(&expected_lurs); 255 SetupExpectedListUpdateResponse(&expected_lurs);
255 base::hash_map<UpdateListIdentifier, std::string> current_list_states;
256 SetupCurrentListStates(&current_list_states);
257 std::unique_ptr<V4UpdateProtocolManager> pm( 256 std::unique_ptr<V4UpdateProtocolManager> pm(
258 CreateProtocolManager(current_list_states, expected_lurs)); 257 CreateProtocolManager(expected_lurs));
259 runner->ClearPendingTasks(); 258 runner->ClearPendingTasks();
260 259
261 // Initial state. No errors. 260 // Initial state. No errors.
262 EXPECT_EQ(0ul, pm->update_error_count_); 261 EXPECT_EQ(0ul, pm->update_error_count_);
263 EXPECT_EQ(1ul, pm->update_back_off_mult_); 262 EXPECT_EQ(1ul, pm->update_back_off_mult_);
264 expect_callback_to_be_called_ = false; 263 expect_callback_to_be_called_ = false;
264 pm->store_state_map_ = std::move(store_state_map_);
265 pm->IssueUpdateRequest(); 265 pm->IssueUpdateRequest();
266 266
267 EXPECT_FALSE(pm->IsUpdateScheduled()); 267 EXPECT_FALSE(pm->IsUpdateScheduled());
268 268
269 runner->RunPendingTasks(); 269 runner->RunPendingTasks();
270 270
271 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 271 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
272 DCHECK(fetcher); 272 DCHECK(fetcher);
273 fetcher->set_status(net::URLRequestStatus()); 273 fetcher->set_status(net::URLRequestStatus());
274 // Response code of anything other than 200 should result in error. 274 // Response code of anything other than 200 should result in error.
(...skipping 17 matching lines...) Expand all
292 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs)); 292 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs));
293 fetcher->delegate()->OnURLFetchComplete(fetcher); 293 fetcher->delegate()->OnURLFetchComplete(fetcher);
294 294
295 // No error, back off multiplier is unchanged. 295 // No error, back off multiplier is unchanged.
296 EXPECT_EQ(0ul, pm->update_error_count_); 296 EXPECT_EQ(0ul, pm->update_error_count_);
297 EXPECT_EQ(1ul, pm->update_back_off_mult_); 297 EXPECT_EQ(1ul, pm->update_back_off_mult_);
298 EXPECT_FALSE(pm->IsUpdateScheduled()); 298 EXPECT_FALSE(pm->IsUpdateScheduled());
299 } 299 }
300 300
301 } // namespace safe_browsing 301 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698