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_
;
68 std::string dev_service_environment_url_
;
69 std::string test_service_environment_url_
;
72 net::FakeURLFetcherFactory url_fetcher_factory_
;
73 scoped_ptr
<base::MessageLoopForIO
> message_loop_
;
75 DISALLOW_COPY_AND_ASSIGN(RemoteHostInfoFetcherTest
);
78 void RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved(
79 base::Closure done_closure
,
80 const RemoteHostInfo
& retrieved_remote_host_info
) {
81 remote_host_info_
= retrieved_remote_host_info
;
86 void RemoteHostInfoFetcherTest::SetUp() {
87 if (!base::MessageLoop::current()) {
88 // Create a temporary message loop if the current thread does not already
89 // have one so we can use its task runner to create a request object.
90 message_loop_
.reset(new base::MessageLoopForIO
);
93 dev_service_environment_url_
=
94 base::StringPrintf(kDevServiceEnvironmentUrlFormat
, kTestApplicationId
);
96 test_service_environment_url_
=
97 base::StringPrintf(kTestServiceEnvironmentUrlFormat
, kTestApplicationId
);
100 void RemoteHostInfoFetcherTest::SetFakeResponse(
102 const std::string
& data
,
103 net::HttpStatusCode code
,
104 net::URLRequestStatus::Status status
) {
105 url_fetcher_factory_
.SetFakeResponse(url
, data
, code
, status
);
108 TEST_F(RemoteHostInfoFetcherTest
, RetrieveRemoteHostInfoFromDev
) {
109 SetFakeResponse(GURL(dev_service_environment_url_
),
110 kRemoteHostInfoReadyResponse
, net::HTTP_OK
,
111 net::URLRequestStatus::SUCCESS
);
113 SetFakeResponse(GURL(test_service_environment_url_
),
114 kRemoteHostInfoEmptyResponse
, net::HTTP_NOT_FOUND
,
115 net::URLRequestStatus::FAILED
);
117 base::RunLoop run_loop
;
118 RemoteHostInfoCallback remote_host_info_callback
=
119 base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved
,
120 base::Unretained(this), run_loop
.QuitClosure());
122 RemoteHostInfoFetcher remote_host_info_fetcher
;
123 bool request_started
= remote_host_info_fetcher
.RetrieveRemoteHostInfo(
124 kTestApplicationId
, kAccessTokenValue
, kDeveloperEnvironment
,
125 remote_host_info_callback
);
129 EXPECT_TRUE(request_started
);
130 EXPECT_TRUE(remote_host_info_
.IsReadyForConnection());
131 EXPECT_EQ(remote_host_info_
.application_id
.compare(kTestApplicationId
), 0);
132 EXPECT_TRUE(!remote_host_info_
.host_id
.empty());
133 EXPECT_TRUE(!remote_host_info_
.host_jid
.empty());
134 EXPECT_TRUE(!remote_host_info_
.authorization_code
.empty());
135 EXPECT_TRUE(!remote_host_info_
.shared_secret
.empty());
138 TEST_F(RemoteHostInfoFetcherTest
, RetrieveRemoteHostInfoFromTest
) {
139 SetFakeResponse(GURL(test_service_environment_url_
),
140 kRemoteHostInfoReadyResponse
, net::HTTP_OK
,
141 net::URLRequestStatus::SUCCESS
);
143 SetFakeResponse(GURL(dev_service_environment_url_
),
144 kRemoteHostInfoEmptyResponse
, net::HTTP_NOT_FOUND
,
145 net::URLRequestStatus::FAILED
);
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
, RetrieveRemoteHostInfoInvalidEnvironment
) {
169 base::RunLoop run_loop
;
170 RemoteHostInfoCallback remote_host_info_callback
=
171 base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved
,
172 base::Unretained(this), run_loop
.QuitClosure());
174 RemoteHostInfoFetcher remote_host_info_fetcher
;
175 bool request_started
= remote_host_info_fetcher
.RetrieveRemoteHostInfo(
176 kTestApplicationId
, kAccessTokenValue
, kUnknownEnvironment
,
177 remote_host_info_callback
);
179 EXPECT_FALSE(request_started
);
182 TEST_F(RemoteHostInfoFetcherTest
, RetrieveRemoteHostInfoNetworkError
) {
183 SetFakeResponse(GURL(dev_service_environment_url_
),
184 kRemoteHostInfoReadyResponse
, net::HTTP_NOT_FOUND
,
185 net::URLRequestStatus::FAILED
);
187 base::RunLoop run_loop
;
188 RemoteHostInfoCallback remote_host_info_callback
=
189 base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved
,
190 base::Unretained(this), run_loop
.QuitClosure());
192 RemoteHostInfoFetcher remote_host_info_fetcher
;
193 bool request_started
= remote_host_info_fetcher
.RetrieveRemoteHostInfo(
194 kTestApplicationId
, kAccessTokenValue
, kDeveloperEnvironment
,
195 remote_host_info_callback
);
199 EXPECT_TRUE(request_started
);
200 EXPECT_FALSE(remote_host_info_
.IsReadyForConnection());
202 // If there was a network error retrieving the remote host info, then none of
203 // the connection details should be populated.
204 EXPECT_TRUE(remote_host_info_
.application_id
.empty());
205 EXPECT_TRUE(remote_host_info_
.host_id
.empty());
206 EXPECT_TRUE(remote_host_info_
.host_jid
.empty());
207 EXPECT_TRUE(remote_host_info_
.authorization_code
.empty());
208 EXPECT_TRUE(remote_host_info_
.shared_secret
.empty());
211 TEST_F(RemoteHostInfoFetcherTest
, RetrieveRemoteHostInfoPendingResponse
) {
212 SetFakeResponse(GURL(dev_service_environment_url_
),
213 kRemoteHostInfoPendingResponse
, net::HTTP_OK
,
214 net::URLRequestStatus::SUCCESS
);
216 base::RunLoop run_loop
;
217 RemoteHostInfoCallback remote_host_info_callback
=
218 base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved
,
219 base::Unretained(this), run_loop
.QuitClosure());
221 RemoteHostInfoFetcher remote_host_info_fetcher
;
222 bool request_started
= remote_host_info_fetcher
.RetrieveRemoteHostInfo(
223 kTestApplicationId
, kAccessTokenValue
, kDeveloperEnvironment
,
224 remote_host_info_callback
);
228 EXPECT_TRUE(request_started
);
229 EXPECT_FALSE(remote_host_info_
.IsReadyForConnection());
231 // If the remote host request is pending, then none of the connection details
232 // should be populated.
233 EXPECT_TRUE(remote_host_info_
.application_id
.empty());
234 EXPECT_TRUE(remote_host_info_
.host_id
.empty());
235 EXPECT_TRUE(remote_host_info_
.host_jid
.empty());
236 EXPECT_TRUE(remote_host_info_
.authorization_code
.empty());
237 EXPECT_TRUE(remote_host_info_
.shared_secret
.empty());
240 TEST_F(RemoteHostInfoFetcherTest
, RetrieveRemoteHostInfoEmptyResponse
) {
241 SetFakeResponse(GURL(dev_service_environment_url_
),
242 kRemoteHostInfoEmptyResponse
, net::HTTP_OK
,
243 net::URLRequestStatus::SUCCESS
);
245 base::RunLoop run_loop
;
246 RemoteHostInfoCallback remote_host_info_callback
=
247 base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved
,
248 base::Unretained(this), run_loop
.QuitClosure());
250 RemoteHostInfoFetcher remote_host_info_fetcher
;
251 bool request_started
= remote_host_info_fetcher
.RetrieveRemoteHostInfo(
252 kTestApplicationId
, kAccessTokenValue
, kDeveloperEnvironment
,
253 remote_host_info_callback
);
257 EXPECT_TRUE(request_started
);
258 EXPECT_FALSE(remote_host_info_
.IsReadyForConnection());
260 // If we received an empty response, then none of the connection details
261 // should be populated.
262 EXPECT_TRUE(remote_host_info_
.application_id
.empty());
263 EXPECT_TRUE(remote_host_info_
.host_id
.empty());
264 EXPECT_TRUE(remote_host_info_
.host_jid
.empty());
265 EXPECT_TRUE(remote_host_info_
.authorization_code
.empty());
266 EXPECT_TRUE(remote_host_info_
.shared_secret
.empty());
269 TEST_F(RemoteHostInfoFetcherTest
, MultipleRetrieveRemoteHostInfoRequests
) {
270 // First, we will fetch info from the development service environment.
271 SetFakeResponse(GURL(dev_service_environment_url_
),
272 kRemoteHostInfoReadyResponse
, net::HTTP_OK
,
273 net::URLRequestStatus::SUCCESS
);
275 SetFakeResponse(GURL(test_service_environment_url_
),
276 kRemoteHostInfoEmptyResponse
, net::HTTP_NOT_FOUND
,
277 net::URLRequestStatus::FAILED
);
279 base::RunLoop dev_run_loop
;
280 RemoteHostInfoCallback dev_remote_host_info_callback
=
281 base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved
,
282 base::Unretained(this), dev_run_loop
.QuitClosure());
284 RemoteHostInfoFetcher remote_host_info_fetcher
;
285 bool dev_request_started
= remote_host_info_fetcher
.RetrieveRemoteHostInfo(
286 kTestApplicationId
, kAccessTokenValue
, kDeveloperEnvironment
,
287 dev_remote_host_info_callback
);
291 EXPECT_TRUE(dev_request_started
);
292 EXPECT_TRUE(remote_host_info_
.IsReadyForConnection());
293 EXPECT_EQ(remote_host_info_
.application_id
.compare(kTestApplicationId
), 0);
294 EXPECT_TRUE(!remote_host_info_
.host_id
.empty());
295 EXPECT_TRUE(!remote_host_info_
.host_jid
.empty());
296 EXPECT_TRUE(!remote_host_info_
.authorization_code
.empty());
297 EXPECT_TRUE(!remote_host_info_
.shared_secret
.empty());
299 // Next, we will fetch info from the test service environment.
300 SetFakeResponse(GURL(test_service_environment_url_
),
301 kRemoteHostInfoReadyResponse
, net::HTTP_OK
,
302 net::URLRequestStatus::SUCCESS
);
304 SetFakeResponse(GURL(dev_service_environment_url_
),
305 kRemoteHostInfoEmptyResponse
, net::HTTP_NOT_FOUND
,
306 net::URLRequestStatus::FAILED
);
308 base::RunLoop test_run_loop
;
309 RemoteHostInfoCallback test_remote_host_info_callback
=
310 base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved
,
311 base::Unretained(this), test_run_loop
.QuitClosure());
313 // Reset the state of our internal |remote_host_info_| object.
314 remote_host_info_
= RemoteHostInfo();
315 EXPECT_FALSE(remote_host_info_
.IsReadyForConnection());
317 bool test_request_started
= remote_host_info_fetcher
.RetrieveRemoteHostInfo(
318 kTestApplicationId
, kAccessTokenValue
, kTestingEnvironment
,
319 test_remote_host_info_callback
);
323 EXPECT_TRUE(test_request_started
);
324 EXPECT_TRUE(remote_host_info_
.IsReadyForConnection());
325 EXPECT_EQ(remote_host_info_
.application_id
.compare(kTestApplicationId
), 0);
326 EXPECT_TRUE(!remote_host_info_
.host_id
.empty());
327 EXPECT_TRUE(!remote_host_info_
.host_jid
.empty());
328 EXPECT_TRUE(!remote_host_info_
.authorization_code
.empty());
329 EXPECT_TRUE(!remote_host_info_
.shared_secret
.empty());
333 } // namespace remoting