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

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

Issue 1719883003: Ignore: v4_update_protocol_manager: Basic implementation with TODOs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@v4_01_prot_mgr
Patch Set: rebase again (and more branching funkiness) 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 SetupCurrentListStates(
69 const base::hash_set<const UpdateListIdentifier*>& lists_to_update,
70 base::hash_map<const UpdateListIdentifier*, const std::string&>*
71 current_list_states) {
72 // TODO(vakh): Implement this to test the cases when we have an existing
73 // state for some of the lists.
74 }
75
76 std::string GetStockV4UpdateResponse() {
77 FetchThreatListUpdatesResponse response;
78
79 ListUpdateResponse* lur = response.add_list_update_responses();
80 lur->set_platform_type(WINDOWS_PLATFORM);
81 lur->set_response_type(ListUpdateResponse::PARTIAL_UPDATE);
82 lur->set_threat_entry_type(URL_EXPRESSION);
83 lur->set_threat_type(MALWARE_THREAT);
84
85 lur = response.add_list_update_responses();
86 lur->set_platform_type(WINDOWS_PLATFORM);
87 lur->set_response_type(ListUpdateResponse::PARTIAL_UPDATE);
88 lur->set_threat_entry_type(URL_EXPRESSION);
89 lur->set_threat_type(UNWANTED_SOFTWARE);
90
91 lur = response.add_list_update_responses();
92 lur->set_platform_type(WINDOWS_PLATFORM);
93 lur->set_response_type(ListUpdateResponse::FULL_UPDATE);
94 lur->set_threat_entry_type(BINARY_DIGEST);
95 lur->set_threat_type(MALWARE_THREAT);
96
97 // Serialize.
98 std::string res_data;
99 response.SerializeToString(&res_data);
100
101 return res_data;
102 }
103 };
104
105 void ValidateGetUpdatesResults(
106 const std::vector<ListUpdateResponse>& expected_lurs,
107 const std::vector<ListUpdateResponse>& list_update_responses) {
108 ASSERT_EQ(expected_lurs.size(), list_update_responses.size());
109
110 for (unsigned int i = 0; i < list_update_responses.size(); ++i) {
111 const ListUpdateResponse& expected = expected_lurs[i];
112 const ListUpdateResponse& actual = list_update_responses[i];
113
114 EXPECT_EQ(expected.platform_type(), actual.platform_type());
115 EXPECT_EQ(expected.response_type(), actual.response_type());
116 EXPECT_EQ(expected.threat_entry_type(), actual.threat_entry_type());
117 EXPECT_EQ(expected.threat_type(), actual.threat_type());
118
119 // TODO(vakh): Test more fields from the proto.
120 }
121 }
122
123 // TODO(vakh): Add many more tests.
124
125 TEST_F(SafeBrowsingV4UpdateProtocolManagerTest,
126 TestGetUpdatesErrorHandlingNetwork) {
127 net::TestURLFetcherFactory factory;
128 scoped_ptr<V4UpdateProtocolManager> pm(CreateProtocolManager());
129
130 const std::vector<ListUpdateResponse> expected_lurs;
131 const base::hash_set<const UpdateListIdentifier*> lists_to_update;
132 const base::hash_map<const UpdateListIdentifier*, const std::string&>
133 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->set_response_code(200);
143 fetcher->SetResponseString(GetStockV4UpdateResponse());
144 fetcher->delegate()->OnURLFetchComplete(fetcher);
145
146 // Should have recorded one error, but back off multiplier is unchanged.
147 EXPECT_EQ(1ul, pm->update_error_count_);
148 EXPECT_EQ(1ul, pm->update_back_off_mult_);
149 }
150
151 TEST_F(SafeBrowsingV4UpdateProtocolManagerTest,
152 TestGetUpdatesErrorHandlingResponseCode) {
153 net::TestURLFetcherFactory factory;
154 scoped_ptr<V4UpdateProtocolManager> pm(CreateProtocolManager());
155
156 const std::vector<ListUpdateResponse> expected_lurs;
157 const base::hash_set<const UpdateListIdentifier*> lists_to_update;
158 const base::hash_map<const UpdateListIdentifier*, const std::string&>
159 current_list_states;
160 pm->GetUpdates(lists_to_update, current_list_states,
161 base::Bind(&ValidateGetUpdatesResults, expected_lurs));
162
163
164 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
165 DCHECK(fetcher);
166 fetcher->set_status(net::URLRequestStatus());
167 // Response code of anything other than 200 should result in error.
168 fetcher->set_response_code(204);
169 fetcher->SetResponseString(GetStockV4UpdateResponse());
170 fetcher->delegate()->OnURLFetchComplete(fetcher);
171
172 // Should have recorded one error, but back off multiplier is unchanged.
173 EXPECT_EQ(1ul, pm->update_error_count_);
174 EXPECT_EQ(1ul, pm->update_back_off_mult_);
175 }
176
177 TEST_F(SafeBrowsingV4UpdateProtocolManagerTest, TestGetUpdatesNoError) {
178 net::TestURLFetcherFactory factory;
179 scoped_ptr<V4UpdateProtocolManager> pm(CreateProtocolManager());
180
181
182 const std::vector<ListUpdateResponse> expected_lurs;
183 base::hash_set<const UpdateListIdentifier*> lists_to_update;
184 SetupListsToUpdate(&lists_to_update);
185 base::hash_map<const UpdateListIdentifier*, const std::string&>
186 current_list_states;
187 SetupCurrentListStates(lists_to_update, &current_list_states);
188 pm->GetUpdates(lists_to_update, current_list_states,
189 base::Bind(&ValidateGetUpdatesResults, expected_lurs));
190
191 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
192 DCHECK(fetcher);
193 fetcher->set_status(net::URLRequestStatus());
194 fetcher->set_response_code(200);
195 fetcher->SetResponseString(GetStockV4UpdateResponse());
196 fetcher->delegate()->OnURLFetchComplete(fetcher);
197
198 // No error, back off multiplier is unchanged.
199 EXPECT_EQ(0ul, pm->update_error_count_);
200 EXPECT_EQ(1ul, pm->update_back_off_mult_);
201 }
202
203 } // 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