chromium/content/services/auction_worklet/trusted_signals_kvv2_helper_unittest.cc

// Copyright 2024 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_kvv2_helper.h"

#if BUILDFLAG(IS_WIN)
#include <winsock2.h>
#else
#include <netinet/in.h>
#endif

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <optional>
#include <string>
#include <vector>

#include "base/containers/span.h"
#include "base/containers/span_writer.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "components/cbor/values.h"
#include "components/cbor/writer.h"
#include "content/services/auction_worklet/public/mojom/auction_worklet_service.mojom.h"
#include "content/services/auction_worklet/trusted_signals.h"
#include "content/services/auction_worklet/trusted_signals_request_manager.h"
#include "net/third_party/quiche/src/quiche/oblivious_http/oblivious_http_gateway.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/zlib/google/compression_utils.h"
#include "url/gurl.h"
#include "url/origin.h"
#include "v8-context.h"

namespace auction_worklet {

namespace {

const char kHostName[] =;
const int kExperimentGroupId =;
const char kTrustedBiddingSignalsSlotSizeParam[] =;
const size_t kFramingHeaderSize =;  // bytes
const size_t kOhttpHeaderSize =;   // bytes
const char kTrustedSignalsUrl[] =;
const char kOriginFooUrl[] =;
const char kOriginFoosubUrl[] =;
const char kOriginBarUrl[] =;
const char kOriginBarsubUrl[] =;
const char kOwnerOriginA[] =;
const char kOwnerOriginB[] =;
const char kJoiningOriginA[] =;
const char kJoiningOriginB[] =;

// These keys were randomly generated as follows:
// EVP_HPKE_KEY keys;
// EVP_HPKE_KEY_generate(&keys, EVP_hpke_x25519_hkdf_sha256());
// and then EVP_HPKE_KEY_public_key and EVP_HPKE_KEY_private_key were used to
// extract the keys.
const uint8_t kTestPrivateKey[] =;

const uint8_t kTestPublicKey[] =;

// Helper to decrypt request body.
std::vector<uint8_t> DecryptRequestBody(const std::string& request_body,
                                        int public_key_id) {}

// GzipCompress() doesn't support writing to a vector, only a std::string. This
// wrapper provides that capability, at the cost of an extra copy.
std::vector<std::uint8_t> GzipCompressHelper(
    const std::vector<std::uint8_t>& in) {}

void ExpectCompressionGroupMapEquals(
    const TrustedSignalsKVv2ResponseParser::CompressionGroupResultMap& map1,
    const TrustedSignalsKVv2ResponseParser::CompressionGroupResultMap& map2) {}

// Check trusted bidding signals' priority vector and bidding signals in json
// format with given interest group names and bidding keys.
void CheckBiddingResult(
    AuctionV8Helper* v8_helper,
    TrustedSignalsKVv2ResponseParser::TrustedSignalsResultMap& result_map,
    TrustedSignalsKVv2RequestHelperBuilder::IsolationIndex& index,
    const std::vector<std::string>& interest_group_names,
    const std::vector<std::string>& keys,
    const std::map<std::string, TrustedSignals::Result::PriorityVector>&
        priority_vector_map,
    const std::string& bidding_signals,
    std::optional<uint32_t> data_version) {}

// Check trusted scoring signals' render urls and ad component signals in json
// format with given render url and ad component render urls.
void CheckScoringResult(
    AuctionV8Helper* v8_helper,
    TrustedSignalsKVv2ResponseParser::TrustedSignalsResultMap& result_map,
    TrustedSignalsKVv2RequestHelperBuilder::IsolationIndex& index,
    const GURL& render_url,
    const std::vector<std::string>& ad_component_render_urls,
    const std::string& expected_signals,
    std::optional<uint32_t> data_version) {}

// Build a response body in string format with a hex string, a given compression
// scheme format byte, and the length of the hex string after it is converted to
// bytes.
std::string BuildResponseBody(const std::string& hex_string,
                              uint8_t compress_scheme = 0x00) {}

// Encrypt the response body string by creating a fake encrypted request using a
// public key and saving the encryption context. Return a pair consisting of the
// encrypted response body string and the encryption context. The context will
// be passed to `ParseResponseToSignalsFetchResult` and used in
// `CreateClientObliviousResponse()` for response decryption.
std::pair<std::string, quiche::ObliviousHttpRequest::Context>
EncryptResponseBodyHelper(const std::string& response_body) {}

std::string GetErrorMessageFromParseResponseToSignalsFetchResult(
    std::string& hex,
    uint8_t compress_scheme = 0x00) {}

std::string GetErrorMessageFromParseBiddingSignalsFetchResultToResultMap(
    scoped_refptr<AuctionV8Helper> v8_helper,
    const std::set<std::string>& interest_group_names,
    const std::set<std::string>& keys,
    const TrustedSignalsKVv2ResponseParser::CompressionGroupResultMap&
        compression_group_result_map) {}

std::string GetErrorMessageFromParseScoringSignalsFetchResultToResultMap(
    scoped_refptr<AuctionV8Helper> v8_helper,
    const std::set<std::string>& render_urls,
    const std::set<std::string>& ad_component_render_urls,
    const TrustedSignalsKVv2ResponseParser::CompressionGroupResultMap&
        compression_group_result_map) {}

}  // namespace

class TrustedSignalsKVv2ResponseParserTest : public testing::Test {};

TEST(TrustedSignalsKVv2RequestHelperTest,
     TrustedBiddingSignalsRequestEncoding) {}

// TODO(crbug.com/337917489): When adding an identical trusted scoring signals
// request, it should use the existing partition instead of creating a new one.
// After the implementation, the EXPECT_EQ() of request I which is duplicated
// from request H, should be failed.
//
// Add the following trusted bidding signals requests:
// Request A[join_origin: foo.test, mode: group-by-origin]
// Request B[join_origin: foo.test, mode: group-by-origin]
// Request C[join_origin: foo.test, mode: compatibility]
// Request D[join_origin: foo.test, mode: compatibility]
// Request E[join_origin: bar.test, mode: compatibility]
// Request F[join_origin: bar.test, mode: group-by-origin]
// Request G[join_origin: bar.test, mode: compatibility]
// Request H[join_origin: bar.test, mode: compatibility]
// Request I[join_origin: bar.test, mode: compatibility]
// will result the following groups:
// Compression: 0 -
//    partition 0: A, B
//    partition 1: C
//    partition 2: D
// Compression: 1 -
//    partition 0: F
//    partition 1: E
//    partition 2: G
//    partition 3: H
//    partition 4: I
TEST(TrustedSignalsKVv2RequestHelperTest, TrustedBiddingSignalsIsolationIndex) {}

TEST(TrustedSignalsKVv2RequestHelperTest,
     TrustedScoringSignalsRequestEncoding) {}

// TODO(crbug.com/337917489): When adding an identical trusted scoring signals
// request, it should use the existing partition instead of creating a new one.
// After the implementation, the EXPECT_EQ() of request E which is duplicated
// from request A, should be failed.
//
// Add the following trusted bidding signals requests:
// Request A[render_url: foo.test, component_url: foosub.test,
//           owner_origin: owner-a, joining_origin: joining-a]
// Request B[render_url: foo.test, component_url: barsub.test,
//           owner_origin: owner-a, joining_origin: joining-a]
// Request C[render_url: bar.test, component_url: foosub.test,
//           owner_origin: owner-a, joining_origin: joining-a]
// Request D[render_url: bar.test, component_url: barsub.test,
//           owner_origin: owner-a, joining_origin: joining-a]
// Request E[render_url: foo.test, component_url: foosub.test,
//           owner_origin: owner-a, joining_origin: joining-a]
// Request F[render_url: foo.test, component_url: foosub.test,
//           owner_origin: owner-a, joining_origin: joining-b]
// Request G[render_url: foo.test, component_url: foosub.test,
//           owner_origin: owner-b, joining_origin: joining-a]
// Request H[render_url: foo.test, component_url: foosub.test,
//           owner_origin: owner-b, joining_origin: joining-b]
// will result the following groups:
// Compression: 0 -
//    partition 0: A
//    partition 1: B
//    partition 2: C
//    partition 4: D
//    partition 4: E
// Compression: 1 -
//    partition 0: F
// Compression: 2 -
//    partition 0: G
// Compression: 3 -
//    partition 0: H
TEST(TrustedSignalsKVv2RequestHelperTest, TrustedScoringSignalsIsolationIndex) {}

// Test trusted bidding signals response parsing with gzip compressed cbor
// bytes.
TEST_F(TrustedSignalsKVv2ResponseParserTest,
       TrustedBiddingSignalsResponseParsing) {}

// Test trusted bidding signals response parsing with uncompressed CBOR bytes.
TEST_F(TrustedSignalsKVv2ResponseParserTest,
       TrustedScoringSignalsResponseParsing) {}

TEST_F(TrustedSignalsKVv2ResponseParserTest, ResponseDecryptionFailure) {}

TEST_F(TrustedSignalsKVv2ResponseParserTest, SignalsFetchResultParseFailure) {}

TEST_F(TrustedSignalsKVv2ResponseParserTest,
       SignalsFetchResultMapParseFailure) {}

}  // namespace auction_worklet