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
|
// Copyright 2016 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/protocol/http_ice_config_request.h"
#include "base/bind.h"
#include "base/logging.h"
#include "remoting/base/url_request.h"
#include "remoting/protocol/ice_config.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace remoting {
namespace protocol {
namespace {
class FakeUrlRequest : public UrlRequest {
public:
FakeUrlRequest(const Result& result) : result_(result) {}
~FakeUrlRequest() override {}
// UrlRequest interface.
void AddHeader(const std::string& value) override { NOTREACHED(); }
void SetPostData(const std::string& content_type,
const std::string& post_data) override {
EXPECT_EQ("application/json", content_type);
EXPECT_EQ("", post_data);
}
void Start(const OnResultCallback& on_result_callback) override {
on_result_callback.Run(result_);
}
private:
Result result_;
};
class FakeUrlRequestFactory : public UrlRequestFactory {
public:
FakeUrlRequestFactory() {}
~FakeUrlRequestFactory() override {}
void SetResult(const std::string& url, const UrlRequest::Result& result) {
results_[url] = result;
}
// UrlRequestFactory interface.
scoped_ptr<UrlRequest> CreateUrlRequest(UrlRequest::Type type,
const std::string& url) override {
EXPECT_EQ(UrlRequest::Type::POST, type);
CHECK(results_.count(url));
return make_scoped_ptr(new FakeUrlRequest(results_[url]));
}
std::map<std::string, UrlRequest::Result> results_;
};
bool operator==(const cricket::ProtocolAddress& a,
const cricket::ProtocolAddress& b) {
return a.address == b.address && a.proto == b.proto && a.secure == b.secure;
}
bool operator==(const cricket::RelayServerConfig& a,
const cricket::RelayServerConfig& b) {
if (a.ports.size() != b.ports.size())
return false;
for (size_t i = 0; i < a.ports.size(); ++i) {
if (!(a.ports[i] == b.ports[i]))
return false;
}
return a.type == b.type &&
a.credentials.username == b.credentials.username &&
a.credentials.password == b.credentials.password;
}
} // namespace
static const char kTestUrl[] = "http://host/ice_config";
class HttpIceConfigRequestTest : public testing::Test {
public:
void OnResult(const IceConfig& config) {
received_config_ = make_scoped_ptr(new IceConfig(config));
}
protected:
FakeUrlRequestFactory url_request_factory_;
scoped_ptr<HttpIceConfigRequest> request_;
scoped_ptr<IceConfig> received_config_;
};
TEST_F(HttpIceConfigRequestTest, Parse) {
const char kTestResponse[] =
"{"
" \"lifetimeDuration\": \"43200.000s\","
" \"iceServers\": ["
" {"
" \"urls\": ["
" \"turn:8.8.8.8:19234\","
" \"turn:[2001:4860:4860::8888]:333\","
" \"turn:[2001:4860:4860::8888]\","
" \"turn:[2001:4860:4860::8888]:333?transport=tcp\","
" \"turns:the_server.com\","
" \"turns:the_server.com?transport=udp\""
" ],"
" \"username\": \"123\","
" \"credential\": \"abc\""
" },"
" {"
" \"urls\": ["
" \"stun:stun_server.com:18344\","
" \"stun:1.2.3.4\""
" ]"
" }"
" ]"
"}";
url_request_factory_.SetResult(kTestUrl,
UrlRequest::Result(200, kTestResponse));
request_.reset(new HttpIceConfigRequest(&url_request_factory_, kTestUrl));
request_->Send(
base::Bind(&HttpIceConfigRequestTest::OnResult, base::Unretained(this)));
ASSERT_FALSE(received_config_->is_null());
// lifetimeDuration in the config is set to 12 hours. HttpIceConfigRequest
// substracts 1 hour. Verify that |expiration_time| is in the rage from 10 to
// 12 hours from now.
EXPECT_TRUE(base::Time::Now() + base::TimeDelta::FromHours(10) <
received_config_->expiration_time);
EXPECT_TRUE(received_config_->expiration_time <
base::Time::Now() + base::TimeDelta::FromHours(12));
EXPECT_EQ(6U, received_config_->turn_servers.size());
EXPECT_TRUE(cricket::RelayServerConfig("8.8.8.8", 19234, "123", "abc",
cricket::PROTO_UDP, false) ==
received_config_->turn_servers[0]);
EXPECT_TRUE(cricket::RelayServerConfig("2001:4860:4860::8888", 333, "123",
"abc", cricket::PROTO_UDP, false) ==
received_config_->turn_servers[1]);
EXPECT_TRUE(cricket::RelayServerConfig("2001:4860:4860::8888", 3478, "123",
"abc", cricket::PROTO_UDP, false) ==
received_config_->turn_servers[2]);
EXPECT_TRUE(cricket::RelayServerConfig("2001:4860:4860::8888", 333, "123",
"abc", cricket::PROTO_TCP, false) ==
received_config_->turn_servers[3]);
EXPECT_TRUE(cricket::RelayServerConfig("the_server.com", 5349, "123", "abc",
cricket::PROTO_TCP, true) ==
received_config_->turn_servers[4]);
EXPECT_TRUE(cricket::RelayServerConfig("the_server.com", 5349, "123", "abc",
cricket::PROTO_UDP, true) ==
received_config_->turn_servers[5]);
EXPECT_EQ(2U, received_config_->stun_servers.size());
EXPECT_EQ(rtc::SocketAddress("stun_server.com", 18344),
received_config_->stun_servers[0]);
EXPECT_EQ(rtc::SocketAddress("1.2.3.4", 3478),
received_config_->stun_servers[1]);
}
// Verify that we can still proceed if some servers cannot be parsed.
TEST_F(HttpIceConfigRequestTest, ParsePartiallyInvalid) {
const char kTestResponse[] =
"{"
" \"lifetimeDuration\": \"43200.000s\","
" \"iceServers\": ["
" {"
" \"urls\": ["
" \"InvalidURL\","
" \"turn:[2001:4860:4860::8888]:333\""
" ],"
" \"username\": \"123\","
" \"credential\": \"abc\""
" },"
" \"42\""
" ]"
"}";
url_request_factory_.SetResult(kTestUrl,
UrlRequest::Result(200, kTestResponse));
request_.reset(new HttpIceConfigRequest(&url_request_factory_, kTestUrl));
request_->Send(
base::Bind(&HttpIceConfigRequestTest::OnResult, base::Unretained(this)));
ASSERT_FALSE(received_config_->is_null());
// Config should be already expired because it couldn't be parsed.
EXPECT_TRUE(received_config_->expiration_time < base::Time::Now());
EXPECT_EQ(1U, received_config_->turn_servers.size());
EXPECT_TRUE(cricket::RelayServerConfig("2001:4860:4860::8888", 333, "123",
"abc", cricket::PROTO_UDP, false) ==
received_config_->turn_servers[0]);
}
TEST_F(HttpIceConfigRequestTest, NotParseable) {
url_request_factory_.SetResult(kTestUrl,
UrlRequest::Result(200, "ERROR"));
request_.reset(new HttpIceConfigRequest(&url_request_factory_, kTestUrl));
request_->Send(
base::Bind(&HttpIceConfigRequestTest::OnResult, base::Unretained(this)));
EXPECT_TRUE(received_config_->is_null());
}
TEST_F(HttpIceConfigRequestTest, FailedRequest) {
url_request_factory_.SetResult(kTestUrl, UrlRequest::Result::Failed());
request_.reset(new HttpIceConfigRequest(&url_request_factory_, kTestUrl));
request_->Send(
base::Bind(&HttpIceConfigRequestTest::OnResult, base::Unretained(this)));
EXPECT_TRUE(received_config_->is_null());
}
} // namespace protocol
} // namespace remoting
|