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 "components/safe_browsing_db/v4_protocol_manager_util.h" | 5 #include "components/safe_browsing_db/v4_protocol_manager_util.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/strings/stringprintf.h" |
10 #include "base/time/time.h" | 11 #include "base/time/time.h" |
11 #include "net/base/escape.h" | 12 #include "net/base/escape.h" |
12 #include "net/http/http_request_headers.h" | 13 #include "net/http/http_request_headers.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 using base::Time; | 16 using base::Time; |
16 using base::TimeDelta; | 17 using base::TimeDelta; |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 const char kClient[] = "unittest"; | 21 const char kClient[] = "unittest"; |
21 const char kAppVer[] = "1.0"; | 22 const char kAppVer[] = "1.0"; |
22 const char kKeyParam[] = "test_key_param"; | 23 const char kKeyParam[] = "test_key_param"; |
23 | 24 |
| 25 bool VectorContains(const std::vector<std::string>& data, |
| 26 const std::string& str) { |
| 27 return std::find(data.begin(), data.end(), str) != data.end(); |
| 28 } |
| 29 |
24 } // namespace | 30 } // namespace |
25 | 31 |
26 namespace safe_browsing { | 32 namespace safe_browsing { |
27 | 33 |
28 class SafeBrowsingV4ProtocolManagerUtilTest : public testing::Test { | 34 class V4ProtocolManagerUtilTest : public testing::Test { |
29 protected: | 35 protected: |
30 void PopulateV4ProtocolConfig(V4ProtocolConfig* config) { | 36 void PopulateV4ProtocolConfig(V4ProtocolConfig* config) { |
31 config->client_name = kClient; | 37 config->client_name = kClient; |
32 config->version = kAppVer; | 38 config->version = kAppVer; |
33 config->key_param = kKeyParam; | 39 config->key_param = kKeyParam; |
34 } | 40 } |
35 }; | 41 }; |
36 | 42 |
37 TEST_F(SafeBrowsingV4ProtocolManagerUtilTest, TestBackOffLogic) { | 43 TEST_F(V4ProtocolManagerUtilTest, TestBackOffLogic) { |
38 size_t error_count = 0, back_off_multiplier = 1; | 44 size_t error_count = 0, back_off_multiplier = 1; |
39 | 45 |
40 // 1 error. | 46 // 1 error. |
41 base::TimeDelta next = V4ProtocolManagerUtil::GetNextBackOffInterval( | 47 base::TimeDelta next = V4ProtocolManagerUtil::GetNextBackOffInterval( |
42 &error_count, &back_off_multiplier); | 48 &error_count, &back_off_multiplier); |
43 EXPECT_EQ(1U, error_count); | 49 EXPECT_EQ(1U, error_count); |
44 EXPECT_EQ(1U, back_off_multiplier); | 50 EXPECT_EQ(1U, back_off_multiplier); |
45 EXPECT_LE(TimeDelta::FromMinutes(15), next); | 51 EXPECT_LE(TimeDelta::FromMinutes(15), next); |
46 EXPECT_GE(TimeDelta::FromMinutes(30), next); | 52 EXPECT_GE(TimeDelta::FromMinutes(30), next); |
47 | 53 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 EXPECT_EQ(TimeDelta::FromHours(24), next); | 107 EXPECT_EQ(TimeDelta::FromHours(24), next); |
102 | 108 |
103 // 9 errors, reached max backoff and multiplier capped. | 109 // 9 errors, reached max backoff and multiplier capped. |
104 next = V4ProtocolManagerUtil::GetNextBackOffInterval(&error_count, | 110 next = V4ProtocolManagerUtil::GetNextBackOffInterval(&error_count, |
105 &back_off_multiplier); | 111 &back_off_multiplier); |
106 EXPECT_EQ(9U, error_count); | 112 EXPECT_EQ(9U, error_count); |
107 EXPECT_EQ(128U, back_off_multiplier); | 113 EXPECT_EQ(128U, back_off_multiplier); |
108 EXPECT_EQ(TimeDelta::FromHours(24), next); | 114 EXPECT_EQ(TimeDelta::FromHours(24), next); |
109 } | 115 } |
110 | 116 |
111 TEST_F(SafeBrowsingV4ProtocolManagerUtilTest, | 117 TEST_F(V4ProtocolManagerUtilTest, TestGetRequestUrlAndUpdateHeaders) { |
112 TestGetRequestUrlAndUpdateHeaders) { | |
113 V4ProtocolConfig config; | 118 V4ProtocolConfig config; |
114 PopulateV4ProtocolConfig(&config); | 119 PopulateV4ProtocolConfig(&config); |
115 | 120 |
116 net::HttpRequestHeaders headers; | 121 net::HttpRequestHeaders headers; |
117 GURL gurl; | 122 GURL gurl; |
118 V4ProtocolManagerUtil::GetRequestUrlAndHeaders("request_base64", "someMethod", | 123 V4ProtocolManagerUtil::GetRequestUrlAndHeaders("request_base64", "someMethod", |
119 config, &gurl, &headers); | 124 config, &gurl, &headers); |
120 std::string expectedUrl = | 125 std::string expectedUrl = |
121 "https://safebrowsing.googleapis.com/v4/someMethod?" | 126 "https://safebrowsing.googleapis.com/v4/someMethod?" |
122 "$req=request_base64&$ct=application/x-protobuf&key=test_key_param"; | 127 "$req=request_base64&$ct=application/x-protobuf&key=test_key_param"; |
123 EXPECT_EQ(expectedUrl, gurl.spec()); | 128 EXPECT_EQ(expectedUrl, gurl.spec()); |
124 std::string header_value; | 129 std::string header_value; |
125 EXPECT_TRUE(headers.GetHeader("X-HTTP-Method-Override", &header_value)); | 130 EXPECT_TRUE(headers.GetHeader("X-HTTP-Method-Override", &header_value)); |
126 EXPECT_EQ("POST", header_value); | 131 EXPECT_EQ("POST", header_value); |
127 } | 132 } |
128 | 133 |
| 134 // Tests that we generate the required host/path combinations for testing |
| 135 // according to the Safe Browsing spec. |
| 136 // See: https://developers.google.com/safe-browsing/v4/urls-hashing |
| 137 TEST_F(V4ProtocolManagerUtilTest, UrlParsing) { |
| 138 std::vector<std::string> hosts, paths; |
| 139 |
| 140 GURL url("http://a.b.c/1/2.html?param=1"); |
| 141 V4ProtocolManagerUtil::GenerateHostsToCheck(url, &hosts); |
| 142 V4ProtocolManagerUtil::GeneratePathsToCheck(url, &paths); |
| 143 EXPECT_EQ(hosts.size(), static_cast<size_t>(2)); |
| 144 EXPECT_EQ(paths.size(), static_cast<size_t>(4)); |
| 145 EXPECT_EQ(hosts[0], "b.c"); |
| 146 EXPECT_EQ(hosts[1], "a.b.c"); |
| 147 |
| 148 EXPECT_TRUE(VectorContains(paths, "/1/2.html?param=1")); |
| 149 EXPECT_TRUE(VectorContains(paths, "/1/2.html")); |
| 150 EXPECT_TRUE(VectorContains(paths, "/1/")); |
| 151 EXPECT_TRUE(VectorContains(paths, "/")); |
| 152 |
| 153 url = GURL("http://a.b.c.d.e.f.g/1.html"); |
| 154 V4ProtocolManagerUtil::GenerateHostsToCheck(url, &hosts); |
| 155 V4ProtocolManagerUtil::GeneratePathsToCheck(url, &paths); |
| 156 EXPECT_EQ(hosts.size(), static_cast<size_t>(5)); |
| 157 EXPECT_EQ(paths.size(), static_cast<size_t>(2)); |
| 158 EXPECT_EQ(hosts[0], "f.g"); |
| 159 EXPECT_EQ(hosts[1], "e.f.g"); |
| 160 EXPECT_EQ(hosts[2], "d.e.f.g"); |
| 161 EXPECT_EQ(hosts[3], "c.d.e.f.g"); |
| 162 EXPECT_EQ(hosts[4], "a.b.c.d.e.f.g"); |
| 163 EXPECT_TRUE(VectorContains(paths, "/1.html")); |
| 164 EXPECT_TRUE(VectorContains(paths, "/")); |
| 165 |
| 166 url = GURL("http://a.b/saw-cgi/eBayISAPI.dll/"); |
| 167 V4ProtocolManagerUtil::GeneratePathsToCheck(url, &paths); |
| 168 EXPECT_EQ(paths.size(), static_cast<size_t>(3)); |
| 169 EXPECT_TRUE(VectorContains(paths, "/saw-cgi/eBayISAPI.dll/")); |
| 170 EXPECT_TRUE(VectorContains(paths, "/saw-cgi/")); |
| 171 EXPECT_TRUE(VectorContains(paths, "/")); |
| 172 } |
| 173 |
| 174 // Tests the url canonicalization according to the Safe Browsing spec. |
| 175 // See: https://developers.google.com/safe-browsing/v4/urls-hashing |
| 176 TEST_F(V4ProtocolManagerUtilTest, CanonicalizeUrl) { |
| 177 struct { |
| 178 const char* input_url; |
| 179 const char* expected_canonicalized_hostname; |
| 180 const char* expected_canonicalized_path; |
| 181 const char* expected_canonicalized_query; |
| 182 } tests[] = { |
| 183 {"http://host/%25%32%35", "host", "/%25", ""}, |
| 184 {"http://host/%25%32%35%25%32%35", "host", "/%25%25", ""}, |
| 185 {"http://host/%2525252525252525", "host", "/%25", ""}, |
| 186 {"http://host/asdf%25%32%35asd", "host", "/asdf%25asd", ""}, |
| 187 {"http://host/%%%25%32%35asd%%", "host", "/%25%25%25asd%25%25", ""}, |
| 188 {"http://host/%%%25%32%35asd%%", "host", "/%25%25%25asd%25%25", ""}, |
| 189 {"http://www.google.com/", "www.google.com", "/", ""}, |
| 190 {"http://%31%36%38%2e%31%38%38%2e%39%39%2e%32%36/%2E%73%65%63%75%72%65/" |
| 191 "%77" |
| 192 "%77%77%2E%65%62%61%79%2E%63%6F%6D/", |
| 193 "168.188.99.26", "/.secure/www.ebay.com/", ""}, |
| 194 {"http://195.127.0.11/uploads/%20%20%20%20/.verify/" |
| 195 ".eBaysecure=updateuserd" |
| 196 "ataxplimnbqmn-xplmvalidateinfoswqpcmlx=hgplmcx/", |
| 197 "195.127.0.11", |
| 198 "/uploads/%20%20%20%20/.verify/" |
| 199 ".eBaysecure=updateuserdataxplimnbqmn-xplmv" |
| 200 "alidateinfoswqpcmlx=hgplmcx/", |
| 201 ""}, |
| 202 {"http://host.com/%257Ea%2521b%2540c%2523d%2524e%25f%255E00%252611%252A" |
| 203 "22%252833%252944_55%252B", |
| 204 "host.com", "/~a!b@c%23d$e%25f^00&11*22(33)44_55+", ""}, |
| 205 {"http://3279880203/blah", "195.127.0.11", "/blah", ""}, |
| 206 {"http://www.google.com/blah/..", "www.google.com", "/", ""}, |
| 207 {"http://www.google.com/blah#fraq", "www.google.com", "/blah", ""}, |
| 208 {"http://www.GOOgle.com/", "www.google.com", "/", ""}, |
| 209 {"http://www.google.com.../", "www.google.com", "/", ""}, |
| 210 {"http://www.google.com/q?", "www.google.com", "/q", ""}, |
| 211 {"http://www.google.com/q?r?", "www.google.com", "/q", "r?"}, |
| 212 {"http://www.google.com/q?r?s", "www.google.com", "/q", "r?s"}, |
| 213 {"http://evil.com/foo#bar#baz", "evil.com", "/foo", ""}, |
| 214 {"http://evil.com/foo;", "evil.com", "/foo;", ""}, |
| 215 {"http://evil.com/foo?bar;", "evil.com", "/foo", "bar;"}, |
| 216 {"http://notrailingslash.com", "notrailingslash.com", "/", ""}, |
| 217 {"http://www.gotaport.com:1234/", "www.gotaport.com", "/", ""}, |
| 218 {" http://www.google.com/ ", "www.google.com", "/", ""}, |
| 219 {"http:// leadingspace.com/", "%20leadingspace.com", "/", ""}, |
| 220 {"http://%20leadingspace.com/", "%20leadingspace.com", "/", ""}, |
| 221 {"https://www.securesite.com/", "www.securesite.com", "/", ""}, |
| 222 {"http://host.com/ab%23cd", "host.com", "/ab%23cd", ""}, |
| 223 {"http://host%3e.com//twoslashes?more//slashes", "host>.com", |
| 224 "/twoslashes", "more//slashes"}, |
| 225 {"http://host.com/abc?val=xyz#anything", "host.com", "/abc", "val=xyz"}, |
| 226 {"http://abc:def@host.com/xyz", "host.com", "/xyz", ""}, |
| 227 {"http://host%3e.com/abc/%2e%2e%2fdef", "host>.com", "/def", ""}, |
| 228 {"http://.......host...com.....//abc/////def%2F%2F%2Fxyz", "host.com", |
| 229 "/abc/def/xyz", ""}, |
| 230 {"ftp://host.com/foo?bar", "host.com", "/foo", "bar"}, |
| 231 {"data:text/html;charset=utf-8,%0D%0A", "", "", ""}, |
| 232 {"javascript:alert()", "", "", ""}, |
| 233 {"mailto:abc@example.com", "", "", ""}, |
| 234 }; |
| 235 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 236 SCOPED_TRACE(base::StringPrintf("Test: %s", tests[i].input_url)); |
| 237 GURL url(tests[i].input_url); |
| 238 |
| 239 std::string canonicalized_hostname; |
| 240 std::string canonicalized_path; |
| 241 std::string canonicalized_query; |
| 242 V4ProtocolManagerUtil::CanonicalizeUrl(url, &canonicalized_hostname, |
| 243 &canonicalized_path, |
| 244 &canonicalized_query); |
| 245 |
| 246 EXPECT_EQ(tests[i].expected_canonicalized_hostname, canonicalized_hostname); |
| 247 EXPECT_EQ(tests[i].expected_canonicalized_path, canonicalized_path); |
| 248 EXPECT_EQ(tests[i].expected_canonicalized_query, canonicalized_query); |
| 249 } |
| 250 } |
| 251 |
129 } // namespace safe_browsing | 252 } // namespace safe_browsing |
OLD | NEW |