chromium/content/browser/interest_group/interest_group_auction_reporter_unittest.cc

// Copyright 2023 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/interest_group/interest_group_auction_reporter.h"

#include <cstdint>
#include <limits>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>

#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_refptr.h"
#include "base/notreached.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "content/browser/fenced_frame/fenced_frame_reporter.h"
#include "content/browser/interest_group/ad_auction_page_data.h"
#include "content/browser/interest_group/auction_worklet_manager.h"
#include "content/browser/interest_group/header_direct_from_seller_signals.h"
#include "content/browser/interest_group/interest_group_k_anonymity_manager.h"
#include "content/browser/interest_group/interest_group_manager_impl.h"
#include "content/browser/interest_group/interest_group_pa_report_util.h"
#include "content/browser/interest_group/mock_auction_process_manager.h"
#include "content/browser/interest_group/subresource_url_builder.h"
#include "content/browser/interest_group/test_interest_group_manager_impl.h"
#include "content/browser/interest_group/test_interest_group_private_aggregation_manager.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/public/browser/page_user_data.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/test_utils.h"
#include "content/services/auction_worklet/public/mojom/private_aggregation_request.mojom.h"
#include "content/services/auction_worklet/public/mojom/real_time_reporting.mojom-forward.h"
#include "content/test/test_content_browser_client.h"
#include "mojo/public/cpp/system/functions.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/public/mojom/client_security_state.mojom.h"
#include "services/network/test/test_url_loader_factory.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/redacted_fenced_frame_config.h"
#include "third_party/blink/public/common/interest_group/auction_config.h"
#include "third_party/blink/public/common/interest_group/interest_group.h"
#include "third_party/blink/public/common/interest_group/test_interest_group_builder.h"
#include "third_party/blink/public/mojom/aggregation_service/aggregatable_report.mojom.h"
#include "third_party/blink/public/mojom/private_aggregation/private_aggregation_host.mojom.h"
#include "url/gurl.h"
#include "url/origin.h"

