File: keep_alive_url_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 (350 lines) | stat: -rw-r--r-- 14,917 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// 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 <memory>
#include <string>
#include <vector>

#include "base/strings/stringprintf.h"
#include "base/test/allow_check_is_test_for_testing.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "chrome/browser/lifetime/browser_shutdown.h"
#include "chrome/browser/loader/keep_alive_request_browsertest_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/page_load_metrics/browser/features.h"
#include "components/variations/net/variations_http_headers.h"
#include "content/public/browser/back_forward_cache.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/keep_alive_url_loader_utils.h"
#include "content/public/test/test_utils.h"
#include "net/http/http_request_headers.h"
#include "net/http/http_status_code.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "url/url_util.h"

namespace {

using testing::Contains;
using testing::Key;
using testing::Not;

}  // namespace

class ChromeKeepAliveURLBrowserTestBase
    : public ChromeKeepAliveRequestBrowserTestBase {
 public:
  ChromeKeepAliveURLBrowserTestBase() {
    InitFeatureList({{blink::features::kKeepAliveInBrowserMigration, {}}});
  }
  ~ChromeKeepAliveURLBrowserTestBase() override = default;

  // Not copyable.
  ChromeKeepAliveURLBrowserTestBase(const ChromeKeepAliveURLBrowserTestBase&) =
      delete;
  ChromeKeepAliveURLBrowserTestBase& operator=(
      const ChromeKeepAliveURLBrowserTestBase&) = delete;
};

// Basic Chrome browser tests to cover behaviors when handling fetch keepalive
// requests in browser process.
//
// Tests here ensure the behaviors are the same as their counterparts in
// `content/browser` even with extra logic added by Chrome embedder.
class ChromeKeepAliveURLBrowserTest
    : public ChromeKeepAliveURLBrowserTestBase,
      public ::testing::WithParamInterface<std::string> {
 protected:
  const std::string& method() const { return GetParam(); }
};

INSTANTIATE_TEST_SUITE_P(
    All,
    ChromeKeepAliveURLBrowserTest,
    ::testing::Values(net::HttpRequestHeaders::kGetMethod,
                      net::HttpRequestHeaders::kPostMethod),
    [](const testing::TestParamInfo<ChromeKeepAliveURLBrowserTest::ParamType>&
           info) { return info.param; });

IN_PROC_BROWSER_TEST_P(ChromeKeepAliveURLBrowserTest, OneRequest) {
  const std::string target_url = kKeepAliveEndpoint;
  auto request_handler = std::move(RegisterRequestHandlers({target_url})[0]);
  ASSERT_TRUE(server()->Start());

  ASSERT_TRUE(NavigateToURL(
      web_contents(), GetKeepAlivePageURL(kPrimaryHost, target_url, method())));
  // Ensure the keepalive request is sent, but delay response.
  request_handler->WaitForRequest();

  // End the keepalive request by sending back response.
  request_handler->Send(k200TextResponse);
  request_handler->Done();

  content::TitleWatcher watcher(web_contents(), kPromiseResolvedPageTitle);
  EXPECT_EQ(watcher.WaitAndGetTitle(), kPromiseResolvedPageTitle);
  loaders_observer().WaitForTotalOnReceiveResponseForwarded(1);
  loaders_observer().WaitForTotalOnCompleteForwarded({net::OK});
}

// Delays response to a keepalive ping until after the page making the keepalive
// ping has been unloaded. The browser must ensure the response is received and
// processed by the browser.
IN_PROC_BROWSER_TEST_P(ChromeKeepAliveURLBrowserTest,
                       ReceiveResponseAfterPageUnload) {
  const std::string target_url = kKeepAliveEndpoint;
  auto request_handler = std::move(RegisterRequestHandlers({target_url})[0]);
  ASSERT_TRUE(server()->Start());

  LoadPageWithKeepAliveRequestAndSendResponseAfterUnload(
      GetKeepAlivePageURL(kPrimaryHost, target_url, method()),
      request_handler.get(), k200TextResponse);

  // The response should be processed in browser.
  loaders_observer().WaitForTotalOnReceiveResponseProcessed(1);
}

