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

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

Issue 1853833002: Added a test for success after one backoff (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@v4_02_init_update
Patch Set: git fetch && pull && bring back checks for update_error_count_ Created 4 years, 8 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
« no previous file with comments | « components/safe_browsing_db/v4_update_protocol_manager.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <vector> 5 #include <vector>
6 6
7 #include "base/base64.h" 7 #include "base/base64.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 #include "base/test/test_simple_task_runner.h" 10 #include "base/test/test_simple_task_runner.h"
(...skipping 16 matching lines...) Expand all
27 const char kClient[] = "unittest"; 27 const char kClient[] = "unittest";
28 const char kAppVer[] = "1.0"; 28 const char kAppVer[] = "1.0";
29 const char kKeyParam[] = "test_key_param"; 29 const char kKeyParam[] = "test_key_param";
30 30
31 } // namespace 31 } // namespace
32 32
33 namespace safe_browsing { 33 namespace safe_browsing {
34 34
35 class V4UpdateProtocolManagerTest : public testing::Test { 35 class V4UpdateProtocolManagerTest : public testing::Test {
36 protected: 36 protected:
37 static void ValidateGetUpdatesResults( 37 void ValidateGetUpdatesResults(
38 const std::vector<ListUpdateResponse>& expected_lurs, 38 const std::vector<ListUpdateResponse>& expected_lurs,
39 const std::vector<ListUpdateResponse>& list_update_responses) { 39 const std::vector<ListUpdateResponse>& list_update_responses) {
40 // The callback should never be called if expect_callback_to_be_called_ is
41 // false.
42 EXPECT_TRUE(expect_callback_to_be_called_);
40 ASSERT_EQ(expected_lurs.size(), list_update_responses.size()); 43 ASSERT_EQ(expected_lurs.size(), list_update_responses.size());
41 44
42 for (unsigned int i = 0; i < list_update_responses.size(); ++i) { 45 for (unsigned int i = 0; i < list_update_responses.size(); ++i) {
43 const ListUpdateResponse& expected = expected_lurs[i]; 46 const ListUpdateResponse& expected = expected_lurs[i];
44 const ListUpdateResponse& actual = list_update_responses[i]; 47 const ListUpdateResponse& actual = list_update_responses[i];
45 48
46 EXPECT_EQ(expected.platform_type(), actual.platform_type()); 49 EXPECT_EQ(expected.platform_type(), actual.platform_type());
47 EXPECT_EQ(expected.response_type(), actual.response_type()); 50 EXPECT_EQ(expected.response_type(), actual.response_type());
48 EXPECT_EQ(expected.threat_entry_type(), actual.threat_entry_type()); 51 EXPECT_EQ(expected.threat_entry_type(), actual.threat_entry_type());
49 EXPECT_EQ(expected.threat_type(), actual.threat_type()); 52 EXPECT_EQ(expected.threat_type(), actual.threat_type());
50 EXPECT_EQ(expected.new_client_state(), actual.new_client_state()); 53 EXPECT_EQ(expected.new_client_state(), actual.new_client_state());
51 54
52 // TODO(vakh): Test more fields from the proto. 55 // TODO(vakh): Test more fields from the proto.
53 } 56 }
54 } 57 }
55 58
56 scoped_ptr<V4UpdateProtocolManager> CreateProtocolManager( 59 scoped_ptr<V4UpdateProtocolManager> CreateProtocolManager(
57 const base::hash_map<UpdateListIdentifier, std::string> 60 const base::hash_map<UpdateListIdentifier, std::string>
58 current_list_states, 61 current_list_states,
59 const std::vector<ListUpdateResponse>& expected_lurs) { 62 const std::vector<ListUpdateResponse>& expected_lurs) {
60 V4ProtocolConfig config; 63 V4ProtocolConfig config;
61 config.client_name = kClient; 64 config.client_name = kClient;
62 config.version = kAppVer; 65 config.version = kAppVer;
63 config.key_param = kKeyParam; 66 config.key_param = kKeyParam;
64 config.disable_auto_update = false; 67 config.disable_auto_update = false;
65 return V4UpdateProtocolManager::Create( 68 return V4UpdateProtocolManager::Create(
66 NULL, config, current_list_states, 69 NULL, config, current_list_states,
67 base::Bind(ValidateGetUpdatesResults, expected_lurs)); 70 base::Bind(&V4UpdateProtocolManagerTest::ValidateGetUpdatesResults,
71 base::Unretained(this), expected_lurs));
68 } 72 }
69 73
70 void SetupCurrentListStates( 74 void SetupCurrentListStates(
71 base::hash_map<UpdateListIdentifier, std::string>* current_list_states) { 75 base::hash_map<UpdateListIdentifier, std::string>* current_list_states) {
72 UpdateListIdentifier list_identifier; 76 UpdateListIdentifier list_identifier;
73 list_identifier.platform_type = WINDOWS_PLATFORM; 77 list_identifier.platform_type = WINDOWS_PLATFORM;
74 list_identifier.threat_entry_type = URL_EXPRESSION; 78 list_identifier.threat_entry_type = URL_EXPRESSION;
75 list_identifier.threat_type = MALWARE_THREAT; 79 list_identifier.threat_type = MALWARE_THREAT;
76 current_list_states->insert({list_identifier, "initial_state_1"}); 80 current_list_states->insert({list_identifier, "initial_state_1"});
77 81
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 lur->set_threat_entry_type(expected_lur.threat_entry_type()); 127 lur->set_threat_entry_type(expected_lur.threat_entry_type());
124 lur->set_threat_type(expected_lur.threat_type()); 128 lur->set_threat_type(expected_lur.threat_type());
125 } 129 }
126 130
127 // Serialize. 131 // Serialize.
128 std::string res_data; 132 std::string res_data;
129 response.SerializeToString(&res_data); 133 response.SerializeToString(&res_data);
130 134
131 return res_data; 135 return res_data;
132 } 136 }
137
138 bool expect_callback_to_be_called_;
133 }; 139 };
134 140
135 // TODO(vakh): Add many more tests. 141 // TODO(vakh): Add many more tests.
136 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingNetwork) { 142 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingNetwork) {
137 scoped_refptr<base::TestSimpleTaskRunner> runner( 143 scoped_refptr<base::TestSimpleTaskRunner> runner(
138 new base::TestSimpleTaskRunner()); 144 new base::TestSimpleTaskRunner());
139 base::ThreadTaskRunnerHandle runner_handler(runner); 145 base::ThreadTaskRunnerHandle runner_handler(runner);
140 net::TestURLFetcherFactory factory; 146 net::TestURLFetcherFactory factory;
141 const base::hash_map<UpdateListIdentifier, std::string> current_list_states; 147 const base::hash_map<UpdateListIdentifier, std::string> current_list_states;
142 const std::vector<ListUpdateResponse> expected_lurs; 148 const std::vector<ListUpdateResponse> expected_lurs;
143 scoped_ptr<V4UpdateProtocolManager> pm( 149 scoped_ptr<V4UpdateProtocolManager> pm(
144 CreateProtocolManager(current_list_states, expected_lurs)); 150 CreateProtocolManager(current_list_states, expected_lurs));
145 runner->ClearPendingTasks(); 151 runner->ClearPendingTasks();
146 152
147 // Initial state. No errors. 153 // Initial state. No errors.
148 EXPECT_EQ(0ul, pm->update_error_count_); 154 EXPECT_EQ(0ul, pm->update_error_count_);
149 EXPECT_EQ(1ul, pm->update_back_off_mult_); 155 EXPECT_EQ(1ul, pm->update_back_off_mult_);
156 expect_callback_to_be_called_ = false;
157 pm->IssueUpdateRequest();
150 158
151 pm->IssueUpdateRequest();
152 EXPECT_FALSE(pm->IsUpdateScheduled()); 159 EXPECT_FALSE(pm->IsUpdateScheduled());
153 160
154 runner->RunPendingTasks(); 161 runner->RunPendingTasks();
155 162
156 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 163 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
157 DCHECK(fetcher); 164 DCHECK(fetcher);
158 // Failed request status should result in error. 165 // Failed request status should result in error.
159 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, 166 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
160 net::ERR_CONNECTION_RESET)); 167 net::ERR_CONNECTION_RESET));
161 fetcher->delegate()->OnURLFetchComplete(fetcher); 168 fetcher->delegate()->OnURLFetchComplete(fetcher);
162 169
163 // Should have recorded one error, but back off multiplier is unchanged. 170 // Should have recorded one error, but back off multiplier is unchanged.
164 EXPECT_EQ(1ul, pm->update_error_count_); 171 EXPECT_EQ(1ul, pm->update_error_count_);
165 EXPECT_EQ(1ul, pm->update_back_off_mult_); 172 EXPECT_EQ(1ul, pm->update_back_off_mult_);
166 EXPECT_TRUE(pm->IsUpdateScheduled()); 173 EXPECT_TRUE(pm->IsUpdateScheduled());
167 } 174 }
168 175
169 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingResponseCode) { 176 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingResponseCode) {
170 scoped_refptr<base::TestSimpleTaskRunner> runner( 177 scoped_refptr<base::TestSimpleTaskRunner> runner(
171 new base::TestSimpleTaskRunner()); 178 new base::TestSimpleTaskRunner());
172 base::ThreadTaskRunnerHandle runner_handler(runner); 179 base::ThreadTaskRunnerHandle runner_handler(runner);
173 net::TestURLFetcherFactory factory; 180 net::TestURLFetcherFactory factory;
174 const std::vector<ListUpdateResponse> expected_lurs; 181 const std::vector<ListUpdateResponse> expected_lurs;
175 const base::hash_map<UpdateListIdentifier, std::string> current_list_states; 182 const base::hash_map<UpdateListIdentifier, std::string> current_list_states;
176 scoped_ptr<V4UpdateProtocolManager> pm( 183 scoped_ptr<V4UpdateProtocolManager> pm(
177 CreateProtocolManager(current_list_states, expected_lurs)); 184 CreateProtocolManager(current_list_states, expected_lurs));
178 runner->ClearPendingTasks(); 185 runner->ClearPendingTasks();
179 186
187 // Initial state. No errors.
188 EXPECT_EQ(0ul, pm->update_error_count_);
189 EXPECT_EQ(1ul, pm->update_back_off_mult_);
190 expect_callback_to_be_called_ = false;
180 pm->IssueUpdateRequest(); 191 pm->IssueUpdateRequest();
192
181 EXPECT_FALSE(pm->IsUpdateScheduled()); 193 EXPECT_FALSE(pm->IsUpdateScheduled());
182 194
183 runner->RunPendingTasks(); 195 runner->RunPendingTasks();
184 196
185 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 197 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
186 DCHECK(fetcher); 198 DCHECK(fetcher);
187 fetcher->set_status(net::URLRequestStatus()); 199 fetcher->set_status(net::URLRequestStatus());
188 // Response code of anything other than 200 should result in error. 200 // Response code of anything other than 200 should result in error.
189 fetcher->set_response_code(net::HTTP_NO_CONTENT); 201 fetcher->set_response_code(net::HTTP_NO_CONTENT);
190 fetcher->SetResponseString(""); 202 fetcher->SetResponseString("");
(...skipping 11 matching lines...) Expand all
202 base::ThreadTaskRunnerHandle runner_handler(runner); 214 base::ThreadTaskRunnerHandle runner_handler(runner);
203 net::TestURLFetcherFactory factory; 215 net::TestURLFetcherFactory factory;
204 std::vector<ListUpdateResponse> expected_lurs; 216 std::vector<ListUpdateResponse> expected_lurs;
205 SetupExpectedListUpdateResponse(&expected_lurs); 217 SetupExpectedListUpdateResponse(&expected_lurs);
206 base::hash_map<UpdateListIdentifier, std::string> current_list_states; 218 base::hash_map<UpdateListIdentifier, std::string> current_list_states;
207 SetupCurrentListStates(&current_list_states); 219 SetupCurrentListStates(&current_list_states);
208 scoped_ptr<V4UpdateProtocolManager> pm( 220 scoped_ptr<V4UpdateProtocolManager> pm(
209 CreateProtocolManager(current_list_states, expected_lurs)); 221 CreateProtocolManager(current_list_states, expected_lurs));
210 runner->ClearPendingTasks(); 222 runner->ClearPendingTasks();
211 223
224 // Initial state. No errors.
225 EXPECT_EQ(0ul, pm->update_error_count_);
226 EXPECT_EQ(1ul, pm->update_back_off_mult_);
227 expect_callback_to_be_called_ = true;
212 pm->IssueUpdateRequest(); 228 pm->IssueUpdateRequest();
229
213 EXPECT_FALSE(pm->IsUpdateScheduled()); 230 EXPECT_FALSE(pm->IsUpdateScheduled());
214 231
215 runner->RunPendingTasks(); 232 runner->RunPendingTasks();
216 233
217 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 234 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
218 DCHECK(fetcher); 235 DCHECK(fetcher);
219 fetcher->set_status(net::URLRequestStatus()); 236 fetcher->set_status(net::URLRequestStatus());
220 fetcher->set_response_code(net::HTTP_OK); 237 fetcher->set_response_code(net::HTTP_OK);
221 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs)); 238 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs));
222 fetcher->delegate()->OnURLFetchComplete(fetcher); 239 fetcher->delegate()->OnURLFetchComplete(fetcher);
223 240
224 // No error, back off multiplier is unchanged. 241 // No error, back off multiplier is unchanged.
225 EXPECT_EQ(0ul, pm->update_error_count_); 242 EXPECT_EQ(0ul, pm->update_error_count_);
226 EXPECT_EQ(1ul, pm->update_back_off_mult_); 243 EXPECT_EQ(1ul, pm->update_back_off_mult_);
227 EXPECT_TRUE(pm->IsUpdateScheduled()); 244 EXPECT_TRUE(pm->IsUpdateScheduled());
228 } 245 }
229 246
247 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesWithOneBackoff) {
248 scoped_refptr<base::TestSimpleTaskRunner> runner(
249 new base::TestSimpleTaskRunner());
250 base::ThreadTaskRunnerHandle runner_handler(runner);
251 net::TestURLFetcherFactory factory;
252 std::vector<ListUpdateResponse> expected_lurs;
253 SetupExpectedListUpdateResponse(&expected_lurs);
254 base::hash_map<UpdateListIdentifier, std::string> current_list_states;
255 SetupCurrentListStates(&current_list_states);
256 scoped_ptr<V4UpdateProtocolManager> pm(
257 CreateProtocolManager(current_list_states, expected_lurs));
258 runner->ClearPendingTasks();
259
260 // Initial state. No errors.
261 EXPECT_EQ(0ul, pm->update_error_count_);
262 EXPECT_EQ(1ul, pm->update_back_off_mult_);
263 expect_callback_to_be_called_ = false;
264 pm->IssueUpdateRequest();
265
266 EXPECT_FALSE(pm->IsUpdateScheduled());
267
268 runner->RunPendingTasks();
269
270 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
271 DCHECK(fetcher);
272 fetcher->set_status(net::URLRequestStatus());
273 // Response code of anything other than 200 should result in error.
274 fetcher->set_response_code(net::HTTP_NO_CONTENT);
275 fetcher->SetResponseString("");
276 fetcher->delegate()->OnURLFetchComplete(fetcher);
277
278 // Should have recorded one error, but back off multiplier is unchanged.
279 EXPECT_EQ(1ul, pm->update_error_count_);
280 EXPECT_EQ(1ul, pm->update_back_off_mult_);
281 EXPECT_TRUE(pm->IsUpdateScheduled());
282
283 // Retry, now no backoff.
284 expect_callback_to_be_called_ = true;
285 runner->RunPendingTasks();
286
287 fetcher = factory.GetFetcherByID(1);
288 DCHECK(fetcher);
289 fetcher->set_status(net::URLRequestStatus());
290 fetcher->set_response_code(net::HTTP_OK);
291 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs));
292 fetcher->delegate()->OnURLFetchComplete(fetcher);
293
294 // No error, back off multiplier is unchanged.
295 EXPECT_EQ(0ul, pm->update_error_count_);
296 EXPECT_EQ(1ul, pm->update_back_off_mult_);
297 EXPECT_TRUE(pm->IsUpdateScheduled());
298 }
299
230 } // namespace safe_browsing 300 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « components/safe_browsing_db/v4_update_protocol_manager.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698