chromium/content/browser/attribution_reporting/attributions_browsertest.cc

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

#include <memory>
#include <optional>
#include <string>
#include <utility>

#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/location.h"
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/strings/strcat.h"
#include "base/task/sequenced_task_runner.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/values_test_util.h"
#include "base/time/time.h"
#include "base/values.h"
#include "components/attribution_reporting/constants.h"
#include "content/browser/attribution_reporting/attribution_manager.h"
#include "content/browser/attribution_reporting/attribution_manager_impl.h"
#include "content/browser/attribution_reporting/attribution_os_level_manager.h"
#include "content/browser/attribution_reporting/attribution_test_utils.h"
#include "content/browser/attribution_reporting/storable_source.h"
#include "content/browser/attribution_reporting/test/mock_attribution_observer.h"
#include "content/browser/attribution_reporting/test/mock_content_browser_client.h"
#include "content/browser/fenced_frame/fenced_frame_reporter.h"
#include "content/browser/fenced_frame/fenced_frame_url_mapping.h"
#include "content/browser/private_aggregation/private_aggregation_manager.h"
#include "content/browser/renderer_host/frame_tree_node.h"
#include "content/browser/renderer_host/page_impl.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/browser/service_worker/service_worker_context_core.h"
#include "content/browser/service_worker/service_worker_context_core_observer.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/browser/storage_partition_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.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/prerender_test_util.h"
#include "content/public/test/test_frame_navigation_observer.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_utils.h"
#include "content/public/test/url_loader_interceptor.h"
#include "content/shell/browser/shell.h"
#include "content/test/content_browser_test_utils_internal.h"
#include "content/test/fenced_frame_test_utils.h"
#include "content/test/test_content_browser_client.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/controllable_http_response.h"
#include "net/test/embedded_test_server/default_handlers.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/test/embedded_test_server/request_handler_util.h"
#include "services/network/public/cpp/features.h"
#include "services/network/test/test_network_connection_tracker.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/fenced_frame/fenced_frame_utils.h"
#include "third_party/blink/public/common/fenced_frame/redacted_fenced_frame_config.h"
#include "third_party/blink/public/common/service_worker/service_worker_status_code.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration_options.mojom.h"
#include "url/gurl.h"
#include "url/origin.h"

