Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 5 | #include <cctype> |
| 6 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 7 | #include "base/base_switches.h" |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 8 | #include "base/bind.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 9 | #include "base/command_line.h" |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 10 | #include "base/metrics/field_trial_param_associator.h" |
Gabriel Charette | b71eec89 | 2017-09-14 22:52:56 | [diff] [blame] | 11 | #include "base/run_loop.h" |
Tarun Bansal | 6bf5430 | 2017-10-02 07:39:14 | [diff] [blame] | 12 | #include "base/stl_util.h" |
Mike West | e555be86 | 2019-02-20 16:17:30 | [diff] [blame] | 13 | #include "base/strings/string_util.h" |
Callum May | 7d88ff3e | 2019-11-12 18:21:46 | [diff] [blame] | 14 | #include "base/synchronization/lock.h" |
Devlin Cronin | 626d80c | 2018-06-01 01:08:36 | [diff] [blame] | 15 | #include "base/test/metrics/histogram_tester.h" |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 16 | #include "build/build_config.h" |
Mike West | e555be86 | 2019-02-20 16:17:30 | [diff] [blame] | 17 | #include "chrome/browser/chrome_content_browser_client.h" |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 18 | #include "chrome/browser/content_settings/cookie_settings_factory.h" |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 19 | #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
Aaron Tagliaboschi | 8f3a2830 | 2020-06-24 03:08:26 | [diff] [blame] | 20 | #include "chrome/browser/policy/policy_test_utils.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 21 | #include "chrome/browser/profiles/profile.h" |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 22 | #include "chrome/browser/ui/browser.h" |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 23 | #include "chrome/browser/ui/browser_commands.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 24 | #include "chrome/test/base/in_process_browser_test.h" |
| 25 | #include "chrome/test/base/ui_test_utils.h" |
Carlos Caballero | b428320 | 2020-08-10 14:40:46 | [diff] [blame^] | 26 | #include "components/content_settings/browser/page_specific_content_settings.h" |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 27 | #include "components/content_settings/core/browser/cookie_settings.h" |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 28 | #include "components/content_settings/core/browser/host_content_settings_map.h" |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 29 | #include "components/content_settings/core/common/pref_names.h" |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 30 | #include "components/metrics/content/subprocess_metrics_provider.h" |
Aaron Tagliaboschi | 8f3a2830 | 2020-06-24 03:08:26 | [diff] [blame] | 31 | #include "components/policy/core/common/policy_map.h" |
| 32 | #include "components/policy/core/common/policy_pref_names.h" |
| 33 | #include "components/policy/policy_constants.h" |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 34 | #include "components/prefs/pref_service.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 35 | #include "content/public/browser/browser_thread.h" |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 36 | #include "content/public/browser/navigation_entry.h" |
Changwan Ryu | 434c3a3 | 2019-07-30 23:42:58 | [diff] [blame] | 37 | #include "content/public/browser/render_view_host.h" |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 38 | #include "content/public/common/content_features.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 39 | #include "content/public/common/content_switches.h" |
Changwan Ryu | 434c3a3 | 2019-07-30 23:42:58 | [diff] [blame] | 40 | #include "content/public/common/web_preferences.h" |
Peter Kasting | 919ce65 | 2020-05-07 10:22:36 | [diff] [blame] | 41 | #include "content/public/test/browser_test.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 42 | #include "content/public/test/browser_test_utils.h" |
| 43 | #include "content/public/test/test_utils.h" |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 44 | #include "content/public/test/url_loader_interceptor.h" |
| 45 | #include "net/dns/mock_host_resolver.h" |
| 46 | #include "net/http/http_request_headers.h" |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 47 | #include "net/nqe/effective_connection_type.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 48 | #include "net/test/embedded_test_server/embedded_test_server.h" |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 49 | #include "net/test/embedded_test_server/http_request.h" |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 50 | #include "net/test/embedded_test_server/http_response.h" |
Tarun Bansal | 74e189d | 2018-05-07 19:07:35 | [diff] [blame] | 51 | #include "services/network/public/cpp/cors/cors.h" |
Tarun Bansal | ceab959 | 2018-05-01 18:57:35 | [diff] [blame] | 52 | #include "services/network/public/cpp/features.h" |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 53 | #include "services/network/public/cpp/network_switches.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 54 | #include "third_party/blink/public/common/client_hints/client_hints.h" |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 55 | |
| 56 | namespace { |
| 57 | |
Yoav Weiss | fd1d19f | 2020-05-05 09:23:03 | [diff] [blame] | 58 | const unsigned expected_client_hints_number = 12u; |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 59 | const int32_t uma_histogram_max_value = 1471228928; |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 60 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 61 | // An interceptor that records count of fetches and client hint headers for |
| 62 | // requests to https://foo.com/non-existing-image.jpg. |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 63 | class ThirdPartyURLLoaderInterceptor { |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 64 | public: |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 65 | explicit ThirdPartyURLLoaderInterceptor(const GURL intercepted_url) |
| 66 | : intercepted_url_(intercepted_url), |
| 67 | interceptor_(base::BindRepeating( |
| 68 | &ThirdPartyURLLoaderInterceptor::InterceptURLRequest, |
| 69 | base::Unretained(this))) {} |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 70 | |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 71 | ~ThirdPartyURLLoaderInterceptor() = default; |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 72 | |
| 73 | size_t request_count_seen() const { return request_count_seen_; } |
| 74 | |
| 75 | size_t client_hints_count_seen() const { return client_hints_count_seen_; } |
| 76 | |
| 77 | private: |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 78 | bool InterceptURLRequest( |
| 79 | content::URLLoaderInterceptor::RequestParams* params) { |
| 80 | if (params->url_request.url != intercepted_url_) |
| 81 | return false; |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 82 | |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 83 | request_count_seen_++; |
Mike West | 14c1110 | 2019-02-04 16:16:47 | [diff] [blame] | 84 | for (size_t i = 0; i < blink::kClientHintsMappingsCount; ++i) { |
Tarun Bansal | f9cf989 | 2018-04-06 04:38:01 | [diff] [blame] | 85 | if (params->url_request.headers.HasHeader( |
| 86 | blink::kClientHintsHeaderMapping[i])) { |
| 87 | client_hints_count_seen_++; |
| 88 | } |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 89 | } |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 90 | return false; |
| 91 | } |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 92 | |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 93 | GURL intercepted_url_; |
| 94 | |
| 95 | size_t request_count_seen_ = 0u; |
| 96 | |
| 97 | size_t client_hints_count_seen_ = 0u; |
| 98 | |
| 99 | content::URLLoaderInterceptor interceptor_; |
| 100 | |
| 101 | DISALLOW_COPY_AND_ASSIGN(ThirdPartyURLLoaderInterceptor); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 102 | }; |
| 103 | |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 104 | // Returns true only if |header_value| satisfies ABNF: 1*DIGIT [ "." 1*DIGIT ] |
| 105 | bool IsSimilarToDoubleABNF(const std::string& header_value) { |
| 106 | if (header_value.empty()) |
| 107 | return false; |
| 108 | char first_char = header_value.at(0); |
| 109 | if (!isdigit(first_char)) |
| 110 | return false; |
| 111 | |
| 112 | bool period_found = false; |
| 113 | bool digit_found_after_period = false; |
| 114 | for (char ch : header_value) { |
| 115 | if (isdigit(ch)) { |
| 116 | if (period_found) { |
| 117 | digit_found_after_period = true; |
| 118 | } |
| 119 | continue; |
| 120 | } |
| 121 | if (ch == '.') { |
| 122 | if (period_found) |
| 123 | return false; |
| 124 | period_found = true; |
| 125 | continue; |
| 126 | } |
| 127 | return false; |
| 128 | } |
| 129 | if (period_found) |
| 130 | return digit_found_after_period; |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | // Returns true only if |header_value| satisfies ABNF: 1*DIGIT |
| 135 | bool IsSimilarToIntABNF(const std::string& header_value) { |
| 136 | if (header_value.empty()) |
| 137 | return false; |
| 138 | |
| 139 | for (char ch : header_value) { |
| 140 | if (!isdigit(ch)) |
| 141 | return false; |
| 142 | } |
| 143 | return true; |
| 144 | } |
| 145 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 146 | } // namespace |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 147 | |
Aaron Tagliaboschi | 8f3a2830 | 2020-06-24 03:08:26 | [diff] [blame] | 148 | class ClientHintsBrowserTest : public policy::PolicyTest, |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 149 | public testing::WithParamInterface<bool> { |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 150 | public: |
| 151 | ClientHintsBrowserTest() |
Mikel Astiz | 2de748d | 2019-11-16 10:39:36 | [diff] [blame] | 152 | : http_server_(net::EmbeddedTestServer::TYPE_HTTP), |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 153 | https_server_(net::EmbeddedTestServer::TYPE_HTTPS), |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 154 | https_cross_origin_server_(net::EmbeddedTestServer::TYPE_HTTPS), |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 155 | expect_client_hints_on_main_frame_(false), |
| 156 | expect_client_hints_on_subresources_(false), |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 157 | count_user_agent_hint_headers_seen_(0), |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 158 | count_ua_mobile_client_hints_headers_seen_(0), |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 159 | count_client_hints_headers_seen_(0), |
| 160 | request_interceptor_(nullptr) { |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 161 | http_server_.ServeFilesFromSourceDirectory("chrome/test/data/client_hints"); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 162 | https_server_.ServeFilesFromSourceDirectory( |
| 163 | "chrome/test/data/client_hints"); |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 164 | https_cross_origin_server_.ServeFilesFromSourceDirectory( |
| 165 | "chrome/test/data/client_hints"); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 166 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 167 | http_server_.RegisterRequestMonitor( |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 168 | base::BindRepeating(&ClientHintsBrowserTest::MonitorResourceRequest, |
| 169 | base::Unretained(this))); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 170 | https_server_.RegisterRequestMonitor( |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 171 | base::BindRepeating(&ClientHintsBrowserTest::MonitorResourceRequest, |
| 172 | base::Unretained(this))); |
| 173 | https_cross_origin_server_.RegisterRequestMonitor( |
| 174 | base::BindRepeating(&ClientHintsBrowserTest::MonitorResourceRequest, |
| 175 | base::Unretained(this))); |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 176 | https_cross_origin_server_.RegisterRequestHandler( |
| 177 | base::BindRepeating(&ClientHintsBrowserTest::RequestHandlerToRedirect, |
| 178 | base::Unretained(this))); |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 179 | https_server_.RegisterRequestHandler(base::BindRepeating( |
| 180 | &ClientHintsBrowserTest::RequestHandlerToFetchCrossOriginIframe, |
| 181 | base::Unretained(this))); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 182 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 183 | EXPECT_TRUE(http_server_.Start()); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 184 | EXPECT_TRUE(https_server_.Start()); |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 185 | EXPECT_TRUE(https_cross_origin_server_.Start()); |
| 186 | |
| 187 | EXPECT_NE(https_server_.base_url(), https_cross_origin_server_.base_url()); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 188 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 189 | accept_ch_with_lifetime_http_local_url_ = |
| 190 | http_server_.GetURL("/accept_ch_with_lifetime.html"); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 191 | http_equiv_accept_ch_with_lifetime_http_local_url_ = |
| 192 | http_server_.GetURL("/http_equiv_accept_ch_with_lifetime.html"); |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 193 | EXPECT_TRUE(accept_ch_with_lifetime_http_local_url_.SchemeIsHTTPOrHTTPS()); |
| 194 | EXPECT_FALSE( |
| 195 | accept_ch_with_lifetime_http_local_url_.SchemeIsCryptographic()); |
| 196 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 197 | accept_ch_with_lifetime_url_ = |
| 198 | https_server_.GetURL("/accept_ch_with_lifetime.html"); |
Tarun Bansal | 73502f9 | 2019-04-16 21:21:19 | [diff] [blame] | 199 | accept_ch_with_short_lifetime_url_ = |
| 200 | https_server_.GetURL("/accept_ch_with_short_lifetime.html"); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 201 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 202 | accept_ch_without_lifetime_url_ = |
| 203 | https_server_.GetURL("/accept_ch_without_lifetime.html"); |
| 204 | EXPECT_TRUE(accept_ch_with_lifetime_url_.SchemeIsHTTPOrHTTPS()); |
| 205 | EXPECT_TRUE(accept_ch_with_lifetime_url_.SchemeIsCryptographic()); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 206 | http_equiv_accept_ch_without_lifetime_url_ = |
| 207 | https_server_.GetURL("/http_equiv_accept_ch_without_lifetime.html"); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 208 | |
| 209 | without_accept_ch_without_lifetime_url_ = |
| 210 | https_server_.GetURL("/without_accept_ch_without_lifetime.html"); |
| 211 | EXPECT_TRUE(without_accept_ch_without_lifetime_url_.SchemeIsHTTPOrHTTPS()); |
| 212 | EXPECT_TRUE( |
| 213 | without_accept_ch_without_lifetime_url_.SchemeIsCryptographic()); |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 214 | |
| 215 | without_accept_ch_without_lifetime_local_url_ = |
| 216 | http_server_.GetURL("/without_accept_ch_without_lifetime.html"); |
| 217 | EXPECT_TRUE( |
| 218 | without_accept_ch_without_lifetime_local_url_.SchemeIsHTTPOrHTTPS()); |
| 219 | EXPECT_FALSE( |
| 220 | without_accept_ch_without_lifetime_local_url_.SchemeIsCryptographic()); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 221 | |
| 222 | without_accept_ch_without_lifetime_img_localhost_ = https_server_.GetURL( |
| 223 | "/without_accept_ch_without_lifetime_img_localhost.html"); |
| 224 | without_accept_ch_without_lifetime_img_foo_com_ = https_server_.GetURL( |
| 225 | "/without_accept_ch_without_lifetime_img_foo_com.html"); |
| 226 | accept_ch_without_lifetime_with_iframe_url_ = |
| 227 | https_server_.GetURL("/accept_ch_without_lifetime_with_iframe.html"); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 228 | http_equiv_accept_ch_without_lifetime_with_iframe_url_ = |
| 229 | https_server_.GetURL( |
| 230 | "/http_equiv_accept_ch_without_lifetime_with_iframe.html"); |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 231 | accept_ch_without_lifetime_with_subresource_url_ = https_server_.GetURL( |
| 232 | "/accept_ch_without_lifetime_with_subresource.html"); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 233 | http_equiv_accept_ch_without_lifetime_with_subresource_url_ = |
| 234 | https_server_.GetURL( |
| 235 | "/http_equiv_accept_ch_without_lifetime_with_subresource.html"); |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 236 | accept_ch_without_lifetime_with_subresource_iframe_url_ = |
| 237 | https_server_.GetURL( |
| 238 | "/accept_ch_without_lifetime_with_subresource_iframe.html"); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 239 | http_equiv_accept_ch_without_lifetime_with_subresource_iframe_url_ = |
| 240 | https_server_.GetURL( |
| 241 | "/http_equiv_accept_ch_without_lifetime_with_subresource_iframe." |
| 242 | "html"); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 243 | accept_ch_without_lifetime_img_localhost_ = |
| 244 | https_server_.GetURL("/accept_ch_without_lifetime_img_localhost.html"); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 245 | http_equiv_accept_ch_without_lifetime_img_localhost_ = https_server_.GetURL( |
| 246 | "/http_equiv_accept_ch_without_lifetime_img_localhost.html"); |
| 247 | http_equiv_accept_ch_with_lifetime_ = |
| 248 | https_server_.GetURL("/http_equiv_accept_ch_with_lifetime.html"); |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 249 | |
| 250 | redirect_url_ = https_cross_origin_server_.GetURL("/redirect.html"); |
Maks Orlovich | 7a588d8 | 2020-05-06 15:17:24 | [diff] [blame] | 251 | |
| 252 | accept_ch_empty_ = https_server_.GetURL("/accept_ch_empty.html"); |
| 253 | http_equiv_accept_ch_merge_ = |
| 254 | https_server_.GetURL("/http_equiv_accept_ch_merge.html"); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | ~ClientHintsBrowserTest() override {} |
| 258 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 259 | virtual std::unique_ptr<base::FeatureList> EnabledFeatures() { |
| 260 | std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
Maks Orlovich | c66745a | 2020-06-30 17:40:02 | [diff] [blame] | 261 | feature_list->InitializeFromCommandLine( |
| 262 | "UserAgentClientHint,LangClientHintHeader", ""); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 263 | return feature_list; |
| 264 | } |
| 265 | |
| 266 | void SetUp() override { |
Maksim Sisov (GMT+3) | 830244f | 2020-08-10 12:52:24 | [diff] [blame] | 267 | scoped_feature_list_.InitWithFeatureList(EnabledFeatures()); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 268 | InProcessBrowserTest::SetUp(); |
| 269 | } |
| 270 | |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 271 | void SetUpOnMainThread() override { |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 272 | host_resolver()->AddRule("*", "127.0.0.1"); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 273 | |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 274 | request_interceptor_ = std::make_unique<ThirdPartyURLLoaderInterceptor>( |
| 275 | GURL("https://foo.com/non-existing-image.jpg")); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 276 | base::RunLoop().RunUntilIdle(); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 277 | } |
| 278 | |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 279 | void TearDownOnMainThread() override { request_interceptor_.reset(); } |
| 280 | |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 281 | void SetUpCommandLine(base::CommandLine* cmd) override { |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 282 | cmd->AppendSwitchASCII(network::switches::kForceEffectiveConnectionType, |
| 283 | net::kEffectiveConnectionType2G); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 284 | } |
| 285 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 286 | void SetClientHintExpectationsOnMainFrame(bool expect_client_hints) { |
| 287 | expect_client_hints_on_main_frame_ = expect_client_hints; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 288 | } |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 289 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 290 | void SetClientHintExpectationsOnSubresources(bool expect_client_hints) { |
Callum May | 1d93974 | 2020-03-02 17:51:30 | [diff] [blame] | 291 | base::AutoLock lock(expect_client_hints_on_subresources_lock_); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 292 | expect_client_hints_on_subresources_ = expect_client_hints; |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 293 | } |
| 294 | |
Callum May | 1d93974 | 2020-03-02 17:51:30 | [diff] [blame] | 295 | bool expect_client_hints_on_subresources() { |
| 296 | base::AutoLock lock(expect_client_hints_on_subresources_lock_); |
| 297 | return expect_client_hints_on_subresources_; |
| 298 | } |
| 299 | |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 300 | // Verify that the user is not notified that cookies or JavaScript were |
| 301 | // blocked on the webpage due to the checks done by client hints. |
| 302 | void VerifyContentSettingsNotNotified() const { |
Carlos Caballero | b428320 | 2020-08-10 14:40:46 | [diff] [blame^] | 303 | auto* pscs = content_settings::PageSpecificContentSettings::GetForFrame( |
Carlos Caballero | d7b75941 | 2020-07-31 18:00:08 | [diff] [blame] | 304 | browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()); |
Carlos Caballero | b428320 | 2020-08-10 14:40:46 | [diff] [blame^] | 305 | EXPECT_FALSE(pscs->IsContentBlocked(ContentSettingsType::COOKIES)); |
| 306 | EXPECT_FALSE(pscs->IsContentBlocked(ContentSettingsType::JAVASCRIPT)); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 307 | } |
| 308 | |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 309 | void SetExpectedEffectiveConnectionType( |
| 310 | net::EffectiveConnectionType effective_connection_type) { |
| 311 | expected_ect = effective_connection_type; |
| 312 | } |
| 313 | |
Changwan Ryu | 434c3a3 | 2019-07-30 23:42:58 | [diff] [blame] | 314 | void SetJsEnabledForActiveView(bool enabled) { |
Rakina Zata Amni | 347b7090 | 2020-07-22 10:49:04 | [diff] [blame] | 315 | content::WebContents* web_contents = |
| 316 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 317 | content::WebPreferences prefs = web_contents->GetOrCreateWebPreferences(); |
Changwan Ryu | 434c3a3 | 2019-07-30 23:42:58 | [diff] [blame] | 318 | prefs.javascript_enabled = enabled; |
Rakina Zata Amni | 347b7090 | 2020-07-22 10:49:04 | [diff] [blame] | 319 | web_contents->SetWebPreferences(prefs); |
Changwan Ryu | 434c3a3 | 2019-07-30 23:42:58 | [diff] [blame] | 320 | } |
| 321 | |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 322 | void TestProfilesIndependent(Browser* browser_a, Browser* browser_b); |
| 323 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 324 | const GURL& accept_ch_with_lifetime_http_local_url() const { |
| 325 | return accept_ch_with_lifetime_http_local_url_; |
| 326 | } |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 327 | const GURL& http_equiv_accept_ch_with_lifetime_http_local_url() const { |
| 328 | return http_equiv_accept_ch_with_lifetime_http_local_url_; |
| 329 | } |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 330 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 331 | // A URL whose response headers include Accept-CH and Accept-CH-Lifetime |
| 332 | // headers. |
| 333 | const GURL& accept_ch_with_lifetime_url() const { |
| 334 | return accept_ch_with_lifetime_url_; |
| 335 | } |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 336 | const GURL& http_equiv_accept_ch_with_lifetime() { |
| 337 | return http_equiv_accept_ch_with_lifetime_; |
| 338 | } |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 339 | |
Tarun Bansal | 73502f9 | 2019-04-16 21:21:19 | [diff] [blame] | 340 | // A URL whose response headers include Accept-CH and Accept-CH-Lifetime |
| 341 | // headers. The Accept-CH-Lifetime duration is set very short to 1 second. |
| 342 | const GURL& accept_ch_with_short_lifetime() const { |
| 343 | return accept_ch_with_short_lifetime_url_; |
| 344 | } |
| 345 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 346 | // A URL whose response headers include only Accept-CH header. |
| 347 | const GURL& accept_ch_without_lifetime_url() const { |
| 348 | return accept_ch_without_lifetime_url_; |
| 349 | } |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 350 | const GURL& http_equiv_accept_ch_without_lifetime_url() const { |
| 351 | return http_equiv_accept_ch_without_lifetime_url_; |
| 352 | } |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 353 | |
| 354 | // A URL whose response headers do not include either Accept-CH or |
| 355 | // Accept-CH-Lifetime headers. Navigating to this URL also fetches an image. |
| 356 | const GURL& without_accept_ch_without_lifetime_url() const { |
| 357 | return without_accept_ch_without_lifetime_url_; |
| 358 | } |
| 359 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 360 | // A URL whose response headers do not include either Accept-CH or |
| 361 | // Accept-CH-Lifetime headers. Navigating to this URL also fetches an image. |
| 362 | const GURL& without_accept_ch_without_lifetime_local_url() const { |
| 363 | return without_accept_ch_without_lifetime_local_url_; |
| 364 | } |
| 365 | |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 366 | // A URL whose response headers do not include either Accept-CH or |
| 367 | // Accept-CH-Lifetime headers. Navigating to this URL also fetches an image |
| 368 | // from localhost. |
| 369 | const GURL& without_accept_ch_without_lifetime_img_localhost() const { |
| 370 | return without_accept_ch_without_lifetime_img_localhost_; |
| 371 | } |
| 372 | |
| 373 | // A URL whose response headers do not include either Accept-CH or |
| 374 | // Accept-CH-Lifetime headers. Navigating to this URL also fetches an image |
| 375 | // from foo.com. |
| 376 | const GURL& without_accept_ch_without_lifetime_img_foo_com() const { |
| 377 | return without_accept_ch_without_lifetime_img_foo_com_; |
| 378 | } |
| 379 | |
| 380 | // A URL whose response does not include Accept-CH or Accept-CH-Lifetime |
| 381 | // headers. The response loads accept_ch_with_lifetime_url() in an iframe. |
| 382 | const GURL& accept_ch_without_lifetime_with_iframe_url() const { |
| 383 | return accept_ch_without_lifetime_with_iframe_url_; |
| 384 | } |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 385 | const GURL& http_equiv_accept_ch_without_lifetime_with_iframe_url() const { |
| 386 | return http_equiv_accept_ch_without_lifetime_with_iframe_url_; |
| 387 | } |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 388 | |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 389 | // A URL whose response does not include Accept-CH or Accept-CH-Lifetime |
| 390 | // headers. The response loads accept_ch_with_lifetime_url() as a subresource |
| 391 | // in the main frame. |
| 392 | const GURL& accept_ch_without_lifetime_with_subresource_url() const { |
| 393 | return accept_ch_without_lifetime_with_subresource_url_; |
| 394 | } |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 395 | const GURL& http_equiv_accept_ch_without_lifetime_with_subresource_url() |
| 396 | const { |
| 397 | return http_equiv_accept_ch_without_lifetime_with_subresource_url_; |
| 398 | } |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 399 | |
| 400 | // A URL whose response does not include Accept-CH or Accept-CH-Lifetime |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 401 | // headers. The response loads accept_ch_with_lifetime_url() or |
| 402 | // http_equiv_accept_ch_with_lifetime_url() as a subresource in the iframe. |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 403 | const GURL& accept_ch_without_lifetime_with_subresource_iframe_url() const { |
| 404 | return accept_ch_without_lifetime_with_subresource_iframe_url_; |
| 405 | } |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 406 | const GURL& |
| 407 | http_equiv_accept_ch_without_lifetime_with_subresource_iframe_url() const { |
| 408 | return http_equiv_accept_ch_without_lifetime_with_subresource_iframe_url_; |
| 409 | } |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 410 | |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 411 | // A URL whose response includes only Accept-CH header. Navigating to |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 412 | // this URL also fetches two images: One from the localhost, and one from |
| 413 | // foo.com. |
| 414 | const GURL& accept_ch_without_lifetime_img_localhost() const { |
| 415 | return accept_ch_without_lifetime_img_localhost_; |
| 416 | } |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 417 | const GURL& http_equiv_accept_ch_without_lifetime_img_localhost() const { |
| 418 | return http_equiv_accept_ch_without_lifetime_img_localhost_; |
| 419 | } |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 420 | |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 421 | const GURL& redirect_url() const { return redirect_url_; } |
| 422 | |
Maks Orlovich | 7a588d8 | 2020-05-06 15:17:24 | [diff] [blame] | 423 | // A URL to a page with a response containing an empty accept_ch header. |
| 424 | const GURL& accept_ch_empty() const { return accept_ch_empty_; } |
| 425 | |
| 426 | // A page where some hints are in accept-ch header, some in http-equiv. |
| 427 | const GURL& http_equiv_accept_ch_merge() const { |
| 428 | return http_equiv_accept_ch_merge_; |
| 429 | } |
| 430 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 431 | size_t count_user_agent_hint_headers_seen() const { |
Callum May | 7d88ff3e | 2019-11-12 18:21:46 | [diff] [blame] | 432 | base::AutoLock lock(count_headers_lock_); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 433 | return count_user_agent_hint_headers_seen_; |
| 434 | } |
| 435 | |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 436 | size_t count_ua_mobile_client_hints_headers_seen() const { |
| 437 | base::AutoLock lock(count_headers_lock_); |
| 438 | return count_ua_mobile_client_hints_headers_seen_; |
| 439 | } |
| 440 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 441 | size_t count_client_hints_headers_seen() const { |
Callum May | 7d88ff3e | 2019-11-12 18:21:46 | [diff] [blame] | 442 | base::AutoLock lock(count_headers_lock_); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 443 | return count_client_hints_headers_seen_; |
| 444 | } |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 445 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 446 | size_t third_party_request_count_seen() const { |
| 447 | return request_interceptor_->request_count_seen(); |
| 448 | } |
| 449 | |
| 450 | size_t third_party_client_hints_count_seen() const { |
| 451 | return request_interceptor_->client_hints_count_seen(); |
| 452 | } |
| 453 | |
Mike West | e555be86 | 2019-02-20 16:17:30 | [diff] [blame] | 454 | const std::string& main_frame_ua_observed() const { |
| 455 | return main_frame_ua_observed_; |
| 456 | } |
| 457 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 458 | const std::string& main_frame_ua_full_version_observed() const { |
| 459 | return main_frame_ua_full_version_observed_; |
| 460 | } |
| 461 | |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 462 | const std::string& main_frame_ua_mobile_observed() const { |
| 463 | return main_frame_ua_mobile_observed_; |
| 464 | } |
| 465 | |
| 466 | const std::string& main_frame_ua_platform_observed() const { |
| 467 | return main_frame_ua_platform_observed_; |
| 468 | } |
| 469 | |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 470 | base::test::ScopedFeatureList scoped_feature_list_; |
| 471 | |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 472 | std::string intercept_iframe_resource_; |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 473 | bool intercept_to_http_equiv_iframe_ = false; |
Callum May | 7d88ff3e | 2019-11-12 18:21:46 | [diff] [blame] | 474 | mutable base::Lock count_headers_lock_; |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 475 | |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 476 | private: |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 477 | // Intercepts only the main frame requests that contain |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 478 | // "redirect" in the resource path. The intercepted requests |
| 479 | // are served an HTML file that fetches an iframe from a cross-origin HTTPS |
| 480 | // server. |
| 481 | std::unique_ptr<net::test_server::HttpResponse> RequestHandlerToRedirect( |
| 482 | const net::test_server::HttpRequest& request) { |
| 483 | // Check if it's a main frame request. |
| 484 | if (request.relative_url.find(".html") == std::string::npos) |
| 485 | return nullptr; |
| 486 | |
| 487 | if (request.GetURL().spec().find("redirect") == std::string::npos) |
| 488 | return nullptr; |
| 489 | |
| 490 | std::unique_ptr<net::test_server::BasicHttpResponse> response; |
| 491 | response.reset(new net::test_server::BasicHttpResponse); |
| 492 | response->set_code(net::HTTP_FOUND); |
| 493 | response->AddCustomHeader("Location", |
| 494 | without_accept_ch_without_lifetime_url().spec()); |
| 495 | return std::move(response); |
| 496 | } |
| 497 | |
| 498 | // Intercepts only the main frame requests that contain |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 499 | // |intercept_iframe_resource_| in the resource path. The intercepted requests |
| 500 | // are served an HTML file that fetches an iframe from a cross-origin HTTPS |
| 501 | // server. |
| 502 | std::unique_ptr<net::test_server::HttpResponse> |
| 503 | RequestHandlerToFetchCrossOriginIframe( |
| 504 | const net::test_server::HttpRequest& request) { |
| 505 | if (intercept_iframe_resource_.empty()) |
| 506 | return nullptr; |
| 507 | |
| 508 | // Check if it's a main frame request. |
| 509 | if (request.relative_url.find(".html") == std::string::npos) |
| 510 | return nullptr; |
| 511 | |
| 512 | if (request.relative_url.find(intercept_iframe_resource_) == |
| 513 | std::string::npos) { |
| 514 | return nullptr; |
| 515 | } |
| 516 | |
| 517 | const std::string iframe_url = |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 518 | intercept_to_http_equiv_iframe_ |
| 519 | ? https_cross_origin_server_ |
| 520 | .GetURL("/http_equiv_accept_ch_with_lifetime.html") |
| 521 | .spec() |
| 522 | : https_cross_origin_server_.GetURL("/accept_ch_with_lifetime.html") |
| 523 | .spec(); |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 524 | |
| 525 | std::unique_ptr<net::test_server::BasicHttpResponse> http_response( |
| 526 | new net::test_server::BasicHttpResponse()); |
| 527 | http_response->set_code(net::HTTP_OK); |
| 528 | http_response->set_content_type("text/html"); |
| 529 | http_response->set_content( |
| 530 | "<html>" |
| 531 | "<link rel='icon' href='data:;base64,='><head></head>" |
| 532 | "Empty file which uses link-rel to disable favicon fetches. " |
| 533 | "<iframe src='" + |
| 534 | iframe_url + "'></iframe></html>"); |
| 535 | |
| 536 | return std::move(http_response); |
| 537 | } |
| 538 | |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 539 | static std::string UpdateHeaderObservation( |
| 540 | const net::test_server::HttpRequest& request, |
| 541 | const std::string& header) { |
| 542 | if (request.headers.find(header) != request.headers.end()) |
| 543 | return request.headers.find(header)->second; |
| 544 | else |
| 545 | return ""; |
| 546 | } |
| 547 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 548 | // Called by |https_server_|. |
| 549 | void MonitorResourceRequest(const net::test_server::HttpRequest& request) { |
Tarun Bansal | 6bf5430 | 2017-10-02 07:39:14 | [diff] [blame] | 550 | bool is_main_frame_navigation = |
| 551 | request.GetURL().spec().find(".html") != std::string::npos; |
| 552 | |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 553 | if (is_main_frame_navigation && |
| 554 | request.GetURL().spec().find("redirect") != std::string::npos) { |
| 555 | return; |
| 556 | } |
| 557 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 558 | if (is_main_frame_navigation) { |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 559 | main_frame_ua_observed_ = UpdateHeaderObservation(request, "sec-ch-ua"); |
| 560 | main_frame_ua_full_version_observed_ = |
| 561 | UpdateHeaderObservation(request, "sec-ch-ua-full-version"); |
| 562 | main_frame_ua_mobile_observed_ = |
| 563 | UpdateHeaderObservation(request, "sec-ch-ua-mobile"); |
| 564 | main_frame_ua_platform_observed_ = |
| 565 | UpdateHeaderObservation(request, "sec-ch-ua-platform"); |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 566 | |
Tarun Bansal | f9cf989 | 2018-04-06 04:38:01 | [diff] [blame] | 567 | VerifyClientHintsReceived(expect_client_hints_on_main_frame_, request); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 568 | if (expect_client_hints_on_main_frame_) { |
| 569 | double value = 0.0; |
| 570 | EXPECT_TRUE(base::StringToDouble( |
| 571 | request.headers.find("device-memory")->second, &value)); |
| 572 | EXPECT_LT(0.0, value); |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 573 | EXPECT_TRUE(IsSimilarToDoubleABNF( |
| 574 | request.headers.find("device-memory")->second)); |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 575 | main_frame_device_memory_observed_ = value; |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 576 | |
| 577 | EXPECT_TRUE( |
| 578 | base::StringToDouble(request.headers.find("dpr")->second, &value)); |
| 579 | EXPECT_LT(0.0, value); |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 580 | EXPECT_TRUE(IsSimilarToDoubleABNF(request.headers.find("dpr")->second)); |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 581 | main_frame_dpr_observed_ = value; |
| 582 | |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 583 | EXPECT_TRUE(base::StringToDouble( |
| 584 | request.headers.find("viewport-width")->second, &value)); |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 585 | EXPECT_TRUE( |
| 586 | IsSimilarToIntABNF(request.headers.find("viewport-width")->second)); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 587 | #if !defined(OS_ANDROID) |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 588 | EXPECT_LT(0.0, value); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 589 | #else |
| 590 | EXPECT_EQ(980, value); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 591 | #endif |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 592 | main_frame_viewport_width_observed_ = value; |
Mike West | e555be86 | 2019-02-20 16:17:30 | [diff] [blame] | 593 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 594 | VerifyNetworkQualityClientHints(request); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 595 | } |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 596 | } |
Tarun Bansal | 6bf5430 | 2017-10-02 07:39:14 | [diff] [blame] | 597 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 598 | if (!is_main_frame_navigation) { |
Callum May | 1d93974 | 2020-03-02 17:51:30 | [diff] [blame] | 599 | VerifyClientHintsReceived(expect_client_hints_on_subresources(), request); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 600 | |
Callum May | 1d93974 | 2020-03-02 17:51:30 | [diff] [blame] | 601 | if (expect_client_hints_on_subresources()) { |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 602 | double value = 0.0; |
| 603 | EXPECT_TRUE(base::StringToDouble( |
| 604 | request.headers.find("device-memory")->second, &value)); |
| 605 | EXPECT_LT(0.0, value); |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 606 | EXPECT_TRUE(IsSimilarToDoubleABNF( |
| 607 | request.headers.find("device-memory")->second)); |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 608 | if (main_frame_device_memory_observed_ > 0) { |
| 609 | EXPECT_EQ(main_frame_device_memory_observed_, value); |
| 610 | } |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 611 | |
| 612 | EXPECT_TRUE( |
| 613 | base::StringToDouble(request.headers.find("dpr")->second, &value)); |
| 614 | EXPECT_LT(0.0, value); |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 615 | EXPECT_TRUE(IsSimilarToDoubleABNF(request.headers.find("dpr")->second)); |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 616 | if (main_frame_dpr_observed_ > 0) { |
| 617 | EXPECT_EQ(main_frame_dpr_observed_, value); |
| 618 | } |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 619 | |
| 620 | EXPECT_TRUE(base::StringToDouble( |
| 621 | request.headers.find("viewport-width")->second, &value)); |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 622 | EXPECT_TRUE( |
| 623 | IsSimilarToIntABNF(request.headers.find("viewport-width")->second)); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 624 | #if !defined(OS_ANDROID) |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 625 | EXPECT_LT(0.0, value); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 626 | #else |
| 627 | EXPECT_EQ(980, value); |
| 628 | #endif |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 629 | #if defined(OS_ANDROID) |
| 630 | // TODO(tbansal): https://crbug.com/825892: Viewport width on main |
| 631 | // frame requests may be incorrect when the Chrome window is not |
| 632 | // maximized. |
| 633 | if (main_frame_viewport_width_observed_ > 0) { |
| 634 | EXPECT_EQ(main_frame_viewport_width_observed_, value); |
| 635 | } |
| 636 | #endif |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 637 | VerifyNetworkQualityClientHints(request); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 638 | } |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 639 | } |
Tarun Bansal | 6bf5430 | 2017-10-02 07:39:14 | [diff] [blame] | 640 | |
Mike West | 14c1110 | 2019-02-04 16:16:47 | [diff] [blame] | 641 | for (size_t i = 0; i < blink::kClientHintsMappingsCount; ++i) { |
Jan Wilken Dörrie | a8cb5630 | 2019-06-06 18:59:36 | [diff] [blame] | 642 | if (base::Contains(request.headers, |
| 643 | blink::kClientHintsHeaderMapping[i])) { |
Callum May | 7d88ff3e | 2019-11-12 18:21:46 | [diff] [blame] | 644 | base::AutoLock lock(count_headers_lock_); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 645 | // The user agent hint is special: |
| 646 | if (std::string(blink::kClientHintsHeaderMapping[i]) == "sec-ch-ua") { |
| 647 | count_user_agent_hint_headers_seen_++; |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 648 | } else if (std::string(blink::kClientHintsHeaderMapping[i]) == |
| 649 | "sec-ch-ua-mobile") { |
| 650 | count_ua_mobile_client_hints_headers_seen_++; |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 651 | } else { |
| 652 | count_client_hints_headers_seen_++; |
| 653 | } |
Tarun Bansal | f9cf989 | 2018-04-06 04:38:01 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | } |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 657 | |
Tarun Bansal | f9cf989 | 2018-04-06 04:38:01 | [diff] [blame] | 658 | void VerifyClientHintsReceived(bool expect_client_hints, |
| 659 | const net::test_server::HttpRequest& request) { |
Mike West | 14c1110 | 2019-02-04 16:16:47 | [diff] [blame] | 660 | for (size_t i = 0; i < blink::kClientHintsMappingsCount; ++i) { |
| 661 | SCOPED_TRACE(testing::Message() |
| 662 | << std::string(blink::kClientHintsHeaderMapping[i])); |
Tarun Bansal | f9cf989 | 2018-04-06 04:38:01 | [diff] [blame] | 663 | // Resource width client hint is only attached on image subresources. |
| 664 | if (std::string(blink::kClientHintsHeaderMapping[i]) == "width") { |
| 665 | continue; |
| 666 | } |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 667 | |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 668 | // `Sec-CH-UA` and `Sec-CH-UA-Mobile` is attached on all requests. |
| 669 | if (std::string(blink::kClientHintsHeaderMapping[i]) == "sec-ch-ua" || |
| 670 | std::string(blink::kClientHintsHeaderMapping[i]) == |
| 671 | "sec-ch-ua-mobile") { |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 672 | continue; |
| 673 | } |
| 674 | |
Jan Wilken Dörrie | a8cb5630 | 2019-06-06 18:59:36 | [diff] [blame] | 675 | EXPECT_EQ( |
| 676 | expect_client_hints, |
| 677 | base::Contains(request.headers, blink::kClientHintsHeaderMapping[i])); |
Tarun Bansal | f9cf989 | 2018-04-06 04:38:01 | [diff] [blame] | 678 | } |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 679 | } |
| 680 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 681 | void VerifyNetworkQualityClientHints( |
| 682 | const net::test_server::HttpRequest& request) const { |
| 683 | // Effective connection type is forced to 2G using command line in these |
| 684 | // tests. |
Tarun Bansal | 509a8dd | 2018-04-10 17:19:16 | [diff] [blame] | 685 | int rtt_value = 0.0; |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 686 | EXPECT_TRUE( |
Tarun Bansal | 509a8dd | 2018-04-10 17:19:16 | [diff] [blame] | 687 | base::StringToInt(request.headers.find("rtt")->second, &rtt_value)); |
| 688 | EXPECT_LE(0, rtt_value); |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 689 | EXPECT_TRUE(IsSimilarToIntABNF(request.headers.find("rtt")->second)); |
Tarun Bansal | 509a8dd | 2018-04-10 17:19:16 | [diff] [blame] | 690 | // Verify that RTT value is a multiple of 50 milliseconds. |
| 691 | EXPECT_EQ(0, rtt_value % 50); |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 692 | EXPECT_GE(expected_ect == net::EFFECTIVE_CONNECTION_TYPE_2G ? 3000 : 500, |
| 693 | rtt_value); |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 694 | |
Tarun Bansal | 509a8dd | 2018-04-10 17:19:16 | [diff] [blame] | 695 | double mbps_value = 0.0; |
| 696 | EXPECT_TRUE(base::StringToDouble(request.headers.find("downlink")->second, |
| 697 | &mbps_value)); |
| 698 | EXPECT_LE(0, mbps_value); |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 699 | EXPECT_TRUE( |
| 700 | IsSimilarToDoubleABNF(request.headers.find("downlink")->second)); |
Tarun Bansal | 509a8dd | 2018-04-10 17:19:16 | [diff] [blame] | 701 | // Verify that the mbps value is a multiple of 0.050 mbps. |
| 702 | // Allow for small amount of noise due to double to integer conversions. |
| 703 | EXPECT_NEAR(0, (static_cast<int>(mbps_value * 1000)) % 50, 1); |
| 704 | EXPECT_GE(10.0, mbps_value); |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 705 | |
| 706 | EXPECT_FALSE(request.headers.find("ect")->second.empty()); |
Tarun Bansal | ceab959 | 2018-05-01 18:57:35 | [diff] [blame] | 707 | |
| 708 | // TODO(tbansal): https://crbug.com/819244: When network servicification is |
Tarun Bansal | 5ac53354 | 2018-08-10 19:45:52 | [diff] [blame] | 709 | // enabled, the renderer processes do not receive notifications on |
| 710 | // change in the network quality. Hence, the network quality client hints |
| 711 | // are not set to the correct value on subresources. |
| 712 | bool is_main_frame_navigation = |
| 713 | request.GetURL().spec().find(".html") != std::string::npos; |
John Abd-El-Malek | 67facbe8 | 2019-06-06 22:37:08 | [diff] [blame] | 714 | if (is_main_frame_navigation) { |
Tarun Bansal | ceab959 | 2018-05-01 18:57:35 | [diff] [blame] | 715 | // Effective connection type is forced to 2G using command line in these |
| 716 | // tests. RTT is expected to be 1800 msec but leave some gap to account |
| 717 | // for added noise and randomization. |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 718 | if (expected_ect == net::EFFECTIVE_CONNECTION_TYPE_2G) { |
| 719 | EXPECT_NEAR(1800, rtt_value, 360); |
| 720 | } else if (expected_ect == net::EFFECTIVE_CONNECTION_TYPE_3G) { |
| 721 | EXPECT_NEAR(450, rtt_value, 90); |
| 722 | } else { |
| 723 | NOTREACHED(); |
| 724 | } |
Tarun Bansal | ceab959 | 2018-05-01 18:57:35 | [diff] [blame] | 725 | |
| 726 | // Effective connection type is forced to 2G using command line in these |
| 727 | // tests. downlink is expected to be 0.075 Mbps but leave some gap to |
| 728 | // account for added noise and randomization. |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 729 | if (expected_ect == net::EFFECTIVE_CONNECTION_TYPE_2G) { |
| 730 | EXPECT_NEAR(0.075, mbps_value, 0.05); |
| 731 | } else if (expected_ect == net::EFFECTIVE_CONNECTION_TYPE_3G) { |
| 732 | EXPECT_NEAR(0.4, mbps_value, 0.1); |
| 733 | } else { |
| 734 | NOTREACHED(); |
| 735 | } |
Tarun Bansal | ceab959 | 2018-05-01 18:57:35 | [diff] [blame] | 736 | |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 737 | EXPECT_EQ(expected_ect == net::EFFECTIVE_CONNECTION_TYPE_2G ? "2g" : "3g", |
| 738 | request.headers.find("ect")->second); |
Tarun Bansal | ceab959 | 2018-05-01 18:57:35 | [diff] [blame] | 739 | } |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 740 | } |
| 741 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 742 | net::EmbeddedTestServer http_server_; |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 743 | net::EmbeddedTestServer https_server_; |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 744 | net::EmbeddedTestServer https_cross_origin_server_; |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 745 | GURL accept_ch_with_lifetime_http_local_url_; |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 746 | GURL http_equiv_accept_ch_with_lifetime_http_local_url_; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 747 | GURL accept_ch_with_lifetime_url_; |
Tarun Bansal | 73502f9 | 2019-04-16 21:21:19 | [diff] [blame] | 748 | GURL accept_ch_with_short_lifetime_url_; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 749 | GURL accept_ch_without_lifetime_url_; |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 750 | GURL http_equiv_accept_ch_without_lifetime_url_; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 751 | GURL without_accept_ch_without_lifetime_url_; |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 752 | GURL without_accept_ch_without_lifetime_local_url_; |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 753 | GURL accept_ch_without_lifetime_with_iframe_url_; |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 754 | GURL http_equiv_accept_ch_without_lifetime_with_iframe_url_; |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 755 | GURL accept_ch_without_lifetime_with_subresource_url_; |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 756 | GURL http_equiv_accept_ch_without_lifetime_with_subresource_url_; |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 757 | GURL accept_ch_without_lifetime_with_subresource_iframe_url_; |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 758 | GURL http_equiv_accept_ch_without_lifetime_with_subresource_iframe_url_; |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 759 | GURL without_accept_ch_without_lifetime_img_foo_com_; |
| 760 | GURL without_accept_ch_without_lifetime_img_localhost_; |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 761 | GURL accept_ch_without_lifetime_img_localhost_; |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 762 | GURL http_equiv_accept_ch_without_lifetime_img_localhost_; |
| 763 | GURL http_equiv_accept_ch_with_lifetime_; |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 764 | GURL redirect_url_; |
Maks Orlovich | 7a588d8 | 2020-05-06 15:17:24 | [diff] [blame] | 765 | GURL accept_ch_empty_; |
| 766 | GURL http_equiv_accept_ch_merge_; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 767 | |
Mike West | e555be86 | 2019-02-20 16:17:30 | [diff] [blame] | 768 | std::string main_frame_ua_observed_; |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 769 | std::string main_frame_ua_full_version_observed_; |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 770 | std::string main_frame_ua_mobile_observed_; |
| 771 | std::string main_frame_ua_platform_observed_; |
Mike West | e555be86 | 2019-02-20 16:17:30 | [diff] [blame] | 772 | |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 773 | double main_frame_dpr_observed_ = -1; |
| 774 | double main_frame_viewport_width_observed_ = -1; |
| 775 | double main_frame_device_memory_observed_ = -1; |
| 776 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 777 | // Expect client hints on all the main frame request. |
| 778 | bool expect_client_hints_on_main_frame_; |
| 779 | // Expect client hints on all the subresource requests. |
Callum May | 1d93974 | 2020-03-02 17:51:30 | [diff] [blame] | 780 | bool expect_client_hints_on_subresources_ |
| 781 | GUARDED_BY(expect_client_hints_on_subresources_lock_); |
| 782 | |
| 783 | base::Lock expect_client_hints_on_subresources_lock_; |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 784 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 785 | size_t count_user_agent_hint_headers_seen_; |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 786 | size_t count_ua_mobile_client_hints_headers_seen_; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 787 | size_t count_client_hints_headers_seen_; |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 788 | |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 789 | std::unique_ptr<ThirdPartyURLLoaderInterceptor> request_interceptor_; |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 790 | |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 791 | // Set to 2G in SetUpCommandLine(). |
| 792 | net::EffectiveConnectionType expected_ect = net::EFFECTIVE_CONNECTION_TYPE_2G; |
| 793 | |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 794 | DISALLOW_COPY_AND_ASSIGN(ClientHintsBrowserTest); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 795 | }; |
| 796 | |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 797 | // True if testing for http-equiv correctness. When set to true, the tests |
| 798 | // use webpages that may contain http-equiv Accept-CH and Accept-CH-Lifetime |
| 799 | // headers. When set to false, the tests use webpages that set the headers in |
| 800 | // the HTTP response headers. |
Ilia Samsonov | 282c3841 | 2019-12-09 08:15:10 | [diff] [blame] | 801 | INSTANTIATE_TEST_SUITE_P(All, |
Victor Costan | e5e9151 | 2019-02-13 08:24:02 | [diff] [blame] | 802 | ClientHintsBrowserTest, |
| 803 | testing::Bool()); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 804 | |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 805 | class ClientHintsAllowThirdPartyBrowserTest : public ClientHintsBrowserTest { |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 806 | std::unique_ptr<base::FeatureList> EnabledFeatures() override { |
| 807 | std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
| 808 | feature_list->InitializeFromCommandLine( |
Maks Orlovich | c66745a | 2020-06-30 17:40:02 | [diff] [blame] | 809 | "AllowClientHintsToThirdParty,UserAgentClientHint,LangClientHintHeader", |
| 810 | ""); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 811 | return feature_list; |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 812 | } |
| 813 | }; |
| 814 | |
Ilia Samsonov | 282c3841 | 2019-12-09 08:15:10 | [diff] [blame] | 815 | INSTANTIATE_TEST_SUITE_P(All, |
Aaron Tagliaboschi | a09ec44 | 2019-09-18 15:29:03 | [diff] [blame] | 816 | ClientHintsAllowThirdPartyBrowserTest, |
| 817 | testing::Bool()); |
| 818 | |
Tarun Bansal | 74e189d | 2018-05-07 19:07:35 | [diff] [blame] | 819 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, CorsChecks) { |
Mike West | 14c1110 | 2019-02-04 16:16:47 | [diff] [blame] | 820 | for (size_t i = 0; i < blink::kClientHintsMappingsCount; ++i) { |
Tarun Bansal | 74e189d | 2018-05-07 19:07:35 | [diff] [blame] | 821 | // Do not test for headers that have not been enabled on the blink "stable" |
| 822 | // yet. |
| 823 | if (std::string(blink::kClientHintsHeaderMapping[i]) == "rtt" || |
| 824 | std::string(blink::kClientHintsHeaderMapping[i]) == "downlink" || |
| 825 | std::string(blink::kClientHintsHeaderMapping[i]) == "ect") { |
| 826 | continue; |
| 827 | } |
Takashi Toyoshima | 2e01e69 | 2018-11-16 03:23:27 | [diff] [blame] | 828 | EXPECT_TRUE(network::cors::IsCorsSafelistedHeader( |
Tarun Bansal | 74e189d | 2018-05-07 19:07:35 | [diff] [blame] | 829 | blink::kClientHintsHeaderMapping[i], "42" /* value */)); |
| 830 | } |
Takashi Toyoshima | 2e01e69 | 2018-11-16 03:23:27 | [diff] [blame] | 831 | EXPECT_FALSE(network::cors::IsCorsSafelistedHeader("not-a-client-hint-header", |
Tarun Bansal | 74e189d | 2018-05-07 19:07:35 | [diff] [blame] | 832 | "" /* value */)); |
| 833 | EXPECT_TRUE( |
Takashi Toyoshima | 2e01e69 | 2018-11-16 03:23:27 | [diff] [blame] | 834 | network::cors::IsCorsSafelistedHeader("save-data", "on" /* value */)); |
Tarun Bansal | 74e189d | 2018-05-07 19:07:35 | [diff] [blame] | 835 | } |
| 836 | |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 837 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, HttpEquivWorks) { |
| 838 | const GURL gurl = http_equiv_accept_ch_without_lifetime_img_localhost(); |
| 839 | base::HistogramTester histogram_tester; |
| 840 | |
| 841 | SetClientHintExpectationsOnMainFrame(false); |
| 842 | SetClientHintExpectationsOnSubresources(true); |
| 843 | |
| 844 | ui_test_utils::NavigateToURL(browser(), gurl); |
| 845 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
| 846 | } |
| 847 | |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 848 | // Loads a webpage that requests persisting of client hints. Verifies that |
| 849 | // the browser receives the mojo notification from the renderer and persists the |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 850 | // client hints to the disk --- unless it's using http-equiv which shouldn't |
| 851 | // persist. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 852 | IN_PROC_BROWSER_TEST_P(ClientHintsBrowserTest, ClientHintsHttps) { |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 853 | base::HistogramTester histogram_tester; |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 854 | const GURL gurl = GetParam() ? http_equiv_accept_ch_with_lifetime() |
| 855 | : accept_ch_with_lifetime_url(); |
| 856 | ui_test_utils::NavigateToURL(browser(), gurl); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 857 | |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 858 | if (GetParam()) |
| 859 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
| 860 | else |
| 861 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 862 | |
| 863 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 864 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 865 | |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 866 | if (GetParam()) { |
| 867 | histogram_tester.ExpectTotalCount("ClientHints.UpdateSize", 0); |
| 868 | histogram_tester.ExpectTotalCount("ClientHints.PersistDuration", 0); |
| 869 | } else { |
| 870 | // client_hints_url() sets the expected number of client hints. |
| 871 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", |
| 872 | expected_client_hints_number, 1); |
| 873 | // accept_ch_with_lifetime_url() sets client hints persist duration to 3600 |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 874 | // seconds, but a maximum value is registered instead. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 875 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 876 | uma_histogram_max_value, 1); |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 877 | } |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 878 | } |
| 879 | |
Aaron Tagliaboschi | 8ef04eca | 2020-05-12 23:16:44 | [diff] [blame] | 880 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, PRE_ClientHintsClearSession) { |
| 881 | const GURL gurl = accept_ch_with_lifetime_url(); |
| 882 | |
| 883 | base::HistogramTester histogram_tester; |
| 884 | ContentSettingsForOneType host_settings; |
| 885 | |
| 886 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 887 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, std::string(), |
| 888 | &host_settings); |
| 889 | EXPECT_EQ(0u, host_settings.size()); |
| 890 | |
| 891 | // Fetching |gurl| should persist the request for client hints iff using |
| 892 | // headers and not http-equiv. |
| 893 | ui_test_utils::NavigateToURL(browser(), gurl); |
| 894 | |
| 895 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 896 | |
| 897 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 898 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Aaron Tagliaboschi | 8ef04eca | 2020-05-12 23:16:44 | [diff] [blame] | 899 | base::RunLoop().RunUntilIdle(); |
| 900 | |
| 901 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", |
| 902 | expected_client_hints_number, 1); |
| 903 | // accept_ch_with_lifetime_url() sets client hints persist duration to 3600 |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 904 | // seconds, but a maximum value is registered instead. |
Aaron Tagliaboschi | 8ef04eca | 2020-05-12 23:16:44 | [diff] [blame] | 905 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 906 | uma_histogram_max_value, 1); |
Aaron Tagliaboschi | 8ef04eca | 2020-05-12 23:16:44 | [diff] [blame] | 907 | |
| 908 | // Clients hints preferences for one origin should be persisted. |
| 909 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 910 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, std::string(), |
| 911 | &host_settings); |
| 912 | EXPECT_EQ(1u, host_settings.size()); |
| 913 | |
| 914 | SetClientHintExpectationsOnMainFrame(true); |
| 915 | SetClientHintExpectationsOnSubresources(true); |
| 916 | ui_test_utils::NavigateToURL(browser(), |
| 917 | without_accept_ch_without_lifetime_url()); |
| 918 | |
| 919 | // The user agent hint is attached to all three requests: |
| 920 | EXPECT_EQ(3u, count_user_agent_hint_headers_seen()); |
| 921 | EXPECT_EQ(3u, count_ua_mobile_client_hints_headers_seen()); |
| 922 | |
| 923 | // Expected number of hints attached to the image request, and the same number |
| 924 | // to the main frame request. |
| 925 | EXPECT_EQ(expected_client_hints_number * 2, |
| 926 | count_client_hints_headers_seen()); |
| 927 | } |
| 928 | |
| 929 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, ClientHintsClearSession) { |
| 930 | const GURL gurl = accept_ch_with_lifetime_url(); |
| 931 | |
| 932 | base::HistogramTester histogram_tester; |
| 933 | ContentSettingsForOneType host_settings; |
| 934 | |
| 935 | // Clients hints preferences for one origin should be persisted. |
| 936 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 937 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, std::string(), |
| 938 | &host_settings); |
| 939 | EXPECT_EQ(0u, host_settings.size()); |
| 940 | |
| 941 | SetClientHintExpectationsOnMainFrame(false); |
| 942 | SetClientHintExpectationsOnSubresources(false); |
| 943 | ui_test_utils::NavigateToURL(browser(), |
| 944 | without_accept_ch_without_lifetime_url()); |
| 945 | |
| 946 | // The user agent hint is attached to all three requests: |
| 947 | EXPECT_EQ(2u, count_user_agent_hint_headers_seen()); |
| 948 | EXPECT_EQ(2u, count_ua_mobile_client_hints_headers_seen()); |
| 949 | |
| 950 | // Expected number of hints attached to the image request, and the same number |
| 951 | // to the main frame request. |
| 952 | EXPECT_EQ(0u, count_client_hints_headers_seen()); |
| 953 | } |
| 954 | |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 955 | // Test that client hints are attached to subresources only if they belong |
| 956 | // to the same host as document host. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 957 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 958 | ClientHintsHttpsSubresourceDifferentOrigin) { |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 959 | const GURL gurl = accept_ch_with_lifetime_url(); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 960 | |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 961 | base::HistogramTester histogram_tester; |
| 962 | |
| 963 | // Add client hints for the embedded test server. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 964 | ui_test_utils::NavigateToURL(browser(), gurl); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 965 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 966 | |
| 967 | // Verify that the client hints settings for localhost have been saved. |
| 968 | ContentSettingsForOneType client_hints_settings; |
| 969 | HostContentSettingsMap* host_content_settings_map = |
| 970 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()); |
| 971 | host_content_settings_map->GetSettingsForOneType( |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 972 | ContentSettingsType::CLIENT_HINTS, std::string(), &client_hints_settings); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 973 | ASSERT_EQ(1U, client_hints_settings.size()); |
| 974 | |
| 975 | // Copy the client hints setting for localhost to foo.com. |
| 976 | host_content_settings_map->SetWebsiteSettingDefaultScope( |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 977 | GURL("https://foo.com/"), GURL(), ContentSettingsType::CLIENT_HINTS, |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 978 | std::string(), |
Jeremy Roman | ec48d7a | 2018-03-01 17:35:09 | [diff] [blame] | 979 | std::make_unique<base::Value>( |
Oksana Zhuravlova | b14dc88 | 2018-04-12 17:34:57 | [diff] [blame] | 980 | client_hints_settings.at(0).setting_value.Clone())); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 981 | |
| 982 | // Verify that client hints for the two hosts has been saved. |
| 983 | host_content_settings_map = |
| 984 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()); |
| 985 | host_content_settings_map->GetSettingsForOneType( |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 986 | ContentSettingsType::CLIENT_HINTS, std::string(), &client_hints_settings); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 987 | ASSERT_EQ(2U, client_hints_settings.size()); |
| 988 | |
| 989 | // Navigating to without_accept_ch_without_lifetime_img_localhost() should |
| 990 | // attach client hints to the image subresouce contained in that page since |
| 991 | // the image is located on the same server as the document origin. |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 992 | SetClientHintExpectationsOnMainFrame(true); |
| 993 | SetClientHintExpectationsOnSubresources(true); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 994 | ui_test_utils::NavigateToURL( |
| 995 | browser(), without_accept_ch_without_lifetime_img_localhost()); |
| 996 | base::RunLoop().RunUntilIdle(); |
| 997 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 998 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 999 | |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1000 | // The user agent hint is attached to all three requests, as is UA-mobile: |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1001 | EXPECT_EQ(3u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1002 | EXPECT_EQ(3u, count_ua_mobile_client_hints_headers_seen()); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1003 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1004 | // Expected number of hints attached to the image request, and the same number |
| 1005 | // to the main frame request. |
| 1006 | EXPECT_EQ(expected_client_hints_number * 2, |
| 1007 | count_client_hints_headers_seen()); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 1008 | |
| 1009 | // Navigating to without_accept_ch_without_lifetime_img_foo_com() should not |
| 1010 | // attach client hints to the image subresouce contained in that page since |
| 1011 | // the image is located on a different server as the document origin. |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 1012 | ui_test_utils::NavigateToURL( |
| 1013 | browser(), without_accept_ch_without_lifetime_img_foo_com()); |
| 1014 | base::RunLoop().RunUntilIdle(); |
| 1015 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 1016 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 1017 | |
Tarun Bansal | b30b753 | 2018-03-14 21:50:38 | [diff] [blame] | 1018 | // The device-memory and dprheader is attached to the main frame request. |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 1019 | #if defined(OS_ANDROID) |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1020 | EXPECT_EQ(expected_client_hints_number, count_client_hints_headers_seen()); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 1021 | #else |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1022 | EXPECT_EQ(expected_client_hints_number * 3, |
| 1023 | count_client_hints_headers_seen()); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 1024 | #endif |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1025 | |
| 1026 | // Requests to third party servers should have only one client hint attached |
| 1027 | // (`Sec-CH-UA`). |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1028 | EXPECT_EQ(1u, third_party_request_count_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1029 | EXPECT_EQ(2u, third_party_client_hints_count_seen()); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 1030 | } |
| 1031 | |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1032 | // Test that client hints are attached to subresources checks the right setting |
| 1033 | // for OTR profile. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1034 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1035 | ClientHintsHttpsSubresourceOffTheRecord) { |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1036 | const GURL gurl = accept_ch_with_lifetime_url(); |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1037 | |
| 1038 | base::HistogramTester histogram_tester; |
| 1039 | |
| 1040 | // Add client hints for the embedded test server. |
| 1041 | ui_test_utils::NavigateToURL(browser(), gurl); |
| 1042 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 1043 | |
| 1044 | // Main profile should get hints for both page and subresources. |
| 1045 | SetClientHintExpectationsOnMainFrame(true); |
| 1046 | SetClientHintExpectationsOnSubresources(true); |
| 1047 | ui_test_utils::NavigateToURL( |
| 1048 | browser(), without_accept_ch_without_lifetime_img_localhost()); |
| 1049 | base::RunLoop().RunUntilIdle(); |
| 1050 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 1051 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1052 | |
| 1053 | // The user agent hint is attached to all three requests: |
| 1054 | EXPECT_EQ(3u, count_user_agent_hint_headers_seen()); |
| 1055 | EXPECT_EQ(3u, count_ua_mobile_client_hints_headers_seen()); |
| 1056 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1057 | // Expected number of hints attached to the image request, and the same number |
| 1058 | // to the main frame request. |
| 1059 | EXPECT_EQ(expected_client_hints_number * 2, |
| 1060 | count_client_hints_headers_seen()); |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1061 | |
| 1062 | // OTR profile should get neither. |
| 1063 | Browser* otr_browser = CreateIncognitoBrowser(browser()->profile()); |
| 1064 | SetClientHintExpectationsOnMainFrame(false); |
| 1065 | SetClientHintExpectationsOnSubresources(false); |
| 1066 | ui_test_utils::NavigateToURL( |
| 1067 | otr_browser, without_accept_ch_without_lifetime_img_localhost()); |
| 1068 | } |
| 1069 | |
Mike West | e555be86 | 2019-02-20 16:17:30 | [diff] [blame] | 1070 | // Verify that we send only major version information in the `Sec-CH-UA` header |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1071 | // by default, regardless of opt-in. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1072 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, UserAgentVersion) { |
| 1073 | const GURL gurl = accept_ch_with_lifetime_url(); |
Mike West | e555be86 | 2019-02-20 16:17:30 | [diff] [blame] | 1074 | |
| 1075 | blink::UserAgentMetadata ua = ::GetUserAgentMetadata(); |
| 1076 | |
| 1077 | // Navigate to a page that opts-into the header: the value should end with |
| 1078 | // the major version, and not contain the full version. |
| 1079 | SetClientHintExpectationsOnMainFrame(false); |
| 1080 | ui_test_utils::NavigateToURL(browser(), gurl); |
Aaron Tagliaboschi | 9f01b68 | 2020-05-05 21:03:17 | [diff] [blame] | 1081 | std::string expected_ua = ua.SerializeBrandVersionList(); |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1082 | EXPECT_EQ(main_frame_ua_observed(), expected_ua); |
| 1083 | EXPECT_TRUE(main_frame_ua_full_version_observed().empty()); |
Mike West | e555be86 | 2019-02-20 16:17:30 | [diff] [blame] | 1084 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1085 | // Navigate again, after the opt-in: the value should stay the major |
Mike West | e555be86 | 2019-02-20 16:17:30 | [diff] [blame] | 1086 | // version. |
| 1087 | SetClientHintExpectationsOnMainFrame(true); |
| 1088 | ui_test_utils::NavigateToURL(browser(), gurl); |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1089 | std::string expected_full_version = "\"" + ua.full_version + "\""; |
| 1090 | EXPECT_EQ(main_frame_ua_observed(), expected_ua); |
| 1091 | EXPECT_EQ(main_frame_ua_full_version_observed(), expected_full_version); |
Mike West | e555be86 | 2019-02-20 16:17:30 | [diff] [blame] | 1092 | } |
| 1093 | |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1094 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, UAHintsTabletMode) { |
| 1095 | const GURL gurl = accept_ch_with_lifetime_url(); |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 1096 | |
| 1097 | blink::UserAgentMetadata ua = ::GetUserAgentMetadata(); |
| 1098 | |
| 1099 | // First request: only minimal hints, no tablet override. |
| 1100 | SetClientHintExpectationsOnMainFrame(false); |
| 1101 | ui_test_utils::NavigateToURL(browser(), gurl); |
Aaron Tagliaboschi | 9f01b68 | 2020-05-05 21:03:17 | [diff] [blame] | 1102 | std::string expected_ua = ua.SerializeBrandVersionList(); |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 1103 | EXPECT_EQ(main_frame_ua_observed(), expected_ua); |
| 1104 | EXPECT_EQ(main_frame_ua_full_version_observed(), ""); |
| 1105 | EXPECT_EQ(main_frame_ua_mobile_observed(), "?0"); |
| 1106 | EXPECT_EQ(main_frame_ua_platform_observed(), ""); |
| 1107 | |
| 1108 | // Second request: table override, all hints. |
| 1109 | chrome::ToggleRequestTabletSite(browser()); |
| 1110 | SetClientHintExpectationsOnMainFrame(true); |
| 1111 | ui_test_utils::NavigateToURL(browser(), gurl); |
| 1112 | EXPECT_EQ(main_frame_ua_observed(), expected_ua); |
| 1113 | std::string expected_full_version = "\"" + ua.full_version + "\""; |
| 1114 | EXPECT_EQ(main_frame_ua_full_version_observed(), expected_full_version); |
| 1115 | EXPECT_EQ(main_frame_ua_mobile_observed(), "?1"); |
| 1116 | EXPECT_EQ(main_frame_ua_platform_observed(), "\"Android\""); |
| 1117 | } |
| 1118 | |
| 1119 | // TODO(morlovich): Move this into WebContentsImplBrowserTest once things are |
| 1120 | // refactored enough that UA client hints actually work in content/ |
| 1121 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, UserAgentOverrideClientHints) { |
| 1122 | content::WebContents* web_contents = |
| 1123 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 1124 | |
| 1125 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 1126 | const std::string kHeaderPath = std::string("/echoheader?") + |
| 1127 | net::HttpRequestHeaders::kUserAgent + |
| 1128 | "&sec-ch-ua&sec-ch-ua-mobile"; |
| 1129 | const GURL kUrl(embedded_test_server()->GetURL(kHeaderPath)); |
| 1130 | |
| 1131 | web_contents->SetUserAgentOverride( |
| 1132 | blink::UserAgentOverride::UserAgentOnly("foo"), false); |
| 1133 | // Not enabled first. |
| 1134 | ui_test_utils::NavigateToURL(browser(), kUrl); |
| 1135 | std::string header_value; |
| 1136 | EXPECT_TRUE(ExecuteScriptAndExtractString( |
| 1137 | web_contents, |
| 1138 | "window.domAutomationController.send(document.body.textContent);", |
| 1139 | &header_value)); |
| 1140 | EXPECT_EQ(std::string::npos, header_value.find("foo")) << header_value; |
| 1141 | |
| 1142 | // Actually turn it on. |
| 1143 | web_contents->GetController() |
| 1144 | .GetLastCommittedEntry() |
| 1145 | ->SetIsOverridingUserAgent(true); |
| 1146 | |
| 1147 | ui_test_utils::NavigateToURL(browser(), kUrl); |
| 1148 | EXPECT_TRUE(ExecuteScriptAndExtractString( |
| 1149 | web_contents, |
| 1150 | "window.domAutomationController.send(document.body.textContent);", |
| 1151 | &header_value)); |
| 1152 | // Since no value was provided for client hints, they are not sent. |
| 1153 | EXPECT_EQ("foo\nNone\nNone", header_value); |
| 1154 | |
| 1155 | // Now actually provide values for the hints. |
| 1156 | blink::UserAgentOverride ua_override; |
| 1157 | ua_override.ua_string_override = "foobar"; |
| 1158 | ua_override.ua_metadata_override.emplace(); |
| 1159 | ua_override.ua_metadata_override->mobile = true; |
Aaron Tagliaboschi | 9f01b68 | 2020-05-05 21:03:17 | [diff] [blame] | 1160 | ua_override.ua_metadata_override->brand_version_list.emplace_back( |
| 1161 | "Foobarnator", "3.14"); |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 1162 | web_contents->SetUserAgentOverride(ua_override, false); |
| 1163 | ui_test_utils::NavigateToURL(browser(), kUrl); |
| 1164 | EXPECT_TRUE(ExecuteScriptAndExtractString( |
| 1165 | web_contents, |
| 1166 | "window.domAutomationController.send(document.body.textContent);", |
| 1167 | &header_value)); |
Aaron Tagliaboschi | 9f01b68 | 2020-05-05 21:03:17 | [diff] [blame] | 1168 | EXPECT_EQ("foobar\n\"Foobarnator\";v=\"3.14\"\n?1", header_value); |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 1169 | } |
| 1170 | |
Maks Orlovich | 7a588d8 | 2020-05-06 15:17:24 | [diff] [blame] | 1171 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, EmptyAcceptCH) { |
| 1172 | // First navigate to a page that enables hints. No CH for it yet, since |
| 1173 | // nothing opted in. |
| 1174 | GURL gurl = accept_ch_with_lifetime_url(); |
| 1175 | SetClientHintExpectationsOnMainFrame(false); |
| 1176 | ui_test_utils::NavigateToURL(browser(), gurl); |
| 1177 | |
| 1178 | // Now go to a page with blank Accept-CH. Should get hints from previous |
| 1179 | // visit. |
| 1180 | gurl = accept_ch_empty(); |
| 1181 | SetClientHintExpectationsOnMainFrame(true); |
| 1182 | ui_test_utils::NavigateToURL(browser(), gurl); |
| 1183 | |
| 1184 | // Visiting again should not expect them since we opted out again. |
| 1185 | SetClientHintExpectationsOnMainFrame(false); |
| 1186 | ui_test_utils::NavigateToURL(browser(), gurl); |
| 1187 | } |
| 1188 | |
| 1189 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, MergeAcceptCH) { |
| 1190 | // Go to page where some hints are enabled by headers, some by |
| 1191 | // http-equiv. It shouldn't get hints itself (due to first visit), |
| 1192 | // but subresources should get all the client hints. |
| 1193 | GURL gurl = http_equiv_accept_ch_merge(); |
| 1194 | SetClientHintExpectationsOnMainFrame(false); |
| 1195 | SetClientHintExpectationsOnSubresources(true); |
| 1196 | ui_test_utils::NavigateToURL(browser(), gurl); |
| 1197 | EXPECT_EQ(expected_client_hints_number, count_client_hints_headers_seen()); |
| 1198 | } |
| 1199 | |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1200 | void ClientHintsBrowserTest::TestProfilesIndependent(Browser* browser_a, |
| 1201 | Browser* browser_b) { |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1202 | const GURL gurl = accept_ch_with_lifetime_url(); |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1203 | |
| 1204 | blink::UserAgentMetadata ua = ::GetUserAgentMetadata(); |
| 1205 | |
| 1206 | // Navigate |browser_a| to a page that opts-into the header: the value should |
| 1207 | // end with the major version, and not contain the full version. |
| 1208 | SetClientHintExpectationsOnMainFrame(false); |
| 1209 | ui_test_utils::NavigateToURL(browser_a, gurl); |
Aaron Tagliaboschi | 9f01b68 | 2020-05-05 21:03:17 | [diff] [blame] | 1210 | std::string expected_ua = ua.SerializeBrandVersionList(); |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1211 | EXPECT_EQ(main_frame_ua_observed(), expected_ua); |
| 1212 | EXPECT_TRUE(main_frame_ua_full_version_observed().empty()); |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1213 | |
| 1214 | // Try again on |browser_a|, the header should have an effect there. |
| 1215 | SetClientHintExpectationsOnMainFrame(true); |
| 1216 | ui_test_utils::NavigateToURL(browser_a, gurl); |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1217 | std::string expected_full_version = "\"" + ua.full_version + "\""; |
| 1218 | EXPECT_EQ(main_frame_ua_observed(), expected_ua); |
| 1219 | EXPECT_EQ(main_frame_ua_full_version_observed(), expected_full_version); |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1220 | |
| 1221 | // Navigate on |browser_b|. That should still only have the major |
| 1222 | // version. |
| 1223 | SetClientHintExpectationsOnMainFrame(false); |
| 1224 | ui_test_utils::NavigateToURL(browser_b, gurl); |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1225 | EXPECT_EQ(main_frame_ua_observed(), expected_ua); |
| 1226 | EXPECT_TRUE(main_frame_ua_full_version_observed().empty()); |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | // Check that client hints attached to navigation inside OTR profiles |
| 1230 | // use the right settings, regular -> OTR direction. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1231 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, OffTheRecordIndependent) { |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1232 | TestProfilesIndependent(browser(), |
| 1233 | CreateIncognitoBrowser(browser()->profile())); |
| 1234 | } |
| 1235 | |
| 1236 | // Check that client hints attached to navigation inside OTR profiles |
| 1237 | // use the right settings, OTR -> regular direction. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1238 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, OffTheRecordIndependent2) { |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1239 | TestProfilesIndependent(CreateIncognitoBrowser(browser()->profile()), |
| 1240 | browser()); |
| 1241 | } |
| 1242 | |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 1243 | // Test that client hints are attached to third party subresources if |
| 1244 | // AllowClientHintsToThirdParty feature is enabled. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1245 | IN_PROC_BROWSER_TEST_P(ClientHintsAllowThirdPartyBrowserTest, |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 1246 | ClientHintsThirdPartyAllowed) { |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1247 | GURL gurl; |
| 1248 | unsigned update_event_count = 0; |
| 1249 | if (GetParam()) { |
| 1250 | gurl = http_equiv_accept_ch_without_lifetime_img_localhost(); |
| 1251 | } else { |
| 1252 | gurl = accept_ch_without_lifetime_img_localhost(); |
| 1253 | update_event_count = 1; |
| 1254 | } |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1255 | |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 1256 | base::HistogramTester histogram_tester; |
| 1257 | |
| 1258 | SetClientHintExpectationsOnMainFrame(false); |
| 1259 | SetClientHintExpectationsOnSubresources(true); |
| 1260 | |
| 1261 | // Add client hints for the embedded test server. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1262 | ui_test_utils::NavigateToURL(browser(), gurl); |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1263 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", |
| 1264 | update_event_count); |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 1265 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1266 | EXPECT_EQ(expected_client_hints_number, count_client_hints_headers_seen()); |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 1267 | |
| 1268 | // Requests to third party servers should not have client hints attached. |
| 1269 | EXPECT_EQ(1u, third_party_request_count_seen()); |
| 1270 | |
Aaron Tagliaboschi | a09ec44 | 2019-09-18 15:29:03 | [diff] [blame] | 1271 | // Device memory, viewport width, DRP, and UA client hints should be sent to |
| 1272 | // the third-party when feature "AllowClientHintsToThirdParty" is enabled. |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1273 | EXPECT_EQ(5u, third_party_client_hints_count_seen()); |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | // Test that client hints are not attached to third party subresources if |
| 1277 | // AllowClientHintsToThirdParty feature is not enabled. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1278 | IN_PROC_BROWSER_TEST_P(ClientHintsBrowserTest, |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 1279 | ClientHintsThirdPartyNotAllowed) { |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1280 | GURL gurl; |
| 1281 | unsigned update_event_count = 0; |
| 1282 | if (GetParam()) { |
| 1283 | gurl = http_equiv_accept_ch_without_lifetime_img_localhost(); |
| 1284 | } else { |
| 1285 | gurl = accept_ch_without_lifetime_img_localhost(); |
| 1286 | update_event_count = 1; |
| 1287 | } |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1288 | |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 1289 | base::HistogramTester histogram_tester; |
| 1290 | |
| 1291 | SetClientHintExpectationsOnMainFrame(false); |
| 1292 | SetClientHintExpectationsOnSubresources(true); |
| 1293 | |
| 1294 | // Add client hints for the embedded test server. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1295 | ui_test_utils::NavigateToURL(browser(), gurl); |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1296 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", |
| 1297 | update_event_count); |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 1298 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1299 | EXPECT_EQ(2u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1300 | EXPECT_EQ(2u, count_ua_mobile_client_hints_headers_seen()); |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1301 | EXPECT_EQ(expected_client_hints_number, count_client_hints_headers_seen()); |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 1302 | |
| 1303 | // Requests to third party servers should not have client hints attached. |
| 1304 | EXPECT_EQ(1u, third_party_request_count_seen()); |
| 1305 | |
| 1306 | // Client hints should not be sent to the third-party when feature |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1307 | // "AllowClientHintsToThirdParty" is not enabled, with the exception of the |
| 1308 | // `Sec-CH-UA` hint, which is sent with every request. |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1309 | EXPECT_EQ(2u, third_party_client_hints_count_seen()); |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 1310 | } |
| 1311 | |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 1312 | // Loads a HTTPS webpage that does not request persisting of client hints. |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 1313 | // A same-origin iframe loaded by the webpage requests persistence of client |
Maks Orlovich | 46619c73 | 2020-05-07 10:40:53 | [diff] [blame] | 1314 | // hints. Since that's not a main frame, persistence should not happen. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1315 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 1316 | PersistenceRequestIframe_SameOrigin) { |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1317 | const GURL gurl = accept_ch_without_lifetime_with_iframe_url(); |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 1318 | base::HistogramTester histogram_tester; |
| 1319 | ContentSettingsForOneType host_settings; |
| 1320 | |
| 1321 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1322 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, std::string(), |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 1323 | &host_settings); |
| 1324 | EXPECT_EQ(0u, host_settings.size()); |
| 1325 | |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1326 | ui_test_utils::NavigateToURL(browser(), gurl); |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 1327 | |
Maks Orlovich | 46619c73 | 2020-05-07 10:40:53 | [diff] [blame] | 1328 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 1329 | |
| 1330 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 1331 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 1332 | |
| 1333 | // accept_ch_without_lifetime_with_iframe_url() loads |
| 1334 | // accept_ch_with_lifetime() in an iframe. The request to persist client |
Maks Orlovich | 46619c73 | 2020-05-07 10:40:53 | [diff] [blame] | 1335 | // hints from accept_ch_with_lifetime() should not be persisted. |
| 1336 | histogram_tester.ExpectTotalCount("ClientHints.UpdateSize", 0); |
| 1337 | histogram_tester.ExpectTotalCount("ClientHints.PersistDuration", 0); |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | // Loads a HTTPS webpage that does not request persisting of client hints. |
| 1341 | // An iframe loaded by the webpage from an cross origin server requests |
| 1342 | // persistence of client hints. |
| 1343 | // Verify that the request from the cross origin iframe is not honored, and |
| 1344 | // client hints preference is not persisted. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1345 | IN_PROC_BROWSER_TEST_P(ClientHintsBrowserTest, |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 1346 | DisregardPersistenceRequestIframe_CrossOrigin) { |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1347 | const GURL gurl = |
| 1348 | GetParam() ? http_equiv_accept_ch_without_lifetime_with_iframe_url() |
| 1349 | : accept_ch_without_lifetime_with_iframe_url(); |
| 1350 | |
| 1351 | intercept_iframe_resource_ = gurl.path(); |
| 1352 | intercept_to_http_equiv_iframe_ = GetParam(); |
| 1353 | |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 1354 | base::HistogramTester histogram_tester; |
| 1355 | ContentSettingsForOneType host_settings; |
| 1356 | |
| 1357 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1358 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, std::string(), |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 1359 | &host_settings); |
| 1360 | EXPECT_EQ(0u, host_settings.size()); |
| 1361 | |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1362 | ui_test_utils::NavigateToURL(browser(), gurl); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 1363 | |
| 1364 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
| 1365 | |
| 1366 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 1367 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 1368 | |
| 1369 | // accept_ch_without_lifetime_with_iframe_url() loads |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 1370 | // accept_ch_with_lifetime() in a cross origin iframe. The request to persist |
| 1371 | // client hints from accept_ch_with_lifetime() should be disregarded. |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 1372 | histogram_tester.ExpectTotalCount("ClientHints.UpdateSize", 0); |
| 1373 | histogram_tester.ExpectTotalCount("ClientHints.PersistDuration", 0); |
| 1374 | } |
| 1375 | |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 1376 | // Loads a HTTPS webpage that does not request persisting of client hints. |
| 1377 | // A subresource loaded by the webpage requests persistence of client hints. |
| 1378 | // Verify that the request from the subresource is not honored, and client hints |
| 1379 | // preference is not persisted. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1380 | IN_PROC_BROWSER_TEST_P(ClientHintsBrowserTest, |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 1381 | DisregardPersistenceRequestSubresource) { |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1382 | const GURL gurl = |
| 1383 | GetParam() ? http_equiv_accept_ch_without_lifetime_with_subresource_url() |
| 1384 | : accept_ch_without_lifetime_with_subresource_url(); |
| 1385 | |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 1386 | base::HistogramTester histogram_tester; |
| 1387 | ContentSettingsForOneType host_settings; |
| 1388 | |
| 1389 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1390 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, std::string(), |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 1391 | &host_settings); |
| 1392 | EXPECT_EQ(0u, host_settings.size()); |
| 1393 | |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1394 | ui_test_utils::NavigateToURL(browser(), gurl); |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 1395 | |
| 1396 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
| 1397 | |
| 1398 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 1399 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 1400 | |
| 1401 | // accept_ch_without_lifetime_with_subresource_url() loads |
| 1402 | // accept_ch_with_lifetime() as a subresource. The request to persist client |
| 1403 | // hints from accept_ch_with_lifetime() should be disregarded. |
| 1404 | histogram_tester.ExpectTotalCount("ClientHints.UpdateSize", 0); |
| 1405 | histogram_tester.ExpectTotalCount("ClientHints.PersistDuration", 0); |
| 1406 | } |
| 1407 | |
| 1408 | // Loads a HTTPS webpage that does not request persisting of client hints. |
| 1409 | // A subresource loaded by the webpage in an iframe requests persistence of |
| 1410 | // client hints. Verify that the request from the subresource in the iframe |
| 1411 | // is not honored, and client hints preference is not persisted. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1412 | IN_PROC_BROWSER_TEST_P(ClientHintsBrowserTest, |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 1413 | DisregardPersistenceRequestSubresourceIframe) { |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1414 | const GURL gurl = |
| 1415 | GetParam() |
| 1416 | ? http_equiv_accept_ch_without_lifetime_with_subresource_iframe_url() |
| 1417 | : accept_ch_without_lifetime_with_subresource_iframe_url(); |
| 1418 | |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 1419 | base::HistogramTester histogram_tester; |
| 1420 | ContentSettingsForOneType host_settings; |
| 1421 | |
| 1422 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1423 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, std::string(), |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 1424 | &host_settings); |
| 1425 | EXPECT_EQ(0u, host_settings.size()); |
| 1426 | |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1427 | ui_test_utils::NavigateToURL(browser(), gurl); |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 1428 | |
| 1429 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
| 1430 | |
| 1431 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 1432 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 1433 | |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1434 | // |gurl| loads accept_ch_with_lifetime() or |
| 1435 | // http_equiv_accept_ch_with_lifetime() as a subresource in an iframe. The |
| 1436 | // request to persist client hints from accept_ch_with_lifetime() or |
| 1437 | // http_equiv_accept_ch_with_lifetime() should be disregarded. |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 1438 | histogram_tester.ExpectTotalCount("ClientHints.UpdateSize", 0); |
| 1439 | histogram_tester.ExpectTotalCount("ClientHints.PersistDuration", 0); |
| 1440 | } |
| 1441 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 1442 | // Loads a HTTP local webpage (which qualifies as a secure context) that |
| 1443 | // requests persisting of client hints. Verifies that the browser receives the |
| 1444 | // mojo notification from the renderer and persists the client hints to the |
| 1445 | // disk. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1446 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 1447 | ClientHintsLifetimeFollowedByNoClientHintHttpLocal) { |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1448 | const GURL gurl = accept_ch_with_lifetime_http_local_url(); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1449 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 1450 | base::HistogramTester histogram_tester; |
| 1451 | ContentSettingsForOneType host_settings; |
| 1452 | |
| 1453 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1454 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, std::string(), |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 1455 | &host_settings); |
| 1456 | EXPECT_EQ(0u, host_settings.size()); |
| 1457 | |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1458 | ui_test_utils::NavigateToURL(browser(), gurl); |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 1459 | |
| 1460 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 1461 | |
| 1462 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 1463 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 1464 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1465 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", |
| 1466 | expected_client_hints_number, 1); |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1467 | // |gurl| sets client hints persist duration to 3600 seconds, but a maximum |
| 1468 | // value is registered instead. |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 1469 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1470 | uma_histogram_max_value, 1); |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 1471 | |
| 1472 | base::RunLoop().RunUntilIdle(); |
| 1473 | |
| 1474 | // Clients hints preferences for one origin should be persisted. |
| 1475 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1476 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, std::string(), |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 1477 | &host_settings); |
| 1478 | EXPECT_EQ(1u, host_settings.size()); |
| 1479 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1480 | SetClientHintExpectationsOnMainFrame(true); |
| 1481 | SetClientHintExpectationsOnSubresources(true); |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 1482 | ui_test_utils::NavigateToURL(browser(), |
| 1483 | without_accept_ch_without_lifetime_local_url()); |
| 1484 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1485 | // The user agent hint is attached to all three requests: |
| 1486 | EXPECT_EQ(3u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1487 | EXPECT_EQ(3u, count_ua_mobile_client_hints_headers_seen()); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1488 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1489 | // Expected number of hints attached to the image request, and the same number |
| 1490 | // to the main frame request. |
| 1491 | EXPECT_EQ(expected_client_hints_number * 2, |
| 1492 | count_client_hints_headers_seen()); |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 1493 | } |
| 1494 | |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 1495 | // Loads a webpage that does not request persisting of client hints. |
| 1496 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, NoClientHintsHttps) { |
| 1497 | base::HistogramTester histogram_tester; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1498 | ui_test_utils::NavigateToURL(browser(), |
| 1499 | without_accept_ch_without_lifetime_url()); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 1500 | |
| 1501 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
| 1502 | |
| 1503 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 1504 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 1505 | |
| 1506 | // no_client_hints_url() does not sets the client hints. |
| 1507 | histogram_tester.ExpectTotalCount("ClientHints.UpdateSize", 0); |
| 1508 | histogram_tester.ExpectTotalCount("ClientHints.PersistDuration", 0); |
| 1509 | } |
| 1510 | |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1511 | IN_PROC_BROWSER_TEST_P(ClientHintsBrowserTest, |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1512 | ClientHintsLifetimeFollowedByNoClientHint) { |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1513 | const GURL gurl = GetParam() ? http_equiv_accept_ch_with_lifetime() |
| 1514 | : accept_ch_with_lifetime_url(); |
| 1515 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1516 | base::HistogramTester histogram_tester; |
| 1517 | ContentSettingsForOneType host_settings; |
| 1518 | |
| 1519 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1520 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, std::string(), |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1521 | &host_settings); |
| 1522 | EXPECT_EQ(0u, host_settings.size()); |
| 1523 | |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1524 | // Fetching |gurl| should persist the request for client hints iff using |
| 1525 | // headers and not http-equiv. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1526 | ui_test_utils::NavigateToURL(browser(), gurl); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1527 | |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1528 | if (GetParam()) |
| 1529 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
| 1530 | else |
| 1531 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1532 | |
| 1533 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 1534 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1535 | base::RunLoop().RunUntilIdle(); |
| 1536 | |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1537 | if (GetParam()) { |
| 1538 | histogram_tester.ExpectTotalCount("ClientHints.UpdateSize", 0); |
| 1539 | histogram_tester.ExpectTotalCount("ClientHints.PersistDuration", 0); |
| 1540 | } else { |
| 1541 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", |
| 1542 | expected_client_hints_number, 1); |
| 1543 | // accept_ch_with_lifetime_url() sets client hints persist duration to 3600 |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1544 | // seconds, but a maximum value is registered instead. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1545 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1546 | uma_histogram_max_value, 1); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1547 | |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1548 | // Clients hints preferences for one origin should be persisted. |
| 1549 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 1550 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, |
| 1551 | std::string(), &host_settings); |
| 1552 | EXPECT_EQ(1u, host_settings.size()); |
| 1553 | } |
| 1554 | |
| 1555 | SetClientHintExpectationsOnMainFrame(!GetParam()); |
| 1556 | SetClientHintExpectationsOnSubresources(!GetParam()); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1557 | ui_test_utils::NavigateToURL(browser(), |
| 1558 | without_accept_ch_without_lifetime_url()); |
Tarun Bansal | 6bf5430 | 2017-10-02 07:39:14 | [diff] [blame] | 1559 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1560 | // The user agent hint is attached to all three requests: |
| 1561 | EXPECT_EQ(3u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1562 | EXPECT_EQ(3u, count_ua_mobile_client_hints_headers_seen()); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1563 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1564 | // Expected number of hints attached to the image request, and the same number |
| 1565 | // to the main frame request. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1566 | EXPECT_EQ(GetParam() ? 0 : expected_client_hints_number * 2, |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1567 | count_client_hints_headers_seen()); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1568 | } |
| 1569 | |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 1570 | // The test first fetches a page that sets Accept-CH-Lifetime. Next, it fetches |
| 1571 | // a URL from a different origin. However, that URL response redirects to the |
| 1572 | // same origin from where the first page was fetched. The test verifies that |
| 1573 | // on receiving redirect to an origin for which the browser has persisted client |
| 1574 | // hints prefs, the browser attaches the client hints headers when fetching the |
| 1575 | // redirected URL. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1576 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 1577 | ClientHintsLifetimeFollowedByRedirectToNoClientHint) { |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1578 | const GURL gurl = accept_ch_with_lifetime_url(); |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 1579 | |
| 1580 | base::HistogramTester histogram_tester; |
| 1581 | ContentSettingsForOneType host_settings; |
| 1582 | |
| 1583 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1584 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, std::string(), |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 1585 | &host_settings); |
| 1586 | EXPECT_EQ(0u, host_settings.size()); |
| 1587 | |
| 1588 | // Fetching |gurl| should persist the request for client hints. |
| 1589 | ui_test_utils::NavigateToURL(browser(), gurl); |
| 1590 | |
| 1591 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 1592 | |
| 1593 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 1594 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 1595 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1596 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", |
| 1597 | expected_client_hints_number, 1); |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 1598 | // accept_ch_with_lifetime_url() sets client hints persist duration to 3600 |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1599 | // seconds, but a maximum value is registered instead. |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 1600 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1601 | uma_histogram_max_value, 1); |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 1602 | base::RunLoop().RunUntilIdle(); |
| 1603 | |
| 1604 | // Clients hints preferences for one origin should be persisted. |
| 1605 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1606 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, std::string(), |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 1607 | &host_settings); |
| 1608 | EXPECT_EQ(1u, host_settings.size()); |
| 1609 | |
| 1610 | SetClientHintExpectationsOnMainFrame(true); |
| 1611 | SetClientHintExpectationsOnSubresources(true); |
| 1612 | ui_test_utils::NavigateToURL(browser(), redirect_url()); |
| 1613 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1614 | // The user agent hint is attached to all three requests: |
| 1615 | EXPECT_EQ(3u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1616 | EXPECT_EQ(3u, count_ua_mobile_client_hints_headers_seen()); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1617 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1618 | // Expected number of hints attached to the image request, and the same number |
| 1619 | // to the main frame request. |
| 1620 | EXPECT_EQ(expected_client_hints_number * 2, |
| 1621 | count_client_hints_headers_seen()); |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 1622 | } |
| 1623 | |
Tarun Bansal | a0c1fc3 | 2018-10-03 16:14:52 | [diff] [blame] | 1624 | // Ensure that even when cookies are blocked, client hint preferences are |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1625 | // persisted. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1626 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
Tarun Bansal | a0c1fc3 | 2018-10-03 16:14:52 | [diff] [blame] | 1627 | ClientHintsLifetimePersistedCookiesBlocked) { |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1628 | const GURL gurl_with = accept_ch_with_lifetime_url(); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1629 | |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1630 | scoped_refptr<content_settings::CookieSettings> cookie_settings_ = |
| 1631 | CookieSettingsFactory::GetForProfile(browser()->profile()); |
| 1632 | base::HistogramTester histogram_tester; |
| 1633 | ContentSettingsForOneType host_settings; |
| 1634 | |
| 1635 | // Block cookies. |
| 1636 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Tarun Bansal | a0c1fc3 | 2018-10-03 16:14:52 | [diff] [blame] | 1637 | ->SetContentSettingDefaultScope(gurl_with, GURL(), |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1638 | ContentSettingsType::COOKIES, |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1639 | std::string(), CONTENT_SETTING_BLOCK); |
| 1640 | |
Tarun Bansal | a0c1fc3 | 2018-10-03 16:14:52 | [diff] [blame] | 1641 | // Fetching |gurl_with| should persist the request for client hints. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1642 | ui_test_utils::NavigateToURL(browser(), gurl_with); |
Tarun Bansal | a0c1fc3 | 2018-10-03 16:14:52 | [diff] [blame] | 1643 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 1); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1644 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1645 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, std::string(), |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1646 | &host_settings); |
| 1647 | EXPECT_EQ(1u, host_settings.size()); |
Tarun Bansal | a0c1fc3 | 2018-10-03 16:14:52 | [diff] [blame] | 1648 | VerifyContentSettingsNotNotified(); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1649 | } |
| 1650 | |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1651 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
Tarun Bansal | a0c1fc3 | 2018-10-03 16:14:52 | [diff] [blame] | 1652 | ClientHintsLifetimeAttachedCookiesBlocked) { |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1653 | const GURL gurl_with = accept_ch_with_lifetime_url(); |
| 1654 | const GURL gurl_without = accept_ch_without_lifetime_url(); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1655 | base::HistogramTester histogram_tester; |
| 1656 | ContentSettingsForOneType host_settings; |
| 1657 | |
| 1658 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1659 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, std::string(), |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1660 | &host_settings); |
| 1661 | EXPECT_EQ(0u, host_settings.size()); |
| 1662 | |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1663 | // Fetching |gurl_with| should persist the request for client hints. |
| 1664 | ui_test_utils::NavigateToURL(browser(), gurl_with); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1665 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 1666 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 1667 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1668 | base::RunLoop().RunUntilIdle(); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1669 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1670 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", |
| 1671 | expected_client_hints_number, 1); |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1672 | // |gurl_with| tries to set client hints persist duration to 3600 seconds, but |
| 1673 | // a maximum value is registered instead. |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1674 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1675 | uma_histogram_max_value, 1); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1676 | |
| 1677 | // Clients hints preferences for one origin should be persisted. |
| 1678 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1679 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, std::string(), |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1680 | &host_settings); |
| 1681 | EXPECT_EQ(1u, host_settings.size()); |
| 1682 | |
Tarun Bansal | a0c1fc3 | 2018-10-03 16:14:52 | [diff] [blame] | 1683 | // Block the cookies: Client hints should be attached. |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1684 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1685 | ->SetContentSettingDefaultScope(gurl_without, GURL(), |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1686 | ContentSettingsType::COOKIES, |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1687 | std::string(), CONTENT_SETTING_BLOCK); |
| 1688 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1689 | SetClientHintExpectationsOnMainFrame(true); |
| 1690 | SetClientHintExpectationsOnSubresources(true); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1691 | ui_test_utils::NavigateToURL(browser(), |
| 1692 | without_accept_ch_without_lifetime_url()); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 1693 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1694 | // The user agent hint is attached to all three requests: |
| 1695 | EXPECT_EQ(3u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1696 | EXPECT_EQ(3u, count_ua_mobile_client_hints_headers_seen()); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1697 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1698 | // Expected number of hints attached to the image request, and the same number |
| 1699 | // to the main frame request. |
| 1700 | EXPECT_EQ(expected_client_hints_number * 2, |
| 1701 | count_client_hints_headers_seen()); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1702 | |
| 1703 | // Clear settings. |
| 1704 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1705 | ->ClearSettingsForOneType(ContentSettingsType::COOKIES); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | // Ensure that when the JavaScript is blocked, client hint preferences are not |
| 1709 | // persisted. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1710 | IN_PROC_BROWSER_TEST_P(ClientHintsBrowserTest, |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1711 | ClientHintsLifetimeNotPersistedJavaScriptBlocked) { |
| 1712 | ContentSettingsForOneType host_settings; |
| 1713 | |
| 1714 | // Start a navigation. This navigation makes it possible to block JavaScript |
| 1715 | // later. |
| 1716 | ui_test_utils::NavigateToURL(browser(), |
| 1717 | without_accept_ch_without_lifetime_url()); |
| 1718 | |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1719 | const GURL gurl = |
| 1720 | GetParam() ? http_equiv_accept_ch_without_lifetime_with_iframe_url() |
| 1721 | : accept_ch_with_lifetime_url(); |
| 1722 | |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1723 | // Block the JavaScript: Client hint preferences should not be persisted. |
| 1724 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1725 | ->SetContentSettingDefaultScope(gurl, GURL(), |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1726 | ContentSettingsType::JAVASCRIPT, |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1727 | std::string(), CONTENT_SETTING_BLOCK); |
| 1728 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 1729 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1730 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, std::string(), |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1731 | &host_settings); |
| 1732 | EXPECT_EQ(0u, host_settings.size()); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 1733 | VerifyContentSettingsNotNotified(); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1734 | |
| 1735 | // Allow the JavaScript: Client hint preferences should be persisted. |
| 1736 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1737 | ->SetContentSettingDefaultScope(gurl, GURL(), |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1738 | ContentSettingsType::JAVASCRIPT, |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1739 | std::string(), CONTENT_SETTING_ALLOW); |
| 1740 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 1741 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1742 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, std::string(), |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1743 | &host_settings); |
| 1744 | EXPECT_EQ(1u, host_settings.size()); |
Tarun Bansal | 59379011 | 2018-03-20 04:53:34 | [diff] [blame] | 1745 | |
| 1746 | // Clear settings. |
| 1747 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1748 | ->ClearSettingsForOneType(ContentSettingsType::JAVASCRIPT); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | // Ensure that when the JavaScript is blocked, persisted client hints are not |
| 1752 | // attached to the request headers. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1753 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1754 | ClientHintsLifetimeNotAttachedJavaScriptBlocked) { |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1755 | const GURL gurl = accept_ch_with_lifetime_url(); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1756 | |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1757 | base::HistogramTester histogram_tester; |
| 1758 | ContentSettingsForOneType host_settings; |
| 1759 | |
| 1760 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1761 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, std::string(), |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1762 | &host_settings); |
| 1763 | EXPECT_EQ(0u, host_settings.size()); |
| 1764 | |
| 1765 | // Fetching accept_ch_with_lifetime_url() should persist the request for |
| 1766 | // client hints. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1767 | ui_test_utils::NavigateToURL(browser(), gurl); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1768 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 1769 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 1770 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1771 | EXPECT_EQ(1u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1772 | EXPECT_EQ(1u, count_ua_mobile_client_hints_headers_seen()); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1773 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1774 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", |
| 1775 | expected_client_hints_number, 1); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1776 | // accept_ch_with_lifetime_url() tries to set client hints persist duration to |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1777 | // 3600 seconds, but a maximum value is registered instead. |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1778 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1779 | uma_histogram_max_value, 1); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1780 | base::RunLoop().RunUntilIdle(); |
| 1781 | |
| 1782 | // Clients hints preferences for one origin should be persisted. |
| 1783 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1784 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, std::string(), |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1785 | &host_settings); |
| 1786 | EXPECT_EQ(1u, host_settings.size()); |
| 1787 | |
Changwan Ryu | 434c3a3 | 2019-07-30 23:42:58 | [diff] [blame] | 1788 | // Block JavaScript via WebPreferences: Client hints should not be attached. |
| 1789 | SetJsEnabledForActiveView(false); |
| 1790 | |
| 1791 | SetClientHintExpectationsOnMainFrame(false); |
| 1792 | SetClientHintExpectationsOnSubresources(false); |
| 1793 | ui_test_utils::NavigateToURL(browser(), |
| 1794 | without_accept_ch_without_lifetime_url()); |
| 1795 | |
| 1796 | EXPECT_EQ(0u, count_client_hints_headers_seen()); |
| 1797 | VerifyContentSettingsNotNotified(); |
| 1798 | EXPECT_EQ(1u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1799 | EXPECT_EQ(1u, count_ua_mobile_client_hints_headers_seen()); |
Changwan Ryu | 434c3a3 | 2019-07-30 23:42:58 | [diff] [blame] | 1800 | |
| 1801 | SetJsEnabledForActiveView(true); |
| 1802 | |
| 1803 | // Block JavaScript via ContentSetting: Client hints should not be attached. |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1804 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 1805 | ->SetContentSettingDefaultScope(without_accept_ch_without_lifetime_url(), |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1806 | GURL(), ContentSettingsType::JAVASCRIPT, |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1807 | std::string(), CONTENT_SETTING_BLOCK); |
| 1808 | ui_test_utils::NavigateToURL(browser(), |
| 1809 | without_accept_ch_without_lifetime_url()); |
| 1810 | EXPECT_EQ(0u, count_client_hints_headers_seen()); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 1811 | VerifyContentSettingsNotNotified(); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1812 | EXPECT_EQ(1u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1813 | EXPECT_EQ(1u, count_ua_mobile_client_hints_headers_seen()); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1814 | |
Changwan Ryu | 434c3a3 | 2019-07-30 23:42:58 | [diff] [blame] | 1815 | // Allow JavaScript: Client hints should now be attached. |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1816 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 1817 | ->SetContentSettingDefaultScope(without_accept_ch_without_lifetime_url(), |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1818 | GURL(), ContentSettingsType::JAVASCRIPT, |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1819 | std::string(), CONTENT_SETTING_ALLOW); |
| 1820 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1821 | SetClientHintExpectationsOnMainFrame(true); |
| 1822 | SetClientHintExpectationsOnSubresources(true); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1823 | ui_test_utils::NavigateToURL(browser(), |
| 1824 | without_accept_ch_without_lifetime_url()); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 1825 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1826 | // The user agent hint is attached to all three requests: |
| 1827 | EXPECT_EQ(3u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1828 | EXPECT_EQ(3u, count_ua_mobile_client_hints_headers_seen()); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1829 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1830 | // Expected number of hints attached to the image request, and the same number |
| 1831 | // to the main frame request. |
| 1832 | EXPECT_EQ(expected_client_hints_number * 2, |
| 1833 | count_client_hints_headers_seen()); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1834 | |
| 1835 | // Clear settings. |
| 1836 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1837 | ->ClearSettingsForOneType(ContentSettingsType::JAVASCRIPT); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1838 | } |
| 1839 | |
Tarun Bansal | 73502f9 | 2019-04-16 21:21:19 | [diff] [blame] | 1840 | // Test that if the content settings are malformed, then the browser does not |
| 1841 | // crash. |
| 1842 | IN_PROC_BROWSER_TEST_P(ClientHintsBrowserTest, |
| 1843 | ClientHintsMalformedContentSettings) { |
| 1844 | ContentSettingsForOneType client_hints_settings; |
| 1845 | HostContentSettingsMap* host_content_settings_map = |
| 1846 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()); |
| 1847 | |
| 1848 | // Add setting for the host. |
| 1849 | std::unique_ptr<base::ListValue> expiration_times_list = |
| 1850 | std::make_unique<base::ListValue>(); |
| 1851 | expiration_times_list->AppendInteger(42 /* client hint value */); |
| 1852 | auto expiration_times_dictionary = std::make_unique<base::DictionaryValue>(); |
| 1853 | expiration_times_dictionary->SetList("client_hints", |
| 1854 | std::move(expiration_times_list)); |
Aaron Tagliaboschi | 3c96a68 | 2019-10-29 18:10:28 | [diff] [blame] | 1855 | expiration_times_dictionary->SetDouble( |
| 1856 | "expiration_time", |
| 1857 | (base::Time::Now() + base::TimeDelta::FromDays(1)).ToDoubleT()); |
Tarun Bansal | 73502f9 | 2019-04-16 21:21:19 | [diff] [blame] | 1858 | host_content_settings_map->SetWebsiteSettingDefaultScope( |
| 1859 | without_accept_ch_without_lifetime_url(), GURL(), |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1860 | ContentSettingsType::CLIENT_HINTS, std::string(), |
Tarun Bansal | 73502f9 | 2019-04-16 21:21:19 | [diff] [blame] | 1861 | std::make_unique<base::Value>(expiration_times_dictionary->Clone())); |
| 1862 | |
| 1863 | // Reading the settings should now return one setting. |
| 1864 | host_content_settings_map->GetSettingsForOneType( |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1865 | ContentSettingsType::CLIENT_HINTS, std::string(), &client_hints_settings); |
Tarun Bansal | 73502f9 | 2019-04-16 21:21:19 | [diff] [blame] | 1866 | EXPECT_EQ(1U, client_hints_settings.size()); |
| 1867 | |
| 1868 | SetClientHintExpectationsOnMainFrame(false); |
| 1869 | SetClientHintExpectationsOnSubresources(false); |
| 1870 | ui_test_utils::NavigateToURL(browser(), |
| 1871 | without_accept_ch_without_lifetime_url()); |
| 1872 | } |
| 1873 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1874 | // Ensure that when the JavaScript is blocked, client hints requested using |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 1875 | // Accept-CH are not attached to the request headers for subresources. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1876 | IN_PROC_BROWSER_TEST_P(ClientHintsBrowserTest, |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1877 | ClientHintsNoLifetimeScriptNotAllowed) { |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1878 | const GURL gurl = GetParam() ? http_equiv_accept_ch_with_lifetime() |
| 1879 | : accept_ch_with_lifetime_url(); |
| 1880 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1881 | base::HistogramTester histogram_tester; |
| 1882 | ContentSettingsForOneType host_settings; |
| 1883 | |
| 1884 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1885 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, std::string(), |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1886 | &host_settings); |
| 1887 | EXPECT_EQ(0u, host_settings.size()); |
| 1888 | |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 1889 | // Block the Javascript: Client hints should not be attached. |
| 1890 | SetClientHintExpectationsOnSubresources(false); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1891 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 1892 | ->SetContentSettingDefaultScope( |
| 1893 | accept_ch_without_lifetime_img_localhost(), GURL(), |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1894 | ContentSettingsType::JAVASCRIPT, std::string(), |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1895 | CONTENT_SETTING_BLOCK); |
| 1896 | ui_test_utils::NavigateToURL(browser(), |
| 1897 | accept_ch_without_lifetime_img_localhost()); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1898 | EXPECT_EQ(0u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1899 | EXPECT_EQ(0u, count_ua_mobile_client_hints_headers_seen()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 1900 | EXPECT_EQ(0u, count_client_hints_headers_seen()); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1901 | EXPECT_EQ(1u, third_party_request_count_seen()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 1902 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1903 | |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 1904 | // Allow the Javascript: Client hints should now be attached. |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1905 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 1906 | ->SetContentSettingDefaultScope( |
| 1907 | accept_ch_without_lifetime_img_localhost(), GURL(), |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1908 | ContentSettingsType::JAVASCRIPT, std::string(), |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1909 | CONTENT_SETTING_ALLOW); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1910 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1911 | SetClientHintExpectationsOnSubresources(true); |
| 1912 | ui_test_utils::NavigateToURL(browser(), |
| 1913 | accept_ch_without_lifetime_img_localhost()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 1914 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1915 | EXPECT_EQ(2u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1916 | EXPECT_EQ(2u, count_ua_mobile_client_hints_headers_seen()); |
Aaron Tagliaboschi | 150fb904 | 2020-05-14 22:20:37 | [diff] [blame] | 1917 | EXPECT_EQ(expected_client_hints_number, count_client_hints_headers_seen()); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1918 | EXPECT_EQ(2u, third_party_request_count_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1919 | EXPECT_EQ(2u, third_party_client_hints_count_seen()); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 1920 | VerifyContentSettingsNotNotified(); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1921 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1922 | // Clear settings. |
| 1923 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1924 | ->ClearSettingsForOneType(ContentSettingsType::JAVASCRIPT); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1925 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1926 | // Block the Javascript again: Client hints should not be attached. |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 1927 | SetClientHintExpectationsOnSubresources(false); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1928 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 1929 | ->SetContentSettingDefaultScope( |
| 1930 | accept_ch_without_lifetime_img_localhost(), GURL(), |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1931 | ContentSettingsType::JAVASCRIPT, std::string(), |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1932 | CONTENT_SETTING_BLOCK); |
| 1933 | ui_test_utils::NavigateToURL(browser(), |
| 1934 | accept_ch_without_lifetime_img_localhost()); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1935 | EXPECT_EQ(2u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1936 | EXPECT_EQ(2u, count_ua_mobile_client_hints_headers_seen()); |
Aaron Tagliaboschi | 150fb904 | 2020-05-14 22:20:37 | [diff] [blame] | 1937 | EXPECT_EQ(expected_client_hints_number, count_client_hints_headers_seen()); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1938 | EXPECT_EQ(3u, third_party_request_count_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1939 | EXPECT_EQ(2u, third_party_client_hints_count_seen()); |
Tarun Bansal | 59379011 | 2018-03-20 04:53:34 | [diff] [blame] | 1940 | |
| 1941 | // Clear settings. |
| 1942 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1943 | ->ClearSettingsForOneType(ContentSettingsType::JAVASCRIPT); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1944 | } |
| 1945 | |
Tarun Bansal | a0c1fc3 | 2018-10-03 16:14:52 | [diff] [blame] | 1946 | // Ensure that when the cookies is blocked, client hints are attached to the |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 1947 | // request headers. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1948 | IN_PROC_BROWSER_TEST_P(ClientHintsBrowserTest, |
Tarun Bansal | a0c1fc3 | 2018-10-03 16:14:52 | [diff] [blame] | 1949 | ClientHintsLifetimeCookiesNotAllowed) { |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1950 | const GURL gurl = GetParam() |
| 1951 | ? http_equiv_accept_ch_without_lifetime_img_localhost() |
| 1952 | : accept_ch_without_lifetime_img_localhost(); |
| 1953 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1954 | base::HistogramTester histogram_tester; |
| 1955 | ContentSettingsForOneType host_settings; |
| 1956 | scoped_refptr<content_settings::CookieSettings> cookie_settings_ = |
| 1957 | CookieSettingsFactory::GetForProfile(browser()->profile()); |
| 1958 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1959 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1960 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, std::string(), |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1961 | &host_settings); |
| 1962 | EXPECT_EQ(0u, host_settings.size()); |
| 1963 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1964 | // Block cookies. |
| 1965 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1966 | ->SetContentSettingDefaultScope(gurl, GURL(), |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1967 | ContentSettingsType::COOKIES, |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1968 | std::string(), CONTENT_SETTING_BLOCK); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1969 | base::RunLoop().RunUntilIdle(); |
| 1970 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1971 | SetClientHintExpectationsOnSubresources(true); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1972 | ui_test_utils::NavigateToURL(browser(), gurl); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1973 | EXPECT_EQ(2u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1974 | EXPECT_EQ(2u, count_ua_mobile_client_hints_headers_seen()); |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1975 | EXPECT_EQ(expected_client_hints_number, count_client_hints_headers_seen()); |
Tarun Bansal | a0c1fc3 | 2018-10-03 16:14:52 | [diff] [blame] | 1976 | EXPECT_EQ(1u, third_party_request_count_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1977 | EXPECT_EQ(2u, third_party_client_hints_count_seen()); |
Tarun Bansal | 59379011 | 2018-03-20 04:53:34 | [diff] [blame] | 1978 | |
| 1979 | // Clear settings. |
| 1980 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1981 | ->ClearSettingsForOneType(ContentSettingsType::COOKIES); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1982 | } |
| 1983 | |
Tarun Bansal | 3ce0ca4 | 2018-06-25 22:52:22 | [diff] [blame] | 1984 | // Verify that client hints are sent in the incognito profiles, and server |
| 1985 | // client hint opt-ins are honored within the incognito profile. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1986 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
Tarun Bansal | 3ce0ca4 | 2018-06-25 22:52:22 | [diff] [blame] | 1987 | ClientHintsLifetimeFollowedByNoClientHintIncognito) { |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1988 | const GURL gurl = accept_ch_with_lifetime_url(); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1989 | |
Tarun Bansal | 3ce0ca4 | 2018-06-25 22:52:22 | [diff] [blame] | 1990 | base::HistogramTester histogram_tester; |
| 1991 | Browser* incognito = CreateIncognitoBrowser(); |
| 1992 | ContentSettingsForOneType host_settings; |
| 1993 | |
| 1994 | HostContentSettingsMapFactory::GetForProfile(incognito->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1995 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, std::string(), |
Tarun Bansal | 3ce0ca4 | 2018-06-25 22:52:22 | [diff] [blame] | 1996 | &host_settings); |
| 1997 | EXPECT_EQ(0u, host_settings.size()); |
| 1998 | |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1999 | // Fetching |gurl| should persist the request for client hints. |
| 2000 | ui_test_utils::NavigateToURL(incognito, gurl); |
Tarun Bansal | 3ce0ca4 | 2018-06-25 22:52:22 | [diff] [blame] | 2001 | |
| 2002 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 2003 | |
| 2004 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 2005 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | 3ce0ca4 | 2018-06-25 22:52:22 | [diff] [blame] | 2006 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 2007 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", |
| 2008 | expected_client_hints_number, 1); |
Tarun Bansal | 3ce0ca4 | 2018-06-25 22:52:22 | [diff] [blame] | 2009 | // accept_ch_with_lifetime_url() sets client hints persist duration to 3600 |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 2010 | // seconds, but a maximum value is registered instead. |
Tarun Bansal | 3ce0ca4 | 2018-06-25 22:52:22 | [diff] [blame] | 2011 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 2012 | uma_histogram_max_value, 1); |
Tarun Bansal | 3ce0ca4 | 2018-06-25 22:52:22 | [diff] [blame] | 2013 | base::RunLoop().RunUntilIdle(); |
| 2014 | |
| 2015 | // Clients hints preferences for one origin should be persisted. |
| 2016 | HostContentSettingsMapFactory::GetForProfile(incognito->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 2017 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, std::string(), |
Tarun Bansal | 3ce0ca4 | 2018-06-25 22:52:22 | [diff] [blame] | 2018 | &host_settings); |
| 2019 | EXPECT_EQ(1u, host_settings.size()); |
| 2020 | |
| 2021 | SetClientHintExpectationsOnMainFrame(true); |
| 2022 | SetClientHintExpectationsOnSubresources(true); |
| 2023 | ui_test_utils::NavigateToURL(incognito, |
| 2024 | without_accept_ch_without_lifetime_url()); |
| 2025 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 2026 | // The user agent hint is attached to all three requests: |
| 2027 | EXPECT_EQ(3u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 2028 | EXPECT_EQ(3u, count_ua_mobile_client_hints_headers_seen()); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 2029 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 2030 | // Expected number of hints attached to the image request, and the same number |
| 2031 | // to the main frame request. |
| 2032 | EXPECT_EQ(expected_client_hints_number * 2, |
| 2033 | count_client_hints_headers_seen()); |
Tarun Bansal | 3ce0ca4 | 2018-06-25 22:52:22 | [diff] [blame] | 2034 | |
| 2035 | // Navigate using regular profile. Client hints should not be send. |
| 2036 | SetClientHintExpectationsOnMainFrame(false); |
| 2037 | SetClientHintExpectationsOnSubresources(false); |
| 2038 | ui_test_utils::NavigateToURL(browser(), |
| 2039 | without_accept_ch_without_lifetime_url()); |
| 2040 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 2041 | // The user agent hint is attached to the two new requests. |
| 2042 | EXPECT_EQ(5u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 2043 | EXPECT_EQ(5u, count_ua_mobile_client_hints_headers_seen()); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 2044 | |
| 2045 | // No additional hints are sent. |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 2046 | EXPECT_EQ(expected_client_hints_number * 2, |
| 2047 | count_client_hints_headers_seen()); |
Tarun Bansal | 3ce0ca4 | 2018-06-25 22:52:22 | [diff] [blame] | 2048 | } |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 2049 | |
Aaron Tagliaboschi | 8f3a2830 | 2020-06-24 03:08:26 | [diff] [blame] | 2050 | class ClientHintsEnterprisePolicyTest : public ClientHintsBrowserTest { |
| 2051 | void SetUpInProcessBrowserTestFixture() override { |
| 2052 | policy::PolicyTest::SetUpInProcessBrowserTestFixture(); |
| 2053 | policy::PolicyMap policies; |
| 2054 | SetPolicy(&policies, policy::key::kUserAgentClientHintsEnabled, |
Aya ElAttar | e881174 | 2020-07-20 08:24:37 | [diff] [blame] | 2055 | base::Value(false)); |
Aaron Tagliaboschi | 8f3a2830 | 2020-06-24 03:08:26 | [diff] [blame] | 2056 | provider_.UpdateChromePolicy(policies); |
| 2057 | } |
| 2058 | }; |
| 2059 | |
| 2060 | // Makes sure that no client hints are sent by default when the |
| 2061 | // "UserAgentClientHintsEnabled" enterprise polickly is set to |
| 2062 | // false |
| 2063 | IN_PROC_BROWSER_TEST_F(ClientHintsEnterprisePolicyTest, |
| 2064 | ClientHintsEnterprisePolicy) { |
| 2065 | const GURL gurl = accept_ch_without_lifetime_url(); |
| 2066 | ui_test_utils::NavigateToURL(browser(), gurl); |
| 2067 | // These would normally be one each |
| 2068 | EXPECT_EQ(0u, count_user_agent_hint_headers_seen()); |
| 2069 | EXPECT_EQ(0u, count_ua_mobile_client_hints_headers_seen()); |
| 2070 | } |
| 2071 | |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 2072 | class ClientHintsWebHoldbackBrowserTest : public ClientHintsBrowserTest { |
| 2073 | public: |
| 2074 | ClientHintsWebHoldbackBrowserTest() : ClientHintsBrowserTest() { |
| 2075 | ConfigureHoldbackExperiment(); |
| 2076 | } |
| 2077 | |
| 2078 | net::EffectiveConnectionType web_effective_connection_type_override() const { |
| 2079 | return web_effective_connection_type_override_; |
| 2080 | } |
| 2081 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 2082 | std::unique_ptr<base::FeatureList> EnabledFeatures() override { |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 2083 | base::FieldTrialParamAssociator::GetInstance()->ClearAllParamsForTesting(); |
| 2084 | const std::string kTrialName = "TrialFoo"; |
| 2085 | const std::string kGroupName = "GroupFoo"; // Value not used |
| 2086 | |
| 2087 | scoped_refptr<base::FieldTrial> trial = |
| 2088 | base::FieldTrialList::CreateFieldTrial(kTrialName, kGroupName); |
| 2089 | |
| 2090 | std::map<std::string, std::string> params; |
| 2091 | |
| 2092 | params["web_effective_connection_type_override"] = |
| 2093 | net::GetNameForEffectiveConnectionType( |
| 2094 | web_effective_connection_type_override_); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 2095 | EXPECT_TRUE( |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 2096 | base::FieldTrialParamAssociator::GetInstance() |
| 2097 | ->AssociateFieldTrialParams(kTrialName, kGroupName, params)); |
| 2098 | |
| 2099 | std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
Maks Orlovich | c66745a | 2020-06-30 17:40:02 | [diff] [blame] | 2100 | feature_list->InitializeFromCommandLine( |
| 2101 | "UserAgentClientHint,LangClientHintHeader", ""); |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 2102 | feature_list->RegisterFieldTrialOverride( |
| 2103 | features::kNetworkQualityEstimatorWebHoldback.name, |
| 2104 | base::FeatureList::OVERRIDE_ENABLE_FEATURE, trial.get()); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 2105 | return feature_list; |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 2106 | } |
| 2107 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 2108 | private: |
| 2109 | void ConfigureHoldbackExperiment() {} |
| 2110 | |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 2111 | const net::EffectiveConnectionType web_effective_connection_type_override_ = |
| 2112 | net::EFFECTIVE_CONNECTION_TYPE_3G; |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 2113 | }; |
| 2114 | |
| 2115 | // Make sure that when NetInfo holdback experiment is enabled, the NetInfo APIs |
| 2116 | // and client hints return the overridden values. Verify that the client hints |
| 2117 | // are overridden on both main frame and subresource requests. |
| 2118 | IN_PROC_BROWSER_TEST_F(ClientHintsWebHoldbackBrowserTest, |
| 2119 | EffectiveConnectionTypeChangeNotified) { |
| 2120 | SetExpectedEffectiveConnectionType(web_effective_connection_type_override()); |
| 2121 | |
| 2122 | SetClientHintExpectationsOnMainFrame(false); |
| 2123 | SetClientHintExpectationsOnSubresources(true); |
| 2124 | |
| 2125 | base::RunLoop().RunUntilIdle(); |
| 2126 | |
| 2127 | EXPECT_TRUE(embedded_test_server()->Start()); |
| 2128 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 2129 | EXPECT_EQ(0u, count_client_hints_headers_seen()); |
| 2130 | EXPECT_EQ(0u, third_party_request_count_seen()); |
| 2131 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
| 2132 | |
| 2133 | SetClientHintExpectationsOnMainFrame(true); |
| 2134 | SetClientHintExpectationsOnSubresources(true); |
| 2135 | ui_test_utils::NavigateToURL( |
| 2136 | browser(), accept_ch_without_lifetime_with_subresource_url()); |
| 2137 | base::RunLoop().RunUntilIdle(); |
| 2138 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 2139 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 2140 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 2141 | EXPECT_EQ(3u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 2142 | EXPECT_EQ(3u, count_ua_mobile_client_hints_headers_seen()); |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 2143 | EXPECT_EQ(expected_client_hints_number * 2, |
| 2144 | count_client_hints_headers_seen()); |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 2145 | EXPECT_EQ(0u, third_party_request_count_seen()); |
| 2146 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
| 2147 | } |