forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost_list_fetcher_unittest.cc
491 lines (409 loc) · 17.5 KB
/
host_list_fetcher_unittest.cc
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// 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/host_list_fetcher.h"
#include "base/bind.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "remoting/test/host_info.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
// Used as a HostListCallback for testing.
void OnHostlistRetrieved(
base::Closure done_closure,
std::vector<remoting::test::HostInfo>* hostlist,
const std::vector<remoting::test::HostInfo>& retrieved_hostlist) {
*hostlist = retrieved_hostlist;
done_closure.Run();
}
const char kAccessTokenValue[] = "test_access_token_value";
const char kHostListReadyResponse[] =
"{"
" \"data\":{"
" \"kind\":\"chromoting#hostList\","
" \"items\":["
" {"
" \"tokenUrlPatterns\":["
" \"tokenUrlPattern_1A\","
" \"tokenUrlPattern_1B\","
" \"tokenUrlPattern_1C\""
" ],"
" \"kind\":\"chromoting#host\","
" \"hostId\":\"test_host_id_1\","
" \"hostName\":\"test_host_name_1\","
" \"publicKey\":\"test_public_key_1\","
" \"jabberId\":\"test_jabber_id_1\","
" \"createdTime\":\"test_created_time_1\","
" \"updatedTime\":\"test_updated_time_1\","
" \"status\":\"ONLINE\","
" \"hostOfflineReason\":\"\","
" \"hostVersion\":\"test_host_version_1\""
" },"
" {"
" \"kind\":\"chromoting#host\","
" \"hostId\":\"test_host_id_2\","
" \"hostName\":\"test_host_name_2\","
" \"publicKey\":\"test_public_key_2\","
" \"jabberId\":\"test_jabber_id_2\","
" \"createdTime\":\"test_created_time_2\","
" \"updatedTime\":\"test_updated_time_2\","
" \"status\":\"OFFLINE\","
" \"hostOfflineReason\":\"test_host_offline_reason_2\","
" \"hostVersion\":\"test_host_version_2\""
" }"
" ]"
" }"
"}";
const char kHostListMissingParametersResponse[] =
"{"
" \"data\":{"
" \"kind\":\"chromoting#hostList\","
" \"items\":["
" {"
" \"tokenUrlPatterns\":["
" \"tokenUrlPattern_1A\","
" \"tokenUrlPattern_1B\","
" \"tokenUrlPattern_1C\""
" ],"
" \"kind\":\"chromoting#host\","
" \"hostId\":\"test_host_id_1\","
" \"hostName\":\"test_host_name_1\","
" \"publicKey\":\"test_public_key_1\","
" \"createdTime\":\"test_created_time_1\","
" \"updatedTime\":\"test_updated_time_1\","
" \"status\":\"OFFLINE\","
" \"hostOfflineReason\":\"\","
" \"hostVersion\":\"test_host_version_1\""
" },"
" {"
" \"kind\":\"chromoting#host\","
" \"hostName\":\"test_host_name_2\","
" \"publicKey\":\"test_public_key_2\","
" \"jabberId\":\"test_jabber_id_2\","
" \"createdTime\":\"test_created_time_2\","
" \"updatedTime\":\"test_updated_time_2\","
" \"status\":\"ONLINE\","
" \"hostOfflineReason\":\"\","
" \"hostVersion\":\"test_host_version_2\""
" },"
" {"
" \"kind\":\"chromoting#host\","
" \"hostId\":\"test_host_id_3\","
" \"publicKey\":\"test_public_key_3\","
" \"jabberId\":\"test_jabber_id_3\","
" \"createdTime\":\"test_created_time_3\","
" \"updatedTime\":\"test_updated_time_3\","
" \"status\":\"ONLINE\","
" \"hostOfflineReason\":\"\","
" \"hostVersion\":\"test_host_version_3\""
" },"
" {"
" \"kind\":\"chromoting#host\","
" \"hostId\":\"test_host_id_4\","
" \"hostName\":\"test_host_name_4\","
" \"jabberId\":\"test_jabber_id_4\","
" \"createdTime\":\"test_created_time_4\","
" \"updatedTime\":\"test_updated_time_4\","
" \"status\":\"ONLINE\","
" \"hostOfflineReason\":\"\","
" \"hostVersion\":\"test_host_version_4\""
" },"
" {"
" \"kind\":\"chromoting#host\","
" \"hostId\":\"test_host_id_5\","
" \"hostName\":\"test_host_name_5\","
" \"publicKey\":\"test_public_key_5\","
" \"jabberId\":\"test_jabber_id_5\","
" \"createdTime\":\"test_created_time_5\","
" \"updatedTime\":\"test_updated_time_5\","
" \"status\":\"OFFLINE\","
" \"hostVersion\":\"test_host_version_5\""
" }"
" ]"
" }"
"}";
const char kHostListEmptyTokenUrlPatternsResponse[] =
"{"
" \"data\":{"
" \"kind\":\"chromoting#hostList\","
" \"items\":["
" {"
" \"tokenUrlPatterns\":["
" ],"
" \"kind\":\"chromoting#host\","
" \"hostId\":\"test_host_id_1\","
" \"hostName\":\"test_host_name_1\","
" \"publicKey\":\"test_public_key_1\","
" \"jabberId\":\"test_jabber_id_1\","
" \"createdTime\":\"test_created_time_1\","
" \"updatedTime\":\"test_updated_time_1\","
" \"status\":\"ONLINE\","
" \"hostOfflineReason\":\"\","
" \"hostVersion\":\"test_host_version_1\""
" }"
" ]"
" }"
"}";
const char kHostListEmptyItemsResponse[] =
"{"
" \"data\":{"
" \"kind\":\"chromoting#hostList\","
" \"items\":["
" ]"
" }"
"}";
const char kHostListEmptyResponse[] = "{}";
const unsigned int kExpectedEmptyPatternsHostListSize = 1;
const unsigned int kExpectedHostListSize = 2;
const unsigned int kExpectedPatternsSize = 3;
} // namespace
namespace remoting {
namespace test {
// Provides base functionality for the HostListFetcher 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
// HostListFetcher. This fixture also creates an IO MessageLoop
// for use by the HostListFetcher.
class HostListFetcherTest : public ::testing::Test {
public:
HostListFetcherTest() : url_fetcher_factory_(nullptr) {}
~HostListFetcherTest() override = default;
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);
private:
net::FakeURLFetcherFactory url_fetcher_factory_;
std::unique_ptr<base::MessageLoopForIO> message_loop_;
DISALLOW_COPY_AND_ASSIGN(HostListFetcherTest);
};
void HostListFetcherTest::SetUp() {
DCHECK(!message_loop_);
message_loop_.reset(new base::MessageLoopForIO);
SetFakeResponse(GURL(kHostListProdRequestUrl),
kHostListEmptyResponse, net::HTTP_NOT_FOUND,
net::URLRequestStatus::FAILED);
SetFakeResponse(GURL(kHostListTestRequestUrl), kHostListEmptyResponse,
net::HTTP_NOT_FOUND, net::URLRequestStatus::FAILED);
}
void HostListFetcherTest::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(HostListFetcherTest, RetrieveHostListFromProd) {
SetFakeResponse(GURL(kHostListProdRequestUrl),
kHostListReadyResponse, net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
std::vector<HostInfo> hostlist;
base::RunLoop run_loop;
HostListFetcher::HostlistCallback host_list_callback =
base::Bind(&OnHostlistRetrieved, run_loop.QuitClosure(), &hostlist);
HostListFetcher host_list_fetcher;
host_list_fetcher.RetrieveHostlist(kAccessTokenValue, kHostListProdRequestUrl,
host_list_callback);
run_loop.Run();
EXPECT_EQ(hostlist.size(), kExpectedHostListSize);
HostInfo online_host_info = hostlist.at(0);
EXPECT_EQ(online_host_info.token_url_patterns.size(), kExpectedPatternsSize);
EXPECT_FALSE(online_host_info.host_id.empty());
EXPECT_FALSE(online_host_info.host_jid.empty());
EXPECT_FALSE(online_host_info.host_name.empty());
EXPECT_EQ(online_host_info.status, HostStatus::kHostStatusOnline);
EXPECT_TRUE(online_host_info.offline_reason.empty());
EXPECT_FALSE(online_host_info.public_key.empty());
HostInfo offline_host_info = hostlist.at(1);
EXPECT_TRUE(offline_host_info.token_url_patterns.empty());
EXPECT_FALSE(offline_host_info.host_id.empty());
EXPECT_FALSE(offline_host_info.host_jid.empty());
EXPECT_FALSE(offline_host_info.host_name.empty());
EXPECT_EQ(offline_host_info.status, HostStatus::kHostStatusOffline);
EXPECT_FALSE(offline_host_info.offline_reason.empty());
EXPECT_FALSE(offline_host_info.public_key.empty());
}
TEST_F(HostListFetcherTest, RetrieveHostListFromTest) {
SetFakeResponse(GURL(kHostListTestRequestUrl), kHostListReadyResponse,
net::HTTP_OK, net::URLRequestStatus::SUCCESS);
std::vector<HostInfo> hostlist;
base::RunLoop run_loop;
HostListFetcher::HostlistCallback host_list_callback =
base::Bind(&OnHostlistRetrieved, run_loop.QuitClosure(), &hostlist);
HostListFetcher host_list_fetcher;
host_list_fetcher.RetrieveHostlist(kAccessTokenValue, kHostListTestRequestUrl,
host_list_callback);
run_loop.Run();
EXPECT_EQ(hostlist.size(), kExpectedHostListSize);
HostInfo online_host_info = hostlist.at(0);
EXPECT_EQ(online_host_info.token_url_patterns.size(), kExpectedPatternsSize);
EXPECT_FALSE(online_host_info.host_id.empty());
EXPECT_FALSE(online_host_info.host_jid.empty());
EXPECT_FALSE(online_host_info.host_name.empty());
EXPECT_EQ(online_host_info.status, HostStatus::kHostStatusOnline);
EXPECT_TRUE(online_host_info.offline_reason.empty());
EXPECT_FALSE(online_host_info.public_key.empty());
HostInfo offline_host_info = hostlist.at(1);
EXPECT_TRUE(offline_host_info.token_url_patterns.empty());
EXPECT_FALSE(offline_host_info.host_id.empty());
EXPECT_FALSE(offline_host_info.host_jid.empty());
EXPECT_FALSE(offline_host_info.host_name.empty());
EXPECT_EQ(offline_host_info.status, HostStatus::kHostStatusOffline);
EXPECT_FALSE(offline_host_info.offline_reason.empty());
EXPECT_FALSE(offline_host_info.public_key.empty());
}
TEST_F(HostListFetcherTest, RetrieveHostListWithEmptyPatterns) {
SetFakeResponse(GURL(kHostListProdRequestUrl),
kHostListEmptyTokenUrlPatternsResponse, net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
std::vector<HostInfo> hostlist;
base::RunLoop run_loop;
HostListFetcher::HostlistCallback host_list_callback =
base::Bind(&OnHostlistRetrieved, run_loop.QuitClosure(), &hostlist);
HostListFetcher host_list_fetcher;
host_list_fetcher.RetrieveHostlist(kAccessTokenValue, kHostListProdRequestUrl,
host_list_callback);
run_loop.Run();
EXPECT_EQ(hostlist.size(), kExpectedEmptyPatternsHostListSize);
// While this is unlikely to happen, empty token url patterns are handled.
HostInfo online_host_info = hostlist.at(0);
EXPECT_TRUE(online_host_info.token_url_patterns.empty());
EXPECT_FALSE(online_host_info.host_id.empty());
EXPECT_FALSE(online_host_info.host_jid.empty());
EXPECT_FALSE(online_host_info.host_name.empty());
EXPECT_EQ(online_host_info.status, HostStatus::kHostStatusOnline);
EXPECT_TRUE(online_host_info.offline_reason.empty());
EXPECT_FALSE(online_host_info.public_key.empty());
}
TEST_F(HostListFetcherTest,
RetrieveHostListMissingParametersResponse) {
SetFakeResponse(GURL(kHostListProdRequestUrl),
kHostListMissingParametersResponse, net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
std::vector<HostInfo> hostlist;
base::RunLoop run_loop;
HostListFetcher::HostlistCallback host_list_callback =
base::Bind(&OnHostlistRetrieved, run_loop.QuitClosure(), &hostlist);
HostListFetcher host_list_fetcher;
host_list_fetcher.RetrieveHostlist(kAccessTokenValue, kHostListProdRequestUrl,
host_list_callback);
run_loop.Run();
EXPECT_EQ(hostlist.size(), kExpectedHostListSize);
HostInfo no_jid_host_info = hostlist.at(0);
EXPECT_EQ(no_jid_host_info.token_url_patterns.size(), kExpectedPatternsSize);
EXPECT_FALSE(no_jid_host_info.host_id.empty());
EXPECT_TRUE(no_jid_host_info.host_jid.empty());
EXPECT_FALSE(no_jid_host_info.host_name.empty());
EXPECT_EQ(no_jid_host_info.status, HostStatus::kHostStatusOffline);
EXPECT_TRUE(no_jid_host_info.offline_reason.empty());
EXPECT_FALSE(no_jid_host_info.public_key.empty());
HostInfo no_offline_reason_host_info = hostlist.at(1);
EXPECT_TRUE(no_offline_reason_host_info.token_url_patterns.empty());
EXPECT_FALSE(no_offline_reason_host_info.host_id.empty());
EXPECT_FALSE(no_offline_reason_host_info.host_jid.empty());
EXPECT_FALSE(no_offline_reason_host_info.host_name.empty());
EXPECT_EQ(no_offline_reason_host_info.status, HostStatus::kHostStatusOffline);
EXPECT_TRUE(no_offline_reason_host_info.offline_reason.empty());
EXPECT_FALSE(no_offline_reason_host_info.public_key.empty());
}
TEST_F(HostListFetcherTest, RetrieveHostListNetworkError) {
base::RunLoop run_loop;
std::vector<HostInfo> hostlist;
HostListFetcher::HostlistCallback host_list_callback =
base::Bind(&OnHostlistRetrieved, run_loop.QuitClosure(), &hostlist);
HostListFetcher host_list_fetcher;
host_list_fetcher.RetrieveHostlist(kAccessTokenValue, kHostListProdRequestUrl,
host_list_callback);
run_loop.Run();
// If there was a network error retrieving the host list, then the host list
// should be empty.
EXPECT_TRUE(hostlist.empty());
}
TEST_F(HostListFetcherTest, RetrieveHostListEmptyItemsResponse) {
SetFakeResponse(GURL(kHostListProdRequestUrl),
kHostListEmptyItemsResponse, net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
base::RunLoop run_loop;
std::vector<HostInfo> hostlist;
HostListFetcher::HostlistCallback host_list_callback =
base::Bind(&OnHostlistRetrieved, run_loop.QuitClosure(), &hostlist);
HostListFetcher host_list_fetcher;
host_list_fetcher.RetrieveHostlist(kAccessTokenValue, kHostListProdRequestUrl,
host_list_callback);
run_loop.Run();
// If we received an empty items response, then host list should be empty.
EXPECT_TRUE(hostlist.empty());
}
TEST_F(HostListFetcherTest, RetrieveHostListEmptyResponse) {
SetFakeResponse(GURL(kHostListProdRequestUrl),
kHostListEmptyResponse, net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
base::RunLoop run_loop;
std::vector<HostInfo> hostlist;
HostListFetcher::HostlistCallback host_list_callback =
base::Bind(&OnHostlistRetrieved, run_loop.QuitClosure(), &hostlist);
HostListFetcher host_list_fetcher;
host_list_fetcher.RetrieveHostlist(kAccessTokenValue, kHostListProdRequestUrl,
host_list_callback);
run_loop.Run();
// If we received an empty response, then host list should be empty.
EXPECT_TRUE(hostlist.empty());
}
TEST_F(HostListFetcherTest, MultipleRetrieveHostListRequests) {
// First, we will retrieve a valid response from the directory service.
SetFakeResponse(GURL(kHostListProdRequestUrl),
kHostListReadyResponse, net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
std::vector<HostInfo> ready_hostlist;
base::RunLoop ready_run_loop;
HostListFetcher::HostlistCallback ready_host_list_callback =
base::Bind(&OnHostlistRetrieved,
ready_run_loop.QuitClosure(),
&ready_hostlist);
HostListFetcher host_list_fetcher;
host_list_fetcher.RetrieveHostlist(kAccessTokenValue, kHostListProdRequestUrl,
ready_host_list_callback);
ready_run_loop.Run();
EXPECT_EQ(ready_hostlist.size(), kExpectedHostListSize);
HostInfo online_host_info = ready_hostlist.at(0);
EXPECT_EQ(online_host_info.token_url_patterns.size(), kExpectedPatternsSize);
EXPECT_FALSE(online_host_info.host_id.empty());
EXPECT_FALSE(online_host_info.host_jid.empty());
EXPECT_FALSE(online_host_info.host_name.empty());
EXPECT_EQ(online_host_info.status, HostStatus::kHostStatusOnline);
EXPECT_TRUE(online_host_info.offline_reason.empty());
EXPECT_FALSE(online_host_info.public_key.empty());
HostInfo offline_host_info = ready_hostlist.at(1);
EXPECT_TRUE(offline_host_info.token_url_patterns.empty());
EXPECT_FALSE(offline_host_info.host_id.empty());
EXPECT_FALSE(offline_host_info.host_jid.empty());
EXPECT_FALSE(offline_host_info.host_name.empty());
EXPECT_EQ(offline_host_info.status, HostStatus::kHostStatusOffline);
EXPECT_FALSE(offline_host_info.offline_reason.empty());
EXPECT_FALSE(offline_host_info.public_key.empty());
// Next, we will retrieve an empty items response from the directory service.
SetFakeResponse(GURL(kHostListProdRequestUrl),
kHostListEmptyItemsResponse, net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
std::vector<HostInfo> empty_items_hostlist;
base::RunLoop empty_items_run_loop;
HostListFetcher::HostlistCallback empty_host_list_callback =
base::Bind(&OnHostlistRetrieved,
empty_items_run_loop.QuitClosure(),
&empty_items_hostlist);
// Re-use the same host_list_fetcher.
host_list_fetcher.RetrieveHostlist(kAccessTokenValue, kHostListProdRequestUrl,
empty_host_list_callback);
empty_items_run_loop.Run();
// If we received an empty items response, then host list should be empty.
EXPECT_TRUE(empty_items_hostlist.empty());
}
} // namespace test
} // namespace remoting