// Shutdown delay is not supported on Android.
#if !BUILDFLAG(IS_ANDROID)
// Mac browser shutdown is flaky: https://crbug.com/1259913
#if BUILDFLAG(IS_MAC)
#define MAYBE_ReceiveResponseAfterBrowserShutdown \
  DISABLED_ReceiveResponseAfterBrowserShutdown
#else
#define MAYBE_ReceiveResponseAfterBrowserShutdown \
  ReceiveResponseAfterBrowserShutdown
#endif
// Verifies that a keepalive ping can be made within a short timeframe after
// browser shutdown.
IN_PROC_BROWSER_TEST_P(ChromeKeepAliveURLBrowserTest,
                       MAYBE_ReceiveResponseAfterBrowserShutdown) {
  const std::string target_url = kKeepAliveEndpoint;
  auto request_handler = std::move(RegisterRequestHandlers({target_url})[0]);
  ASSERT_TRUE(server()->Start());
  auto keepalive_page_url =
      GetKeepAlivePageURL(kPrimaryHost, target_url, method());

  ASSERT_TRUE(content::NavigateToURL(web_contents(), keepalive_page_url));

  // Close the browser.
  CloseBrowserSynchronously(browser());
  ASSERT_TRUE(browser_shutdown::IsTryingToQuit());
  ASSERT_TRUE(BrowserList::GetInstance()->empty());
  ASSERT_EQ(browser_shutdown::GetShutdownType(),
            browser_shutdown::ShutdownType::kWindowClose);
  // The keepalive request may be sent before or after shutting down, but only
  // get processed by the server after shutting down here.
  request_handler->WaitForRequest();
  // The disconnected loader is pending to receive response.

  // Send back response to terminate in-browser request handling.
  request_handler->Send(k200TextResponse);
  request_handler->Done();

  // The response should be processed by browser before shutting down.
  // TODO(crbug.com/40236167): Deflake WaitForTotalOnReceiveResponseProcessed
}
#endif  // !BUILDFLAG(IS_ANDROID)

// Delays response to a keepalive ping until after the page making the keepalive
// ping is put into BackForwardCache. The response should be processed by the
// renderer after the page is restored from BackForwardCache.
IN_PROC_BROWSER_TEST_P(ChromeKeepAliveURLBrowserTest,
                       ReceiveResponseInBackForwardCache) {
  const std::string target_url = kKeepAliveEndpoint;
  auto request_handler = std::move(RegisterRequestHandlers({target_url})[0]);
  ASSERT_TRUE(server()->Start());

  ASSERT_TRUE(NavigateToURL(
      web_contents(), GetKeepAlivePageURL(kPrimaryHost, target_url, method())));
  content::RenderFrameHostWrapper rfh_1(current_frame_host());
  // Ensure the keepalive request is sent before leaving the current page.
  request_handler->WaitForRequest();

  // Navigate to cross-origin page.
  ASSERT_TRUE(NavigateToURL(web_contents(), GetCrossOriginPageURL()));
  // Ensure the previous page has been put into BackForwardCache.
  ASSERT_EQ(rfh_1->GetLifecycleState(),
            content::RenderFrameHost::LifecycleState::kInBackForwardCache);

  // Send back response.
  request_handler->Send(k200TextResponse);
  // The response is immediately forwarded to the in-BackForwardCache renderer.
  loaders_observer().WaitForTotalOnReceiveResponseForwarded(1);
  // Go back to `rfh_1`.
  ASSERT_TRUE(HistoryGoBack(web_contents()));

  // The response should be processed in renderer. Hence resolving Promise.
  content::TitleWatcher watcher(web_contents(), kPromiseResolvedPageTitle);
  EXPECT_EQ(watcher.WaitAndGetTitle(), kPromiseResolvedPageTitle);
  request_handler->Done();
  loaders_observer().WaitForTotalOnCompleteForwarded({net::OK});
}

