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

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

Issue 1727033003: v4_update_protocol_manager: Basic implementation with TODOs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Delete some objects allocated for tests Created 4 years, 10 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include <vector>
6
7 #include "base/base64.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/time/time.h"
11 #include "components/safe_browsing_db/safebrowsing.pb.h"
12 #include "components/safe_browsing_db/util.h"
13 #include "components/safe_browsing_db/v4_update_protocol_manager.h"
14 #include "net/base/escape.h"
15 #include "net/base/load_flags.h"
16 #include "net/base/net_errors.h"
17 #include "net/url_request/test_url_fetcher_factory.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 using base::Time;
21 using base::TimeDelta;
22
23 namespace {
24
25 const char kClient[] = "unittest";
26 const char kAppVer[] = "1.0";
27 const char kKeyParam[] = "test_key_param";
28
29 } // namespace
30
31 namespace safe_browsing {
32
33 typedef V4UpdateProtocolManager::ListUpdateRequest ListUpdateRequest;
34 typedef V4UpdateProtocolManager::ListUpdateResponse ListUpdateResponse;
35
36 class SafeBrowsingV4UpdateProtocolManagerTest : public testing::Test {
37 protected:
38 scoped_ptr<V4UpdateProtocolManager> CreateProtocolManager() {
39 V4ProtocolConfig config;
40 config.client_name = kClient;
41 config.version = kAppVer;
42 config.key_param = kKeyParam;
43 return scoped_ptr<V4UpdateProtocolManager>(
44 V4UpdateProtocolManager::Create(NULL, config));
45 }
46
47 void SetupListsToUpdate(
48 base::hash_set<const UpdateListIdentifier*>* lists_to_update) {
49 UpdateListIdentifier* list_identifier = new UpdateListIdentifier();
50 list_identifier->platform_type = WINDOWS_PLATFORM;
51 list_identifier->threat_entry_type = URL_EXPRESSION;
52 list_identifier->threat_type = MALWARE_THREAT;
53 lists_to_update->insert(list_identifier);
54
55 list_identifier = new UpdateListIdentifier();
56 list_identifier->platform_type = WINDOWS_PLATFORM;
57 list_identifier->threat_entry_type = URL_EXPRESSION;
58 list_identifier->threat_type = UNWANTED_SOFTWARE;
59 lists_to_update->insert(list_identifier);
60
61 list_identifier = new UpdateListIdentifier();
62 list_identifier->platform_type = WINDOWS_PLATFORM;
63 list_identifier->threat_entry_type = BINARY_DIGEST;
64 list_identifier->threat_type = MALWARE_THREAT;
65 lists_to_update->insert(list_identifier);
66 }
67
68 void ClearListsToUpdate(
69 base::hash_set<const UpdateListIdentifier*>* lists_to_update) {
70 for (const UpdateListIdentifier* id : *lists_to_update) {
71 delete id;
72 }
73 lists_to_update->clear();
74 }
75
76 void SetupCurrentListStates(
77 const base::hash_set<const UpdateListIdentifier*>& lists_to_update,
78 base::hash_map<const UpdateListIdentifier*, const std::string&>*
79 current_list_states) {
80 // TODO(vakh): Implement this to test the cases when we have an existing
81 // state for some of the lists.
82 }
83
84 std::string GetStockV4UpdateResponse() {
85 FetchThreatListUpdatesResponse response;
86
87 ListUpdateResponse* lur = response.add_list_update_responses();
88 lur->set_platform_type(WINDOWS_PLATFORM);
89 lur->set_response_type(ListUpdateResponse::PARTIAL_UPDATE);
90 lur->set_threat_entry_type(URL_EXPRESSION);
91 lur->set_threat_type(MALWARE_THREAT);
92
93 lur = response.add_list_update_responses();
94 lur->set_platform_type(WINDOWS_PLATFORM);
95 lur->set_response_type(ListUpdateResponse::PARTIAL_UPDATE);
96 lur->set_threat_entry_type(URL_EXPRESSION);
97 lur->set_threat_type(UNWANTED_SOFTWARE);
98
99 lur = response.add_list_update_responses();
100 lur->set_platform_type(WINDOWS_PLATFORM);
101 lur->set_response_type(ListUpdateResponse::FULL_UPDATE);
102 lur->set_threat_entry_type(BINARY_DIGEST);
103 lur->set_threat_type(MALWARE_THREAT);
104
105 // Serialize.
106 std::string res_data;
107 response.SerializeToString(&res_data);
108
109 return res_data;
110 }
111 };
112
113 void ValidateGetUpdatesResults(
114 const std::vector<ListUpdateResponse>& expected_lurs,
115 const std::vector<ListUpdateResponse>& list_update_responses) {
116 ASSERT_EQ(expected_lurs.size(), list_update_responses.size());
117
118 for (unsigned int i = 0; i < list_update_responses.size(); ++i) {
119 const ListUpdateResponse& expected = expected_lurs[i];
120 const ListUpdateResponse& actual = list_update_responses[i];
121
122 EXPECT_EQ(expected.platform_type(), actual.platform_type());
123 EXPECT_EQ(expected.response_type(), actual.response_type());
124 EXPECT_EQ(expected.threat_entry_type(), actual.threat_entry_type());
125 EXPECT_EQ(expected.threat_type(), actual.threat_type());
126
127 // TODO(vakh): Test more fields from the proto.
128 }
129 }
130
131 // TODO(vakh): Add many more tests.
132
133 TEST_F(SafeBrowsingV4UpdateProtocolManagerTest,
134 TestGetUpdatesErrorHandlingNetwork) {
135 net::TestURLFetcherFactory factory;
136 scoped_ptr<V4UpdateProtocolManager> pm(CreateProtocolManager());
137
138 const std::vector<ListUpdateResponse> expected_lurs;
139 const base::hash_set<const UpdateListIdentifier*> lists_to_update;
140 const base::hash_map<const UpdateListIdentifier*, const std::string&>
141 current_list_states;
142 pm->GetUpdates(lists_to_update, current_list_states,
143 base::Bind(&ValidateGetUpdatesResults, expected_lurs));
144
145 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
146 DCHECK(fetcher);
147 // Failed request status should result in error.
148 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
149 net::ERR_CONNECTION_RESET));
150 fetcher->set_response_code(200);
151 fetcher->SetResponseString(GetStockV4UpdateResponse());
152 fetcher->delegate()->OnURLFetchComplete(fetcher);
153
154 // Should have recorded one error, but back off multiplier is unchanged.
155 EXPECT_EQ(1ul, pm->update_error_count_);
156 EXPECT_EQ(1ul, pm->update_back_off_mult_);
157 }
158
159 TEST_F(SafeBrowsingV4UpdateProtocolManagerTest,
160 TestGetUpdatesErrorHandlingResponseCode) {
161 net::TestURLFetcherFactory factory;
162 scoped_ptr<V4UpdateProtocolManager> pm(CreateProtocolManager());
163
164 const std::vector<ListUpdateResponse> expected_lurs;
165 const base::hash_set<const UpdateListIdentifier*> lists_to_update;
166 const base::hash_map<const UpdateListIdentifier*, const std::string&>
167 current_list_states;
168 pm->GetUpdates(lists_to_update, current_list_states,
169 base::Bind(&ValidateGetUpdatesResults, expected_lurs));
170
171
172 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
173 DCHECK(fetcher);
174 fetcher->set_status(net::URLRequestStatus());
175 // Response code of anything other than 200 should result in error.
176 fetcher->set_response_code(204);
177 fetcher->SetResponseString(GetStockV4UpdateResponse());
178 fetcher->delegate()->OnURLFetchComplete(fetcher);
179
180 // Should have recorded one error, but back off multiplier is unchanged.
181 EXPECT_EQ(1ul, pm->update_error_count_);
182 EXPECT_EQ(1ul, pm->update_back_off_mult_);
183 }
184
185 TEST_F(SafeBrowsingV4UpdateProtocolManagerTest, TestGetUpdatesNoError) {
186 net::TestURLFetcherFactory factory;
187 scoped_ptr<V4UpdateProtocolManager> pm(CreateProtocolManager());
188
189
190 const std::vector<ListUpdateResponse> expected_lurs;
191 base::hash_set<const UpdateListIdentifier*> lists_to_update;
192 SetupListsToUpdate(&lists_to_update);
193 base::hash_map<const UpdateListIdentifier*, const std::string&>
194 current_list_states;
195 SetupCurrentListStates(lists_to_update, &current_list_states);
196 pm->GetUpdates(lists_to_update, current_list_states,
197 base::Bind(&ValidateGetUpdatesResults, expected_lurs));
198 ClearListsToUpdate(&lists_to_update);
199
200 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
201 DCHECK(fetcher);
202 fetcher->set_status(net::URLRequestStatus());
203 fetcher->set_response_code(200);
204 fetcher->SetResponseString(GetStockV4UpdateResponse());
205 fetcher->delegate()->OnURLFetchComplete(fetcher);
206
207 // No error, back off multiplier is unchanged.
208 EXPECT_EQ(0ul, pm->update_error_count_);
209 EXPECT_EQ(1ul, pm->update_back_off_mult_);
210 }
211
212 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698