chromium/content/browser/download/download_browsertest.cc

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This file contains download browser tests that are known to be runnable
// in a pure content context.  Over time tests should be migrated here.

#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 {

// Default request count for parallel download tests.
constexpr int kTestRequestCount =;

// Offset for download to pause.
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) {}

// Implementation of TestContentBrowserClient that overrides
// AllowRenderingMhtmlOverHttp() and allows consumers to set a value.
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 {};

// All routines on this class must be called on the UI thread.
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) {}

// static
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 {};

// Factory for creating download files that allow error injection. All routines
// on this class must be called on the UI thread.
class ErrorInjectionDownloadFileFactory : public download::DownloadFileFactory {};

class TestShellDownloadManagerDelegate : public ShellDownloadManagerDelegate {};

// Get the next created download.
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 to wait for a WebContents to kick off a specified number of
// navigations.
class NavigationStartObserver : public WebContentsObserver {};

bool IsDownloadInState(download::DownloadItem::DownloadState state,
                       download::DownloadItem* item) {}

// Request handler to be used with CreateRedirectHandler().
std::unique_ptr<net::test_server::HttpResponse>
HandleRequestAndSendRedirectResponse(
    const std::string& relative_url,
    const GURL& target_url,
    const net::test_server::HttpRequest& request) {}

// Creates a request handler for EmbeddedTestServer that responds with a HTTP
// 302 redirect if the request URL matches |relative_url|.
net::EmbeddedTestServer::HandleRequestCallback CreateRedirectHandler(
    const std::string& relative_url,
    const GURL& target_url) {}

// Request handler to be used with CreateBasicResponseHandler().
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) {}

// Creates a request handler for an EmbeddedTestServer that response with an
// HTTP 200 status code, a Content-Type header and a body.
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) {}

// Creates a request handler for an EmbeddedTestServer that echos the value
// of the cookie header back as a body, and sends a Content-Disposition header.
net::EmbeddedTestServer::HandleRequestCallback CreateEchoCookieHandler(
    const std::string& relative_url) {}

// A request handler that takes the content of the request and sends it back on
// the response.
std::unique_ptr<net::test_server::HttpResponse> HandleUploadRequest(
    const net::test_server::HttpRequest& request) {}

// Helper class to "flatten" handling of
// TestDownloadHttpResponse::OnPauseHandler.
class TestRequestPauseHandler {};

class DownloadContentTest : public ContentBrowserTest {};

constexpr int kValidationLength =;

class DownloadContentTestWithoutStrongValidators : public DownloadContentTest {};

// Test fixture for parallel downloading.
class ParallelDownloadTest : public DownloadContentTest {};

class DownloadPrerenderTest : public DownloadContentTest {};

class DownloadFencedFrameTest : public DownloadContentTest {};

}  // namespace

// Flaky. See https://crbug.com/754679.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadCancelled) {}

// Check that downloading multiple (in this case, 2) files does not result in
// corrupted files.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, MultiDownload) {}

// Tests that metrics are recorded when a page opens a named window, navigates
// it to a URL, then navigates it again to a download. The navigated URL is same
// origin as the opener (example.com). The actual download URL doesn't matter.
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
                       InitiatedByWindowOpener_SameOrigin) {}

// Same as InitiatedByWindowOpener_SameOrigin, but the navigated URL is same
// site as the opener (example.com vs one.example.com).
IN_PROC_BROWSER_TEST_F(DownloadContentTest, InitiatedByWindowOpener_SameSite) {}

// The opener and the openee are under the same domain name blogspot.com, but
// blogspot.com is a private registry according to the Public Suffix List, so
// its subdomains are not considered same host.
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
                       InitiatedByWindowOpener_PrivateRegistry) {}

// Same as InitiatedByWindowOpener_SameOrigin, but the navigated URL is cross
// origin to the opener (example.com vs example.site).
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
                       InitiatedByWindowOpener_CrossOrigin) {}

// Same as InitiatedByWindowOpener_CrossOrigin, but the newly opened tab is
// about:blank.
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
                       InitiatedByWindowOpener_CrossOrigin_NonHttpOrHttps) {}

#if BUILDFLAG(ENABLE_PLUGINS)
// Content served with a MIME type of application/octet-stream should be
// downloaded even when a plugin can be found that handles the file type.
// See https://crbug.com/104331 for the details.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadOctetStream) {}

// Content served with a MIME type of application/octet-stream should be
// downloaded even when a plugin can be found that handles the file type.
// See https://crbug.com/104331 for the details.
// In this test, the url is in scope of a service worker but the response is
// served from network.
// This is regression test for https://crbug.com/896696.
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
                       DownloadOctetStream_PassThroughServiceWorker) {}

