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 // Gives unit tests the ability to access/mutate private members of the | |
64 // AppRemotingTestDriverEnvironment class. | |
65 class AppRemotingTestDriverEnvironmentUnittestHelper { | |
Wez
2015/02/23 20:17:14
What's the benefit of having this class versus hav
joedow
2015/02/24 02:01:24
Done.
| |
66 public: | |
67 static void SetAccessTokenFetcher( | |
68 AppRemotingTestDriverEnvironment* environment, | |
69 scoped_ptr<AccessTokenFetcher> access_token_fetcher) { | |
70 DCHECK(environment); | |
71 DCHECK(access_token_fetcher); | |
72 | |
73 environment->test_access_token_fetcher_ = access_token_fetcher.Pass(); | |
74 } | |
75 | |
76 static void SetRefreshTokenStore( | |
77 AppRemotingTestDriverEnvironment* environment, | |
78 scoped_ptr<RefreshTokenStore> refresh_token_store) { | |
79 DCHECK(environment); | |
80 DCHECK(refresh_token_store); | |
81 | |
82 environment->refresh_token_store_ = refresh_token_store.Pass(); | |
83 } | |
84 | |
85 private: | |
86 AppRemotingTestDriverEnvironmentUnittestHelper() {} | |
87 ~AppRemotingTestDriverEnvironmentUnittestHelper() {} | |
88 }; | |
Wez
2015/02/23 20:17:14
DISALLOW_COPY_AND_ASSIGN
joedow
2015/02/24 02:01:24
Done.
| |
89 | |
90 TEST(AppRemotingTestDriverEnvironmentTest, InitializeObjectWithAuthCode) { | |
91 scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( | |
92 new MockAccessTokenFetcher()); | |
93 | |
94 mock_access_token_fetcher->SetFakeAccessTokenFetcher( | |
95 make_scoped_ptr(new FakeAccessTokenFetcher())); | |
96 | |
97 // We need to use the |fake_token_store| object for verification purposes so | |
98 // use a raw pointer here instead of a scoped_ptr. | |
99 FakeRefreshTokenStore* fake_token_store = new FakeRefreshTokenStore(); | |
100 | |
101 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) | |
102 .Times(1); | |
103 | |
104 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) | |
105 .Times(0); | |
106 | |
107 AppRemotingTestDriverEnvironment environment_object( | |
108 kUserNameValue, | |
109 kDevEnvironmentValue); | |
110 | |
111 AppRemotingTestDriverEnvironmentUnittestHelper::SetAccessTokenFetcher( | |
112 &environment_object, mock_access_token_fetcher.Pass()); | |
113 | |
114 // Pass in a new scoped_ptr to transfer ownership of |fake_token_store| to the | |
115 // environment object. The |fake_token_store| object will remain accessible | |
116 // until the environment object is destroyed. | |
117 AppRemotingTestDriverEnvironmentUnittestHelper::SetRefreshTokenStore( | |
118 &environment_object, make_scoped_ptr(fake_token_store)); | |
119 | |
120 bool init_result = environment_object.Initialize(kAuthCodeValue); | |
121 | |
122 EXPECT_TRUE(init_result); | |
123 EXPECT_TRUE(fake_token_store->refresh_token_write_attempted); | |
124 EXPECT_STREQ(kUserNameValue, | |
125 fake_token_store->user_name_written.c_str()); | |
126 EXPECT_STREQ(kFakeAccessTokenFetcherRefreshTokenValue, | |
127 fake_token_store->refresh_token_value_written.c_str()); | |
128 EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); | |
129 EXPECT_STREQ(kFakeAccessTokenFetcherAccessTokenValue, | |
130 environment_object.access_token().c_str()); | |
131 | |
132 // Attempt to init again, we should not see any additional calls or errors. | |
133 init_result = environment_object.Initialize(kAuthCodeValue); | |
134 EXPECT_TRUE(init_result); | |
135 } | |
136 | |
137 TEST(AppRemotingTestDriverEnvironmentTest, InitializeObjectWithAuthCodeFailed) { | |
138 scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( | |
139 new MockAccessTokenFetcher()); | |
140 | |
141 scoped_ptr<FakeAccessTokenFetcher> fake_access_token_fetcher( | |
142 new FakeAccessTokenFetcher()); | |
143 | |
144 fake_access_token_fetcher->SetAuthCodeError(true); | |
145 | |
146 mock_access_token_fetcher->SetFakeAccessTokenFetcher( | |
147 fake_access_token_fetcher.Pass()); | |
148 | |
149 // We need to use the |fake_token_store| object for verification purposes so | |
150 // use a raw pointer here instead of a scoped_ptr. | |
151 FakeRefreshTokenStore* fake_token_store = new FakeRefreshTokenStore(); | |
152 | |
153 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) | |
154 .Times(1); | |
155 | |
156 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) | |
157 .Times(0); | |
158 | |
159 AppRemotingTestDriverEnvironment environment_object( | |
160 kUserNameValue, | |
161 kDevEnvironmentValue); | |
162 | |
163 AppRemotingTestDriverEnvironmentUnittestHelper::SetAccessTokenFetcher( | |
164 &environment_object, mock_access_token_fetcher.Pass()); | |
165 | |
166 // Pass in a new scoped_ptr to transfer ownership of |fake_token_store| to the | |
167 // environment object. The |fake_token_store| object will remain accessible | |
168 // until the environment object is destroyed. | |
169 AppRemotingTestDriverEnvironmentUnittestHelper::SetRefreshTokenStore( | |
170 &environment_object, make_scoped_ptr(fake_token_store)); | |
171 | |
172 bool init_result = environment_object.Initialize(kAuthCodeValue); | |
173 | |
174 EXPECT_FALSE(init_result); | |
175 EXPECT_FALSE(fake_token_store->refresh_token_write_attempted); | |
176 } | |
177 | |
178 TEST(AppRemotingTestDriverEnvironmentTest, InitializeObjectWithRefreshToken) { | |
179 scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( | |
180 new MockAccessTokenFetcher()); | |
181 | |
182 mock_access_token_fetcher->SetFakeAccessTokenFetcher( | |
183 make_scoped_ptr(new FakeAccessTokenFetcher())); | |
184 | |
185 // We need to use the |fake_token_store| object for verification purposes so | |
186 // use a raw pointer here instead of a scoped_ptr. | |
187 FakeRefreshTokenStore* fake_token_store = new FakeRefreshTokenStore(); | |
188 | |
189 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) | |
190 .Times(1); | |
191 | |
192 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) | |
193 .Times(0); | |
194 | |
195 AppRemotingTestDriverEnvironment environment_object( | |
196 kUserNameValue, | |
197 kDevEnvironmentValue); | |
198 | |
199 AppRemotingTestDriverEnvironmentUnittestHelper::SetAccessTokenFetcher( | |
200 &environment_object, mock_access_token_fetcher.Pass()); | |
201 | |
202 // Pass in a new scoped_ptr to transfer ownership of |fake_token_store| to the | |
203 // environment object. The |fake_token_store| object will remain accessible | |
204 // until the environment object is destroyed. | |
205 AppRemotingTestDriverEnvironmentUnittestHelper::SetRefreshTokenStore( | |
206 &environment_object, make_scoped_ptr(fake_token_store)); | |
207 | |
208 // Pass in an empty auth code since we are using a refresh token. | |
209 bool init_result = environment_object.Initialize(std::string()); | |
210 | |
211 EXPECT_TRUE(init_result); | |
212 | |
213 // We should not write the refresh token a second time if we read from the | |
214 // disk originally. | |
215 EXPECT_FALSE(fake_token_store->refresh_token_write_attempted); | |
216 | |
217 // Verify the object was initialized correctly. | |
218 EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); | |
219 EXPECT_STREQ(kFakeAccessTokenFetcherAccessTokenValue, | |
220 environment_object.access_token().c_str()); | |
221 | |
222 // Attempt to init again, we should not see any additional calls or errors. | |
223 init_result = environment_object.Initialize(std::string()); | |
224 EXPECT_TRUE(init_result); | |
225 } | |
226 | |
227 TEST(AppRemotingTestDriverEnvironmentTest, | |
228 InitializeObjectWithRefreshTokenFailed) { | |
229 scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( | |
230 new MockAccessTokenFetcher()); | |
231 | |
232 scoped_ptr<FakeAccessTokenFetcher> fake_access_token_fetcher( | |
233 new FakeAccessTokenFetcher()); | |
234 | |
235 fake_access_token_fetcher->SetRefreshTokenError(true); | |
236 | |
237 mock_access_token_fetcher->SetFakeAccessTokenFetcher( | |
238 fake_access_token_fetcher.Pass()); | |
239 | |
240 // We need to use the |fake_token_store| object for verification purposes so | |
241 // use a raw pointer here instead of a scoped_ptr. | |
242 FakeRefreshTokenStore* fake_token_store = new FakeRefreshTokenStore(); | |
243 | |
244 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) | |
245 .Times(1); | |
246 | |
247 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) | |
248 .Times(0); | |
249 | |
250 AppRemotingTestDriverEnvironment environment_object( | |
251 kUserNameValue, | |
252 kDevEnvironmentValue); | |
253 | |
254 AppRemotingTestDriverEnvironmentUnittestHelper::SetAccessTokenFetcher( | |
255 &environment_object, mock_access_token_fetcher.Pass()); | |
256 | |
257 // Pass in a new scoped_ptr to transfer ownership of |fake_token_store| to the | |
258 // environment object. The |fake_token_store| object will remain accessible | |
259 // until the environment object is destroyed. | |
260 AppRemotingTestDriverEnvironmentUnittestHelper::SetRefreshTokenStore( | |
261 &environment_object, make_scoped_ptr(fake_token_store)); | |
262 | |
263 // Pass in an empty auth code since we are using a refresh token. | |
264 bool init_result = environment_object.Initialize(std::string()); | |
265 | |
266 EXPECT_FALSE(init_result); | |
267 EXPECT_FALSE(fake_token_store->refresh_token_write_attempted); | |
268 } | |
269 | |
270 TEST(AppRemotingTestDriverEnvironmentTest, | |
271 InitializeObjectNoAuthCodeOrRefreshToken) { | |
272 scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( | |
273 new MockAccessTokenFetcher()); | |
274 | |
275 mock_access_token_fetcher->SetFakeAccessTokenFetcher( | |
276 make_scoped_ptr(new FakeAccessTokenFetcher())); | |
277 | |
278 // We need to use the |fake_token_store| object for verification purposes so | |
279 // use a raw pointer here instead of a scoped_ptr. | |
280 FakeRefreshTokenStore* fake_token_store = new FakeRefreshTokenStore(); | |
281 | |
282 // Neither method should be called in this scenario. | |
283 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) | |
284 .Times(0); | |
285 | |
286 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) | |
287 .Times(0); | |
288 | |
289 AppRemotingTestDriverEnvironment environment_object( | |
290 kUserNameValue, | |
291 kDevEnvironmentValue); | |
292 | |
293 AppRemotingTestDriverEnvironmentUnittestHelper::SetAccessTokenFetcher( | |
294 &environment_object, mock_access_token_fetcher.Pass()); | |
295 | |
296 // Pass in a new scoped_ptr to transfer ownership of |fake_token_store| to the | |
297 // environment object. The |fake_token_store| object will remain accessible | |
298 // until the environment object is destroyed. | |
299 AppRemotingTestDriverEnvironmentUnittestHelper::SetRefreshTokenStore( | |
300 &environment_object, make_scoped_ptr(fake_token_store)); | |
301 | |
302 // Clear out the 'stored' refresh token value. | |
303 fake_token_store->refresh_token_value = ""; | |
304 | |
305 // Pass in an empty auth code. | |
306 bool init_result = environment_object.Initialize(std::string()); | |
307 | |
308 // With no auth code or refresh token, then the initialization should fail. | |
309 EXPECT_FALSE(init_result); | |
310 EXPECT_FALSE(fake_token_store->refresh_token_write_attempted); | |
311 } | |
312 | |
313 TEST(AppRemotingTestDriverEnvironmentTest, | |
314 InitializeObjectWithAuthCodeWriteFailed) { | |
315 scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( | |
316 new MockAccessTokenFetcher()); | |
317 | |
318 mock_access_token_fetcher->SetFakeAccessTokenFetcher( | |
319 make_scoped_ptr(new FakeAccessTokenFetcher())); | |
320 | |
321 // We need to use the |fake_token_store| object for verification purposes so | |
322 // use a raw pointer here instead of a scoped_ptr. | |
323 FakeRefreshTokenStore* fake_token_store = new FakeRefreshTokenStore(); | |
324 | |
325 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) | |
326 .Times(1); | |
327 | |
328 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) | |
329 .Times(0); | |
330 | |
331 AppRemotingTestDriverEnvironment environment_object( | |
332 kUserNameValue, | |
333 kDevEnvironmentValue); | |
334 | |
335 AppRemotingTestDriverEnvironmentUnittestHelper::SetAccessTokenFetcher( | |
336 &environment_object, mock_access_token_fetcher.Pass()); | |
337 | |
338 // Pass in a new scoped_ptr to transfer ownership of |fake_token_store| to the | |
339 // environment object. The |fake_token_store| object will remain accessible | |
340 // until the environment object is destroyed. | |
341 AppRemotingTestDriverEnvironmentUnittestHelper::SetRefreshTokenStore( | |
342 &environment_object, make_scoped_ptr(fake_token_store)); | |
343 | |
344 // Simulate a failure writing the token to the disk. | |
345 fake_token_store->refresh_token_write_succeeded = false; | |
346 | |
347 bool init_result = environment_object.Initialize(kAuthCodeValue); | |
348 | |
349 EXPECT_FALSE(init_result); | |
350 EXPECT_TRUE(fake_token_store->refresh_token_write_attempted); | |
351 } | |
352 | |
353 TEST(AppRemotingTestDriverEnvironmentTest, | |
354 RefreshAccessTokenAfterUsingAuthCode) { | |
355 scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( | |
356 new MockAccessTokenFetcher()); | |
357 | |
358 mock_access_token_fetcher->SetFakeAccessTokenFetcher( | |
359 make_scoped_ptr(new FakeAccessTokenFetcher())); | |
360 | |
361 // We need to use the |fake_token_store| object for verification purposes so | |
362 // use a raw pointer here instead of a scoped_ptr. | |
363 FakeRefreshTokenStore* fake_token_store = new FakeRefreshTokenStore(); | |
364 | |
365 { | |
366 testing::Sequence call_sequence; | |
367 | |
368 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) | |
369 .Times(1); | |
370 | |
371 EXPECT_CALL(*mock_access_token_fetcher, | |
372 GetAccessTokenFromRefreshToken(_, _)).Times(1); | |
373 } | |
374 | |
375 AppRemotingTestDriverEnvironment environment_object( | |
376 kUserNameValue, | |
377 kDevEnvironmentValue); | |
378 | |
379 AppRemotingTestDriverEnvironmentUnittestHelper::SetAccessTokenFetcher( | |
380 &environment_object, mock_access_token_fetcher.Pass()); | |
381 | |
382 // Pass in a new scoped_ptr to transfer ownership of |fake_token_store| to the | |
383 // environment object. The |fake_token_store| object will remain accessible | |
384 // until the environment object is destroyed. | |
385 AppRemotingTestDriverEnvironmentUnittestHelper::SetRefreshTokenStore( | |
386 &environment_object, make_scoped_ptr(fake_token_store)); | |
387 | |
388 bool init_result = environment_object.Initialize(kAuthCodeValue); | |
389 | |
390 EXPECT_TRUE(init_result); | |
391 EXPECT_TRUE(fake_token_store->refresh_token_write_attempted); | |
392 EXPECT_STREQ(kUserNameValue, | |
393 fake_token_store->user_name_written.c_str()); | |
394 EXPECT_STREQ(kFakeAccessTokenFetcherRefreshTokenValue, | |
395 fake_token_store->refresh_token_value_written.c_str()); | |
396 EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); | |
397 EXPECT_STREQ(kFakeAccessTokenFetcherAccessTokenValue, | |
398 environment_object.access_token().c_str()); | |
399 | |
400 // Attempt to init again, we should not see any additional calls or errors. | |
401 bool refresh_result = environment_object.RefreshAccessToken(); | |
402 EXPECT_TRUE(refresh_result); | |
403 } | |
404 | |
405 TEST(AppRemotingTestDriverEnvironmentTest, RefreshAccessTokenFailure) { | |
406 scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( | |
407 new MockAccessTokenFetcher()); | |
408 | |
409 // Use a raw pointer as we want to adjust behavior after we've handed off the | |
410 // mock class. | |
411 FakeAccessTokenFetcher* fake_access_token_fetcher = | |
412 new FakeAccessTokenFetcher(); | |
413 mock_access_token_fetcher->SetFakeAccessTokenFetcher( | |
414 make_scoped_ptr(fake_access_token_fetcher)); | |
415 | |
416 // We need to use the |fake_token_store| object for verification purposes so | |
417 // use a raw pointer here instead of a scoped_ptr. | |
418 FakeRefreshTokenStore* fake_token_store = new FakeRefreshTokenStore(); | |
419 | |
420 { | |
421 testing::Sequence call_sequence; | |
422 | |
423 // Mock is set up for this call to succeed. | |
424 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) | |
425 .Times(1); | |
426 | |
427 // Mock is set up for this call to fail. | |
428 EXPECT_CALL(*mock_access_token_fetcher, | |
429 GetAccessTokenFromRefreshToken(_, _)).Times(1); | |
430 } | |
431 | |
432 AppRemotingTestDriverEnvironment environment_object( | |
433 kUserNameValue, | |
434 kDevEnvironmentValue); | |
435 | |
436 AppRemotingTestDriverEnvironmentUnittestHelper::SetAccessTokenFetcher( | |
437 &environment_object, mock_access_token_fetcher.Pass()); | |
438 | |
439 // Pass in a new scoped_ptr to transfer ownership of |fake_token_store| to the | |
440 // environment object. The |fake_token_store| object will remain accessible | |
441 // until the environment object is destroyed. | |
442 AppRemotingTestDriverEnvironmentUnittestHelper::SetRefreshTokenStore( | |
443 &environment_object, make_scoped_ptr(fake_token_store)); | |
444 | |
445 bool init_result = environment_object.Initialize(kAuthCodeValue); | |
446 | |
447 EXPECT_TRUE(init_result); | |
448 EXPECT_TRUE(fake_token_store->refresh_token_write_attempted); | |
449 EXPECT_STREQ(kUserNameValue, | |
450 fake_token_store->user_name_written.c_str()); | |
451 EXPECT_STREQ(kFakeAccessTokenFetcherRefreshTokenValue, | |
452 fake_token_store->refresh_token_value_written.c_str()); | |
453 EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); | |
454 EXPECT_STREQ(kFakeAccessTokenFetcherAccessTokenValue, | |
455 environment_object.access_token().c_str()); | |
456 | |
457 fake_access_token_fetcher->SetRefreshTokenError(true); | |
458 | |
459 bool refresh_result = environment_object.RefreshAccessToken(); | |
460 | |
461 // We expect the refresh to have failed, the user name to remain valid, | |
462 // and the access token to have been cleared. | |
463 EXPECT_FALSE(refresh_result); | |
464 EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); | |
465 EXPECT_STREQ("", environment_object.access_token().c_str()); | |
466 } | |
467 | |
468 } // namespace test | |
469 } // namespace remoting | |
OLD | NEW |