| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/signin/core/browser/signin_header_helper.h" |
| 6 |
| 5 #include <memory> | 7 #include <memory> |
| 8 #include <string> |
| 6 | 9 |
| 7 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 8 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "build/build_config.h" |
| 9 #include "components/content_settings/core/browser/cookie_settings.h" | 13 #include "components/content_settings/core/browser/cookie_settings.h" |
| 10 #include "components/signin/core/browser/signin_header_helper.h" | 14 #include "components/signin/core/browser/chrome_connected_header_helper.h" |
| 11 #include "components/signin/core/common/profile_management_switches.h" | 15 #include "components/signin/core/common/profile_management_switches.h" |
| 12 #include "components/sync_preferences/testing_pref_service_syncable.h" | 16 #include "components/sync_preferences/testing_pref_service_syncable.h" |
| 17 #include "google_apis/gaia/gaia_urls.h" |
| 13 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" | 18 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
| 14 #include "net/url_request/url_request_test_util.h" | 19 #include "net/url_request/url_request_test_util.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 17 | 22 |
| 23 #if !defined(OS_IOS) && !defined(OS_ANDROID) |
| 24 #include "components/signin/core/browser/dice_header_helper.h" |
| 25 #endif |
| 26 |
| 18 class SigninHeaderHelperTest : public testing::Test { | 27 class SigninHeaderHelperTest : public testing::Test { |
| 19 protected: | 28 protected: |
| 20 void SetUp() override { | 29 void SetUp() override { |
| 21 content_settings::CookieSettings::RegisterProfilePrefs(prefs_.registry()); | 30 content_settings::CookieSettings::RegisterProfilePrefs(prefs_.registry()); |
| 22 HostContentSettingsMap::RegisterProfilePrefs(prefs_.registry()); | 31 HostContentSettingsMap::RegisterProfilePrefs(prefs_.registry()); |
| 23 | 32 |
| 24 settings_map_ = new HostContentSettingsMap( | 33 settings_map_ = new HostContentSettingsMap( |
| 25 &prefs_, false /* incognito_profile */, false /* guest_profile */, | 34 &prefs_, false /* incognito_profile */, false /* guest_profile */, |
| 26 false /* store_last_modified */); | 35 false /* store_last_modified */); |
| 27 cookie_settings_ = | 36 cookie_settings_ = |
| 28 new content_settings::CookieSettings(settings_map_.get(), &prefs_, ""); | 37 new content_settings::CookieSettings(settings_map_.get(), &prefs_, ""); |
| 29 } | 38 } |
| 30 | 39 |
| 31 void TearDown() override { settings_map_->ShutdownOnUIThread(); } | 40 void TearDown() override { settings_map_->ShutdownOnUIThread(); } |
| 32 | 41 |
| 33 void CheckMirrorCookieRequest(const GURL& url, | 42 void CheckMirrorCookieRequest(const GURL& url, |
| 34 const std::string& account_id, | 43 const std::string& account_id, |
| 35 const std::string& expected_request) { | 44 const std::string& expected_request) { |
| 36 EXPECT_EQ(signin::BuildMirrorRequestCookieIfPossible( | 45 EXPECT_EQ(signin::BuildMirrorRequestCookieIfPossible( |
| 37 url, account_id, cookie_settings_.get(), | 46 url, account_id, cookie_settings_.get(), |
| 38 signin::PROFILE_MODE_DEFAULT), | 47 signin::PROFILE_MODE_DEFAULT), |
| 39 expected_request); | 48 expected_request); |
| 40 } | 49 } |
| 41 | 50 |
| 51 std::unique_ptr<net::URLRequest> CreateRequest( |
| 52 const GURL& url, |
| 53 const std::string& account_id) { |
| 54 std::unique_ptr<net::URLRequest> url_request = |
| 55 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr, |
| 56 TRAFFIC_ANNOTATION_FOR_TESTS); |
| 57 signin::AppendOrRemoveAccountConsistentyRequestHeader( |
| 58 url_request.get(), GURL(), account_id, cookie_settings_.get(), |
| 59 signin::PROFILE_MODE_DEFAULT); |
| 60 return url_request; |
| 61 } |
| 62 |
| 63 void CheckAccountConsistencyHeaderRequest( |
| 64 net::URLRequest* url_request, |
| 65 const char* header_name, |
| 66 const std::string& expected_request) { |
| 67 bool expected_result = !expected_request.empty(); |
| 68 std::string request; |
| 69 EXPECT_EQ( |
| 70 url_request->extra_request_headers().GetHeader(header_name, &request), |
| 71 expected_result); |
| 72 if (expected_result) { |
| 73 EXPECT_EQ(expected_request, request); |
| 74 } |
| 75 } |
| 76 |
| 42 void CheckMirrorHeaderRequest(const GURL& url, | 77 void CheckMirrorHeaderRequest(const GURL& url, |
| 43 const std::string& account_id, | 78 const std::string& account_id, |
| 44 const std::string& expected_request) { | 79 const std::string& expected_request) { |
| 45 bool expected_result = !expected_request.empty(); | |
| 46 std::unique_ptr<net::URLRequest> url_request = | 80 std::unique_ptr<net::URLRequest> url_request = |
| 47 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr, | 81 CreateRequest(url, account_id); |
| 48 TRAFFIC_ANNOTATION_FOR_TESTS); | 82 CheckAccountConsistencyHeaderRequest( |
| 49 EXPECT_EQ(signin::AppendOrRemoveAccountConsistentyRequestHeader( | 83 url_request.get(), signin::kChromeConnectedHeader, expected_request); |
| 50 url_request.get(), GURL(), account_id, cookie_settings_.get(), | |
| 51 signin::PROFILE_MODE_DEFAULT), | |
| 52 expected_result); | |
| 53 std::string request; | |
| 54 EXPECT_EQ(url_request->extra_request_headers().GetHeader( | |
| 55 signin::kChromeConnectedHeader, &request), | |
| 56 expected_result); | |
| 57 if (expected_result) { | |
| 58 EXPECT_EQ(expected_request, request); | |
| 59 } | |
| 60 } | 84 } |
| 61 | 85 |
| 86 #if !defined(OS_IOS) && !defined(OS_ANDROID) |
| 87 void CheckDiceHeaderRequest(const GURL& url, |
| 88 const std::string& account_id, |
| 89 const std::string& expected_mirror_request, |
| 90 const std::string& expected_dice_request) { |
| 91 std::unique_ptr<net::URLRequest> url_request = |
| 92 CreateRequest(url, account_id); |
| 93 CheckAccountConsistencyHeaderRequest(url_request.get(), |
| 94 signin::kChromeConnectedHeader, |
| 95 expected_mirror_request); |
| 96 CheckAccountConsistencyHeaderRequest( |
| 97 url_request.get(), signin::kDiceRequestHeader, expected_dice_request); |
| 98 } |
| 99 #endif |
| 100 |
| 62 base::MessageLoop loop_; | 101 base::MessageLoop loop_; |
| 63 | 102 |
| 64 sync_preferences::TestingPrefServiceSyncable prefs_; | 103 sync_preferences::TestingPrefServiceSyncable prefs_; |
| 65 net::TestURLRequestContext url_request_context_; | 104 net::TestURLRequestContext url_request_context_; |
| 66 | 105 |
| 67 scoped_refptr<HostContentSettingsMap> settings_map_; | 106 scoped_refptr<HostContentSettingsMap> settings_map_; |
| 68 scoped_refptr<content_settings::CookieSettings> cookie_settings_; | 107 scoped_refptr<content_settings::CookieSettings> cookie_settings_; |
| 69 }; | 108 }; |
| 70 | 109 |
| 71 // Tests that no Mirror request is returned when the user is not signed in (no | 110 // Tests that no Mirror request is returned when the user is not signed in (no |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 TEST_F(SigninHeaderHelperTest, TestMirrorRequestGoogleCom) { | 150 TEST_F(SigninHeaderHelperTest, TestMirrorRequestGoogleCom) { |
| 112 switches::EnableAccountConsistencyMirrorForTesting( | 151 switches::EnableAccountConsistencyMirrorForTesting( |
| 113 base::CommandLine::ForCurrentProcess()); | 152 base::CommandLine::ForCurrentProcess()); |
| 114 CheckMirrorHeaderRequest(GURL("https://www.google.com"), "0123456789", | 153 CheckMirrorHeaderRequest(GURL("https://www.google.com"), "0123456789", |
| 115 "mode=0,enable_account_consistency=true"); | 154 "mode=0,enable_account_consistency=true"); |
| 116 CheckMirrorCookieRequest( | 155 CheckMirrorCookieRequest( |
| 117 GURL("https://www.google.com"), "0123456789", | 156 GURL("https://www.google.com"), "0123456789", |
| 118 "id=0123456789:mode=0:enable_account_consistency=true"); | 157 "id=0123456789:mode=0:enable_account_consistency=true"); |
| 119 } | 158 } |
| 120 | 159 |
| 160 // Mirror is always enabled on Android and iOS, so these tests are only relevant |
| 161 // on Desktop. |
| 162 #if !defined(OS_ANDROID) && !defined(OS_IOS) |
| 163 |
| 164 // Tests that the Mirror request is returned when the target is a Gaia URL, even |
| 165 // if account consistency is disabled. |
| 166 TEST_F(SigninHeaderHelperTest, TestMirrorRequestGaiaURL) { |
| 167 ASSERT_FALSE(switches::IsAccountConsistencyMirrorEnabled()); |
| 168 CheckMirrorHeaderRequest(GURL("https://accounts.google.com"), "0123456789", |
| 169 "mode=0,enable_account_consistency=false"); |
| 170 CheckMirrorCookieRequest( |
| 171 GURL("https://accounts.google.com"), "0123456789", |
| 172 "id=0123456789:mode=0:enable_account_consistency=false"); |
| 173 } |
| 174 |
| 175 // Tests Dice requests. |
| 176 TEST_F(SigninHeaderHelperTest, TestDiceRequest) { |
| 177 switches::EnableAccountConsistencyDiceForTesting( |
| 178 base::CommandLine::ForCurrentProcess()); |
| 179 // ChromeConnected but no Dice for Docs URLs. |
| 180 CheckDiceHeaderRequest( |
| 181 GURL("https://docs.google.com"), "0123456789", |
| 182 "id=0123456789,mode=0,enable_account_consistency=false", ""); |
| 183 |
| 184 // ChromeConnected and Dice for Gaia URLs. |
| 185 std::string client_id = GaiaUrls::GetInstance()->oauth2_chrome_client_id(); |
| 186 ASSERT_FALSE(client_id.empty()); |
| 187 CheckDiceHeaderRequest(GURL("https://accounts.google.com"), "0123456789", |
| 188 "mode=0,enable_account_consistency=false", |
| 189 "client_id=" + client_id); |
| 190 |
| 191 // No ChromeConnected and no Dice for other URLs. |
| 192 CheckDiceHeaderRequest(GURL("https://www.google.com"), "0123456789", "", ""); |
| 193 } |
| 194 |
| 195 // Tests that no Dice request is returned when Dice is not enabled. |
| 196 TEST_F(SigninHeaderHelperTest, TestNoDiceRequestWhenDisabled) { |
| 197 switches::EnableAccountConsistencyMirrorForTesting( |
| 198 base::CommandLine::ForCurrentProcess()); |
| 199 CheckDiceHeaderRequest(GURL("https://accounts.google.com"), "0123456789", |
| 200 "mode=0,enable_account_consistency=true", ""); |
| 201 } |
| 202 |
| 121 // Tests that the Mirror request is returned with the GAIA Id on Drive origin, | 203 // Tests that the Mirror request is returned with the GAIA Id on Drive origin, |
| 122 // even if account consistency is disabled. | 204 // even if account consistency is disabled. |
| 123 // | |
| 124 // Account consistency if always enabled on Android and iOS, so this test is | |
| 125 // only relevant on Desktop. | |
| 126 #if !defined(OS_ANDROID) && !defined(OS_IOS) | |
| 127 TEST_F(SigninHeaderHelperTest, TestMirrorRequestDrive) { | 205 TEST_F(SigninHeaderHelperTest, TestMirrorRequestDrive) { |
| 128 DCHECK(!switches::IsAccountConsistencyMirrorEnabled()); | 206 ASSERT_FALSE(switches::IsAccountConsistencyMirrorEnabled()); |
| 129 CheckMirrorHeaderRequest( | 207 CheckMirrorHeaderRequest( |
| 130 GURL("https://docs.google.com/document"), "0123456789", | 208 GURL("https://docs.google.com/document"), "0123456789", |
| 131 "id=0123456789,mode=0,enable_account_consistency=false"); | 209 "id=0123456789,mode=0,enable_account_consistency=false"); |
| 132 CheckMirrorCookieRequest( | 210 CheckMirrorCookieRequest( |
| 133 GURL("https://drive.google.com/drive"), "0123456789", | 211 GURL("https://drive.google.com/drive"), "0123456789", |
| 134 "id=0123456789:mode=0:enable_account_consistency=false"); | 212 "id=0123456789:mode=0:enable_account_consistency=false"); |
| 135 | 213 |
| 136 // Enable Account Consistency will override the disable. | 214 // Enable Account Consistency will override the disable. |
| 137 switches::EnableAccountConsistencyMirrorForTesting( | 215 switches::EnableAccountConsistencyMirrorForTesting( |
| 138 base::CommandLine::ForCurrentProcess()); | 216 base::CommandLine::ForCurrentProcess()); |
| 139 CheckMirrorHeaderRequest( | 217 CheckMirrorHeaderRequest( |
| 140 GURL("https://docs.google.com/document"), "0123456789", | 218 GURL("https://docs.google.com/document"), "0123456789", |
| 141 "id=0123456789,mode=0,enable_account_consistency=true"); | 219 "id=0123456789,mode=0,enable_account_consistency=true"); |
| 142 CheckMirrorCookieRequest( | 220 CheckMirrorCookieRequest( |
| 143 GURL("https://drive.google.com/drive"), "0123456789", | 221 GURL("https://drive.google.com/drive"), "0123456789", |
| 144 "id=0123456789:mode=0:enable_account_consistency=true"); | 222 "id=0123456789:mode=0:enable_account_consistency=true"); |
| 145 } | 223 } |
| 146 #endif | 224 |
| 225 #endif // !defined(OS_ANDROID) && !defined(OS_IOS) |
| 147 | 226 |
| 148 // Tests that the Mirror header request is returned normally when the redirect | 227 // Tests that the Mirror header request is returned normally when the redirect |
| 149 // URL is eligible. | 228 // URL is eligible. |
| 150 TEST_F(SigninHeaderHelperTest, TestMirrorHeaderEligibleRedirectURL) { | 229 TEST_F(SigninHeaderHelperTest, TestMirrorHeaderEligibleRedirectURL) { |
| 151 switches::EnableAccountConsistencyMirrorForTesting( | 230 switches::EnableAccountConsistencyMirrorForTesting( |
| 152 base::CommandLine::ForCurrentProcess()); | 231 base::CommandLine::ForCurrentProcess()); |
| 153 const GURL url("https://docs.google.com/document"); | 232 const GURL url("https://docs.google.com/document"); |
| 154 const GURL redirect_url("https://www.google.com"); | 233 const GURL redirect_url("https://www.google.com"); |
| 155 const std::string account_id = "0123456789"; | 234 const std::string account_id = "0123456789"; |
| 156 std::unique_ptr<net::URLRequest> url_request = | 235 std::unique_ptr<net::URLRequest> url_request = |
| 157 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr, | 236 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr, |
| 158 TRAFFIC_ANNOTATION_FOR_TESTS); | 237 TRAFFIC_ANNOTATION_FOR_TESTS); |
| 159 EXPECT_TRUE(signin::AppendOrRemoveAccountConsistentyRequestHeader( | 238 signin::AppendOrRemoveAccountConsistentyRequestHeader( |
| 160 url_request.get(), redirect_url, account_id, cookie_settings_.get(), | 239 url_request.get(), redirect_url, account_id, cookie_settings_.get(), |
| 161 signin::PROFILE_MODE_DEFAULT)); | 240 signin::PROFILE_MODE_DEFAULT); |
| 162 EXPECT_TRUE(url_request->extra_request_headers().HasHeader( | 241 EXPECT_TRUE(url_request->extra_request_headers().HasHeader( |
| 163 signin::kChromeConnectedHeader)); | 242 signin::kChromeConnectedHeader)); |
| 164 } | 243 } |
| 165 | 244 |
| 166 // Tests that the Mirror header request is stripped when the redirect URL is not | 245 // Tests that the Mirror header request is stripped when the redirect URL is not |
| 167 // eligible. | 246 // eligible. |
| 168 TEST_F(SigninHeaderHelperTest, TestMirrorHeaderNonEligibleRedirectURL) { | 247 TEST_F(SigninHeaderHelperTest, TestMirrorHeaderNonEligibleRedirectURL) { |
| 169 switches::EnableAccountConsistencyMirrorForTesting( | 248 switches::EnableAccountConsistencyMirrorForTesting( |
| 170 base::CommandLine::ForCurrentProcess()); | 249 base::CommandLine::ForCurrentProcess()); |
| 171 const GURL url("https://docs.google.com/document"); | 250 const GURL url("https://docs.google.com/document"); |
| 172 const GURL redirect_url("http://www.foo.com"); | 251 const GURL redirect_url("http://www.foo.com"); |
| 173 const std::string account_id = "0123456789"; | 252 const std::string account_id = "0123456789"; |
| 174 std::unique_ptr<net::URLRequest> url_request = | 253 std::unique_ptr<net::URLRequest> url_request = |
| 175 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr, | 254 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr, |
| 176 TRAFFIC_ANNOTATION_FOR_TESTS); | 255 TRAFFIC_ANNOTATION_FOR_TESTS); |
| 177 EXPECT_FALSE(signin::AppendOrRemoveAccountConsistentyRequestHeader( | 256 signin::AppendOrRemoveAccountConsistentyRequestHeader( |
| 178 url_request.get(), redirect_url, account_id, cookie_settings_.get(), | 257 url_request.get(), redirect_url, account_id, cookie_settings_.get(), |
| 179 signin::PROFILE_MODE_DEFAULT)); | 258 signin::PROFILE_MODE_DEFAULT); |
| 180 EXPECT_FALSE(url_request->extra_request_headers().HasHeader( | 259 EXPECT_FALSE(url_request->extra_request_headers().HasHeader( |
| 181 signin::kChromeConnectedHeader)); | 260 signin::kChromeConnectedHeader)); |
| 182 } | 261 } |
| 183 | 262 |
| 184 // Tests that the Mirror header, whatever its value is, is untouched when both | 263 // Tests that the Mirror header, whatever its value is, is untouched when both |
| 185 // the current and the redirect URL are non-eligible. | 264 // the current and the redirect URL are non-eligible. |
| 186 TEST_F(SigninHeaderHelperTest, TestIgnoreMirrorHeaderNonEligibleURLs) { | 265 TEST_F(SigninHeaderHelperTest, TestIgnoreMirrorHeaderNonEligibleURLs) { |
| 187 switches::EnableAccountConsistencyMirrorForTesting( | 266 switches::EnableAccountConsistencyMirrorForTesting( |
| 188 base::CommandLine::ForCurrentProcess()); | 267 base::CommandLine::ForCurrentProcess()); |
| 189 const GURL url("https://www.bar.com"); | 268 const GURL url("https://www.bar.com"); |
| 190 const GURL redirect_url("http://www.foo.com"); | 269 const GURL redirect_url("http://www.foo.com"); |
| 191 const std::string account_id = "0123456789"; | 270 const std::string account_id = "0123456789"; |
| 192 const std::string fake_header = "foo,bar"; | 271 const std::string fake_header = "foo,bar"; |
| 193 std::unique_ptr<net::URLRequest> url_request = | 272 std::unique_ptr<net::URLRequest> url_request = |
| 194 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr, | 273 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr, |
| 195 TRAFFIC_ANNOTATION_FOR_TESTS); | 274 TRAFFIC_ANNOTATION_FOR_TESTS); |
| 196 url_request->SetExtraRequestHeaderByName(signin::kChromeConnectedHeader, | 275 url_request->SetExtraRequestHeaderByName(signin::kChromeConnectedHeader, |
| 197 fake_header, false); | 276 fake_header, false); |
| 198 EXPECT_FALSE(signin::AppendOrRemoveAccountConsistentyRequestHeader( | 277 signin::AppendOrRemoveAccountConsistentyRequestHeader( |
| 199 url_request.get(), redirect_url, account_id, cookie_settings_.get(), | 278 url_request.get(), redirect_url, account_id, cookie_settings_.get(), |
| 200 signin::PROFILE_MODE_DEFAULT)); | 279 signin::PROFILE_MODE_DEFAULT); |
| 201 std::string header; | 280 std::string header; |
| 202 EXPECT_TRUE(url_request->extra_request_headers().GetHeader( | 281 EXPECT_TRUE(url_request->extra_request_headers().GetHeader( |
| 203 signin::kChromeConnectedHeader, &header)); | 282 signin::kChromeConnectedHeader, &header)); |
| 204 EXPECT_EQ(fake_header, header); | 283 EXPECT_EQ(fake_header, header); |
| 205 } | 284 } |
| OLD | NEW |