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

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: 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 scoped_ptr<V4UpdateProtocolManager>(V4UpdateProtocolManager::Create( 68 return scoped_ptr<V4UpdateProtocolManager>(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 expect_callback_to_be_called_ = false;
148 EXPECT_EQ(0ul, pm->update_error_count_); 154 pm->IssueUpdateRequest();
149 EXPECT_EQ(1ul, pm->update_back_off_mult_);
150 155
151 pm->IssueUpdateRequest();
152 EXPECT_FALSE(pm->IsUpdateScheduled()); 156 EXPECT_FALSE(pm->IsUpdateScheduled());
153 157
154 runner->RunPendingTasks(); 158 runner->RunPendingTasks();
155 159
156 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 160 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
157 DCHECK(fetcher); 161 DCHECK(fetcher);
158 // Failed request status should result in error. 162 // Failed request status should result in error.
159 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, 163 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
160 net::ERR_CONNECTION_RESET)); 164 net::ERR_CONNECTION_RESET));
161 fetcher->delegate()->OnURLFetchComplete(fetcher); 165 fetcher->delegate()->OnURLFetchComplete(fetcher);
162 166
163 // Should have recorded one error, but back off multiplier is unchanged.
164 EXPECT_EQ(1ul, pm->update_error_count_);
165 EXPECT_EQ(1ul, pm->update_back_off_mult_);
166 EXPECT_TRUE(pm->IsUpdateScheduled()); 167 EXPECT_TRUE(pm->IsUpdateScheduled());
167 } 168 }
168 169
169 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingResponseCode) { 170 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingResponseCode) {
170 scoped_refptr<base::TestSimpleTaskRunner> runner( 171 scoped_refptr<base::TestSimpleTaskRunner> runner(
171 new base::TestSimpleTaskRunner()); 172 new base::TestSimpleTaskRunner());
172 base::ThreadTaskRunnerHandle runner_handler(runner); 173 base::ThreadTaskRunnerHandle runner_handler(runner);
173 net::TestURLFetcherFactory factory; 174 net::TestURLFetcherFactory factory;
174 const std::vector<ListUpdateResponse> expected_lurs; 175 const std::vector<ListUpdateResponse> expected_lurs;
175 const base::hash_map<UpdateListIdentifier, std::string> current_list_states; 176 const base::hash_map<UpdateListIdentifier, std::string> current_list_states;
176 scoped_ptr<V4UpdateProtocolManager> pm( 177 scoped_ptr<V4UpdateProtocolManager> pm(
177 CreateProtocolManager(current_list_states, expected_lurs)); 178 CreateProtocolManager(current_list_states, expected_lurs));
178 runner->ClearPendingTasks(); 179 runner->ClearPendingTasks();
179 180
181 expect_callback_to_be_called_ = false;
180 pm->IssueUpdateRequest(); 182 pm->IssueUpdateRequest();
183
181 EXPECT_FALSE(pm->IsUpdateScheduled()); 184 EXPECT_FALSE(pm->IsUpdateScheduled());
182 185
183 runner->RunPendingTasks(); 186 runner->RunPendingTasks();
184 187
185 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 188 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
186 DCHECK(fetcher); 189 DCHECK(fetcher);
187 fetcher->set_status(net::URLRequestStatus()); 190 fetcher->set_status(net::URLRequestStatus());
188 // Response code of anything other than 200 should result in error. 191 // Response code of anything other than 200 should result in error.
189 fetcher->set_response_code(net::HTTP_NO_CONTENT); 192 fetcher->set_response_code(net::HTTP_NO_CONTENT);
190 fetcher->SetResponseString(""); 193 fetcher->SetResponseString("");
191 fetcher->delegate()->OnURLFetchComplete(fetcher); 194 fetcher->delegate()->OnURLFetchComplete(fetcher);
192 195
193 // Should have recorded one error, but back off multiplier is unchanged.
194 EXPECT_EQ(1ul, pm->update_error_count_);
195 EXPECT_EQ(1ul, pm->update_back_off_mult_);
196 EXPECT_TRUE(pm->IsUpdateScheduled()); 196 EXPECT_TRUE(pm->IsUpdateScheduled());
197 } 197 }
198 198
199 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesNoError) { 199 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesNoError) {
200 scoped_refptr<base::TestSimpleTaskRunner> runner( 200 scoped_refptr<base::TestSimpleTaskRunner> runner(
201 new base::TestSimpleTaskRunner()); 201 new base::TestSimpleTaskRunner());
202 base::ThreadTaskRunnerHandle runner_handler(runner); 202 base::ThreadTaskRunnerHandle runner_handler(runner);
203 net::TestURLFetcherFactory factory; 203 net::TestURLFetcherFactory factory;
204 std::vector<ListUpdateResponse> expected_lurs; 204 std::vector<ListUpdateResponse> expected_lurs;
205 SetupExpectedListUpdateResponse(&expected_lurs); 205 SetupExpectedListUpdateResponse(&expected_lurs);
206 base::hash_map<UpdateListIdentifier, std::string> current_list_states; 206 base::hash_map<UpdateListIdentifier, std::string> current_list_states;
207 SetupCurrentListStates(&current_list_states); 207 SetupCurrentListStates(&current_list_states);
208 scoped_ptr<V4UpdateProtocolManager> pm( 208 scoped_ptr<V4UpdateProtocolManager> pm(
209 CreateProtocolManager(current_list_states, expected_lurs)); 209 CreateProtocolManager(current_list_states, expected_lurs));
210 runner->ClearPendingTasks(); 210 runner->ClearPendingTasks();
211 211
212 expect_callback_to_be_called_ = true;
212 pm->IssueUpdateRequest(); 213 pm->IssueUpdateRequest();
214
213 EXPECT_FALSE(pm->IsUpdateScheduled()); 215 EXPECT_FALSE(pm->IsUpdateScheduled());
214 216
215 runner->RunPendingTasks(); 217 runner->RunPendingTasks();
218
219 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
220 DCHECK(fetcher);
221 fetcher->set_status(net::URLRequestStatus());
222 fetcher->set_response_code(net::HTTP_OK);
223 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs));
224 fetcher->delegate()->OnURLFetchComplete(fetcher);
225
226 EXPECT_TRUE(pm->IsUpdateScheduled());
227 }
228
229 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesWithOneBackoff) {
230 scoped_refptr<base::TestSimpleTaskRunner> runner(
231 new base::TestSimpleTaskRunner());
232 base::ThreadTaskRunnerHandle runner_handler(runner);
233 net::TestURLFetcherFactory factory;
234 std::vector<ListUpdateResponse> expected_lurs;
235 SetupExpectedListUpdateResponse(&expected_lurs);
236 base::hash_map<UpdateListIdentifier, std::string> current_list_states;
237 SetupCurrentListStates(&current_list_states);
238 scoped_ptr<V4UpdateProtocolManager> pm(
239 CreateProtocolManager(current_list_states, expected_lurs));
240 runner->ClearPendingTasks();
241
242 expect_callback_to_be_called_ = false;
243 pm->IssueUpdateRequest();
244
245 EXPECT_FALSE(pm->IsUpdateScheduled());
246
247 runner->RunPendingTasks();
216 248
217 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 249 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
218 DCHECK(fetcher); 250 DCHECK(fetcher);
219 fetcher->set_status(net::URLRequestStatus()); 251 fetcher->set_status(net::URLRequestStatus());
252 // Response code of anything other than 200 should result in error.
253 fetcher->set_response_code(net::HTTP_NO_CONTENT);
254 fetcher->SetResponseString("");
255 fetcher->delegate()->OnURLFetchComplete(fetcher);
256
257 EXPECT_TRUE(pm->IsUpdateScheduled());
258
259 // Retry, now no backoff.
260 expect_callback_to_be_called_ = true;
261 runner->RunPendingTasks();
262
263 fetcher = factory.GetFetcherByID(1);
264 DCHECK(fetcher);
265 fetcher->set_status(net::URLRequestStatus());
220 fetcher->set_response_code(net::HTTP_OK); 266 fetcher->set_response_code(net::HTTP_OK);
221 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs)); 267 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs));
222 fetcher->delegate()->OnURLFetchComplete(fetcher); 268 fetcher->delegate()->OnURLFetchComplete(fetcher);
223 269
224 // No error, back off multiplier is unchanged.
225 EXPECT_EQ(0ul, pm->update_error_count_);
226 EXPECT_EQ(1ul, pm->update_back_off_mult_);
227 EXPECT_TRUE(pm->IsUpdateScheduled()); 270 EXPECT_TRUE(pm->IsUpdateScheduled());
Nathan Parker 2016/04/01 23:10:10 Why remove the checks of pm->update_*
vakh (use Gerrit instead) 2016/04/01 23:13:23 That code is covered by https://code.google.com/p/
228 } 271 }
229 272
230 } // namespace safe_browsing 273 } // 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