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 #include "chrome/browser/safe_browsing/download_protection_service.h" | 5 #include "chrome/browser/safe_browsing/download_protection_service.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
(...skipping 1962 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1973 DownloadProtectionService::SetDownloadPingToken(&item, std::string()); | 1973 DownloadProtectionService::SetDownloadPingToken(&item, std::string()); |
1974 EXPECT_TRUE(DownloadProtectionService::GetDownloadPingToken(&item).empty()); | 1974 EXPECT_TRUE(DownloadProtectionService::GetDownloadPingToken(&item).empty()); |
1975 } | 1975 } |
1976 | 1976 |
1977 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_Unsupported) { | 1977 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_Unsupported) { |
1978 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.txt")); | 1978 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.txt")); |
1979 std::vector<base::FilePath::StringType> alternate_extensions{ | 1979 std::vector<base::FilePath::StringType> alternate_extensions{ |
1980 FILE_PATH_LITERAL(".tmp"), FILE_PATH_LITERAL(".asdfasdf")}; | 1980 FILE_PATH_LITERAL(".tmp"), FILE_PATH_LITERAL(".asdfasdf")}; |
1981 download_service_->CheckPPAPIDownloadRequest( | 1981 download_service_->CheckPPAPIDownloadRequest( |
1982 GURL("http://example.com/foo"), default_file_path, alternate_extensions, | 1982 GURL("http://example.com/foo"), default_file_path, alternate_extensions, |
| 1983 false, false, |
1983 base::Bind(&DownloadProtectionServiceTest::SyncCheckDoneCallback, | 1984 base::Bind(&DownloadProtectionServiceTest::SyncCheckDoneCallback, |
1984 base::Unretained(this))); | 1985 base::Unretained(this))); |
1985 ASSERT_TRUE(IsResult(DownloadProtectionService::SAFE)); | 1986 ASSERT_TRUE(IsResult(DownloadProtectionService::SAFE)); |
1986 } | 1987 } |
1987 | 1988 |
1988 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_SupportedDefault) { | 1989 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_SupportedDefault) { |
1989 net::FakeURLFetcherFactory factory(nullptr); | 1990 net::FakeURLFetcherFactory factory(nullptr); |
1990 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx")); | 1991 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx")); |
1991 std::vector<base::FilePath::StringType> alternate_extensions; | 1992 std::vector<base::FilePath::StringType> alternate_extensions; |
1992 EXPECT_CALL(*sb_service_->mock_database_manager(), | 1993 EXPECT_CALL(*sb_service_->mock_database_manager(), |
(...skipping 11 matching lines...) Expand all Loading... |
2004 {ClientDownloadResponse::POTENTIALLY_UNWANTED, | 2005 {ClientDownloadResponse::POTENTIALLY_UNWANTED, |
2005 DownloadProtectionService::POTENTIALLY_UNWANTED}, | 2006 DownloadProtectionService::POTENTIALLY_UNWANTED}, |
2006 }; | 2007 }; |
2007 | 2008 |
2008 for (const auto& test_case : kExpectedResults) { | 2009 for (const auto& test_case : kExpectedResults) { |
2009 factory.ClearFakeResponses(); | 2010 factory.ClearFakeResponses(); |
2010 PrepareResponse(&factory, test_case.verdict, net::HTTP_OK, | 2011 PrepareResponse(&factory, test_case.verdict, net::HTTP_OK, |
2011 net::URLRequestStatus::SUCCESS); | 2012 net::URLRequestStatus::SUCCESS); |
2012 download_service_->CheckPPAPIDownloadRequest( | 2013 download_service_->CheckPPAPIDownloadRequest( |
2013 GURL("http://example.com/foo"), default_file_path, alternate_extensions, | 2014 GURL("http://example.com/foo"), default_file_path, alternate_extensions, |
| 2015 false, false, |
2014 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 2016 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
2015 base::Unretained(this))); | 2017 base::Unretained(this))); |
2016 MessageLoop::current()->Run(); | 2018 MessageLoop::current()->Run(); |
2017 ASSERT_TRUE(IsResult(test_case.expected_result)); | 2019 ASSERT_TRUE(IsResult(test_case.expected_result)); |
2018 } | 2020 } |
2019 } | 2021 } |
2020 | 2022 |
2021 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_SupportedAlternate) { | 2023 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_SupportedAlternate) { |
2022 net::FakeURLFetcherFactory factory(nullptr); | 2024 net::FakeURLFetcherFactory factory(nullptr); |
2023 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.txt")); | 2025 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.txt")); |
2024 std::vector<base::FilePath::StringType> alternate_extensions{ | 2026 std::vector<base::FilePath::StringType> alternate_extensions{ |
2025 FILE_PATH_LITERAL(".tmp"), FILE_PATH_LITERAL(".crx")}; | 2027 FILE_PATH_LITERAL(".tmp"), FILE_PATH_LITERAL(".crx")}; |
2026 PrepareResponse(&factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK, | 2028 PrepareResponse(&factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK, |
2027 net::URLRequestStatus::SUCCESS); | 2029 net::URLRequestStatus::SUCCESS); |
2028 EXPECT_CALL(*sb_service_->mock_database_manager(), | 2030 EXPECT_CALL(*sb_service_->mock_database_manager(), |
2029 MatchDownloadWhitelistUrl(_)) | 2031 MatchDownloadWhitelistUrl(_)) |
2030 .WillRepeatedly(Return(false)); | 2032 .WillRepeatedly(Return(false)); |
2031 download_service_->CheckPPAPIDownloadRequest( | 2033 download_service_->CheckPPAPIDownloadRequest( |
2032 GURL("http://example.com/foo"), default_file_path, alternate_extensions, | 2034 GURL("http://example.com/foo"), default_file_path, alternate_extensions, |
| 2035 false, false, |
2033 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 2036 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
2034 base::Unretained(this))); | 2037 base::Unretained(this))); |
2035 MessageLoop::current()->Run(); | 2038 MessageLoop::current()->Run(); |
2036 | 2039 |
2037 ASSERT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); | 2040 ASSERT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); |
2038 } | 2041 } |
2039 | 2042 |
2040 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_WhitelistedURL) { | 2043 TEST_F(DownloadProtectionServiceTest, |
| 2044 PPAPIDownloadRequest_WhitelistedURLNotExtendedReporting) { |
2041 net::FakeURLFetcherFactory factory(nullptr); | 2045 net::FakeURLFetcherFactory factory(nullptr); |
2042 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx")); | 2046 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx")); |
2043 std::vector<base::FilePath::StringType> alternate_extensions; | 2047 std::vector<base::FilePath::StringType> alternate_extensions; |
2044 EXPECT_CALL(*sb_service_->mock_database_manager(), | 2048 EXPECT_CALL(*sb_service_->mock_database_manager(), |
2045 MatchDownloadWhitelistUrl(_)) | 2049 MatchDownloadWhitelistUrl(_)) |
2046 .WillRepeatedly(Return(true)); | 2050 .WillRepeatedly(Return(true)); |
2047 download_service_->CheckPPAPIDownloadRequest( | 2051 download_service_->CheckPPAPIDownloadRequest( |
2048 GURL("http://example.com/foo"), default_file_path, alternate_extensions, | 2052 GURL("http://example.com/foo"), default_file_path, alternate_extensions, |
| 2053 false, false, |
2049 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 2054 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
2050 base::Unretained(this))); | 2055 base::Unretained(this))); |
2051 MessageLoop::current()->Run(); | 2056 MessageLoop::current()->Run(); |
2052 | 2057 |
2053 ASSERT_TRUE(IsResult(DownloadProtectionService::SAFE)); | 2058 ASSERT_TRUE(IsResult(DownloadProtectionService::SAFE)); |
2054 } | 2059 } |
2055 | 2060 |
| 2061 TEST_F(DownloadProtectionServiceTest, |
| 2062 PPAPIDownloadRequest_WhitelistedURLIncognito) { |
| 2063 net::FakeURLFetcherFactory factory(nullptr); |
| 2064 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx")); |
| 2065 std::vector<base::FilePath::StringType> alternate_extensions; |
| 2066 EXPECT_CALL(*sb_service_->mock_database_manager(), |
| 2067 MatchDownloadWhitelistUrl(_)) |
| 2068 .WillRepeatedly(Return(true)); |
| 2069 download_service_->CheckPPAPIDownloadRequest( |
| 2070 GURL("http://example.com/foo"), default_file_path, alternate_extensions, |
| 2071 true, true, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
| 2072 base::Unretained(this))); |
| 2073 MessageLoop::current()->Run(); |
| 2074 |
| 2075 ASSERT_TRUE(IsResult(DownloadProtectionService::SAFE)); |
| 2076 } |
| 2077 |
| 2078 TEST_F(DownloadProtectionServiceTest, |
| 2079 PPAPIDownloadRequest_SampleWhitelistedURL) { |
| 2080 net::FakeURLFetcherFactory factory(nullptr); |
| 2081 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx")); |
| 2082 std::vector<base::FilePath::StringType> alternate_extensions; |
| 2083 // Configs "server" side response to DANGEROUS. |
| 2084 PrepareResponse(&factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK, |
| 2085 net::URLRequestStatus::SUCCESS); |
| 2086 // Sets the sample rate to 1.00 to trigger ClientDownloadRequest. |
| 2087 SetWhitelistedDownloadSampleRate(1.00); |
| 2088 EXPECT_CALL(*sb_service_->mock_database_manager(), |
| 2089 MatchDownloadWhitelistUrl(_)) |
| 2090 .WillRepeatedly(Return(true)); |
| 2091 download_service_->CheckPPAPIDownloadRequest( |
| 2092 GURL("http://example.com/foo"), default_file_path, alternate_extensions, |
| 2093 true, false, // In extended reporting group, and not in incognito mode. |
| 2094 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
| 2095 base::Unretained(this))); |
| 2096 MessageLoop::current()->Run(); |
| 2097 |
| 2098 ASSERT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); |
| 2099 } |
| 2100 |
2056 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_FetchFailed) { | 2101 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_FetchFailed) { |
2057 net::FakeURLFetcherFactory factory(nullptr); | 2102 net::FakeURLFetcherFactory factory(nullptr); |
2058 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx")); | 2103 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx")); |
2059 std::vector<base::FilePath::StringType> alternate_extensions; | 2104 std::vector<base::FilePath::StringType> alternate_extensions; |
2060 PrepareResponse(&factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK, | 2105 PrepareResponse(&factory, ClientDownloadResponse::DANGEROUS, net::HTTP_OK, |
2061 net::URLRequestStatus::FAILED); | 2106 net::URLRequestStatus::FAILED); |
2062 EXPECT_CALL(*sb_service_->mock_database_manager(), | 2107 EXPECT_CALL(*sb_service_->mock_database_manager(), |
2063 MatchDownloadWhitelistUrl(_)) | 2108 MatchDownloadWhitelistUrl(_)) |
2064 .WillRepeatedly(Return(false)); | 2109 .WillRepeatedly(Return(false)); |
2065 download_service_->CheckPPAPIDownloadRequest( | 2110 download_service_->CheckPPAPIDownloadRequest( |
2066 GURL("http://example.com/foo"), default_file_path, alternate_extensions, | 2111 GURL("http://example.com/foo"), default_file_path, alternate_extensions, |
| 2112 false, false, |
2067 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 2113 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
2068 base::Unretained(this))); | 2114 base::Unretained(this))); |
2069 MessageLoop::current()->Run(); | 2115 MessageLoop::current()->Run(); |
2070 | 2116 |
2071 ASSERT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); | 2117 ASSERT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); |
2072 } | 2118 } |
2073 | 2119 |
2074 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_InvalidResponse) { | 2120 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_InvalidResponse) { |
2075 net::FakeURLFetcherFactory factory(nullptr); | 2121 net::FakeURLFetcherFactory factory(nullptr); |
2076 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx")); | 2122 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx")); |
2077 std::vector<base::FilePath::StringType> alternate_extensions; | 2123 std::vector<base::FilePath::StringType> alternate_extensions; |
2078 factory.SetFakeResponse(DownloadProtectionService::GetDownloadRequestUrl(), | 2124 factory.SetFakeResponse(DownloadProtectionService::GetDownloadRequestUrl(), |
2079 "Hello world!", net::HTTP_OK, | 2125 "Hello world!", net::HTTP_OK, |
2080 net::URLRequestStatus::SUCCESS); | 2126 net::URLRequestStatus::SUCCESS); |
2081 EXPECT_CALL(*sb_service_->mock_database_manager(), | 2127 EXPECT_CALL(*sb_service_->mock_database_manager(), |
2082 MatchDownloadWhitelistUrl(_)) | 2128 MatchDownloadWhitelistUrl(_)) |
2083 .WillRepeatedly(Return(false)); | 2129 .WillRepeatedly(Return(false)); |
2084 download_service_->CheckPPAPIDownloadRequest( | 2130 download_service_->CheckPPAPIDownloadRequest( |
2085 GURL("http://example.com/foo"), default_file_path, alternate_extensions, | 2131 GURL("http://example.com/foo"), default_file_path, alternate_extensions, |
| 2132 false, false, |
2086 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 2133 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
2087 base::Unretained(this))); | 2134 base::Unretained(this))); |
2088 MessageLoop::current()->Run(); | 2135 MessageLoop::current()->Run(); |
2089 | 2136 |
2090 ASSERT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); | 2137 ASSERT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); |
2091 } | 2138 } |
2092 | 2139 |
2093 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_Timeout) { | 2140 TEST_F(DownloadProtectionServiceTest, PPAPIDownloadRequest_Timeout) { |
2094 net::FakeURLFetcherFactory factory(nullptr); | 2141 net::FakeURLFetcherFactory factory(nullptr); |
2095 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx")); | 2142 base::FilePath default_file_path(FILE_PATH_LITERAL("/foo/bar/test.crx")); |
2096 std::vector<base::FilePath::StringType> alternate_extensions; | 2143 std::vector<base::FilePath::StringType> alternate_extensions; |
2097 EXPECT_CALL(*sb_service_->mock_database_manager(), | 2144 EXPECT_CALL(*sb_service_->mock_database_manager(), |
2098 MatchDownloadWhitelistUrl(_)) | 2145 MatchDownloadWhitelistUrl(_)) |
2099 .WillRepeatedly(Return(false)); | 2146 .WillRepeatedly(Return(false)); |
2100 PrepareResponse(&factory, ClientDownloadResponse::SAFE, net::HTTP_OK, | 2147 PrepareResponse(&factory, ClientDownloadResponse::SAFE, net::HTTP_OK, |
2101 net::URLRequestStatus::SUCCESS); | 2148 net::URLRequestStatus::SUCCESS); |
2102 download_service_->download_request_timeout_ms_ = 0; | 2149 download_service_->download_request_timeout_ms_ = 0; |
2103 download_service_->CheckPPAPIDownloadRequest( | 2150 download_service_->CheckPPAPIDownloadRequest( |
2104 GURL("http://example.com/foo"), default_file_path, alternate_extensions, | 2151 GURL("http://example.com/foo"), default_file_path, alternate_extensions, |
| 2152 false, false, |
2105 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 2153 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
2106 base::Unretained(this))); | 2154 base::Unretained(this))); |
2107 MessageLoop::current()->Run(); | 2155 MessageLoop::current()->Run(); |
2108 | 2156 |
2109 ASSERT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); | 2157 ASSERT_TRUE(IsResult(DownloadProtectionService::UNKNOWN)); |
2110 } | 2158 } |
2111 | 2159 |
2112 namespace { | 2160 namespace { |
2113 | 2161 |
2114 std::unique_ptr<net::FakeURLFetcher> FakeURLFetcherCreatorFunc( | 2162 std::unique_ptr<net::FakeURLFetcher> FakeURLFetcherCreatorFunc( |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2154 std::vector<base::FilePath::StringType> alternate_extensions{ | 2202 std::vector<base::FilePath::StringType> alternate_extensions{ |
2155 FILE_PATH_LITERAL(".txt"), FILE_PATH_LITERAL(".abc"), | 2203 FILE_PATH_LITERAL(".txt"), FILE_PATH_LITERAL(".abc"), |
2156 FILE_PATH_LITERAL(""), FILE_PATH_LITERAL(".sdF")}; | 2204 FILE_PATH_LITERAL(""), FILE_PATH_LITERAL(".sdF")}; |
2157 EXPECT_CALL(*sb_service_->mock_database_manager(), | 2205 EXPECT_CALL(*sb_service_->mock_database_manager(), |
2158 MatchDownloadWhitelistUrl(_)) | 2206 MatchDownloadWhitelistUrl(_)) |
2159 .WillRepeatedly(Return(false)); | 2207 .WillRepeatedly(Return(false)); |
2160 PrepareResponse(&factory, ClientDownloadResponse::SAFE, net::HTTP_OK, | 2208 PrepareResponse(&factory, ClientDownloadResponse::SAFE, net::HTTP_OK, |
2161 net::URLRequestStatus::SUCCESS); | 2209 net::URLRequestStatus::SUCCESS); |
2162 const GURL kRequestorUrl("http://example.com/foo"); | 2210 const GURL kRequestorUrl("http://example.com/foo"); |
2163 download_service_->CheckPPAPIDownloadRequest( | 2211 download_service_->CheckPPAPIDownloadRequest( |
2164 kRequestorUrl, default_file_path, alternate_extensions, | 2212 kRequestorUrl, default_file_path, alternate_extensions, false, false, |
2165 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 2213 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
2166 base::Unretained(this))); | 2214 base::Unretained(this))); |
2167 MessageLoop::current()->Run(); | 2215 MessageLoop::current()->Run(); |
2168 | 2216 |
2169 ASSERT_FALSE(upload_data.empty()); | 2217 ASSERT_FALSE(upload_data.empty()); |
2170 | 2218 |
2171 ClientDownloadRequest request; | 2219 ClientDownloadRequest request; |
2172 ASSERT_TRUE(request.ParseFromString(upload_data)); | 2220 ASSERT_TRUE(request.ParseFromString(upload_data)); |
2173 | 2221 |
2174 EXPECT_EQ(ClientDownloadRequest::PPAPI_SAVE_REQUEST, request.download_type()); | 2222 EXPECT_EQ(ClientDownloadRequest::PPAPI_SAVE_REQUEST, request.download_type()); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2255 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, | 2303 &item, base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, |
2256 base::Unretained(this))); | 2304 base::Unretained(this))); |
2257 MessageLoop::current()->Run(); | 2305 MessageLoop::current()->Run(); |
2258 | 2306 |
2259 EXPECT_FALSE(HasClientDownloadRequest()); | 2307 EXPECT_FALSE(HasClientDownloadRequest()); |
2260 // Overriden by flag: | 2308 // Overriden by flag: |
2261 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); | 2309 EXPECT_TRUE(IsResult(DownloadProtectionService::DANGEROUS)); |
2262 } | 2310 } |
2263 | 2311 |
2264 } // namespace safe_browsing | 2312 } // namespace safe_browsing |
OLD | NEW |