summaryrefslogtreecommitdiffstats
path: root/remoting/test/remote_host_info_fetcher_unittest.cc
blob: e088a6ba36a564f58941600675cd7e27d806c8d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/test/remote_host_info_fetcher.h"

#include "base/bind.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {
const char kTestApplicationId[] = "klasdfjlkasdfjklasjfdkljsadf";
const char kTestApplicationId2[] = "klasdfjlkasdfjklasjfdkljsadf2";
const char kAccessTokenValue[] = "test_access_token_value";
const char kRemoteHostInfoReadyResponse[] =
    "{"
    "  \"status\": \"done\","
    "  \"host\": {"
    "    \"kind\": \"test_kind\","
    "    \"applicationId\": \"klasdfjlkasdfjklasjfdkljsadf\","
    "    \"hostId\": \"test_host_id\""
    "  },"
    "  \"hostJid\": \"test_host_jid\","
    "  \"authorizationCode\": \"test_authorization_code\","
    "  \"sharedSecret\": \"test_shared_secret\""
    "}";
const char kRemoteHostInfoReadyResponse2[] =
    "{"
    "  \"status\": \"done\","
    "  \"host\": {"
    "    \"kind\": \"test_kind\","
    "    \"applicationId\": \"klasdfjlkasdfjklasjfdkljsadf2\","
    "    \"hostId\": \"test_host_id\""
    "  },"
    "  \"hostJid\": \"test_host_jid\","
    "  \"authorizationCode\": \"test_authorization_code\","
    "  \"sharedSecret\": \"test_shared_secret\""
    "}";
const char kRemoteHostInfoPendingResponse[] =
    "{"
    "  \"status\": \"pending\""
    "}";
const char kRemoteHostInfoEmptyResponse[] = "{}";
}  // namespace

namespace remoting {
namespace test {

// Provides base functionality for the RemoteHostInfoFetcher Tests below.  The
// FakeURLFetcherFactory allows us to override the response data and payload for
// specified URLs.  We use this to stub out network calls made by the
// RemoteHostInfoFetcher.  This fixture also creates an IO MessageLoop, if
// necessary, for use by the RemoteHostInfoFetcher.
class RemoteHostInfoFetcherTest : public ::testing::Test {
 public:
  RemoteHostInfoFetcherTest() : url_fetcher_factory_(nullptr) {}
  ~RemoteHostInfoFetcherTest() override {}

  // Used as a RemoteHostInfoCallback for testing.
  void OnRemoteHostInfoRetrieved(
      base::Closure done_closure,
      const RemoteHostInfo& retrieved_remote_host_info);

 protected:
  // testing::Test interface.
  void SetUp() override;

  // Sets the HTTP status and data returned for a specified URL.
  void SetFakeResponse(const GURL& url,
                       const std::string& data,
                       net::HttpStatusCode code,
                       net::URLRequestStatus::Status status);

  // Used for result verification.
  RemoteHostInfo remote_host_info_;

  std::string dev_service_environment_url_;
  std::string dev_service_environment_url_2_;

 private:
  net::FakeURLFetcherFactory url_fetcher_factory_;
  scoped_ptr<base::MessageLoopForIO> message_loop_;

  DISALLOW_COPY_AND_ASSIGN(RemoteHostInfoFetcherTest);
};

void RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved(
    base::Closure done_closure,
    const RemoteHostInfo& retrieved_remote_host_info) {
  remote_host_info_ = retrieved_remote_host_info;

  done_closure.Run();
}

void RemoteHostInfoFetcherTest::SetUp() {
  DCHECK(!message_loop_);
  message_loop_.reset(new base::MessageLoopForIO);

  dev_service_environment_url_ =
      GetRunApplicationUrl(kTestApplicationId, kDeveloperEnvironment);
  SetFakeResponse(GURL(dev_service_environment_url_),
                  kRemoteHostInfoEmptyResponse, net::HTTP_NOT_FOUND,
                  net::URLRequestStatus::FAILED);

  dev_service_environment_url_2_ =
      GetRunApplicationUrl(kTestApplicationId2, kDeveloperEnvironment);
  SetFakeResponse(GURL(dev_service_environment_url_2_),
                  kRemoteHostInfoEmptyResponse, net::HTTP_NOT_FOUND,
                  net::URLRequestStatus::FAILED);
}

void RemoteHostInfoFetcherTest::SetFakeResponse(
    const GURL& url,
    const std::string& data,
    net::HttpStatusCode code,
    net::URLRequestStatus::Status status) {
  url_fetcher_factory_.SetFakeResponse(url, data, code, status);
}

TEST_F(RemoteHostInfoFetcherTest, RetrieveRemoteHostInfoFromDev) {
  SetFakeResponse(GURL(dev_service_environment_url_),
                  kRemoteHostInfoReadyResponse, net::HTTP_OK,
                  net::URLRequestStatus::SUCCESS);

  base::RunLoop run_loop;
  RemoteHostInfoCallback remote_host_info_callback =
      base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved,
                 base::Unretained(this), run_loop.QuitClosure());

