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