OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
11 #include "components/safe_browsing_db/util.h" | 11 #include "components/safe_browsing_db/util.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "url/gurl.h" | 13 #include "url/gurl.h" |
14 | 14 |
15 namespace safe_browsing { | 15 namespace safe_browsing { |
16 | 16 |
| 17 namespace { |
| 18 |
| 19 bool VectorContains(const std::vector<std::string>& data, |
| 20 const std::string& str) { |
| 21 return std::find(data.begin(), data.end(), str) != data.end(); |
| 22 } |
| 23 |
| 24 } // namespace |
| 25 |
| 26 // Tests that we generate the required host/path combinations for testing |
| 27 // according to the Safe Browsing spec. |
| 28 // See section 6.2 in |
| 29 // http://code.google.com/p/google-safe-browsing/wiki/Protocolv2Spec. |
| 30 TEST(SafeBrowsingDbUtilTest, UrlParsing) { |
| 31 std::vector<std::string> hosts, paths; |
| 32 |
| 33 GURL url("http://a.b.c/1/2.html?param=1"); |
| 34 GenerateHostsToCheck(url, &hosts); |
| 35 GeneratePathsToCheck(url, &paths); |
| 36 EXPECT_EQ(hosts.size(), static_cast<size_t>(2)); |
| 37 EXPECT_EQ(paths.size(), static_cast<size_t>(4)); |
| 38 EXPECT_EQ(hosts[0], "b.c"); |
| 39 EXPECT_EQ(hosts[1], "a.b.c"); |
| 40 |
| 41 EXPECT_TRUE(VectorContains(paths, "/1/2.html?param=1")); |
| 42 EXPECT_TRUE(VectorContains(paths, "/1/2.html")); |
| 43 EXPECT_TRUE(VectorContains(paths, "/1/")); |
| 44 EXPECT_TRUE(VectorContains(paths, "/")); |
| 45 |
| 46 url = GURL("http://a.b.c.d.e.f.g/1.html"); |
| 47 GenerateHostsToCheck(url, &hosts); |
| 48 GeneratePathsToCheck(url, &paths); |
| 49 EXPECT_EQ(hosts.size(), static_cast<size_t>(5)); |
| 50 EXPECT_EQ(paths.size(), static_cast<size_t>(2)); |
| 51 EXPECT_EQ(hosts[0], "f.g"); |
| 52 EXPECT_EQ(hosts[1], "e.f.g"); |
| 53 EXPECT_EQ(hosts[2], "d.e.f.g"); |
| 54 EXPECT_EQ(hosts[3], "c.d.e.f.g"); |
| 55 EXPECT_EQ(hosts[4], "a.b.c.d.e.f.g"); |
| 56 EXPECT_TRUE(VectorContains(paths, "/1.html")); |
| 57 EXPECT_TRUE(VectorContains(paths, "/")); |
| 58 |
| 59 url = GURL("http://a.b/saw-cgi/eBayISAPI.dll/"); |
| 60 GeneratePathsToCheck(url, &paths); |
| 61 EXPECT_EQ(paths.size(), static_cast<size_t>(3)); |
| 62 EXPECT_TRUE(VectorContains(paths, "/saw-cgi/eBayISAPI.dll/")); |
| 63 EXPECT_TRUE(VectorContains(paths, "/saw-cgi/")); |
| 64 EXPECT_TRUE(VectorContains(paths, "/")); |
| 65 } |
| 66 |
| 67 // Tests the url canonicalization according to the Safe Browsing spec. |
| 68 // See section 6.1 in |
| 69 // http://code.google.com/p/google-safe-browsing/wiki/Protocolv2Spec. |
| 70 TEST(SafeBrowsingDbUtilTest, CanonicalizeUrl) { |
| 71 struct { |
| 72 const char* input_url; |
| 73 const char* expected_canonicalized_hostname; |
| 74 const char* expected_canonicalized_path; |
| 75 const char* expected_canonicalized_query; |
| 76 } tests[] = { |
| 77 { |
| 78 "http://host/%25%32%35", |
| 79 "host", |
| 80 "/%25", |
| 81 "" |
| 82 }, { |
| 83 "http://host/%25%32%35%25%32%35", |
| 84 "host", |
| 85 "/%25%25", |
| 86 "" |
| 87 }, { |
| 88 "http://host/%2525252525252525", |
| 89 "host", |
| 90 "/%25", |
| 91 "" |
| 92 }, { |
| 93 "http://host/asdf%25%32%35asd", |
| 94 "host", |
| 95 "/asdf%25asd", |
| 96 "" |
| 97 }, { |
| 98 "http://host/%%%25%32%35asd%%", |
| 99 "host", |
| 100 "/%25%25%25asd%25%25", |
| 101 "" |
| 102 }, { |
| 103 "http://host/%%%25%32%35asd%%", |
| 104 "host", |
| 105 "/%25%25%25asd%25%25", |
| 106 "" |
| 107 }, { |
| 108 "http://www.google.com/", |
| 109 "www.google.com", |
| 110 "/", |
| 111 "" |
| 112 }, { |
| 113 "http://%31%36%38%2e%31%38%38%2e%39%39%2e%32%36/%2E%73%65%63%75%72%65/%77" |
| 114 "%77%77%2E%65%62%61%79%2E%63%6F%6D/", |
| 115 "168.188.99.26", |
| 116 "/.secure/www.ebay.com/", |
| 117 "" |
| 118 }, { |
| 119 "http://195.127.0.11/uploads/%20%20%20%20/.verify/.eBaysecure=updateuserd" |
| 120 "ataxplimnbqmn-xplmvalidateinfoswqpcmlx=hgplmcx/", |
| 121 "195.127.0.11", |
| 122 "/uploads/%20%20%20%20/.verify/.eBaysecure=updateuserdataxplimnbqmn-xplmv" |
| 123 "alidateinfoswqpcmlx=hgplmcx/", |
| 124 "" |
| 125 }, { |
| 126 "http://host.com/%257Ea%2521b%2540c%2523d%2524e%25f%255E00%252611%252A" |
| 127 "22%252833%252944_55%252B", |
| 128 "host.com", |
| 129 "/~a!b@c%23d$e%25f^00&11*22(33)44_55+", |
| 130 "" |
| 131 }, { |
| 132 "http://3279880203/blah", |
| 133 "195.127.0.11", |
| 134 "/blah", |
| 135 "" |
| 136 }, { |
| 137 "http://www.google.com/blah/..", |
| 138 "www.google.com", |
| 139 "/", |
| 140 "" |
| 141 }, { |
| 142 "http://www.google.com/blah#fraq", |
| 143 "www.google.com", |
| 144 "/blah", |
| 145 "" |
| 146 }, { |
| 147 "http://www.GOOgle.com/", |
| 148 "www.google.com", |
| 149 "/", |
| 150 "" |
| 151 }, { |
| 152 "http://www.google.com.../", |
| 153 "www.google.com", |
| 154 "/", |
| 155 "" |
| 156 }, { |
| 157 "http://www.google.com/q?", |
| 158 "www.google.com", |
| 159 "/q", |
| 160 "" |
| 161 }, { |
| 162 "http://www.google.com/q?r?", |
| 163 "www.google.com", |
| 164 "/q", |
| 165 "r?" |
| 166 }, { |
| 167 "http://www.google.com/q?r?s", |
| 168 "www.google.com", |
| 169 "/q", |
| 170 "r?s" |
| 171 }, { |
| 172 "http://evil.com/foo#bar#baz", |
| 173 "evil.com", |
| 174 "/foo", |
| 175 "" |
| 176 }, { |
| 177 "http://evil.com/foo;", |
| 178 "evil.com", |
| 179 "/foo;", |
| 180 "" |
| 181 }, { |
| 182 "http://evil.com/foo?bar;", |
| 183 "evil.com", |
| 184 "/foo", |
| 185 "bar;" |
| 186 }, { |
| 187 "http://notrailingslash.com", |
| 188 "notrailingslash.com", |
| 189 "/", |
| 190 "" |
| 191 }, { |
| 192 "http://www.gotaport.com:1234/", |
| 193 "www.gotaport.com", |
| 194 "/", |
| 195 "" |
| 196 }, { |
| 197 " http://www.google.com/ ", |
| 198 "www.google.com", |
| 199 "/", |
| 200 "" |
| 201 }, { |
| 202 "http:// leadingspace.com/", |
| 203 "%20leadingspace.com", |
| 204 "/", |
| 205 "" |
| 206 }, { |
| 207 "http://%20leadingspace.com/", |
| 208 "%20leadingspace.com", |
| 209 "/", |
| 210 "" |
| 211 }, { |
| 212 "https://www.securesite.com/", |
| 213 "www.securesite.com", |
| 214 "/", |
| 215 "" |
| 216 }, { |
| 217 "http://host.com/ab%23cd", |
| 218 "host.com", |
| 219 "/ab%23cd", |
| 220 "" |
| 221 }, { |
| 222 "http://host%3e.com//twoslashes?more//slashes", |
| 223 "host>.com", |
| 224 "/twoslashes", |
| 225 "more//slashes" |
| 226 }, { |
| 227 "http://host.com/abc?val=xyz#anything", |
| 228 "host.com", |
| 229 "/abc", |
| 230 "val=xyz" |
| 231 }, { |
| 232 "http://abc:def@host.com/xyz", |
| 233 "host.com", |
| 234 "/xyz", |
| 235 "" |
| 236 }, { |
| 237 "http://host%3e.com/abc/%2e%2e%2fdef", |
| 238 "host>.com", |
| 239 "/def", |
| 240 "" |
| 241 }, { |
| 242 "http://.......host...com.....//abc/////def%2F%2F%2Fxyz", |
| 243 "host.com", |
| 244 "/abc/def/xyz", |
| 245 "" |
| 246 }, { |
| 247 "ftp://host.com/foo?bar", |
| 248 "host.com", |
| 249 "/foo", |
| 250 "bar" |
| 251 }, { |
| 252 "data:text/html;charset=utf-8,%0D%0A", |
| 253 "", |
| 254 "", |
| 255 "" |
| 256 }, { |
| 257 "javascript:alert()", |
| 258 "", |
| 259 "", |
| 260 "" |
| 261 }, { |
| 262 "mailto:abc@example.com", |
| 263 "", |
| 264 "", |
| 265 "" |
| 266 }, |
| 267 }; |
| 268 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 269 SCOPED_TRACE(base::StringPrintf("Test: %s", tests[i].input_url)); |
| 270 GURL url(tests[i].input_url); |
| 271 |
| 272 std::string canonicalized_hostname; |
| 273 std::string canonicalized_path; |
| 274 std::string canonicalized_query; |
| 275 CanonicalizeUrl(url, &canonicalized_hostname, &canonicalized_path, |
| 276 &canonicalized_query); |
| 277 |
| 278 EXPECT_EQ(tests[i].expected_canonicalized_hostname, canonicalized_hostname); |
| 279 EXPECT_EQ(tests[i].expected_canonicalized_path, canonicalized_path); |
| 280 EXPECT_EQ(tests[i].expected_canonicalized_query, canonicalized_query); |
| 281 } |
| 282 } |
| 283 |
17 TEST(SafeBrowsingDbUtilTest, UrlToFullHashes) { | 284 TEST(SafeBrowsingDbUtilTest, UrlToFullHashes) { |
18 std::vector<SBFullHash> results; | 285 std::vector<SBFullHash> results; |
19 GURL url("http://www.evil.com/evil1/evilness.html"); | 286 GURL url("http://www.evil.com/evil1/evilness.html"); |
20 UrlToFullHashes(url, false, &results); | 287 UrlToFullHashes(url, false, &results); |
21 | 288 |
22 EXPECT_EQ(6UL, results.size()); | 289 EXPECT_EQ(6UL, results.size()); |
23 EXPECT_TRUE(SBFullHashEqual(SBFullHashForString("evil.com/"), | 290 EXPECT_TRUE(SBFullHashEqual(SBFullHashForString("evil.com/"), |
24 results[0])); | 291 results[0])); |
25 EXPECT_TRUE(SBFullHashEqual(SBFullHashForString("evil.com/evil1/"), | 292 EXPECT_TRUE(SBFullHashEqual(SBFullHashForString("evil.com/evil1/"), |
26 results[1])); | 293 results[1])); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 EXPECT_FALSE(SBFullHashEqual(kHash2, kHash1)); | 369 EXPECT_FALSE(SBFullHashEqual(kHash2, kHash1)); |
103 | 370 |
104 EXPECT_FALSE(SBFullHashLess(kHash1, kHash2)); | 371 EXPECT_FALSE(SBFullHashLess(kHash1, kHash2)); |
105 EXPECT_TRUE(SBFullHashLess(kHash2, kHash1)); | 372 EXPECT_TRUE(SBFullHashLess(kHash2, kHash1)); |
106 | 373 |
107 EXPECT_FALSE(SBFullHashLess(kHash1, kHash1)); | 374 EXPECT_FALSE(SBFullHashLess(kHash1, kHash1)); |
108 EXPECT_FALSE(SBFullHashLess(kHash2, kHash2)); | 375 EXPECT_FALSE(SBFullHashLess(kHash2, kHash2)); |
109 } | 376 } |
110 | 377 |
111 } // namespace safe_browsing | 378 } // namespace safe_browsing |
OLD | NEW |