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/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(const std::string& user_name) override { | |
| 37 return refresh_token_value; | |
| 38 }; | |
| 39 | |
| 40 bool StoreRefreshToken( | |
| 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 | |
| 60 DISALLOW_COPY_AND_ASSIGN(FakeRefreshTokenStore); | |
| 61 }; | |
| 62 | |
| 63 TEST(AppRemotingTestDriverEnvironmentTest, InitializeObjectWithAuthCode) { | |
| 64 scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( | |
| 65 new MockAccessTokenFetcher()); | |
| 66 | |
| 67 mock_access_token_fetcher->SetAccessTokenFetcher( | |
| 68 make_scoped_ptr(new FakeAccessTokenFetcher())); | |
| 69 | |
| 70 scoped_ptr<FakeRefreshTokenStore> fake_token_store( | |
| 71 new FakeRefreshTokenStore()); | |
|
Wez
2015/02/24 03:10:19
You could just create these (and the mock above) o
joedow1
2015/02/24 05:17:40
Done.
| |
| 72 | |
| 73 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) | |
| 74 .Times(1); | |
| 75 | |
| 76 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) | |
| 77 .Times(0); | |
| 78 | |
| 79 AppRemotingTestDriverEnvironment environment_object( | |
| 80 kUserNameValue, | |
| 81 kDevEnvironmentValue); | |
| 82 | |
| 83 environment_object.SetAccessTokenFetcherForTest( | |
| 84 mock_access_token_fetcher.get()); | |
| 85 | |
| 86 environment_object.SetRefreshTokenStoreForTest( | |
| 87 fake_token_store.get()); | |
| 88 | |
| 89 bool init_result = environment_object.Initialize(kAuthCodeValue); | |
| 90 | |
| 91 EXPECT_TRUE(init_result); | |
| 92 EXPECT_TRUE(fake_token_store->refresh_token_write_attempted); | |
| 93 EXPECT_STREQ(kUserNameValue, | |
| 94 fake_token_store->user_name_written.c_str()); | |
| 95 EXPECT_STREQ(kFakeAccessTokenFetcherRefreshTokenValue, | |
| 96 fake_token_store->refresh_token_value_written.c_str()); | |
| 97 EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); | |
| 98 EXPECT_STREQ(kFakeAccessTokenFetcherAccessTokenValue, | |
| 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 scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( | |
| 108 new MockAccessTokenFetcher()); | |
| 109 | |
| 110 scoped_ptr<FakeAccessTokenFetcher> fake_access_token_fetcher( | |
| 111 new FakeAccessTokenFetcher()); | |
| 112 | |
| 113 fake_access_token_fetcher->SetFailAccessTokenFromAuthCode(true); | |
| 114 | |
| 115 mock_access_token_fetcher->SetAccessTokenFetcher( | |
| 116 fake_access_token_fetcher.Pass()); | |
| 117 | |
| 118 scoped_ptr<FakeRefreshTokenStore> fake_token_store( | |
| 119 new FakeRefreshTokenStore()); | |
| 120 | |
| 121 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) | |
| 122 .Times(1); | |
| 123 | |
| 124 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) | |
| 125 .Times(0); | |
| 126 | |
| 127 AppRemotingTestDriverEnvironment environment_object( | |
| 128 kUserNameValue, | |
| 129 kDevEnvironmentValue); | |
| 130 | |
| 131 environment_object.SetAccessTokenFetcherForTest( | |
| 132 mock_access_token_fetcher.get()); | |
| 133 | |
| 134 environment_object.SetRefreshTokenStoreForTest( | |
| 135 fake_token_store.get()); | |
| 136 | |
| 137 bool init_result = environment_object.Initialize(kAuthCodeValue); | |
| 138 | |
| 139 EXPECT_FALSE(init_result); | |
| 140 EXPECT_FALSE(fake_token_store->refresh_token_write_attempted); | |
| 141 } | |
| 142 | |
| 143 TEST(AppRemotingTestDriverEnvironmentTest, InitializeObjectWithRefreshToken) { | |
| 144 scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( | |
| 145 new MockAccessTokenFetcher()); | |
| 146 | |
| 147 mock_access_token_fetcher->SetAccessTokenFetcher( | |
| 148 make_scoped_ptr(new FakeAccessTokenFetcher())); | |
| 149 | |
| 150 scoped_ptr<FakeRefreshTokenStore> fake_token_store( | |
| 151 new FakeRefreshTokenStore()); | |
| 152 | |
| 153 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) | |
| 154 .Times(1); | |
| 155 | |
| 156 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) | |
| 157 .Times(0); | |
| 158 | |
| 159 AppRemotingTestDriverEnvironment environment_object( | |
| 160 kUserNameValue, | |
| 161 kDevEnvironmentValue); | |
| 162 | |
| 163 environment_object.SetAccessTokenFetcherForTest( | |
| 164 mock_access_token_fetcher.get()); | |
| 165 | |
| 166 environment_object.SetRefreshTokenStoreForTest( | |
| 167 fake_token_store.get()); | |
| 168 | |
| 169 // Pass in an empty auth code since we are using a refresh token. | |
| 170 bool init_result = environment_object.Initialize(std::string()); | |
| 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_token_store->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(kFakeAccessTokenFetcherAccessTokenValue, | |
| 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(std::string()); | |
| 185 EXPECT_TRUE(init_result); | |
| 186 } | |
| 187 | |
| 188 TEST(AppRemotingTestDriverEnvironmentTest, | |
| 189 InitializeObjectWithRefreshTokenFailed) { | |
| 190 scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( | |
| 191 new MockAccessTokenFetcher()); | |
| 192 | |
| 193 scoped_ptr<FakeAccessTokenFetcher> fake_access_token_fetcher( | |
| 194 new FakeAccessTokenFetcher()); | |
| 195 | |
| 196 fake_access_token_fetcher->SetFailAccessTokenFromRefreshToken(true); | |
| 197 | |
| 198 mock_access_token_fetcher->SetAccessTokenFetcher( | |
| 199 fake_access_token_fetcher.Pass()); | |
| 200 | |
| 201 scoped_ptr<FakeRefreshTokenStore> fake_token_store( | |
| 202 new FakeRefreshTokenStore()); | |
| 203 | |
| 204 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) | |
| 205 .Times(1); | |
| 206 | |
| 207 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) | |
| 208 .Times(0); | |
| 209 | |
| 210 AppRemotingTestDriverEnvironment environment_object( | |
| 211 kUserNameValue, | |
| 212 kDevEnvironmentValue); | |
| 213 | |
| 214 environment_object.SetAccessTokenFetcherForTest( | |
| 215 mock_access_token_fetcher.get()); | |
| 216 | |
| 217 environment_object.SetRefreshTokenStoreForTest( | |
| 218 fake_token_store.get()); | |
| 219 | |
| 220 // Pass in an empty auth code since we are using a refresh token. | |
| 221 bool init_result = environment_object.Initialize(std::string()); | |
| 222 | |
| 223 EXPECT_FALSE(init_result); | |
| 224 EXPECT_FALSE(fake_token_store->refresh_token_write_attempted); | |
| 225 } | |
| 226 | |
| 227 TEST(AppRemotingTestDriverEnvironmentTest, | |
| 228 InitializeObjectNoAuthCodeOrRefreshToken) { | |
| 229 scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( | |
| 230 new MockAccessTokenFetcher()); | |
| 231 | |
| 232 mock_access_token_fetcher->SetAccessTokenFetcher( | |
| 233 make_scoped_ptr(new FakeAccessTokenFetcher())); | |
| 234 | |
| 235 scoped_ptr<FakeRefreshTokenStore> fake_token_store( | |
| 236 new FakeRefreshTokenStore()); | |
| 237 | |
| 238 // Neither method should be called in this scenario. | |
| 239 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) | |
| 240 .Times(0); | |
| 241 | |
| 242 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) | |
| 243 .Times(0); | |
| 244 | |
| 245 AppRemotingTestDriverEnvironment environment_object( | |
| 246 kUserNameValue, | |
| 247 kDevEnvironmentValue); | |
| 248 | |
| 249 environment_object.SetAccessTokenFetcherForTest( | |
| 250 mock_access_token_fetcher.get()); | |
| 251 | |
| 252 environment_object.SetRefreshTokenStoreForTest( | |
| 253 fake_token_store.get()); | |
| 254 | |
| 255 // Clear out the 'stored' refresh token value. | |
| 256 fake_token_store->refresh_token_value = ""; | |
| 257 | |
| 258 // Pass in an empty auth code. | |
| 259 bool init_result = environment_object.Initialize(std::string()); | |
| 260 | |
| 261 // With no auth code or refresh token, then the initialization should fail. | |
| 262 EXPECT_FALSE(init_result); | |
| 263 EXPECT_FALSE(fake_token_store->refresh_token_write_attempted); | |
| 264 } | |
| 265 | |
| 266 TEST(AppRemotingTestDriverEnvironmentTest, | |
| 267 InitializeObjectWithAuthCodeWriteFailed) { | |
| 268 scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( | |
| 269 new MockAccessTokenFetcher()); | |
| 270 | |
| 271 mock_access_token_fetcher->SetAccessTokenFetcher( | |
| 272 make_scoped_ptr(new FakeAccessTokenFetcher())); | |
| 273 | |
| 274 scoped_ptr<FakeRefreshTokenStore> fake_token_store( | |
| 275 new FakeRefreshTokenStore()); | |
| 276 | |
| 277 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) | |
| 278 .Times(1); | |
| 279 | |
| 280 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) | |
| 281 .Times(0); | |
| 282 | |
| 283 AppRemotingTestDriverEnvironment environment_object( | |
| 284 kUserNameValue, | |
| 285 kDevEnvironmentValue); | |
| 286 | |
| 287 environment_object.SetAccessTokenFetcherForTest( | |
| 288 mock_access_token_fetcher.get()); | |
| 289 | |
| 290 environment_object.SetRefreshTokenStoreForTest( | |
| 291 fake_token_store.get()); | |
| 292 | |
| 293 // Simulate a failure writing the token to the disk. | |
| 294 fake_token_store->refresh_token_write_succeeded = false; | |
| 295 | |
| 296 bool init_result = environment_object.Initialize(kAuthCodeValue); | |
| 297 | |
| 298 EXPECT_FALSE(init_result); | |
| 299 EXPECT_TRUE(fake_token_store->refresh_token_write_attempted); | |
| 300 } | |
| 301 | |
| 302 TEST(AppRemotingTestDriverEnvironmentTest, | |
| 303 RefreshAccessTokenAfterUsingAuthCode) { | |
| 304 scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( | |
| 305 new MockAccessTokenFetcher()); | |
| 306 | |
| 307 mock_access_token_fetcher->SetAccessTokenFetcher( | |
| 308 make_scoped_ptr(new FakeAccessTokenFetcher())); | |
| 309 | |
| 310 scoped_ptr<FakeRefreshTokenStore> fake_token_store( | |
| 311 new FakeRefreshTokenStore()); | |
| 312 | |
| 313 { | |
| 314 testing::Sequence call_sequence; | |
| 315 | |
| 316 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) | |
| 317 .Times(1); | |
| 318 | |
| 319 EXPECT_CALL(*mock_access_token_fetcher, | |
| 320 GetAccessTokenFromRefreshToken(_, _)).Times(1); | |
| 321 } | |
| 322 | |
| 323 AppRemotingTestDriverEnvironment environment_object( | |
| 324 kUserNameValue, | |
| 325 kDevEnvironmentValue); | |
| 326 | |
| 327 environment_object.SetAccessTokenFetcherForTest( | |
| 328 mock_access_token_fetcher.get()); | |
| 329 | |
| 330 environment_object.SetRefreshTokenStoreForTest( | |
| 331 fake_token_store.get()); | |
| 332 | |
| 333 bool init_result = environment_object.Initialize(kAuthCodeValue); | |
| 334 | |
| 335 EXPECT_TRUE(init_result); | |
| 336 EXPECT_TRUE(fake_token_store->refresh_token_write_attempted); | |
| 337 EXPECT_STREQ(kUserNameValue, | |
| 338 fake_token_store->user_name_written.c_str()); | |
| 339 EXPECT_STREQ(kFakeAccessTokenFetcherRefreshTokenValue, | |
| 340 fake_token_store->refresh_token_value_written.c_str()); | |
| 341 EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); | |
| 342 EXPECT_STREQ(kFakeAccessTokenFetcherAccessTokenValue, | |
| 343 environment_object.access_token().c_str()); | |
| 344 | |
| 345 // Attempt to init again, we should not see any additional calls or errors. | |
| 346 bool refresh_result = environment_object.RefreshAccessToken(); | |
| 347 EXPECT_TRUE(refresh_result); | |
| 348 } | |
| 349 | |
| 350 TEST(AppRemotingTestDriverEnvironmentTest, RefreshAccessTokenFailure) { | |
| 351 scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( | |
| 352 new MockAccessTokenFetcher()); | |
| 353 | |
| 354 // Use a raw pointer as we want to adjust behavior after we've handed off the | |
| 355 // mock class. | |
| 356 FakeAccessTokenFetcher* fake_access_token_fetcher = | |
| 357 new FakeAccessTokenFetcher(); | |
| 358 mock_access_token_fetcher->SetAccessTokenFetcher( | |
| 359 make_scoped_ptr(fake_access_token_fetcher)); | |
| 360 | |
| 361 scoped_ptr<FakeRefreshTokenStore> fake_token_store( | |
| 362 new FakeRefreshTokenStore()); | |
| 363 | |
| 364 { | |
| 365 testing::Sequence call_sequence; | |
| 366 | |
| 367 // Mock is set up for this call to succeed. | |
| 368 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) | |
| 369 .Times(1); | |
| 370 | |
| 371 // Mock is set up for this call to fail. | |
| 372 EXPECT_CALL(*mock_access_token_fetcher, | |
| 373 GetAccessTokenFromRefreshToken(_, _)).Times(1); | |
| 374 } | |
| 375 | |
| 376 AppRemotingTestDriverEnvironment environment_object( | |
| 377 kUserNameValue, | |
| 378 kDevEnvironmentValue); | |
| 379 | |
| 380 environment_object.SetAccessTokenFetcherForTest( | |
| 381 mock_access_token_fetcher.get()); | |
| 382 | |
| 383 environment_object.SetRefreshTokenStoreForTest( | |
| 384 fake_token_store.get()); | |
| 385 | |
| 386 bool init_result = environment_object.Initialize(kAuthCodeValue); | |
| 387 | |
| 388 EXPECT_TRUE(init_result); | |
| 389 EXPECT_TRUE(fake_token_store->refresh_token_write_attempted); | |
| 390 EXPECT_STREQ(kUserNameValue, | |
| 391 fake_token_store->user_name_written.c_str()); | |
| 392 EXPECT_STREQ(kFakeAccessTokenFetcherRefreshTokenValue, | |
| 393 fake_token_store->refresh_token_value_written.c_str()); | |
| 394 EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); | |
| 395 EXPECT_STREQ(kFakeAccessTokenFetcherAccessTokenValue, | |
| 396 environment_object.access_token().c_str()); | |
| 397 | |
| 398 fake_access_token_fetcher->SetFailAccessTokenFromRefreshToken(true); | |
| 399 | |
| 400 bool refresh_result = environment_object.RefreshAccessToken(); | |
| 401 | |
| 402 // We expect the refresh to have failed, the user name to remain valid, | |
| 403 // and the access token to have been cleared. | |
| 404 EXPECT_FALSE(refresh_result); | |
| 405 EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); | |
| 406 EXPECT_STREQ("", environment_object.access_token().c_str()); | |
| 407 } | |
| 408 | |
| 409 } // namespace test | |
| 410 } // namespace remoting | |
| OLD | NEW |