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/chromoting_test_driver_environment.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "base/files/file_path.h" | |
10 #include "remoting/test/fake_access_token_fetcher.h" | |
11 #include "remoting/test/fake_app_remoting_report_issue_request.h" | |
12 #include "remoting/test/fake_host_list_fetcher.h" | |
13 #include "remoting/test/fake_refresh_token_store.h" | |
14 #include "remoting/test/refresh_token_store.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 namespace { | |
18 const char kAuthCodeValue[] = "4/892379827345jkefvkdfbv"; | |
19 const char kUserNameValue[] = "remoting_user@gmail.com"; | |
20 const char kHostNameValue[] = "remote_host_name"; | |
21 const char kFakeHostNameValue[] = "fake_host_name"; | |
22 const char kFakeHostIdValue[] = "fake_host_id"; | |
23 const char kFakeHostJidValue[] = "fake_host_jid"; | |
24 const char kFakeHostOfflineReasonValue[] = "fake_offline_reason"; | |
25 const char kFakeHostPublicKeyValue[] = "fake_public_key"; | |
26 const char kFakeHostFirstTokenUrlValue[] = "token_url_1"; | |
27 const char kFakeHostSecondTokenUrlValue[] = "token_url_2"; | |
28 const char kFakeHostThirdTokenUrlValue[] = "token_url_3"; | |
29 } // namespace | |
30 | |
31 namespace remoting { | |
32 namespace test { | |
33 | |
34 class ChromotingTestDriverEnvironmentTest : public ::testing::Test { | |
35 public: | |
36 ChromotingTestDriverEnvironmentTest(); | |
37 ~ChromotingTestDriverEnvironmentTest() override; | |
38 | |
39 protected: | |
40 void Initialize(); | |
41 void Initialize( | |
42 const ChromotingTestDriverEnvironment::EnvironmentOptions& options); | |
joedow
2015/07/15 03:04:53
The app remoting counterpart to this file uses two
tonychun
2015/07/15 17:24:49
Done.
| |
43 | |
44 FakeAccessTokenFetcher fake_access_token_fetcher_; | |
45 FakeRefreshTokenStore fake_token_store_; | |
46 FakeHostListFetcher fake_host_list_fetcher_; | |
47 | |
48 scoped_ptr<ChromotingTestDriverEnvironment> environment_object_; | |
49 | |
50 private: | |
51 DISALLOW_COPY_AND_ASSIGN(ChromotingTestDriverEnvironmentTest); | |
52 }; | |
53 | |
54 ChromotingTestDriverEnvironmentTest::ChromotingTestDriverEnvironmentTest() { | |
55 } | |
56 | |
57 ChromotingTestDriverEnvironmentTest::~ChromotingTestDriverEnvironmentTest() { | |
58 } | |
59 | |
60 void ChromotingTestDriverEnvironmentTest::Initialize() { | |
61 ChromotingTestDriverEnvironment::EnvironmentOptions options; | |
62 options.user_name = kUserNameValue; | |
63 options.host_name = kHostNameValue; | |
64 | |
65 Initialize(options); | |
66 } | |
67 | |
68 void ChromotingTestDriverEnvironmentTest::Initialize( | |
69 const ChromotingTestDriverEnvironment::EnvironmentOptions& options) { | |
70 environment_object_.reset(new ChromotingTestDriverEnvironment(options)); | |
71 | |
72 std::vector<HostInfo> fake_host_list; | |
73 HostInfo fake_host; | |
74 fake_host.host_id = kFakeHostIdValue; | |
75 fake_host.host_jid = kFakeHostJidValue; | |
76 fake_host.host_name = kFakeHostNameValue; | |
77 fake_host.offline_reason = kFakeHostOfflineReasonValue; | |
78 fake_host.public_key = kFakeHostPublicKeyValue; | |
79 fake_host.token_url_patterns = {kFakeHostFirstTokenUrlValue, | |
80 kFakeHostSecondTokenUrlValue, | |
81 kFakeHostThirdTokenUrlValue}; | |
82 fake_host_list.push_back(fake_host); | |
83 | |
84 fake_host_list_fetcher_.set_retrieved_host_list(fake_host_list); | |
85 | |
86 environment_object_->SetAccessTokenFetcherForTest( | |
87 &fake_access_token_fetcher_); | |
88 environment_object_->SetRefreshTokenStoreForTest(&fake_token_store_); | |
89 environment_object_->SetHostListFetcherForTest(&fake_host_list_fetcher_); | |
90 } | |
91 | |
92 | |
93 TEST_F(ChromotingTestDriverEnvironmentTest, InitializeObjectWithAuthCode) { | |
94 Initialize(); | |
95 | |
96 EXPECT_TRUE(environment_object_->Initialize(kAuthCodeValue)); | |
97 EXPECT_TRUE(fake_token_store_.refresh_token_write_attempted()); | |
98 EXPECT_EQ(fake_token_store_.stored_refresh_token_value(), | |
99 kFakeAccessTokenFetcherRefreshTokenValue); | |
100 EXPECT_EQ(environment_object_->user_name(), kUserNameValue); | |
101 EXPECT_EQ(environment_object_->host_name(), kHostNameValue); | |
102 EXPECT_FALSE(environment_object_->host_list().empty()); | |
103 EXPECT_EQ(environment_object_->access_token(), | |
104 kFakeAccessTokenFetcherAccessTokenValue); | |
105 | |
106 // Attempt to init again, we should not see any additional calls or errors. | |
joedow
2015/07/15 03:04:53
Please update this string, it mentions 'any additi
joedow
2015/07/15 03:04:53
Also, thanks for adding unit tests! :)
tonychun
2015/07/15 17:24:49
Done.
tonychun
2015/07/15 17:24:49
Done.
| |
107 EXPECT_TRUE(environment_object_->Initialize(kAuthCodeValue)); | |
108 } | |
109 | |
110 TEST_F(ChromotingTestDriverEnvironmentTest, | |
111 InitializeObjectWithAuthCodeFailed) { | |
112 Initialize(); | |
113 | |
114 fake_access_token_fetcher_.set_fail_access_token_from_auth_code(true); | |
115 | |
116 EXPECT_FALSE(environment_object_->Initialize(kAuthCodeValue)); | |
117 EXPECT_FALSE(fake_token_store_.refresh_token_write_attempted()); | |
118 } | |
119 | |
120 TEST_F(ChromotingTestDriverEnvironmentTest, InitializeObjectWithRefreshToken) { | |
121 Initialize(); | |
122 | |
123 // Pass in an empty auth code since we are using a refresh token. | |
124 EXPECT_TRUE(environment_object_->Initialize(std::string())); | |
125 | |
126 // We should not write the refresh token a second time if we read from the | |
127 // disk originally. | |
128 EXPECT_FALSE(fake_token_store_.refresh_token_write_attempted()); | |
129 | |
130 // Verify the object was initialized correctly. | |
131 EXPECT_EQ(environment_object_->user_name(), kUserNameValue); | |
132 EXPECT_EQ(environment_object_->host_name(), kHostNameValue); | |
133 EXPECT_FALSE(environment_object_->host_list().empty()); | |
134 EXPECT_EQ(environment_object_->access_token(), | |
135 kFakeAccessTokenFetcherAccessTokenValue); | |
136 | |
137 // Attempt to init again, we should not see any additional calls or errors. | |
138 EXPECT_TRUE(environment_object_->Initialize(std::string())); | |
139 } | |
140 | |
141 TEST_F(ChromotingTestDriverEnvironmentTest, | |
142 InitializeObjectWithRefreshTokenFailed) { | |
143 Initialize(); | |
144 | |
145 fake_access_token_fetcher_.set_fail_access_token_from_refresh_token(true); | |
146 | |
147 // Pass in an empty auth code since we are using a refresh token. | |
148 EXPECT_FALSE(environment_object_->Initialize(std::string())); | |
149 EXPECT_FALSE(fake_token_store_.refresh_token_write_attempted()); | |
150 } | |
151 | |
152 TEST_F(ChromotingTestDriverEnvironmentTest, TearDownAfterInitializeSucceeds) { | |
153 Initialize(); | |
154 | |
155 // Pass in an empty auth code since we are using a refresh token. | |
156 EXPECT_TRUE(environment_object_->Initialize(std::string())); | |
157 | |
158 // Note: We are using a static cast here because the TearDown() method is | |
159 // private as it is an interface method that we only want to call | |
160 // directly in tests or by the GTEST framework. | |
161 static_cast<testing::Environment*>(environment_object_.get())->TearDown(); | |
162 } | |
163 | |
164 TEST_F(ChromotingTestDriverEnvironmentTest, | |
165 InitializeObjectNoAuthCodeOrRefreshToken) { | |
166 Initialize(); | |
167 | |
168 // Clear out the 'stored' refresh token value. | |
169 fake_token_store_.set_refresh_token_value(std::string()); | |
170 | |
171 // With no auth code or refresh token, then the initialization should fail. | |
172 EXPECT_FALSE(environment_object_->Initialize(std::string())); | |
173 EXPECT_FALSE(fake_token_store_.refresh_token_write_attempted()); | |
174 } | |
175 | |
176 TEST_F(ChromotingTestDriverEnvironmentTest, | |
177 InitializeObjectWithAuthCodeWriteFailed) { | |
178 Initialize(); | |
179 | |
180 // Simulate a failure writing the token to the disk. | |
181 fake_token_store_.set_refresh_token_write_succeeded(false); | |
182 | |
183 EXPECT_FALSE(environment_object_->Initialize(kAuthCodeValue)); | |
184 EXPECT_TRUE(fake_token_store_.refresh_token_write_attempted()); | |
185 } | |
186 | |
187 TEST_F(ChromotingTestDriverEnvironmentTest, HostListEmptyFromDirectory) { | |
188 Initialize(); | |
189 | |
190 // Set the host list fetcher to return an empty list. | |
191 fake_host_list_fetcher_.set_retrieved_host_list(std::vector<HostInfo>()); | |
192 | |
193 EXPECT_FALSE(environment_object_->Initialize(kAuthCodeValue)); | |
194 EXPECT_TRUE(fake_token_store_.refresh_token_write_attempted()); | |
195 } | |
196 | |
197 } // namespace test | |
198 } // namespace remoting | |
OLD | NEW |