// Content served with a MIME type of application/octet-stream should be
// downloaded even when a plugin can be found that handles the file type.
// See https://crbug.com/104331 for the details.
// In this test, the response will be served from a service worker.
// This is regression test for https://crbug.com/896696.
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
                       DownloadOctetStream_OctetStreamServiceWorker) {}

// Content served with a MIME type of application/octet-stream should be
// downloaded even when a plugin can be found that handles the file type.
// See https://crbug.com/104331 for the details.
// In this test, the url is in scope of a service worker and the response is
// served from the network via service worker.
// This is regression test for https://crbug.com/896696.
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
                       DownloadOctetStream_RespondWithFetchServiceWorker) {}

#endif

// Try to cancel just before we release the download file, by delaying final
// rename callback.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelAtFinalRename) {}

// Try to cancel just after we release the download file, by delaying
// in ShouldOpenDownload.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelAtRelease) {}

// Try to shutdown with a download in progress to make sure shutdown path
// works properly.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, ShutdownInProgress) {}

// Try to shutdown just after we release the download file, by delaying
// release.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, ShutdownAtRelease) {}

// Test resumption with a response that contains strong validators.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, ResumeWithStrongValidators) {}

// Test resumption when strong validators are not present in the response.
IN_PROC_BROWSER_TEST_F(DownloadContentTestWithoutStrongValidators,
                       ResumeWithoutStrongValidators) {}

// Test resumption when strong validators are not present in the response and
// the content of the download changes.
IN_PROC_BROWSER_TEST_F(DownloadContentTestWithoutStrongValidators,
                       ResumeWithoutStrongValidatorsAndFailValidation) {}

// Resumption should only attempt to contact the final URL if the download has a
// URL chain.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, RedirectBeforeResume) {}

// If a resumption request results in a redirect, the response should be ignored
// and the download should be marked as interrupted again.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, RedirectWhileResume) {}

// Verify that DownloadUrl can support URL redirect.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, RedirectDownload) {}

// Verify that DownloadUrl can detect and fail a cross-origin URL redirect.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, FailCrossOriginDownload) {}

// Verify that DownloadUrl() to URL with unsafe scheme should fail.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, RedirectUnsafeDownload) {}

// Verify that DownloadUrl() with no DownloadManagerDelegate drops the download.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, NoDownloadManagerDelegateDownload) {}

// If the server response for the resumption request specifies a bad range (i.e.
// not the range that was requested), then the download should be marked as
// interrupted and restart from the beginning.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, BadRangeHeader) {}

// If the server response for the resumption request specifies an invalid range,
// then the download should be marked as interrupted and as interrupted again
// without discarding the partial state.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, InvalidRangeHeader) {}

// If the server response for the resumption request cannot be decoded,
// the download will need to restart. This is to simulate some servers
// that doesn't handle range request properly.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, BadEncoding) {}

// A partial resumption results in an HTTP 200 response. I.e. the server ignored
// the range request and sent the entire resource instead. For If-Range requests
// (as opposed to If-Match), the behavior for a precondition failure is also to
// respond with a 200. So this test case covers both validation failure and
// ignoring the range request.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, RestartIfNotPartialResponse) {}

// Confirm we restart if we don't have a verifier.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, RestartIfNoETag) {}

// Partial file goes missing before the download is resumed. The download should
// restart.
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) {}

// An interrupted download should remove the intermediate file when it is
// cancelled.
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) {}

// TODO(qinmin): Flaky crashes on ASAN Linux. https://crbug.com/836689
#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) {}

// Test that the referrer header is set correctly for a download that's resumed
// partially.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, ReferrerForPartialResumption) {}

// Test that the referrer header is dropped for HTTP downloads from HTTPS.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, ReferrerForHTTPS) {}

// Check that the site-for-cookies is correctly updated when downloading a file
// that redirects cross site, by verifying that a SameSite cookie can be set
// following a cross-site redirect.
// (It is not enough to redirect across origins with the same host but different
// port numbers, because cookies do not respect ports.)
IN_PROC_BROWSER_TEST_F(DownloadContentTest, UpdateSiteForCookies) {}

// Tests that if `update_first_party_url_on_redirect` is set to false, download
// will not behave like a top-level frame navigation and SameSite=Strict cookies
// will not be set on a redirection.
IN_PROC_BROWSER_TEST_F(
    DownloadContentTest,
    SiteForCookies_DownloadUrl_NotUpdateFirstPartyUrlOnRedirect) {}

