#include <stddef.h>
#include <stdint.h>
#include <memory>
#include <optional>
#include <tuple>
#include <utility>
#include <vector>
#include "base/compiler_specific.h"
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/format_macros.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/field_trial_params.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "base/test/mock_entropy_provider.h"
#include "base/test/scoped_feature_list.h"
#include "base/threading/thread_restrictions.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "components/download/public/common/download_danger_type.h"
#include "components/download/public/common/download_features.h"
#include "components/download/public/common/download_file_factory.h"
#include "components/download/public/common/download_file_impl.h"
#include "components/download/public/common/download_item.h"
#include "components/download/public/common/download_item_impl.h"
#include "components/download/public/common/download_stats.h"
#include "components/download/public/common/download_task_runner.h"
#include "components/download/public/common/parallel_download_configs.h"
#include "content/browser/download/download_manager_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_request_utils.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_paths.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/url_constants.h"
#include "content/public/common/webplugininfo.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_content_browser_client.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/download_test_observer.h"
#include "content/public/test/fenced_frame_test_util.h"
#include "content/public/test/prerender_test_util.h"
#include "content/public/test/slow_download_http_response.h"
#include "content/public/test/test_download_http_response.h"
#include "content/public/test/test_file_error_injector.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_utils.h"
#include "content/public/test/url_loader_monitor.h"
#include "content/shell/browser/shell.h"
#include "content/shell/browser/shell_browser_context.h"
#include "content/shell/browser/shell_download_manager_delegate.h"
#include "content/test/content_browser_test_utils_internal.h"
#include "content/test/fake_network_url_loader_factory.h"
#include "net/base/features.h"
#include "net/base/filename_util.h"
#include "net/dns/mock_host_resolver.h"
#include "net/http/http_connection_info.h"
#include "net/test/embedded_test_server/controllable_http_response.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "ppapi/buildflags/buildflags.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/input/web_mouse_event.h"
#include "third_party/blink/public/common/switches.h"
#include "url/gurl.h"
#include "url/origin.h"
#if BUILDFLAG(ENABLE_PLUGINS)
#include "content/browser/plugin_service_impl.h"
#endif
#if BUILDFLAG(IS_ANDROID)
#include "base/android/build_info.h"
#endif
_;
AllOf;
Field;
InSequence;
Property;
Return;
StrictMock;
Values;
namespace net {
class NetLogWithSource;
}
namespace content {
namespace {
constexpr int kTestRequestCount = …;
const int kPauseOffset = …;
const char kOriginOne[] = …;
const char kOriginTwo[] = …;
const char kOrigin[] = …;
const char kOriginSubdomain[] = …;
const char kOtherOrigin[] = …;
const char kBlogspotSite1[] = …;
const char kBlogspotSite2[] = …;
const char k404Response[] = …;
void ExpectRequestIsolationInfo(
const GURL& request_url,
const net::IsolationInfo& expected_isolation_info,
base::OnceCallback<void()> function) { … }
class DownloadTestContentBrowserClient
: public ContentBrowserTestContentBrowserClient { … };
class MockDownloadItemObserver : public download::DownloadItem::Observer { … };
class MockDownloadManagerObserver : public DownloadManager::Observer { … };
class DownloadFileWithDelayFactory;
static DownloadManagerImpl* DownloadManagerForShell(Shell* shell) { … }
class DownloadFileWithDelay : public download::DownloadFileImpl { … };
class DownloadFileWithDelayFactory : public download::DownloadFileFactory { … };
DownloadFileWithDelay::DownloadFileWithDelay(
std::unique_ptr<download::DownloadSaveInfo> save_info,
const base::FilePath& default_download_directory,
std::unique_ptr<download::InputStream> stream,
uint32_t download_id,
base::WeakPtr<download::DownloadDestinationObserver> observer,
base::WeakPtr<DownloadFileWithDelayFactory> owner)
: … { … }
DownloadFileWithDelay::~DownloadFileWithDelay() { … }
void DownloadFileWithDelay::RenameAndUniquify(
const base::FilePath& full_path,
RenameCompletionCallback callback) { … }
void DownloadFileWithDelay::RenameAndAnnotate(
const base::FilePath& full_path,
const std::string& client_guid,
const GURL& source_url,
const GURL& referrer_url,
mojo::PendingRemote<quarantine::mojom::Quarantine> remote_quarantine,
RenameCompletionCallback callback) { … }
void DownloadFileWithDelay::RenameCallbackWrapper(
const base::WeakPtr<DownloadFileWithDelayFactory>& factory,
RenameCompletionCallback original_callback,
download::DownloadInterruptReason reason,
const base::FilePath& path) { … }
DownloadFileWithDelayFactory::DownloadFileWithDelayFactory() { … }
DownloadFileWithDelayFactory::~DownloadFileWithDelayFactory() { … }
download::DownloadFile* DownloadFileWithDelayFactory::CreateFile(
std::unique_ptr<download::DownloadSaveInfo> save_info,
const base::FilePath& default_download_directory,
std::unique_ptr<download::InputStream> stream,
uint32_t download_id,
const base::FilePath& duplicate_download_file_path,
base::WeakPtr<download::DownloadDestinationObserver> observer) { … }
void DownloadFileWithDelayFactory::AddRenameCallback(
base::OnceClosure callback) { … }
void DownloadFileWithDelayFactory::GetAllRenameCallbacks(
std::vector<base::OnceClosure>* results) { … }
void DownloadFileWithDelayFactory::WaitForSomeCallback() { … }
class CountingDownloadFile : public download::DownloadFileImpl { … };
int CountingDownloadFile::active_files_ = …;
class CountingDownloadFileFactory : public download::DownloadFileFactory { … };
class ErrorInjectionDownloadFile : public download::DownloadFileImpl { … };
class ErrorInjectionDownloadFileFactory : public download::DownloadFileFactory { … };
class TestShellDownloadManagerDelegate : public ShellDownloadManagerDelegate { … };
class DownloadCreateObserver : DownloadManager::Observer { … };
class DownloadInProgressObserver : public DownloadTestObserverInProgress { … };
class DownloadCountingObserver : public download::DownloadItem::Observer { … };
class ReceivedSlicesCountingObserver : public DownloadCountingObserver { … };
class ErrorStreamCountingObserver : public DownloadCountingObserver { … };
class ReceivedBytesCountingObserver : public DownloadCountingObserver { … };
class NavigationStartObserver : public WebContentsObserver { … };
bool IsDownloadInState(download::DownloadItem::DownloadState state,
download::DownloadItem* item) { … }
std::unique_ptr<net::test_server::HttpResponse>
HandleRequestAndSendRedirectResponse(
const std::string& relative_url,
const GURL& target_url,
const net::test_server::HttpRequest& request) { … }
net::EmbeddedTestServer::HandleRequestCallback CreateRedirectHandler(
const std::string& relative_url,
const GURL& target_url) { … }
std::unique_ptr<net::test_server::HttpResponse>
HandleRequestAndSendBasicResponse(
const std::string& relative_url,
net::HttpStatusCode code,
const base::StringPairs& headers,
const std::string& content_type,
const std::string& body,
const net::test_server::HttpRequest& request) { … }
net::EmbeddedTestServer::HandleRequestCallback CreateBasicResponseHandler(
const std::string& relative_url,
net::HttpStatusCode code,
const base::StringPairs& headers,
const std::string& content_type,
const std::string& body) { … }
std::unique_ptr<net::test_server::HttpResponse> HandleRequestAndEchoCookies(
const std::string& relative_url,
const net::test_server::HttpRequest& request) { … }
net::EmbeddedTestServer::HandleRequestCallback CreateEchoCookieHandler(
const std::string& relative_url) { … }
std::unique_ptr<net::test_server::HttpResponse> HandleUploadRequest(
const net::test_server::HttpRequest& request) { … }
class TestRequestPauseHandler { … };
class DownloadContentTest : public ContentBrowserTest { … };
constexpr int kValidationLength = …;
class DownloadContentTestWithoutStrongValidators : public DownloadContentTest { … };
class ParallelDownloadTest : public DownloadContentTest { … };
class DownloadPrerenderTest : public DownloadContentTest { … };
class DownloadFencedFrameTest : public DownloadContentTest { … };
}
IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadCancelled) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, MultiDownload) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
InitiatedByWindowOpener_SameOrigin) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, InitiatedByWindowOpener_SameSite) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
InitiatedByWindowOpener_PrivateRegistry) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
InitiatedByWindowOpener_CrossOrigin) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
InitiatedByWindowOpener_CrossOrigin_NonHttpOrHttps) { … }
#if BUILDFLAG(ENABLE_PLUGINS)
IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadOctetStream) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
DownloadOctetStream_PassThroughServiceWorker) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
DownloadOctetStream_OctetStreamServiceWorker) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
DownloadOctetStream_RespondWithFetchServiceWorker) { … }
#endif
IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelAtFinalRename) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelAtRelease) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, ShutdownInProgress) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, ShutdownAtRelease) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, ResumeWithStrongValidators) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTestWithoutStrongValidators,
ResumeWithoutStrongValidators) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTestWithoutStrongValidators,
ResumeWithoutStrongValidatorsAndFailValidation) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, RedirectBeforeResume) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, RedirectWhileResume) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, RedirectDownload) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, FailCrossOriginDownload) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, RedirectUnsafeDownload) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, NoDownloadManagerDelegateDownload) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, BadRangeHeader) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, InvalidRangeHeader) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, BadEncoding) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, RestartIfNotPartialResponse) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, RestartIfNoETag) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, RestartIfNoPartialFile) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, RecoverFromInitFileError) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
RecoverFromIntermediateFileRenameError) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, RecoverFromFinalRenameError) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, Resume_Hash) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelInterruptedDownload) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveInterruptedDownload) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveCompletedDownload) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveResumingDownload) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelResumingDownload) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveResumedDownload) { … }
#if BUILDFLAG(IS_ANDROID) || \
(BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) && \
defined(ADDRESS_SANITIZER)
#define MAYBE_CancelResumedDownload …
#else
#define MAYBE_CancelResumedDownload …
#endif
IN_PROC_BROWSER_TEST_F(DownloadContentTest, MAYBE_CancelResumedDownload) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, ResumeRestoredDownload_NoFile) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, ResumeRestoredDownload_NoHash) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
ResumeRestoredDownload_EtagMismatch) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
ResumeRestoredDownload_CorrectHash) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, ResumeRestoredDownload_WrongHash) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, ResumeRestoredDownload_ShortFile) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, ResumeRestoredDownload_LongFile) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, ReferrerForPartialResumption) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, ReferrerForHTTPS) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, UpdateSiteForCookies) { … }
IN_PROC_BROWSER_TEST_F(
DownloadContentTest,
SiteForCookies_DownloadUrl_NotUpdateFirstPartyUrlOnRedirect) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
SiteForCookies_DownloadUrl_IsolationInfoPopulated) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
DownloadAttributeCrossOriginRedirect) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
DownloadAttributeSameOriginRedirect) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
DISABLED_DownloadAttributeSameOriginRedirectNavigation) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
DownloadAttributeSameOriginRedirectNavigationTimeOut) { … }
#if !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
DownloadAttributeSameOriginRedirectAltClick) { … }
#endif
IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadAttributeDataUrl) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadAttributeServerError) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadAttributeNetworkError) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadAttributeInvalidURL) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadAttributeBlobURL) { … }
class DownloadContentSameSiteCookieTest
: public DownloadContentTest,
public ::testing::WithParamInterface<bool> { … };
IN_PROC_BROWSER_TEST_P(DownloadContentSameSiteCookieTest,
DownloadAttributeSameSiteCookie) { … }
INSTANTIATE_TEST_SUITE_P(…);
IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadGZipWithNoContent) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, SniffedMimeType) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, SniffedMimeTypeForDownloadURL) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, DuplicateContentDisposition) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
AnchorDownload_Resume_IsolationInfoPopulated) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadAttributeSameOriginIFrame) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
DownloadAttributeCrossOriginIFrame) { … }
IN_PROC_BROWSER_TEST_F(ParallelDownloadTest, ParallelDownloadComplete) { … }
IN_PROC_BROWSER_TEST_F(ParallelDownloadTest, LastRequestRejected) { … }
IN_PROC_BROWSER_TEST_F(ParallelDownloadTest, SecondRequestRejected) { … }
IN_PROC_BROWSER_TEST_F(ParallelDownloadTest, OnlyFirstRequestValid) { … }
IN_PROC_BROWSER_TEST_F(ParallelDownloadTest, NoPartialResponse) { … }
IN_PROC_BROWSER_TEST_F(ParallelDownloadTest, Resumption) { … }
IN_PROC_BROWSER_TEST_F(ParallelDownloadTest, ResumptionLastSliceFinished) { … }
IN_PROC_BROWSER_TEST_F(ParallelDownloadTest, ResumptionWithOnlyOneSlice) { … }
IN_PROC_BROWSER_TEST_F(ParallelDownloadTest, ResumptionLastSliceUnfinished) { … }
IN_PROC_BROWSER_TEST_F(ParallelDownloadTest, ResumptionNoPartialResponse) { … }
#if BUILDFLAG(IS_FUCHSIA)
#define MAYBE_ResumptionMiddleSliceTemporaryError …
#else
#define MAYBE_ResumptionMiddleSliceTemporaryError …
#endif
IN_PROC_BROWSER_TEST_F(ParallelDownloadTest,
MAYBE_ResumptionMiddleSliceTemporaryError) { … }
IN_PROC_BROWSER_TEST_F(ParallelDownloadTest, MiddleSliceDelayedError) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadIgnoresXFO) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, FetchErrorResponseBody) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, UploadBytes) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, FetchErrorResponseBodyResumption) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadFromWebUI) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadFromWebUIWithoutRenderer) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest, SaveImageAt) { … }
class MhtmlDownloadTest : public DownloadContentTest { … };
IN_PROC_BROWSER_TEST_F(MhtmlDownloadTest,
AllowListForNonHTTPNotTriggerDownload) { … }
#if defined(THREAD_SANITIZER)
#define MAYBE_ForceDownloadMultipartRelatedPage …
#else
#define MAYBE_ForceDownloadMultipartRelatedPage …
#endif
IN_PROC_BROWSER_TEST_F(MhtmlDownloadTest,
MAYBE_ForceDownloadMultipartRelatedPage) { … }
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || defined(ADDRESS_SANITIZER)
#define MAYBE_ForceDownloadMessageRfc822Page …
#else
#define MAYBE_ForceDownloadMessageRfc822Page …
#endif
IN_PROC_BROWSER_TEST_F(MhtmlDownloadTest,
MAYBE_ForceDownloadMessageRfc822Page) { … }
class MhtmlLoadingTest : public DownloadContentTest { … };
IN_PROC_BROWSER_TEST_F(MhtmlLoadingTest,
AllowRenderMultipartRelatedPageFromFile) { … }
IN_PROC_BROWSER_TEST_F(MhtmlLoadingTest, AllowRenderMessageRfc822PageFromFile) { … }
IN_PROC_BROWSER_TEST_F(MhtmlLoadingTest,
DisallowRenderMultipartRelatedPageFromHTTP) { … }
IN_PROC_BROWSER_TEST_F(MhtmlLoadingTest,
DisallowRenderMessageRfc822PageFromHTTP) { … }
IN_PROC_BROWSER_TEST_F(MhtmlLoadingTest, DisallowRenderMessageRfc822Iframe) { … }
IN_PROC_BROWSER_TEST_F(DownloadPrerenderTest, DiscardNonNavigationDownload) { … }
IN_PROC_BROWSER_TEST_F(DownloadFencedFrameTest, DiscardNonNavigationDownload) { … }
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
DownloadAttributePreservesUserGesture) { … }
DownloadRangeTestParams;
class DownloadRangeTest
: public DownloadContentTest,
public ::testing::WithParamInterface<DownloadRangeTestParams> { … };
INSTANTIATE_TEST_SUITE_P(…);
IN_PROC_BROWSER_TEST_P(DownloadRangeTest, ArbitraryDownloadRangeTest) { … }
class DownloadRangeResumptionTest : public DownloadContentTest { … };
IN_PROC_BROWSER_TEST_F(DownloadRangeResumptionTest,
ArbitraryDownloadRangeResumptionTest) { … }
}