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 // Unit tests for the SafeBrowsing storage system. | 5 // Unit tests for the SafeBrowsing storage system. |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
12 #include "chrome/browser/safe_browsing/safe_browsing_database.h" | 12 #include "chrome/browser/safe_browsing/safe_browsing_database.h" |
13 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h" | 13 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h" |
14 #include "chrome/browser/safe_browsing/safe_browsing_store_unittest_helper.h" | 14 #include "chrome/browser/safe_browsing/safe_browsing_store_unittest_helper.h" |
15 #include "content/public/test/test_browser_thread.h" | 15 #include "content/public/test/test_browser_thread_bundle.h" |
16 #include "crypto/sha2.h" | 16 #include "crypto/sha2.h" |
17 #include "sql/connection.h" | 17 #include "sql/connection.h" |
18 #include "sql/statement.h" | 18 #include "sql/statement.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
20 #include "testing/platform_test.h" | 20 #include "testing/platform_test.h" |
21 #include "url/gurl.h" | 21 #include "url/gurl.h" |
22 | 22 |
23 using base::Time; | 23 using base::Time; |
24 using content::BrowserThread; | |
25 | 24 |
26 namespace { | 25 namespace { |
27 | 26 |
28 SBPrefix Sha256Prefix(const std::string& str) { | 27 SBPrefix Sha256Prefix(const std::string& str) { |
29 SBPrefix prefix; | 28 SBPrefix prefix; |
30 crypto::SHA256HashString(str, &prefix, sizeof(prefix)); | 29 crypto::SHA256HashString(str, &prefix, sizeof(prefix)); |
31 return prefix; | 30 return prefix; |
32 } | 31 } |
33 | 32 |
34 SBFullHash Sha256Hash(const std::string& str) { | 33 SBFullHash Sha256Hash(const std::string& str) { |
(...skipping 1210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1245 EXPECT_TRUE(database_->ContainsDownloadUrl(urls, &prefix_hits)); | 1244 EXPECT_TRUE(database_->ContainsDownloadUrl(urls, &prefix_hits)); |
1246 ASSERT_EQ(prefix_hits.size(), 2U); | 1245 ASSERT_EQ(prefix_hits.size(), 2U); |
1247 EXPECT_EQ(prefix_hits[0], Sha256Prefix(kEvil1Url1)); | 1246 EXPECT_EQ(prefix_hits[0], Sha256Prefix(kEvil1Url1)); |
1248 EXPECT_EQ(prefix_hits[1], Sha256Prefix(kEvil1Url2)); | 1247 EXPECT_EQ(prefix_hits[1], Sha256Prefix(kEvil1Url2)); |
1249 database_.reset(); | 1248 database_.reset(); |
1250 } | 1249 } |
1251 | 1250 |
1252 // Checks that the whitelists are handled properly. | 1251 // Checks that the whitelists are handled properly. |
1253 TEST_F(SafeBrowsingDatabaseTest, Whitelists) { | 1252 TEST_F(SafeBrowsingDatabaseTest, Whitelists) { |
1254 database_.reset(); | 1253 database_.reset(); |
1255 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); | |
1256 // We expect all calls to ContainsCsdWhitelistedUrl in particular to be made | 1254 // We expect all calls to ContainsCsdWhitelistedUrl in particular to be made |
1257 // from the IO thread. In general the whitelist lookups are thread-safe. | 1255 // from the IO thread. In general the whitelist lookups are thread-safe. |
1258 content::TestBrowserThread io_thread(BrowserThread::IO, &loop); | 1256 content::TestBrowserThreadBundle thread_bundle_; |
1259 | 1257 |
1260 // If the whitelist is disabled everything should match the whitelist. | 1258 // If the whitelist is disabled everything should match the whitelist. |
1261 database_.reset(new SafeBrowsingDatabaseNew(new SafeBrowsingStoreFile(), | 1259 database_.reset(new SafeBrowsingDatabaseNew(new SafeBrowsingStoreFile(), |
1262 NULL, NULL, NULL, NULL, NULL)); | 1260 NULL, NULL, NULL, NULL, NULL)); |
1263 database_->Init(database_filename_); | 1261 database_->Init(database_filename_); |
1264 EXPECT_TRUE(database_->ContainsDownloadWhitelistedUrl( | 1262 EXPECT_TRUE(database_->ContainsDownloadWhitelistedUrl( |
1265 GURL(std::string("http://www.phishing.com/")))); | 1263 GURL(std::string("http://www.phishing.com/")))); |
1266 EXPECT_TRUE(database_->ContainsDownloadWhitelistedUrl( | 1264 EXPECT_TRUE(database_->ContainsDownloadWhitelistedUrl( |
1267 GURL(std::string("http://www.phishing.com/")))); | 1265 GURL(std::string("http://www.phishing.com/")))); |
1268 EXPECT_TRUE(database_->ContainsDownloadWhitelistedString("asdf")); | 1266 EXPECT_TRUE(database_->ContainsDownloadWhitelistedString("asdf")); |
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1662 ASSERT_FALSE(base::PathExists(filter_file)); | 1660 ASSERT_FALSE(base::PathExists(filter_file)); |
1663 database_.reset(new SafeBrowsingDatabaseNew); | 1661 database_.reset(new SafeBrowsingDatabaseNew); |
1664 database_->Init(database_filename_); | 1662 database_->Init(database_filename_); |
1665 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1663 EXPECT_FALSE(database_->ContainsBrowseUrl( |
1666 GURL("http://www.evil.com/malware.html"), | 1664 GURL("http://www.evil.com/malware.html"), |
1667 &matching_list, &prefix_hits, &full_hashes, now)); | 1665 &matching_list, &prefix_hits, &full_hashes, now)); |
1668 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1666 EXPECT_FALSE(database_->ContainsBrowseUrl( |
1669 GURL("http://www.good.com/goodware.html"), | 1667 GURL("http://www.good.com/goodware.html"), |
1670 &matching_list, &prefix_hits, &full_hashes, now)); | 1668 &matching_list, &prefix_hits, &full_hashes, now)); |
1671 } | 1669 } |
OLD | NEW |