OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "remoting/test/app_remoting_test_driver_environment.h" |
| 6 |
| 7 #include "remoting/test/fake_access_token_fetcher.h" |
| 8 #include "remoting/test/mock_access_token_fetcher.h" |
| 9 #include "remoting/test/refresh_token_store.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace { |
| 13 const char kAuthCodeValue[] = "4/892379827345jkefvkdfbv"; |
| 14 const char kRefreshTokenValue[] = "1/lkjalseLKJlsiJgr45jbv"; |
| 15 const char kUserNameValue[] = "remoting_user@gmail.com"; |
| 16 const char kDevEnvironmentValue[] = "dev"; |
| 17 } |
| 18 |
| 19 namespace remoting { |
| 20 namespace test { |
| 21 |
| 22 using testing::_; |
| 23 |
| 24 // Stubs out the file API and returns fake data so we can remove |
| 25 // file system dependencies when testing the TestDriverEnvironment. |
| 26 class FakeRefreshTokenStore : public RefreshTokenStore { |
| 27 public: |
| 28 FakeRefreshTokenStore() { |
| 29 // Set some success defaults. |
| 30 refresh_token_value = kRefreshTokenValue; |
| 31 refresh_token_write_attempted = false; |
| 32 refresh_token_write_succeeded = true; |
| 33 } |
| 34 ~FakeRefreshTokenStore() override {} |
| 35 |
| 36 std::string FetchRefreshToken() override { |
| 37 return refresh_token_value; |
| 38 }; |
| 39 |
| 40 bool StoreRefreshToken(const std::string& refresh_token) override { |
| 41 // Record the information passed to us to write. |
| 42 refresh_token_write_attempted = true; |
| 43 refresh_token_value_written = refresh_token; |
| 44 |
| 45 return refresh_token_write_succeeded; |
| 46 }; |
| 47 |
| 48 // Control members used to return specific data to the caller. |
| 49 std::string refresh_token_value; |
| 50 bool refresh_token_write_succeeded; |
| 51 |
| 52 // Verification members to observe the value of the data being written. |
| 53 bool refresh_token_write_attempted; |
| 54 std::string refresh_token_value_written; |
| 55 |
| 56 DISALLOW_COPY_AND_ASSIGN(FakeRefreshTokenStore); |
| 57 }; |
| 58 |
| 59 TEST(AppRemotingTestDriverEnvironmentTest, InitializeObjectWithAuthCode) { |
| 60 MockAccessTokenFetcher mock_access_token_fetcher; |
| 61 |
| 62 mock_access_token_fetcher.SetAccessTokenFetcher( |
| 63 make_scoped_ptr(new FakeAccessTokenFetcher())); |
| 64 |
| 65 FakeRefreshTokenStore fake_token_store; |
| 66 |
| 67 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) |
| 68 .Times(1); |
| 69 |
| 70 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) |
| 71 .Times(0); |
| 72 |
| 73 AppRemotingTestDriverEnvironment environment_object( |
| 74 kUserNameValue, |
| 75 kDevEnvironmentValue); |
| 76 |
| 77 environment_object.SetAccessTokenFetcherForTest(&mock_access_token_fetcher); |
| 78 |
| 79 environment_object.SetRefreshTokenStoreForTest(&fake_token_store); |
| 80 |
| 81 bool init_result = environment_object.Initialize(kAuthCodeValue); |
| 82 |
| 83 EXPECT_TRUE(init_result); |
| 84 EXPECT_TRUE(fake_token_store.refresh_token_write_attempted); |
| 85 EXPECT_STREQ(kFakeAccessTokenFetcherRefreshTokenValue, |
| 86 fake_token_store.refresh_token_value_written.c_str()); |
| 87 EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); |
| 88 EXPECT_STREQ(kFakeAccessTokenFetcherAccessTokenValue, |
| 89 environment_object.access_token().c_str()); |
| 90 |
| 91 // Attempt to init again, we should not see any additional calls or errors. |
| 92 init_result = environment_object.Initialize(kAuthCodeValue); |
| 93 EXPECT_TRUE(init_result); |
| 94 } |
| 95 |
| 96 TEST(AppRemotingTestDriverEnvironmentTest, InitializeObjectWithAuthCodeFailed) { |
| 97 MockAccessTokenFetcher mock_access_token_fetcher; |
| 98 |
| 99 scoped_ptr<FakeAccessTokenFetcher> fake_access_token_fetcher( |
| 100 new FakeAccessTokenFetcher()); |
| 101 |
| 102 fake_access_token_fetcher->set_fail_access_token_from_auth_code(true); |
| 103 |
| 104 mock_access_token_fetcher.SetAccessTokenFetcher( |
| 105 fake_access_token_fetcher.Pass()); |
| 106 |
| 107 FakeRefreshTokenStore fake_token_store; |
| 108 |
| 109 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) |
| 110 .Times(1); |
| 111 |
| 112 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) |
| 113 .Times(0); |
| 114 |
| 115 AppRemotingTestDriverEnvironment environment_object( |
| 116 kUserNameValue, |
| 117 kDevEnvironmentValue); |
| 118 |
| 119 environment_object.SetAccessTokenFetcherForTest(&mock_access_token_fetcher); |
| 120 |
| 121 environment_object.SetRefreshTokenStoreForTest(&fake_token_store); |
| 122 |
| 123 bool init_result = environment_object.Initialize(kAuthCodeValue); |
| 124 |
| 125 EXPECT_FALSE(init_result); |
| 126 EXPECT_FALSE(fake_token_store.refresh_token_write_attempted); |
| 127 } |
| 128 |
| 129 TEST(AppRemotingTestDriverEnvironmentTest, InitializeObjectWithRefreshToken) { |
| 130 MockAccessTokenFetcher mock_access_token_fetcher; |
| 131 |
| 132 mock_access_token_fetcher.SetAccessTokenFetcher( |
| 133 make_scoped_ptr(new FakeAccessTokenFetcher())); |
| 134 |
| 135 FakeRefreshTokenStore fake_token_store; |
| 136 |
| 137 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) |
| 138 .Times(1); |
| 139 |
| 140 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) |
| 141 .Times(0); |
| 142 |
| 143 AppRemotingTestDriverEnvironment environment_object( |
| 144 kUserNameValue, |
| 145 kDevEnvironmentValue); |
| 146 |
| 147 environment_object.SetAccessTokenFetcherForTest(&mock_access_token_fetcher); |
| 148 |
| 149 environment_object.SetRefreshTokenStoreForTest(&fake_token_store); |
| 150 |
| 151 // Pass in an empty auth code since we are using a refresh token. |
| 152 bool init_result = environment_object.Initialize(std::string()); |
| 153 |
| 154 EXPECT_TRUE(init_result); |
| 155 |
| 156 // We should not write the refresh token a second time if we read from the |
| 157 // disk originally. |
| 158 EXPECT_FALSE(fake_token_store.refresh_token_write_attempted); |
| 159 |
| 160 // Verify the object was initialized correctly. |
| 161 EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); |
| 162 EXPECT_STREQ(kFakeAccessTokenFetcherAccessTokenValue, |
| 163 environment_object.access_token().c_str()); |
| 164 |
| 165 // Attempt to init again, we should not see any additional calls or errors. |
| 166 init_result = environment_object.Initialize(std::string()); |
| 167 EXPECT_TRUE(init_result); |
| 168 } |
| 169 |
| 170 TEST(AppRemotingTestDriverEnvironmentTest, |
| 171 InitializeObjectWithRefreshTokenFailed) { |
| 172 MockAccessTokenFetcher mock_access_token_fetcher; |
| 173 |
| 174 scoped_ptr<FakeAccessTokenFetcher> fake_access_token_fetcher( |
| 175 new FakeAccessTokenFetcher()); |
| 176 |
| 177 fake_access_token_fetcher->set_fail_access_token_from_refresh_token(true); |
| 178 |
| 179 mock_access_token_fetcher.SetAccessTokenFetcher( |
| 180 fake_access_token_fetcher.Pass()); |
| 181 |
| 182 FakeRefreshTokenStore fake_token_store; |
| 183 |
| 184 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) |
| 185 .Times(1); |
| 186 |
| 187 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) |
| 188 .Times(0); |
| 189 |
| 190 AppRemotingTestDriverEnvironment environment_object( |
| 191 kUserNameValue, |
| 192 kDevEnvironmentValue); |
| 193 |
| 194 environment_object.SetAccessTokenFetcherForTest(&mock_access_token_fetcher); |
| 195 |
| 196 environment_object.SetRefreshTokenStoreForTest(&fake_token_store); |
| 197 |
| 198 // Pass in an empty auth code since we are using a refresh token. |
| 199 bool init_result = environment_object.Initialize(std::string()); |
| 200 |
| 201 EXPECT_FALSE(init_result); |
| 202 EXPECT_FALSE(fake_token_store.refresh_token_write_attempted); |
| 203 } |
| 204 |
| 205 TEST(AppRemotingTestDriverEnvironmentTest, |
| 206 InitializeObjectNoAuthCodeOrRefreshToken) { |
| 207 MockAccessTokenFetcher mock_access_token_fetcher; |
| 208 |
| 209 mock_access_token_fetcher.SetAccessTokenFetcher( |
| 210 make_scoped_ptr(new FakeAccessTokenFetcher())); |
| 211 |
| 212 FakeRefreshTokenStore fake_token_store; |
| 213 |
| 214 // Neither method should be called in this scenario. |
| 215 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) |
| 216 .Times(0); |
| 217 |
| 218 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) |
| 219 .Times(0); |
| 220 |
| 221 AppRemotingTestDriverEnvironment environment_object( |
| 222 kUserNameValue, |
| 223 kDevEnvironmentValue); |
| 224 |
| 225 environment_object.SetAccessTokenFetcherForTest(&mock_access_token_fetcher); |
| 226 |
| 227 environment_object.SetRefreshTokenStoreForTest(&fake_token_store); |
| 228 |
| 229 // Clear out the 'stored' refresh token value. |
| 230 fake_token_store.refresh_token_value = ""; |
| 231 |
| 232 // Pass in an empty auth code. |
| 233 bool init_result = environment_object.Initialize(std::string()); |
| 234 |
| 235 // With no auth code or refresh token, then the initialization should fail. |
| 236 EXPECT_FALSE(init_result); |
| 237 EXPECT_FALSE(fake_token_store.refresh_token_write_attempted); |
| 238 } |
| 239 |
| 240 TEST(AppRemotingTestDriverEnvironmentTest, |
| 241 InitializeObjectWithAuthCodeWriteFailed) { |
| 242 MockAccessTokenFetcher mock_access_token_fetcher; |
| 243 |
| 244 mock_access_token_fetcher.SetAccessTokenFetcher( |
| 245 make_scoped_ptr(new FakeAccessTokenFetcher())); |
| 246 |
| 247 FakeRefreshTokenStore fake_token_store; |
| 248 |
| 249 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) |
| 250 .Times(1); |
| 251 |
| 252 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) |
| 253 .Times(0); |
| 254 |
| 255 AppRemotingTestDriverEnvironment environment_object( |
| 256 kUserNameValue, |
| 257 kDevEnvironmentValue); |
| 258 |
| 259 environment_object.SetAccessTokenFetcherForTest(&mock_access_token_fetcher); |
| 260 |
| 261 environment_object.SetRefreshTokenStoreForTest(&fake_token_store); |
| 262 |
| 263 // Simulate a failure writing the token to the disk. |
| 264 fake_token_store.refresh_token_write_succeeded = false; |
| 265 |
| 266 bool init_result = environment_object.Initialize(kAuthCodeValue); |
| 267 |
| 268 EXPECT_FALSE(init_result); |
| 269 EXPECT_TRUE(fake_token_store.refresh_token_write_attempted); |
| 270 } |
| 271 |
| 272 TEST(AppRemotingTestDriverEnvironmentTest, |
| 273 RefreshAccessTokenAfterUsingAuthCode) { |
| 274 MockAccessTokenFetcher mock_access_token_fetcher; |
| 275 |
| 276 mock_access_token_fetcher.SetAccessTokenFetcher( |
| 277 make_scoped_ptr(new FakeAccessTokenFetcher())); |
| 278 |
| 279 FakeRefreshTokenStore fake_token_store; |
| 280 |
| 281 { |
| 282 testing::Sequence call_sequence; |
| 283 |
| 284 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) |
| 285 .Times(1); |
| 286 |
| 287 EXPECT_CALL(mock_access_token_fetcher, |
| 288 GetAccessTokenFromRefreshToken(_, _)).Times(1); |
| 289 } |
| 290 |
| 291 AppRemotingTestDriverEnvironment environment_object( |
| 292 kUserNameValue, |
| 293 kDevEnvironmentValue); |
| 294 |
| 295 environment_object.SetAccessTokenFetcherForTest(&mock_access_token_fetcher); |
| 296 |
| 297 environment_object.SetRefreshTokenStoreForTest(&fake_token_store); |
| 298 |
| 299 bool init_result = environment_object.Initialize(kAuthCodeValue); |
| 300 |
| 301 EXPECT_TRUE(init_result); |
| 302 EXPECT_TRUE(fake_token_store.refresh_token_write_attempted); |
| 303 EXPECT_STREQ(kFakeAccessTokenFetcherRefreshTokenValue, |
| 304 fake_token_store.refresh_token_value_written.c_str()); |
| 305 EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); |
| 306 EXPECT_STREQ(kFakeAccessTokenFetcherAccessTokenValue, |
| 307 environment_object.access_token().c_str()); |
| 308 |
| 309 // Attempt to init again, we should not see any additional calls or errors. |
| 310 bool refresh_result = environment_object.RefreshAccessToken(); |
| 311 EXPECT_TRUE(refresh_result); |
| 312 } |
| 313 |
| 314 TEST(AppRemotingTestDriverEnvironmentTest, RefreshAccessTokenFailure) { |
| 315 MockAccessTokenFetcher mock_access_token_fetcher; |
| 316 |
| 317 // Use a raw pointer as we want to adjust behavior after we've handed off the |
| 318 // mock class. |
| 319 FakeAccessTokenFetcher* fake_access_token_fetcher = |
| 320 new FakeAccessTokenFetcher(); |
| 321 mock_access_token_fetcher.SetAccessTokenFetcher( |
| 322 make_scoped_ptr(fake_access_token_fetcher)); |
| 323 |
| 324 FakeRefreshTokenStore fake_token_store; |
| 325 |
| 326 { |
| 327 testing::Sequence call_sequence; |
| 328 |
| 329 // Mock is set up for this call to succeed. |
| 330 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) |
| 331 .Times(1); |
| 332 |
| 333 // Mock is set up for this call to fail. |
| 334 EXPECT_CALL(mock_access_token_fetcher, |
| 335 GetAccessTokenFromRefreshToken(_, _)).Times(1); |
| 336 } |
| 337 |
| 338 AppRemotingTestDriverEnvironment environment_object( |
| 339 kUserNameValue, |
| 340 kDevEnvironmentValue); |
| 341 |
| 342 environment_object.SetAccessTokenFetcherForTest(&mock_access_token_fetcher); |
| 343 |
| 344 environment_object.SetRefreshTokenStoreForTest(&fake_token_store); |
| 345 |
| 346 bool init_result = environment_object.Initialize(kAuthCodeValue); |
| 347 |
| 348 EXPECT_TRUE(init_result); |
| 349 EXPECT_TRUE(fake_token_store.refresh_token_write_attempted); |
| 350 EXPECT_STREQ(kFakeAccessTokenFetcherRefreshTokenValue, |
| 351 fake_token_store.refresh_token_value_written.c_str()); |
| 352 EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); |
| 353 EXPECT_STREQ(kFakeAccessTokenFetcherAccessTokenValue, |
| 354 environment_object.access_token().c_str()); |
| 355 |
| 356 fake_access_token_fetcher->set_fail_access_token_from_refresh_token(true); |
| 357 |
| 358 bool refresh_result = environment_object.RefreshAccessToken(); |
| 359 |
| 360 // We expect the refresh to have failed, the user name to remain valid, |
| 361 // and the access token to have been cleared. |
| 362 EXPECT_FALSE(refresh_result); |
| 363 EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); |
| 364 EXPECT_STREQ("", environment_object.access_token().c_str()); |
| 365 } |
| 366 |
| 367 } // namespace test |
| 368 } // namespace remoting |
OLD | NEW |