OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/chromeos/settings/device_oauth2_token_service.h" | 5 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/prefs/testing_pref_service.h" | 8 #include "base/prefs/testing_pref_service.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "chrome/common/pref_names.h" | 10 #include "chrome/common/pref_names.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 class TestDeviceOAuth2TokenService : public DeviceOAuth2TokenService { | 36 class TestDeviceOAuth2TokenService : public DeviceOAuth2TokenService { |
37 public: | 37 public: |
38 explicit TestDeviceOAuth2TokenService(net::URLRequestContextGetter* getter, | 38 explicit TestDeviceOAuth2TokenService(net::URLRequestContextGetter* getter, |
39 PrefService* local_state) | 39 PrefService* local_state) |
40 : DeviceOAuth2TokenService(getter, local_state) { | 40 : DeviceOAuth2TokenService(getter, local_state) { |
41 } | 41 } |
42 void SetRobotAccountIdPolicyValue(const std::string& id) { | 42 void SetRobotAccountIdPolicyValue(const std::string& id) { |
43 robot_account_id_ = id; | 43 robot_account_id_ = id; |
44 } | 44 } |
45 | 45 |
46 protected: | |
47 // Skip calling into the policy subsystem and return our test value. | 46 // Skip calling into the policy subsystem and return our test value. |
48 virtual std::string GetRobotAccountId() OVERRIDE { | 47 virtual std::string GetRobotAccountId() OVERRIDE { |
49 return robot_account_id_; | 48 return robot_account_id_; |
50 } | 49 } |
51 | 50 |
52 private: | 51 private: |
53 std::string robot_account_id_; | 52 std::string robot_account_id_; |
54 DISALLOW_COPY_AND_ASSIGN(TestDeviceOAuth2TokenService); | 53 DISALLOW_COPY_AND_ASSIGN(TestDeviceOAuth2TokenService); |
55 }; | 54 }; |
56 | 55 |
(...skipping 15 matching lines...) Expand all Loading... |
72 // Local State (if the value is an empty string, it will be ignored). | 71 // Local State (if the value is an empty string, it will be ignored). |
73 void SetUpDefaultValues() { | 72 void SetUpDefaultValues() { |
74 cryptohome_library_.reset(chromeos::CryptohomeLibrary::GetTestImpl()); | 73 cryptohome_library_.reset(chromeos::CryptohomeLibrary::GetTestImpl()); |
75 chromeos::CryptohomeLibrary::SetForTest(cryptohome_library_.get()); | 74 chromeos::CryptohomeLibrary::SetForTest(cryptohome_library_.get()); |
76 SetDeviceRefreshTokenInLocalState("device_refresh_token_4_test"); | 75 SetDeviceRefreshTokenInLocalState("device_refresh_token_4_test"); |
77 oauth2_service_.SetRobotAccountIdPolicyValue("service_acct@g.com"); | 76 oauth2_service_.SetRobotAccountIdPolicyValue("service_acct@g.com"); |
78 AssertConsumerTokensAndErrors(0, 0); | 77 AssertConsumerTokensAndErrors(0, 0); |
79 } | 78 } |
80 | 79 |
81 scoped_ptr<OAuth2TokenService::Request> StartTokenRequest() { | 80 scoped_ptr<OAuth2TokenService::Request> StartTokenRequest() { |
82 return oauth2_service_.StartRequest(std::set<std::string>(), &consumer_); | 81 return oauth2_service_.StartRequest(oauth2_service_.GetRobotAccountId(), |
| 82 std::set<std::string>(), |
| 83 &consumer_); |
83 } | 84 } |
84 | 85 |
85 virtual void TearDown() OVERRIDE { | 86 virtual void TearDown() OVERRIDE { |
86 CryptohomeLibrary::SetForTest(NULL); | 87 CryptohomeLibrary::SetForTest(NULL); |
87 base::RunLoop().RunUntilIdle(); | 88 base::RunLoop().RunUntilIdle(); |
88 } | 89 } |
89 | 90 |
90 // Utility method to set a value in Local State for the device refresh token | 91 // Utility method to set a value in Local State for the device refresh token |
91 // (it must have a non-empty value or it won't be used). | 92 // (it must have a non-empty value or it won't be used). |
92 void SetDeviceRefreshTokenInLocalState(const std::string& refresh_token) { | 93 void SetDeviceRefreshTokenInLocalState(const std::string& refresh_token) { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 .WillOnce(Return("")); | 156 .WillOnce(Return("")); |
156 EXPECT_CALL(mock_cryptohome_library, | 157 EXPECT_CALL(mock_cryptohome_library, |
157 EncryptWithSystemSalt(StrEq("test-token"))) | 158 EncryptWithSystemSalt(StrEq("test-token"))) |
158 .Times(1) | 159 .Times(1) |
159 .WillOnce(Return("encrypted")); | 160 .WillOnce(Return("encrypted")); |
160 EXPECT_CALL(mock_cryptohome_library, | 161 EXPECT_CALL(mock_cryptohome_library, |
161 DecryptWithSystemSalt(StrEq("encrypted"))) | 162 DecryptWithSystemSalt(StrEq("encrypted"))) |
162 .Times(1) | 163 .Times(1) |
163 .WillOnce(Return("test-token")); | 164 .WillOnce(Return("test-token")); |
164 | 165 |
165 ASSERT_EQ("", oauth2_service_.GetRefreshToken()); | 166 ASSERT_EQ("", oauth2_service_.GetRefreshToken( |
| 167 oauth2_service_.GetRobotAccountId())); |
166 oauth2_service_.SetAndSaveRefreshToken("test-token"); | 168 oauth2_service_.SetAndSaveRefreshToken("test-token"); |
167 ASSERT_EQ("test-token", oauth2_service_.GetRefreshToken()); | 169 ASSERT_EQ("test-token", oauth2_service_.GetRefreshToken( |
| 170 oauth2_service_.GetRobotAccountId())); |
168 | 171 |
169 // This call won't invoke decrypt again, since the value is cached. | 172 // This call won't invoke decrypt again, since the value is cached. |
170 ASSERT_EQ("test-token", oauth2_service_.GetRefreshToken()); | 173 ASSERT_EQ("test-token", oauth2_service_.GetRefreshToken( |
| 174 oauth2_service_.GetRobotAccountId())); |
171 } | 175 } |
172 | 176 |
173 TEST_F(DeviceOAuth2TokenServiceTest, RefreshTokenValidation_Success) { | 177 TEST_F(DeviceOAuth2TokenServiceTest, RefreshTokenValidation_Success) { |
174 SetUpDefaultValues(); | 178 SetUpDefaultValues(); |
175 scoped_ptr<OAuth2TokenService::Request> request = StartTokenRequest(); | 179 scoped_ptr<OAuth2TokenService::Request> request = StartTokenRequest(); |
176 | 180 |
177 ReturnOAuthUrlFetchResults( | 181 ReturnOAuthUrlFetchResults( |
178 kValidatorUrlFetcherId, | 182 kValidatorUrlFetcherId, |
179 net::HTTP_OK, | 183 net::HTTP_OK, |
180 GetValidTokenResponse("tokeninfo_access_token", 3600)); | 184 GetValidTokenResponse("tokeninfo_access_token", 3600)); |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 kValidatorUrlFetcherId, | 363 kValidatorUrlFetcherId, |
360 net::HTTP_OK, | 364 net::HTTP_OK, |
361 GetValidTokenInfoResponse("service_acct@g.com")); | 365 GetValidTokenInfoResponse("service_acct@g.com")); |
362 | 366 |
363 // All fetches were successful, but consumer still given error since | 367 // All fetches were successful, but consumer still given error since |
364 // the token owner doesn't match the policy value. | 368 // the token owner doesn't match the policy value. |
365 AssertConsumerTokensAndErrors(0, 1); | 369 AssertConsumerTokensAndErrors(0, 1); |
366 } | 370 } |
367 | 371 |
368 } // namespace chromeos | 372 } // namespace chromeos |
OLD | NEW |