// Verifies that isolation info set in DownloadUrlParameters can be populated.
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
                       SiteForCookies_DownloadUrl_IsolationInfoPopulated) {}

// A filename suggestion specified via a @download attribute should not be
// effective if the final download URL is in another origin from the original
// download URL.
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
                       DownloadAttributeCrossOriginRedirect) {}

// A filename suggestion specified via a @download attribute should not be
// effective if there are cross origin redirects in the middle of the redirect
// chain.
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
                       DownloadAttributeSameOriginRedirect) {}

// A file type that Blink can handle should not be downloaded if there are cross
// origin redirects in the middle of the redirect chain.
// TODO(crbug.com/40650833): Fix flakes on various bots and re-enable
// this test.
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
                       DISABLED_DownloadAttributeSameOriginRedirectNavigation) {}

// Tests that if a renderer initiated download triggers cross origin in the
// redirect chain, the visible URL of the current tab shouldn't change.
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
                       DownloadAttributeSameOriginRedirectNavigationTimeOut) {}

// A download initiated by the user via alt-click on a link should download,
// even when redirected cross origin.
//
// Alt-click doesn't make sense on Android, and download a HTML file results
// in an intent, so just skip.
#if !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
                       DownloadAttributeSameOriginRedirectAltClick) {}
#endif  // !BUILDFLAG(IS_ANDROID)

// Test that the suggested filename for data: URLs works.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadAttributeDataUrl) {}

// A request for a non-existent same-origin resource should result in a
// DownloadItem that's created in an interrupted state.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadAttributeServerError) {}

// A cross-origin request that fails before it gets a response from the server
// should result in a network error page.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadAttributeNetworkError) {}

// A request that fails due to it being rejected by policy should result in a
// corresponding navigation.
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();

// The file empty.bin is served with a MIME type of application/octet-stream.
// The content body is empty. Make sure this case is handled properly and we
// don't regress on http://crbug.com/320394.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadGZipWithNoContent) {}

// Make sure that sniffed MIME types are correctly passed through to the
// download item.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, SniffedMimeType) {}

// Verify that for download that is not triggered by navigation, MIME sniffing
// is working.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, SniffedMimeTypeForDownloadURL) {}

IN_PROC_BROWSER_TEST_F(DownloadContentTest, DuplicateContentDisposition) {}

// Test that the network isolation key is populated for:
// (1) <a download> triggered download request that doesn't go through the
// navigation path
// (2) the request resuming an interrupted download.
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
                       AnchorDownload_Resume_IsolationInfoPopulated) {}

IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadAttributeSameOriginIFrame) {}

IN_PROC_BROWSER_TEST_F(DownloadContentTest,
                       DownloadAttributeCrossOriginIFrame) {}

// Verify parallel download in normal case.
IN_PROC_BROWSER_TEST_F(ParallelDownloadTest, ParallelDownloadComplete) {}

// When the last request is rejected by the server, other parallel requests
// should take over and complete the download.
IN_PROC_BROWSER_TEST_F(ParallelDownloadTest, LastRequestRejected) {}

// When the second request is rejected by the server, other parallel requests
// should take over and complete the download.
IN_PROC_BROWSER_TEST_F(ParallelDownloadTest, SecondRequestRejected) {}

// The server will only accept the original request, and reject all other
// requests. The original request should complete the whole download.
IN_PROC_BROWSER_TEST_F(ParallelDownloadTest, OnlyFirstRequestValid) {}

// The server will send Accept-Ranges header without partial response.
IN_PROC_BROWSER_TEST_F(ParallelDownloadTest, NoPartialResponse) {}

// Verify parallel download resumption.
IN_PROC_BROWSER_TEST_F(ParallelDownloadTest, Resumption) {}

// Verifies that if the last slice is finished, parallel download resumption
// can complete.
IN_PROC_BROWSER_TEST_F(ParallelDownloadTest, ResumptionLastSliceFinished) {}

// Verify parallel download resumption when only 1 slice was created in previous
// attempt.
IN_PROC_BROWSER_TEST_F(ParallelDownloadTest, ResumptionWithOnlyOneSlice) {}

// Verifies that if the last slice is finished, but the database record is not
// finished, which may happen in database migration.
// When the server sends HTTP range not satisfied, the download can complete.
IN_PROC_BROWSER_TEST_F(ParallelDownloadTest, ResumptionLastSliceUnfinished) {}

