// Copyright 2021 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/services/auction_worklet/trusted_signals_request_manager.h" #include <cstddef> #include <memory> #include <optional> #include <string> #include <utility> #include <vector> #include "base/functional/bind.h" #include "base/memory/scoped_refptr.h" #include "base/run_loop.h" #include "base/synchronization/waitable_event.h" #include "base/test/bind.h" #include "base/test/scoped_feature_list.h" #include "base/test/task_environment.h" #include "content/services/auction_worklet/auction_v8_helper.h" #include "content/services/auction_worklet/trusted_signals.h" #include "content/services/auction_worklet/worklet_test_util.h" #include "mojo/public/cpp/bindings/pending_remote.h" #include "net/http/http_status_code.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 "url/gurl.h" #include "v8/include/v8-context.h" #include "v8/include/v8-forward.h" namespace auction_worklet { namespace { // Very short time used by some tests that want to wait until just before a // timer triggers. constexpr base::TimeDelta kTinyTime = …; // Common JSON used for most bidding signals tests. const char kBaseBiddingJson[] = …; // Common JSON used for most scoring signals tests. const char kBaseScoringJson[] = …; // Expected result when fetch trusted scoring signals with `renderUrl` of // `"https://foo.test/"` and `adComponentRenderURLs` in `kBaseScoringJson[]`. const char kBaseScoringExpectedResult[] = …; const char kTopLevelOrigin[] = …; // Callback for loading signals that stores the result and runs a closure to // exit a message loop. void LoadSignalsCallback(scoped_refptr<TrustedSignals::Result>* results_out, std::optional<std::string>* error_msg_out, base::OnceClosure quit_closure, scoped_refptr<TrustedSignals::Result> result, std::optional<std::string> error_msg) { … } // Callback that should never be invoked, for cancellation tests. void NeverInvokedLoadSignalsCallback( scoped_refptr<TrustedSignals::Result> result, std::optional<std::string> error_msg) { … } class TrustedSignalsRequestManagerTest : public testing::Test { … }; TEST_F(TrustedSignalsRequestManagerTest, BiddingSignalsError) { … } TEST_F(TrustedSignalsRequestManagerTest, ScoringSignalsError) { … } TEST_F(TrustedSignalsRequestManagerTest, BiddingSignalsBatchedRequestError) { … } TEST_F(TrustedSignalsRequestManagerTest, ScoringSignalsBatchedRequestError) { … } TEST_F(TrustedSignalsRequestManagerTest, BiddingSignalsOneRequestNullKeys) { … } TEST_F(TrustedSignalsRequestManagerTest, BiddingSignalsOneRequest) { … } TEST_F(TrustedSignalsRequestManagerTest, ScoringSignalsOneRequest) { … } TEST_F(TrustedSignalsRequestManagerTest, BiddingSignalsSequentialRequests) { … } TEST_F(TrustedSignalsRequestManagerTest, ScoringSignalsSequentialRequests) { … } // Test the case where there are multiple network requests live at once. TEST_F(TrustedSignalsRequestManagerTest, BiddingSignalsSimultaneousNetworkRequests) { … } // Test the case where there are multiple network requests live at once. TEST_F(TrustedSignalsRequestManagerTest, ScoringSignalsSimultaneousNetworkRequests) { … } TEST_F(TrustedSignalsRequestManagerTest, BiddingSignalsBatchedRequests) { … } TEST_F(TrustedSignalsRequestManagerTest, ScoringSignalsBatchedRequests) { … } // Make two requests, cancel both, then try to start a network request. No // requests should be made. Only test bidders, since sellers have no significant // differences in this path. TEST_F(TrustedSignalsRequestManagerTest, CancelAllQueuedRequests) { … } // Make two requests, cancel the first one, then try to start a network request. // A request should be made, but only for the key in the request that was not // cancelled. Only test bidders, since sellers have no significant differences // in this path. TEST_F(TrustedSignalsRequestManagerTest, CancelOneRequest) { … } // Make two requests, try to start a network request, then cancel both requests. // The network request should be cancelled. Only test bidders, since sellers // have no significant differences in this path. TEST_F(TrustedSignalsRequestManagerTest, CancelAllLiveRequests) { … } // Make two requests, try to start a network request, then cancel the first one. // The request that was not cancelled should complete normally. Only test // bidders, since sellers have no significant differences in this path. TEST_F(TrustedSignalsRequestManagerTest, CancelOneLiveRequest) { … } // Test that when `automatically_send_requests` is false, requests are not // automatically started. TEST_F(TrustedSignalsRequestManagerTest, AutomaticallySendRequestsDisabled) { … } TEST_F(TrustedSignalsRequestManagerTest, AutomaticallySendRequestsEnabled) { … } TEST_F(TrustedSignalsRequestManagerTest, AutomaticallySendRequestsCancelAllRequestsRestartsTimer) { … } TEST_F(TrustedSignalsRequestManagerTest, AutomaticallySendRequestsCancelSomeRequestsDoesNotRestartTimer) { … } // Test bidding signals request carries experiment ID. TEST_F(TrustedSignalsRequestManagerTest, BiddingExperimentGroupIds) { … } // Test scoring signals request carries experiment ID. TEST_F(TrustedSignalsRequestManagerTest, ScoringExperimentGroupIds) { … } // Test a single bidding request with 0 (unlimited) length limit. // TODO(crbug.com/326082728): Remove this test because it will be duplicated // with `BiddingSignalsOneRequest` after the split feature is enabled by // default. TEST_F(TrustedSignalsRequestManagerTest, BiddingSignalsOneRequestWithZeroLimit) { … } // Test a single scoring request with 0 (unlimited) length limit. // TODO(xtlsheep): Remove this test because it will be duplicated with // `ScoringSignalsOneRequest` after the split feature is enabled by default. TEST_F(TrustedSignalsRequestManagerTest, ScoringSignalsOneRequestWithZeroLimit) { … } // Test a single bidding request with a tiny length limit that is smaller than // the URL generated by itself. TEST_F(TrustedSignalsRequestManagerTest, BiddingSignalsOneRequestWithTinyLimit) { … } // Test a single scoring request with a tiny length limit that is smaller than // the URL generated by itself. TEST_F(TrustedSignalsRequestManagerTest, ScoringSignalsOneRequestWithTinyLimit) { … } // Test a single bidding request with normal length limit that is larger than // the URL generated by itself. TEST_F(TrustedSignalsRequestManagerTest, BiddingSignalsOneRequestWithNormalLimit) { … } // Test a single scoring request with normal length limit that is larger than // the URL generated by itself. TEST_F(TrustedSignalsRequestManagerTest, ScoringSignalsOneRequestWithNormalLimit) { … } // Test two requests are issued in one request when their joint URL length is // smaller than either of their limits: // Request A has a limit of 0. // Request B has a limit of 1000. // The combined URL length of requests A and B is 131. TEST_F(TrustedSignalsRequestManagerTest, BiddingSignalsJointBatchedRequests) { … } // Test two requests are issued in one request when their joint URL length is // smaller than either of their limits: // Request A has a limit of 0. // Request B has a limit of 1000. // The combined URL length of requests A and B is 208. TEST_F(TrustedSignalsRequestManagerTest, ScoringSignalsJointBatchedRequests) { … } // Test two requests are issued separately in two requests when their joint URL // length is bigger than either of their limits: // Request A has a limit of 130. // Request B has a limit of 130. // The combined URL length of requests A and B is 131. TEST_F(TrustedSignalsRequestManagerTest, BiddingSignalsSplitBatchedRequests) { … } // Test two requests are issued in one request when their joint URL length is // smaller than either of their limits: // Request A has a limit of 200. // Request B has a limit of 200. // The combined URL length of requests A and B is 208. TEST_F(TrustedSignalsRequestManagerTest, ScoringSignalsSplitBatchedRequests) { … } // Test whether three requests are issued in two requests: request A and B can // be issued together because adding request C will result in an oversized URL. // Request A has a limit of 0. // Request B has a limit of 1000. // Request C has a limit of 130. // The combined URL length of requests A and B is 131. // The combined URL length of requests A, B and C is 137. TEST_F(TrustedSignalsRequestManagerTest, BiddingSignalsPartlyJointBatchedRequests1) { … } // Test whether three requests are issued in two requests: request A and B can // be issued together because adding request C will result in an oversized URL. // Request A has a limit of 0. // Request B has a limit of 1000. // Request C has a limit of 200. // The combined URL length of requests A and B is 208. // The combined URL length of requests A, B and C is 234. TEST_F(TrustedSignalsRequestManagerTest, ScoringSignalsPartlyJointBatchedRequests1) { … } // Test whether three signal requests are issued in two fetch requests: request // A is issued individually, because requests A and B will result an oversized // URL for B's limit. // Request A has a limit of 0. Request B has a limit of 131. // Request C has a limit of 131. // The combined URL length of requests A and B is 143. // The combined URL length of requests B and C is 131. TEST_F(TrustedSignalsRequestManagerTest, BiddingSignalsPartlyJointBatchedRequests2) { … } // Test whether three signal requests are issued in two fetch requests: request // A is issued individually, because requests A and B will result an oversized // URL for B's limit. // Request A has a limit of 0. Request B has a limit of 208. // Request C has a limit of 208. // The combined URL length of requests A and B is 221. // The combined URL length of requests B and C is 208. TEST_F(TrustedSignalsRequestManagerTest, ScoringSignalsPartlyJointBatchedRequests2) { … } // Test two identical signal requests with same interest group name and no // bidder keys will result two separate fetch request. // Request A has a limit of 104. // Request B has a limit of 104. TEST_F(TrustedSignalsRequestManagerTest, BiddingSignalsIdenticalRequests) { … } // Test two identical signal requests with same render url and no // ad component urls will result two separate fetch request. // Request A has a limit of 73. // Request B has a limit of 73. TEST_F(TrustedSignalsRequestManagerTest, ScoringSignalsIdenticalRequests) { … } } // namespace } // namespace auction_worklet