Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(426)

Side by Side Diff: content/browser/download/quarantine_win_unittest.cc

Issue 2374793002: [Downloads] Fix UMA recording of invalid results from AttachmentServices (Closed)
Patch Set: . Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/browser/download/quarantine_win.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "content/browser/download/quarantine.h"
10 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
11 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
12 #include "base/files/scoped_temp_dir.h" 11 #include "base/files/scoped_temp_dir.h"
12 #include "base/macros.h"
13 #include "base/test/histogram_tester.h" 13 #include "base/test/histogram_tester.h"
14 #include "base/test/test_file_util.h"
15 #include "content/browser/download/quarantine.h"
14 #include "net/base/filename_util.h" 16 #include "net/base/filename_util.h"
15 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
16 #include "url/gurl.h" 18 #include "url/gurl.h"
17 19
18 namespace content { 20 namespace content {
19 21
20 namespace { 22 namespace {
21 23
22 const char kDummySourceUrl[] = "https://example.com/foo"; 24 const char kDummySourceUrl[] = "https://example.com/foo";
23 const char kDummyReferrerUrl[] = "https://example.com/referrer"; 25 const char kDummyReferrerUrl[] = "https://example.com/referrer";
24 const char kDummyClientGuid[] = "A1B69307-8FA2-4B6F-9181-EA06051A48A7"; 26 const char kDummyClientGuid[] = "A1B69307-8FA2-4B6F-9181-EA06051A48A7";
25 27
26 const char kMotwForInternetZone[] = "[ZoneTransfer]\r\nZoneId=3\r\n"; 28 const char kMotwForInternetZone[] = "[ZoneTransfer]\r\nZoneId=3\r\n";
27 const base::FilePath::CharType kMotwStreamSuffix[] = 29 const base::FilePath::CharType kMotwStreamSuffix[] =
28 FILE_PATH_LITERAL(":Zone.Identifier"); 30 FILE_PATH_LITERAL(":Zone.Identifier");
29 31
32 const char kTestData[] = "Hello world!";
33
30 const char* const kUntrustedURLs[] = { 34 const char* const kUntrustedURLs[] = {
31 "http://example.com/foo", 35 "http://example.com/foo",
32 "https://example.com/foo", 36 "https://example.com/foo",
33 "ftp://example.com/foo", 37 "ftp://example.com/foo",
34 "ftp://example.com:2121/foo", 38 "ftp://example.com:2121/foo",
35 "data:text/plain,Hello%20world", 39 "data:text/plain,Hello%20world",
36 "blob://example.com/126278b3-58f3-4b4a-a914-1d1185d634f6", 40 "blob://example.com/126278b3-58f3-4b4a-a914-1d1185d634f6",
37 "about:internet", 41 "about:internet",
38 ""}; 42 ""};
39 43
(...skipping 21 matching lines...) Expand all
61 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); 65 ASSERT_TRUE(test_dir.CreateUniqueTempDir());
62 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); 66 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe");
63 67
64 const char* const kLocalSourceURLs[] = { 68 const char* const kLocalSourceURLs[] = {
65 "http://localhost/foo", 69 "http://localhost/foo",
66 "file:///C:/some-local-dir/foo.exe" 70 "file:///C:/some-local-dir/foo.exe"
67 }; 71 };
68 72
69 for (const auto source_url : kLocalSourceURLs) { 73 for (const auto source_url : kLocalSourceURLs) {
70 SCOPED_TRACE(::testing::Message() << "Trying URL " << source_url); 74 SCOPED_TRACE(::testing::Message() << "Trying URL " << source_url);
71 ASSERT_EQ(5, base::WriteFile(test_file, "Hello", 5u)); 75 ASSERT_EQ(static_cast<int>(arraysize(kTestData)),
76 base::WriteFile(test_file, kTestData, arraysize(kTestData)));
72 77
73 EXPECT_EQ( 78 EXPECT_EQ(
74 QuarantineFileResult::OK, 79 QuarantineFileResult::OK,
75 QuarantineFile(test_file, GURL(source_url), GURL(), kDummyClientGuid)); 80 QuarantineFile(test_file, GURL(source_url), GURL(), kDummyClientGuid));
76 81
77 std::string motw_contents; 82 std::string motw_contents;
78 base::ReadFileToString( 83 base::ReadFileToString(
79 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents); 84 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents);
80 85
81 // These warnings aren't displayed on successful test runs. They are there 86 // These warnings aren't displayed on successful test runs. They are there
(...skipping 18 matching lines...) Expand all
100 // 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
101 // a trusted source for anything. 106 // a trusted source for anything.
102 TEST(QuarantineWinTest, DownloadedFile_DependsOnLocalConfig) { 107 TEST(QuarantineWinTest, DownloadedFile_DependsOnLocalConfig) {
103 base::HistogramTester histogram_tester; 108 base::HistogramTester histogram_tester;
104 base::ScopedTempDir test_dir; 109 base::ScopedTempDir test_dir;
105 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); 110 ASSERT_TRUE(test_dir.CreateUniqueTempDir());
106 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); 111 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe");
107 112
108 for (const auto source_url : kUntrustedURLs) { 113 for (const auto source_url : kUntrustedURLs) {
109 SCOPED_TRACE(::testing::Message() << "Trying URL " << source_url); 114 SCOPED_TRACE(::testing::Message() << "Trying URL " << source_url);
110 ASSERT_EQ(5, base::WriteFile(test_file, "Hello", 5u)); 115 ASSERT_EQ(static_cast<int>(arraysize(kTestData)),
116 base::WriteFile(test_file, kTestData, arraysize(kTestData)));
111 EXPECT_EQ( 117 EXPECT_EQ(
112 QuarantineFileResult::OK, 118 QuarantineFileResult::OK,
113 QuarantineFile(test_file, GURL(source_url), GURL(), kDummyClientGuid)); 119 QuarantineFile(test_file, GURL(source_url), GURL(), kDummyClientGuid));
114 std::string motw_contents; 120 std::string motw_contents;
115 ASSERT_TRUE(base::ReadFileToString( 121 ASSERT_TRUE(base::ReadFileToString(
116 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); 122 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents));
117 // The actual assigned zone could be anything. So only testing that there is 123 // The actual assigned zone could be anything. So only testing that there is
118 // a zone annotation. 124 // a zone annotation.
119 EXPECT_FALSE(motw_contents.empty()); 125 EXPECT_FALSE(motw_contents.empty());
120 126
(...skipping 17 matching lines...) Expand all
138 144
139 std::vector<std::string> unsafe_referrers(std::begin(kUntrustedURLs), 145 std::vector<std::string> unsafe_referrers(std::begin(kUntrustedURLs),
140 std::end(kUntrustedURLs)); 146 std::end(kUntrustedURLs));
141 147
142 std::string huge_referrer = "http://example.com/"; 148 std::string huge_referrer = "http://example.com/";
143 huge_referrer.append(INTERNET_MAX_URL_LENGTH * 2, 'a'); 149 huge_referrer.append(INTERNET_MAX_URL_LENGTH * 2, 'a');
144 unsafe_referrers.push_back(huge_referrer); 150 unsafe_referrers.push_back(huge_referrer);
145 151
146 for (const auto referrer_url : unsafe_referrers) { 152 for (const auto referrer_url : unsafe_referrers) {
147 SCOPED_TRACE(::testing::Message() << "Trying URL " << referrer_url); 153 SCOPED_TRACE(::testing::Message() << "Trying URL " << referrer_url);
148 ASSERT_EQ(5, base::WriteFile(test_file, "Hello", 5u)); 154 ASSERT_EQ(static_cast<int>(arraysize(kTestData)),
155 base::WriteFile(test_file, kTestData, arraysize(kTestData)));
149 EXPECT_EQ(QuarantineFileResult::OK, 156 EXPECT_EQ(QuarantineFileResult::OK,
150 QuarantineFile(test_file, GURL("http://example.com/good"), 157 QuarantineFile(test_file, GURL("http://example.com/good"),
151 GURL(referrer_url), kDummyClientGuid)); 158 GURL(referrer_url), kDummyClientGuid));
152 std::string motw_contents; 159 std::string motw_contents;
153 ASSERT_TRUE(base::ReadFileToString( 160 ASSERT_TRUE(base::ReadFileToString(
154 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); 161 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents));
155 // The actual assigned zone could be anything. So only testing that there is 162 // The actual assigned zone could be anything. So only testing that there is
156 // a zone annotation. 163 // a zone annotation.
157 EXPECT_FALSE(motw_contents.empty()); 164 EXPECT_FALSE(motw_contents.empty());
158 165
159 // These warnings aren't displayed on successful test runs. They are there 166 // These warnings aren't displayed on successful test runs. They are there
160 // so that we can check for deviations in behavior during manual testing. 167 // so that we can check for deviations in behavior during manual testing.
161 if (motw_contents != kMotwForInternetZone) 168 if (motw_contents != kMotwForInternetZone)
162 LOG(WARNING) << "Unexpected zone marker: " << motw_contents; 169 LOG(WARNING) << "Unexpected zone marker: " << motw_contents;
163 base::DeleteFile(test_file, false); 170 base::DeleteFile(test_file, false);
164 } 171 }
165 172
166 // Bucket 0 is SUCCESS_WITH_MOTW. 173 // Bucket 0 is SUCCESS_WITH_MOTW.
167 histogram_tester.ExpectUniqueSample("Download.AttachmentServices.Result", 0, 174 histogram_tester.ExpectUniqueSample("Download.AttachmentServices.Result", 0,
168 unsafe_referrers.size()); 175 unsafe_referrers.size());
169 } 176 }
170 177
171 // 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
172 // downloaded from the internet. 179 // downloaded from the internet.
173 TEST(QuarantineWinTest, EmptySource_DependsOnLocalConfig) { 180 TEST(QuarantineWinTest, EmptySource_DependsOnLocalConfig) {
174 base::HistogramTester histogram_tester; 181 base::HistogramTester histogram_tester;
175 base::ScopedTempDir test_dir; 182 base::ScopedTempDir test_dir;
176 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); 183 ASSERT_TRUE(test_dir.CreateUniqueTempDir());
177 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); 184 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe");
178 ASSERT_EQ(5, base::WriteFile(test_file, "Hello", 5u)); 185 ASSERT_EQ(static_cast<int>(arraysize(kTestData)),
186 base::WriteFile(test_file, kTestData, arraysize(kTestData)));
179 187
180 EXPECT_EQ(QuarantineFileResult::OK, 188 EXPECT_EQ(QuarantineFileResult::OK,
181 QuarantineFile(test_file, GURL(), GURL(), kDummyClientGuid)); 189 QuarantineFile(test_file, GURL(), GURL(), kDummyClientGuid));
182 std::string motw_contents; 190 std::string motw_contents;
183 ASSERT_TRUE(base::ReadFileToString( 191 ASSERT_TRUE(base::ReadFileToString(
184 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); 192 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents));
185 // 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
186 // zone annotation. 194 // zone annotation.
187 EXPECT_FALSE(motw_contents.empty()); 195 EXPECT_FALSE(motw_contents.empty());
188 196
189 // Bucket 0 is SUCCESS_WITH_MOTW. 197 // Bucket 0 is SUCCESS_WITH_MOTW.
190 histogram_tester.ExpectUniqueSample("Download.AttachmentServices.Result", 0, 198 histogram_tester.ExpectUniqueSample("Download.AttachmentServices.Result", 0,
191 1); 199 1);
192 } 200 }
193 201
194 // 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
195 // 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
196 // it and the test would fail. 204 // it and the test would fail.
197 TEST(QuarantineWinTest, EmptyFile) { 205 TEST(QuarantineWinTest, EmptyFile) {
206 base::HistogramTester histogram_tester;
198 base::ScopedTempDir test_dir; 207 base::ScopedTempDir test_dir;
199 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); 208 ASSERT_TRUE(test_dir.CreateUniqueTempDir());
200 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); 209 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe");
201 ASSERT_EQ(0, base::WriteFile(test_file, "", 0u)); 210 ASSERT_EQ(0, base::WriteFile(test_file, "", 0u));
202 211
203 EXPECT_EQ(QuarantineFileResult::OK, 212 EXPECT_EQ(QuarantineFileResult::OK,
204 QuarantineFile(test_file, net::FilePathToFileURL(test_file), GURL(), 213 QuarantineFile(test_file, net::FilePathToFileURL(test_file), GURL(),
205 kDummyClientGuid)); 214 kDummyClientGuid));
206 std::string motw_contents; 215 std::string motw_contents;
207 ASSERT_TRUE(base::ReadFileToString( 216 ASSERT_TRUE(base::ReadFileToString(
208 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); 217 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents));
209 EXPECT_STREQ(kMotwForInternetZone, motw_contents.c_str()); 218 EXPECT_STREQ(kMotwForInternetZone, motw_contents.c_str());
219
220 // Attachment services shouldn't have been invoked at all.
221 histogram_tester.ExpectTotalCount("Download.AttachmentServices.Result", 0);
210 } 222 }
211 223
212 // 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
213 // 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
214 // 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
215 // test would fail. 227 // test would fail.
216 TEST(QuarantineWinTest, NoClientGuid) { 228 TEST(QuarantineWinTest, NoClientGuid) {
217 base::ScopedTempDir test_dir; 229 base::ScopedTempDir test_dir;
218 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); 230 ASSERT_TRUE(test_dir.CreateUniqueTempDir());
219 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); 231 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe");
220 ASSERT_EQ(5, base::WriteFile(test_file, "Hello", 5u)); 232 ASSERT_EQ(static_cast<int>(arraysize(kTestData)),
233 base::WriteFile(test_file, kTestData, arraysize(kTestData)));
221 234
222 EXPECT_EQ(QuarantineFileResult::OK, 235 EXPECT_EQ(QuarantineFileResult::OK,
223 QuarantineFile(test_file, net::FilePathToFileURL(test_file), GURL(), 236 QuarantineFile(test_file, net::FilePathToFileURL(test_file), GURL(),
224 std::string())); 237 std::string()));
225 std::string motw_contents; 238 std::string motw_contents;
226 ASSERT_TRUE(base::ReadFileToString( 239 ASSERT_TRUE(base::ReadFileToString(
227 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); 240 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents));
228 EXPECT_STREQ(kMotwForInternetZone, motw_contents.c_str()); 241 EXPECT_STREQ(kMotwForInternetZone, motw_contents.c_str());
229 } 242 }
230 243
231 // 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
232 // 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
233 // 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.
234 TEST(QuarantineWinTest, SuperLongURL) { 247 TEST(QuarantineWinTest, SuperLongURL) {
235 base::ScopedTempDir test_dir; 248 base::ScopedTempDir test_dir;
236 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); 249 ASSERT_TRUE(test_dir.CreateUniqueTempDir());
237 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); 250 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe");
238 ASSERT_EQ(5, base::WriteFile(test_file, "Hello", 5u)); 251 ASSERT_EQ(static_cast<int>(arraysize(kTestData)),
252 base::WriteFile(test_file, kTestData, arraysize(kTestData)));
239 253
240 std::string source_url("http://example.com/"); 254 std::string source_url("http://example.com/");
241 source_url.append(INTERNET_MAX_URL_LENGTH * 2, 'a'); 255 source_url.append(INTERNET_MAX_URL_LENGTH * 2, 'a');
242 EXPECT_EQ(QuarantineFileResult::OK, 256 EXPECT_EQ(QuarantineFileResult::OK,
243 QuarantineFile(test_file, GURL(source_url), GURL(), std::string())); 257 QuarantineFile(test_file, GURL(source_url), GURL(), std::string()));
244 258
245 std::string motw_contents; 259 std::string motw_contents;
246 ASSERT_TRUE(base::ReadFileToString( 260 ASSERT_TRUE(base::ReadFileToString(
247 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); 261 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents));
248 EXPECT_STREQ(kMotwForInternetZone, motw_contents.c_str()); 262 EXPECT_STREQ(kMotwForInternetZone, motw_contents.c_str());
249 } 263 }
250 264
251 } // content 265 } // content
OLDNEW
« no previous file with comments | « content/browser/download/quarantine_win.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698