// 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 SERVICES_NETWORK_TEST_TRUST_TOKEN_TEST_UTIL_H_ #define SERVICES_NETWORK_TEST_TRUST_TOKEN_TEST_UTIL_H_ #include <memory> #include <string> #include <string_view> #include "base/component_export.h" #include "base/containers/flat_map.h" #include "base/json/json_string_value_serializer.h" #include "base/test/task_environment.h" #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" #include "net/url_request/url_request_test_util.h" #include "services/network/public/mojom/trust_tokens.mojom.h" #include "services/network/public/mojom/url_response_head.mojom-forward.h" #include "services/network/trust_tokens/trust_token_request_helper.h" #include "testing/gtest/include/gtest/gtest.h" #include "url/origin.h" namespace net { class URLRequest; class URLRequestContext; } // namespace net namespace network { // TestURLRequestMaker is a mixin allowing consumers to factor out the // boilerplate of constructing URLRequests in tests. class TestURLRequestMaker { … }; // TrustTokenRequestHelperTest is a fixture common to tests for Trust Tokens // issuance, redemption, and signing. It factors out the boilerplate of // waiting for asynchronous store operations' results. class TrustTokenRequestHelperTest : public ::testing::Test { … }; // The following helper methods unify parameterized unit/integration testing of // the Trust Tokens interface. // // They provide a way to serialize a number of Trust Tokens parameter structures // to JSON in a manner that covers all of the Trust Tokens // parameters, and all of the permitted values of the enum and bool parameters, // in order to verify that the parameters' values correctly // serialize/deserialize and are properly propagated to the network stack. // // Intended use: // - parameterize tests by k{Issuance, Signing, Redemption}TrustTokenParameters; // - in the tests, call |SerializeTrustTokenParametersAndConstructExpectation| // to construct (1) a string representation of the trustToken JS argument and // (2) a corresponding mojom::TrustTokenParams object expected to // appear downstream; // - pass the provided argument to the API (fetch, iframe, XHR, ...) and check // that the corresponding Mojo struct does, in fact, subsequently materialize. // The instantiations of this struct will be serialized and passed to a // `fetch` call in executed JS. This class is declared out-of-line so that it // can be shared between embedder- and Blink-side code. struct TrustTokenTestParameters final { … }; // Serializes the value of a Trust Tokens enum parameter to its JS string // representation. Must be kept in sync with the corresponding IDL enum // definition. std::string TrustTokenEnumToString(mojom::TrustTokenOperationType operation); std::string TrustTokenEnumToString(mojom::TrustTokenRefreshPolicy policy); std::string TrustTokenEnumToString( mojom::TrustTokenSignRequestData sign_request_data); // For a given test case, creates and returns: // 1. a serialized JSON dictionary suitable for passing as the value of // `fetch`'s (and XHR's, and iframe's) `trustToken` parameter. // 2. a TrustTokenParams object that should equal the // value eventually passed downstream when a fetch/XHR/iframe load // is provided the serialized parameters. struct TrustTokenParametersAndSerialization { … }; TrustTokenParametersAndSerialization SerializeTrustTokenParametersAndConstructExpectation( const TrustTokenTestParameters& input); // These groups of parameters are defined in this utility file so that they can // be shared among different tests deserializing and propagating Trust Tokens // parameters; see above for a more detailed description of the intended use. const TrustTokenTestParameters kIssuanceTrustTokenTestParameters[]{ … }; const TrustTokenTestParameters kRedemptionTrustTokenTestParameters[]{ … }; const TrustTokenTestParameters kSigningTrustTokenTestParameters[]{ … }; // Given a well-formed key commitment record JSON and an issuer origin, returns // a serialized one-item dictionary mapping the commitment to the issuer. // // Example: // WrapKeyCommitmentForIssuers({{ // "https://issuer.com", R"( {"batchsize": 5} )" // }}) // = R"( { "https://issuer.com": { "batchsize": 5 } } )" std::string WrapKeyCommitmentsForIssuers( base::flat_map<url::Origin, std::string_view> issuers_and_commitments); } // namespace network #endif // SERVICES_NETWORK_TEST_TRUST_TOKEN_TEST_UTIL_H_