namespace content {
namespace {

InterestGroupAuctionReporter::SellerWinningBidInfo CreateSellerWinningBidInfo(
    blink::AuctionConfig* auction_config) {}

// Helper to avoid excess boilerplate.
template <typename... Ts>
auto ElementsAreRequests(Ts&... requests) {}

// Helper to avoid excess boilerplate.
template <typename... Ts>
auto ElementsAreContributions(Ts&... requests) {}

class EventReportingAttestationBrowserClient : public TestContentBrowserClient {};

// These tests cover the InterestGroupAuctionReporter state machine with respect
// to auction worklets and sending reports. All tests use mock auction worklets.
// Passing arguments correctly to reporting worklets is not covered by these
// tests, but rather by the AuctionRunner tests.
// TODO(crbug.com/334053709): Add test with reporting IDs.
class InterestGroupAuctionReporterTest
    : public RenderViewHostTestHarness,
      public AuctionWorkletManager::Delegate {};

TEST_F(InterestGroupAuctionReporterTest, SingleSellerNoReports) {}

TEST_F(InterestGroupAuctionReporterTest, ComponentAuctionNoReports) {}

TEST_F(InterestGroupAuctionReporterTest, SingleSellerReports) {}

// Test the case where we are in the middle of reporting and the frametree node
// is destroyed, we want to ensure that there is no crash.
TEST_F(InterestGroupAuctionReporterTest,
       SingleSellerReportsWhenFrameTreeNodeIsDestroyed) {}

TEST_F(InterestGroupAuctionReporterTest, ComponentAuctionReports) {}

// Test the case where all worklets return errors. Errors are non-fatal, so
// reports are still sent.
TEST_F(InterestGroupAuctionReporterTest, ComponentAuctionErrors) {}

// Test case case where navigation occurs only after all reporting scripts have
// run.
TEST_F(InterestGroupAuctionReporterTest, SingleSellerReportsLateNavigation) {}

// Test case case where navigation occurs only after all reporting scripts have
// run.
TEST_F(InterestGroupAuctionReporterTest,
       ComponentAuctionReportsLateNavigation) {}

// Test the case where the top-level seller script process crashes while
// reporting a component auction, making sure the bidder reporting script is
// still run, despite the crash.
TEST_F(InterestGroupAuctionReporterTest, SingleSellerSellerCrash) {}

// Test the case where the buyer worklet crashes while reporting a component
// auction. The reporter should complete, but with an error.
TEST_F(InterestGroupAuctionReporterTest, SingleSellerBuyerCrash) {}

// Test the case where the top-level seller script process crashes while
// reporting a component auction, making sure the component seller and bidder
// reporting scripts are still run, despite the crash.
TEST_F(InterestGroupAuctionReporterTest, ComponentAuctionTopLevelSellerCrash) {}

// Test the case where the component seller script process crashes while
// reporting a component auction, making sure the bidder reporting script is
// still run, despite the crash.
TEST_F(InterestGroupAuctionReporterTest, ComponentAuctionComponentSellerCrash) {}

// Test case where a bad report URL is received over Mojo from the seller
// worklet. Bad report URLs should be rejected in the Mojo process, so this
// results in reporting a bad Mojo message, though the reporting phase is
// allowed to continue.
TEST_F(InterestGroupAuctionReporterTest, SingleSellerBadSellerReportUrl) {}

// Test case where a bad report URL is received over Mojo from the component
// seller worklet. Bad report URLs should be rejected in the Mojo process, so
// this results in reporting a bad Mojo message, though the reporting phase is
// allowed to continue.
TEST_F(InterestGroupAuctionReporterTest, ComponentAuctionBadSellerReportUrl) {}

// Test case where a bad report URL is received over Mojo from the bidder
// worklet. Bad report URLs should be rejected in the Mojo process, so this
// results in reporting a bad Mojo message, though the reporting phase is
// allowed to continue.
TEST_F(InterestGroupAuctionReporterTest, SingleSellerBadBidderReportUrl) {}

TEST_F(InterestGroupAuctionReporterTest, SingleSellerReportBeaconMap) {}

TEST_F(InterestGroupAuctionReporterTest, ComponentAuctionReportBeaconMap) {}

// Test case where a bad report URL is received over Mojo from the seller
// worklet. Bad report URLs should be rejected in the Mojo process, so this
// results in reporting a bad Mojo message, though the reporting phase is
// allowed to continue.
TEST_F(InterestGroupAuctionReporterTest,
       ComponentAuctionReportBeaconMapBadSellerUrl) {}

// Test case where a bad report URL is received over Mojo from the bidder
// worklet. Bad report URLs should be rejected in the Mojo process, so this
// results in reporting a bad Mojo message, though the reporting phase is
// allowed to continue.
TEST_F(InterestGroupAuctionReporterTest,
       ComponentAuctionReportBeaconMapBadBidderUrl) {}

TEST_F(InterestGroupAuctionReporterTest, DebugReportsEarlyNavigation) {}

TEST_F(InterestGroupAuctionReporterTest, DebugReportsLateNavigation) {}

// Check that the winning interest group and bids are reported to the
// InterestGroupManager, in the case where the fenced frame is navigated to
// before any reporting scripts have run.
TEST_F(InterestGroupAuctionReporterTest, RecordWinAndBids) {}

// Check that the winning interest group and bids are reported to the
// InterestGroupManager, in the case where the fenced frame is navigated to only
// after all reporting scripts have been run.
TEST_F(InterestGroupAuctionReporterTest, RecordWinAndBidsLateNavigation) {}

// Check that the passed in `k_anon_keys_to_join` are reported to the
// InterestGroupManager, in the case where the fenced frame is navigated to
// before any reporting scripts have run.
TEST_F(InterestGroupAuctionReporterTest, RecordKAnonKeysToJoin) {}

// Check that the passed in `k_anon_keys_to_join` are reported to the
// InterestGroupManager, in the case where the fenced frame is navigated to only
// after all reporting scripts have been run.
TEST_F(InterestGroupAuctionReporterTest, RecordKAnonKeysToJoinLateNavigation) {}

// Check that private aggregation requests are passed along as expected. This
// creates an auction which is both passed aggregation reports from the bidding
// and scoring phase of the auction, and receives more from each reporting
// worklet that's invoked. This covers the case where a navigation occurs before
// the seller's reporting script completes.
TEST_F(InterestGroupAuctionReporterTest, PrivateAggregationRequests) {}

// Check that private aggregation requests are passed along as expected. This
// creates an auction which is both passed aggregation reports from the bidding
// and scoring phase of the auction, and receives more from each reporting
// worklet that's invoked. This covers the case where a navigation occurs after
// all reporting scripts have completed.
TEST_F(InterestGroupAuctionReporterTest,
       PrivateAggregationRequestsLateNavigation) {}

// Check that private aggregation requests of non-reserved event types are
// passed along as expected. This creates an auction which is both passed
// aggregation reports from the bidding and scoring phase of the auction, and
// receives more from each reporting worklet that's invoked. This covers the
// case where a navigation occurs before the seller's reporting script
// completes.
TEST_F(InterestGroupAuctionReporterTest,
       PrivateAggregationRequestsNonReserved) {}

// Check that private aggregation requests of non-reserved event types are
// passed along as expected. This creates an auction which is both passed
// aggregation reports from the bidding and scoring phase of the auction, and
// receives more from each reporting worklet that's invoked. This covers the
// case where a navigation occurs after all reporting scripts have completed.
TEST_F(InterestGroupAuctionReporterTest,
       PrivateAggregationRequestsNonReservedLateNavigation) {}

// Check that real time reporting contributions are passed along as expected.
// This covers the case where a navigation occurs before the seller's reporting
// script completes.
TEST_F(InterestGroupAuctionReporterTest, RealTimeReporting) {}

// Check that real time reporting histograms are passed along as expected. This
// covers the case where a navigation occurs after all reporting scripts have
// completed.
TEST_F(InterestGroupAuctionReporterTest, RealTimeReportingLateNavigation) {}

// Check that private aggregation requests are passed along to trigger use
// counter logging as appropriate.
TEST_F(InterestGroupAuctionReporterTest,
       PrivateAggregationLoggingForUseCounter) {}

// Check that no private aggregation requests are passed along to trigger use
// counter logging if the API was not used.
TEST_F(InterestGroupAuctionReporterTest,
       PrivateAggregationLoggingForUseCounterNotUsed) {}

// Test the case that the InterestGroupAutionReporter is destroyed while calling
// the top-level seller's reportResult() method, before navigation. This
// primarily serves to test UMA.
TEST_F(InterestGroupAuctionReporterTest,
       DestroyedDuringSellerReportResultBeforeNavigation) {}

// Test the case that the InterestGroupAutionReporter is destroyed while calling
// the top-level seller's reportResult() method, after navigation. This
// primarily serves to test UMA.
TEST_F(InterestGroupAuctionReporterTest, DestroyedDuringSellerReportResult) {}

// Test the case that the InterestGroupAutionReporter is destroyed while calling
// the component seller's reportResult() method, after navigation. This
// primarily serves to test UMA.
TEST_F(InterestGroupAuctionReporterTest,
       DestroyedDuringComponentSellerReportResult) {}

// Test the case that the InterestGroupAutionReporter is destroyed while calling
// the buyer's reportWin() method, after navigation. This primarily serves to
// test UMA.
TEST_F(InterestGroupAuctionReporterTest, DestroyedDuringReportWin) {}

// Test that nothing is recorded and no reports are sent in the case that the
// reporting scripts are successfully run, but the frame is never navigated to.
TEST_F(InterestGroupAuctionReporterTest, NoNavigation) {}

// Test multiple navigations result in only a single set of reports, and
// metadata being recorded exactly once once by the InterestGroupManager.
TEST_F(InterestGroupAuctionReporterTest, MultipleNavigations) {}

// Disable feature kPrivateAggregationApi.
class InterestGroupAuctionReporterPrivateAggregationDisabledTest
    : public InterestGroupAuctionReporterTest {};

TEST_F(InterestGroupAuctionReporterPrivateAggregationDisabledTest,
       PrivateAggregationRequestsNonReserved) {}

// Disable FLEDGE-specific extensions of Private Aggregation API.
class InterestGroupAuctionReporterPrivateAggregationFledgeExtensionDisabledTest
    : public InterestGroupAuctionReporterTest {};

TEST_F(
    InterestGroupAuctionReporterPrivateAggregationFledgeExtensionDisabledTest,
    PrivateAggregationRequestsNonReserved) {}

TEST(InterestGroupAuctionReporterStochasticRounding, MatchesTable) {}

TEST(InterestGroupAuctionReporterStochasticRounding, PassesNaN) {}

TEST(InterestGroupAuctionReporterStochasticRounding, IsNonDeterministic) {}

TEST(InterestGroupAuctionReporterStochasticRounding, RoundsUpAndDown) {}

TEST(InterestGroupAuctionReporterStochasticRounding, HandlesOverflow) {}

// Test that random rounding allows mean to approximate the true value.
TEST(InterestGroupAuctionReporterStochasticRounding, ApproximatesTrueSum) {}

class InterestGroupAuctionReporterAdMacroReportingEnabledTest
    : public InterestGroupAuctionReporterTest {};

TEST_F(InterestGroupAuctionReporterAdMacroReportingEnabledTest,
       SingleSellerReportMacros) {}

TEST_F(InterestGroupAuctionReporterAdMacroReportingEnabledTest,
       ComponentAuctionReportMacros) {}

}  // namespace
}  // namespace content