chromium/content/browser/attribution_reporting/attribution_manager_impl_unittest.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 "content/browser/attribution_reporting/attribution_manager_impl.h"

#include <stddef.h>

#include <initializer_list>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "base/check.h"
#include "base/containers/circular_deque.h"
#include "base/containers/flat_set.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/functional/function_ref.h"
#include "base/memory/raw_ptr.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/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/task/updateable_sequenced_task_runner.h"
#include "base/test/bind.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/metrics/histogram_tester.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/aggregatable_debug_reporting_config.h"
#include "components/attribution_reporting/aggregatable_dedup_key.h"
#include "components/attribution_reporting/aggregatable_trigger_data.h"
#include "components/attribution_reporting/aggregatable_values.h"
#include "components/attribution_reporting/debug_types.mojom.h"
#include "components/attribution_reporting/event_trigger_data.h"
#include "components/attribution_reporting/features.h"
#include "components/attribution_reporting/filters.h"
#include "components/attribution_reporting/os_registration.h"
#include "components/attribution_reporting/privacy_math.h"
#include "components/attribution_reporting/registrar.h"
#include "components/attribution_reporting/registration_header_error.h"
#include "components/attribution_reporting/source_registration_error.mojom.h"
#include "components/attribution_reporting/suitable_origin.h"
#include "content/browser/aggregation_service/aggregatable_report.h"
#include "content/browser/aggregation_service/aggregation_service.h"
#include "content/browser/aggregation_service/aggregation_service_test_utils.h"
#include "content/browser/attribution_reporting/aggregatable_attribution_utils.h"
#include "content/browser/attribution_reporting/aggregatable_debug_report.h"
#include "content/browser/attribution_reporting/attribution_cookie_checker.h"
#include "content/browser/attribution_reporting/attribution_debug_report.h"
#include "content/browser/attribution_reporting/attribution_features.h"
#include "content/browser/attribution_reporting/attribution_input_event.h"
#include "content/browser/attribution_reporting/attribution_observer.h"
#include "content/browser/attribution_reporting/attribution_os_level_manager.h"
#include "content/browser/attribution_reporting/attribution_report.h"
#include "content/browser/attribution_reporting/attribution_report_sender.h"
#include "content/browser/attribution_reporting/attribution_reporting.mojom.h"
#include "content/browser/attribution_reporting/attribution_resolver.h"
#include "content/browser/attribution_reporting/attribution_resolver_delegate.h"
#include "content/browser/attribution_reporting/attribution_test_utils.h"
#include "content/browser/attribution_reporting/attribution_trigger.h"
#include "content/browser/attribution_reporting/common_source_info.h"
#include "content/browser/attribution_reporting/create_report_result.h"
#include "content/browser/attribution_reporting/os_registration.h"
#include "content/browser/attribution_reporting/send_result.h"
#include "content/browser/attribution_reporting/storable_source.h"
#include "content/browser/attribution_reporting/stored_source.h"
#include "content/browser/attribution_reporting/test/configurable_storage_delegate.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/browsing_data/browsing_data_filter_builder_impl.h"
#include "content/browser/storage_partition_impl.h"
#include "content/public/browser/attribution_data_model.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browsing_data_filter_builder.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_utils.h"
#include "net/base/schemeful_site.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/mojom/network_change_manager.mojom.h"
#include "services/network/test/test_network_connection_tracker.h"
#include "storage/browser/test/mock_special_storage_policy.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/origin.h"

