chromium/content/browser/renderer_host/mixed_content_checker_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/renderer_host/mixed_content_checker.h"

#include <memory>
#include <optional>
#include <ostream>
#include <tuple>
#include <vector>

#include "content/public/browser/web_contents.h"
#include "content/public/test/fake_local_frame.h"
#include "content/test/navigation_simulator_impl.h"
#include "content/test/test_render_frame_host.h"
#include "content/test/test_render_view_host.h"
#include "services/network/public/mojom/source_location.mojom-forward.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/fetch/fetch_api_request.mojom.h"
#include "third_party/blink/public/mojom/loader/mixed_content.mojom.h"
#include "third_party/blink/public/mojom/security_context/insecure_request_policy.mojom.h"
#include "third_party/blink/public/mojom/use_counter/metrics/web_feature.mojom-shared.h"
#include "url/gurl.h"

namespace content {
namespace {

Eq;
FieldsAre;
IsEmpty;
Optional;
UnorderedElementsAre;

// Intercepts the mojo calls of `MixedContentFound()` and
// `ReportBlinkFeatureUsage()` from `rfh` to a LocalFrame in renderer.
class LocalFrameInterceptor : public FakeLocalFrame {};

// Needed by GTest to display errors.
std::ostream& operator<<(std::ostream& out,
                         const LocalFrameInterceptor::MixedContentResult& m) {}

}  // namespace

// Tests that `content::MixedContentChecker` correctly detects or ignores many
// cases where there is or there is not mixed content, respectively.
// Note: Browser side version of
// `blink::MixedContentCheckerTest::IsMixedContent`.
// Must be kept in sync manually!
// LINT.IfChange
TEST(MixedContentCheckerTest, IsMixedContent) {}
// LINT.ThenChange(third_party/blink/renderer/core/loader/mixed_content_checker_test.cc)

class MixedContentCheckerShouldBlockTestBase
    : public RenderViewHostImplTestHarness,
      public testing::WithParamInterface<bool> {};

class MixedContentCheckerShouldBlockNavigationTestBase
    : public MixedContentCheckerShouldBlockTestBase {};

MixedContentCheckerShouldBlockNavigationTest;

INSTANTIATE_TEST_SUITE_P();

// Main frame navigations cannot be mixed content, no matter the source page is
// secure or not.
TEST_P(MixedContentCheckerShouldBlockNavigationTest,
       ShouldNotBlockNavigationFromInsecureMainFrame) {}

// Main frame navigations cannot be mixed content, no matter the source page is
// secure or not.
TEST_P(MixedContentCheckerShouldBlockNavigationTest,
       ShouldNotBlockNavigationFromSecureMainFrame) {}

// Navigates from insecure content is not mixed content.
TEST_P(MixedContentCheckerShouldBlockNavigationTest,
       ShouldNotBlockNavigationFromInsecureSubFrame) {}

// Tests to cover MixedContentContextType = kBlockable.
class MixedContentCheckerShouldBlockNavigationWithBlockableContextTest
    : public MixedContentCheckerShouldBlockNavigationTestBase {};

INSTANTIATE_TEST_SUITE_P();

// ShouldBlockNavigation(subframe) => true
// - MixedContentContextType = kBlockable
// - main frame's InsecureRequestPolicy = kLeaveInsecureRequestsAlone
TEST_P(MixedContentCheckerShouldBlockNavigationWithBlockableContextTest,
       ShouldBlockMixedContentNavigationWithPolicyLeaveInsecureRequestAlone) {}

// ShouldBlockNavigation(subframe) => true
// - MixedContentContextType = kBlockable
// - main frame's InsecureRequestPolicy = kBlockAllMixedContent
TEST_P(MixedContentCheckerShouldBlockNavigationWithBlockableContextTest,
       ShouldBlockMixedContentNavigationWithPolicyBlockAll) {}

// Tests to cover MixedContentContextType = kOptionallyBlockable.
class MixedContentCheckerShouldBlockNavigationWithOptionallyBlockableContextTest
    : public MixedContentCheckerShouldBlockNavigationTestBase {};

INSTANTIATE_TEST_SUITE_P();

// ShouldBlockNavigation(subframe) => false
// - MixedContentContextType = kOptionallyBlockable
// - main frame's InsecureRequestPolicy = kLeaveInsecureRequestsAlone
TEST_P(
    MixedContentCheckerShouldBlockNavigationWithOptionallyBlockableContextTest,
    ShouldNotBlockMixedContentNavigationWithPolicyLeaveInsecureRequestAlone) {}

// ShouldBlockNavigation(subframe) => true
// - MixedContentContextType = kOptionallyBlockable
// - main frame's InsecureRequestPolicy = kBlockAllMixedContent
TEST_P(
    MixedContentCheckerShouldBlockNavigationWithOptionallyBlockableContextTest,
    ShouldBlockMixedContentNavigationWithPolicyBlockAll) {}

// Tests to cover MixedContentContextType = kShouldBeBlockable.
class MixedContentCheckerShouldBlockNavigationWithShouldBeBlockableContextTest
    : public MixedContentCheckerShouldBlockNavigationTestBase {};

INSTANTIATE_TEST_SUITE_P();

// ShouldBlockNavigation(subframe) => false
// - MixedContentContextType = kShouldBeBlockable
// - main frame's InsecureRequestPolicy = kLeaveInsecureRequestsAlone
TEST_P(
    MixedContentCheckerShouldBlockNavigationWithShouldBeBlockableContextTest,
    ShouldNotBlockMixedContentNavigationWithPolicyLeaveInsecureRequestAlone) {}

// ShouldBlockNavigation(subframe) => true
// - MixedContentContextType = kShouldBeBlockable
// - main frame's InsecureRequestPolicy = kBlockAllMixedContent
TEST_P(MixedContentCheckerShouldBlockNavigationWithShouldBeBlockableContextTest,
       ShouldBlockMixedContentNavigationWithPolicyBlockAll) {}

class MixedContentCheckerShouldBlockFetchKeepAliveTestBase
    : public MixedContentCheckerShouldBlockTestBase {};

MixedContentCheckerShouldBlockFetchKeepAliveTest;

INSTANTIATE_TEST_SUITE_P();

// Loading insecure url from insecure main frame should not be blocked.
TEST_P(MixedContentCheckerShouldBlockFetchKeepAliveTest,
       ShouldNotBlockInsecureFetchFromInsecureMainFrame) {}

// Loading insecure url from insecure subframe should not be blocked.
TEST_P(MixedContentCheckerShouldBlockFetchKeepAliveTest,
       ShouldNotBlockInsecureFetchFromInsecureSubFrame) {}

// Loading insecure url from secure main/sub frame should be blocked, where the
// frame's InsecureRequestPolicy = kLeaveInsecureRequestsAlone.
TEST_P(
    MixedContentCheckerShouldBlockFetchKeepAliveTest,
    ShouldBlockInsecureFetchFromSecureFrameWithPolicyLeaveInsecureRequestAlone) {}

// Loading insecure url from secure main/sub frame should be blocked, where the
// frame's InsecureRequestPolicy = kBlockAllMixedContent.
TEST_P(MixedContentCheckerShouldBlockFetchKeepAliveTest,
       ShouldBlockInsecureFetchFromSecureFrameWithPolicyBlockAllMixedContent) {}

}  // namespace content