File: back_forward_cache_policy_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (197 lines) | stat: -rw-r--r-- 7,678 bytes parent folder | download | duplicates (6)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// 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 "base/test/scoped_feature_list.h"
#include "base/values.h"
#include "chrome/browser/policy/policy_test_utils.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/policy_constants.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_utils.h"

class Browser;

namespace policy {

enum class CacheControlNoStorePagePolicy {
  kDefault,
  kAllowed,
  kDisallowed,
};
struct BackForwardCacheWithCacheControlNoStorePagePolicyTestParam {
  CacheControlNoStorePagePolicy policy_value;
  bool expected_allow_bfcache_ccns_page;
};

class BackForwardCacheWithCacheControlNoStorePagePolicyBrowserTest
    : public PolicyTest,
      public ::testing::WithParamInterface<
          BackForwardCacheWithCacheControlNoStorePagePolicyTestParam> {
 public:
  static std::string DescribeParams(
      const ::testing::TestParamInfo<ParamType>& info) {
    switch (info.param.policy_value) {
      case CacheControlNoStorePagePolicy::kDefault:
        return "Default";
      case CacheControlNoStorePagePolicy::kAllowed:
        return "Allowed";
      case CacheControlNoStorePagePolicy::kDisallowed:
        return "Disallowed";
    }
  }

 protected:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    feature_list_.InitWithFeaturesAndParameters(
        {{features::kBackForwardCache, {}},
         {features::kCacheControlNoStoreEnterBackForwardCache,
          {{"level", "restore-unless-http-only-cookie-change"}}}},
        {});
  }

  void SetUpInProcessBrowserTestFixture() override {
    PolicyTest::SetUpInProcessBrowserTestFixture();

    if (GetParam().policy_value == CacheControlNoStorePagePolicy::kDefault) {
      return;
    }

    // Set up the policy value for
    // `kAllowBackForwardCacheForCacheControlNoStorePageEnabled`.
    PolicyMap policies;
    SetPolicy(&policies,
              key::kAllowBackForwardCacheForCacheControlNoStorePageEnabled,
              base::Value(GetParam().policy_value ==
                          CacheControlNoStorePagePolicy::kAllowed));
    provider_.UpdateChromePolicy(policies);
  }

  content::RenderFrameHost* current_render_frame_host() {
    return chrome_test_utils::GetActiveWebContents(this)->GetPrimaryMainFrame();
  }

 private:
  base::test::ScopedFeatureList feature_list_;
};

const auto test_suite_value = ::testing::Values(
    BackForwardCacheWithCacheControlNoStorePagePolicyTestParam{
        CacheControlNoStorePagePolicy::kDefault,
        /* expected_allow_bfcache_ccns_page= */ true},
    BackForwardCacheWithCacheControlNoStorePagePolicyTestParam{
        CacheControlNoStorePagePolicy::kAllowed,
        /* expected_allow_bfcache_ccns_page= */ true},
    BackForwardCacheWithCacheControlNoStorePagePolicyTestParam{
        CacheControlNoStorePagePolicy::kDisallowed,
        /* expected_allow_bfcache_ccns_page= */ false});

INSTANTIATE_TEST_SUITE_P(
    All,
    BackForwardCacheWithCacheControlNoStorePagePolicyBrowserTest,
    test_suite_value,
    &BackForwardCacheWithCacheControlNoStorePagePolicyBrowserTest::
        DescribeParams);