  RemoteHostInfoFetcher remote_host_info_fetcher;
  bool request_started = remote_host_info_fetcher.RetrieveRemoteHostInfo(
      kTestApplicationId, kAccessTokenValue, kDeveloperEnvironment,
      remote_host_info_callback);

  run_loop.Run();

  EXPECT_TRUE(request_started);
  EXPECT_TRUE(remote_host_info_.IsReadyForConnection());
  EXPECT_EQ(remote_host_info_.application_id.compare(kTestApplicationId), 0);
  EXPECT_TRUE(!remote_host_info_.host_id.empty());
  EXPECT_TRUE(!remote_host_info_.host_jid.empty());
  EXPECT_TRUE(!remote_host_info_.authorization_code.empty());
  EXPECT_TRUE(!remote_host_info_.shared_secret.empty());
}

TEST_F(RemoteHostInfoFetcherTest, RetrieveRemoteHostInfoInvalidEnvironment) {
  base::RunLoop run_loop;
  RemoteHostInfoCallback remote_host_info_callback =
      base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved,
                 base::Unretained(this), run_loop.QuitClosure());

  RemoteHostInfoFetcher remote_host_info_fetcher;
  bool request_started = remote_host_info_fetcher.RetrieveRemoteHostInfo(
      kTestApplicationId, kAccessTokenValue, kUnknownEnvironment,
      remote_host_info_callback);

  EXPECT_FALSE(request_started);
}

TEST_F(RemoteHostInfoFetcherTest, RetrieveRemoteHostInfoNetworkError) {
  base::RunLoop run_loop;
  RemoteHostInfoCallback remote_host_info_callback =
      base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved,
                 base::Unretained(this), run_loop.QuitClosure());

  RemoteHostInfoFetcher remote_host_info_fetcher;
  bool request_started = remote_host_info_fetcher.RetrieveRemoteHostInfo(
      kTestApplicationId, kAccessTokenValue, kDeveloperEnvironment,
      remote_host_info_callback);

  run_loop.Run();

  EXPECT_TRUE(request_started);
  EXPECT_FALSE(remote_host_info_.IsReadyForConnection());

  // If there was a network error retrieving the remote host info, then none of
  // the connection details should be populated.
  EXPECT_TRUE(remote_host_info_.application_id.empty());
  EXPECT_TRUE(remote_host_info_.host_id.empty());
  EXPECT_TRUE(remote_host_info_.host_jid.empty());
  EXPECT_TRUE(remote_host_info_.authorization_code.empty());
  EXPECT_TRUE(remote_host_info_.shared_secret.empty());
}

TEST_F(RemoteHostInfoFetcherTest, RetrieveRemoteHostInfoPendingResponse) {
  SetFakeResponse(GURL(dev_service_environment_url_),
                  kRemoteHostInfoPendingResponse, net::HTTP_OK,
                  net::URLRequestStatus::SUCCESS);

  base::RunLoop run_loop;
  RemoteHostInfoCallback remote_host_info_callback =
      base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved,
                 base::Unretained(this), run_loop.QuitClosure());

  RemoteHostInfoFetcher remote_host_info_fetcher;
  bool request_started = remote_host_info_fetcher.RetrieveRemoteHostInfo(
      kTestApplicationId, kAccessTokenValue, kDeveloperEnvironment,
      remote_host_info_callback);

  run_loop.Run();

  EXPECT_TRUE(request_started);
  EXPECT_FALSE(remote_host_info_.IsReadyForConnection());

  // If the remote host request is pending, then none of the connection details
  // should be populated.
  EXPECT_TRUE(remote_host_info_.application_id.empty());
  EXPECT_TRUE(remote_host_info_.host_id.empty());
  EXPECT_TRUE(remote_host_info_.host_jid.empty());
  EXPECT_TRUE(remote_host_info_.authorization_code.empty());
  EXPECT_TRUE(remote_host_info_.shared_secret.empty());
}

