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 "base/strings/stringprintf.h"
11 #include "net/url_request/test_url_fetcher_factory.h"
12 #include "testing/gtest/include/gtest/gtest.h"
15 const char kTestApplicationId
[] = "klasdfjlkasdfjklasjfdkljsadf";
16 const char kAccessTokenValue
[] = "test_access_token_value";
17 const char kRemoteHostInfoReadyResponse
[] =
19 " \"status\": \"done\","
21 " \"kind\": \"test_kind\","
22 " \"applicationId\": \"klasdfjlkasdfjklasjfdkljsadf\","
23 " \"hostId\": \"test_host_id\""
25 " \"hostJid\": \"test_host_jid\","
26 " \"authorizationCode\": \"test_authorization_code\","
27 " \"sharedSecret\": \"test_shared_secret\""
29 const char kRemoteHostInfoPendingResponse
[] =
31 " \"status\": \"pending\""
33 const char kRemoteHostInfoEmptyResponse
[] = "{}";
39 // Provides base functionality for the RemoteHostInfoFetcher Tests below. The
40 // FakeURLFetcherFactory allows us to override the response data and payload for
41 // specified URLs. We use this to stub out network calls made by the
42 // RemoteHostInfoFetcher. This fixture also creates an IO MessageLoop, if
43 // necessary, for use by the RemoteHostInfoFetcher.
44 class RemoteHostInfoFetcherTest
: public ::testing::Test
{
46 RemoteHostInfoFetcherTest() : url_fetcher_factory_(nullptr) {}
47 ~RemoteHostInfoFetcherTest() override
{}
49 // Used as a RemoteHostInfoCallback for testing.
50 void OnRemoteHostInfoRetrieved(
51 base::Closure done_closure
,
52 const RemoteHostInfo
& retrieved_remote_host_info
);
55 // testing::Test interface.
56 void SetUp() override
;
58 // Sets the HTTP status and data returned for a specified URL.
59 void SetFakeResponse(const GURL
& url
,
60 const std::string
& data
,
61 net::HttpStatusCode code
,
62 net::URLRequestStatus::Status status
);
64 // Used for result verification.
65 RemoteHostInfo remote_host_info_
;
67 std::string dev_service_environment_url_
;
68 std::string test_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 base::StringPrintf(kDevServiceEnvironmentUrlFormat
, kTestApplicationId
);
92 test_service_environment_url_
=
93 base::StringPrintf(kTestServiceEnvironmentUrlFormat
, kTestApplicationId
);
96 void RemoteHostInfoFetcherTest::SetFakeResponse(
98 const std::string
& data
,
99 net::HttpStatusCode code
,
100 net::URLRequestStatus::Status status
) {
101 url_fetcher_factory_
.SetFakeResponse(url
, data
, code
, status
);
104 TEST_F(RemoteHostInfoFetcherTest
, RetrieveRemoteHostInfoFromDev
) {
105 SetFakeResponse(GURL(dev_service_environment_url_
),
106 kRemoteHostInfoReadyResponse
, net::HTTP_OK
,
107 net::URLRequestStatus::SUCCESS
);
109 SetFakeResponse(GURL(test_service_environment_url_
),
110 kRemoteHostInfoEmptyResponse
, net::HTTP_NOT_FOUND
,
111 net::URLRequestStatus::FAILED
);
113 base::RunLoop run_loop
;
114 RemoteHostInfoCallback remote_host_info_callback
=
115 base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved
,
116 base::Unretained(this), run_loop
.QuitClosure());
118 RemoteHostInfoFetcher remote_host_info_fetcher
;
119 bool request_started
= remote_host_info_fetcher
.RetrieveRemoteHostInfo(
120 kTestApplicationId
, kAccessTokenValue
, kDeveloperEnvironment
,
121 remote_host_info_callback
);
125 EXPECT_TRUE(request_started
);
126 EXPECT_TRUE(remote_host_info_
.IsReadyForConnection());
127 EXPECT_EQ(remote_host_info_
.application_id
.compare(kTestApplicationId
), 0);
128 EXPECT_TRUE(!remote_host_info_
.host_id
.empty());
129 EXPECT_TRUE(!remote_host_info_
.host_jid
.empty());
130 EXPECT_TRUE(!remote_host_info_
.authorization_code
.empty());
131 EXPECT_TRUE(!remote_host_info_
.shared_secret
.empty());
134 TEST_F(RemoteHostInfoFetcherTest
, RetrieveRemoteHostInfoFromTest
) {
135 SetFakeResponse(GURL(test_service_environment_url_
),
136 kRemoteHostInfoReadyResponse
, net::HTTP_OK
,
137 net::URLRequestStatus::SUCCESS
);
139 SetFakeResponse(GURL(dev_service_environment_url_
),
140 kRemoteHostInfoEmptyResponse
, net::HTTP_NOT_FOUND
,
141 net::URLRequestStatus::FAILED
);
143 base::RunLoop run_loop
;
144 RemoteHostInfoCallback remote_host_info_callback
=
145 base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved
,
146 base::Unretained(this), run_loop
.QuitClosure());
148 RemoteHostInfoFetcher remote_host_info_fetcher
;
149 bool request_started
= remote_host_info_fetcher
.RetrieveRemoteHostInfo(
150 kTestApplicationId
, kAccessTokenValue
, kTestingEnvironment
,
151 remote_host_info_callback
);
155 EXPECT_TRUE(request_started
);
156 EXPECT_TRUE(remote_host_info_
.IsReadyForConnection());
157 EXPECT_EQ(remote_host_info_
.application_id
.compare(kTestApplicationId
), 0);
158 EXPECT_TRUE(!remote_host_info_
.host_id
.empty());
159 EXPECT_TRUE(!remote_host_info_
.host_jid
.empty());
160 EXPECT_TRUE(!remote_host_info_
.authorization_code
.empty());
161 EXPECT_TRUE(!remote_host_info_
.shared_secret
.empty());
164 TEST_F(RemoteHostInfoFetcherTest
, RetrieveRemoteHostInfoInvalidEnvironment
) {
165 base::RunLoop run_loop
;
166 RemoteHostInfoCallback remote_host_info_callback
=
167 base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved
,
168 base::Unretained(this), run_loop
.QuitClosure());
170 RemoteHostInfoFetcher remote_host_info_fetcher
;
171 bool request_started
= remote_host_info_fetcher
.RetrieveRemoteHostInfo(
172 kTestApplicationId
, kAccessTokenValue
, kUnknownEnvironment
,
173 remote_host_info_callback
);
175 EXPECT_FALSE(request_started
);
178 TEST_F(RemoteHostInfoFetcherTest
, RetrieveRemoteHostInfoNetworkError
) {
179 SetFakeResponse(GURL(dev_service_environment_url_
),
180 kRemoteHostInfoReadyResponse
, net::HTTP_NOT_FOUND
,
181 net::URLRequestStatus::FAILED
);
183 base::RunLoop run_loop
;
184 RemoteHostInfoCallback remote_host_info_callback
=
185 base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved
,
186 base::Unretained(this), run_loop
.QuitClosure());
188 RemoteHostInfoFetcher remote_host_info_fetcher
;
189 bool request_started
= remote_host_info_fetcher
.RetrieveRemoteHostInfo(
190 kTestApplicationId
, kAccessTokenValue
, kDeveloperEnvironment
,
191 remote_host_info_callback
);
195 EXPECT_TRUE(request_started
);
196 EXPECT_FALSE(remote_host_info_
.IsReadyForConnection());
198 // If there was a network error retrieving the remote host info, then none of
199 // the connection details should be populated.
200 EXPECT_TRUE(remote_host_info_
.application_id
.empty());
201 EXPECT_TRUE(remote_host_info_
.host_id
.empty());
202 EXPECT_TRUE(remote_host_info_
.host_jid
.empty());
203 EXPECT_TRUE(remote_host_info_
.authorization_code
.empty());
204 EXPECT_TRUE(remote_host_info_
.shared_secret
.empty());
207 TEST_F(RemoteHostInfoFetcherTest
, RetrieveRemoteHostInfoPendingResponse
) {
208 SetFakeResponse(GURL(dev_service_environment_url_
),
209 kRemoteHostInfoPendingResponse
, net::HTTP_OK
,
210 net::URLRequestStatus::SUCCESS
);
212 base::RunLoop run_loop
;
213 RemoteHostInfoCallback remote_host_info_callback
=
214 base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved
,
215 base::Unretained(this), run_loop
.QuitClosure());
217 RemoteHostInfoFetcher remote_host_info_fetcher
;
218 bool request_started
= remote_host_info_fetcher
.RetrieveRemoteHostInfo(
219 kTestApplicationId
, kAccessTokenValue
, kDeveloperEnvironment
,
220 remote_host_info_callback
);
224 EXPECT_TRUE(request_started
);
225 EXPECT_FALSE(remote_host_info_
.IsReadyForConnection());
227 // If the remote host request is pending, then none of the connection details
228 // should be populated.
229 EXPECT_TRUE(remote_host_info_
.application_id
.empty());
230 EXPECT_TRUE(remote_host_info_
.host_id
.empty());
231 EXPECT_TRUE(remote_host_info_
.host_jid
.empty());
232 EXPECT_TRUE(remote_host_info_
.authorization_code
.empty());
233 EXPECT_TRUE(remote_host_info_
.shared_secret
.empty());
236 TEST_F(RemoteHostInfoFetcherTest
, RetrieveRemoteHostInfoEmptyResponse
) {
237 SetFakeResponse(GURL(dev_service_environment_url_
),
238 kRemoteHostInfoEmptyResponse
, net::HTTP_OK
,
239 net::URLRequestStatus::SUCCESS
);
241 base::RunLoop run_loop
;
242 RemoteHostInfoCallback remote_host_info_callback
=
243 base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved
,
244 base::Unretained(this), run_loop
.QuitClosure());
246 RemoteHostInfoFetcher remote_host_info_fetcher
;
247 bool request_started
= remote_host_info_fetcher
.RetrieveRemoteHostInfo(
248 kTestApplicationId
, kAccessTokenValue
, kDeveloperEnvironment
,
249 remote_host_info_callback
);
253 EXPECT_TRUE(request_started
);
254 EXPECT_FALSE(remote_host_info_
.IsReadyForConnection());
256 // If we received an empty response, then none of the connection details
257 // should be populated.
258 EXPECT_TRUE(remote_host_info_
.application_id
.empty());
259 EXPECT_TRUE(remote_host_info_
.host_id
.empty());
260 EXPECT_TRUE(remote_host_info_
.host_jid
.empty());
261 EXPECT_TRUE(remote_host_info_
.authorization_code
.empty());
262 EXPECT_TRUE(remote_host_info_
.shared_secret
.empty());
265 TEST_F(RemoteHostInfoFetcherTest
, MultipleRetrieveRemoteHostInfoRequests
) {
266 // First, we will fetch info from the development service environment.
267 SetFakeResponse(GURL(dev_service_environment_url_
),
268 kRemoteHostInfoReadyResponse
, net::HTTP_OK
,
269 net::URLRequestStatus::SUCCESS
);
271 SetFakeResponse(GURL(test_service_environment_url_
),
272 kRemoteHostInfoEmptyResponse
, net::HTTP_NOT_FOUND
,
273 net::URLRequestStatus::FAILED
);
275 base::RunLoop dev_run_loop
;
276 RemoteHostInfoCallback dev_remote_host_info_callback
=
277 base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved
,
278 base::Unretained(this), dev_run_loop
.QuitClosure());
280 RemoteHostInfoFetcher remote_host_info_fetcher
;
281 bool dev_request_started
= remote_host_info_fetcher
.RetrieveRemoteHostInfo(
282 kTestApplicationId
, kAccessTokenValue
, kDeveloperEnvironment
,
283 dev_remote_host_info_callback
);
287 EXPECT_TRUE(dev_request_started
);
288 EXPECT_TRUE(remote_host_info_
.IsReadyForConnection());
289 EXPECT_EQ(remote_host_info_
.application_id
.compare(kTestApplicationId
), 0);
290 EXPECT_TRUE(!remote_host_info_
.host_id
.empty());
291 EXPECT_TRUE(!remote_host_info_
.host_jid
.empty());
292 EXPECT_TRUE(!remote_host_info_
.authorization_code
.empty());
293 EXPECT_TRUE(!remote_host_info_
.shared_secret
.empty());
295 // Next, we will fetch info from the test service environment.
296 SetFakeResponse(GURL(test_service_environment_url_
),
297 kRemoteHostInfoReadyResponse
, net::HTTP_OK
,
298 net::URLRequestStatus::SUCCESS
);
300 SetFakeResponse(GURL(dev_service_environment_url_
),
301 kRemoteHostInfoEmptyResponse
, net::HTTP_NOT_FOUND
,
302 net::URLRequestStatus::FAILED
);
304 base::RunLoop test_run_loop
;
305 RemoteHostInfoCallback test_remote_host_info_callback
=
306 base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved
,
307 base::Unretained(this), test_run_loop
.QuitClosure());
309 // Reset the state of our internal |remote_host_info_| object.
310 remote_host_info_
= RemoteHostInfo();
311 EXPECT_FALSE(remote_host_info_
.IsReadyForConnection());
313 bool test_request_started
= remote_host_info_fetcher
.RetrieveRemoteHostInfo(
314 kTestApplicationId
, kAccessTokenValue
, kTestingEnvironment
,
315 test_remote_host_info_callback
);
319 EXPECT_TRUE(test_request_started
);
320 EXPECT_TRUE(remote_host_info_
.IsReadyForConnection());
321 EXPECT_EQ(remote_host_info_
.application_id
.compare(kTestApplicationId
), 0);
322 EXPECT_TRUE(!remote_host_info_
.host_id
.empty());
323 EXPECT_TRUE(!remote_host_info_
.host_jid
.empty());
324 EXPECT_TRUE(!remote_host_info_
.authorization_code
.empty());
325 EXPECT_TRUE(!remote_host_info_
.shared_secret
.empty());
329 } // namespace remoting