File: policy_container_host_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (118 lines) | stat: -rw-r--r-- 5,049 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// 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.

#include "content/browser/renderer_host/policy_container_host.h"

#include "base/run_loop.h"
#include "content/public/test/browser_task_environment.h"
#include "services/network/public/mojom/content_security_policy.mojom.h"
#include "services/network/public/mojom/integrity_policy.mojom.h"
#include "services/network/public/mojom/ip_address_space.mojom-shared.h"
#include "services/network/public/mojom/referrer_policy.mojom-shared.h"
#include "services/network/public/mojom/web_sandbox_flags.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace content {

using ::testing::ByRef;
using ::testing::Eq;
using ::testing::Pointee;

namespace {

struct SameSizeAsPolicyContainerPolicies {
  network::mojom::ReferrerPolicy referrer_policy;
  network::mojom::IPAddressSpace ip_address_space;
  bool is_web_secure_context;
  std::vector<network::mojom::ContentSecurityPolicyPtr>
      content_security_policies;
  network::CrossOriginOpenerPolicy cross_origin_opener_policy;
  network::CrossOriginEmbedderPolicy cross_origin_embedder_policy;
  network::DocumentIsolationPolicy document_isolation_policy;
  network::IntegrityPolicy integrity_policy;
  network::IntegrityPolicy integrity_policy_report_only;
  network::mojom::WebSandboxFlags sandbox_flags;
  bool is_credentialless;
  bool can_navigate_top_without_user_gesture;
};

}  // namespace

// Asserts size of PolicyContainerPolicies, so that whenever a new element is
// added to PolicyContainerPolicies, the assert will fail. When hitting this
// assert failure, please ensure that the attribute is
// - added to the PolicyContainerPolicies constructor
// - copied correctly in PolicyContainerPolicies::Clone()
// - checked correctly in PolicyContainerPolicies::operator==
// - handled correctly in PolicyContainerPolicies::operator<<
// - tested correctly in PolicyContainerHostTest.PolicyContainerPolicies below.
static_assert(sizeof(PolicyContainerPolicies) ==
                  sizeof(SameSizeAsPolicyContainerPolicies),
              "PolicyContainerPolicies have been modified. Please carefully "
              "read the comment in this file and make sure you updated all "
              "relevant methods of `PolicyContainerPolicies`.");

TEST(PolicyContainerPoliciesTest, CloneIsEqual) {
  std::vector<network::mojom::ContentSecurityPolicyPtr> csps;
  auto csp = network::mojom::ContentSecurityPolicy::New();
  csp->treat_as_public_address = true;
  csps.push_back(std::move(csp));

  network::CrossOriginOpenerPolicy coop;
  network::mojom::WebSandboxFlags sandbox_flags =
      network::mojom::WebSandboxFlags::kOrientationLock |
      network::mojom::WebSandboxFlags::kPropagatesToAuxiliaryBrowsingContexts;
  coop.value = network::mojom::CrossOriginOpenerPolicyValue::kSameOrigin;
  coop.report_only_value =
      network::mojom::CrossOriginOpenerPolicyValue::kSameOriginAllowPopups;
  coop.reporting_endpoint = "endpoint 1";
  coop.report_only_reporting_endpoint = "endpoint 2";

  network::CrossOriginEmbedderPolicy coep;
  coep.value = network::mojom::CrossOriginEmbedderPolicyValue::kRequireCorp;
  coep.report_only_value =
      network::mojom::CrossOriginEmbedderPolicyValue::kCredentialless;
  coep.reporting_endpoint = "endpoint 1";
  coep.report_only_reporting_endpoint = "endpoint 2";

  network::DocumentIsolationPolicy dip;
  dip.value =
      network::mojom::DocumentIsolationPolicyValue::kIsolateAndRequireCorp;
  dip.report_only_value =
      network::mojom::DocumentIsolationPolicyValue::kIsolateAndCredentialless;
  dip.reporting_endpoint = "endpoint 1";
  dip.report_only_reporting_endpoint = "endpoint 2";

  network::IntegrityPolicy ip;
  ip.sources.push_back(network::mojom::IntegrityPolicy::Source::kInline);
  ip.blocked_destinations.push_back(
      network::mojom::IntegrityPolicy::Destination::kScript);
  ip.endpoints.push_back("integrity endpoint");

  PolicyContainerPolicies policies(
      network::mojom::ReferrerPolicy::kAlways,
      network::mojom::IPAddressSpace::kUnknown,
      /*is_web_secure_context=*/true, std::move(csps), coop, coep,
      std::move(dip), ip, network::IntegrityPolicy(), sandbox_flags,
      /*is_credentialless=*/true,
      /*can_navigate_top_without_user_gesture=*/true,
      /*cross_origin_isolation_enabled_by_dip=*/false);

  EXPECT_THAT(policies.Clone(), Eq(ByRef(policies)));
}

TEST(PolicyContainerHostTest, ReferrerPolicy) {
  scoped_refptr<PolicyContainerHost> policy_container =
      base::MakeRefCounted<PolicyContainerHost>();
  EXPECT_EQ(network::mojom::ReferrerPolicy::kDefault,
            policy_container->referrer_policy());

  static_cast<blink::mojom::PolicyContainerHost*>(policy_container.get())
      ->SetReferrerPolicy(network::mojom::ReferrerPolicy::kAlways);
  EXPECT_EQ(network::mojom::ReferrerPolicy::kAlways,
            policy_container->referrer_policy());
}

}  // namespace content