TEST_F(RemoteHostInfoFetcherTest, RetrieveRemoteHostInfoEmptyResponse) {
  SetFakeResponse(GURL(dev_service_environment_url_),
                  kRemoteHostInfoEmptyResponse, net::HTTP_OK,
                  net::URLRequestStatus::SUCCESS);

  base::RunLoop run_loop;
  RemoteHostInfoCallback remote_host_info_callback =
      base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved,
                 base::Unretained(this), run_loop.QuitClosure());

  RemoteHostInfoFetcher remote_host_info_fetcher;
  bool request_started = remote_host_info_fetcher.RetrieveRemoteHostInfo(
      kTestApplicationId, kAccessTokenValue, kDeveloperEnvironment,
      remote_host_info_callback);

  run_loop.Run();

  EXPECT_TRUE(request_started);
  EXPECT_FALSE(remote_host_info_.IsReadyForConnection());

  // If we received an empty response, then none of the connection details
  // should be populated.
  EXPECT_TRUE(remote_host_info_.application_id.empty());
  EXPECT_TRUE(remote_host_info_.host_id.empty());
  EXPECT_TRUE(remote_host_info_.host_jid.empty());
  EXPECT_TRUE(remote_host_info_.authorization_code.empty());
  EXPECT_TRUE(remote_host_info_.shared_secret.empty());
}

TEST_F(RemoteHostInfoFetcherTest, MultipleRetrieveRemoteHostInfoRequests) {
  // First, we will fetch info from the development service environment.
  SetFakeResponse(GURL(dev_service_environment_url_),
                  kRemoteHostInfoReadyResponse, net::HTTP_OK,
                  net::URLRequestStatus::SUCCESS);

  base::RunLoop dev_run_loop;
  RemoteHostInfoCallback dev_remote_host_info_callback =
      base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved,
                 base::Unretained(this), dev_run_loop.QuitClosure());

  RemoteHostInfoFetcher remote_host_info_fetcher;
  bool dev_request_started = remote_host_info_fetcher.RetrieveRemoteHostInfo(
      kTestApplicationId, kAccessTokenValue, kDeveloperEnvironment,
      dev_remote_host_info_callback);

  dev_run_loop.Run();

  EXPECT_TRUE(dev_request_started);
  EXPECT_TRUE(remote_host_info_.IsReadyForConnection());
  EXPECT_EQ(remote_host_info_.application_id.compare(kTestApplicationId), 0);
  EXPECT_TRUE(!remote_host_info_.host_id.empty());
  EXPECT_TRUE(!remote_host_info_.host_jid.empty());
  EXPECT_TRUE(!remote_host_info_.authorization_code.empty());
  EXPECT_TRUE(!remote_host_info_.shared_secret.empty());

  // Next, we will fetch a different application info block from the dev
  // service environment.
  SetFakeResponse(GURL(dev_service_environment_url_2_),
                  kRemoteHostInfoReadyResponse2, net::HTTP_OK,
                  net::URLRequestStatus::SUCCESS);

  base::RunLoop test_run_loop;
  RemoteHostInfoCallback test_remote_host_info_callback =
      base::Bind(&RemoteHostInfoFetcherTest::OnRemoteHostInfoRetrieved,
                 base::Unretained(this), test_run_loop.QuitClosure());

  // Reset the state of our internal |remote_host_info_| object.
  remote_host_info_ = RemoteHostInfo();
  EXPECT_FALSE(remote_host_info_.IsReadyForConnection());

  bool test_request_started = remote_host_info_fetcher.RetrieveRemoteHostInfo(
      kTestApplicationId2, kAccessTokenValue, kDeveloperEnvironment,
      test_remote_host_info_callback);

  test_run_loop.Run();

  EXPECT_TRUE(test_request_started);
  EXPECT_TRUE(remote_host_info_.IsReadyForConnection());
  EXPECT_EQ(remote_host_info_.application_id.compare(kTestApplicationId2), 0);
  EXPECT_TRUE(!remote_host_info_.host_id.empty());
  EXPECT_TRUE(!remote_host_info_.host_jid.empty());
  EXPECT_TRUE(!remote_host_info_.authorization_code.empty());
  EXPECT_TRUE(!remote_host_info_.shared_secret.empty());
}

}  // namespace test
}  // namespace remoting