OLD | NEW |
1 // Copyright (c) 2011 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 <algorithm> | 5 #include <algorithm> |
6 | 6 |
7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
8 #include "chrome/browser/safe_browsing/safe_browsing_util.h" | 8 #include "components/safe_browsing_db/util.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "url/gurl.h" | 10 #include "url/gurl.h" |
11 | 11 |
12 namespace { | 12 namespace { |
13 | 13 |
14 bool VectorContains(const std::vector<std::string>& data, | 14 bool VectorContains(const std::vector<std::string>& data, |
15 const std::string& str) { | 15 const std::string& str) { |
16 return std::find(data.begin(), data.end(), str) != data.end(); | 16 return std::find(data.begin(), data.end(), str) != data.end(); |
17 } | 17 } |
18 | 18 |
19 // Tests that we generate the required host/path combinations for testing | 19 // Tests that we generate the required host/path combinations for testing |
20 // according to the Safe Browsing spec. | 20 // according to the Safe Browsing spec. |
21 // See section 6.2 in | 21 // See section 6.2 in |
22 // http://code.google.com/p/google-safe-browsing/wiki/Protocolv2Spec. | 22 // http://code.google.com/p/google-safe-browsing/wiki/Protocolv2Spec. |
23 TEST(SafeBrowsingUtilTest, UrlParsing) { | 23 TEST(SafeBrowsingDbUtilTest, UrlParsing) { |
24 std::vector<std::string> hosts, paths; | 24 std::vector<std::string> hosts, paths; |
25 | 25 |
26 GURL url("http://a.b.c/1/2.html?param=1"); | 26 GURL url("http://a.b.c/1/2.html?param=1"); |
27 safe_browsing_util::GenerateHostsToCheck(url, &hosts); | 27 safe_browsing::GenerateHostsToCheck(url, &hosts); |
28 safe_browsing_util::GeneratePathsToCheck(url, &paths); | 28 safe_browsing::GeneratePathsToCheck(url, &paths); |
29 EXPECT_EQ(hosts.size(), static_cast<size_t>(2)); | 29 EXPECT_EQ(hosts.size(), static_cast<size_t>(2)); |
30 EXPECT_EQ(paths.size(), static_cast<size_t>(4)); | 30 EXPECT_EQ(paths.size(), static_cast<size_t>(4)); |
31 EXPECT_EQ(hosts[0], "b.c"); | 31 EXPECT_EQ(hosts[0], "b.c"); |
32 EXPECT_EQ(hosts[1], "a.b.c"); | 32 EXPECT_EQ(hosts[1], "a.b.c"); |
33 | 33 |
34 EXPECT_TRUE(VectorContains(paths, "/1/2.html?param=1")); | 34 EXPECT_TRUE(VectorContains(paths, "/1/2.html?param=1")); |
35 EXPECT_TRUE(VectorContains(paths, "/1/2.html")); | 35 EXPECT_TRUE(VectorContains(paths, "/1/2.html")); |
36 EXPECT_TRUE(VectorContains(paths, "/1/")); | 36 EXPECT_TRUE(VectorContains(paths, "/1/")); |
37 EXPECT_TRUE(VectorContains(paths, "/")); | 37 EXPECT_TRUE(VectorContains(paths, "/")); |
38 | 38 |
39 url = GURL("http://a.b.c.d.e.f.g/1.html"); | 39 url = GURL("http://a.b.c.d.e.f.g/1.html"); |
40 safe_browsing_util::GenerateHostsToCheck(url, &hosts); | 40 safe_browsing::GenerateHostsToCheck(url, &hosts); |
41 safe_browsing_util::GeneratePathsToCheck(url, &paths); | 41 safe_browsing::GeneratePathsToCheck(url, &paths); |
42 EXPECT_EQ(hosts.size(), static_cast<size_t>(5)); | 42 EXPECT_EQ(hosts.size(), static_cast<size_t>(5)); |
43 EXPECT_EQ(paths.size(), static_cast<size_t>(2)); | 43 EXPECT_EQ(paths.size(), static_cast<size_t>(2)); |
44 EXPECT_EQ(hosts[0], "f.g"); | 44 EXPECT_EQ(hosts[0], "f.g"); |
45 EXPECT_EQ(hosts[1], "e.f.g"); | 45 EXPECT_EQ(hosts[1], "e.f.g"); |
46 EXPECT_EQ(hosts[2], "d.e.f.g"); | 46 EXPECT_EQ(hosts[2], "d.e.f.g"); |
47 EXPECT_EQ(hosts[3], "c.d.e.f.g"); | 47 EXPECT_EQ(hosts[3], "c.d.e.f.g"); |
48 EXPECT_EQ(hosts[4], "a.b.c.d.e.f.g"); | 48 EXPECT_EQ(hosts[4], "a.b.c.d.e.f.g"); |
49 EXPECT_TRUE(VectorContains(paths, "/1.html")); | 49 EXPECT_TRUE(VectorContains(paths, "/1.html")); |
50 EXPECT_TRUE(VectorContains(paths, "/")); | 50 EXPECT_TRUE(VectorContains(paths, "/")); |
51 | 51 |
52 url = GURL("http://a.b/saw-cgi/eBayISAPI.dll/"); | 52 url = GURL("http://a.b/saw-cgi/eBayISAPI.dll/"); |
53 safe_browsing_util::GeneratePathsToCheck(url, &paths); | 53 safe_browsing::GeneratePathsToCheck(url, &paths); |
54 EXPECT_EQ(paths.size(), static_cast<size_t>(3)); | 54 EXPECT_EQ(paths.size(), static_cast<size_t>(3)); |
55 EXPECT_TRUE(VectorContains(paths, "/saw-cgi/eBayISAPI.dll/")); | 55 EXPECT_TRUE(VectorContains(paths, "/saw-cgi/eBayISAPI.dll/")); |
56 EXPECT_TRUE(VectorContains(paths, "/saw-cgi/")); | 56 EXPECT_TRUE(VectorContains(paths, "/saw-cgi/")); |
57 EXPECT_TRUE(VectorContains(paths, "/")); | 57 EXPECT_TRUE(VectorContains(paths, "/")); |
58 } | 58 } |
59 | 59 |
60 // Tests the url canonicalization according to the Safe Browsing spec. | 60 // Tests the url canonicalization according to the Safe Browsing spec. |
61 // See section 6.1 in | 61 // See section 6.1 in |
62 // http://code.google.com/p/google-safe-browsing/wiki/Protocolv2Spec. | 62 // http://code.google.com/p/google-safe-browsing/wiki/Protocolv2Spec. |
63 TEST(SafeBrowsingUtilTest, CanonicalizeUrl) { | 63 TEST(SafeBrowsingDbUtilTest, CanonicalizeUrl) { |
64 struct { | 64 struct { |
65 const char* input_url; | 65 const char* input_url; |
66 const char* expected_canonicalized_hostname; | 66 const char* expected_canonicalized_hostname; |
67 const char* expected_canonicalized_path; | 67 const char* expected_canonicalized_path; |
68 const char* expected_canonicalized_query; | 68 const char* expected_canonicalized_query; |
69 } tests[] = { | 69 } tests[] = { |
70 { | 70 { |
71 "http://host/%25%32%35", | 71 "http://host/%25%32%35", |
72 "host", | 72 "host", |
73 "/%25", | 73 "/%25", |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 "" | 258 "" |
259 }, | 259 }, |
260 }; | 260 }; |
261 for (size_t i = 0; i < arraysize(tests); ++i) { | 261 for (size_t i = 0; i < arraysize(tests); ++i) { |
262 SCOPED_TRACE(base::StringPrintf("Test: %s", tests[i].input_url)); | 262 SCOPED_TRACE(base::StringPrintf("Test: %s", tests[i].input_url)); |
263 GURL url(tests[i].input_url); | 263 GURL url(tests[i].input_url); |
264 | 264 |
265 std::string canonicalized_hostname; | 265 std::string canonicalized_hostname; |
266 std::string canonicalized_path; | 266 std::string canonicalized_path; |
267 std::string canonicalized_query; | 267 std::string canonicalized_query; |
268 safe_browsing_util::CanonicalizeUrl(url, &canonicalized_hostname, | 268 safe_browsing::CanonicalizeUrl(url, &canonicalized_hostname, |
269 &canonicalized_path, &canonicalized_query); | 269 &canonicalized_path, &canonicalized_query); |
270 | 270 |
271 EXPECT_EQ(tests[i].expected_canonicalized_hostname, | 271 EXPECT_EQ(tests[i].expected_canonicalized_hostname, |
272 canonicalized_hostname); | 272 canonicalized_hostname); |
273 EXPECT_EQ(tests[i].expected_canonicalized_path, | 273 EXPECT_EQ(tests[i].expected_canonicalized_path, |
274 canonicalized_path); | 274 canonicalized_path); |
275 EXPECT_EQ(tests[i].expected_canonicalized_query, | 275 EXPECT_EQ(tests[i].expected_canonicalized_query, |
276 canonicalized_query); | 276 canonicalized_query); |
277 } | 277 } |
278 } | 278 } |
279 | 279 |
280 TEST(SafeBrowsingUtilTest, ListIdListNameConversion) { | 280 TEST(SafeBrowsingDbUtilTest, ListIdListNameConversion) { |
281 std::string list_name; | 281 std::string list_name; |
282 EXPECT_FALSE(safe_browsing_util::GetListName(safe_browsing_util::INVALID, | 282 EXPECT_FALSE(safe_browsing::GetListName(safe_browsing::INVALID, |
283 &list_name)); | 283 &list_name)); |
284 EXPECT_TRUE(safe_browsing_util::GetListName(safe_browsing_util::MALWARE, | 284 EXPECT_TRUE(safe_browsing::GetListName(safe_browsing::MALWARE, |
285 &list_name)); | 285 &list_name)); |
286 EXPECT_EQ(list_name, std::string(safe_browsing_util::kMalwareList)); | 286 EXPECT_EQ(list_name, std::string(safe_browsing::kMalwareList)); |
287 EXPECT_EQ(safe_browsing_util::MALWARE, | 287 EXPECT_EQ(safe_browsing::MALWARE, |
288 safe_browsing_util::GetListId(list_name)); | 288 safe_browsing::GetListId(list_name)); |
289 | 289 |
290 EXPECT_TRUE(safe_browsing_util::GetListName(safe_browsing_util::PHISH, | 290 EXPECT_TRUE(safe_browsing::GetListName(safe_browsing::PHISH, |
291 &list_name)); | 291 &list_name)); |
292 EXPECT_EQ(list_name, std::string(safe_browsing_util::kPhishingList)); | 292 EXPECT_EQ(list_name, std::string(safe_browsing::kPhishingList)); |
293 EXPECT_EQ(safe_browsing_util::PHISH, | 293 EXPECT_EQ(safe_browsing::PHISH, |
294 safe_browsing_util::GetListId(list_name)); | 294 safe_browsing::GetListId(list_name)); |
295 | 295 |
296 EXPECT_TRUE(safe_browsing_util::GetListName(safe_browsing_util::BINURL, | 296 EXPECT_TRUE(safe_browsing::GetListName(safe_browsing::BINURL, |
297 &list_name)); | 297 &list_name)); |
298 EXPECT_EQ(list_name, std::string(safe_browsing_util::kBinUrlList)); | 298 EXPECT_EQ(list_name, std::string(safe_browsing::kBinUrlList)); |
299 EXPECT_EQ(safe_browsing_util::BINURL, | 299 EXPECT_EQ(safe_browsing::BINURL, |
300 safe_browsing_util::GetListId(list_name)); | 300 safe_browsing::GetListId(list_name)); |
301 } | 301 } |
302 | 302 |
303 // Since the ids are saved in file, we need to make sure they don't change. | 303 // Since the ids are saved in file, we need to make sure they don't change. |
304 // Since only the last bit of each id is saved in file together with | 304 // Since only the last bit of each id is saved in file together with |
305 // chunkids, this checks only last bit. | 305 // chunkids, this checks only last bit. |
306 TEST(SafeBrowsingUtilTest, ListIdVerification) { | 306 TEST(SafeBrowsingDbUtilTest, ListIdVerification) { |
307 EXPECT_EQ(0, safe_browsing_util::MALWARE % 2); | 307 EXPECT_EQ(0, safe_browsing::MALWARE % 2); |
308 EXPECT_EQ(1, safe_browsing_util::PHISH % 2); | 308 EXPECT_EQ(1, safe_browsing::PHISH % 2); |
309 EXPECT_EQ(0, safe_browsing_util::BINURL %2); | 309 EXPECT_EQ(0, safe_browsing::BINURL %2); |
310 } | 310 } |
311 | 311 |
312 TEST(SafeBrowsingUtilTest, StringToSBFullHashAndSBFullHashToString) { | 312 TEST(SafeBrowsingDbUtilTest, StringToSBFullHashAndSBFullHashToString) { |
313 // 31 chars plus the last \0 as full_hash. | 313 // 31 chars plus the last \0 as full_hash. |
314 const std::string hash_in = "12345678902234567890323456789012"; | 314 const std::string hash_in = "12345678902234567890323456789012"; |
315 SBFullHash hash_out = safe_browsing_util::StringToSBFullHash(hash_in); | 315 SBFullHash hash_out = safe_browsing::StringToSBFullHash(hash_in); |
316 EXPECT_EQ(0x34333231U, hash_out.prefix); | 316 EXPECT_EQ(0x34333231U, hash_out.prefix); |
317 EXPECT_EQ(0, memcmp(hash_in.data(), hash_out.full_hash, sizeof(SBFullHash))); | 317 EXPECT_EQ(0, memcmp(hash_in.data(), hash_out.full_hash, sizeof(SBFullHash))); |
318 | 318 |
319 std::string hash_final = safe_browsing_util::SBFullHashToString(hash_out); | 319 std::string hash_final = safe_browsing::SBFullHashToString(hash_out); |
320 EXPECT_EQ(hash_in, hash_final); | 320 EXPECT_EQ(hash_in, hash_final); |
321 } | 321 } |
322 | 322 |
| 323 TEST(SafeBrowsingDbUtilTest, FullHashOperators) { |
| 324 const SBFullHash kHash1 = safe_browsing::SBFullHashForString("one"); |
| 325 const SBFullHash kHash2 = safe_browsing::SBFullHashForString("two"); |
| 326 |
| 327 EXPECT_TRUE(safe_browsing::SBFullHashEqual(kHash1, kHash1)); |
| 328 EXPECT_TRUE(safe_browsing::SBFullHashEqual(kHash2, kHash2)); |
| 329 EXPECT_FALSE(safe_browsing::SBFullHashEqual(kHash1, kHash2)); |
| 330 EXPECT_FALSE(safe_browsing::SBFullHashEqual(kHash2, kHash1)); |
| 331 |
| 332 EXPECT_FALSE(safe_browsing::SBFullHashLess(kHash1, kHash2)); |
| 333 EXPECT_TRUE(safe_browsing::SBFullHashLess(kHash2, kHash1)); |
| 334 |
| 335 EXPECT_FALSE(safe_browsing::SBFullHashLess(kHash1, kHash1)); |
| 336 EXPECT_FALSE(safe_browsing::SBFullHashLess(kHash2, kHash2)); |
| 337 } |
| 338 |
323 } // namespace | 339 } // namespace |
OLD | NEW |