| 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" | |
| 11 #include "components/signin/core/common/profile_management_switches.h" | 14 #include "components/signin/core/common/profile_management_switches.h" |
| 12 #include "components/sync_preferences/testing_pref_service_syncable.h" | 15 #include "components/sync_preferences/testing_pref_service_syncable.h" |
| 16 #include "google_apis/gaia/gaia_urls.h" |
| 13 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" | 17 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
| 14 #include "net/url_request/url_request_test_util.h" | 18 #include "net/url_request/url_request_test_util.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 17 | 21 |
| 18 class SigninHeaderHelperTest : public testing::Test { | 22 class SigninHeaderHelperTest : public testing::Test { |
| 19 protected: | 23 protected: |
| 20 void SetUp() override { | 24 void SetUp() override { |
| 21 content_settings::CookieSettings::RegisterProfilePrefs(prefs_.registry()); | 25 content_settings::CookieSettings::RegisterProfilePrefs(prefs_.registry()); |
| 22 HostContentSettingsMap::RegisterProfilePrefs(prefs_.registry()); | 26 HostContentSettingsMap::RegisterProfilePrefs(prefs_.registry()); |
| 23 | 27 |
| 24 settings_map_ = new HostContentSettingsMap( | 28 settings_map_ = new HostContentSettingsMap( |
| 25 &prefs_, false /* incognito_profile */, false /* guest_profile */, | 29 &prefs_, false /* incognito_profile */, false /* guest_profile */, |
| 26 false /* store_last_modified */); | 30 false /* store_last_modified */); |
| 27 cookie_settings_ = | 31 cookie_settings_ = |
| 28 new content_settings::CookieSettings(settings_map_.get(), &prefs_, ""); | 32 new content_settings::CookieSettings(settings_map_.get(), &prefs_, ""); |
| 29 } | 33 } |
| 30 | 34 |
| 31 void TearDown() override { settings_map_->ShutdownOnUIThread(); } | 35 void TearDown() override { settings_map_->ShutdownOnUIThread(); } |
| 32 | 36 |
| 33 void CheckMirrorCookieRequest(const GURL& url, | 37 void CheckMirrorCookieRequest(const GURL& url, |
| 34 const std::string& account_id, | 38 const std::string& account_id, |
| 35 const std::string& expected_request) { | 39 const std::string& expected_request) { |
| 36 EXPECT_EQ(signin::BuildMirrorRequestCookieIfPossible( | 40 EXPECT_EQ(signin::BuildMirrorRequestCookieIfPossible( |
| 37 url, account_id, cookie_settings_.get(), | 41 url, account_id, cookie_settings_.get(), |
| 38 signin::PROFILE_MODE_DEFAULT), | 42 signin::PROFILE_MODE_DEFAULT), |
| 39 expected_request); | 43 expected_request); |
| 40 } | 44 } |
| 41 | 45 |
| 42 void CheckMirrorHeaderRequest(const GURL& url, | 46 void CheckAccountConsistencyHeaderRequest( |
| 43 const std::string& account_id, | 47 const GURL& url, |
| 44 const std::string& expected_request) { | 48 const std::string& account_id, |
| 49 const char* header_name, |
| 50 const std::string& expected_request) { |
| 45 bool expected_result = !expected_request.empty(); | 51 bool expected_result = !expected_request.empty(); |
| 46 std::unique_ptr<net::URLRequest> url_request = | 52 std::unique_ptr<net::URLRequest> url_request = |
| 47 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr, | 53 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr, |
| 48 TRAFFIC_ANNOTATION_FOR_TESTS); | 54 TRAFFIC_ANNOTATION_FOR_TESTS); |
| 49 EXPECT_EQ(signin::AppendOrRemoveAccountConsistentyRequestHeader( | 55 EXPECT_EQ(signin::AppendOrRemoveAccountConsistentyRequestHeader( |
| 50 url_request.get(), GURL(), account_id, cookie_settings_.get(), | 56 url_request.get(), GURL(), account_id, cookie_settings_.get(), |
| 51 signin::PROFILE_MODE_DEFAULT), | 57 signin::PROFILE_MODE_DEFAULT), |
| 52 expected_result); | 58 expected_result); |
| 53 std::string request; | 59 std::string request; |
| 54 EXPECT_EQ(url_request->extra_request_headers().GetHeader( | 60 EXPECT_EQ( |
| 55 signin::kChromeConnectedHeader, &request), | 61 url_request->extra_request_headers().GetHeader(header_name, &request), |
| 56 expected_result); | 62 expected_result); |
| 57 if (expected_result) { | 63 if (expected_result) { |
| 58 EXPECT_EQ(expected_request, request); | 64 EXPECT_EQ(expected_request, request); |
| 59 } | 65 } |
| 60 } | 66 } |
| 61 | 67 |
| 68 void CheckMirrorHeaderRequest(const GURL& url, |
| 69 const std::string& account_id, |
| 70 const std::string& expected_request) { |
| 71 return CheckAccountConsistencyHeaderRequest( |
| 72 url, account_id, signin::kChromeConnectedHeader, expected_request); |
| 73 } |
| 74 |
| 75 void CheckDiceHeaderRequest(const GURL& url, |
| 76 const std::string& expected_request) { |
| 77 return CheckAccountConsistencyHeaderRequest( |
| 78 url, "", signin::kChromeIDConsistencyRequestHeader, expected_request); |
| 79 } |
| 80 |
| 62 base::MessageLoop loop_; | 81 base::MessageLoop loop_; |
| 63 | 82 |
| 64 sync_preferences::TestingPrefServiceSyncable prefs_; | 83 sync_preferences::TestingPrefServiceSyncable prefs_; |
| 65 net::TestURLRequestContext url_request_context_; | 84 net::TestURLRequestContext url_request_context_; |
| 66 | 85 |
| 67 scoped_refptr<HostContentSettingsMap> settings_map_; | 86 scoped_refptr<HostContentSettingsMap> settings_map_; |
| 68 scoped_refptr<content_settings::CookieSettings> cookie_settings_; | 87 scoped_refptr<content_settings::CookieSettings> cookie_settings_; |
| 69 }; | 88 }; |
| 70 | 89 |
| 71 // Tests that no Mirror request is returned when the user is not signed in (no | 90 // 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) { | 130 TEST_F(SigninHeaderHelperTest, TestMirrorRequestGoogleCom) { |
| 112 switches::EnableAccountConsistencyMirrorForTesting( | 131 switches::EnableAccountConsistencyMirrorForTesting( |
| 113 base::CommandLine::ForCurrentProcess()); | 132 base::CommandLine::ForCurrentProcess()); |
| 114 CheckMirrorHeaderRequest(GURL("https://www.google.com"), "0123456789", | 133 CheckMirrorHeaderRequest(GURL("https://www.google.com"), "0123456789", |
| 115 "mode=0,enable_account_consistency=true"); | 134 "mode=0,enable_account_consistency=true"); |
| 116 CheckMirrorCookieRequest( | 135 CheckMirrorCookieRequest( |
| 117 GURL("https://www.google.com"), "0123456789", | 136 GURL("https://www.google.com"), "0123456789", |
| 118 "id=0123456789:mode=0:enable_account_consistency=true"); | 137 "id=0123456789:mode=0:enable_account_consistency=true"); |
| 119 } | 138 } |
| 120 | 139 |
| 140 // Tests that the Mirror request is returned when the target is a Gaia URL, even |
| 141 // if account consistency is disabled. |
| 142 TEST_F(SigninHeaderHelperTest, TestMirrorRequestGaiaURL) { |
| 143 ASSERT_FALSE(switches::IsAccountConsistencyMirrorEnabled()); |
| 144 CheckMirrorHeaderRequest(GURL("https://accounts.google.com"), "0123456789", |
| 145 "mode=0,enable_account_consistency=false"); |
| 146 CheckMirrorCookieRequest( |
| 147 GURL("https://accounts.google.com"), "0123456789", |
| 148 "id=0123456789:mode=0:enable_account_consistency=false"); |
| 149 } |
| 150 |
| 151 // DICe is only supported on desktop. |
| 152 #if !defined(OS_ANDROID) && !defined(OS_IOS) |
| 153 // Tests that no DICe request is returned when the target is not a Gaia URL. |
| 154 TEST_F(SigninHeaderHelperTest, TestNoDiceRequestNoGaiaURL) { |
| 155 switches::EnableAccountConsistencyDiceForTesting( |
| 156 base::CommandLine::ForCurrentProcess()); |
| 157 CheckDiceHeaderRequest(GURL("https://docs.google.com"), ""); |
| 158 } |
| 159 |
| 160 // Tests that a DICe request is returned when the target is a Gaia URL. |
| 161 TEST_F(SigninHeaderHelperTest, TestDiceRequestGaiaURL) { |
| 162 switches::EnableAccountConsistencyDiceForTesting( |
| 163 base::CommandLine::ForCurrentProcess()); |
| 164 std::string client_id = GaiaUrls::GetInstance()->oauth2_chrome_client_id(); |
| 165 ASSERT_FALSE(client_id.empty()); |
| 166 CheckDiceHeaderRequest(GURL("https://accounts.google.com"), |
| 167 "client_id=" + client_id); |
| 168 } |
| 169 #endif |
| 170 |
| 121 // Tests that the Mirror request is returned with the GAIA Id on Drive origin, | 171 // Tests that the Mirror request is returned with the GAIA Id on Drive origin, |
| 122 // even if account consistency is disabled. | 172 // even if account consistency is disabled. |
| 123 // | 173 // |
| 124 // Account consistency if always enabled on Android and iOS, so this test is | 174 // Account consistency if always enabled on Android and iOS, so this test is |
| 125 // only relevant on Desktop. | 175 // only relevant on Desktop. |
| 126 #if !defined(OS_ANDROID) && !defined(OS_IOS) | 176 #if !defined(OS_ANDROID) && !defined(OS_IOS) |
| 127 TEST_F(SigninHeaderHelperTest, TestMirrorRequestDrive) { | 177 TEST_F(SigninHeaderHelperTest, TestMirrorRequestDrive) { |
| 128 DCHECK(!switches::IsAccountConsistencyMirrorEnabled()); | 178 ASSERT_FALSE(switches::IsAccountConsistencyMirrorEnabled()); |
| 129 CheckMirrorHeaderRequest( | 179 CheckMirrorHeaderRequest( |
| 130 GURL("https://docs.google.com/document"), "0123456789", | 180 GURL("https://docs.google.com/document"), "0123456789", |
| 131 "id=0123456789,mode=0,enable_account_consistency=false"); | 181 "id=0123456789,mode=0,enable_account_consistency=false"); |
| 132 CheckMirrorCookieRequest( | 182 CheckMirrorCookieRequest( |
| 133 GURL("https://drive.google.com/drive"), "0123456789", | 183 GURL("https://drive.google.com/drive"), "0123456789", |
| 134 "id=0123456789:mode=0:enable_account_consistency=false"); | 184 "id=0123456789:mode=0:enable_account_consistency=false"); |
| 135 | 185 |
| 136 // Enable Account Consistency will override the disable. | 186 // Enable Account Consistency will override the disable. |
| 137 switches::EnableAccountConsistencyMirrorForTesting( | 187 switches::EnableAccountConsistencyMirrorForTesting( |
| 138 base::CommandLine::ForCurrentProcess()); | 188 base::CommandLine::ForCurrentProcess()); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 url_request->SetExtraRequestHeaderByName(signin::kChromeConnectedHeader, | 246 url_request->SetExtraRequestHeaderByName(signin::kChromeConnectedHeader, |
| 197 fake_header, false); | 247 fake_header, false); |
| 198 EXPECT_FALSE(signin::AppendOrRemoveAccountConsistentyRequestHeader( | 248 EXPECT_FALSE(signin::AppendOrRemoveAccountConsistentyRequestHeader( |
| 199 url_request.get(), redirect_url, account_id, cookie_settings_.get(), | 249 url_request.get(), redirect_url, account_id, cookie_settings_.get(), |
| 200 signin::PROFILE_MODE_DEFAULT)); | 250 signin::PROFILE_MODE_DEFAULT)); |
| 201 std::string header; | 251 std::string header; |
| 202 EXPECT_TRUE(url_request->extra_request_headers().GetHeader( | 252 EXPECT_TRUE(url_request->extra_request_headers().GetHeader( |
| 203 signin::kChromeConnectedHeader, &header)); | 253 signin::kChromeConnectedHeader, &header)); |
| 204 EXPECT_EQ(fake_header, header); | 254 EXPECT_EQ(fake_header, header); |
| 205 } | 255 } |
| OLD | NEW |