// Test that a page loaded with "Cache-Control:no-store" header cannot enter
// BackForwardCache if the ContentBrowserClient disables BFCache for CCNS pages.
IN_PROC_BROWSER_TEST_P(
    BackForwardCacheWithCacheControlNoStorePagePolicyBrowserTest,
    PolicyIsFollowed) {
  ASSERT_TRUE(embedded_test_server()->Start());

  GURL url_a(embedded_test_server()->GetURL(
      "a.com", "/set-header?Cache-Control: no-store"));
  GURL url_b(embedded_test_server()->GetURL("b.com", "/title1.html"));
  GURL url_c(embedded_test_server()->GetURL("c.com", "/title1.html"));

  // 1) Load the document and specify no-store for the main resource.
  ASSERT_TRUE(NavigateToUrl(url_a, this));
  content::RenderFrameHostWrapper rfh_a(current_render_frame_host());

  // 2) Navigate away. If the enterprise policy disallows BFCaching CCNS page,
  // `rfh_a` should not enter BFCache. Otherwise, `rfh_a` should be stored in
  // BFCache.
  ASSERT_TRUE(NavigateToUrl(url_b, this));
  content::RenderFrameHostWrapper rfh_b(current_render_frame_host());
  if (GetParam().expected_allow_bfcache_ccns_page) {
    ASSERT_TRUE(rfh_a->GetLifecycleState() ==
                content::RenderFrameHost::LifecycleState::kInBackForwardCache);
  } else {
    ASSERT_TRUE(rfh_a.WaitUntilRenderFrameDeleted());
  }

  // 3) Verify that the page without CCNS is eligible for BFCache.
  ASSERT_TRUE(NavigateToUrl(url_c, this));
  ASSERT_TRUE(rfh_b->GetLifecycleState() ==
              content::RenderFrameHost::LifecycleState::kInBackForwardCache);
}

// Test that the `ShouldAllowBackForwardCacheForCacheControlNoStorePage()`
// returns the correct value for different policy settings.
IN_PROC_BROWSER_TEST_P(
    BackForwardCacheWithCacheControlNoStorePagePolicyBrowserTest,
    ShouldAllowBackForwardCacheForCacheControlNoStorePage) {
  bool should_allow_bfcache_ccns_page =
      content::GetContentClientForTesting()
          ->browser()
          ->ShouldAllowBackForwardCacheForCacheControlNoStorePage(
              current_render_frame_host()->GetBrowserContext());

  ASSERT_EQ(should_allow_bfcache_ccns_page,
            GetParam().expected_allow_bfcache_ccns_page);
}

class BackForwardCacheWithCacheControlNoStorePagePolicyBrowserTestKioskMode
    : public BackForwardCacheWithCacheControlNoStorePagePolicyBrowserTest {
 protected:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    command_line->AppendSwitch(switches::kKioskMode);
    BackForwardCacheWithCacheControlNoStorePagePolicyBrowserTest::
        SetUpCommandLine(command_line);
  }
};

INSTANTIATE_TEST_SUITE_P(
    All,
    BackForwardCacheWithCacheControlNoStorePagePolicyBrowserTestKioskMode,
    test_suite_value,
    &BackForwardCacheWithCacheControlNoStorePagePolicyBrowserTest::
        DescribeParams);

// Test that a page loaded with "Cache-Control:no-store" header cannot enter
// BackForwardCache if the ContentBrowserClient disables BFCache for CCNS pages.
IN_PROC_BROWSER_TEST_P(
    BackForwardCacheWithCacheControlNoStorePagePolicyBrowserTestKioskMode,
    PolicyIsOverridenByKioskMode) {
  ASSERT_TRUE(embedded_test_server()->Start());

  GURL url_a(embedded_test_server()->GetURL(
      "a.com", "/set-header?Cache-Control: no-store"));
  GURL url_b(embedded_test_server()->GetURL("b.com", "/title1.html"));
  GURL url_c(embedded_test_server()->GetURL("c.com", "/title1.html"));

  // 1) Load the document and specify no-store for the main resource.
  ASSERT_TRUE(NavigateToUrl(url_a, this));
  content::RenderFrameHostWrapper rfh_a(current_render_frame_host());

  // 2) Navigate away. If the enterprise policy disallows BFCaching CCNS page,
  // `rfh_a` should not enter BFCache. Otherwise, `rfh_a` should be stored in
  // BFCache.
  ASSERT_TRUE(NavigateToUrl(url_b, this));
  content::RenderFrameHostWrapper rfh_b(current_render_frame_host());
  ASSERT_TRUE(rfh_a.WaitUntilRenderFrameDeleted());

  // 3) Verify that the page without CCNS is eligible for BFCache.
  ASSERT_TRUE(NavigateToUrl(url_c, this));
  ASSERT_TRUE(rfh_b->GetLifecycleState() ==
              content::RenderFrameHost::LifecycleState::kInBackForwardCache);
}

}  // namespace policy