OLD | NEW |
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/time/time.h" | 10 #include "base/time/time.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 const char kAppVer[] = "1.0"; | 26 const char kAppVer[] = "1.0"; |
27 const char kKeyParam[] = "test_key_param"; | 27 const char kKeyParam[] = "test_key_param"; |
28 | 28 |
29 } // namespace | 29 } // namespace |
30 | 30 |
31 namespace safe_browsing { | 31 namespace safe_browsing { |
32 | 32 |
33 class SafeBrowsingV4GetHashProtocolManagerTest : public testing::Test { | 33 class SafeBrowsingV4GetHashProtocolManagerTest : public testing::Test { |
34 protected: | 34 protected: |
35 scoped_ptr<V4GetHashProtocolManager> CreateProtocolManager() { | 35 scoped_ptr<V4GetHashProtocolManager> CreateProtocolManager() { |
36 V4GetHashProtocolConfig config; | 36 V4ProtocolConfig config; |
37 config.client_name = kClient; | 37 config.client_name = kClient; |
38 config.version = kAppVer; | 38 config.version = kAppVer; |
39 config.key_param = kKeyParam; | 39 config.key_param = kKeyParam; |
40 return scoped_ptr<V4GetHashProtocolManager>( | 40 return scoped_ptr<V4GetHashProtocolManager>( |
41 V4GetHashProtocolManager::Create(NULL, config)); | 41 V4GetHashProtocolManager::Create(NULL, config)); |
42 } | 42 } |
43 | 43 |
44 std::string GetStockV4HashResponse() { | 44 std::string GetStockV4HashResponse() { |
45 FindFullHashesResponse res; | 45 FindFullHashesResponse res; |
46 res.mutable_negative_cache_duration()->set_seconds(600); | 46 res.mutable_negative_cache_duration()->set_seconds(600); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 fetcher->set_status(net::URLRequestStatus()); | 156 fetcher->set_status(net::URLRequestStatus()); |
157 fetcher->set_response_code(200); | 157 fetcher->set_response_code(200); |
158 fetcher->SetResponseString(GetStockV4HashResponse()); | 158 fetcher->SetResponseString(GetStockV4HashResponse()); |
159 fetcher->delegate()->OnURLFetchComplete(fetcher); | 159 fetcher->delegate()->OnURLFetchComplete(fetcher); |
160 | 160 |
161 // No error, back off multiplier is unchanged. | 161 // No error, back off multiplier is unchanged. |
162 EXPECT_EQ(0ul, pm->gethash_error_count_); | 162 EXPECT_EQ(0ul, pm->gethash_error_count_); |
163 EXPECT_EQ(1ul, pm->gethash_back_off_mult_); | 163 EXPECT_EQ(1ul, pm->gethash_back_off_mult_); |
164 } | 164 } |
165 | 165 |
166 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, TestGetHashBackOffTimes) { | |
167 scoped_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); | |
168 | |
169 // No errors or back off time yet. | |
170 EXPECT_EQ(0U, pm->gethash_error_count_); | |
171 EXPECT_EQ(1U, pm->gethash_back_off_mult_); | |
172 Time now = Time::Now(); | |
173 EXPECT_TRUE(pm->next_gethash_time_ < now); | |
174 | |
175 // 1 error. | |
176 pm->HandleGetHashError(now); | |
177 EXPECT_EQ(1U, pm->gethash_error_count_); | |
178 EXPECT_EQ(1U, pm->gethash_back_off_mult_); | |
179 EXPECT_LE(now + TimeDelta::FromMinutes(15), pm->next_gethash_time_); | |
180 EXPECT_GE(now + TimeDelta::FromMinutes(30), pm->next_gethash_time_); | |
181 | |
182 // 2 errors. | |
183 pm->HandleGetHashError(now); | |
184 EXPECT_EQ(2U, pm->gethash_error_count_); | |
185 EXPECT_EQ(2U, pm->gethash_back_off_mult_); | |
186 EXPECT_LE(now + TimeDelta::FromMinutes(30), pm->next_gethash_time_); | |
187 EXPECT_GE(now + TimeDelta::FromMinutes(60), pm->next_gethash_time_); | |
188 | |
189 // 3 errors. | |
190 pm->HandleGetHashError(now); | |
191 EXPECT_EQ(3U, pm->gethash_error_count_); | |
192 EXPECT_EQ(4U, pm->gethash_back_off_mult_); | |
193 EXPECT_LE(now + TimeDelta::FromMinutes(60), pm->next_gethash_time_); | |
194 EXPECT_GE(now + TimeDelta::FromMinutes(120), pm->next_gethash_time_); | |
195 | |
196 // 4 errors. | |
197 pm->HandleGetHashError(now); | |
198 EXPECT_EQ(4U, pm->gethash_error_count_); | |
199 EXPECT_EQ(8U, pm->gethash_back_off_mult_); | |
200 EXPECT_LE(now + TimeDelta::FromMinutes(120), pm->next_gethash_time_); | |
201 EXPECT_GE(now + TimeDelta::FromMinutes(240), pm->next_gethash_time_); | |
202 | |
203 // 5 errors. | |
204 pm->HandleGetHashError(now); | |
205 EXPECT_EQ(5U, pm->gethash_error_count_); | |
206 EXPECT_EQ(16U, pm->gethash_back_off_mult_); | |
207 EXPECT_LE(now + TimeDelta::FromMinutes(240), pm->next_gethash_time_); | |
208 EXPECT_GE(now + TimeDelta::FromMinutes(480), pm->next_gethash_time_); | |
209 | |
210 // 6 errors. | |
211 pm->HandleGetHashError(now); | |
212 EXPECT_EQ(6U, pm->gethash_error_count_); | |
213 EXPECT_EQ(32U, pm->gethash_back_off_mult_); | |
214 EXPECT_LE(now + TimeDelta::FromMinutes(480), pm->next_gethash_time_); | |
215 EXPECT_GE(now + TimeDelta::FromMinutes(960), pm->next_gethash_time_); | |
216 | |
217 // 7 errors. | |
218 pm->HandleGetHashError(now); | |
219 EXPECT_EQ(7U, pm->gethash_error_count_); | |
220 EXPECT_EQ(64U, pm->gethash_back_off_mult_); | |
221 EXPECT_LE(now + TimeDelta::FromMinutes(960), pm->next_gethash_time_); | |
222 EXPECT_GE(now + TimeDelta::FromMinutes(1920), pm->next_gethash_time_); | |
223 | |
224 // 8 errors, reached max backoff. | |
225 pm->HandleGetHashError(now); | |
226 EXPECT_EQ(8U, pm->gethash_error_count_); | |
227 EXPECT_EQ(128U, pm->gethash_back_off_mult_); | |
228 EXPECT_EQ(now + TimeDelta::FromHours(24), pm->next_gethash_time_); | |
229 | |
230 // 9 errors, reached max backoff and multiplier capped. | |
231 pm->HandleGetHashError(now); | |
232 EXPECT_EQ(9U, pm->gethash_error_count_); | |
233 EXPECT_EQ(128U, pm->gethash_back_off_mult_); | |
234 EXPECT_EQ(now + TimeDelta::FromHours(24), pm->next_gethash_time_); | |
235 } | |
236 | |
237 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, TestGetHashUrl) { | |
238 scoped_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); | |
239 | |
240 EXPECT_EQ( | |
241 "https://safebrowsing.googleapis.com/v4/encodedFullHashes/request_base64?" | |
242 "alt=proto&client_id=unittest&client_version=1.0&key=test_key_param", | |
243 pm->GetHashUrl("request_base64").spec()); | |
244 } | |
245 | |
246 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, TestGetHashRequest) { | 166 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, TestGetHashRequest) { |
247 scoped_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); | 167 scoped_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); |
248 | 168 |
249 FindFullHashesRequest req; | 169 FindFullHashesRequest req; |
250 ThreatInfo* info = req.mutable_threat_info(); | 170 ThreatInfo* info = req.mutable_threat_info(); |
251 info->add_threat_types(API_ABUSE); | 171 info->add_threat_types(API_ABUSE); |
252 info->add_platform_types(CHROME_PLATFORM); | 172 info->add_platform_types(CHROME_PLATFORM); |
253 info->add_threat_entry_types(URL_EXPRESSION); | 173 info->add_threat_entry_types(URL_EXPRESSION); |
254 | 174 |
255 SBPrefix one = 1u; | 175 SBPrefix one = 1u; |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 // Serialize. | 343 // Serialize. |
424 std::string res_data; | 344 std::string res_data; |
425 res.SerializeToString(&res_data); | 345 res.SerializeToString(&res_data); |
426 | 346 |
427 std::vector<SBFullHashResult> full_hashes; | 347 std::vector<SBFullHashResult> full_hashes; |
428 base::TimeDelta cache_lifetime; | 348 base::TimeDelta cache_lifetime; |
429 EXPECT_FALSE(pm->ParseHashResponse(res_data, &full_hashes, &cache_lifetime)); | 349 EXPECT_FALSE(pm->ParseHashResponse(res_data, &full_hashes, &cache_lifetime)); |
430 } | 350 } |
431 | 351 |
432 } // namespace safe_browsing | 352 } // namespace safe_browsing |
OLD | NEW |