OLD | NEW |
---|---|
(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<UpdateListIdentifier>* lists_to_update) { | |
49 UpdateListIdentifier* list_identifier = new UpdateListIdentifier(); | |
Nathan Parker
2016/03/25 23:51:01
This should be on the stack.
vakh (use Gerrit instead)
2016/03/26 00:06:56
Done.
| |
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, const std::string&>* | |
74 current_list_states) { | |
75 // TODO(vakh): Implement this to test the cases when we have an existing | |
76 // state for some of the lists. | |
77 } | |
78 | |
79 std::string GetStockV4UpdateResponse() { | |
80 FetchThreatListUpdatesResponse response; | |
81 | |
82 ListUpdateResponse* lur = response.add_list_update_responses(); | |
83 lur->set_platform_type(WINDOWS_PLATFORM); | |
84 lur->set_response_type(ListUpdateResponse::PARTIAL_UPDATE); | |
85 lur->set_threat_entry_type(URL_EXPRESSION); | |
86 lur->set_threat_type(MALWARE_THREAT); | |
87 | |
88 lur = response.add_list_update_responses(); | |
89 lur->set_platform_type(WINDOWS_PLATFORM); | |
90 lur->set_response_type(ListUpdateResponse::PARTIAL_UPDATE); | |
91 lur->set_threat_entry_type(URL_EXPRESSION); | |
92 lur->set_threat_type(UNWANTED_SOFTWARE); | |
93 | |
94 lur = response.add_list_update_responses(); | |
95 lur->set_platform_type(WINDOWS_PLATFORM); | |
96 lur->set_response_type(ListUpdateResponse::FULL_UPDATE); | |
97 lur->set_threat_entry_type(BINARY_DIGEST); | |
98 lur->set_threat_type(MALWARE_THREAT); | |
99 | |
100 // Serialize. | |
101 std::string res_data; | |
102 response.SerializeToString(&res_data); | |
103 | |
104 return res_data; | |
105 } | |
106 }; | |
107 | |
108 void ValidateGetUpdatesResults( | |
109 const std::vector<ListUpdateResponse>& expected_lurs, | |
110 const std::vector<ListUpdateResponse>& list_update_responses) { | |
111 ASSERT_EQ(expected_lurs.size(), list_update_responses.size()); | |
112 | |
113 for (unsigned int i = 0; i < list_update_responses.size(); ++i) { | |
114 const ListUpdateResponse& expected = expected_lurs[i]; | |
115 const ListUpdateResponse& actual = list_update_responses[i]; | |
116 | |
117 EXPECT_EQ(expected.platform_type(), actual.platform_type()); | |
118 EXPECT_EQ(expected.response_type(), actual.response_type()); | |
119 EXPECT_EQ(expected.threat_entry_type(), actual.threat_entry_type()); | |
120 EXPECT_EQ(expected.threat_type(), actual.threat_type()); | |
121 | |
122 // TODO(vakh): Test more fields from the proto. | |
123 } | |
124 } | |
125 | |
126 // TODO(vakh): Add many more tests. | |
127 | |
128 TEST_F(SafeBrowsingV4UpdateProtocolManagerTest, | |
129 TestGetUpdatesErrorHandlingNetwork) { | |
130 net::TestURLFetcherFactory factory; | |
131 scoped_ptr<V4UpdateProtocolManager> pm(CreateProtocolManager()); | |
132 | |
133 const std::vector<ListUpdateResponse> expected_lurs; | |
134 const base::hash_set<UpdateListIdentifier> lists_to_update; | |
135 const base::hash_map<UpdateListIdentifier, const std::string&> | |
136 current_list_states; | |
137 pm->GetUpdates(lists_to_update, current_list_states, | |
138 base::Bind(&ValidateGetUpdatesResults, expected_lurs)); | |
139 | |
140 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | |
141 DCHECK(fetcher); | |
142 // Failed request status should result in error. | |
143 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, | |
144 net::ERR_CONNECTION_RESET)); | |
145 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
146 | |
147 // Should have recorded one error, but back off multiplier is unchanged. | |
148 EXPECT_EQ(1ul, pm->update_error_count_); | |
149 EXPECT_EQ(1ul, pm->update_back_off_mult_); | |
150 } | |
151 | |
152 TEST_F(SafeBrowsingV4UpdateProtocolManagerTest, | |
153 TestGetUpdatesErrorHandlingResponseCode) { | |
154 net::TestURLFetcherFactory factory; | |
155 scoped_ptr<V4UpdateProtocolManager> pm(CreateProtocolManager()); | |
156 | |
157 const std::vector<ListUpdateResponse> expected_lurs; | |
158 const base::hash_set<UpdateListIdentifier> lists_to_update; | |
159 const base::hash_map<UpdateListIdentifier, const std::string&> | |
160 current_list_states; | |
161 pm->GetUpdates(lists_to_update, current_list_states, | |
162 base::Bind(&ValidateGetUpdatesResults, expected_lurs)); | |
163 | |
164 | |
165 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | |
166 DCHECK(fetcher); | |
167 fetcher->set_status(net::URLRequestStatus()); | |
168 // Response code of anything other than 200 should result in error. | |
169 fetcher->set_response_code(204); | |
170 fetcher->SetResponseString(GetStockV4UpdateResponse()); | |
171 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
172 | |
173 // Should have recorded one error, but back off multiplier is unchanged. | |
174 EXPECT_EQ(1ul, pm->update_error_count_); | |
175 EXPECT_EQ(1ul, pm->update_back_off_mult_); | |
176 } | |
177 | |
178 TEST_F(SafeBrowsingV4UpdateProtocolManagerTest, TestGetUpdatesNoError) { | |
179 net::TestURLFetcherFactory factory; | |
180 scoped_ptr<V4UpdateProtocolManager> pm(CreateProtocolManager()); | |
181 | |
182 | |
183 const std::vector<ListUpdateResponse> expected_lurs; | |
184 base::hash_set<UpdateListIdentifier> lists_to_update; | |
185 SetupListsToUpdate(&lists_to_update); | |
186 base::hash_map<UpdateListIdentifier, const std::string&> | |
187 current_list_states; | |
188 SetupCurrentListStates(lists_to_update, ¤t_list_states); | |
189 pm->GetUpdates(lists_to_update, current_list_states, | |
190 base::Bind(&ValidateGetUpdatesResults, expected_lurs)); | |
191 ClearListsToUpdate(&lists_to_update); | |
192 | |
193 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | |
194 DCHECK(fetcher); | |
195 fetcher->set_status(net::URLRequestStatus()); | |
196 fetcher->set_response_code(200); | |
197 fetcher->SetResponseString(GetStockV4UpdateResponse()); | |
198 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
199 | |
200 // No error, back off multiplier is unchanged. | |
201 EXPECT_EQ(0ul, pm->update_error_count_); | |
202 EXPECT_EQ(1ul, pm->update_back_off_mult_); | |
203 } | |
204 | |
205 } // namespace safe_browsing | |
OLD | NEW |