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