#include <stddef.h>
#include <stdint.h>
#include <string>
#include <utility>
#include <vector>
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/repeating_test_future.h"
#include "base/test/test_file_util.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/download/chrome_download_manager_delegate.h"
#include "chrome/browser/download/download_core_service.h"
#include "chrome/browser/download/download_core_service_factory.h"
#include "chrome/browser/download/download_history.h"
#include "chrome/browser/download/download_prefs.h"
#include "chrome/browser/download/save_package_file_picker.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/download/public/common/download_item.h"
#include "components/history/core/browser/download_constants.h"
#include "components/history/core/browser/download_row.h"
#include "components/prefs/pref_member.h"
#include "components/prefs/pref_service.h"
#include "components/security_state/core/security_state.h"
#include "components/services/quarantine/test_support.h"
#include "content/public/browser/download_manager.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/download_test_observer.h"
#include "content/public/test/no_renderer_crashes_assertion.h"
#include "net/base/filename_util.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/shell_dialogs/fake_select_file_dialog.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chromeos/dbus/dlp/dlp_client.h"
#endif
BrowserContext;
BrowserThread;
DownloadManager;
RenderFrameHost;
RenderProcessHost;
WebContents;
DownloadItem;
_;
ContainsRegex;
HasSubstr;
FakeSelectFileDialog;
namespace {
std::string ReadFileAndCollapseWhitespace(const base::FilePath& file_path) { … }
std::string WriteSavedFromPath(const std::string& file_contents,
const GURL& url) { … }
class DownloadPersistedObserver : public DownloadHistory::Observer { … };
class DownloadRemovedObserver : public DownloadPersistedObserver { … };
bool DownloadStoredProperly(const GURL& expected_url,
const base::FilePath& expected_path,
int64_t num_files,
history::DownloadState expected_state,
DownloadItem* item,
const history::DownloadRow& info) { … }
static const char kAppendedExtension[] = …;
class DownloadItemCreatedObserver : public DownloadManager::Observer { … };
class SavePageBrowserTest : public InProcessBrowserTest { … };
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { … }
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveFileURL) { … }
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest,
SaveHTMLOnly_CrossOriginReadPolicy) { … }
#if BUILDFLAG(IS_MAC) && defined(ARCH_CPU_ARM64)
#define MAYBE_SaveHTMLOnlyCancel …
#else
#define MAYBE_SaveHTMLOnlyCancel …
#endif
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, MAYBE_SaveHTMLOnlyCancel) { … }
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLWithLongTextContent) { … }
class DelayingDownloadManagerDelegate : public ChromeDownloadManagerDelegate { … };
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, DISABLED_SaveHTMLOnlyTabDestroy) { … }
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) { … }
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) { … }
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest,
SaveDuringInitialNavigationIncognito) { … }
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) { … }
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) { … }
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, RemoveFromList) { … }
#if BUILDFLAG(IS_WIN)
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, CleanFilenameFromPageTitle) {
base::FilePath download_dir =
DownloadPrefs::FromDownloadManager(GetDownloadManager())->
DownloadPath();
base::FilePath full_file_name =
download_dir.AppendASCII(std::string("test.exe") + kAppendedExtension);
base::FilePath dir = download_dir.AppendASCII("test.exe_files");
base::ScopedAllowBlockingForTesting allow_blocking;
EXPECT_FALSE(base::PathExists(full_file_name));
GURL url = embedded_test_server()->GetURL("/save_page/c.htm");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
SavePackageFilePicker::SetShouldPromptUser(false);
base::RunLoop run_loop;
content::SavePackageFinishedObserver observer(
browser()->profile()->GetDownloadManager(), run_loop.QuitClosure());
chrome::SavePage(browser());
run_loop.Run();
EXPECT_TRUE(base::PathExists(full_file_name));
EXPECT_TRUE(base::DieFileDie(full_file_name, false));
EXPECT_TRUE(base::DieFileDie(dir, true));
}
#endif
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SecurityLevelHistogram) { … }
#if BUILDFLAG(IS_WIN)
#define MAYBE_SavePageAsMHTML …
#else
#define MAYBE_SavePageAsMHTML …
#endif
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, MAYBE_SavePageAsMHTML) { … }
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest,
SavePageAsMHTMLByPrefUpdatesExtension) { … }
#if BUILDFLAG(IS_WIN)
#define MAYBE_SavePageBrowserTest_NonMHTML …
#else
#define MAYBE_SavePageBrowserTest_NonMHTML …
#endif
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest,
MAYBE_SavePageBrowserTest_NonMHTML) { … }
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, DangerousSubresources) { … }
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveDownloadableIFrame) { … }
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveUnauthorizedResource) { … }
#if BUILDFLAG(IS_WIN)
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveURLQuarantine) {
GURL url = embedded_test_server()->GetURL("/save_page/text.txt");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
base::FilePath full_file_name, dir;
SaveCurrentTab(url, content::SAVE_PAGE_TYPE_AS_ONLY_HTML, "test", 1, &dir,
&full_file_name);
ASSERT_FALSE(HasFailure());
base::ScopedAllowBlockingForTesting allow_blocking;
EXPECT_TRUE(base::PathExists(full_file_name));
EXPECT_FALSE(base::PathExists(dir));
EXPECT_TRUE(base::ContentsEqual(GetTestDirFile("text.txt"), full_file_name));
EXPECT_TRUE(quarantine::IsFileQuarantined(full_file_name, url, GURL()));
}
#endif
class SavePageSitePerProcessBrowserTest : public SavePageBrowserTest { … };
IN_PROC_BROWSER_TEST_F(SavePageSitePerProcessBrowserTest, SaveAsCompleteHtml) { … }
#if BUILDFLAG(IS_MAC)
#define MAYBE_SaveAsMHTML …
#else
#define MAYBE_SaveAsMHTML …
#endif
IN_PROC_BROWSER_TEST_F(SavePageSitePerProcessBrowserTest,
MAYBE_SaveAsMHTML) { … }
IN_PROC_BROWSER_TEST_F(SavePageSitePerProcessBrowserTest,
CompleteHtmlWhenRendererIsDead) { … }
class SavePageOriginalVsSavedComparisonTest
: public SavePageSitePerProcessBrowserTest,
public ::testing::WithParamInterface<content::SavePageType> { … };
IN_PROC_BROWSER_TEST_P(SavePageOriginalVsSavedComparisonTest, CrossSite) { … }
IN_PROC_BROWSER_TEST_P(SavePageOriginalVsSavedComparisonTest,
DISABLED_ObjectElementsViaHttp) { … }
IN_PROC_BROWSER_TEST_P(SavePageOriginalVsSavedComparisonTest,
DISABLED_ObjectElementsViaFile) { … }
#if BUILDFLAG(IS_MAC)
#define MAYBE_AboutBlank …
#else
#define MAYBE_AboutBlank …
#endif
IN_PROC_BROWSER_TEST_P(SavePageOriginalVsSavedComparisonTest,
MAYBE_AboutBlank) { … }
IN_PROC_BROWSER_TEST_P(SavePageOriginalVsSavedComparisonTest, NestedFrames) { … }
#if BUILDFLAG(IS_MAC)
#define MAYBE_RuntimeChanges …
#else
#define MAYBE_RuntimeChanges …
#endif
IN_PROC_BROWSER_TEST_P(SavePageOriginalVsSavedComparisonTest,
MAYBE_RuntimeChanges) { … }
IN_PROC_BROWSER_TEST_P(SavePageOriginalVsSavedComparisonTest, Encoding) { … }
#if BUILDFLAG(IS_MAC)
#define MAYBE_Style …
#else
#define MAYBE_Style …
#endif
IN_PROC_BROWSER_TEST_P(SavePageOriginalVsSavedComparisonTest, MAYBE_Style) { … }
IN_PROC_BROWSER_TEST_P(SavePageOriginalVsSavedComparisonTest, BrokenImage) { … }
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
#define MAYBE_CrossSiteObject …
#else
#define MAYBE_CrossSiteObject …
#endif
IN_PROC_BROWSER_TEST_P(SavePageOriginalVsSavedComparisonTest,
MAYBE_CrossSiteObject) { … }
INSTANTIATE_TEST_SUITE_P(…);
INSTANTIATE_TEST_SUITE_P(…);
class BlockingDownloadManagerDelegate : public ChromeDownloadManagerDelegate { … };
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveOnlyHTMLBlocked) { … }
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTMLBlocked) { … }
#if BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLWithDlp) {
base::FilePath full_file_name, dir;
GURL url;
chromeos::DlpClient::Shutdown();
chromeos::DlpClient::InitializeFake();
base::test::RepeatingTestFuture<
dlp::AddFilesRequest, base::OnceCallback<void(dlp::AddFilesResponse)>>
add_file_cb;
chromeos::DlpClient::Get()->GetTestInterface()->SetAddFilesMock(
add_file_cb.GetCallback());
url = NavigateToMockURL("a");
SaveCurrentTab(url, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML, "a", 1, &dir,
&full_file_name);
ASSERT_FALSE(HasFailure());
auto request = std::get<0>(add_file_cb.Take());
EXPECT_EQ(1, request.add_file_requests().size());
EXPECT_EQ(full_file_name.value(), request.add_file_requests(0).file_path());
EXPECT_EQ(request.add_file_requests(0).source_url(), url.spec());
base::ScopedAllowBlockingForTesting allow_blocking;
EXPECT_TRUE(base::PathExists(full_file_name));
EXPECT_FALSE(base::PathExists(dir));
}
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveMHTMLWithDlp) {
base::FilePath full_file_name, dir;
GURL url;
chromeos::DlpClient::Shutdown();
chromeos::DlpClient::InitializeFake();
base::test::RepeatingTestFuture<
dlp::AddFilesRequest, base::OnceCallback<void(dlp::AddFilesResponse)>>
add_file_cb;
chromeos::DlpClient::Get()->GetTestInterface()->SetAddFilesMock(
add_file_cb.GetCallback());
url = NavigateToMockURL("a");
SaveCurrentTab(url, content::SAVE_PAGE_TYPE_AS_MHTML, "a", -1, &dir,
&full_file_name);
ASSERT_FALSE(HasFailure());
auto request = std::get<0>(add_file_cb.Take());
EXPECT_EQ(1, request.add_file_requests().size());
EXPECT_EQ(full_file_name.value(), request.add_file_requests(0).file_path());
EXPECT_EQ(request.add_file_requests(0).source_url(), url.spec());
base::ScopedAllowBlockingForTesting allow_blocking;
EXPECT_TRUE(base::PathExists(full_file_name));
EXPECT_FALSE(base::PathExists(dir));
}
#endif
}