chromium/content/browser/attribution_reporting/attribution_test_utils.h

// 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.

#ifndef CONTENT_BROWSER_ATTRIBUTION_REPORTING_ATTRIBUTION_TEST_UTILS_H_
#define CONTENT_BROWSER_ATTRIBUTION_REPORTING_ATTRIBUTION_TEST_UTILS_H_

#include <stdint.h>

#include <iosfwd>
#include <optional>
#include <string>
#include <vector>

#include "base/containers/flat_set.h"
#include "base/time/time.h"
#include "base/uuid.h"
#include "components/attribution_reporting/aggregatable_debug_reporting_config.h"
#include "components/attribution_reporting/aggregatable_filtering_id_max_bytes.h"
#include "components/attribution_reporting/constants.h"
#include "components/attribution_reporting/filters.h"
#include "components/attribution_reporting/source_registration.h"
#include "components/attribution_reporting/source_registration_time_config.mojom.h"
#include "components/attribution_reporting/source_type.mojom.h"
#include "components/attribution_reporting/suitable_origin.h"
#include "components/attribution_reporting/test_utils.h"
#include "components/attribution_reporting/trigger_data_matching.mojom-forward.h"
#include "content/browser/attribution_reporting/attribution_info.h"
#include "content/browser/attribution_reporting/attribution_report.h"
#include "content/browser/attribution_reporting/create_report_result.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/public/browser/attribution_data_model.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/blink/public/mojom/aggregation_service/aggregatable_report.mojom-forward.h"

namespace attribution_reporting {
class AggregatableValues;
class AggregationKeys;
class AttributionScopesData;
class TriggerSpecs;
}  // namespace attribution_reporting

namespace net {
class SchemefulSite;
}  // namespace net

