OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/protocol/jingle_session.h" | 5 #include "remoting/protocol/jingle_session.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
15 #include "base/test/test_timeouts.h" | 16 #include "base/test/test_timeouts.h" |
16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
17 #include "net/socket/socket.h" | 18 #include "net/socket/socket.h" |
18 #include "net/socket/stream_socket.h" | 19 #include "net/socket/stream_socket.h" |
19 #include "net/url_request/url_request_context_getter.h" | 20 #include "net/url_request/url_request_context_getter.h" |
20 #include "remoting/base/constants.h" | 21 #include "remoting/base/constants.h" |
21 #include "remoting/protocol/authenticator.h" | 22 #include "remoting/protocol/authenticator.h" |
22 #include "remoting/protocol/channel_authenticator.h" | 23 #include "remoting/protocol/channel_authenticator.h" |
23 #include "remoting/protocol/chromium_port_allocator_factory.h" | 24 #include "remoting/protocol/chromium_port_allocator_factory.h" |
24 #include "remoting/protocol/connection_tester.h" | 25 #include "remoting/protocol/connection_tester.h" |
25 #include "remoting/protocol/fake_authenticator.h" | 26 #include "remoting/protocol/fake_authenticator.h" |
26 #include "remoting/protocol/jingle_session_manager.h" | 27 #include "remoting/protocol/jingle_session_manager.h" |
27 #include "remoting/protocol/network_settings.h" | 28 #include "remoting/protocol/network_settings.h" |
| 29 #include "remoting/protocol/session_plugin.h" |
28 #include "remoting/protocol/transport.h" | 30 #include "remoting/protocol/transport.h" |
29 #include "remoting/protocol/transport_context.h" | 31 #include "remoting/protocol/transport_context.h" |
30 #include "remoting/signaling/fake_signal_strategy.h" | 32 #include "remoting/signaling/fake_signal_strategy.h" |
31 #include "testing/gmock/include/gmock/gmock.h" | 33 #include "testing/gmock/include/gmock/gmock.h" |
32 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
33 #include "third_party/webrtc/libjingle/xmpp/constants.h" | 35 #include "third_party/webrtc/libjingle/xmpp/constants.h" |
34 | 36 |
35 using testing::_; | 37 using testing::_; |
36 using testing::AtLeast; | 38 using testing::AtLeast; |
37 using testing::AtMost; | 39 using testing::AtMost; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 on_message_callback_.Run(); | 96 on_message_callback_.Run(); |
95 return true; | 97 return true; |
96 } | 98 } |
97 | 99 |
98 private: | 100 private: |
99 SendTransportInfoCallback send_transport_info_callback_; | 101 SendTransportInfoCallback send_transport_info_callback_; |
100 std::vector<std::unique_ptr<buzz::XmlElement>> received_messages_; | 102 std::vector<std::unique_ptr<buzz::XmlElement>> received_messages_; |
101 base::Closure on_message_callback_; | 103 base::Closure on_message_callback_; |
102 }; | 104 }; |
103 | 105 |
| 106 class FakePlugin : public SessionPlugin { |
| 107 public: |
| 108 std::unique_ptr<buzz::XmlElement> GetNextMessage() override { |
| 109 std::string tag_name = "test-tag-"; |
| 110 tag_name += base::IntToString(outgoing_messages_.size()); |
| 111 std::unique_ptr<buzz::XmlElement> new_message(new buzz::XmlElement( |
| 112 buzz::QName("test-namespace", tag_name))); |
| 113 outgoing_messages_.push_back(*new_message); |
| 114 return new_message; |
| 115 } |
| 116 |
| 117 void OnIncomingMessage(const buzz::XmlElement& attachments) override { |
| 118 for (const buzz::XmlElement* it = attachments.FirstElement(); |
| 119 it != nullptr; |
| 120 it = it->NextElement()) { |
| 121 incoming_messages_.push_back(*it); |
| 122 } |
| 123 } |
| 124 |
| 125 const std::vector<buzz::XmlElement>& outgoing_messages() const { |
| 126 return outgoing_messages_; |
| 127 } |
| 128 |
| 129 const std::vector<buzz::XmlElement>& incoming_messages() const { |
| 130 return incoming_messages_; |
| 131 } |
| 132 |
| 133 void Clear() { |
| 134 outgoing_messages_.clear(); |
| 135 incoming_messages_.clear(); |
| 136 } |
| 137 |
| 138 private: |
| 139 std::vector<buzz::XmlElement> outgoing_messages_; |
| 140 std::vector<buzz::XmlElement> incoming_messages_; |
| 141 }; |
| 142 |
104 std::unique_ptr<buzz::XmlElement> CreateTransportInfo(const std::string& id) { | 143 std::unique_ptr<buzz::XmlElement> CreateTransportInfo(const std::string& id) { |
105 std::unique_ptr<buzz::XmlElement> result( | 144 std::unique_ptr<buzz::XmlElement> result( |
106 buzz::XmlElement::ForStr("<transport xmlns='google:remoting:ice'/>")); | 145 buzz::XmlElement::ForStr("<transport xmlns='google:remoting:ice'/>")); |
107 result->AddAttr(buzz::QN_ID, id); | 146 result->AddAttr(buzz::QN_ID, id); |
108 return result; | 147 return result; |
109 } | 148 } |
110 | 149 |
111 } // namespace | 150 } // namespace |
112 | 151 |
113 class JingleSessionTest : public testing::Test { | 152 class JingleSessionTest : public testing::Test { |
114 public: | 153 public: |
115 JingleSessionTest() { | 154 JingleSessionTest() { |
116 message_loop_.reset(new base::MessageLoopForIO()); | 155 message_loop_.reset(new base::MessageLoopForIO()); |
117 network_settings_ = | 156 network_settings_ = |
118 NetworkSettings(NetworkSettings::NAT_TRAVERSAL_OUTGOING); | 157 NetworkSettings(NetworkSettings::NAT_TRAVERSAL_OUTGOING); |
119 } | 158 } |
120 | 159 |
121 // Helper method that handles OnIncomingSession(). | 160 // Helper method that handles OnIncomingSession(). |
122 void SetHostSession(Session* session) { | 161 void SetHostSession(Session* session) { |
123 DCHECK(session); | 162 DCHECK(session); |
124 host_session_.reset(session); | 163 host_session_.reset(session); |
125 host_session_->SetEventHandler(&host_session_event_handler_); | 164 host_session_->SetEventHandler(&host_session_event_handler_); |
126 host_session_->SetTransport(&host_transport_); | 165 host_session_->SetTransport(&host_transport_); |
| 166 host_session_->AddPlugin(&host_plugin_); |
127 } | 167 } |
128 | 168 |
129 void DeleteHostSession() { host_session_.reset(); } | 169 void DeleteHostSession() { host_session_.reset(); } |
130 | 170 |
131 void DeleteClientSession() { client_session_.reset(); } | 171 void DeleteClientSession() { client_session_.reset(); } |
132 | 172 |
133 protected: | 173 protected: |
134 void TearDown() override { | 174 void TearDown() override { |
135 CloseSessions(); | 175 CloseSessions(); |
136 CloseSessionManager(); | 176 CloseSessionManager(); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 OnSessionStateChange(Session::CLOSED)) | 270 OnSessionStateChange(Session::CLOSED)) |
231 .Times(AtMost(1)); | 271 .Times(AtMost(1)); |
232 } | 272 } |
233 } | 273 } |
234 | 274 |
235 void ConnectClient(std::unique_ptr<Authenticator> authenticator) { | 275 void ConnectClient(std::unique_ptr<Authenticator> authenticator) { |
236 client_session_ = | 276 client_session_ = |
237 client_server_->Connect(host_jid_, std::move(authenticator)); | 277 client_server_->Connect(host_jid_, std::move(authenticator)); |
238 client_session_->SetEventHandler(&client_session_event_handler_); | 278 client_session_->SetEventHandler(&client_session_event_handler_); |
239 client_session_->SetTransport(&client_transport_); | 279 client_session_->SetTransport(&client_transport_); |
| 280 client_session_->AddPlugin(&client_plugin_); |
240 base::RunLoop().RunUntilIdle(); | 281 base::RunLoop().RunUntilIdle(); |
241 } | 282 } |
242 | 283 |
243 void InitiateConnection(int auth_round_trips, | 284 void InitiateConnection(int auth_round_trips, |
244 FakeAuthenticator::Action auth_action, | 285 FakeAuthenticator::Action auth_action, |
245 bool expect_fail) { | 286 bool expect_fail) { |
246 SetHostExpectation(expect_fail); | 287 SetHostExpectation(expect_fail); |
247 SetClientExpectation(expect_fail); | 288 SetClientExpectation(expect_fail); |
248 ConnectClient(base::MakeUnique<FakeAuthenticator>( | 289 ConnectClient(base::MakeUnique<FakeAuthenticator>( |
249 FakeAuthenticator::CLIENT, auth_round_trips, auth_action, true)); | 290 FakeAuthenticator::CLIENT, auth_round_trips, auth_action, true)); |
250 } | 291 } |
251 | 292 |
252 void ExpectRouteChange(const std::string& channel_name) { | 293 void ExpectRouteChange(const std::string& channel_name) { |
253 EXPECT_CALL(host_session_event_handler_, | 294 EXPECT_CALL(host_session_event_handler_, |
254 OnSessionRouteChange(channel_name, _)) | 295 OnSessionRouteChange(channel_name, _)) |
255 .Times(AtLeast(1)); | 296 .Times(AtLeast(1)); |
256 EXPECT_CALL(client_session_event_handler_, | 297 EXPECT_CALL(client_session_event_handler_, |
257 OnSessionRouteChange(channel_name, _)) | 298 OnSessionRouteChange(channel_name, _)) |
258 .Times(AtLeast(1)); | 299 .Times(AtLeast(1)); |
259 } | 300 } |
260 | 301 |
| 302 void ExpectPluginMessagesEqual() const { |
| 303 ASSERT_EQ(client_plugin_.outgoing_messages().size(), |
| 304 host_plugin_.incoming_messages().size()); |
| 305 for (size_t i = 0; i < client_plugin_.outgoing_messages().size(); i++) { |
| 306 ASSERT_EQ(client_plugin_.outgoing_messages()[i].Str(), |
| 307 host_plugin_.incoming_messages()[i].Str()); |
| 308 } |
| 309 |
| 310 ASSERT_EQ(client_plugin_.incoming_messages().size(), |
| 311 host_plugin_.outgoing_messages().size()); |
| 312 for (size_t i = 0; i < client_plugin_.incoming_messages().size(); i++) { |
| 313 ASSERT_EQ(client_plugin_.incoming_messages()[i].Str(), |
| 314 host_plugin_.outgoing_messages()[i].Str()); |
| 315 } |
| 316 } |
| 317 |
261 std::unique_ptr<base::MessageLoopForIO> message_loop_; | 318 std::unique_ptr<base::MessageLoopForIO> message_loop_; |
262 | 319 |
263 NetworkSettings network_settings_; | 320 NetworkSettings network_settings_; |
264 | 321 |
265 std::unique_ptr<FakeSignalStrategy> host_signal_strategy_; | 322 std::unique_ptr<FakeSignalStrategy> host_signal_strategy_; |
266 std::unique_ptr<FakeSignalStrategy> client_signal_strategy_; | 323 std::unique_ptr<FakeSignalStrategy> client_signal_strategy_; |
267 | 324 |
268 std::string host_jid_ = kHostJid; | 325 std::string host_jid_ = kHostJid; |
269 | 326 |
270 std::unique_ptr<JingleSessionManager> host_server_; | 327 std::unique_ptr<JingleSessionManager> host_server_; |
271 MockSessionManagerListener host_server_listener_; | 328 MockSessionManagerListener host_server_listener_; |
272 std::unique_ptr<JingleSessionManager> client_server_; | 329 std::unique_ptr<JingleSessionManager> client_server_; |
273 | 330 |
274 std::unique_ptr<Session> host_session_; | 331 std::unique_ptr<Session> host_session_; |
275 MockSessionEventHandler host_session_event_handler_; | 332 MockSessionEventHandler host_session_event_handler_; |
276 FakeTransport host_transport_; | 333 FakeTransport host_transport_; |
277 std::unique_ptr<Session> client_session_; | 334 std::unique_ptr<Session> client_session_; |
278 MockSessionEventHandler client_session_event_handler_; | 335 MockSessionEventHandler client_session_event_handler_; |
279 FakeTransport client_transport_; | 336 FakeTransport client_transport_; |
| 337 |
| 338 FakePlugin host_plugin_; |
| 339 FakePlugin client_plugin_; |
280 }; | 340 }; |
281 | 341 |
282 | 342 |
283 // Verify that we can create and destroy session managers without a | 343 // Verify that we can create and destroy session managers without a |
284 // connection. | 344 // connection. |
285 TEST_F(JingleSessionTest, CreateAndDestoy) { | 345 TEST_F(JingleSessionTest, CreateAndDestoy) { |
286 CreateSessionManagers(1, FakeAuthenticator::ACCEPT); | 346 CreateSessionManagers(1, FakeAuthenticator::ACCEPT); |
287 } | 347 } |
288 | 348 |
289 // Verify that an incoming session can be rejected, and that the | 349 // Verify that an incoming session can be rejected, and that the |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
548 // Resume authentication. | 608 // Resume authentication. |
549 authenticator->Resume(); | 609 authenticator->Resume(); |
550 base::RunLoop().RunUntilIdle(); | 610 base::RunLoop().RunUntilIdle(); |
551 | 611 |
552 // Verify that transport-info that the first transport-info message was | 612 // Verify that transport-info that the first transport-info message was |
553 // received. | 613 // received. |
554 ASSERT_EQ(client_transport_.received_messages().size(), 1U); | 614 ASSERT_EQ(client_transport_.received_messages().size(), 1U); |
555 EXPECT_EQ("1", client_transport_.received_messages()[0]->Attr(buzz::QN_ID)); | 615 EXPECT_EQ("1", client_transport_.received_messages()[0]->Attr(buzz::QN_ID)); |
556 } | 616 } |
557 | 617 |
| 618 TEST_F(JingleSessionTest, TestSessionPlugin) { |
| 619 host_plugin_.Clear(); |
| 620 client_plugin_.Clear(); |
| 621 CreateSessionManagers(3, FakeAuthenticator::ACCEPT); |
| 622 ASSERT_NO_FATAL_FAILURE( |
| 623 InitiateConnection(3, FakeAuthenticator::ACCEPT, false)); |
| 624 ExpectPluginMessagesEqual(); |
| 625 } |
| 626 |
| 627 TEST_F(JingleSessionTest, SessionPluginShouldNotBeInvolvedInSessionTerminate) { |
| 628 host_plugin_.Clear(); |
| 629 client_plugin_.Clear(); |
| 630 CreateSessionManagers(1, FakeAuthenticator::REJECT); |
| 631 InitiateConnection(1, FakeAuthenticator::ACCEPT, true); |
| 632 // It's expected the client sends one more plugin message than host, the host |
| 633 // won't send plugin message in the SESSION_TERMINATE message. |
| 634 ASSERT_EQ(client_plugin_.outgoing_messages().size() - 1, |
| 635 client_plugin_.incoming_messages().size()); |
| 636 ExpectPluginMessagesEqual(); |
| 637 } |
| 638 |
| 639 TEST_F(JingleSessionTest, ImmediatelyCloseSessionAfterConnect) { |
| 640 CreateSessionManagers(3, FakeAuthenticator::ACCEPT); |
| 641 client_session_ = client_server_->Connect(host_jid_, |
| 642 base::MakeUnique<FakeAuthenticator>( |
| 643 FakeAuthenticator::CLIENT, 3, FakeAuthenticator::ACCEPT, true)); |
| 644 client_session_->Close(HOST_OVERLOAD); |
| 645 base::RunLoop().RunUntilIdle(); |
| 646 // We should only send a SESSION_TERMINATE message if the session has been |
| 647 // closed before SESSION_INITIATE message. |
| 648 ASSERT_EQ(1U, host_signal_strategy_->received_messages().size()); |
| 649 } |
| 650 |
558 } // namespace protocol | 651 } // namespace protocol |
559 } // namespace remoting | 652 } // namespace remoting |
OLD | NEW |