namespace content {

namespace {

_;
Return;

constexpr char kBaseDataDir[] =;

kAttributionReportingRegisterSourceHeader;

void ExpectRegisterResultAndRun(blink::ServiceWorkerStatusCode expected,
                                base::RepeatingClosure continuation,
                                blink::ServiceWorkerStatusCode actual) {}

// Observer which waits for a service worker to register in the browser process
// by observing worker activation status.
class WorkerStateObserver : public ServiceWorkerContextCoreObserver {};

// Waits for the a given |report_url| to be received by the test server. Wraps a
// ControllableHttpResponse so that it can wait for the server request in a
// thread-safe manner. Therefore, these must be registered prior to |server|
// starting.
struct ExpectedReportWaiter {};

}  // namespace

class InterestGroupEnabledContentBrowserClient
    : public ContentBrowserTestContentBrowserClient {};

class AttributionsBrowserTestBase : public ContentBrowserTest {};

class AttributionsBrowserTest : public AttributionsBrowserTestBase,
                                public ::testing::WithParamInterface<bool> {};

INSTANTIATE_TEST_SUITE_P();

// Verifies that storage initialization does not hang when initialized in a
// browsertest context, see https://crbug.com/1080764).
IN_PROC_BROWSER_TEST_P(AttributionsBrowserTest,
                       FeatureEnabled_StorageInitWithoutHang) {}

IN_PROC_BROWSER_TEST_P(AttributionsBrowserTest,
                       ImpressionNavigationMultipleRedirects_FirstReportSent) {}

IN_PROC_BROWSER_TEST_P(AttributionsBrowserTest,
                       ImpressionsRegisteredOnNavigation_ReportSent) {}

class AttributionsCrossAppWebDisabledBrowserTest
    : public AttributionsBrowserTest {};

INSTANTIATE_TEST_SUITE_P();

IN_PROC_BROWSER_TEST_P(AttributionsCrossAppWebDisabledBrowserTest,
                       AttributionEligibleNavigation_SetsEligibleHeader) {}

IN_PROC_BROWSER_TEST_P(AttributionsBrowserTest,
                       NonAttributionEligibleNavigation_NoEligibleHeader) {}

IN_PROC_BROWSER_TEST_P(AttributionsBrowserTest,
                       ImpressionFromCrossOriginSubframe_ReportSent) {}

// Regression test for crbug.com/1366513.
// TODO(b/331159758): Disabled due to flakiness.
IN_PROC_BROWSER_TEST_P(AttributionsBrowserTest,
                       DISABLED_AttributionSrcInSandboxedIframe_NoCrash) {}

IN_PROC_BROWSER_TEST_P(AttributionsBrowserTest,
                       ImpressionOnNoOpenerNavigation_ReportSent) {}

IN_PROC_BROWSER_TEST_P(
    AttributionsBrowserTest,
    ConversionOnDifferentSubdomainThanLandingPage_ReportSent) {}

IN_PROC_BROWSER_TEST_P(
    AttributionsBrowserTest,
    ServiceWorkerPerformsAttributionSrcRedirect_ReporterSet) {}

IN_PROC_BROWSER_TEST_P(
    AttributionsBrowserTest,
    ServiceWorkerPerformsAttributionEligibleRedirect_ReporterSet) {}

IN_PROC_BROWSER_TEST_P(AttributionsBrowserTest,
                       NavigationNoneSupported_EligibleHeaderNotSet) {}

class AttributionsPrerenderBrowserTest : public AttributionsBrowserTest {};

INSTANTIATE_TEST_SUITE_P();

// TODO(crbug.com/40231714): these tests are flaky on most release bots.
#if defined(NDEBUG)
#define ATTRIBUTION_PRERENDER_BROWSER_TEST
#else
#define ATTRIBUTION_PRERENDER_BROWSER_TEST(TEST_NAME)
#endif

ATTRIBUTION_PRERENDER_BROWSER_TEST(NoConversionsOnPrerender) {}

ATTRIBUTION_PRERENDER_BROWSER_TEST(ConversionsRegisteredOnActivatedPrerender) {}

class AttributionsCrossAppWebEnabledBrowserTest
    : public AttributionsBrowserTest {};

INSTANTIATE_TEST_SUITE_P();

IN_PROC_BROWSER_TEST_P(AttributionsCrossAppWebEnabledBrowserTest,
                       AttributionEligibleNavigation_SetsSupportHeader) {}

IN_PROC_BROWSER_TEST_P(
    AttributionsCrossAppWebEnabledBrowserTest,
    AttributionEligibleNavigationOsLevelEnabled_SetsSupportHeader) {}

class AttributionsFencedFrameBrowserTest : public AttributionsBrowserTest {};

INSTANTIATE_TEST_SUITE_P();

IN_PROC_BROWSER_TEST_P(AttributionsFencedFrameBrowserTest,
                       ReportEvent_ReportSent) {}

IN_PROC_BROWSER_TEST_P(AttributionsFencedFrameBrowserTest,
                       ReportEventRedirect_BothReportsSent) {}

IN_PROC_BROWSER_TEST_P(AttributionsFencedFrameBrowserTest,
                       AutomaticBeacon_ReportSent) {}

class AttributionsBrowserTestWithKeepAliveMigration
    : public AttributionsBrowserTestBase {};

// Regression test for crbug.com/1374121.
IN_PROC_BROWSER_TEST_F(AttributionsBrowserTestWithKeepAliveMigration,
                       SourceRegisteredAfterNavigation) {}

void TestServiceWorker(const char* registration_js,
                       WebContents* web_contents,
                       ServiceWorkerContextWrapper* sw_wrapper,
                       net::EmbeddedTestServer* https_server) {}

IN_PROC_BROWSER_TEST_P(
    AttributionsBrowserTest,
    ServiceWorkerPerformsAttributionSrcRegistration_SupportHeaderSet_createAttributionEligibleImgSrc) {}
IN_PROC_BROWSER_TEST_P(
    AttributionsBrowserTest,
    ServiceWorkerPerformsAttributionSrcRegistration_SupportHeaderSet_createAttributionSrcScript) {}
IN_PROC_BROWSER_TEST_P(
    AttributionsBrowserTest,
    ServiceWorkerPerformsAttributionSrcRegistration_SupportHeaderSet_doAttributionEligibleFetch) {}

IN_PROC_BROWSER_TEST_P(
    AttributionsBrowserTest,
    ServiceWorkerPerformsAttributionSrcRegistration_SupportHeaderSet_doAttributionEligibleXHR) {}

IN_PROC_BROWSER_TEST_P(
    AttributionsBrowserTest,
    ServiceWorkerPerformsAttributionSrcRegistration_SupportHeaderSet_createAttributionEligibleScriptSrc) {}

}  // namespace content