| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <windows.h> | 5 #include <windows.h> |
| 6 | 6 |
| 7 #include <wininet.h> | 7 #include <wininet.h> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 ""}; | 42 ""}; |
| 43 | 43 |
| 44 } // namespace | 44 } // namespace |
| 45 | 45 |
| 46 // If the file is missing, the QuarantineFile() call should return FILE_MISSING. | 46 // If the file is missing, the QuarantineFile() call should return FILE_MISSING. |
| 47 TEST(QuarantineWinTest, MissingFile) { | 47 TEST(QuarantineWinTest, MissingFile) { |
| 48 base::ScopedTempDir test_dir; | 48 base::ScopedTempDir test_dir; |
| 49 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); | 49 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); |
| 50 | 50 |
| 51 EXPECT_EQ(QuarantineFileResult::FILE_MISSING, | 51 EXPECT_EQ(QuarantineFileResult::FILE_MISSING, |
| 52 QuarantineFile(test_dir.path().AppendASCII("does-not-exist.exe"), | 52 QuarantineFile(test_dir.GetPath().AppendASCII("does-not-exist.exe"), |
| 53 GURL(kDummySourceUrl), GURL(kDummyReferrerUrl), | 53 GURL(kDummySourceUrl), GURL(kDummyReferrerUrl), |
| 54 kDummyClientGuid)); | 54 kDummyClientGuid)); |
| 55 } | 55 } |
| 56 | 56 |
| 57 // On Windows systems, files downloaded from a local source are considered | 57 // On Windows systems, files downloaded from a local source are considered |
| 58 // trustworthy. Hence they aren't annotated with source information. This test | 58 // trustworthy. Hence they aren't annotated with source information. This test |
| 59 // verifies this behavior since the other tests in this suite would pass with a | 59 // verifies this behavior since the other tests in this suite would pass with a |
| 60 // false positive if local files are being annotated with the MOTW for the | 60 // false positive if local files are being annotated with the MOTW for the |
| 61 // internet zone. | 61 // internet zone. |
| 62 TEST(QuarantineWinTest, LocalFile_DependsOnLocalConfig) { | 62 TEST(QuarantineWinTest, LocalFile_DependsOnLocalConfig) { |
| 63 base::HistogramTester histogram_tester; | 63 base::HistogramTester histogram_tester; |
| 64 base::ScopedTempDir test_dir; | 64 base::ScopedTempDir test_dir; |
| 65 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); | 65 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); |
| 66 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); | 66 base::FilePath test_file = test_dir.GetPath().AppendASCII("foo.exe"); |
| 67 | 67 |
| 68 const char* const kLocalSourceURLs[] = { | 68 const char* const kLocalSourceURLs[] = { |
| 69 "http://localhost/foo", | 69 "http://localhost/foo", |
| 70 "file:///C:/some-local-dir/foo.exe" | 70 "file:///C:/some-local-dir/foo.exe" |
| 71 }; | 71 }; |
| 72 | 72 |
| 73 for (const auto source_url : kLocalSourceURLs) { | 73 for (const auto source_url : kLocalSourceURLs) { |
| 74 SCOPED_TRACE(::testing::Message() << "Trying URL " << source_url); | 74 SCOPED_TRACE(::testing::Message() << "Trying URL " << source_url); |
| 75 ASSERT_EQ(static_cast<int>(arraysize(kTestData)), | 75 ASSERT_EQ(static_cast<int>(arraysize(kTestData)), |
| 76 base::WriteFile(test_file, kTestData, arraysize(kTestData))); | 76 base::WriteFile(test_file, kTestData, arraysize(kTestData))); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 101 } | 101 } |
| 102 | 102 |
| 103 // A file downloaded from the internet should be annotated with .. something. | 103 // A file downloaded from the internet should be annotated with .. something. |
| 104 // The specific zone assigned to our dummy source URL depends on the local | 104 // The specific zone assigned to our dummy source URL depends on the local |
| 105 // configuration. But no sane configuration should be treating the dummy URL as | 105 // configuration. But no sane configuration should be treating the dummy URL as |
| 106 // a trusted source for anything. | 106 // a trusted source for anything. |
| 107 TEST(QuarantineWinTest, DownloadedFile_DependsOnLocalConfig) { | 107 TEST(QuarantineWinTest, DownloadedFile_DependsOnLocalConfig) { |
| 108 base::HistogramTester histogram_tester; | 108 base::HistogramTester histogram_tester; |
| 109 base::ScopedTempDir test_dir; | 109 base::ScopedTempDir test_dir; |
| 110 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); | 110 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); |
| 111 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); | 111 base::FilePath test_file = test_dir.GetPath().AppendASCII("foo.exe"); |
| 112 | 112 |
| 113 for (const auto source_url : kUntrustedURLs) { | 113 for (const auto source_url : kUntrustedURLs) { |
| 114 SCOPED_TRACE(::testing::Message() << "Trying URL " << source_url); | 114 SCOPED_TRACE(::testing::Message() << "Trying URL " << source_url); |
| 115 ASSERT_EQ(static_cast<int>(arraysize(kTestData)), | 115 ASSERT_EQ(static_cast<int>(arraysize(kTestData)), |
| 116 base::WriteFile(test_file, kTestData, arraysize(kTestData))); | 116 base::WriteFile(test_file, kTestData, arraysize(kTestData))); |
| 117 EXPECT_EQ( | 117 EXPECT_EQ( |
| 118 QuarantineFileResult::OK, | 118 QuarantineFileResult::OK, |
| 119 QuarantineFile(test_file, GURL(source_url), GURL(), kDummyClientGuid)); | 119 QuarantineFile(test_file, GURL(source_url), GURL(), kDummyClientGuid)); |
| 120 std::string motw_contents; | 120 std::string motw_contents; |
| 121 ASSERT_TRUE(base::ReadFileToString( | 121 ASSERT_TRUE(base::ReadFileToString( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 133 | 133 |
| 134 // Bucket 0 is SUCCESS_WITH_MOTW. | 134 // Bucket 0 is SUCCESS_WITH_MOTW. |
| 135 histogram_tester.ExpectUniqueSample("Download.AttachmentServices.Result", 0, | 135 histogram_tester.ExpectUniqueSample("Download.AttachmentServices.Result", 0, |
| 136 arraysize(kUntrustedURLs)); | 136 arraysize(kUntrustedURLs)); |
| 137 } | 137 } |
| 138 | 138 |
| 139 TEST(QuarantineWinTest, UnsafeReferrer_DependsOnLocalConfig) { | 139 TEST(QuarantineWinTest, UnsafeReferrer_DependsOnLocalConfig) { |
| 140 base::HistogramTester histogram_tester; | 140 base::HistogramTester histogram_tester; |
| 141 base::ScopedTempDir test_dir; | 141 base::ScopedTempDir test_dir; |
| 142 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); | 142 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); |
| 143 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); | 143 base::FilePath test_file = test_dir.GetPath().AppendASCII("foo.exe"); |
| 144 | 144 |
| 145 std::vector<std::string> unsafe_referrers(std::begin(kUntrustedURLs), | 145 std::vector<std::string> unsafe_referrers(std::begin(kUntrustedURLs), |
| 146 std::end(kUntrustedURLs)); | 146 std::end(kUntrustedURLs)); |
| 147 | 147 |
| 148 std::string huge_referrer = "http://example.com/"; | 148 std::string huge_referrer = "http://example.com/"; |
| 149 huge_referrer.append(INTERNET_MAX_URL_LENGTH * 2, 'a'); | 149 huge_referrer.append(INTERNET_MAX_URL_LENGTH * 2, 'a'); |
| 150 unsafe_referrers.push_back(huge_referrer); | 150 unsafe_referrers.push_back(huge_referrer); |
| 151 | 151 |
| 152 for (const auto referrer_url : unsafe_referrers) { | 152 for (const auto referrer_url : unsafe_referrers) { |
| 153 SCOPED_TRACE(::testing::Message() << "Trying URL " << referrer_url); | 153 SCOPED_TRACE(::testing::Message() << "Trying URL " << referrer_url); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 174 histogram_tester.ExpectUniqueSample("Download.AttachmentServices.Result", 0, | 174 histogram_tester.ExpectUniqueSample("Download.AttachmentServices.Result", 0, |
| 175 unsafe_referrers.size()); | 175 unsafe_referrers.size()); |
| 176 } | 176 } |
| 177 | 177 |
| 178 // An empty source URL should result in a file that's treated the same as one | 178 // An empty source URL should result in a file that's treated the same as one |
| 179 // downloaded from the internet. | 179 // downloaded from the internet. |
| 180 TEST(QuarantineWinTest, EmptySource_DependsOnLocalConfig) { | 180 TEST(QuarantineWinTest, EmptySource_DependsOnLocalConfig) { |
| 181 base::HistogramTester histogram_tester; | 181 base::HistogramTester histogram_tester; |
| 182 base::ScopedTempDir test_dir; | 182 base::ScopedTempDir test_dir; |
| 183 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); | 183 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); |
| 184 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); | 184 base::FilePath test_file = test_dir.GetPath().AppendASCII("foo.exe"); |
| 185 ASSERT_EQ(static_cast<int>(arraysize(kTestData)), | 185 ASSERT_EQ(static_cast<int>(arraysize(kTestData)), |
| 186 base::WriteFile(test_file, kTestData, arraysize(kTestData))); | 186 base::WriteFile(test_file, kTestData, arraysize(kTestData))); |
| 187 | 187 |
| 188 EXPECT_EQ(QuarantineFileResult::OK, | 188 EXPECT_EQ(QuarantineFileResult::OK, |
| 189 QuarantineFile(test_file, GURL(), GURL(), kDummyClientGuid)); | 189 QuarantineFile(test_file, GURL(), GURL(), kDummyClientGuid)); |
| 190 std::string motw_contents; | 190 std::string motw_contents; |
| 191 ASSERT_TRUE(base::ReadFileToString( | 191 ASSERT_TRUE(base::ReadFileToString( |
| 192 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); | 192 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); |
| 193 // The actual assigned zone could be anything. So only testing that there is a | 193 // The actual assigned zone could be anything. So only testing that there is a |
| 194 // zone annotation. | 194 // zone annotation. |
| 195 EXPECT_FALSE(motw_contents.empty()); | 195 EXPECT_FALSE(motw_contents.empty()); |
| 196 | 196 |
| 197 // Bucket 0 is SUCCESS_WITH_MOTW. | 197 // Bucket 0 is SUCCESS_WITH_MOTW. |
| 198 histogram_tester.ExpectUniqueSample("Download.AttachmentServices.Result", 0, | 198 histogram_tester.ExpectUniqueSample("Download.AttachmentServices.Result", 0, |
| 199 1); | 199 1); |
| 200 } | 200 } |
| 201 | 201 |
| 202 // Empty files aren't passed to AVScanFile. They are instead marked manually. If | 202 // Empty files aren't passed to AVScanFile. They are instead marked manually. If |
| 203 // the file is passed to AVScanFile, then there wouldn't be a MOTW attached to | 203 // the file is passed to AVScanFile, then there wouldn't be a MOTW attached to |
| 204 // it and the test would fail. | 204 // it and the test would fail. |
| 205 TEST(QuarantineWinTest, EmptyFile) { | 205 TEST(QuarantineWinTest, EmptyFile) { |
| 206 base::HistogramTester histogram_tester; | 206 base::HistogramTester histogram_tester; |
| 207 base::ScopedTempDir test_dir; | 207 base::ScopedTempDir test_dir; |
| 208 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); | 208 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); |
| 209 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); | 209 base::FilePath test_file = test_dir.GetPath().AppendASCII("foo.exe"); |
| 210 ASSERT_EQ(0, base::WriteFile(test_file, "", 0u)); | 210 ASSERT_EQ(0, base::WriteFile(test_file, "", 0u)); |
| 211 | 211 |
| 212 EXPECT_EQ(QuarantineFileResult::OK, | 212 EXPECT_EQ(QuarantineFileResult::OK, |
| 213 QuarantineFile(test_file, net::FilePathToFileURL(test_file), GURL(), | 213 QuarantineFile(test_file, net::FilePathToFileURL(test_file), GURL(), |
| 214 kDummyClientGuid)); | 214 kDummyClientGuid)); |
| 215 std::string motw_contents; | 215 std::string motw_contents; |
| 216 ASSERT_TRUE(base::ReadFileToString( | 216 ASSERT_TRUE(base::ReadFileToString( |
| 217 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); | 217 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); |
| 218 EXPECT_STREQ(kMotwForInternetZone, motw_contents.c_str()); | 218 EXPECT_STREQ(kMotwForInternetZone, motw_contents.c_str()); |
| 219 | 219 |
| 220 // Attachment services shouldn't have been invoked at all. | 220 // Attachment services shouldn't have been invoked at all. |
| 221 histogram_tester.ExpectTotalCount("Download.AttachmentServices.Result", 0); | 221 histogram_tester.ExpectTotalCount("Download.AttachmentServices.Result", 0); |
| 222 } | 222 } |
| 223 | 223 |
| 224 // If there is no client GUID supplied to the QuarantineFile() call, then rather | 224 // If there is no client GUID supplied to the QuarantineFile() call, then rather |
| 225 // than invoking AVScanFile, the MOTW will be applied manually. If the file is | 225 // than invoking AVScanFile, the MOTW will be applied manually. If the file is |
| 226 // passed to AVScanFile, then there wouldn't be a MOTW attached to it and the | 226 // passed to AVScanFile, then there wouldn't be a MOTW attached to it and the |
| 227 // test would fail. | 227 // test would fail. |
| 228 TEST(QuarantineWinTest, NoClientGuid) { | 228 TEST(QuarantineWinTest, NoClientGuid) { |
| 229 base::ScopedTempDir test_dir; | 229 base::ScopedTempDir test_dir; |
| 230 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); | 230 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); |
| 231 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); | 231 base::FilePath test_file = test_dir.GetPath().AppendASCII("foo.exe"); |
| 232 ASSERT_EQ(static_cast<int>(arraysize(kTestData)), | 232 ASSERT_EQ(static_cast<int>(arraysize(kTestData)), |
| 233 base::WriteFile(test_file, kTestData, arraysize(kTestData))); | 233 base::WriteFile(test_file, kTestData, arraysize(kTestData))); |
| 234 | 234 |
| 235 EXPECT_EQ(QuarantineFileResult::OK, | 235 EXPECT_EQ(QuarantineFileResult::OK, |
| 236 QuarantineFile(test_file, net::FilePathToFileURL(test_file), GURL(), | 236 QuarantineFile(test_file, net::FilePathToFileURL(test_file), GURL(), |
| 237 std::string())); | 237 std::string())); |
| 238 std::string motw_contents; | 238 std::string motw_contents; |
| 239 ASSERT_TRUE(base::ReadFileToString( | 239 ASSERT_TRUE(base::ReadFileToString( |
| 240 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); | 240 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); |
| 241 EXPECT_STREQ(kMotwForInternetZone, motw_contents.c_str()); | 241 EXPECT_STREQ(kMotwForInternetZone, motw_contents.c_str()); |
| 242 } | 242 } |
| 243 | 243 |
| 244 // URLs longer than INTERNET_MAX_URL_LENGTH are known to break URLMon. Such a | 244 // URLs longer than INTERNET_MAX_URL_LENGTH are known to break URLMon. Such a |
| 245 // URL, when used as a source URL shouldn't break QuarantineFile() which should | 245 // URL, when used as a source URL shouldn't break QuarantineFile() which should |
| 246 // mark the file as being from the internet zone as a safe fallback. | 246 // mark the file as being from the internet zone as a safe fallback. |
| 247 TEST(QuarantineWinTest, SuperLongURL) { | 247 TEST(QuarantineWinTest, SuperLongURL) { |
| 248 base::ScopedTempDir test_dir; | 248 base::ScopedTempDir test_dir; |
| 249 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); | 249 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); |
| 250 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); | 250 base::FilePath test_file = test_dir.GetPath().AppendASCII("foo.exe"); |
| 251 ASSERT_EQ(static_cast<int>(arraysize(kTestData)), | 251 ASSERT_EQ(static_cast<int>(arraysize(kTestData)), |
| 252 base::WriteFile(test_file, kTestData, arraysize(kTestData))); | 252 base::WriteFile(test_file, kTestData, arraysize(kTestData))); |
| 253 | 253 |
| 254 std::string source_url("http://example.com/"); | 254 std::string source_url("http://example.com/"); |
| 255 source_url.append(INTERNET_MAX_URL_LENGTH * 2, 'a'); | 255 source_url.append(INTERNET_MAX_URL_LENGTH * 2, 'a'); |
| 256 EXPECT_EQ(QuarantineFileResult::OK, | 256 EXPECT_EQ(QuarantineFileResult::OK, |
| 257 QuarantineFile(test_file, GURL(source_url), GURL(), std::string())); | 257 QuarantineFile(test_file, GURL(source_url), GURL(), std::string())); |
| 258 | 258 |
| 259 std::string motw_contents; | 259 std::string motw_contents; |
| 260 ASSERT_TRUE(base::ReadFileToString( | 260 ASSERT_TRUE(base::ReadFileToString( |
| 261 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); | 261 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); |
| 262 EXPECT_STREQ(kMotwForInternetZone, motw_contents.c_str()); | 262 EXPECT_STREQ(kMotwForInternetZone, motw_contents.c_str()); |
| 263 } | 263 } |
| 264 | 264 |
| 265 } // content | 265 } // content |
| OLD | NEW |