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

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

Issue 1848973004: Makes V4UpdateProtocolManager auto-schedule update fetching (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@v4_01_
Patch Set: Delete v4_update_protocol_manager_ on IO thread stop. And fix BUILD.gn 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.cc ('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"
11 #include "base/thread_task_runner_handle.h"
10 #include "base/time/time.h" 12 #include "base/time/time.h"
11 #include "components/safe_browsing_db/safebrowsing.pb.h" 13 #include "components/safe_browsing_db/safebrowsing.pb.h"
12 #include "components/safe_browsing_db/util.h" 14 #include "components/safe_browsing_db/util.h"
13 #include "components/safe_browsing_db/v4_update_protocol_manager.h" 15 #include "components/safe_browsing_db/v4_update_protocol_manager.h"
14 #include "net/base/escape.h" 16 #include "net/base/escape.h"
15 #include "net/base/load_flags.h" 17 #include "net/base/load_flags.h"
16 #include "net/base/net_errors.h" 18 #include "net/base/net_errors.h"
17 #include "net/url_request/test_url_fetcher_factory.h" 19 #include "net/url_request/test_url_fetcher_factory.h"
18 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
19 21
20 using base::Time; 22 using base::Time;
21 using base::TimeDelta; 23 using base::TimeDelta;
22 24
23 namespace { 25 namespace {
24 26
25 const char kClient[] = "unittest"; 27 const char kClient[] = "unittest";
26 const char kAppVer[] = "1.0"; 28 const char kAppVer[] = "1.0";
27 const char kKeyParam[] = "test_key_param"; 29 const char kKeyParam[] = "test_key_param";
28 30
29 } // namespace 31 } // namespace
30 32
31 namespace safe_browsing { 33 namespace safe_browsing {
32 34
33 typedef V4UpdateProtocolManager::ListUpdateRequest ListUpdateRequest;
34 typedef V4UpdateProtocolManager::ListUpdateResponse ListUpdateResponse;
35
36 class V4UpdateProtocolManagerTest : public testing::Test { 35 class V4UpdateProtocolManagerTest : public testing::Test {
37 protected: 36 protected:
38 scoped_ptr<V4UpdateProtocolManager> CreateProtocolManager() { 37 static void ValidateGetUpdatesResults(
38 const std::vector<ListUpdateResponse>& expected_lurs,
39 const std::vector<ListUpdateResponse>& list_update_responses) {
40 ASSERT_EQ(expected_lurs.size(), list_update_responses.size());
41
42 for (unsigned int i = 0; i < list_update_responses.size(); ++i) {
43 const ListUpdateResponse& expected = expected_lurs[i];
44 const ListUpdateResponse& actual = list_update_responses[i];
45
46 EXPECT_EQ(expected.platform_type(), actual.platform_type());
47 EXPECT_EQ(expected.response_type(), actual.response_type());
48 EXPECT_EQ(expected.threat_entry_type(), actual.threat_entry_type());
49 EXPECT_EQ(expected.threat_type(), actual.threat_type());
50 EXPECT_EQ(expected.new_client_state(), actual.new_client_state());
51
52 // TODO(vakh): Test more fields from the proto.
53 }
54 }
55
56 scoped_ptr<V4UpdateProtocolManager> CreateProtocolManager(
57 const base::hash_map<UpdateListIdentifier, std::string>
58 current_list_states,
59 const std::vector<ListUpdateResponse>& expected_lurs) {
39 V4ProtocolConfig config; 60 V4ProtocolConfig config;
40 config.client_name = kClient; 61 config.client_name = kClient;
41 config.version = kAppVer; 62 config.version = kAppVer;
42 config.key_param = kKeyParam; 63 config.key_param = kKeyParam;
43 return scoped_ptr<V4UpdateProtocolManager>( 64 config.disable_auto_update = false;
44 V4UpdateProtocolManager::Create(NULL, config)); 65 return scoped_ptr<V4UpdateProtocolManager>(V4UpdateProtocolManager::Create(
Nathan Parker 2016/04/01 17:56:58 the Create() method already returns a scoped_ptr
vakh (use Gerrit instead) 2016/04/01 19:35:48 Done. Good catch.
66 NULL, config, current_list_states,
67 base::Bind(ValidateGetUpdatesResults, expected_lurs)));
45 } 68 }
46 69
47 void SetupListsToUpdate( 70 void SetupCurrentListStates(
48 base::hash_set<UpdateListIdentifier>* lists_to_update) { 71 base::hash_map<UpdateListIdentifier, std::string>* current_list_states) {
49 UpdateListIdentifier list_identifier; 72 UpdateListIdentifier list_identifier;
50 list_identifier.platform_type = WINDOWS_PLATFORM; 73 list_identifier.platform_type = WINDOWS_PLATFORM;
51 list_identifier.threat_entry_type = URL_EXPRESSION; 74 list_identifier.threat_entry_type = URL_EXPRESSION;
52 list_identifier.threat_type = MALWARE_THREAT; 75 list_identifier.threat_type = MALWARE_THREAT;
53 lists_to_update->insert(list_identifier); 76 current_list_states->insert({list_identifier, "initial_state_1"});
54 77
55 list_identifier.platform_type = WINDOWS_PLATFORM; 78 list_identifier.platform_type = WINDOWS_PLATFORM;
56 list_identifier.threat_entry_type = URL_EXPRESSION; 79 list_identifier.threat_entry_type = URL_EXPRESSION;
57 list_identifier.threat_type = UNWANTED_SOFTWARE; 80 list_identifier.threat_type = UNWANTED_SOFTWARE;
58 lists_to_update->insert(list_identifier); 81 current_list_states->insert({list_identifier, "initial_state_2"});
59 82
60 list_identifier.platform_type = WINDOWS_PLATFORM; 83 list_identifier.platform_type = WINDOWS_PLATFORM;
61 list_identifier.threat_entry_type = BINARY_DIGEST; 84 list_identifier.threat_entry_type = BINARY_DIGEST;
62 list_identifier.threat_type = MALWARE_THREAT; 85 list_identifier.threat_type = MALWARE_THREAT;
63 lists_to_update->insert(list_identifier); 86 current_list_states->insert({list_identifier, "initial_state_3"});
64 } 87 }
65 88
66 void ClearListsToUpdate( 89 void SetupExpectedListUpdateResponse(
67 base::hash_set<UpdateListIdentifier>* lists_to_update) { 90 std::vector<ListUpdateResponse>* expected_lurs) {
68 lists_to_update->clear(); 91 ListUpdateResponse lur;
92 lur.set_platform_type(WINDOWS_PLATFORM);
93 lur.set_response_type(ListUpdateResponse::PARTIAL_UPDATE);
94 lur.set_threat_entry_type(URL_EXPRESSION);
95 lur.set_threat_type(MALWARE_THREAT);
96 lur.set_new_client_state("new_state_1");
97 expected_lurs->push_back(lur);
98
99 lur.set_platform_type(WINDOWS_PLATFORM);
100 lur.set_response_type(ListUpdateResponse::PARTIAL_UPDATE);
101 lur.set_threat_entry_type(URL_EXPRESSION);
102 lur.set_threat_type(UNWANTED_SOFTWARE);
103 lur.set_new_client_state("new_state_2");
104 expected_lurs->push_back(lur);
105
106 lur.set_platform_type(WINDOWS_PLATFORM);
107 lur.set_response_type(ListUpdateResponse::FULL_UPDATE);
108 lur.set_threat_entry_type(BINARY_DIGEST);
109 lur.set_threat_type(MALWARE_THREAT);
110 lur.set_new_client_state("new_state_3");
111 expected_lurs->push_back(lur);
69 } 112 }
70 113
71 void SetupCurrentListStates( 114 std::string GetExpectedV4UpdateResponse(
72 const base::hash_set<UpdateListIdentifier>& lists_to_update, 115 std::vector<ListUpdateResponse>& expected_lurs) const {
73 base::hash_map<UpdateListIdentifier, std::string>* current_list_states) {
74 // TODO(vakh): Implement this to test the cases when we have an existing
75 // state for some of the lists.
76 }
77
78 std::string GetStockV4UpdateResponse() {
79 FetchThreatListUpdatesResponse response; 116 FetchThreatListUpdatesResponse response;
80 117
81 ListUpdateResponse* lur = response.add_list_update_responses(); 118 for (const auto& expected_lur : expected_lurs) {
82 lur->set_platform_type(WINDOWS_PLATFORM); 119 ListUpdateResponse* lur = response.add_list_update_responses();
83 lur->set_response_type(ListUpdateResponse::PARTIAL_UPDATE); 120 lur->set_new_client_state(expected_lur.new_client_state());
84 lur->set_threat_entry_type(URL_EXPRESSION); 121 lur->set_platform_type(expected_lur.platform_type());
85 lur->set_threat_type(MALWARE_THREAT); 122 lur->set_response_type(expected_lur.response_type());
86 123 lur->set_threat_entry_type(expected_lur.threat_entry_type());
87 lur = response.add_list_update_responses(); 124 lur->set_threat_type(expected_lur.threat_type());
88 lur->set_platform_type(WINDOWS_PLATFORM); 125 }
89 lur->set_response_type(ListUpdateResponse::PARTIAL_UPDATE);
90 lur->set_threat_entry_type(URL_EXPRESSION);
91 lur->set_threat_type(UNWANTED_SOFTWARE);
92
93 lur = response.add_list_update_responses();
94 lur->set_platform_type(WINDOWS_PLATFORM);
95 lur->set_response_type(ListUpdateResponse::FULL_UPDATE);
96 lur->set_threat_entry_type(BINARY_DIGEST);
97 lur->set_threat_type(MALWARE_THREAT);
98 126
99 // Serialize. 127 // Serialize.
100 std::string res_data; 128 std::string res_data;
101 response.SerializeToString(&res_data); 129 response.SerializeToString(&res_data);
102 130
103 return res_data; 131 return res_data;
104 } 132 }
105 }; 133 };
106 134
107 void ValidateGetUpdatesResults( 135 // TODO(vakh): Add many more tests.
108 const std::vector<ListUpdateResponse>& expected_lurs, 136 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingNetwork) {
109 const std::vector<ListUpdateResponse>& list_update_responses) { 137 scoped_refptr<base::TestSimpleTaskRunner> runner(
110 ASSERT_EQ(expected_lurs.size(), list_update_responses.size()); 138 new base::TestSimpleTaskRunner());
139 base::ThreadTaskRunnerHandle runner_handler(runner);
140 net::TestURLFetcherFactory factory;
141 const base::hash_map<UpdateListIdentifier, std::string> current_list_states;
142 const std::vector<ListUpdateResponse> expected_lurs;
143 scoped_ptr<V4UpdateProtocolManager> pm(
144 CreateProtocolManager(current_list_states, expected_lurs));
145 runner->ClearPendingTasks();
111 146
112 for (unsigned int i = 0; i < list_update_responses.size(); ++i) { 147 // Initial state. No errors.
113 const ListUpdateResponse& expected = expected_lurs[i]; 148 EXPECT_EQ(0ul, pm->update_error_count_);
114 const ListUpdateResponse& actual = list_update_responses[i]; 149 EXPECT_EQ(1ul, pm->update_back_off_mult_);
115 150
116 EXPECT_EQ(expected.platform_type(), actual.platform_type()); 151 pm->IssueUpdateRequest();
117 EXPECT_EQ(expected.response_type(), actual.response_type()); 152 EXPECT_FALSE(pm->IsUpdateScheduled());
118 EXPECT_EQ(expected.threat_entry_type(), actual.threat_entry_type());
119 EXPECT_EQ(expected.threat_type(), actual.threat_type());
120 153
121 // TODO(vakh): Test more fields from the proto. 154 runner->RunPendingTasks();
122 }
123 }
124
125 // TODO(vakh): Add many more tests.
126
127 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingNetwork) {
128 net::TestURLFetcherFactory factory;
129 scoped_ptr<V4UpdateProtocolManager> pm(CreateProtocolManager());
130
131 const std::vector<ListUpdateResponse> expected_lurs;
132 const base::hash_set<UpdateListIdentifier> lists_to_update;
133 const base::hash_map<UpdateListIdentifier, std::string> current_list_states;
134 pm->GetUpdates(lists_to_update, current_list_states,
135 base::Bind(&ValidateGetUpdatesResults, expected_lurs));
136 155
137 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 156 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
138 DCHECK(fetcher); 157 DCHECK(fetcher);
139 // Failed request status should result in error. 158 // Failed request status should result in error.
140 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, 159 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
141 net::ERR_CONNECTION_RESET)); 160 net::ERR_CONNECTION_RESET));
142 fetcher->delegate()->OnURLFetchComplete(fetcher); 161 fetcher->delegate()->OnURLFetchComplete(fetcher);
143 162
144 // Should have recorded one error, but back off multiplier is unchanged. 163 // Should have recorded one error, but back off multiplier is unchanged.
145 EXPECT_EQ(1ul, pm->update_error_count_); 164 EXPECT_EQ(1ul, pm->update_error_count_);
146 EXPECT_EQ(1ul, pm->update_back_off_mult_); 165 EXPECT_EQ(1ul, pm->update_back_off_mult_);
166 EXPECT_TRUE(pm->IsUpdateScheduled());
147 } 167 }
148 168
149 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingResponseCode) { 169 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingResponseCode) {
170 scoped_refptr<base::TestSimpleTaskRunner> runner(
Nathan Parker 2016/04/01 17:56:58 nit: You could make most of these part of the clas
vakh (use Gerrit instead) 2016/04/01 19:35:48 Not storing any state in the class keeps each of t
171 new base::TestSimpleTaskRunner());
172 base::ThreadTaskRunnerHandle runner_handler(runner);
150 net::TestURLFetcherFactory factory; 173 net::TestURLFetcherFactory factory;
151 scoped_ptr<V4UpdateProtocolManager> pm(CreateProtocolManager()); 174 const std::vector<ListUpdateResponse> expected_lurs;
175 const base::hash_map<UpdateListIdentifier, std::string> current_list_states;
176 scoped_ptr<V4UpdateProtocolManager> pm(
177 CreateProtocolManager(current_list_states, expected_lurs));
178 runner->ClearPendingTasks();
152 179
153 const std::vector<ListUpdateResponse> expected_lurs; 180 pm->IssueUpdateRequest();
154 const base::hash_set<UpdateListIdentifier> lists_to_update; 181 EXPECT_FALSE(pm->IsUpdateScheduled());
155 const base::hash_map<UpdateListIdentifier, std::string> current_list_states;
156 pm->GetUpdates(lists_to_update, current_list_states,
157 base::Bind(&ValidateGetUpdatesResults, expected_lurs));
158 182
183 runner->RunPendingTasks();
159 184
160 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 185 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
161 DCHECK(fetcher); 186 DCHECK(fetcher);
162 fetcher->set_status(net::URLRequestStatus()); 187 fetcher->set_status(net::URLRequestStatus());
163 // Response code of anything other than 200 should result in error. 188 // Response code of anything other than 200 should result in error.
164 fetcher->set_response_code(204); 189 fetcher->set_response_code(net::HTTP_NO_CONTENT);
165 fetcher->SetResponseString(GetStockV4UpdateResponse()); 190 fetcher->SetResponseString("");
166 fetcher->delegate()->OnURLFetchComplete(fetcher); 191 fetcher->delegate()->OnURLFetchComplete(fetcher);
167 192
168 // Should have recorded one error, but back off multiplier is unchanged. 193 // Should have recorded one error, but back off multiplier is unchanged.
169 EXPECT_EQ(1ul, pm->update_error_count_); 194 EXPECT_EQ(1ul, pm->update_error_count_);
170 EXPECT_EQ(1ul, pm->update_back_off_mult_); 195 EXPECT_EQ(1ul, pm->update_back_off_mult_);
196 EXPECT_TRUE(pm->IsUpdateScheduled());
171 } 197 }
172 198
173 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesNoError) { 199 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesNoError) {
200 scoped_refptr<base::TestSimpleTaskRunner> runner(
201 new base::TestSimpleTaskRunner());
202 base::ThreadTaskRunnerHandle runner_handler(runner);
174 net::TestURLFetcherFactory factory; 203 net::TestURLFetcherFactory factory;
175 scoped_ptr<V4UpdateProtocolManager> pm(CreateProtocolManager()); 204 std::vector<ListUpdateResponse> expected_lurs;
205 SetupExpectedListUpdateResponse(&expected_lurs);
206 base::hash_map<UpdateListIdentifier, std::string> current_list_states;
207 SetupCurrentListStates(&current_list_states);
208 scoped_ptr<V4UpdateProtocolManager> pm(
209 CreateProtocolManager(current_list_states, expected_lurs));
210 runner->ClearPendingTasks();
176 211
212 pm->IssueUpdateRequest();
213 EXPECT_FALSE(pm->IsUpdateScheduled());
177 214
178 const std::vector<ListUpdateResponse> expected_lurs; 215 runner->RunPendingTasks();
179 base::hash_set<UpdateListIdentifier> lists_to_update;
180 SetupListsToUpdate(&lists_to_update);
181 base::hash_map<UpdateListIdentifier, std::string> current_list_states;
182 SetupCurrentListStates(lists_to_update, &current_list_states);
183 pm->GetUpdates(lists_to_update, current_list_states,
184 base::Bind(&ValidateGetUpdatesResults, expected_lurs));
185 ClearListsToUpdate(&lists_to_update);
186 216
187 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 217 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
188 DCHECK(fetcher); 218 DCHECK(fetcher);
189 fetcher->set_status(net::URLRequestStatus()); 219 fetcher->set_status(net::URLRequestStatus());
190 fetcher->set_response_code(200); 220 fetcher->set_response_code(net::HTTP_OK);
191 fetcher->SetResponseString(GetStockV4UpdateResponse()); 221 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs));
192 fetcher->delegate()->OnURLFetchComplete(fetcher); 222 fetcher->delegate()->OnURLFetchComplete(fetcher);
193 223
194 // No error, back off multiplier is unchanged. 224 // No error, back off multiplier is unchanged.
195 EXPECT_EQ(0ul, pm->update_error_count_); 225 EXPECT_EQ(0ul, pm->update_error_count_);
196 EXPECT_EQ(1ul, pm->update_back_off_mult_); 226 EXPECT_EQ(1ul, pm->update_back_off_mult_);
227 EXPECT_TRUE(pm->IsUpdateScheduled());
197 } 228 }
198 229
199 } // namespace safe_browsing 230 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « components/safe_browsing_db/v4_update_protocol_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698