// Verify that if server doesn't support partial response, resuming a parallel
// download should complete the download.
IN_PROC_BROWSER_TEST_F(ParallelDownloadTest, ResumptionNoPartialResponse) {}

// Verify that if a temporary error happens to one of the parallel request,
// resuming a parallel download should still complete.
// Flaky on fuchsia: https://crbug.com/1492656
#if BUILDFLAG(IS_FUCHSIA)
#define MAYBE_ResumptionMiddleSliceTemporaryError
#else
#define MAYBE_ResumptionMiddleSliceTemporaryError
#endif
IN_PROC_BROWSER_TEST_F(ParallelDownloadTest,
                       MAYBE_ResumptionMiddleSliceTemporaryError) {}

// Verify that if the second request fails after the beginning request takes
// over and completes its slice, download should complete.
IN_PROC_BROWSER_TEST_F(ParallelDownloadTest, MiddleSliceDelayedError) {}

// Test to verify that the browser-side enforcement of X-Frame-Options does
// not impact downloads. Since XFO is only checked for subframes, this test
// initiates a download in an iframe and expects it to succeed.
// See https://crbug.com/717971.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadIgnoresXFO) {}

// Verify that the response body of non-successful server response can be
// downloaded to a file, when |fetch_error_body| sets to true.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, FetchErrorResponseBody) {}

// Verify that the upload body of a request is received correctly by the server.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, UploadBytes) {}

// Verify the case that the first response is HTTP 200, and then interrupted,
// and the second response is HTTP 404, the response body of 404 should be
// fetched.
// Also verify the request header is correctly piped to download item.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, FetchErrorResponseBodyResumption) {}

// Verify WebUI download will success with an associated renderer process.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadFromWebUI) {}

// Verify WebUI download will gracefully fail without an associated renderer
// process.
IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadFromWebUIWithoutRenderer) {}

IN_PROC_BROWSER_TEST_F(DownloadContentTest, SaveImageAt) {}

// Test fixture for forcing MHTML download.
class MhtmlDownloadTest : public DownloadContentTest {};

// Test allow list for non http schemes which should not trigger
// download for mhtml.
IN_PROC_BROWSER_TEST_F(MhtmlDownloadTest,
                       AllowListForNonHTTPNotTriggerDownload) {}

#if defined(THREAD_SANITIZER)
// Flaky on TSAN https://crbug.com/932092
#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)
// Flaky https://crbug.com/852073
#define MAYBE_ForceDownloadMessageRfc822Page
#else
#define MAYBE_ForceDownloadMessageRfc822Page
#endif
IN_PROC_BROWSER_TEST_F(MhtmlDownloadTest,
                       MAYBE_ForceDownloadMessageRfc822Page) {}

// Test fixture for loading MHTML.
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) {}

// Regression test for https://crbug.com/1171765
IN_PROC_BROWSER_TEST_F(MhtmlLoadingTest, DisallowRenderMessageRfc822Iframe) {}

// Verify that downloads not triggered by navigation are discarded when
// initiated from a non-active page.
// Navigation downloads won't reach the DownloadManager. That is tested in
// PrerenderBrowserTest.{DownloadInMainFrame,DownloadInSubframe}.
IN_PROC_BROWSER_TEST_F(DownloadPrerenderTest, DiscardNonNavigationDownload) {}

// Verify that downloads not triggered by navigation are discarded when
// initiated from a fenced frame.
IN_PROC_BROWSER_TEST_F(DownloadFencedFrameTest, DiscardNonNavigationDownload) {}

// A download triggered by clicking on a link with a |download| attribute should
// have the user-gesture flag set.
IN_PROC_BROWSER_TEST_F(DownloadContentTest,
                       DownloadAttributePreservesUserGesture) {}

DownloadRangeTestParams;

// Browser test for arbitrary range download. This is for download system
// caller to explicitly ask for range request, not for parallel download and
// resumption that internally use range requests.
class DownloadRangeTest
    : public DownloadContentTest,
      public ::testing::WithParamInterface<DownloadRangeTestParams> {};

INSTANTIATE_TEST_SUITE_P();

// Test to download with range request with
// |DownloadUrlParameters::set_range_request_offset|.
IN_PROC_BROWSER_TEST_P(DownloadRangeTest, ArbitraryDownloadRangeTest) {}

class DownloadRangeResumptionTest : public DownloadContentTest {};

// Test to download resumption from a partially downloaded file with range
// request with |DownloadUrlParameters::set_range_request_offset|.
IN_PROC_BROWSER_TEST_F(DownloadRangeResumptionTest,
                       ArbitraryDownloadRangeResumptionTest) {}

}  // namespace content