OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <memory> |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/message_loop/message_loop.h" |
| 9 #include "components/content_settings/core/browser/cookie_settings.h" |
| 10 #include "components/pref_registry/testing_pref_service_syncable.h" |
| 11 #include "components/signin/core/browser/signin_header_helper.h" |
| 12 #include "components/signin/core/common/signin_switches.h" |
| 13 #include "net/url_request/url_request_test_util.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "url/gurl.h" |
| 16 |
| 17 class SigninHeaderHelperTest : public testing::Test { |
| 18 protected: |
| 19 void SetUp() override { |
| 20 content_settings::CookieSettings::RegisterProfilePrefs(prefs_.registry()); |
| 21 HostContentSettingsMap::RegisterProfilePrefs(prefs_.registry()); |
| 22 |
| 23 settings_map_ = new HostContentSettingsMap( |
| 24 &prefs_, false /* incognito_profile */, false /* guest_profile */); |
| 25 cookie_settings_ = |
| 26 new content_settings::CookieSettings(settings_map_.get(), &prefs_, ""); |
| 27 } |
| 28 |
| 29 void TearDown() override { settings_map_->ShutdownOnUIThread(); } |
| 30 |
| 31 void CheckMirrorCookieRequest(const GURL& url, |
| 32 const std::string& account_id, |
| 33 const std::string& expected_request) { |
| 34 EXPECT_EQ(signin::BuildMirrorRequestCookieIfPossible( |
| 35 url, account_id, cookie_settings_.get(), |
| 36 signin::PROFILE_MODE_DEFAULT), |
| 37 expected_request); |
| 38 } |
| 39 |
| 40 void CheckMirrorHeaderRequest(const GURL& url, |
| 41 const std::string& account_id, |
| 42 const std::string& expected_request) { |
| 43 bool expected_result = !expected_request.empty(); |
| 44 std::unique_ptr<net::URLRequest> url_request = |
| 45 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr); |
| 46 EXPECT_EQ(signin::AppendOrRemoveMirrorRequestHeaderIfPossible( |
| 47 url_request.get(), GURL(), account_id, cookie_settings_.get(), |
| 48 signin::PROFILE_MODE_DEFAULT), |
| 49 expected_result); |
| 50 std::string request; |
| 51 EXPECT_EQ(url_request->extra_request_headers().GetHeader( |
| 52 signin::kChromeConnectedHeader, &request), |
| 53 expected_result); |
| 54 if (expected_result) { |
| 55 EXPECT_EQ(expected_request, request); |
| 56 } |
| 57 } |
| 58 |
| 59 base::MessageLoop loop_; |
| 60 |
| 61 user_prefs::TestingPrefServiceSyncable prefs_; |
| 62 net::TestURLRequestContext url_request_context_; |
| 63 |
| 64 scoped_refptr<HostContentSettingsMap> settings_map_; |
| 65 scoped_refptr<content_settings::CookieSettings> cookie_settings_; |
| 66 }; |
| 67 |
| 68 // Tests that no Mirror request is returned when the user is not signed in (no |
| 69 // account id). |
| 70 TEST_F(SigninHeaderHelperTest, TestNoMirrorRequestNoAccountId) { |
| 71 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 72 switches::kEnableAccountConsistency); |
| 73 CheckMirrorHeaderRequest(GURL("https://docs.google.com"), "", ""); |
| 74 CheckMirrorCookieRequest(GURL("https://docs.google.com"), "", ""); |
| 75 } |
| 76 |
| 77 // Tests that no Mirror request is returned when the cookies aren't allowed to |
| 78 // be set. |
| 79 TEST_F(SigninHeaderHelperTest, TestNoMirrorRequestCookieSettingBlocked) { |
| 80 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 81 switches::kEnableAccountConsistency); |
| 82 cookie_settings_->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK); |
| 83 CheckMirrorHeaderRequest(GURL("https://docs.google.com"), "0123456789", ""); |
| 84 CheckMirrorCookieRequest(GURL("https://docs.google.com"), "0123456789", ""); |
| 85 } |
| 86 |
| 87 // Tests that no Mirror request is returned when the target is a non-Google URL. |
| 88 TEST_F(SigninHeaderHelperTest, TestNoMirrorRequestExternalURL) { |
| 89 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 90 switches::kEnableAccountConsistency); |
| 91 CheckMirrorHeaderRequest(GURL("https://foo.com"), "0123456789", ""); |
| 92 CheckMirrorCookieRequest(GURL("https://foo.com"), "0123456789", ""); |
| 93 } |
| 94 |
| 95 // Tests that the Mirror request is returned without the GAIA Id when the target |
| 96 // is a google TLD domain. |
| 97 TEST_F(SigninHeaderHelperTest, TestMirrorRequestGoogleTLD) { |
| 98 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 99 switches::kEnableAccountConsistency); |
| 100 CheckMirrorHeaderRequest(GURL("https://google.fr"), "0123456789", |
| 101 "mode=0,enable_account_consistency=true"); |
| 102 CheckMirrorCookieRequest(GURL("https://google.de"), "0123456789", |
| 103 "mode=0:enable_account_consistency=true"); |
| 104 } |
| 105 |
| 106 // Tests that the Mirror request is returned when the target is the domain |
| 107 // google.com, and that the GAIA Id is only attached for the cookie. |
| 108 TEST_F(SigninHeaderHelperTest, TestMirrorRequestGoogleCom) { |
| 109 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 110 switches::kEnableAccountConsistency); |
| 111 CheckMirrorHeaderRequest(GURL("https://www.google.com"), "0123456789", |
| 112 "mode=0,enable_account_consistency=true"); |
| 113 CheckMirrorCookieRequest( |
| 114 GURL("https://www.google.com"), "0123456789", |
| 115 "id=0123456789:mode=0:enable_account_consistency=true"); |
| 116 } |
| 117 |
| 118 // Tests that the Mirror request is returned with the GAIA Id on Drive origin, |
| 119 // even if account consistency is disabled. |
| 120 TEST_F(SigninHeaderHelperTest, TestMirrorRequestDrive) { |
| 121 DCHECK(!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 122 switches::kEnableAccountConsistency)); |
| 123 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 124 switches::kDisableAccountConsistency); |
| 125 CheckMirrorHeaderRequest( |
| 126 GURL("https://docs.google.com/document"), "0123456789", |
| 127 "id=0123456789,mode=0,enable_account_consistency=false"); |
| 128 CheckMirrorCookieRequest( |
| 129 GURL("https://drive.google.com/drive"), "0123456789", |
| 130 "id=0123456789:mode=0:enable_account_consistency=false"); |
| 131 |
| 132 // Enable Account Consistency will override the disable. |
| 133 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 134 switches::kEnableAccountConsistency); |
| 135 CheckMirrorHeaderRequest( |
| 136 GURL("https://docs.google.com/document"), "0123456789", |
| 137 "id=0123456789,mode=0,enable_account_consistency=true"); |
| 138 CheckMirrorCookieRequest( |
| 139 GURL("https://drive.google.com/drive"), "0123456789", |
| 140 "id=0123456789:mode=0:enable_account_consistency=true"); |
| 141 } |
| 142 |
| 143 // Tests that the Mirror header request is returned normally when the redirect |
| 144 // URL is eligible. |
| 145 TEST_F(SigninHeaderHelperTest, TestMirrorHeaderEligibleRedirectURL) { |
| 146 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 147 switches::kEnableAccountConsistency); |
| 148 |
| 149 const GURL url("https://docs.google.com/document"); |
| 150 const GURL redirect_url("https://www.google.com"); |
| 151 const std::string account_id = "0123456789"; |
| 152 std::unique_ptr<net::URLRequest> url_request = |
| 153 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr); |
| 154 EXPECT_TRUE(signin::AppendOrRemoveMirrorRequestHeaderIfPossible( |
| 155 url_request.get(), redirect_url, account_id, cookie_settings_.get(), |
| 156 signin::PROFILE_MODE_DEFAULT)); |
| 157 EXPECT_TRUE(url_request->extra_request_headers().HasHeader( |
| 158 signin::kChromeConnectedHeader)); |
| 159 } |
| 160 |
| 161 // Tests that the Mirror header request is stripped when the redirect URL is not |
| 162 // eligible. |
| 163 TEST_F(SigninHeaderHelperTest, TestMirrorHeaderNonEligibleRedirectURL) { |
| 164 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 165 switches::kEnableAccountConsistency); |
| 166 |
| 167 const GURL url("https://docs.google.com/document"); |
| 168 const GURL redirect_url("http://www.foo.com"); |
| 169 const std::string account_id = "0123456789"; |
| 170 std::unique_ptr<net::URLRequest> url_request = |
| 171 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr); |
| 172 EXPECT_FALSE(signin::AppendOrRemoveMirrorRequestHeaderIfPossible( |
| 173 url_request.get(), redirect_url, account_id, cookie_settings_.get(), |
| 174 signin::PROFILE_MODE_DEFAULT)); |
| 175 EXPECT_FALSE(url_request->extra_request_headers().HasHeader( |
| 176 signin::kChromeConnectedHeader)); |
| 177 } |
| 178 |
| 179 // Tests that the Mirror header, whatever its value is, is untouched when both |
| 180 // the current and the redirect URL are non-eligible. |
| 181 TEST_F(SigninHeaderHelperTest, TestIgnoreMirrorHeaderNonEligibleURLs) { |
| 182 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 183 switches::kEnableAccountConsistency); |
| 184 |
| 185 const GURL url("https://www.bar.com"); |
| 186 const GURL redirect_url("http://www.foo.com"); |
| 187 const std::string account_id = "0123456789"; |
| 188 const std::string fake_header = "foo,bar"; |
| 189 std::unique_ptr<net::URLRequest> url_request = |
| 190 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr); |
| 191 url_request->SetExtraRequestHeaderByName(signin::kChromeConnectedHeader, |
| 192 fake_header, false); |
| 193 EXPECT_FALSE(signin::AppendOrRemoveMirrorRequestHeaderIfPossible( |
| 194 url_request.get(), redirect_url, account_id, cookie_settings_.get(), |
| 195 signin::PROFILE_MODE_DEFAULT)); |
| 196 std::string header; |
| 197 EXPECT_TRUE(url_request->extra_request_headers().GetHeader( |
| 198 signin::kChromeConnectedHeader, &header)); |
| 199 EXPECT_EQ(fake_header, header); |
| 200 } |
OLD | NEW |