// Delays handling redirect for a keepalive ping until after the page making the
// keepalive ping has been unloaded. The browser must ensure the redirect is
// verified and properly processed by the browser.
IN_PROC_BROWSER_TEST_P(ChromeKeepAliveURLBrowserTest,
                       ReceiveRedirectAfterPageUnload) {
  const std::string target_url = kKeepAliveEndpoint;
  const char redirect_target[] = "/beacon-redirected";
  auto request_handlers =
      RegisterRequestHandlers({target_url, redirect_target});
  ASSERT_TRUE(server()->Start());

  // Set up redirects according to the following redirect chain:
  // fetch("http://a.com:<port>/beacon", keepalive: true)
  // --> http://a.com:<port>/beacon-redirected
  LoadPageWithKeepAliveRequestAndSendResponseAfterUnload(
      GetKeepAlivePageURL(kPrimaryHost, target_url, method()),
      request_handlers[0].get(),
      base::StringPrintf(k301ResponseTemplate, redirect_target));

  // The redirect request should be processed in browser and gets sent.
  request_handlers[1]->WaitForRequest();
  // No variations header when redirected to non-Google.
  EXPECT_THAT(request_handlers[1]->http_request()->headers,
              Not(Contains(Key(variations::kClientDataHeader))));
  // End the keepalive request by sending back final response.
  request_handlers[1]->Send(k200TextResponse);
  request_handlers[1]->Done();

  // The response should be processed in browser.
  loaders_observer().WaitForTotalOnReceiveResponseProcessed(1);
}

// Delays handling an unsafe redirect for a keepalive ping until after the page
// making the keepalive ping has been unloaded.
// The browser must ensure the unsafe redirect is not followed.
// TODO(crbug.com/407716208): Broken by crrev.com/c/6039011. Fix and re-enable
// this test.
IN_PROC_BROWSER_TEST_P(ChromeKeepAliveURLBrowserTest,
                       DISABLED_ReceiveUnSafeRedirectAfterPageUnload) {
  const std::string target_url = kKeepAliveEndpoint;
  const char unsafe_redirect_target[] = "chrome://settings";
  auto request_handler = std::move(RegisterRequestHandlers({target_url})[0]);
  ASSERT_TRUE(server()->Start());

  // Sets up redirects according to the following redirect chain:
  // fetch("http://a.com:<port>/beacon", keepalive: true)
  // --> chrome://settings
  LoadPageWithKeepAliveRequestAndSendResponseAfterUnload(
      GetKeepAlivePageURL(kPrimaryHost, target_url, method()),
      request_handler.get(),
      base::StringPrintf("HTTP/1.1 301 Moved Permanently\r\n"
                         "Location: %s\r\n"
                         "\r\n",
                         unsafe_redirect_target));

  // The redirect is unsafe, so the loader is terminated.
  loaders_observer().WaitForTotalOnCompleteProcessed(
      {net::ERR_UNSAFE_REDIRECT});
  // The test is too flaky to assert on the number of redirects processed.
}

