OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/download/base_file.h" |
| 6 |
| 7 #include "base/files/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "content/public/browser/download_interrupt_reasons.h" |
| 10 #include "content/public/test/test_browser_thread_bundle.h" |
| 11 #include "net/base/filename_util.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 TEST(BaseFileWin, AnnotateWithSourceInformation) { |
| 17 const base::FilePath::CharType kZoneIdentifierStreamName[] = |
| 18 FILE_PATH_LITERAL(":Zone.Identifier"); |
| 19 const char kInternetZoneIdentifierString[] = "[ZoneTransfer]\r\nZoneId=3\r\n"; |
| 20 |
| 21 struct { |
| 22 const char* const url; |
| 23 const char* const referrer; |
| 24 bool expected_internet_zone; |
| 25 } kTestCases[] = { |
| 26 // Test cases where we expect a MOTW. |
| 27 {"http://example.com", "http://example.com", true}, |
| 28 {"", "http://example.com", true}, |
| 29 {"", "", true}, |
| 30 {"http://example.com", "", true}, |
| 31 {"data:text/plain,Foo", "http://example.com", true}, |
| 32 {"data:text/plain,Foo", "", true}, |
| 33 {"data:text/plain,Foo", "data:text/plain,Bar", true}, |
| 34 {"data:text/plain,Foo", "ftp://localhost/foo", true}, |
| 35 {"http://example.com", "http://localhost/foo", true}, |
| 36 {"ftp://example.com/foo", "", true}, |
| 37 |
| 38 // Test cases where we don't expect a MOTW. These test cases result in |
| 39 // different behavior across Windows versions. |
| 40 {"ftp://localhost/foo", "", false}, |
| 41 {"http://localhost/foo", "", false}, |
| 42 {"", "http://localhost/foo", false}, |
| 43 {"file:///exists.txt", "", false}, |
| 44 {"file:///exists.txt", "http://example.com", false}, |
| 45 {"file:///does-not-exist.txt", "", false}, |
| 46 }; |
| 47 |
| 48 content::TestBrowserThreadBundle threads; |
| 49 base::ScopedTempDir target_directory; |
| 50 ASSERT_TRUE(target_directory.CreateUniqueTempDir()); |
| 51 |
| 52 ASSERT_EQ(6, |
| 53 base::WriteFile(target_directory.path().AppendASCII("exists.txt"), |
| 54 "Exists", 6)); |
| 55 |
| 56 for (const auto& test_case : kTestCases) { |
| 57 GURL url(test_case.url); |
| 58 GURL referrer(test_case.referrer); |
| 59 |
| 60 // Resolve file:// URLs relative to our temp directory. |
| 61 if (url.SchemeIsFile()) { |
| 62 base::FilePath relative_path = |
| 63 base::FilePath().AppendASCII(url.path().substr(1)); |
| 64 url = |
| 65 net::FilePathToFileURL(target_directory.path().Append(relative_path)); |
| 66 } |
| 67 |
| 68 SCOPED_TRACE(::testing::Message() << "Source URL: " << url.spec() |
| 69 << " Referrer: " << test_case.referrer); |
| 70 |
| 71 BaseFile base_file((net::NetLogWithSource())); |
| 72 ASSERT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 73 base_file.Initialize(base::FilePath(), target_directory.path(), |
| 74 base::File(), 0, std::string(), |
| 75 std::unique_ptr<crypto::SecureHash>())); |
| 76 ASSERT_FALSE(base_file.full_path().empty()); |
| 77 ASSERT_EQ( |
| 78 DOWNLOAD_INTERRUPT_REASON_NONE, |
| 79 base_file.Rename(target_directory.path().AppendASCII("test_file.doc"))); |
| 80 ASSERT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 81 base_file.AnnotateWithSourceInformation( |
| 82 "7B2CEE7C-DC81-4160-86F1-9C968597118F", url, referrer)); |
| 83 base_file.Detach(); |
| 84 base_file.Finish(); |
| 85 |
| 86 base::FilePath path = base_file.full_path(); |
| 87 base::FilePath zone_identifier_stream(path.value() + |
| 88 kZoneIdentifierStreamName); |
| 89 |
| 90 ASSERT_TRUE(base::PathExists(path)); |
| 91 |
| 92 std::string zone_identifier; |
| 93 base::ReadFileToString(zone_identifier_stream, &zone_identifier); |
| 94 |
| 95 if (test_case.expected_internet_zone) { |
| 96 EXPECT_STREQ(kInternetZoneIdentifierString, zone_identifier.c_str()); |
| 97 } else { |
| 98 // Seeing an unexpected zone identifier is not an error, but we log a |
| 99 // warning just the same so that such cases can be identified during |
| 100 // manual testing. |
| 101 if (zone_identifier == kInternetZoneIdentifierString) { |
| 102 LOG(WARNING) << "Unexpected internet zone annotation for Source:" |
| 103 << url.spec() << " Referrer:" << test_case.referrer; |
| 104 } else if (!zone_identifier.empty()) { |
| 105 LOG(WARNING) << "Unexpected zone annotation for Source:" << url.spec() |
| 106 << " Referrer:" << test_case.referrer |
| 107 << " Annotation:" << std::endl |
| 108 << zone_identifier; |
| 109 } |
| 110 } |
| 111 base::DeleteFile(path, false); |
| 112 } |
| 113 } |
| 114 |
| 115 } // namespace content |
OLD | NEW |