namespace content {

class AttributionTrigger;
class CommonSourceInfo;

enum class RateLimitResult : int;

base::Uuid DefaultExternalReportID();

// Helper class to construct a StorableSource for tests using default data.
// StorableSource members are not mutable after construction requiring a
// builder pattern.
class SourceBuilder {};

// Returns a AttributionTrigger with default data which matches the default
// impressions created by SourceBuilder.
AttributionTrigger DefaultTrigger();

// Helper class to construct a AttributionTrigger for tests using default data.
// AttributionTrigger members are not mutable after construction requiring a
// builder pattern.
class TriggerBuilder {};

// Helper class to construct an `AttributionInfo` for tests using default data.
class AttributionInfoBuilder {};

// Helper class to construct an `AttributionReport` for tests using default
// data.
class ReportBuilder {};

bool operator==(const StoredSource&, const StoredSource&);

bool operator==(const AttributionReport::CommonAggregatableData&,
                const AttributionReport::CommonAggregatableData&);

bool operator==(const AttributionReport::AggregatableAttributionData&,
                const AttributionReport::AggregatableAttributionData&);

bool operator==(const AttributionReport::NullAggregatableData&,
                const AttributionReport::NullAggregatableData&);

bool operator==(const AttributionReport&, const AttributionReport&);

std::ostream& operator<<(std::ostream& out, RateLimitResult result);

std::ostream& operator<<(std::ostream& out,
                         const AttributionTrigger& conversion);

std::ostream& operator<<(std::ostream& out, const CommonSourceInfo& source);

std::ostream& operator<<(std::ostream& out,
                         const AttributionInfo& attribution_info);

std::ostream& operator<<(std::ostream& out, const StorableSource& source);

std::ostream& operator<<(std::ostream& out, const StoredSource& source);

std::ostream& operator<<(
    std::ostream& out,
    const blink::mojom::AggregatableReportHistogramContribution& contribution);

std::ostream& operator<<(std::ostream& out,
                         const AttributionReport::EventLevelData& data);

std::ostream& operator<<(std::ostream& out,
                         const AttributionReport::CommonAggregatableData&);

std::ostream& operator<<(
    std::ostream& out,
    const AttributionReport::AggregatableAttributionData& data);

std::ostream& operator<<(std::ostream& out,
                         const AttributionReport::NullAggregatableData&);

std::ostream& operator<<(std::ostream& out, const AttributionReport& report);

std::ostream& operator<<(std::ostream& out, SendResult::Status status);

std::ostream& operator<<(std::ostream& out, const SendResult& info);

std::ostream& operator<<(std::ostream& out,
                         StoredSource::AttributionLogic attribution_logic);

std::ostream& operator<<(std::ostream& out,
                         StoredSource::ActiveState active_state);

// TODO: Move to test-only public header to be reused by other test code
// that rely on DataKey
std::ostream& operator<<(std::ostream& out,
                         const AttributionDataModel::DataKey& key);

// Source matchers

MATCHER_P(SourceRegistrationIs, matcher, "") {}

MATCHER_P(RegistrationAttributionScopesDataIs, matcher, "") {}

MATCHER_P(RegistrationSourceEventIdIs, matcher, "") {}

MATCHER_P(SourceEventIdIs, matcher, "") {}

MATCHER_P(ImpressionOriginIs, matcher, "") {}

MATCHER_P(ReportingOriginIs, matcher, "") {}

MATCHER_P(SourceTypeIs, matcher, "") {}

MATCHER_P(SourceDebugKeyIs, matcher, "") {}

MATCHER_P(SourceDebugCookieSetIs, matcher, "") {}

MATCHER_P(SourceFilterDataIs, matcher, "") {}

MATCHER_P(DedupKeysAre, matcher, "") {}

MATCHER_P(AggregatableDedupKeysAre, matcher, "") {}

MATCHER_P(AggregationKeysAre, matcher, "") {}

MATCHER_P(RemainingAggregatableAttributionBudgetIs, matcher, "") {}

MATCHER_P(AttributionScopesDataIs, matcher, "") {}

MATCHER_P(AttributionScopesSetIs, matcher, "") {}

MATCHER_P(RandomizedResponseRateIs, matcher, "") {}

MATCHER_P(SourceActiveStateIs, matcher, "") {}

// Trigger matchers.

MATCHER_P(TriggerDestinationOriginIs, matcher, "") {}

// Report matchers

MATCHER_P(ReportTimeIs, matcher, "") {}

MATCHER_P(InitialReportTimeIs, matcher, "") {}

MATCHER_P(FailedSendAttemptsIs, matcher, "") {}

MATCHER_P(TriggerDebugKeyIs, matcher, "") {}

MATCHER_P(ReportSourceDebugKeyIs, matcher, "") {}

MATCHER_P(EventLevelDataIs, matcher, "") {}

MATCHER_P(TriggerDataIs, matcher, "") {}

MATCHER_P(TriggerPriorityIs, matcher, "") {}

MATCHER_P(ReportURLIs, matcher, "") {}

MATCHER_P(ReportOriginIs, matcher, "") {}

MATCHER_P(ReportTypeIs, matcher, "") {}

MATCHER_P(AggregatableAttributionDataIs, matcher, "") {}

MATCHER_P(NullAggregatableDataIs, matcher, "") {}

MATCHER_P(AggregatableHistogramContributionsAre, matcher, "") {}

MATCHER_P(AggregationCoordinatorOriginIs, matcher, "") {}

MATCHER_P(SourceRegistrationTimeConfigIs, matcher, "") {}

MATCHER_P(TriggerContextIdIs, matcher, "") {}

// `CreateReportResult` matchers

MATCHER_P(CreateReportEventLevelStatusIs, matcher, "") {}

MATCHER_P(CreateReportAggregatableStatusIs, matcher, "") {}

MATCHER_P(ReplacedEventLevelReportIs, matcher, "") {}

MATCHER_P(DeactivatedSourceIs, matcher, "") {}

MATCHER_P(NewEventLevelReportIs, matcher, "") {}

MATCHER_P(NewAggregatableReportIs, matcher, "") {}

MATCHER_P(DroppedEventLevelReportIs, matcher, "") {}

class TestAggregatableSourceProvider {};

TriggerBuilder DefaultAggregatableTriggerBuilder(
    const std::vector<uint32_t>& histogram_values = {};

std::vector<blink::mojom::AggregatableReportHistogramContribution>
DefaultAggregatableHistogramContributions(
    const std::vector<int32_t>& histogram_values = {};

struct OsRegistration;

bool operator==(const OsRegistration&, const OsRegistration&);

std::ostream& operator<<(std::ostream&, const OsRegistration&);

void ExpectValidAttributionReportingEligibleHeaderForEventBeacon(
    const std::string& header);
void ExpectValidAttributionReportingEligibleHeaderForImg(
    const std::string& header);
void ExpectValidAttributionReportingEligibleHeaderForNavigation(
    const std::string& header);
void ExpectEmptyAttributionReportingEligibleHeader(const std::string& header);

void ExpectValidAttributionReportingSupportHeader(const std::string& header,
                                                  bool web_expected,
                                                  bool os_expected);

}  // namespace content

#endif  // CONTENT_BROWSER_ATTRIBUTION_REPORTING_ATTRIBUTION_TEST_UTILS_H_