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 <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 #include <string> | 7 #include <string> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
16 #include "base/files/scoped_temp_dir.h" | 16 #include "base/files/scoped_temp_dir.h" |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
18 #include "base/path_service.h" | 18 #include "base/path_service.h" |
19 #include "base/run_loop.h" | 19 #include "base/run_loop.h" |
20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 21 #include "base/strings/stringprintf.h" |
21 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
22 #include "base/test/test_file_util.h" | 23 #include "base/test/test_file_util.h" |
23 #include "build/build_config.h" | 24 #include "build/build_config.h" |
24 #include "chrome/app/chrome_command_ids.h" | 25 #include "chrome/app/chrome_command_ids.h" |
25 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 26 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
26 #include "chrome/browser/download/download_history.h" | 27 #include "chrome/browser/download/download_history.h" |
27 #include "chrome/browser/download/download_prefs.h" | 28 #include "chrome/browser/download/download_prefs.h" |
28 #include "chrome/browser/download/download_service.h" | 29 #include "chrome/browser/download/download_service.h" |
29 #include "chrome/browser/download/download_service_factory.h" | 30 #include "chrome/browser/download/download_service_factory.h" |
30 #include "chrome/browser/download/save_package_file_picker.h" | 31 #include "chrome/browser/download/save_package_file_picker.h" |
(...skipping 34 matching lines...) Loading... |
65 using content::BrowserContext; | 66 using content::BrowserContext; |
66 using content::BrowserThread; | 67 using content::BrowserThread; |
67 using content::DownloadItem; | 68 using content::DownloadItem; |
68 using content::DownloadManager; | 69 using content::DownloadManager; |
69 using content::RenderFrameHost; | 70 using content::RenderFrameHost; |
70 using content::RenderProcessHost; | 71 using content::RenderProcessHost; |
71 using content::WebContents; | 72 using content::WebContents; |
72 using net::URLRequestMockHTTPJob; | 73 using net::URLRequestMockHTTPJob; |
73 using testing::ContainsRegex; | 74 using testing::ContainsRegex; |
74 using testing::HasSubstr; | 75 using testing::HasSubstr; |
| 76 using testing::Not; |
75 | 77 |
76 namespace { | 78 namespace { |
77 | 79 |
78 // Returns file contents with each continuous run of whitespace replaced by a | 80 // Returns file contents with each continuous run of whitespace replaced by a |
79 // single space. | 81 // single space. |
80 std::string ReadFileAndCollapseWhitespace(const base::FilePath& file_path) { | 82 std::string ReadFileAndCollapseWhitespace(const base::FilePath& file_path) { |
81 std::string file_contents; | 83 std::string file_contents; |
82 if (!base::ReadFileToString(file_path, &file_contents)) { | 84 if (!base::ReadFileToString(file_path, &file_contents)) { |
83 ADD_FAILURE() << "Failed to read \"" << file_path.value() << "\" file."; | 85 ADD_FAILURE() << "Failed to read \"" << file_path.value() << "\" file."; |
84 return std::string(); | 86 return std::string(); |
(...skipping 719 matching lines...) Loading... |
804 base::FilePath full_file_name, dir; | 806 base::FilePath full_file_name, dir; |
805 SaveCurrentTab(url, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML, | 807 SaveCurrentTab(url, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML, |
806 "iframe-src-is-a-download", 3, &dir, &full_file_name); | 808 "iframe-src-is-a-download", 3, &dir, &full_file_name); |
807 ASSERT_FALSE(HasFailure()); | 809 ASSERT_FALSE(HasFailure()); |
808 | 810 |
809 EXPECT_TRUE(base::PathExists(full_file_name)); | 811 EXPECT_TRUE(base::PathExists(full_file_name)); |
810 EXPECT_TRUE(base::PathExists(dir.AppendASCII("thisdayinhistory.html"))); | 812 EXPECT_TRUE(base::PathExists(dir.AppendASCII("thisdayinhistory.html"))); |
811 EXPECT_TRUE(base::PathExists(dir.AppendASCII("no-such-file.html"))); | 813 EXPECT_TRUE(base::PathExists(dir.AppendASCII("no-such-file.html"))); |
812 } | 814 } |
813 | 815 |
| 816 // Test that file: URI won't be saved when referred to from an HTTP page. |
| 817 // See also https://crbug.com/616429. |
| 818 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveUnauthorizedResource) { |
| 819 GURL url = NavigateToMockURL("unauthorized-access"); |
| 820 |
| 821 // Create a test file (that the web page should not have access to). |
| 822 base::ScopedTempDir temp_dir2; |
| 823 ASSERT_TRUE(temp_dir2.CreateUniqueTempDir()); |
| 824 base::FilePath file_path = |
| 825 temp_dir2.path().Append(FILE_PATH_LITERAL("should-not-save.jpg")); |
| 826 std::string file_content("fake-jpg"); |
| 827 ASSERT_LT( |
| 828 0, base::WriteFile(file_path, file_content.data(), file_content.size())); |
| 829 |
| 830 // Refer to the test file from the test page. |
| 831 GURL file_url = net::FilePathToFileURL(file_path); |
| 832 ASSERT_TRUE(ExecuteScript( |
| 833 browser()->tab_strip_model()->GetWebContentsAt(0), |
| 834 base::StringPrintf("document.getElementById('resource1').src = '%s';", |
| 835 file_url.spec().data()))); |
| 836 |
| 837 // Save the current page. |
| 838 base::FilePath full_file_name, dir; |
| 839 SaveCurrentTab(url, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML, |
| 840 "unauthorized-access", 2, &dir, &full_file_name); |
| 841 |
| 842 // We should not save resource that the web page didn't have access to. |
| 843 // (because executing a resource request can have side effects - for example |
| 844 // after https://crbug.com/590714 a website from the internet should not be |
| 845 // able to issue a resource request to an intranet website and trigger |
| 846 // server-side actions in the internet; this test uses a file: URI as a |
| 847 // canary for detecting whether a website can access restricted resources). |
| 848 EXPECT_FALSE(base::PathExists(dir.AppendASCII("should-not-save.jpg"))); |
| 849 } |
| 850 |
814 // Test suite that allows testing --site-per-process against cross-site frames. | 851 // Test suite that allows testing --site-per-process against cross-site frames. |
815 // See http://dev.chromium.org/developers/design-documents/site-isolation. | 852 // See http://dev.chromium.org/developers/design-documents/site-isolation. |
816 class SavePageSitePerProcessBrowserTest : public SavePageBrowserTest { | 853 class SavePageSitePerProcessBrowserTest : public SavePageBrowserTest { |
817 public: | 854 public: |
818 SavePageSitePerProcessBrowserTest() {} | 855 SavePageSitePerProcessBrowserTest() {} |
819 | 856 |
820 protected: | 857 protected: |
821 void SetUpCommandLine(base::CommandLine* command_line) override { | 858 void SetUpCommandLine(base::CommandLine* command_line) override { |
822 SavePageBrowserTest::SetUpCommandLine(command_line); | 859 SavePageBrowserTest::SetUpCommandLine(command_line); |
823 | 860 |
(...skipping 190 matching lines...) Loading... |
1014 // [saved version of the page] as for the "before" | 1051 // [saved version of the page] as for the "before" |
1015 // [the original version of the page]. | 1052 // [the original version of the page]. |
1016 ui_test_utils::NavigateToURL(browser(), | 1053 ui_test_utils::NavigateToURL(browser(), |
1017 GURL(net::FilePathToFileURL(full_file_name))); | 1054 GURL(net::FilePathToFileURL(full_file_name))); |
1018 DLOG(INFO) << "Verifying test expectations for saved page... : " | 1055 DLOG(INFO) << "Verifying test expectations for saved page... : " |
1019 << GetCurrentTab(browser())->GetLastCommittedURL(); | 1056 << GetCurrentTab(browser())->GetLastCommittedURL(); |
1020 AssertExpectationsAboutCurrentTab(expected_number_of_frames, | 1057 AssertExpectationsAboutCurrentTab(expected_number_of_frames, |
1021 expected_substrings); | 1058 expected_substrings); |
1022 } | 1059 } |
1023 | 1060 |
| 1061 // Helper method to deduplicate some code across 2 tests. |
| 1062 void RunObjectElementsTest(GURL url) { |
| 1063 content::SavePageType save_page_type = GetParam(); |
| 1064 |
| 1065 // 7 comes from: |
| 1066 // - main frame (frames-objects.htm) |
| 1067 // - object with frame-nested.htm + 2 subframes (frames-nested2.htm + b.htm) |
| 1068 // - iframe with a.htm |
| 1069 // - object with svg.svg |
| 1070 // - object with text.txt |
| 1071 // (pdf and png objects do not get a separate frame) |
| 1072 int expected_number_of_frames = 7; |
| 1073 |
| 1074 std::string arr[] = { |
| 1075 "frames-objects.htm: 8da13db4-a512-4d9b-b1c5-dc1c134234b9", |
| 1076 "a.htm: 1b8aae2b-e164-462f-bd5b-98aa366205f2", |
| 1077 "b.htm: 3a35f7fa-96a9-4487-9f18-4470263907fa", |
| 1078 "frames-nested.htm: 4388232f-8d45-4d2e-9807-721b381be153", |
| 1079 "frames-nested2.htm: 6d23dc47-f283-4977-96ec-66bcf72301a4", |
| 1080 "text-object.txt: ae52dd09-9746-4b7e-86a6-6ada5e2680c2", |
| 1081 }; |
| 1082 std::vector<std::string> expected_substrings(std::begin(arr), |
| 1083 std::end(arr)); |
| 1084 |
| 1085 // TODO(lukasza): crbug.com/553478: Enable <object> testing of MHTML. |
| 1086 if (save_page_type == content::SAVE_PAGE_TYPE_AS_MHTML) |
| 1087 return; |
| 1088 |
| 1089 TestOriginalVsSavedPage(save_page_type, url, expected_number_of_frames, |
| 1090 expected_substrings); |
| 1091 } |
| 1092 |
1024 private: | 1093 private: |
1025 void AssertExpectationsAboutCurrentTab( | 1094 void AssertExpectationsAboutCurrentTab( |
1026 int expected_number_of_frames, | 1095 int expected_number_of_frames, |
1027 const std::vector<std::string>& expected_substrings) { | 1096 const std::vector<std::string>& expected_substrings) { |
1028 int actual_number_of_frames = 0; | 1097 int actual_number_of_frames = 0; |
1029 GetCurrentTab(browser())->ForEachFrame(base::Bind( | 1098 GetCurrentTab(browser())->ForEachFrame(base::Bind( |
1030 &IncrementInteger, base::Unretained(&actual_number_of_frames))); | 1099 &IncrementInteger, base::Unretained(&actual_number_of_frames))); |
1031 EXPECT_EQ(expected_number_of_frames, actual_number_of_frames); | 1100 EXPECT_EQ(expected_number_of_frames, actual_number_of_frames); |
1032 | 1101 |
1033 for (const auto& expected_substring : expected_substrings) { | 1102 for (const auto& expected_substring : expected_substrings) { |
(...skipping 49 matching lines...) Loading... |
1083 std::vector<std::string> expected_substrings(std::begin(arr), std::end(arr)); | 1152 std::vector<std::string> expected_substrings(std::begin(arr), std::end(arr)); |
1084 | 1153 |
1085 GURL url( | 1154 GURL url( |
1086 embedded_test_server()->GetURL("a.com", "/save_page/frames-xsite.htm")); | 1155 embedded_test_server()->GetURL("a.com", "/save_page/frames-xsite.htm")); |
1087 | 1156 |
1088 TestOriginalVsSavedPage(save_page_type, url, 3, expected_substrings); | 1157 TestOriginalVsSavedPage(save_page_type, url, 3, expected_substrings); |
1089 } | 1158 } |
1090 | 1159 |
1091 // Test compares original-vs-saved for a page with <object> elements. | 1160 // Test compares original-vs-saved for a page with <object> elements. |
1092 // (see crbug.com/553478). | 1161 // (see crbug.com/553478). |
1093 IN_PROC_BROWSER_TEST_P(SavePageOriginalVsSavedComparisonTest, ObjectElements) { | 1162 IN_PROC_BROWSER_TEST_P(SavePageOriginalVsSavedComparisonTest, |
1094 content::SavePageType save_page_type = GetParam(); | 1163 ObjectElementsViaHttp) { |
1095 | |
1096 // 4 = main frame + iframe + object w/ html doc + object w/ pdf doc | |
1097 // (svg and png objects do not get a separate frame) | |
1098 int expected_number_of_frames = 6; | |
1099 | |
1100 std::string arr[] = { | |
1101 "frames-objects.htm: 8da13db4-a512-4d9b-b1c5-dc1c134234b9", | |
1102 "a.htm: 1b8aae2b-e164-462f-bd5b-98aa366205f2", | |
1103 "b.htm: 3a35f7fa-96a9-4487-9f18-4470263907fa", | |
1104 "frames-nested.htm: 4388232f-8d45-4d2e-9807-721b381be153", | |
1105 "frames-nested2.htm: 6d23dc47-f283-4977-96ec-66bcf72301a4", | |
1106 }; | |
1107 std::vector<std::string> expected_substrings(std::begin(arr), std::end(arr)); | |
1108 | |
1109 GURL url( | 1164 GURL url( |
1110 embedded_test_server()->GetURL("a.com", "/save_page/frames-objects.htm")); | 1165 embedded_test_server()->GetURL("a.com", "/save_page/frames-objects.htm")); |
1111 | 1166 |
1112 // TODO(lukasza): crbug.com/553478: Enable <object> testing of MHTML. | 1167 RunObjectElementsTest(url); |
1113 if (save_page_type == content::SAVE_PAGE_TYPE_AS_MHTML) | 1168 } |
1114 return; | |
1115 | 1169 |
1116 TestOriginalVsSavedPage(save_page_type, url, expected_number_of_frames, | 1170 // Tests that saving a page from file: URI works. |
1117 expected_substrings); | 1171 IN_PROC_BROWSER_TEST_P(SavePageOriginalVsSavedComparisonTest, |
| 1172 ObjectElementsViaFile) { |
| 1173 base::FilePath test_data_dir; |
| 1174 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir)); |
| 1175 GURL url(net::FilePathToFileURL( |
| 1176 test_data_dir.Append(FILE_PATH_LITERAL("save_page/frames-objects.htm")))); |
| 1177 EXPECT_TRUE(url.SchemeIsFile()); |
| 1178 |
| 1179 RunObjectElementsTest(url); |
1118 } | 1180 } |
1119 | 1181 |
1120 // Test compares original-vs-saved for a page with frames at about:blank uri. | 1182 // Test compares original-vs-saved for a page with frames at about:blank uri. |
1121 // This tests handling of iframe elements without src attribute (only with | 1183 // This tests handling of iframe elements without src attribute (only with |
1122 // srcdoc attribute) and how they get saved / cross-referenced. | 1184 // srcdoc attribute) and how they get saved / cross-referenced. |
1123 IN_PROC_BROWSER_TEST_P(SavePageOriginalVsSavedComparisonTest, AboutBlank) { | 1185 IN_PROC_BROWSER_TEST_P(SavePageOriginalVsSavedComparisonTest, AboutBlank) { |
1124 content::SavePageType save_page_type = GetParam(); | 1186 content::SavePageType save_page_type = GetParam(); |
1125 | 1187 |
1126 std::string arr[] = { | 1188 std::string arr[] = { |
1127 "main: acb0609d-eb10-4c26-83e2-ad8afb7b0ff3", | 1189 "main: acb0609d-eb10-4c26-83e2-ad8afb7b0ff3", |
(...skipping 127 matching lines...) Loading... |
1255 TestOriginalVsSavedPage(save_page_type, url, 1, expected_substrings); | 1317 TestOriginalVsSavedPage(save_page_type, url, 1, expected_substrings); |
1256 } | 1318 } |
1257 | 1319 |
1258 INSTANTIATE_TEST_CASE_P( | 1320 INSTANTIATE_TEST_CASE_P( |
1259 SaveType, | 1321 SaveType, |
1260 SavePageOriginalVsSavedComparisonTest, | 1322 SavePageOriginalVsSavedComparisonTest, |
1261 ::testing::Values(content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML, | 1323 ::testing::Values(content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML, |
1262 content::SAVE_PAGE_TYPE_AS_MHTML)); | 1324 content::SAVE_PAGE_TYPE_AS_MHTML)); |
1263 | 1325 |
1264 } // namespace | 1326 } // namespace |
OLD | NEW |