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.
5 #include "remoting/test/remote_host_info_fetcher.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "net/url_request/test_url_fetcher_factory.h"
11 #include "testing/gtest/include/gtest/gtest.h"
14 const char kTestApplicationId
[] = "klasdfjlkasdfjklasjfdkljsadf";
15 const char kAccessTokenValue
[] = "test_access_token_value";
16 const char kRemoteHostInfoReadyResponse
[] =
18 " \"status\": \"done\","
20 " \"kind\": \"test_kind\","
21 " \"applicationId\": \"klasdfjlkasdfjklasjfdkljsadf\","
22 " \"hostId\": \"test_host_id\""
24 " \"hostJid\": \"test_host_jid\","
25 " \"authorizationCode\": \"test_authorization_code\","
26 " \"sharedSecret\": \"test_shared_secret\""
28 const char kRemoteHostInfoPendingResponse
[] =
30 " \"status\": \"pending\""
32 const char kRemoteHostInfoEmptyResponse
[] = "{}";
38 // Provides base functionality for the RemoteHostInfoFetcher Tests below. The
39 // FakeURLFetcherFactory allows us to override the response data and payload for
40 // specified URLs. We use this to stub out network calls made by the
41 // RemoteHostInfoFetcher. This fixture also creates an IO MessageLoop, if
42 // necessary, for use by the RemoteHostInfoFetcher.
43 class RemoteHostInfoFetcherTest
: public ::testing::Test
{
45 RemoteHostInfoFetcherTest() : url_fetcher_factory_(nullptr) {}
46 ~RemoteHostInfoFetcherTest() override
{}
48 // Used as a RemoteHostInfoCallback for testing.
49 void OnRemoteHostInfoRetrieved(
50 base::Closure done_closure
,
51 const RemoteHostInfo
& retrieved_remote_host_info
);
54 // testing::Test interface.
55 void SetUp() override
;
57 // Sets the HTTP status and data returned for a specified URL.
58 void SetFakeResponse(const GURL
& url
,
59 const std::string
& data
,
60 net::HttpStatusCode code
,
61 net::URLRequestStatus::Status status
);
63 // Used for result verification.
64 RemoteHostInfo remote_host_info_
;
66 std::string dev_service_environment_url_
;
67 std::string test_service_environment_url_
;
68 std::string staging_service_environment_url_
;
71 net::FakeURLFetcherFactory url_fetcher_factory_
;
72 scoped_ptr
<base::MessageLoopForIO
> message_loop_
;
74 DISALLOW_COPY_AND_ASSIGN(RemoteHostInfoFetcherTest
);
77 void RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved(
78 base::Closure done_closure
,
79 const RemoteHostInfo
& retrieved_remote_host_info
) {
80 remote_host_info_
= retrieved_remote_host_info
;
85 void RemoteHostInfoFetcherTest::SetUp() {
86 DCHECK(!message_loop_
);
87 message_loop_
.reset(new base::MessageLoopForIO
);
89 dev_service_environment_url_
=
90 GetRunApplicationUrl(kTestApplicationId
, kDeveloperEnvironment
);
91 SetFakeResponse(GURL(dev_service_environment_url_
),
92 kRemoteHostInfoEmptyResponse
, net::HTTP_NOT_FOUND
,
93 net::URLRequestStatus::FAILED
);
95 test_service_environment_url_
=
96 GetRunApplicationUrl(kTestApplicationId
, kTestingEnvironment
);
97 SetFakeResponse(GURL(test_service_environment_url_
),
98 kRemoteHostInfoEmptyResponse
, net::HTTP_NOT_FOUND
,
99 net::URLRequestStatus::FAILED
);
101 staging_service_environment_url_
=
102 GetRunApplicationUrl(kTestApplicationId
, kStagingEnvironment
);
103 SetFakeResponse(GURL(staging_service_environment_url_
),
104 kRemoteHostInfoEmptyResponse
, net::HTTP_NOT_FOUND
,
105 net::URLRequestStatus::FAILED
);
108 void RemoteHostInfoFetcherTest::SetFakeResponse(
110 const std::string
& data
,
111 net::HttpStatusCode code
,
112 net::URLRequestStatus::Status status
) {
113 url_fetcher_factory_
.SetFakeResponse(url
, data
, code
, status
);
116 TEST_F(RemoteHostInfoFetcherTest
, RetrieveRemoteHostInfoFromDev
) {
117 SetFakeResponse(GURL(dev_service_environment_url_
),
118 kRemoteHostInfoReadyResponse
, net::HTTP_OK
,
119 net::URLRequestStatus::SUCCESS
);
121 base::RunLoop run_loop
;
122 RemoteHostInfoCallback remote_host_info_callback
=
123 base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved
,
124 base::Unretained(this), run_loop
.QuitClosure());
126 RemoteHostInfoFetcher remote_host_info_fetcher
;
127 bool request_started
= remote_host_info_fetcher
.RetrieveRemoteHostInfo(
128 kTestApplicationId
, kAccessTokenValue
, kDeveloperEnvironment
,
129 remote_host_info_callback
);
133 EXPECT_TRUE(request_started
);
134 EXPECT_TRUE(remote_host_info_
.IsReadyForConnection());
135 EXPECT_EQ(remote_host_info_
.application_id
.compare(kTestApplicationId
), 0);
136 EXPECT_TRUE(!remote_host_info_
.host_id
.empty());
137 EXPECT_TRUE(!remote_host_info_
.host_jid
.empty());
138 EXPECT_TRUE(!remote_host_info_
.authorization_code
.empty());
139 EXPECT_TRUE(!remote_host_info_
.shared_secret
.empty());
142 TEST_F(RemoteHostInfoFetcherTest
, RetrieveRemoteHostInfoFromTest
) {
143 SetFakeResponse(GURL(test_service_environment_url_
),
144 kRemoteHostInfoReadyResponse
, net::HTTP_OK
,
145 net::URLRequestStatus::SUCCESS
);
147 base::RunLoop run_loop
;
148 RemoteHostInfoCallback remote_host_info_callback
=
149 base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved
,
150 base::Unretained(this), run_loop
.QuitClosure());
152 RemoteHostInfoFetcher remote_host_info_fetcher
;
153 bool request_started
= remote_host_info_fetcher
.RetrieveRemoteHostInfo(
154 kTestApplicationId
, kAccessTokenValue
, kTestingEnvironment
,
155 remote_host_info_callback
);
159 EXPECT_TRUE(request_started
);
160 EXPECT_TRUE(remote_host_info_
.IsReadyForConnection());
161 EXPECT_EQ(remote_host_info_
.application_id
.compare(kTestApplicationId
), 0);
162 EXPECT_TRUE(!remote_host_info_
.host_id
.empty());
163 EXPECT_TRUE(!remote_host_info_
.host_jid
.empty());
164 EXPECT_TRUE(!remote_host_info_
.authorization_code
.empty());
165 EXPECT_TRUE(!remote_host_info_
.shared_secret
.empty());
168 TEST_F(RemoteHostInfoFetcherTest
, RetrieveRemoteHostInfoFromStaging
) {
169 SetFakeResponse(GURL(staging_service_environment_url_
),
170 kRemoteHostInfoReadyResponse
, net::HTTP_OK
,
171 net::URLRequestStatus::SUCCESS
);
173 base::RunLoop run_loop
;
174 RemoteHostInfoCallback remote_host_info_callback
=
175 base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved
,
176 base::Unretained(this), run_loop
.QuitClosure());
178 RemoteHostInfoFetcher remote_host_info_fetcher
;
179 bool request_started
= remote_host_info_fetcher
.RetrieveRemoteHostInfo(
180 kTestApplicationId
, kAccessTokenValue
, kStagingEnvironment
,
181 remote_host_info_callback
);
185 EXPECT_TRUE(request_started
);
186 EXPECT_TRUE(remote_host_info_
.IsReadyForConnection());
187 EXPECT_EQ(remote_host_info_
.application_id
.compare(kTestApplicationId
), 0);
188 EXPECT_TRUE(!remote_host_info_
.host_id
.empty());
189 EXPECT_TRUE(!remote_host_info_
.host_jid
.empty());
190 EXPECT_TRUE(!remote_host_info_
.authorization_code
.empty());
191 EXPECT_TRUE(!remote_host_info_
.shared_secret
.empty());
194 TEST_F(RemoteHostInfoFetcherTest
, RetrieveRemoteHostInfoInvalidEnvironment
) {
195 base::RunLoop run_loop
;
196 RemoteHostInfoCallback remote_host_info_callback
=
197 base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved
,
198 base::Unretained(this), run_loop
.QuitClosure());
200 RemoteHostInfoFetcher remote_host_info_fetcher
;
201 bool request_started
= remote_host_info_fetcher
.RetrieveRemoteHostInfo(
202 kTestApplicationId
, kAccessTokenValue
, kUnknownEnvironment
,
203 remote_host_info_callback
);
205 EXPECT_FALSE(request_started
);
208 TEST_F(RemoteHostInfoFetcherTest
, RetrieveRemoteHostInfoNetworkError
) {
209 base::RunLoop run_loop
;
210 RemoteHostInfoCallback remote_host_info_callback
=
211 base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved
,
212 base::Unretained(this), run_loop
.QuitClosure());
214 RemoteHostInfoFetcher remote_host_info_fetcher
;
215 bool request_started
= remote_host_info_fetcher
.RetrieveRemoteHostInfo(
216 kTestApplicationId
, kAccessTokenValue
, kDeveloperEnvironment
,
217 remote_host_info_callback
);
221 EXPECT_TRUE(request_started
);
222 EXPECT_FALSE(remote_host_info_
.IsReadyForConnection());
224 // If there was a network error retrieving the remote host info, then none of
225 // the connection details should be populated.
226 EXPECT_TRUE(remote_host_info_
.application_id
.empty());
227 EXPECT_TRUE(remote_host_info_
.host_id
.empty());
228 EXPECT_TRUE(remote_host_info_
.host_jid
.empty());
229 EXPECT_TRUE(remote_host_info_
.authorization_code
.empty());
230 EXPECT_TRUE(remote_host_info_
.shared_secret
.empty());
233 TEST_F(RemoteHostInfoFetcherTest
, RetrieveRemoteHostInfoPendingResponse
) {
234 SetFakeResponse(GURL(dev_service_environment_url_
),
235 kRemoteHostInfoPendingResponse
, net::HTTP_OK
,
236 net::URLRequestStatus::SUCCESS
);
238 base::RunLoop run_loop
;
239 RemoteHostInfoCallback remote_host_info_callback
=
240 base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved
,
241 base::Unretained(this), run_loop
.QuitClosure());
243 RemoteHostInfoFetcher remote_host_info_fetcher
;
244 bool request_started
= remote_host_info_fetcher
.RetrieveRemoteHostInfo(
245 kTestApplicationId
, kAccessTokenValue
, kDeveloperEnvironment
,
246 remote_host_info_callback
);
250 EXPECT_TRUE(request_started
);
251 EXPECT_FALSE(remote_host_info_
.IsReadyForConnection());
253 // If the remote host request is pending, then none of the connection details
254 // should be populated.
255 EXPECT_TRUE(remote_host_info_
.application_id
.empty());
256 EXPECT_TRUE(remote_host_info_
.host_id
.empty());
257 EXPECT_TRUE(remote_host_info_
.host_jid
.empty());
258 EXPECT_TRUE(remote_host_info_
.authorization_code
.empty());
259 EXPECT_TRUE(remote_host_info_
.shared_secret
.empty());
262 TEST_F(RemoteHostInfoFetcherTest
, RetrieveRemoteHostInfoEmptyResponse
) {
263 SetFakeResponse(GURL(dev_service_environment_url_
),
264 kRemoteHostInfoEmptyResponse
, net::HTTP_OK
,
265 net::URLRequestStatus::SUCCESS
);
267 base::RunLoop run_loop
;
268 RemoteHostInfoCallback remote_host_info_callback
=
269 base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved
,
270 base::Unretained(this), run_loop
.QuitClosure());
272 RemoteHostInfoFetcher remote_host_info_fetcher
;
273 bool request_started
= remote_host_info_fetcher
.RetrieveRemoteHostInfo(
274 kTestApplicationId
, kAccessTokenValue
, kDeveloperEnvironment
,
275 remote_host_info_callback
);
279 EXPECT_TRUE(request_started
);
280 EXPECT_FALSE(remote_host_info_
.IsReadyForConnection());
282 // If we received an empty response, then none of the connection details
283 // should be populated.
284 EXPECT_TRUE(remote_host_info_
.application_id
.empty());
285 EXPECT_TRUE(remote_host_info_
.host_id
.empty());
286 EXPECT_TRUE(remote_host_info_
.host_jid
.empty());
287 EXPECT_TRUE(remote_host_info_
.authorization_code
.empty());
288 EXPECT_TRUE(remote_host_info_
.shared_secret
.empty());
291 TEST_F(RemoteHostInfoFetcherTest
, MultipleRetrieveRemoteHostInfoRequests
) {
292 // First, we will fetch info from the development service environment.
293 SetFakeResponse(GURL(dev_service_environment_url_
),
294 kRemoteHostInfoReadyResponse
, net::HTTP_OK
,
295 net::URLRequestStatus::SUCCESS
);
297 base::RunLoop dev_run_loop
;
298 RemoteHostInfoCallback dev_remote_host_info_callback
=
299 base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved
,
300 base::Unretained(this), dev_run_loop
.QuitClosure());
302 RemoteHostInfoFetcher remote_host_info_fetcher
;
303 bool dev_request_started
= remote_host_info_fetcher
.RetrieveRemoteHostInfo(
304 kTestApplicationId
, kAccessTokenValue
, kDeveloperEnvironment
,
305 dev_remote_host_info_callback
);
309 EXPECT_TRUE(dev_request_started
);
310 EXPECT_TRUE(remote_host_info_
.IsReadyForConnection());
311 EXPECT_EQ(remote_host_info_
.application_id
.compare(kTestApplicationId
), 0);
312 EXPECT_TRUE(!remote_host_info_
.host_id
.empty());
313 EXPECT_TRUE(!remote_host_info_
.host_jid
.empty());
314 EXPECT_TRUE(!remote_host_info_
.authorization_code
.empty());
315 EXPECT_TRUE(!remote_host_info_
.shared_secret
.empty());
317 // Next, we will fetch info from the test service environment.
318 SetFakeResponse(GURL(test_service_environment_url_
),
319 kRemoteHostInfoReadyResponse
, net::HTTP_OK
,
320 net::URLRequestStatus::SUCCESS
);
322 base::RunLoop test_run_loop
;
323 RemoteHostInfoCallback test_remote_host_info_callback
=
324 base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved
,
325 base::Unretained(this), test_run_loop
.QuitClosure());
327 // Reset the state of our internal |remote_host_info_| object.
328 remote_host_info_
= RemoteHostInfo();
329 EXPECT_FALSE(remote_host_info_
.IsReadyForConnection());
331 bool test_request_started
= remote_host_info_fetcher
.RetrieveRemoteHostInfo(
332 kTestApplicationId
, kAccessTokenValue
, kTestingEnvironment
,
333 test_remote_host_info_callback
);
337 EXPECT_TRUE(test_request_started
);
338 EXPECT_TRUE(remote_host_info_
.IsReadyForConnection());
339 EXPECT_EQ(remote_host_info_
.application_id
.compare(kTestApplicationId
), 0);
340 EXPECT_TRUE(!remote_host_info_
.host_id
.empty());
341 EXPECT_TRUE(!remote_host_info_
.host_jid
.empty());
342 EXPECT_TRUE(!remote_host_info_
.authorization_code
.empty());
343 EXPECT_TRUE(!remote_host_info_
.shared_secret
.empty());
347 } // namespace remoting