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 // Mocks out the file API and returns fake data so we can remove | |
25 // file system dependencies in this unit test. | |
26 class FakeRefreshTokenStore : public RefreshTokenStoreInterface { | |
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 ReadRefreshTokenFromDisk(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 | |
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->SetFakeAccessTokenFetcher( | |
68 make_scoped_ptr(new FakeAccessTokenFetcher())); | |
69 | |
70 FakeRefreshTokenStore* fake_token_store = new FakeRefreshTokenStore(); | |
71 | |
72 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) | |
73 .Times(1); | |
74 | |
75 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) | |
76 .Times(0); | |
77 | |
78 AppRemotingTestDriverEnvironment environment_object( | |
79 kUserNameValue, | |
80 kDevEnvironmentValue); | |
81 | |
82 environment_object.SetAccessTokenFetcherForTest( | |
83 mock_access_token_fetcher.Pass()); | |
84 | |
85 // We will continue to use the token_store object as it will be alive until | |
86 // the environment object is destructed, but we do not need to free it. | |
Wez
2015/02/19 22:00:24
I don't understand this comment - there's no "toke
joedow
2015/02/20 02:58:36
Done.
| |
87 environment_object.SetRefreshTokenStoreForTest( | |
88 make_scoped_ptr(fake_token_store)); | |
89 | |
90 bool init_result = environment_object.Initialize(kAuthCodeValue); | |
91 | |
92 EXPECT_TRUE(init_result); | |
93 EXPECT_TRUE(fake_token_store->refresh_token_write_attempted); | |
94 EXPECT_STREQ(kUserNameValue, | |
95 fake_token_store->user_name_written.c_str()); | |
96 EXPECT_STREQ(kFakeAccessTokenFetcherRefreshTokenValue, | |
97 fake_token_store->refresh_token_value_written.c_str()); | |
98 EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); | |
99 EXPECT_STREQ(kFakeAccessTokenFetcherAccessTokenValue, | |
100 environment_object.access_token().c_str()); | |
101 | |
102 // Attempt to init again, we should not see any additional calls or errors. | |
103 init_result = environment_object.Initialize(kAuthCodeValue); | |
104 EXPECT_TRUE(init_result); | |
105 } | |
106 | |
107 TEST(AppRemotingTestDriverEnvironmentTest, InitializeObjectWithAuthCodeFailed) { | |
108 scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( | |
109 new MockAccessTokenFetcher()); | |
110 | |
111 scoped_ptr<FakeAccessTokenFetcher> fake_access_token_fetcher( | |
112 new FakeAccessTokenFetcher()); | |
113 | |
114 fake_access_token_fetcher->SetAuthCodeError(true); | |
115 | |
116 mock_access_token_fetcher->SetFakeAccessTokenFetcher( | |
117 fake_access_token_fetcher.Pass()); | |
118 | |
119 FakeRefreshTokenStore* fake_token_store = 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.Pass()); | |
133 | |
134 // We will continue to use the token_store object as it will be alive until | |
135 // the environment object is destructed, but we do not need to free it. | |
136 environment_object.SetRefreshTokenStoreForTest( | |
137 make_scoped_ptr(fake_token_store)); | |
138 | |
139 bool init_result = environment_object.Initialize(kAuthCodeValue); | |
140 | |
141 EXPECT_FALSE(init_result); | |
142 EXPECT_FALSE(fake_token_store->refresh_token_write_attempted); | |
143 } | |
144 | |
145 TEST(AppRemotingTestDriverEnvironmentTest, InitializeObjectWithRefreshToken) { | |
146 scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( | |
147 new MockAccessTokenFetcher()); | |
148 | |
149 mock_access_token_fetcher->SetFakeAccessTokenFetcher( | |
150 make_scoped_ptr(new FakeAccessTokenFetcher())); | |
151 | |
152 FakeRefreshTokenStore* fake_token_store = new FakeRefreshTokenStore(); | |
153 | |
154 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) | |
155 .Times(1); | |
156 | |
157 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) | |
158 .Times(0); | |
159 | |
160 AppRemotingTestDriverEnvironment environment_object( | |
161 kUserNameValue, | |
162 kDevEnvironmentValue); | |
163 | |
164 environment_object.SetAccessTokenFetcherForTest( | |
165 mock_access_token_fetcher.Pass()); | |
166 | |
167 // We will continue to use the token_store object as it will be alive until | |
168 // the environment object is destructed, but we do not need to free it. | |
169 environment_object.SetRefreshTokenStoreForTest( | |
170 make_scoped_ptr(fake_token_store)); | |
171 | |
172 // Pass in an empty auth code since we are using a refresh token. | |
173 bool init_result = environment_object.Initialize(std::string()); | |
174 | |
175 EXPECT_TRUE(init_result); | |
176 | |
177 // We should not write the refresh token a second time if we read from the | |
178 // disk originally. | |
179 EXPECT_FALSE(fake_token_store->refresh_token_write_attempted); | |
180 | |
181 // Verify the object was initialized correctly. | |
182 EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); | |
183 EXPECT_STREQ(kFakeAccessTokenFetcherAccessTokenValue, | |
184 environment_object.access_token().c_str()); | |
185 | |
186 // Attempt to init again, we should not see any additional calls or errors. | |
187 init_result = environment_object.Initialize(std::string()); | |
188 EXPECT_TRUE(init_result); | |
189 } | |
190 | |
191 TEST(AppRemotingTestDriverEnvironmentTest, | |
192 InitializeObjectWithRefreshTokenFailed) { | |
193 scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( | |
194 new MockAccessTokenFetcher()); | |
195 | |
196 scoped_ptr<FakeAccessTokenFetcher> fake_access_token_fetcher( | |
197 new FakeAccessTokenFetcher()); | |
198 | |
199 fake_access_token_fetcher->SetRefreshTokenError(true); | |
200 | |
201 mock_access_token_fetcher->SetFakeAccessTokenFetcher( | |
202 fake_access_token_fetcher.Pass()); | |
203 | |
204 FakeRefreshTokenStore* fake_token_store = new FakeRefreshTokenStore(); | |
205 | |
206 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) | |
207 .Times(1); | |
208 | |
209 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) | |
210 .Times(0); | |
211 | |
212 AppRemotingTestDriverEnvironment environment_object( | |
213 kUserNameValue, | |
214 kDevEnvironmentValue); | |
215 | |
216 environment_object.SetAccessTokenFetcherForTest( | |
217 mock_access_token_fetcher.Pass()); | |
218 | |
219 // We will continue to use the token_store object as it will be alive until | |
220 // the environment object is destructed, but we do not need to free it. | |
221 environment_object.SetRefreshTokenStoreForTest( | |
222 make_scoped_ptr(fake_token_store)); | |
223 | |
224 // Pass in an empty auth code since we are using a refresh token. | |
225 bool init_result = environment_object.Initialize(std::string()); | |
226 | |
227 EXPECT_FALSE(init_result); | |
228 EXPECT_FALSE(fake_token_store->refresh_token_write_attempted); | |
229 } | |
230 | |
231 TEST(AppRemotingTestDriverEnvironmentTest, | |
232 InitializeObjectNoAuthCodeOrRefreshToken) { | |
233 scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( | |
234 new MockAccessTokenFetcher()); | |
235 | |
236 mock_access_token_fetcher->SetFakeAccessTokenFetcher( | |
237 make_scoped_ptr(new FakeAccessTokenFetcher())); | |
238 | |
239 FakeRefreshTokenStore* fake_token_store = new FakeRefreshTokenStore(); | |
240 | |
241 // Neither method should be called in this scenario. | |
242 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) | |
243 .Times(0); | |
244 | |
245 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) | |
246 .Times(0); | |
247 | |
248 AppRemotingTestDriverEnvironment environment_object( | |
249 kUserNameValue, | |
250 kDevEnvironmentValue); | |
251 | |
252 environment_object.SetAccessTokenFetcherForTest( | |
253 mock_access_token_fetcher.Pass()); | |
254 | |
255 // We will continue to use the token_store object as it will be alive until | |
256 // the environment object is destructed, but we do not need to free it. | |
257 environment_object.SetRefreshTokenStoreForTest( | |
258 make_scoped_ptr(fake_token_store)); | |
259 | |
260 // Clear out the 'stored' refresh token value. | |
261 fake_token_store->refresh_token_value = ""; | |
262 | |
263 // Pass in an empty auth code. | |
264 bool init_result = environment_object.Initialize(std::string()); | |
265 | |
266 // With no auth code or refresh token, then the initialization should fail. | |
267 EXPECT_FALSE(init_result); | |
268 EXPECT_FALSE(fake_token_store->refresh_token_write_attempted); | |
269 } | |
270 | |
271 TEST(AppRemotingTestDriverEnvironmentTest, | |
272 InitializeObjectWithAuthCodeWriteFailed) { | |
273 scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( | |
274 new MockAccessTokenFetcher()); | |
275 | |
276 mock_access_token_fetcher->SetFakeAccessTokenFetcher( | |
277 make_scoped_ptr(new FakeAccessTokenFetcher())); | |
278 | |
279 FakeRefreshTokenStore* fake_token_store = new FakeRefreshTokenStore(); | |
280 | |
281 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) | |
282 .Times(1); | |
283 | |
284 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _)) | |
285 .Times(0); | |
286 | |
287 AppRemotingTestDriverEnvironment environment_object( | |
288 kUserNameValue, | |
289 kDevEnvironmentValue); | |
290 | |
291 environment_object.SetAccessTokenFetcherForTest( | |
292 mock_access_token_fetcher.Pass()); | |
293 | |
294 // We will continue to use the token_store object as it will be alive until | |
295 // the environment object is destructed, but we do not need to free it. | |
296 environment_object.SetRefreshTokenStoreForTest( | |
297 make_scoped_ptr(fake_token_store)); | |
298 | |
299 // Simulate a failure writing the token to the disk. | |
300 fake_token_store->refresh_token_write_succeeded = false; | |
301 | |
302 bool init_result = environment_object.Initialize(kAuthCodeValue); | |
303 | |
304 EXPECT_FALSE(init_result); | |
305 EXPECT_TRUE(fake_token_store->refresh_token_write_attempted); | |
306 } | |
307 | |
308 TEST(AppRemotingTestDriverEnvironmentTest, | |
309 RefreshAccessTokenAfterUsingAuthCode) { | |
310 scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( | |
311 new MockAccessTokenFetcher()); | |
312 | |
313 mock_access_token_fetcher->SetFakeAccessTokenFetcher( | |
314 make_scoped_ptr(new FakeAccessTokenFetcher())); | |
315 | |
316 FakeRefreshTokenStore* fake_token_store = new FakeRefreshTokenStore(); | |
317 | |
318 { | |
319 testing::Sequence call_sequence; | |
320 | |
321 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) | |
322 .Times(1); | |
323 | |
324 EXPECT_CALL(*mock_access_token_fetcher, | |
325 GetAccessTokenFromRefreshToken(_, _)).Times(1); | |
326 } | |
327 | |
328 AppRemotingTestDriverEnvironment environment_object( | |
329 kUserNameValue, | |
330 kDevEnvironmentValue); | |
331 | |
332 environment_object.SetAccessTokenFetcherForTest( | |
333 mock_access_token_fetcher.Pass()); | |
334 | |
335 // We will continue to use the token_store object as it will be alive until | |
336 // the environment object is destructed, but we do not need to free it. | |
337 environment_object.SetRefreshTokenStoreForTest( | |
338 make_scoped_ptr(fake_token_store)); | |
339 | |
340 bool init_result = environment_object.Initialize(kAuthCodeValue); | |
341 | |
342 EXPECT_TRUE(init_result); | |
343 EXPECT_TRUE(fake_token_store->refresh_token_write_attempted); | |
344 EXPECT_STREQ(kUserNameValue, | |
345 fake_token_store->user_name_written.c_str()); | |
346 EXPECT_STREQ(kFakeAccessTokenFetcherRefreshTokenValue, | |
347 fake_token_store->refresh_token_value_written.c_str()); | |
348 EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); | |
349 EXPECT_STREQ(kFakeAccessTokenFetcherAccessTokenValue, | |
350 environment_object.access_token().c_str()); | |
351 | |
352 // Attempt to init again, we should not see any additional calls or errors. | |
353 bool refresh_result = environment_object.RefreshAccessToken(); | |
354 EXPECT_TRUE(refresh_result); | |
355 } | |
356 | |
357 TEST(AppRemotingTestDriverEnvironmentTest, RefreshAccessTokenFailure) { | |
358 scoped_ptr<MockAccessTokenFetcher> mock_access_token_fetcher( | |
359 new MockAccessTokenFetcher()); | |
360 | |
361 // Use a raw pointer as we want to adjust behavior after we've handed off the | |
362 // mock class. | |
363 FakeAccessTokenFetcher* fake_access_token_fetcher = | |
364 new FakeAccessTokenFetcher(); | |
365 mock_access_token_fetcher->SetFakeAccessTokenFetcher( | |
366 make_scoped_ptr(fake_access_token_fetcher)); | |
367 | |
368 FakeRefreshTokenStore* fake_token_store = new FakeRefreshTokenStore(); | |
369 | |
370 { | |
371 testing::Sequence call_sequence; | |
372 | |
373 // Mock is set up for this call to succeed. | |
374 EXPECT_CALL(*mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _)) | |
375 .Times(1); | |
376 | |
377 // Mock is set up for this call to fail. | |
378 EXPECT_CALL(*mock_access_token_fetcher, | |
379 GetAccessTokenFromRefreshToken(_, _)).Times(1); | |
380 } | |
381 | |
382 AppRemotingTestDriverEnvironment environment_object( | |
383 kUserNameValue, | |
384 kDevEnvironmentValue); | |
385 | |
386 environment_object.SetAccessTokenFetcherForTest( | |
387 mock_access_token_fetcher.Pass()); | |
388 | |
389 // We will continue to use the token_store object as it will be alive until | |
390 // the environment object is destructed, but we do not need to free it. | |
391 environment_object.SetRefreshTokenStoreForTest( | |
392 make_scoped_ptr(fake_token_store)); | |
393 | |
394 bool init_result = environment_object.Initialize(kAuthCodeValue); | |
395 | |
396 EXPECT_TRUE(init_result); | |
397 EXPECT_TRUE(fake_token_store->refresh_token_write_attempted); | |
398 EXPECT_STREQ(kUserNameValue, | |
399 fake_token_store->user_name_written.c_str()); | |
400 EXPECT_STREQ(kFakeAccessTokenFetcherRefreshTokenValue, | |
401 fake_token_store->refresh_token_value_written.c_str()); | |
402 EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); | |
403 EXPECT_STREQ(kFakeAccessTokenFetcherAccessTokenValue, | |
404 environment_object.access_token().c_str()); | |
405 | |
406 fake_access_token_fetcher->SetRefreshTokenError(true); | |
407 | |
408 bool refresh_result = environment_object.RefreshAccessToken(); | |
409 | |
410 // We expect the refresh to have failed, the user name to remain valid, | |
411 // and the access token to have been cleared. | |
412 EXPECT_FALSE(refresh_result); | |
413 EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str()); | |
414 EXPECT_STREQ("", environment_object.access_token().c_str()); | |
415 } | |
416 | |
417 } // namespace test | |
418 } // namespace remoting | |
OLD | NEW |