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
|
// 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/protocol/webrtc_transport.h"
#include <utility>
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "jingle/glue/thread_wrapper.h"
#include "net/base/io_buffer.h"
#include "net/url_request/url_request_context_getter.h"
#include "remoting/protocol/connection_tester.h"
#include "remoting/protocol/fake_authenticator.h"
#include "remoting/protocol/message_channel_factory.h"
#include "remoting/protocol/message_pipe.h"
#include "remoting/protocol/network_settings.h"
#include "remoting/protocol/transport_context.h"
#include "remoting/signaling/fake_signal_strategy.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
namespace remoting {
namespace protocol {
namespace {
const char kChannelName[] = "test_channel";
class TestTransportEventHandler : public WebrtcTransport::EventHandler {
public:
typedef base::Callback<void(ErrorCode error)> ErrorCallback;
TestTransportEventHandler() {}
~TestTransportEventHandler() {}
// All callbacks must be set before the test handler is passed to a Transport
// object.
void set_connecting_callback(const base::Closure& callback) {
connecting_callback_ = callback;
}
void set_connected_callback(const base::Closure& callback) {
connected_callback_ = callback;
}
void set_error_callback(const ErrorCallback& callback) {
error_callback_ = callback;
}
// WebrtcTransport::EventHandler interface.
void OnWebrtcTransportConnecting() override {
if (!connecting_callback_.is_null())
connecting_callback_.Run();
}
void OnWebrtcTransportConnected() override {
if (!connected_callback_.is_null())
connected_callback_.Run();
}
void OnWebrtcTransportError(ErrorCode error) override {
error_callback_.Run(error);
}
void OnWebrtcTransportMediaStreamAdded(
scoped_refptr<webrtc::MediaStreamInterface> stream) override {}
void OnWebrtcTransportMediaStreamRemoved(
scoped_refptr<webrtc::MediaStreamInterface> stream) override {}
private:
base::Closure connecting_callback_;
base::Closure connected_callback_;
ErrorCallback error_callback_;
DISALLOW_COPY_AND_ASSIGN(TestTransportEventHandler);
};
} // namespace
class WebrtcTransportTest : public testing::Test {
public:
WebrtcTransportTest() {
jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop();
network_settings_ =
NetworkSettings(NetworkSettings::NAT_TRAVERSAL_OUTGOING);
}
void TearDown() override {
run_loop_.reset();
client_message_pipe_.reset();
client_transport_.reset();
host_message_pipe_.reset();
host_transport_.reset();
base::RunLoop().RunUntilIdle();
}
void ProcessTransportInfo(scoped_ptr<WebrtcTransport>* target_transport,
scoped_ptr<buzz::XmlElement> transport_info) {
ASSERT_TRUE(target_transport);
EXPECT_TRUE((*target_transport)
->ProcessTransportInfo(transport_info.get()));
}
void InitializeConnection() {
host_transport_.reset(
new WebrtcTransport(jingle_glue::JingleThreadWrapper::current(),
TransportContext::ForTests(TransportRole::SERVER),
&host_event_handler_));
host_authenticator_.reset(new FakeAuthenticator(
FakeAuthenticator::HOST, 0, FakeAuthenticator::ACCEPT, false));
client_transport_.reset(
new WebrtcTransport(jingle_glue::JingleThreadWrapper::current(),
TransportContext::ForTests(TransportRole::CLIENT),
&client_event_handler_));
client_authenticator_.reset(new FakeAuthenticator(
FakeAuthenticator::CLIENT, 0, FakeAuthenticator::ACCEPT, false));
}
void StartConnection() {
host_event_handler_.set_connected_callback(base::Bind(&base::DoNothing));
client_event_handler_.set_connected_callback(base::Bind(&base::DoNothing));
host_event_handler_.set_error_callback(
base::Bind(&WebrtcTransportTest::OnSessionError, base::Unretained(this),
TransportRole::SERVER));
client_event_handler_.set_error_callback(
base::Bind(&WebrtcTransportTest::OnSessionError, base::Unretained(this),
TransportRole::CLIENT));
// Start both transports.
host_transport_->Start(
host_authenticator_.get(),
base::Bind(&WebrtcTransportTest::ProcessTransportInfo,
base::Unretained(this), &client_transport_));
client_transport_->Start(
client_authenticator_.get(),
base::Bind(&WebrtcTransportTest::ProcessTransportInfo,
base::Unretained(this), &host_transport_));
}
void WaitUntilConnected() {
int counter = 2;
host_event_handler_.set_connected_callback(
base::Bind(&WebrtcTransportTest::QuitRunLoopOnCounter,
base::Unretained(this), &counter));
client_event_handler_.set_connected_callback(
base::Bind(&WebrtcTransportTest::QuitRunLoopOnCounter,
base::Unretained(this), &counter));
run_loop_.reset(new base::RunLoop());
run_loop_->Run();
host_event_handler_.set_connected_callback(base::Closure());
client_event_handler_.set_connected_callback(base::Closure());
EXPECT_EQ(OK, client_error_);
EXPECT_EQ(OK, host_error_);
}
void CreateClientDataStream() {
client_transport_->incoming_channel_factory()->CreateChannel(
kChannelName, base::Bind(&WebrtcTransportTest::OnClientChannelCreated,
base::Unretained(this)));
}
void CreateHostDataStream() {
host_transport_->outgoing_channel_factory()->CreateChannel(
kChannelName, base::Bind(&WebrtcTransportTest::OnHostChannelCreated,
base::Unretained(this)));
}
void OnClientChannelCreated(scoped_ptr<MessagePipe> pipe) {
client_message_pipe_ = std::move(pipe);
if (run_loop_ && host_message_pipe_)
run_loop_->Quit();
}
void OnHostChannelCreated(scoped_ptr<MessagePipe> pipe) {
host_message_pipe_ = std::move(pipe);
if (run_loop_ && client_message_pipe_)
run_loop_->Quit();
}
void OnSessionError(TransportRole role, ErrorCode error) {
if (role == TransportRole::SERVER) {
host_error_ = error;
if (destroy_on_error_) {
host_message_pipe_.reset();
host_transport_.reset();
}
} else {
CHECK(role == TransportRole::CLIENT);
client_error_ = error;
if (destroy_on_error_) {
client_message_pipe_.reset();
client_transport_.reset();
}
}
run_loop_->Quit();
}
void QuitRunLoopOnCounter(int* counter) {
--(*counter);
if (*counter == 0)
run_loop_->Quit();
}
protected:
base::MessageLoopForIO message_loop_;
scoped_ptr<base::RunLoop> run_loop_;
NetworkSettings network_settings_;
scoped_ptr<WebrtcTransport> host_transport_;
TestTransportEventHandler host_event_handler_;
scoped_ptr<FakeAuthenticator> host_authenticator_;
scoped_ptr<WebrtcTransport> client_transport_;
TestTransportEventHandler client_event_handler_;
scoped_ptr<FakeAuthenticator> client_authenticator_;
scoped_ptr<MessagePipe> client_message_pipe_;
scoped_ptr<MessagePipe> host_message_pipe_;
ErrorCode client_error_ = OK;
ErrorCode host_error_ = OK;
bool destroy_on_error_ = false;
};
TEST_F(WebrtcTransportTest, Connects) {
InitializeConnection();
StartConnection();
WaitUntilConnected();
}
TEST_F(WebrtcTransportTest, DataStream) {
client_event_handler_.set_connecting_callback(base::Bind(
&WebrtcTransportTest::CreateClientDataStream, base::Unretained(this)));
host_event_handler_.set_connecting_callback(base::Bind(
&WebrtcTransportTest::CreateHostDataStream, base::Unretained(this)));
InitializeConnection();
StartConnection();
run_loop_.reset(new base::RunLoop());
run_loop_->Run();
EXPECT_TRUE(client_message_pipe_);
EXPECT_TRUE(host_message_pipe_);
const int kMessageSize = 1024;
const int kMessages = 100;
MessagePipeConnectionTester tester(host_message_pipe_.get(),
client_message_pipe_.get(), kMessageSize,
kMessages);
tester.RunAndCheckResults();
}
// Verify that data streams can be created after connection has been initiated.
TEST_F(WebrtcTransportTest, DataStreamLate) {
InitializeConnection();
StartConnection();
WaitUntilConnected();
CreateClientDataStream();
CreateHostDataStream();
run_loop_.reset(new base::RunLoop());
run_loop_->Run();
EXPECT_TRUE(client_message_pipe_);
EXPECT_TRUE(host_message_pipe_);
}
TEST_F(WebrtcTransportTest, TerminateDataChannel) {
InitializeConnection();
StartConnection();
WaitUntilConnected();
CreateClientDataStream();
CreateHostDataStream();
run_loop_.reset(new base::RunLoop());
run_loop_->Run();
EXPECT_TRUE(client_message_pipe_);
EXPECT_TRUE(host_message_pipe_);
destroy_on_error_ = true;
// Destroy pipe on one side of the of the connection. It should get closed on
// the other side.
client_message_pipe_.reset();
run_loop_.reset(new base::RunLoop());
run_loop_->Run();
// Check that OnSessionError() has been called.
EXPECT_EQ(CHANNEL_CONNECTION_ERROR, host_error_);
EXPECT_FALSE(host_transport_);
}
} // namespace protocol
} // namespace remoting
|