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

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: Incorporate review feedback. Created 4 years, 9 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 V4UpdateProtocolManagerTest : 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<UpdateListIdentifier>* lists_to_update) {
49 UpdateListIdentifier list_identifier;
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.platform_type = WINDOWS_PLATFORM;
56 list_identifier.threat_entry_type = URL_EXPRESSION;
57 list_identifier.threat_type = UNWANTED_SOFTWARE;
58 lists_to_update->insert(list_identifier);
59
60 list_identifier.platform_type = WINDOWS_PLATFORM;
61 list_identifier.threat_entry_type = BINARY_DIGEST;
62 list_identifier.threat_type = MALWARE_THREAT;
63 lists_to_update->insert(list_identifier);
64 }
65
66 void ClearListsToUpdate(
67 base::hash_set<UpdateListIdentifier>* lists_to_update) {
68 lists_to_update->clear();
69 }
70
71 void SetupCurrentListStates(
72 const base::hash_set<UpdateListIdentifier>& lists_to_update,
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;
80
81 ListUpdateResponse* lur = response.add_list_update_responses();
82 lur->set_platform_type(WINDOWS_PLATFORM);
83 lur->set_response_type(ListUpdateResponse::PARTIAL_UPDATE);
84 lur->set_threat_entry_type(URL_EXPRESSION);
85 lur->set_threat_type(MALWARE_THREAT);
86
87 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(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
99 // Serialize.
100 std::string res_data;
101 response.SerializeToString(&res_data);
102
103 return res_data;
104 }
105 };
106
107 void ValidateGetUpdatesResults(
108 const std::vector<ListUpdateResponse>& expected_lurs,
109 const std::vector<ListUpdateResponse>& list_update_responses) {
110 ASSERT_EQ(expected_lurs.size(), list_update_responses.size());
111
112 for (unsigned int i = 0; i < list_update_responses.size(); ++i) {
113 const ListUpdateResponse& expected = expected_lurs[i];
114 const ListUpdateResponse& actual = list_update_responses[i];
115
116 EXPECT_EQ(expected.platform_type(), actual.platform_type());
117 EXPECT_EQ(expected.response_type(), actual.response_type());
118 EXPECT_EQ(expected.threat_entry_type(), actual.threat_entry_type());
119 EXPECT_EQ(expected.threat_type(), actual.threat_type());
120
121 // TODO(vakh): Test more fields from the proto.
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
137 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
138 DCHECK(fetcher);
139 // Failed request status should result in error.
140 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
141 net::ERR_CONNECTION_RESET));
142 fetcher->delegate()->OnURLFetchComplete(fetcher);
143
144 // Should have recorded one error, but back off multiplier is unchanged.
145 EXPECT_EQ(1ul, pm->update_error_count_);
146 EXPECT_EQ(1ul, pm->update_back_off_mult_);
147 }
148
149 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingResponseCode) {
150 net::TestURLFetcherFactory factory;
151 scoped_ptr<V4UpdateProtocolManager> pm(CreateProtocolManager());
152
153 const std::vector<ListUpdateResponse> expected_lurs;
154 const base::hash_set<UpdateListIdentifier> lists_to_update;
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
159
160 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
161 DCHECK(fetcher);
162 fetcher->set_status(net::URLRequestStatus());
163 // Response code of anything other than 200 should result in error.
164 fetcher->set_response_code(204);
165 fetcher->SetResponseString(GetStockV4UpdateResponse());
166 fetcher->delegate()->OnURLFetchComplete(fetcher);
167
168 // Should have recorded one error, but back off multiplier is unchanged.
169 EXPECT_EQ(1ul, pm->update_error_count_);
170 EXPECT_EQ(1ul, pm->update_back_off_mult_);
171 }
172
173 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesNoError) {
174 net::TestURLFetcherFactory factory;
175 scoped_ptr<V4UpdateProtocolManager> pm(CreateProtocolManager());
176
177
178 const std::vector<ListUpdateResponse> expected_lurs;
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
187 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
188 DCHECK(fetcher);
189 fetcher->set_status(net::URLRequestStatus());
190 fetcher->set_response_code(200);
191 fetcher->SetResponseString(GetStockV4UpdateResponse());
192 fetcher->delegate()->OnURLFetchComplete(fetcher);
193
194 // No error, back off multiplier is unchanged.
195 EXPECT_EQ(0ul, pm->update_error_count_);
196 EXPECT_EQ(1ul, pm->update_back_off_mult_);
197 }
198
199 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « components/safe_browsing_db/v4_update_protocol_manager.cc ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698