// Checks that when a fetch keepalive request's redirect is handled in browser
// the variations header (X-Client-Data) is attached to the requests to Google.
// TODO(crbug.com/407998594): Fix test before re-enabling.
IN_PROC_BROWSER_TEST_P(
    ChromeKeepAliveURLBrowserTest,
    DISABLED_ReceiveMultipleRedirectsToGoogleAfterPageUnload) {
  const std::string target_url = kKeepAliveEndpoint;
  const std::string redirect_target1 = "/redirected1";
  const std::string redirect_target2 = "/redirected2";
  SetUseHttps();
  auto request_handlers =
      RegisterRequestHandlers({target_url, redirect_target1, redirect_target2});
  ASSERT_TRUE(server()->Start());

  // Set up redirects according to the following redirect chain:
  // fetch("https://www.google.com:<port>/beacon", keepalive: true)
  // --> https://www.google.com:<port>/redirected1
  // --> https://www.google.com:<port>/redirected2
  LoadPageWithKeepAliveRequestAndSendResponseAfterUnload(
      GetKeepAlivePageURL(kGoogleHost, target_url, method()),
      request_handlers[0].get(),
      base::StringPrintf(k301ResponseTemplate, redirect_target1.c_str()));

  request_handlers[1]->WaitForRequest();
  EXPECT_THAT(request_handlers[1]->http_request()->headers,
              Contains(Key(variations::kClientDataHeader)));
  request_handlers[1]->Send(
      base::StringPrintf(k301ResponseTemplate, redirect_target2.c_str()));
  request_handlers[1]->Done();

  request_handlers[2]->WaitForRequest();
  EXPECT_THAT(request_handlers[2]->http_request()->headers,
              Contains(Key(variations::kClientDataHeader)));
  // End the keepalive request by sending back final response.
  request_handlers[2]->Send(k200TextResponse);
  request_handlers[2]->Done();

  // The redirects/response should all be processed in browser.
  loaders_observer().WaitForTotalOnReceiveRedirectProcessed(2);
  loaders_observer().WaitForTotalOnReceiveResponseProcessed(1);
}

// Chrome browser tests to cover variation header-related behaviors for fetch
// keepalive requests.
class ChromeKeepAliveURLVariationBrowserTest
    : public ChromeKeepAliveURLBrowserTestBase,
      public ::testing::WithParamInterface<std::string> {
 protected:
  const std::string& method() const { return GetParam(); }
};

INSTANTIATE_TEST_SUITE_P(
    All,
    ChromeKeepAliveURLVariationBrowserTest,
    ::testing::Values(net::HttpRequestHeaders::kGetMethod,
                      net::HttpRequestHeaders::kPostMethod),
    [](const testing::TestParamInfo<
        ChromeKeepAliveURLVariationBrowserTest::ParamType>& info) {
      return info.param;
    });

// Verifies that the variations header (X-Client-Data) is attached to network
// requests to Google, but stripped on redirects to non-Google.
IN_PROC_BROWSER_TEST_P(ChromeKeepAliveURLVariationBrowserTest,
                       ReceiveRedirectToGoogleAfterPageUnloadAndStripHeaders) {
  const std::string target_url = kKeepAliveEndpoint;
  SetUseHttps();
  auto request_handlers =
      RegisterRequestHandlers({target_url, "/redirect", "/final"});
  ASSERT_TRUE(server()->Start());

  // Set up redirects according to the following redirect chain:
  // fetch("https://www.google.com:<port>/beacon", keepalive: true)
  // --> https://www.google.com:<port>/redirect
  // --> https://www.b.com:<port>/final
  LoadPageWithKeepAliveRequestAndSendResponseAfterUnload(
      GetKeepAlivePageURL(kGoogleHost, target_url, method()),
      request_handlers[0].get(),
      ("HTTP/1.1 301 Moved Permanently\r\n"
       "Access-Control-Allow-Origin: *\r\n"
       "Location: /redirect\r\n"
       "\r\n"));

  // The 1st redirect should be processed in browser.
  loaders_observer().WaitForTotalOnReceiveRedirectProcessed(1);
  // This redirect request to Google should contain variation header.
  request_handlers[1]->WaitForRequest();
  EXPECT_THAT(request_handlers[1]->http_request()->headers,
              testing::Contains(testing::Key(variations::kClientDataHeader)));

  // The 2nd redirect request should contain no variation header.
  request_handlers[1]->Send(
      "HTTP/1.1 301 Moved Permanently\r\n"
      "Access-Control-Allow-Origin: *\r\n"
      "Location: https://b.com/final\r\n"
      "\r\n");
  request_handlers[1]->Done();
  loaders_observer().WaitForTotalOnReceiveRedirectProcessed(2);
  // The request is dropped by network service, so no way to verify variation
  // header from `request_handlers[2]`.
}