OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/signin/dice_response_handler.h" | 5 #include "chrome/browser/signin/dice_response_handler.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/test/test_mock_time_task_runner.h" | 10 #include "base/test/test_mock_time_task_runner.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
25 | 25 |
26 using signin::DiceAction; | 26 using signin::DiceAction; |
27 using signin::DiceResponseParams; | 27 using signin::DiceResponseParams; |
28 | 28 |
29 namespace { | 29 namespace { |
30 | 30 |
31 const char kAuthorizationCode[] = "authorization_code"; | 31 const char kAuthorizationCode[] = "authorization_code"; |
32 const char kEmail[] = "email"; | 32 const char kEmail[] = "email"; |
33 const char kObfuscatedGaiaID[] = "obfuscated_gaia_id"; | 33 const char kGaiaID[] = "gaia_id"; |
34 const int kSessionIndex = 42; | 34 const int kSessionIndex = 42; |
35 | 35 |
36 // TestSigninClient implementation that intercepts the GaiaAuthConsumer and | 36 // TestSigninClient implementation that intercepts the GaiaAuthConsumer and |
37 // replaces it by a dummy one. | 37 // replaces it by a dummy one. |
38 class DiceTestSigninClient : public TestSigninClient, public GaiaAuthConsumer { | 38 class DiceTestSigninClient : public TestSigninClient, public GaiaAuthConsumer { |
39 public: | 39 public: |
40 explicit DiceTestSigninClient(PrefService* pref_service) | 40 explicit DiceTestSigninClient(PrefService* pref_service) |
41 : TestSigninClient(pref_service), consumer_(nullptr) {} | 41 : TestSigninClient(pref_service), consumer_(nullptr) {} |
42 | 42 |
43 std::unique_ptr<GaiaAuthFetcher> CreateGaiaAuthFetcher( | 43 std::unique_ptr<GaiaAuthFetcher> CreateGaiaAuthFetcher( |
(...skipping 30 matching lines...) Expand all Loading... |
74 signin_client_.SetURLRequestContext(request_context_getter_.get()); | 74 signin_client_.SetURLRequestContext(request_context_getter_.get()); |
75 AccountTrackerService::RegisterPrefs(pref_service_.registry()); | 75 AccountTrackerService::RegisterPrefs(pref_service_.registry()); |
76 account_tracker_service_.Initialize(&signin_client_); | 76 account_tracker_service_.Initialize(&signin_client_); |
77 } | 77 } |
78 | 78 |
79 ~DiceResponseHandlerTest() override {} | 79 ~DiceResponseHandlerTest() override {} |
80 | 80 |
81 DiceResponseParams MakeDiceParams(DiceAction action) { | 81 DiceResponseParams MakeDiceParams(DiceAction action) { |
82 DiceResponseParams dice_params; | 82 DiceResponseParams dice_params; |
83 dice_params.user_intention = action; | 83 dice_params.user_intention = action; |
84 dice_params.obfuscated_gaia_id = kObfuscatedGaiaID; | 84 dice_params.gaia_id = kGaiaID; |
85 dice_params.email = kEmail; | 85 dice_params.email = kEmail; |
86 dice_params.session_index = kSessionIndex; | 86 dice_params.session_index = kSessionIndex; |
87 dice_params.authorization_code = kAuthorizationCode; | 87 dice_params.authorization_code = kAuthorizationCode; |
88 return dice_params; | 88 return dice_params; |
89 } | 89 } |
90 | 90 |
91 base::MessageLoop loop_; | 91 base::MessageLoop loop_; |
92 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; | 92 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; |
93 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | 93 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
94 sync_preferences::TestingPrefServiceSyncable pref_service_; | 94 sync_preferences::TestingPrefServiceSyncable pref_service_; |
95 DiceTestSigninClient signin_client_; | 95 DiceTestSigninClient signin_client_; |
96 ProfileOAuth2TokenService token_service_; | 96 ProfileOAuth2TokenService token_service_; |
97 AccountTrackerService account_tracker_service_; | 97 AccountTrackerService account_tracker_service_; |
98 DiceResponseHandler dice_response_handler_; | 98 DiceResponseHandler dice_response_handler_; |
99 }; | 99 }; |
100 | 100 |
101 // Checks that a SIGNIN action triggers a token exchange request. | 101 // Checks that a SIGNIN action triggers a token exchange request. |
102 TEST_F(DiceResponseHandlerTest, Signin) { | 102 TEST_F(DiceResponseHandlerTest, Signin) { |
103 DiceResponseParams dice_params = MakeDiceParams(DiceAction::SIGNIN); | 103 DiceResponseParams dice_params = MakeDiceParams(DiceAction::SIGNIN); |
104 ASSERT_FALSE( | 104 ASSERT_FALSE(token_service_.RefreshTokenIsAvailable(dice_params.gaia_id)); |
105 token_service_.RefreshTokenIsAvailable(dice_params.obfuscated_gaia_id)); | |
106 dice_response_handler_.ProcessDiceHeader(dice_params); | 105 dice_response_handler_.ProcessDiceHeader(dice_params); |
107 // Check that a GaiaAuthFetcher has been created. | 106 // Check that a GaiaAuthFetcher has been created. |
108 ASSERT_THAT(signin_client_.consumer_, testing::NotNull()); | 107 ASSERT_THAT(signin_client_.consumer_, testing::NotNull()); |
109 // Simulate GaiaAuthFetcher success. | 108 // Simulate GaiaAuthFetcher success. |
110 signin_client_.consumer_->OnClientOAuthSuccess( | 109 signin_client_.consumer_->OnClientOAuthSuccess( |
111 GaiaAuthConsumer::ClientOAuthResult("refresh_token", "access_token", 10)); | 110 GaiaAuthConsumer::ClientOAuthResult("refresh_token", "access_token", 10)); |
112 // Check that the token has been inserted in the token service. | 111 // Check that the token has been inserted in the token service. |
113 EXPECT_TRUE( | 112 EXPECT_TRUE(token_service_.RefreshTokenIsAvailable(dice_params.gaia_id)); |
114 token_service_.RefreshTokenIsAvailable(dice_params.obfuscated_gaia_id)); | |
115 } | 113 } |
116 | 114 |
117 // Checks that a second token for the same account is not requested when a | 115 // Checks that a second token for the same account is not requested when a |
118 // request is already in flight. | 116 // request is already in flight. |
119 TEST_F(DiceResponseHandlerTest, SigninRepeatedWithSameAccount) { | 117 TEST_F(DiceResponseHandlerTest, SigninRepeatedWithSameAccount) { |
120 DiceResponseParams dice_params = MakeDiceParams(DiceAction::SIGNIN); | 118 DiceResponseParams dice_params = MakeDiceParams(DiceAction::SIGNIN); |
121 ASSERT_FALSE( | 119 ASSERT_FALSE(token_service_.RefreshTokenIsAvailable(dice_params.gaia_id)); |
122 token_service_.RefreshTokenIsAvailable(dice_params.obfuscated_gaia_id)); | |
123 dice_response_handler_.ProcessDiceHeader(dice_params); | 120 dice_response_handler_.ProcessDiceHeader(dice_params); |
124 // Check that a GaiaAuthFetcher has been created. | 121 // Check that a GaiaAuthFetcher has been created. |
125 GaiaAuthConsumer* consumer = signin_client_.consumer_; | 122 GaiaAuthConsumer* consumer = signin_client_.consumer_; |
126 ASSERT_THAT(consumer, testing::NotNull()); | 123 ASSERT_THAT(consumer, testing::NotNull()); |
127 // Start a second request for the same account. | 124 // Start a second request for the same account. |
128 signin_client_.consumer_ = nullptr; | 125 signin_client_.consumer_ = nullptr; |
129 dice_response_handler_.ProcessDiceHeader(dice_params); | 126 dice_response_handler_.ProcessDiceHeader(dice_params); |
130 // Check that there is no new request. | 127 // Check that there is no new request. |
131 ASSERT_THAT(signin_client_.consumer_, testing::IsNull()); | 128 ASSERT_THAT(signin_client_.consumer_, testing::IsNull()); |
132 // Simulate GaiaAuthFetcher success for the first request. | 129 // Simulate GaiaAuthFetcher success for the first request. |
133 consumer->OnClientOAuthSuccess( | 130 consumer->OnClientOAuthSuccess( |
134 GaiaAuthConsumer::ClientOAuthResult("refresh_token", "access_token", 10)); | 131 GaiaAuthConsumer::ClientOAuthResult("refresh_token", "access_token", 10)); |
135 // Check that the token has been inserted in the token service. | 132 // Check that the token has been inserted in the token service. |
136 EXPECT_TRUE( | 133 EXPECT_TRUE(token_service_.RefreshTokenIsAvailable(dice_params.gaia_id)); |
137 token_service_.RefreshTokenIsAvailable(dice_params.obfuscated_gaia_id)); | |
138 } | 134 } |
139 | 135 |
140 // Checks that two SIGNIN requests can happen concurrently. | 136 // Checks that two SIGNIN requests can happen concurrently. |
141 TEST_F(DiceResponseHandlerTest, SigninWithTwoAccounts) { | 137 TEST_F(DiceResponseHandlerTest, SigninWithTwoAccounts) { |
142 DiceResponseParams dice_params_1 = MakeDiceParams(DiceAction::SIGNIN); | 138 DiceResponseParams dice_params_1 = MakeDiceParams(DiceAction::SIGNIN); |
143 DiceResponseParams dice_params_2 = MakeDiceParams(DiceAction::SIGNIN); | 139 DiceResponseParams dice_params_2 = MakeDiceParams(DiceAction::SIGNIN); |
144 dice_params_2.email = "other_email"; | 140 dice_params_2.email = "other_email"; |
145 dice_params_2.obfuscated_gaia_id = "other_gaia_id"; | 141 dice_params_2.gaia_id = "other_gaia_id"; |
146 ASSERT_FALSE( | 142 ASSERT_FALSE(token_service_.RefreshTokenIsAvailable(dice_params_1.gaia_id)); |
147 token_service_.RefreshTokenIsAvailable(dice_params_1.obfuscated_gaia_id)); | 143 ASSERT_FALSE(token_service_.RefreshTokenIsAvailable(dice_params_2.gaia_id)); |
148 ASSERT_FALSE( | |
149 token_service_.RefreshTokenIsAvailable(dice_params_2.obfuscated_gaia_id)); | |
150 // Start first request. | 144 // Start first request. |
151 dice_response_handler_.ProcessDiceHeader(dice_params_1); | 145 dice_response_handler_.ProcessDiceHeader(dice_params_1); |
152 // Check that a GaiaAuthFetcher has been created. | 146 // Check that a GaiaAuthFetcher has been created. |
153 GaiaAuthConsumer* consumer_1 = signin_client_.consumer_; | 147 GaiaAuthConsumer* consumer_1 = signin_client_.consumer_; |
154 ASSERT_THAT(consumer_1, testing::NotNull()); | 148 ASSERT_THAT(consumer_1, testing::NotNull()); |
155 // Start second request. | 149 // Start second request. |
156 signin_client_.consumer_ = nullptr; | 150 signin_client_.consumer_ = nullptr; |
157 dice_response_handler_.ProcessDiceHeader(dice_params_2); | 151 dice_response_handler_.ProcessDiceHeader(dice_params_2); |
158 GaiaAuthConsumer* consumer_2 = signin_client_.consumer_; | 152 GaiaAuthConsumer* consumer_2 = signin_client_.consumer_; |
159 ASSERT_THAT(consumer_2, testing::NotNull()); | 153 ASSERT_THAT(consumer_2, testing::NotNull()); |
160 // Simulate GaiaAuthFetcher success for the first request. | 154 // Simulate GaiaAuthFetcher success for the first request. |
161 consumer_1->OnClientOAuthSuccess( | 155 consumer_1->OnClientOAuthSuccess( |
162 GaiaAuthConsumer::ClientOAuthResult("refresh_token", "access_token", 10)); | 156 GaiaAuthConsumer::ClientOAuthResult("refresh_token", "access_token", 10)); |
163 // Check that the token has been inserted in the token service. | 157 // Check that the token has been inserted in the token service. |
164 EXPECT_TRUE( | 158 EXPECT_TRUE(token_service_.RefreshTokenIsAvailable(dice_params_1.gaia_id)); |
165 token_service_.RefreshTokenIsAvailable(dice_params_1.obfuscated_gaia_id)); | |
166 // Simulate GaiaAuthFetcher success for the second request. | 159 // Simulate GaiaAuthFetcher success for the second request. |
167 consumer_2->OnClientOAuthSuccess( | 160 consumer_2->OnClientOAuthSuccess( |
168 GaiaAuthConsumer::ClientOAuthResult("refresh_token", "access_token", 10)); | 161 GaiaAuthConsumer::ClientOAuthResult("refresh_token", "access_token", 10)); |
169 // Check that the token has been inserted in the token service. | 162 // Check that the token has been inserted in the token service. |
170 EXPECT_TRUE( | 163 EXPECT_TRUE(token_service_.RefreshTokenIsAvailable(dice_params_2.gaia_id)); |
171 token_service_.RefreshTokenIsAvailable(dice_params_2.obfuscated_gaia_id)); | |
172 } | 164 } |
173 | 165 |
174 TEST_F(DiceResponseHandlerTest, Timeout) { | 166 TEST_F(DiceResponseHandlerTest, Timeout) { |
175 DiceResponseParams dice_params = MakeDiceParams(DiceAction::SIGNIN); | 167 DiceResponseParams dice_params = MakeDiceParams(DiceAction::SIGNIN); |
176 ASSERT_FALSE( | 168 ASSERT_FALSE(token_service_.RefreshTokenIsAvailable(dice_params.gaia_id)); |
177 token_service_.RefreshTokenIsAvailable(dice_params.obfuscated_gaia_id)); | |
178 dice_response_handler_.ProcessDiceHeader(dice_params); | 169 dice_response_handler_.ProcessDiceHeader(dice_params); |
179 // Check that a GaiaAuthFetcher has been created. | 170 // Check that a GaiaAuthFetcher has been created. |
180 ASSERT_THAT(signin_client_.consumer_, testing::NotNull()); | 171 ASSERT_THAT(signin_client_.consumer_, testing::NotNull()); |
181 EXPECT_EQ( | 172 EXPECT_EQ( |
182 1u, dice_response_handler_.GetPendingDiceTokenFetchersCountForTesting()); | 173 1u, dice_response_handler_.GetPendingDiceTokenFetchersCountForTesting()); |
183 // Force a timeout. | 174 // Force a timeout. |
184 task_runner_->FastForwardBy( | 175 task_runner_->FastForwardBy( |
185 base::TimeDelta::FromSeconds(kDiceTokenFetchTimeoutSeconds + 1)); | 176 base::TimeDelta::FromSeconds(kDiceTokenFetchTimeoutSeconds + 1)); |
186 EXPECT_EQ( | 177 EXPECT_EQ( |
187 0u, dice_response_handler_.GetPendingDiceTokenFetchersCountForTesting()); | 178 0u, dice_response_handler_.GetPendingDiceTokenFetchersCountForTesting()); |
188 } | 179 } |
189 | 180 |
190 // Tests that the DiceResponseHandler is created for a normal profile but not | 181 // Tests that the DiceResponseHandler is created for a normal profile but not |
191 // for an incognito profile. | 182 // for an incognito profile. |
192 TEST(DiceResponseHandlerFactoryTest, NotInIncognito) { | 183 TEST(DiceResponseHandlerFactoryTest, NotInIncognito) { |
193 content::TestBrowserThreadBundle thread_bundle; | 184 content::TestBrowserThreadBundle thread_bundle; |
194 TestingProfile profile; | 185 TestingProfile profile; |
195 EXPECT_THAT(DiceResponseHandler::GetForProfile(&profile), testing::NotNull()); | 186 EXPECT_THAT(DiceResponseHandler::GetForProfile(&profile), testing::NotNull()); |
196 EXPECT_THAT( | 187 EXPECT_THAT( |
197 DiceResponseHandler::GetForProfile(profile.GetOffTheRecordProfile()), | 188 DiceResponseHandler::GetForProfile(profile.GetOffTheRecordProfile()), |
198 testing::IsNull()); | 189 testing::IsNull()); |
199 } | 190 } |
200 | 191 |
201 } // namespace | 192 } // namespace |
OLD | NEW |