| 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 // This test creates a safebrowsing service using test safebrowsing database | 5 // This test creates a safebrowsing service using test safebrowsing database |
| 6 // and a test protocol manager. It is used to test logics in safebrowsing | 6 // and a test protocol manager. It is used to test logics in safebrowsing |
| 7 // service. | 7 // service. |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 } // namespace | 63 } // namespace |
| 64 | 64 |
| 65 // A SafeBrowingDatabase class that allows us to inject the malicious URLs. | 65 // A SafeBrowingDatabase class that allows us to inject the malicious URLs. |
| 66 class TestSafeBrowsingDatabase : public SafeBrowsingDatabase { | 66 class TestSafeBrowsingDatabase : public SafeBrowsingDatabase { |
| 67 public: | 67 public: |
| 68 TestSafeBrowsingDatabase() {} | 68 TestSafeBrowsingDatabase() {} |
| 69 | 69 |
| 70 virtual ~TestSafeBrowsingDatabase() {} | 70 virtual ~TestSafeBrowsingDatabase() {} |
| 71 | 71 |
| 72 // Initializes the database with the given filename. | 72 // Initializes the database with the given filename. |
| 73 virtual void Init(const FilePath& filename) {} | 73 virtual void Init(const FilePath& filename) OVERRIDE {} |
| 74 | 74 |
| 75 // Deletes the current database and creates a new one. | 75 // Deletes the current database and creates a new one. |
| 76 virtual bool ResetDatabase() { | 76 virtual bool ResetDatabase() OVERRIDE { |
| 77 badurls_.clear(); | 77 badurls_.clear(); |
| 78 return true; | 78 return true; |
| 79 } | 79 } |
| 80 | 80 |
| 81 // Called on the IO thread to check if the given URL is safe or not. If we | 81 // Called on the IO thread to check if the given URL is safe or not. If we |
| 82 // can synchronously determine that the URL is safe, CheckUrl returns true, | 82 // can synchronously determine that the URL is safe, CheckUrl returns true, |
| 83 // otherwise it returns false. | 83 // otherwise it returns false. |
| 84 virtual bool ContainsBrowseUrl(const GURL& url, | 84 virtual bool ContainsBrowseUrl(const GURL& url, |
| 85 std::string* matching_list, | 85 std::string* matching_list, |
| 86 std::vector<SBPrefix>* prefix_hits, | 86 std::vector<SBPrefix>* prefix_hits, |
| 87 std::vector<SBFullHashResult>* full_hits, | 87 std::vector<SBFullHashResult>* full_hits, |
| 88 base::Time last_update) { | 88 base::Time last_update) OVERRIDE { |
| 89 std::vector<GURL> urls(1, url); | 89 std::vector<GURL> urls(1, url); |
| 90 return ContainsUrl(safe_browsing_util::kMalwareList, | 90 return ContainsUrl(safe_browsing_util::kMalwareList, |
| 91 safe_browsing_util::kPhishingList, | 91 safe_browsing_util::kPhishingList, |
| 92 urls, prefix_hits, full_hits); | 92 urls, prefix_hits, full_hits); |
| 93 } | 93 } |
| 94 virtual bool ContainsDownloadUrl(const std::vector<GURL>& urls, | 94 virtual bool ContainsDownloadUrl( |
| 95 std::vector<SBPrefix>* prefix_hits) { | 95 const std::vector<GURL>& urls, |
| 96 std::vector<SBPrefix>* prefix_hits) OVERRIDE { |
| 96 std::vector<SBFullHashResult> full_hits; | 97 std::vector<SBFullHashResult> full_hits; |
| 97 bool found = ContainsUrl(safe_browsing_util::kBinUrlList, | 98 bool found = ContainsUrl(safe_browsing_util::kBinUrlList, |
| 98 safe_browsing_util::kBinHashList, | 99 safe_browsing_util::kBinHashList, |
| 99 urls, prefix_hits, &full_hits); | 100 urls, prefix_hits, &full_hits); |
| 100 if (!found) | 101 if (!found) |
| 101 return false; | 102 return false; |
| 102 DCHECK_LE(1U, prefix_hits->size()); | 103 DCHECK_LE(1U, prefix_hits->size()); |
| 103 return true; | 104 return true; |
| 104 } | 105 } |
| 105 virtual bool ContainsDownloadHashPrefix(const SBPrefix& prefix) { | 106 virtual bool ContainsDownloadHashPrefix(const SBPrefix& prefix) OVERRIDE { |
| 106 return download_digest_prefix_.count(prefix) > 0; | 107 return download_digest_prefix_.count(prefix) > 0; |
| 107 } | 108 } |
| 108 virtual bool ContainsCsdWhitelistedUrl(const GURL& url) { | 109 virtual bool ContainsCsdWhitelistedUrl(const GURL& url) OVERRIDE { |
| 109 return true; | 110 return true; |
| 110 } | 111 } |
| 111 virtual bool ContainsDownloadWhitelistedString(const std::string& str) { | 112 virtual bool ContainsDownloadWhitelistedString( |
| 113 const std::string& str) OVERRIDE { |
| 112 return true; | 114 return true; |
| 113 } | 115 } |
| 114 virtual bool ContainsDownloadWhitelistedUrl(const GURL& url) { | 116 virtual bool ContainsDownloadWhitelistedUrl(const GURL& url) OVERRIDE { |
| 115 return true; | 117 return true; |
| 116 } | 118 } |
| 117 virtual bool ContainsExtensionPrefixes( | 119 virtual bool ContainsExtensionPrefixes( |
| 118 const std::vector<SBPrefix>& prefixes, | 120 const std::vector<SBPrefix>& prefixes, |
| 119 std::vector<SBPrefix>* prefix_hits) OVERRIDE { | 121 std::vector<SBPrefix>* prefix_hits) OVERRIDE { |
| 120 return true; | 122 return true; |
| 121 } | 123 } |
| 122 virtual bool UpdateStarted(std::vector<SBListChunkRanges>* lists) { | 124 virtual bool UpdateStarted(std::vector<SBListChunkRanges>* lists) OVERRIDE { |
| 123 ADD_FAILURE() << "Not implemented."; | 125 ADD_FAILURE() << "Not implemented."; |
| 124 return false; | 126 return false; |
| 125 } | 127 } |
| 126 virtual void InsertChunks(const std::string& list_name, | 128 virtual void InsertChunks(const std::string& list_name, |
| 127 const SBChunkList& chunks) { | 129 const SBChunkList& chunks) OVERRIDE { |
| 128 ADD_FAILURE() << "Not implemented."; | 130 ADD_FAILURE() << "Not implemented."; |
| 129 } | 131 } |
| 130 virtual void DeleteChunks(const std::vector<SBChunkDelete>& chunk_deletes) { | 132 virtual void DeleteChunks( |
| 133 const std::vector<SBChunkDelete>& chunk_deletes) OVERRIDE { |
| 131 ADD_FAILURE() << "Not implemented."; | 134 ADD_FAILURE() << "Not implemented."; |
| 132 } | 135 } |
| 133 virtual void UpdateFinished(bool update_succeeded) { | 136 virtual void UpdateFinished(bool update_succeeded) OVERRIDE { |
| 134 ADD_FAILURE() << "Not implemented."; | 137 ADD_FAILURE() << "Not implemented."; |
| 135 } | 138 } |
| 136 virtual void CacheHashResults(const std::vector<SBPrefix>& prefixes, | 139 virtual void CacheHashResults(const std::vector<SBPrefix>& prefixes, |
| 137 const std::vector<SBFullHashResult>& full_hits) { | 140 const std::vector<SBFullHashResult>& full_hits) OVERRIDE { |
| 138 // Do nothing for the cache. | 141 // Do nothing for the cache. |
| 139 } | 142 } |
| 140 | 143 |
| 141 // Fill up the database with test URL. | 144 // Fill up the database with test URL. |
| 142 void AddUrl(const GURL& url, | 145 void AddUrl(const GURL& url, |
| 143 const std::string& list_name, | 146 const std::string& list_name, |
| 144 const std::vector<SBPrefix>& prefix_hits, | 147 const std::vector<SBPrefix>& prefix_hits, |
| 145 const std::vector<SBFullHashResult>& full_hits) { | 148 const std::vector<SBFullHashResult>& full_hits) { |
| 146 badurls_[url.spec()].list_name = list_name; | 149 badurls_[url.spec()].list_name = list_name; |
| 147 badurls_[url.spec()].prefix_hits = prefix_hits; | 150 badurls_[url.spec()].prefix_hits = prefix_hits; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 // Factory that creates TestSafeBrowsingDatabase instances. | 199 // Factory that creates TestSafeBrowsingDatabase instances. |
| 197 class TestSafeBrowsingDatabaseFactory : public SafeBrowsingDatabaseFactory { | 200 class TestSafeBrowsingDatabaseFactory : public SafeBrowsingDatabaseFactory { |
| 198 public: | 201 public: |
| 199 TestSafeBrowsingDatabaseFactory() : db_(NULL) {} | 202 TestSafeBrowsingDatabaseFactory() : db_(NULL) {} |
| 200 virtual ~TestSafeBrowsingDatabaseFactory() {} | 203 virtual ~TestSafeBrowsingDatabaseFactory() {} |
| 201 | 204 |
| 202 virtual SafeBrowsingDatabase* CreateSafeBrowsingDatabase( | 205 virtual SafeBrowsingDatabase* CreateSafeBrowsingDatabase( |
| 203 bool enable_download_protection, | 206 bool enable_download_protection, |
| 204 bool enable_client_side_whitelist, | 207 bool enable_client_side_whitelist, |
| 205 bool enable_download_whitelist, | 208 bool enable_download_whitelist, |
| 206 bool enable_extension_blacklist) { | 209 bool enable_extension_blacklist) OVERRIDE { |
| 207 db_ = new TestSafeBrowsingDatabase(); | 210 db_ = new TestSafeBrowsingDatabase(); |
| 208 return db_; | 211 return db_; |
| 209 } | 212 } |
| 210 TestSafeBrowsingDatabase* GetDb() { | 213 TestSafeBrowsingDatabase* GetDb() { |
| 211 return db_; | 214 return db_; |
| 212 } | 215 } |
| 213 private: | 216 private: |
| 214 // Owned by the SafebrowsingService. | 217 // Owned by the SafebrowsingService. |
| 215 TestSafeBrowsingDatabase* db_; | 218 TestSafeBrowsingDatabase* db_; |
| 216 }; | 219 }; |
| 217 | 220 |
| 218 // A TestProtocolManager that could return fixed responses from | 221 // A TestProtocolManager that could return fixed responses from |
| 219 // safebrowsing server for testing purpose. | 222 // safebrowsing server for testing purpose. |
| 220 class TestProtocolManager : public SafeBrowsingProtocolManager { | 223 class TestProtocolManager : public SafeBrowsingProtocolManager { |
| 221 public: | 224 public: |
| 222 TestProtocolManager(SafeBrowsingProtocolManagerDelegate* delegate, | 225 TestProtocolManager(SafeBrowsingProtocolManagerDelegate* delegate, |
| 223 net::URLRequestContextGetter* request_context_getter, | 226 net::URLRequestContextGetter* request_context_getter, |
| 224 const SafeBrowsingProtocolConfig& config) | 227 const SafeBrowsingProtocolConfig& config) |
| 225 : SafeBrowsingProtocolManager(delegate, request_context_getter, config) { | 228 : SafeBrowsingProtocolManager(delegate, request_context_getter, config) { |
| 226 create_count_++; | 229 create_count_++; |
| 227 } | 230 } |
| 228 | 231 |
| 229 ~TestProtocolManager() { | 232 virtual ~TestProtocolManager() { |
| 230 delete_count_++; | 233 delete_count_++; |
| 231 } | 234 } |
| 232 | 235 |
| 233 // This function is called when there is a prefix hit in local safebrowsing | 236 // This function is called when there is a prefix hit in local safebrowsing |
| 234 // database and safebrowsing service issues a get hash request to backends. | 237 // database and safebrowsing service issues a get hash request to backends. |
| 235 // We return a result from the prefilled full_hashes_ hash_map to simulate | 238 // We return a result from the prefilled full_hashes_ hash_map to simulate |
| 236 // server's response. At the same time, latency is added to simulate real | 239 // server's response. At the same time, latency is added to simulate real |
| 237 // life network issues. | 240 // life network issues. |
| 238 virtual void GetFullHash( | 241 virtual void GetFullHash( |
| 239 const std::vector<SBPrefix>& prefixes, | 242 const std::vector<SBPrefix>& prefixes, |
| (...skipping 622 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 862 | 865 |
| 863 // End the test, shutting down the browser. | 866 // End the test, shutting down the browser. |
| 864 // SafeBrowsingServiceShutdownTest::TearDown will check the create_count and | 867 // SafeBrowsingServiceShutdownTest::TearDown will check the create_count and |
| 865 // delete_count again. | 868 // delete_count again. |
| 866 } | 869 } |
| 867 | 870 |
| 868 class SafeBrowsingDatabaseManagerCookieTest : public InProcessBrowserTest { | 871 class SafeBrowsingDatabaseManagerCookieTest : public InProcessBrowserTest { |
| 869 public: | 872 public: |
| 870 SafeBrowsingDatabaseManagerCookieTest() {} | 873 SafeBrowsingDatabaseManagerCookieTest() {} |
| 871 | 874 |
| 872 virtual void SetUpCommandLine(CommandLine* command_line) { | 875 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 873 // We need to start the test server to get the host&port in the url. | 876 // We need to start the test server to get the host&port in the url. |
| 874 ASSERT_TRUE(test_server()->Start()); | 877 ASSERT_TRUE(test_server()->Start()); |
| 875 | 878 |
| 876 // Makes sure the auto update is not triggered. This test will force the | 879 // Makes sure the auto update is not triggered. This test will force the |
| 877 // update when needed. | 880 // update when needed. |
| 878 command_line->AppendSwitch(switches::kSbDisableAutoUpdate); | 881 command_line->AppendSwitch(switches::kSbDisableAutoUpdate); |
| 879 | 882 |
| 880 // Point to the testing server for all SafeBrowsing requests. | 883 // Point to the testing server for all SafeBrowsing requests. |
| 881 GURL url_prefix = test_server()->GetURL( | 884 GURL url_prefix = test_server()->GetURL( |
| 882 "expect-and-set-cookie?expect=a%3db" | 885 "expect-and-set-cookie?expect=a%3db" |
| 883 "&set=c%3dd%3b%20Expires=Fri,%2001%20Jan%202038%2001:01:01%20GMT" | 886 "&set=c%3dd%3b%20Expires=Fri,%2001%20Jan%202038%2001:01:01%20GMT" |
| 884 "&data=foo#"); | 887 "&data=foo#"); |
| 885 command_line->AppendSwitchASCII(switches::kSbURLPrefix, url_prefix.spec()); | 888 command_line->AppendSwitchASCII(switches::kSbURLPrefix, url_prefix.spec()); |
| 886 } | 889 } |
| 887 | 890 |
| 888 virtual bool SetUpUserDataDirectory() { | 891 virtual bool SetUpUserDataDirectory() OVERRIDE { |
| 889 FilePath cookie_path(SafeBrowsingService::GetCookieFilePathForTesting()); | 892 FilePath cookie_path(SafeBrowsingService::GetCookieFilePathForTesting()); |
| 890 EXPECT_FALSE(file_util::PathExists(cookie_path)); | 893 EXPECT_FALSE(file_util::PathExists(cookie_path)); |
| 891 | 894 |
| 892 FilePath test_dir; | 895 FilePath test_dir; |
| 893 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_dir)) { | 896 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_dir)) { |
| 894 EXPECT_TRUE(false); | 897 EXPECT_TRUE(false); |
| 895 return false; | 898 return false; |
| 896 } | 899 } |
| 897 | 900 |
| 898 // Initialize the SafeBrowsing cookies with a pre-created cookie store. It | 901 // Initialize the SafeBrowsing cookies with a pre-created cookie store. It |
| (...skipping 24 matching lines...) Expand all Loading... |
| 923 return false; | 926 return false; |
| 924 } | 927 } |
| 925 if (!smt.Run()) { | 928 if (!smt.Run()) { |
| 926 EXPECT_TRUE(false); | 929 EXPECT_TRUE(false); |
| 927 return false; | 930 return false; |
| 928 } | 931 } |
| 929 | 932 |
| 930 return InProcessBrowserTest::SetUpUserDataDirectory(); | 933 return InProcessBrowserTest::SetUpUserDataDirectory(); |
| 931 } | 934 } |
| 932 | 935 |
| 933 virtual void TearDownInProcessBrowserTestFixture() { | 936 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE { |
| 934 InProcessBrowserTest::TearDownInProcessBrowserTestFixture(); | 937 InProcessBrowserTest::TearDownInProcessBrowserTestFixture(); |
| 935 | 938 |
| 936 sql::Connection db; | 939 sql::Connection db; |
| 937 FilePath cookie_path(SafeBrowsingService::GetCookieFilePathForTesting()); | 940 FilePath cookie_path(SafeBrowsingService::GetCookieFilePathForTesting()); |
| 938 ASSERT_TRUE(db.Open(cookie_path)); | 941 ASSERT_TRUE(db.Open(cookie_path)); |
| 939 | 942 |
| 940 sql::Statement smt(db.GetUniqueStatement( | 943 sql::Statement smt(db.GetUniqueStatement( |
| 941 "SELECT name, value FROM cookies ORDER BY name")); | 944 "SELECT name, value FROM cookies ORDER BY name")); |
| 942 ASSERT_TRUE(smt.is_valid()); | 945 ASSERT_TRUE(smt.is_valid()); |
| 943 | 946 |
| 944 ASSERT_TRUE(smt.Step()); | 947 ASSERT_TRUE(smt.Step()); |
| 945 ASSERT_EQ("a", smt.ColumnString(0)); | 948 ASSERT_EQ("a", smt.ColumnString(0)); |
| 946 ASSERT_EQ("b", smt.ColumnString(1)); | 949 ASSERT_EQ("b", smt.ColumnString(1)); |
| 947 ASSERT_TRUE(smt.Step()); | 950 ASSERT_TRUE(smt.Step()); |
| 948 ASSERT_EQ("c", smt.ColumnString(0)); | 951 ASSERT_EQ("c", smt.ColumnString(0)); |
| 949 ASSERT_EQ("d", smt.ColumnString(1)); | 952 ASSERT_EQ("d", smt.ColumnString(1)); |
| 950 EXPECT_FALSE(smt.Step()); | 953 EXPECT_FALSE(smt.Step()); |
| 951 } | 954 } |
| 952 | 955 |
| 953 virtual void SetUpOnMainThread() { | 956 virtual void SetUpOnMainThread() OVERRIDE { |
| 954 sb_service_ = g_browser_process->safe_browsing_service(); | 957 sb_service_ = g_browser_process->safe_browsing_service(); |
| 955 ASSERT_TRUE(sb_service_ != NULL); | 958 ASSERT_TRUE(sb_service_ != NULL); |
| 956 } | 959 } |
| 957 | 960 |
| 958 virtual void CleanUpOnMainThread() { | 961 virtual void CleanUpOnMainThread() OVERRIDE { |
| 959 sb_service_ = NULL; | 962 sb_service_ = NULL; |
| 960 } | 963 } |
| 961 | 964 |
| 962 void ForceUpdate() { | 965 void ForceUpdate() { |
| 963 sb_service_->protocol_manager()->ForceScheduleNextUpdate( | 966 sb_service_->protocol_manager()->ForceScheduleNextUpdate( |
| 964 base::TimeDelta::FromSeconds(0)); | 967 base::TimeDelta::FromSeconds(0)); |
| 965 } | 968 } |
| 966 | 969 |
| 967 scoped_refptr<SafeBrowsingService> sb_service_; | 970 scoped_refptr<SafeBrowsingService> sb_service_; |
| 968 | 971 |
| 969 private: | 972 private: |
| 970 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingDatabaseManagerCookieTest); | 973 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingDatabaseManagerCookieTest); |
| 971 }; | 974 }; |
| 972 | 975 |
| 973 // Test that a Safe Browsing database update request both sends cookies and can | 976 // Test that a Safe Browsing database update request both sends cookies and can |
| 974 // save cookies. | 977 // save cookies. |
| 975 IN_PROC_BROWSER_TEST_F(SafeBrowsingDatabaseManagerCookieTest, | 978 IN_PROC_BROWSER_TEST_F(SafeBrowsingDatabaseManagerCookieTest, |
| 976 TestSBUpdateCookies) { | 979 TestSBUpdateCookies) { |
| 977 content::WindowedNotificationObserver observer( | 980 content::WindowedNotificationObserver observer( |
| 978 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, | 981 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, |
| 979 content::Source<SafeBrowsingDatabaseManager>( | 982 content::Source<SafeBrowsingDatabaseManager>( |
| 980 sb_service_->database_manager())); | 983 sb_service_->database_manager())); |
| 981 BrowserThread::PostTask( | 984 BrowserThread::PostTask( |
| 982 BrowserThread::IO, FROM_HERE, | 985 BrowserThread::IO, FROM_HERE, |
| 983 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, this)); | 986 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, this)); |
| 984 observer.Wait(); | 987 observer.Wait(); |
| 985 } | 988 } |
| OLD | NEW |