Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 | 5 |
| 6 #include "base/logging.h" | 6 #include "base/logging.h" |
| 7 #include "base/time.h" | 7 #include "base/time.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "chrome/browser/safe_browsing/protocol_manager.h" | 9 #include "chrome/browser/safe_browsing/protocol_manager.h" |
| 10 | 10 |
| 11 using base::Time; | 11 using base::Time; |
| 12 using base::TimeDelta; | 12 using base::TimeDelta; |
| 13 | 13 |
| 14 static const char kUrlPrefix[] = "https://prefix.com/foo"; | 14 static const char kUrlPrefix[] = "https://prefix.com/foo"; |
| 15 static const char kClient[] = "unittest"; | 15 static const char kClient[] = "unittest"; |
| 16 static const char kAppVer[] = "1.0"; | 16 static const char kAppVer[] = "1.0"; |
| 17 static const char kAdditionalQuery[] = "additional_query"; | 17 static const char kAdditionalQuery[] = "additional_query"; |
| 18 | 18 |
| 19 class SafeBrowsingProtocolManagerTest : public testing::Test { | 19 class SafeBrowsingProtocolManagerTest : public testing::Test { |
| 20 }; | 20 }; |
| 21 | 21 |
| 22 // Ensure that we respect section 5 of the SafeBrowsing protocol specification. | 22 // Ensure that we respect section 5 of the SafeBrowsing protocol specification. |
| 23 TEST_F(SafeBrowsingProtocolManagerTest, TestBackOffTimes) { | 23 TEST_F(SafeBrowsingProtocolManagerTest, TestBackOffTimes) { |
| 24 SafeBrowsingProtocolManager pm(NULL, kClient, NULL, kUrlPrefix, false); | 24 SafeBrowsingProtocolManager pm(NULL, kClient, NULL, kUrlPrefix, false); |
| 25 pm.next_update_sec_ = 1800; | 25 pm.next_update_interval_ = base::TimeDelta::FromSeconds(1800); |
| 26 DCHECK(pm.back_off_fuzz_ >= 0.0 && pm.back_off_fuzz_ <= 1.0); | 26 DCHECK(pm.back_off_fuzz_ >= 0.0 && pm.back_off_fuzz_ <= 1.0); |
|
mattm
2012/09/12 20:00:03
While you're here you could change this to some AS
cbentzel
2012/09/12 21:32:47
This was the only DCHECK in the file -> converted
| |
| 27 | 27 |
| 28 base::TimeDelta next; | |
| 29 | |
| 28 // No errors received so far. | 30 // No errors received so far. |
| 29 EXPECT_EQ(pm.GetNextUpdateTime(false), 1800 * 1000); | 31 next = pm.GetNextUpdateInterval(false); |
| 32 EXPECT_EQ(next, base::TimeDelta::FromSeconds(1800)); | |
| 30 | 33 |
| 31 // 1 error. | 34 // 1 error. |
| 32 EXPECT_EQ(pm.GetNextUpdateTime(true), 60 * 1000); | 35 next = pm.GetNextUpdateInterval(true); |
| 36 EXPECT_EQ(next, base::TimeDelta::FromSeconds(60)); | |
| 33 | 37 |
| 34 // 2 errors. | 38 // 2 errors. |
| 35 int next_time = pm.GetNextUpdateTime(true) / (60 * 1000); // Minutes | 39 next = pm.GetNextUpdateInterval(true); |
| 36 EXPECT_TRUE(next_time >= 30 && next_time <= 60); | 40 EXPECT_TRUE(next >= base::TimeDelta::FromMinutes(30) && |
| 41 next <= base::TimeDelta::FromMinutes(60)); | |
| 37 | 42 |
| 38 // 3 errors. | 43 // 3 errors. |
| 39 next_time = pm.GetNextUpdateTime(true) / (60 * 1000); | 44 next = pm.GetNextUpdateInterval(true); |
| 40 EXPECT_TRUE(next_time >= 60 && next_time <= 120); | 45 EXPECT_TRUE(next >= base::TimeDelta::FromMinutes(60) && |
| 46 next <= base::TimeDelta::FromMinutes(120)); | |
| 41 | 47 |
| 42 // 4 errors. | 48 // 4 errors. |
| 43 next_time = pm.GetNextUpdateTime(true) / (60 * 1000); | 49 next = pm.GetNextUpdateInterval(true); |
| 44 EXPECT_TRUE(next_time >= 120 && next_time <= 240); | 50 EXPECT_TRUE(next >= base::TimeDelta::FromMinutes(120) && |
| 51 next <= base::TimeDelta::FromMinutes(240)); | |
| 45 | 52 |
| 46 // 5 errors. | 53 // 5 errors. |
| 47 next_time = pm.GetNextUpdateTime(true) / (60 * 1000); | 54 next = pm.GetNextUpdateInterval(true); |
| 48 EXPECT_TRUE(next_time >= 240 && next_time <= 480); | 55 EXPECT_TRUE(next >= base::TimeDelta::FromMinutes(240) && |
| 56 next <= base::TimeDelta::FromMinutes(480)); | |
| 49 | 57 |
| 50 // 6 errors, reached max backoff. | 58 // 6 errors, reached max backoff. |
| 51 EXPECT_EQ(pm.GetNextUpdateTime(true), 480 * 60 * 1000); | 59 next = pm.GetNextUpdateInterval(true); |
| 60 EXPECT_EQ(next, base::TimeDelta::FromMinutes(480)); | |
| 52 | 61 |
| 53 // 7 errors. | 62 // 7 errors. |
| 54 EXPECT_EQ(pm.GetNextUpdateTime(true), 480 * 60 * 1000); | 63 next = pm.GetNextUpdateInterval(true); |
| 64 EXPECT_EQ(next, base::TimeDelta::FromMinutes(480)); | |
| 55 | 65 |
| 56 // Received a successful response. | 66 // Received a successful response. |
| 57 EXPECT_EQ(pm.GetNextUpdateTime(false), 1800 * 1000); | 67 next = pm.GetNextUpdateInterval(false); |
| 68 EXPECT_EQ(next, base::TimeDelta::FromSeconds(1800)); | |
| 58 } | 69 } |
| 59 | 70 |
| 60 TEST_F(SafeBrowsingProtocolManagerTest, TestChunkStrings) { | 71 TEST_F(SafeBrowsingProtocolManagerTest, TestChunkStrings) { |
| 61 SafeBrowsingProtocolManager pm(NULL, kClient, NULL, kUrlPrefix, false); | 72 SafeBrowsingProtocolManager pm(NULL, kClient, NULL, kUrlPrefix, false); |
| 62 | 73 |
| 63 // Add and Sub chunks. | 74 // Add and Sub chunks. |
| 64 SBListChunkRanges phish("goog-phish-shavar"); | 75 SBListChunkRanges phish("goog-phish-shavar"); |
| 65 phish.adds = "1,4,6,8-20,99"; | 76 phish.adds = "1,4,6,8-20,99"; |
| 66 phish.subs = "16,32,64-96"; | 77 phish.subs = "16,32,64-96"; |
| 67 EXPECT_EQ(pm.FormatList(phish), | 78 EXPECT_EQ(pm.FormatList(phish), |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 242 pm.set_additional_query(kAdditionalQuery); | 253 pm.set_additional_query(kAdditionalQuery); |
| 243 EXPECT_EQ("https://localhost:1234/foo/bar?foo&additional_query", | 254 EXPECT_EQ("https://localhost:1234/foo/bar?foo&additional_query", |
| 244 pm.NextChunkUrl(url_partial).spec()); | 255 pm.NextChunkUrl(url_partial).spec()); |
| 245 EXPECT_EQ("http://localhost:1234/foo/bar?foo&additional_query", | 256 EXPECT_EQ("http://localhost:1234/foo/bar?foo&additional_query", |
| 246 pm.NextChunkUrl(url_http_full).spec()); | 257 pm.NextChunkUrl(url_http_full).spec()); |
| 247 EXPECT_EQ("https://localhost:1234/foo/bar?foo&additional_query", | 258 EXPECT_EQ("https://localhost:1234/foo/bar?foo&additional_query", |
| 248 pm.NextChunkUrl(url_https_full).spec()); | 259 pm.NextChunkUrl(url_https_full).spec()); |
| 249 EXPECT_EQ("https://localhost:1234/foo/bar?additional_query", | 260 EXPECT_EQ("https://localhost:1234/foo/bar?additional_query", |
| 250 pm.NextChunkUrl(url_https_no_query).spec()); | 261 pm.NextChunkUrl(url_https_no_query).spec()); |
| 251 } | 262 } |
| OLD | NEW |