namespace content {

namespace {

AggregatableDebugReportingConfig;
AggregatableValues;
AggregatableValuesValue;
OsRegistrationItem;
SourceAggregatableDebugReportingConfig;
SuitableOrigin;
DebugDataType;
OsRegistrationResult;

SentResult;

_;
AllOf;
An;
AnyOf;
ElementsAre;
Expectation;
Field;
Ge;
InSequence;
IsEmpty;
IsNull;
Le;
Matcher;
Pointee;
Property;
Return;
SizeIs;
UnorderedElementsAre;

AttributionReportingOperation;

Checkpoint;

DebugReportSentCallback;
ReportSentCallback;
AggregatableDebugReportSentCallback;

constexpr size_t kMaxPendingEvents =;
constexpr size_t kMaxPendingReportsTimings =;

const GlobalRenderFrameHostId kFrameId =;
constexpr attribution_reporting::Registrar kRegistrar =;

constexpr AttributionResolverDelegate::OfflineReportDelayConfig
    kDefaultOfflineReportDelay{};

const base::TimeDelta kPrivacySandboxAttestationsTimeout =;

constexpr char kPendingAndBrowserWentOfflineTimeSinceCreation[] =;
constexpr char kPendingAndBrowserWentOfflineTimeUntilReportTime[] =;

constexpr char kSentVerboseDebugReportTypeMetric[] =;

auto InvokeReportSentCallback(SentResult result) {}

AggregatableReport CreateExampleAggregatableReport() {}

AggregatableReport CreateExampleAggregatableDebugReport() {}

// Time after impression that a conversion can first be sent. See
// AttributionResolverDelegateImpl::GetReportTimeForConversion().
constexpr base::TimeDelta kFirstReportingWindow =;

// Give impressions a sufficiently long expiry.
constexpr base::TimeDelta kImpressionExpiry =;

class MockReportSender : public AttributionReportSender {};

class MockCookieChecker : public AttributionCookieChecker {};

class MockAttributionOsLevelManager : public AttributionOsLevelManager {};

}  // namespace

class AttributionManagerImplTest : public testing::Test {};

TEST_F(AttributionManagerImplTest, ImpressionRegistered_ReturnedToWebUI) {}

TEST_F(AttributionManagerImplTest, ExpiredImpression_NotReturnedToWebUI) {}

TEST_F(AttributionManagerImplTest, ImpressionConverted_ReportReturnedToWebUI) {}

TEST_F(AttributionManagerImplTest, ImpressionConverted_ReportSent) {}

TEST_F(AttributionManagerImplTest,
       MultipleReportsWithSameReportTime_AllSentSimultaneously) {}

TEST_F(AttributionManagerImplTest, SenderStillHandlingReport_NotSentAgain) {}

TEST_F(AttributionManagerImplTest,
       QueuedReportFailedWithShouldRetry_QueuedAgain) {}

TEST_F(AttributionManagerImplTest, RetryLogicOverridesGetReportTimer) {}

TEST_F(AttributionManagerImplTest,
       QueuedReportFailedWithoutShouldRetry_NotQueuedAgain) {}

TEST_F(AttributionManagerImplTest, QueuedReportAlwaysFails_StopsSending) {}

TEST_F(AttributionManagerImplTest, ReportExpiredAtStartup_Sent) {}

TEST_F(AttributionManagerImplTest, ReportSent_Deleted) {}

TEST_F(AttributionManagerImplTest, QueuedReportSent_ObserversNotified) {}

TEST_F(AttributionManagerImplTest, TriggerHandled_ObserversNotified) {}

// This functionality is tested more thoroughly in the AttributionResolverImpl
// unit tests. Here, just test to make sure the basic control flow is working.
TEST_F(AttributionManagerImplTest, ClearDataFromBrowserOnly) {}

TEST_F(AttributionManagerImplTest, ClearDataFromBrowserAndOs) {}

TEST_F(AttributionManagerImplTest, ClearAllDataFromBrowserAndOs) {}

TEST_F(AttributionManagerImplTest, RemoveDataKeyFromBrowserAndOs) {}

TEST_F(AttributionManagerImplTest, HandleOsRegistration) {}

TEST_F(AttributionManagerImplTest, ConversionsSentFromUI_ReportedImmediately) {}

TEST_F(AttributionManagerImplTest, ExpiredReportsAtStartup_Delayed) {}

TEST_F(AttributionManagerImplTest,
       NonExpiredReportsQueuedAtStartup_NotDelayed) {}

TEST_F(AttributionManagerImplTest, SessionOnlyOrigins_DataDeletedAtShutdown) {}

TEST_F(AttributionManagerImplTest, HandleTrigger_RecordsMetric) {}

TEST_F(AttributionManagerImplTest,
       HandleTrigger_RecordsAggregatableFilteringIdMetrics) {}

TEST_F(AttributionManagerImplTest, HandleSource_RecordsMetric) {}

TEST_F(AttributionManagerImplTest, OnReportSent_NotifiesObservers) {}

TEST_F(AttributionManagerImplTest, HandleSource_NotifiesObservers) {}

TEST_F(AttributionManagerImplTest, HandleTrigger_NotifiesObservers) {}

TEST_F(AttributionManagerImplTest, ClearData_NotifiesObservers) {}

TEST_F(AttributionManagerImplTest,
       EmbedderDisallowsImpressions_SourceNotStored) {}

TEST_F(AttributionManagerImplTest,
       EmbedderDisallowsConversions_ReportNotStored) {}

TEST_F(AttributionManagerImplTest, EmbedderDisallowsReporting_ReportNotSent) {}

TEST_F(AttributionManagerImplTest,
       EmbedderDisallowsReporting_DebugReportNotSent) {}

TEST_F(AttributionManagerImplTest, Offline_NoReportSent) {}

class AttributionManagerImplOnlineConnectionTypeTest
    : public AttributionManagerImplTest {};

TEST_F(AttributionManagerImplOnlineConnectionTypeTest,
       OnlineConnectionTypeChanges_ReportTimesNotAdjusted) {}

TEST_F(AttributionManagerImplTest, TimeFromConversionToReportSendHistogram) {}

TEST_F(AttributionManagerImplTest, ReportRetriesTillSuccessHistogram) {}

TEST_F(AttributionManagerImplTest, ReportRetryDelayFeatureParams) {}

TEST_F(AttributionManagerImplTest, SendReport_RecordsExtraReportDelay2) {}

TEST_F(AttributionManagerImplTest, SendReport_RecordsSchedulerReportDelay) {}

TEST_F(AttributionManagerImplTest, SendReportsFromWebUI_DoesNotRecordMetrics) {}

class AttributionManagerImplFakeReportTest : public AttributionManagerImplTest {};

// Regression test for https://crbug.com/1294519.
TEST_F(AttributionManagerImplFakeReportTest,
       FakeReport_UpdatesSendReportTimer) {}

// Regression test for https://crbug.com/1506245.
TEST_F(AttributionManagerImplFakeReportTest, FakeReport_NotifiesObservers) {}

class AttributionManagerImplNoFakeReportTest
    : public AttributionManagerImplTest {};

TEST_F(AttributionManagerImplNoFakeReportTest, NotNotifyObservers) {}

// Test that multiple source and trigger registrations, with and without debug
// keys present, are handled in the order they are received by the manager.
TEST_F(AttributionManagerImplTest, RegistrationsHandledInOrder) {}

namespace {

const struct {} kDebugKeyTestCases[] =;

}  // namespace

TEST_F(AttributionManagerImplTest, HandleSource_DebugKey) {}

TEST_F(AttributionManagerImplTest, HandleTrigger_DebugKey) {}

TEST_F(AttributionManagerImplTest, DebugReport_SentImmediately) {}

TEST_F(AttributionManagerImplTest,
       HandleSource_NotifiesObservers_SourceHandled) {}

TEST_F(AttributionManagerImplTest,
       AggregateReportAssemblySucceeded_ReportSent) {}

TEST_F(AttributionManagerImplTest, OnReportSent_RecordReportDelay) {}

TEST_F(AttributionManagerImplTest,
       AggregateReportAssemblyFailed_RetriedAndReportNotSent) {}

TEST_F(AttributionManagerImplTest, AggregationServiceDisabled_ReportNotSent) {}

TEST_F(AttributionManagerImplTest, GetFailedReportDelay) {}

TEST_F(AttributionManagerImplTest, TooManyEventsInQueue) {}

TEST_F(AttributionManagerImplTest, TriggerVerboseDebugReport_ReportSent) {}

TEST_F(AttributionManagerImplTest,
       EmbedderDisallowsTriggerVerboseDebugReport_NoReportSent) {}

TEST_F(AttributionManagerImplTest, PendingReportsMetrics) {}

TEST_F(AttributionManagerImplTest,
       PendingReportsMetrics_WithoutPendingReports) {}

TEST_F(AttributionManagerImplTest, PendingReportsMetrics_Offline) {}

TEST_F(AttributionManagerImplTest, PendingReportsMetrics_OverLimits) {}

TEST_F(AttributionManagerImplTest,
       RemoveAttributionDataByDataKey_NotifiesObservers) {}

class AttributionManagerImplCookieBasedDebugReportTest
    : public AttributionManagerImplTest {};

TEST_F(AttributionManagerImplCookieBasedDebugReportTest,
       VerboseDebugReport_ReportSent) {}

TEST_F(AttributionManagerImplCookieBasedDebugReportTest,
       EmbedderDisallowsVerboseDebugReport_NoReportSent) {}

class AttributionManagerImplNullAggregatableReportTest
    : public AttributionManagerImplTest {};

TEST_F(AttributionManagerImplNullAggregatableReportTest, ReportSent) {}

TEST_F(AttributionManagerImplNullAggregatableReportTest,
       EmbedderDisallowsReporting_ReportNotSent) {}

TEST_F(AttributionManagerImplTest,
       OsRegistrationVerboseDebugReport_ReportSent) {}

TEST_F(AttributionManagerImplTest,
       EmbedderDisallowsOsRegistrationVerboseDebugReport_NoReportSent) {}

TEST_F(AttributionManagerImplTest,
       SourceRegistrationDelayedByAttestationsLoading) {}

TEST_F(AttributionManagerImplTest,
       SourceRegistrationDelayedByAttestationsLoadingAtTimeout) {}

TEST_F(AttributionManagerImplTest,
       ReportAtStartupDelayedByAttestationsLoading) {}

TEST_F(AttributionManagerImplTest,
       ReportAtStartupDelayedByAttestationsLoadingAtTimeout) {}

TEST_F(AttributionManagerImplTest, RegistrationHeaderErrorDebugReport) {}

// Regression test for http://crbug.com/331915077. This test will fail flakily
// if the manager's queue processing uses reentrant calls.
TEST_F(AttributionManagerImplTest, OsQueueNotReentrant) {}

TEST_F(AttributionManagerImplTest,
       AggregatableReportWithTriggerContextId_MetricsRecorded) {}

TEST_F(AttributionManagerImplTest, AggregatableDebugReport_ReportSent) {}

TEST_F(AttributionManagerImplTest,
       EmbedderDisallowsSourceAggregatableDebugReport_ReportNotSent) {}

TEST_F(AttributionManagerImplTest,
       EmbedderDisallowsTriggerAggregatableDebugReport_ReportNotSent) {}

TEST_F(AttributionManagerImplTest, SetDebugMode_NotifiesObservers) {}

}  // namespace content