// Copyright 2022 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "third_party/blink/public/common/fenced_frame/redacted_fenced_frame_config_mojom_traits.h" #include <functional> #include <optional> #include <tuple> #include <type_traits> #include <utility> #include "base/ranges/algorithm.h" #include "base/test/gtest_util.h" #include "content/browser/fenced_frame/fenced_frame_config.h" #include "content/browser/fenced_frame/fenced_frame_reporter.h" #include "content/public/test/test_renderer_host.h" #include "mojo/public/cpp/test_support/test_utils.h" #include "net/base/schemeful_site.h" #include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h" #include "testing/gtest/include/gtest/gtest.h" #include "third_party/blink/public/common/fenced_frame/redacted_fenced_frame_config.h" #include "third_party/blink/public/mojom/fenced_frame/fenced_frame_config.mojom.h" #include "url/origin.h" namespace content { RedactedFencedFrameConfig; RedactedFencedFrameProperties; Entity; // This macro creates the following test pattern: // * Redact a config. // * Check that the desired property was redacted as expected. // * Serialize and deserialize the redacted config into a copy (using mojom type // mappings implicitly). // * Check that the desired property was copied correctly. // // Template Arguments: // `ClassName`: `FencedFrameConfig` or `FencedFrameProperties` // Which base class to use for this test. // `RedactedClassName`: `RedactedFencedFrameConfig` or // `RedactedFencedFrameProperties` // The redacted version of `ClassName`. // `TestType`: The type of the value being tested. // `RedactedTestType`: The type of the redacted value being tested. Sometimes // the type of variable being stored in the non-redacted and // its redacted equivalent can differ. // `UnredactedToRedactedCompare`: The shape of the function that will compare // an unredacted config value to a redacted // config value. // `RedactedToRedactedCompare`: The shape of the function that will compare // two redacted config values. // // Arguments: // `config`: The FencedFrameConfig or FencedFrameProperties object being tested. // `property`: A pointer to the class's field to test (e.g. `mapped_url_`) // `entity`: The entity (kEmbedder or kContent) that the config should be // redacted for in the test. // `is_defined`: Whether we expect the property to have a defined value. // `is_opaque`: Whether we expect the property's defined value to be opaque. // `unredacted_redacted_equality_fn`: A comparator function that has the // function signature is_eq(`type`, Redacted`type`). A return value of // `true` means equal; `false` means not equal. // `redacted_redacted_equality_fn`: A comparator function that has the function // signature is_eq(Redacted`type`, Redacted`type`). A return value of `true` // means equal; `false` means not equal. template <typename ClassName, typename RedactedClassName, typename TestType, typename RedactedTestType, typename UnredactedToRedactedCompare, typename RedactedToRedactedCompare> void TestPropertyForEntityIsDefinedIsOpaque( ClassName config, std::optional<FencedFrameProperty<TestType>> ClassName::*property, const std::optional< blink::FencedFrame::RedactedFencedFrameProperty<RedactedTestType>>& ( RedactedClassName::*redacted_property)() const, Entity entity, bool is_defined, bool is_opaque, UnredactedToRedactedCompare unredacted_redacted_equality_fn, RedactedToRedactedCompare redacted_redacted_equality_fn) { … } class FencedFrameConfigMojomTraitsTest : public RenderViewHostTestHarness { … }; TEST_F(FencedFrameConfigMojomTraitsTest, ConfigMojomTraitsInternalUrnTest) { … } TEST_F(FencedFrameConfigMojomTraitsTest, ConfigMojomTraitsModeTest) { … } TEST_F(FencedFrameConfigMojomTraitsTest, ConfigMojomTraitsNullInternalUrnTest) { … } // C++23: Can replace AndThen(opt, acc) with opt.and_then(acc). template <typename Opt, typename Acc> std::optional<GURL> AndThen(const Opt& opt, Acc acc) { … } // Projections for [un]redacted fenced frame configs to allow comparisons. These // only compare the `mapped_url` field for convenience. (We don't need an // equality operator for configs outside of tests, so it would be wasteful to // declare it as default in the class declaration.) std::optional<GURL> Project(const FencedFrameConfig& config) { … } std::optional<GURL> Project(const RedactedFencedFrameConfig& config) { … } TEST_F(FencedFrameConfigMojomTraitsTest, ConfigMojomTraitsTest) { … } // Test `has_fenced_frame_reporting`, which only appears in // FencedFrameProperties, and does not use the redacted mechanism used by other // fields. TEST_F(FencedFrameConfigMojomTraitsTest, PropertiesHasFencedFrameReportingTest) { … } // Test `can_disable_untrusted_network`, which only appears in // FencedFrameProperties, and does not use the redacted mechanism used by other // fields. TEST_F(FencedFrameConfigMojomTraitsTest, PropertiesCanDisableUntrustedNetworkTest) { … } } // namespace content