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> OnOutgoing( |
| 109 Session::State state, |
| 110 JingleMessage::ActionType action) override { |
| 111 std::string tag_name = "test-tag-"; |
| 112 tag_name += base::IntToString(outgoing_messages_.size()); |
| 113 std::unique_ptr<buzz::XmlElement> new_message(new buzz::XmlElement( |
| 114 buzz::QName("test-namespace", tag_name))); |
| 115 outgoing_messages_.push_back(*new_message); |
| 116 return new_message; |
| 117 } |
| 118 |
| 119 void OnIncoming(Session::State state, |
| 120 JingleMessage::ActionType action, |
| 121 const buzz::XmlElement* attachments) override { |
| 122 if (attachments) { |
| 123 for (const buzz::XmlElement* it = attachments->FirstElement(); |
| 124 it != nullptr; |
| 125 it = it->NextElement()) { |
| 126 incoming_messages_.push_back(*it); |
| 127 } |
| 128 } |
| 129 } |
| 130 |
| 131 const std::vector<buzz::XmlElement>& outgoing_messages() const { |
| 132 return outgoing_messages_; |
| 133 } |
| 134 |
| 135 const std::vector<buzz::XmlElement>& incoming_messages() const { |
| 136 return incoming_messages_; |
| 137 } |
| 138 |
| 139 private: |
| 140 std::vector<buzz::XmlElement> outgoing_messages_; |
| 141 std::vector<buzz::XmlElement> incoming_messages_; |
| 142 }; |
| 143 |
104 std::unique_ptr<buzz::XmlElement> CreateTransportInfo(const std::string& id) { | 144 std::unique_ptr<buzz::XmlElement> CreateTransportInfo(const std::string& id) { |
105 std::unique_ptr<buzz::XmlElement> result( | 145 std::unique_ptr<buzz::XmlElement> result( |
106 buzz::XmlElement::ForStr("<transport xmlns='google:remoting:ice'/>")); | 146 buzz::XmlElement::ForStr("<transport xmlns='google:remoting:ice'/>")); |
107 result->AddAttr(buzz::QN_ID, id); | 147 result->AddAttr(buzz::QN_ID, id); |
108 return result; | 148 return result; |
109 } | 149 } |
110 | 150 |
111 } // namespace | 151 } // namespace |
112 | 152 |
113 class JingleSessionTest : public testing::Test { | 153 class JingleSessionTest : public testing::Test { |
114 public: | 154 public: |
115 JingleSessionTest() { | 155 JingleSessionTest() { |
116 message_loop_.reset(new base::MessageLoopForIO()); | 156 message_loop_.reset(new base::MessageLoopForIO()); |
117 network_settings_ = | 157 network_settings_ = |
118 NetworkSettings(NetworkSettings::NAT_TRAVERSAL_OUTGOING); | 158 NetworkSettings(NetworkSettings::NAT_TRAVERSAL_OUTGOING); |
119 } | 159 } |
120 | 160 |
121 // Helper method that handles OnIncomingSession(). | 161 // Helper method that handles OnIncomingSession(). |
122 void SetHostSession(Session* session) { | 162 void SetHostSession(Session* session) { |
123 DCHECK(session); | 163 DCHECK(session); |
124 host_session_.reset(session); | 164 host_session_.reset(session); |
125 host_session_->SetEventHandler(&host_session_event_handler_); | 165 host_session_->SetEventHandler(&host_session_event_handler_); |
126 host_session_->SetTransport(&host_transport_); | 166 host_session_->SetTransport(&host_transport_); |
| 167 std::unique_ptr<FakePlugin> plugin(new FakePlugin()); |
| 168 host_plugin_ = plugin.get(); |
| 169 host_session_->AddPlugin(std::move(plugin)); |
127 } | 170 } |
128 | 171 |
129 void DeleteHostSession() { host_session_.reset(); } | 172 void DeleteHostSession() { host_session_.reset(); } |
130 | 173 |
131 void DeleteClientSession() { client_session_.reset(); } | 174 void DeleteClientSession() { client_session_.reset(); } |
132 | 175 |
133 protected: | 176 protected: |
134 void TearDown() override { | 177 void TearDown() override { |
135 CloseSessions(); | 178 CloseSessions(); |
136 CloseSessionManager(); | 179 CloseSessionManager(); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 OnSessionStateChange(Session::CLOSED)) | 273 OnSessionStateChange(Session::CLOSED)) |
231 .Times(AtMost(1)); | 274 .Times(AtMost(1)); |
232 } | 275 } |
233 } | 276 } |
234 | 277 |
235 void ConnectClient(std::unique_ptr<Authenticator> authenticator) { | 278 void ConnectClient(std::unique_ptr<Authenticator> authenticator) { |
236 client_session_ = | 279 client_session_ = |
237 client_server_->Connect(host_jid_, std::move(authenticator)); | 280 client_server_->Connect(host_jid_, std::move(authenticator)); |
238 client_session_->SetEventHandler(&client_session_event_handler_); | 281 client_session_->SetEventHandler(&client_session_event_handler_); |
239 client_session_->SetTransport(&client_transport_); | 282 client_session_->SetTransport(&client_transport_); |
| 283 std::unique_ptr<FakePlugin> plugin(new FakePlugin()); |
| 284 client_plugin_ = plugin.get(); |
| 285 client_session_->AddPlugin(std::move(plugin)); |
240 base::RunLoop().RunUntilIdle(); | 286 base::RunLoop().RunUntilIdle(); |
241 } | 287 } |
242 | 288 |
243 void InitiateConnection(int auth_round_trips, | 289 void InitiateConnection(int auth_round_trips, |
244 FakeAuthenticator::Action auth_action, | 290 FakeAuthenticator::Action auth_action, |
245 bool expect_fail) { | 291 bool expect_fail) { |
246 SetHostExpectation(expect_fail); | 292 SetHostExpectation(expect_fail); |
247 SetClientExpectation(expect_fail); | 293 SetClientExpectation(expect_fail); |
248 ConnectClient(base::MakeUnique<FakeAuthenticator>( | 294 ConnectClient(base::MakeUnique<FakeAuthenticator>( |
249 FakeAuthenticator::CLIENT, auth_round_trips, auth_action, true)); | 295 FakeAuthenticator::CLIENT, auth_round_trips, auth_action, true)); |
(...skipping 20 matching lines...) Expand all Loading... |
270 std::unique_ptr<JingleSessionManager> host_server_; | 316 std::unique_ptr<JingleSessionManager> host_server_; |
271 MockSessionManagerListener host_server_listener_; | 317 MockSessionManagerListener host_server_listener_; |
272 std::unique_ptr<JingleSessionManager> client_server_; | 318 std::unique_ptr<JingleSessionManager> client_server_; |
273 | 319 |
274 std::unique_ptr<Session> host_session_; | 320 std::unique_ptr<Session> host_session_; |
275 MockSessionEventHandler host_session_event_handler_; | 321 MockSessionEventHandler host_session_event_handler_; |
276 FakeTransport host_transport_; | 322 FakeTransport host_transport_; |
277 std::unique_ptr<Session> client_session_; | 323 std::unique_ptr<Session> client_session_; |
278 MockSessionEventHandler client_session_event_handler_; | 324 MockSessionEventHandler client_session_event_handler_; |
279 FakeTransport client_transport_; | 325 FakeTransport client_transport_; |
| 326 |
| 327 FakePlugin* host_plugin_; |
| 328 FakePlugin* client_plugin_; |
280 }; | 329 }; |
281 | 330 |
282 | 331 |
283 // Verify that we can create and destroy session managers without a | 332 // Verify that we can create and destroy session managers without a |
284 // connection. | 333 // connection. |
285 TEST_F(JingleSessionTest, CreateAndDestoy) { | 334 TEST_F(JingleSessionTest, CreateAndDestoy) { |
286 CreateSessionManagers(1, FakeAuthenticator::ACCEPT); | 335 CreateSessionManagers(1, FakeAuthenticator::ACCEPT); |
287 } | 336 } |
288 | 337 |
289 // Verify that an incoming session can be rejected, and that the | 338 // 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. | 597 // Resume authentication. |
549 authenticator->Resume(); | 598 authenticator->Resume(); |
550 base::RunLoop().RunUntilIdle(); | 599 base::RunLoop().RunUntilIdle(); |
551 | 600 |
552 // Verify that transport-info that the first transport-info message was | 601 // Verify that transport-info that the first transport-info message was |
553 // received. | 602 // received. |
554 ASSERT_EQ(client_transport_.received_messages().size(), 1U); | 603 ASSERT_EQ(client_transport_.received_messages().size(), 1U); |
555 EXPECT_EQ("1", client_transport_.received_messages()[0]->Attr(buzz::QN_ID)); | 604 EXPECT_EQ("1", client_transport_.received_messages()[0]->Attr(buzz::QN_ID)); |
556 } | 605 } |
557 | 606 |
| 607 TEST_F(JingleSessionTest, TestSessionPlugin) { |
| 608 CreateSessionManagers(3, FakeAuthenticator::ACCEPT); |
| 609 ASSERT_NO_FATAL_FAILURE( |
| 610 InitiateConnection(3, FakeAuthenticator::ACCEPT, false)); |
| 611 ASSERT_EQ(client_plugin_->outgoing_messages().size(), |
| 612 host_plugin_->incoming_messages().size()); |
| 613 for (size_t i = 0; i < client_plugin_->outgoing_messages().size(); i++) { |
| 614 ASSERT_EQ(client_plugin_->outgoing_messages()[i].Str(), |
| 615 host_plugin_->incoming_messages()[i].Str()); |
| 616 } |
| 617 |
| 618 ASSERT_EQ(client_plugin_->incoming_messages().size(), |
| 619 host_plugin_->outgoing_messages().size()); |
| 620 for (size_t i = 0; i < client_plugin_->incoming_messages().size(); i++) { |
| 621 ASSERT_EQ(client_plugin_->incoming_messages()[i].Str(), |
| 622 host_plugin_->outgoing_messages()[i].Str()); |
| 623 } |
| 624 } |
| 625 |
558 } // namespace protocol | 626 } // namespace protocol |
559 } // namespace remoting | 627 } // namespace remoting |
OLD | NEW |