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 "content/browser/download/quarantine.h" | 5 #include "content/browser/download/quarantine.h" |
6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/test/histogram_tester.h" | 9 #include "base/test/histogram_tester.h" |
10 #include "net/base/filename_util.h" | 10 #include "net/base/filename_util.h" |
(...skipping 23 matching lines...) Expand all Loading... | |
34 QuarantineFile(test_dir.path().AppendASCII("does-not-exist.exe"), | 34 QuarantineFile(test_dir.path().AppendASCII("does-not-exist.exe"), |
35 GURL(kDummySourceUrl), GURL(kDummyReferrerUrl), | 35 GURL(kDummySourceUrl), GURL(kDummyReferrerUrl), |
36 kDummyClientGuid)); | 36 kDummyClientGuid)); |
37 } | 37 } |
38 | 38 |
39 // On Windows systems, files downloaded from a local source are considered | 39 // On Windows systems, files downloaded from a local source are considered |
40 // trustworthy. Hence they aren't annotated with source information. This test | 40 // trustworthy. Hence they aren't annotated with source information. This test |
41 // verifies this behavior since the other tests in this suite would pass with a | 41 // verifies this behavior since the other tests in this suite would pass with a |
42 // false positive if local files are being annotated with the MOTW for the | 42 // false positive if local files are being annotated with the MOTW for the |
43 // internet zone. | 43 // internet zone. |
44 TEST(QuarantineWinTest, LocalFileZoneAssumption_DependsOnLocalConfig) { | 44 TEST(QuarantineWinTest, LocalFile_DependsOnLocalConfig) { |
45 base::HistogramTester histogram_tester; | 45 base::HistogramTester histogram_tester; |
46 base::ScopedTempDir test_dir; | 46 base::ScopedTempDir test_dir; |
47 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); | 47 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); |
48 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); | 48 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); |
49 ASSERT_EQ(5, base::WriteFile(test_file, "Hello", 5u)); | |
50 | 49 |
51 EXPECT_EQ(QuarantineFileResult::OK, | 50 const char* const kLocalSourceURLs[] = { |
52 QuarantineFile(test_file, net::FilePathToFileURL(test_file), GURL(), | 51 "http://localhost/foo", |
53 kDummyClientGuid)); | 52 "data:text/plain,Foobar", |
54 std::string contents; | 53 "data:application/octet-stream,Foobar", |
55 EXPECT_FALSE(base::ReadFileToString( | 54 "file:///some-local-dir/foo.exe" |
56 base::FilePath(test_file.value() + kMotwStreamSuffix), &contents)); | 55 }; |
56 | |
57 for (const auto source_url : kLocalSourceURLs) { | |
58 SCOPED_TRACE(::testing::Message() << "Trying URL " << source_url); | |
59 ASSERT_EQ(5, base::WriteFile(test_file, "Hello", 5u)); | |
60 | |
61 EXPECT_EQ( | |
62 QuarantineFileResult::OK, | |
63 QuarantineFile(test_file, GURL(source_url), GURL(), kDummyClientGuid)); | |
64 | |
65 std::string contents; | |
66 base::ReadFileToString( | |
67 base::FilePath(test_file.value() + kMotwStreamSuffix), &contents); | |
68 | |
69 // These warnings aren't displayed on successful test runs. They are there | |
70 // so that we can check for deviations in behavior during manual testing. | |
71 if (!contents.empty()) { | |
72 LOG(WARNING) << "Unexpected zone marker for file " << test_file.value() | |
73 << " Source URL:" << source_url; | |
74 if (contents != kMotwForInternetZone) | |
75 LOG(WARNING) << "Zone marker contents: " << contents; | |
76 } | |
77 | |
78 base::DeleteFile(test_file, false); | |
79 } | |
57 | 80 |
58 // Bucket 1 is SUCCESS_WITHOUT_MOTW. | 81 // Bucket 1 is SUCCESS_WITHOUT_MOTW. |
59 histogram_tester.ExpectUniqueSample("Download.AttachmentServices.Result", 1, | 82 histogram_tester.ExpectUniqueSample("Download.AttachmentServices.Result", 1, |
60 1); | 83 arraysize(kLocalSourceURLs)); |
61 } | 84 } |
62 | 85 |
63 // A file downloaded from the internet should be annotated with .. something. | 86 // A file downloaded from the internet should be annotated with .. something. |
64 // The specific zone assigned to our dummy source URL depends on the local | 87 // The specific zone assigned to our dummy source URL depends on the local |
65 // configuration. But no sane configuration should be treating the dummy URL as | 88 // configuration. But no sane configuration should be treating the dummy URL as |
66 // a trusted source for anything. | 89 // a trusted source for anything. |
67 TEST(QuarantineWinTest, DownloadedFile_DependsOnLocalConfig) { | 90 TEST(QuarantineWinTest, DownloadedFile_DependsOnLocalConfig) { |
68 base::HistogramTester histogram_tester; | 91 base::HistogramTester histogram_tester; |
69 base::ScopedTempDir test_dir; | 92 base::ScopedTempDir test_dir; |
70 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); | 93 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); |
71 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); | 94 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); |
95 | |
96 const char* const kUnsafeSourceURLs[] = { | |
97 "http://example.com/foo", | |
98 "https://exmaple.com/foo", | |
elawrence
2016/08/02 21:09:22
I *think* "exmaple" is a harmless typo, but maybe
asanka
2016/09/23 01:34:53
Fixed :)
| |
99 "ftp://example.com/foo", | |
100 "ftp://example.com:2121/foo", | |
101 "about:internet", | |
102 "" | |
103 }; | |
104 | |
105 for (const auto source_url : kUnsafeSourceURLs) { | |
106 SCOPED_TRACE(::testing::Message() << "Trying URL " << source_url); | |
107 ASSERT_EQ(5, base::WriteFile(test_file, "Hello", 5u)); | |
108 EXPECT_EQ( | |
109 QuarantineFileResult::OK, | |
110 QuarantineFile(test_file, GURL(source_url), GURL(), kDummyClientGuid)); | |
111 std::string contents; | |
112 ASSERT_TRUE(base::ReadFileToString( | |
113 base::FilePath(test_file.value() + kMotwStreamSuffix), &contents)); | |
114 // The actual assigned zone could be anything. So only testing that there is | |
115 // a zone annotation. | |
116 EXPECT_FALSE(contents.empty()); | |
117 | |
118 // These warnings aren't displayed on successful test runs. They are there | |
119 // so that we can check for deviations in behavior during manual testing. | |
120 if (contents != kMotwForInternetZone) | |
121 LOG(WARNING) << "Unexpected zone marker: " << contents; | |
122 base::DeleteFile(test_file, false); | |
123 } | |
124 | |
125 // Bucket 0 is SUCCESS_WITH_MOTW. | |
126 histogram_tester.ExpectUniqueSample("Download.AttachmentServices.Result", 0, | |
127 arraysize(kUnsafeSourceURLs)); | |
128 } | |
129 | |
130 // An empty source URL should result in a file that's treated the same as one | |
131 // downloaded from the internet. | |
132 TEST(QuarantineWinTest, EmptySource_DependsOnLocalConfig) { | |
133 base::HistogramTester histogram_tester; | |
134 base::ScopedTempDir test_dir; | |
135 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); | |
136 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); | |
72 ASSERT_EQ(5, base::WriteFile(test_file, "Hello", 5u)); | 137 ASSERT_EQ(5, base::WriteFile(test_file, "Hello", 5u)); |
73 | 138 |
74 EXPECT_EQ(QuarantineFileResult::OK, | 139 EXPECT_EQ(QuarantineFileResult::OK, |
75 QuarantineFile(test_file, GURL(kDummySourceUrl), | 140 QuarantineFile(test_file, GURL(), GURL(), kDummyClientGuid)); |
76 GURL(kDummyReferrerUrl), kDummyClientGuid)); | |
77 std::string contents; | 141 std::string contents; |
78 ASSERT_TRUE(base::ReadFileToString( | 142 ASSERT_TRUE(base::ReadFileToString( |
79 base::FilePath(test_file.value() + kMotwStreamSuffix), &contents)); | 143 base::FilePath(test_file.value() + kMotwStreamSuffix), &contents)); |
80 // The actual assigned zone could be anything. So only testing that there is a | 144 // The actual assigned zone could be anything. So only testing that there is a |
81 // zone annotation. | 145 // zone annotation. |
82 EXPECT_FALSE(contents.empty()); | 146 EXPECT_FALSE(contents.empty()); |
83 | 147 |
84 // Bucket 0 is SUCCESS_WITH_MOTW. | 148 // Bucket 0 is SUCCESS_WITH_MOTW. |
85 histogram_tester.ExpectUniqueSample("Download.AttachmentServices.Result", 0, | 149 histogram_tester.ExpectUniqueSample("Download.AttachmentServices.Result", 0, |
86 1); | 150 1); |
(...skipping 30 matching lines...) Expand all Loading... | |
117 EXPECT_EQ(QuarantineFileResult::OK, | 181 EXPECT_EQ(QuarantineFileResult::OK, |
118 QuarantineFile(test_file, net::FilePathToFileURL(test_file), GURL(), | 182 QuarantineFile(test_file, net::FilePathToFileURL(test_file), GURL(), |
119 std::string())); | 183 std::string())); |
120 std::string contents; | 184 std::string contents; |
121 ASSERT_TRUE(base::ReadFileToString( | 185 ASSERT_TRUE(base::ReadFileToString( |
122 base::FilePath(test_file.value() + kMotwStreamSuffix), &contents)); | 186 base::FilePath(test_file.value() + kMotwStreamSuffix), &contents)); |
123 EXPECT_STREQ(kMotwForInternetZone, contents.c_str()); | 187 EXPECT_STREQ(kMotwForInternetZone, contents.c_str()); |
124 } | 188 } |
125 | 189 |
126 } // content | 190 } // content |
OLD | NEW |