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_util.h" | 14 #include "base/strings/string_util.h" |
15 #include "base/test/test_timeouts.h" | 15 #include "base/test/test_timeouts.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
17 #include "net/socket/socket.h" | 17 #include "net/socket/socket.h" |
18 #include "net/socket/stream_socket.h" | 18 #include "net/socket/stream_socket.h" |
19 #include "net/url_request/url_request_context_getter.h" | 19 #include "net/url_request/url_request_context_getter.h" |
20 #include "remoting/base/constants.h" | 20 #include "remoting/base/constants.h" |
21 #include "remoting/protocol/authenticator.h" | 21 #include "remoting/protocol/authenticator.h" |
22 #include "remoting/protocol/channel_authenticator.h" | 22 #include "remoting/protocol/channel_authenticator.h" |
23 #include "remoting/protocol/chromium_port_allocator_factory.h" | 23 #include "remoting/protocol/chromium_port_allocator_factory.h" |
24 #include "remoting/protocol/connection_tester.h" | 24 #include "remoting/protocol/connection_tester.h" |
25 #include "remoting/protocol/fake_authenticator.h" | 25 #include "remoting/protocol/fake_authenticator.h" |
26 #include "remoting/protocol/jingle_session_manager.h" | 26 #include "remoting/protocol/jingle_session_manager.h" |
27 #include "remoting/protocol/network_settings.h" | 27 #include "remoting/protocol/network_settings.h" |
| 28 #include "remoting/protocol/session_plugin.h" |
28 #include "remoting/protocol/transport.h" | 29 #include "remoting/protocol/transport.h" |
29 #include "remoting/protocol/transport_context.h" | 30 #include "remoting/protocol/transport_context.h" |
30 #include "remoting/signaling/fake_signal_strategy.h" | 31 #include "remoting/signaling/fake_signal_strategy.h" |
31 #include "testing/gmock/include/gmock/gmock.h" | 32 #include "testing/gmock/include/gmock/gmock.h" |
32 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
33 #include "third_party/webrtc/libjingle/xmpp/constants.h" | 34 #include "third_party/webrtc/libjingle/xmpp/constants.h" |
34 | 35 |
35 using testing::_; | 36 using testing::_; |
36 using testing::AtLeast; | 37 using testing::AtLeast; |
37 using testing::AtMost; | 38 using testing::AtMost; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 on_message_callback_.Run(); | 95 on_message_callback_.Run(); |
95 return true; | 96 return true; |
96 } | 97 } |
97 | 98 |
98 private: | 99 private: |
99 SendTransportInfoCallback send_transport_info_callback_; | 100 SendTransportInfoCallback send_transport_info_callback_; |
100 std::vector<std::unique_ptr<buzz::XmlElement>> received_messages_; | 101 std::vector<std::unique_ptr<buzz::XmlElement>> received_messages_; |
101 base::Closure on_message_callback_; | 102 base::Closure on_message_callback_; |
102 }; | 103 }; |
103 | 104 |
| 105 class FakePlugin : public SessionPlugin { |
| 106 public: |
| 107 void OnSending(Session::State state, |
| 108 JingleMessage::ActionType action, |
| 109 std::unique_ptr<buzz::XmlElement>* attachments) override { |
| 110 on_sending_count_++; |
| 111 } |
| 112 |
| 113 void OnReceiving( |
| 114 Session::State state, |
| 115 JingleMessage::ActionType action, |
| 116 const std::unique_ptr<buzz::XmlElement>& attachments) override { |
| 117 on_receiving_count_++; |
| 118 } |
| 119 |
| 120 int on_sending_count() const { |
| 121 return on_sending_count_; |
| 122 } |
| 123 |
| 124 int on_receiving_count() const { |
| 125 return on_receiving_count_; |
| 126 } |
| 127 |
| 128 private: |
| 129 int on_sending_count_ = 0; |
| 130 int on_receiving_count_ = 0; |
| 131 }; |
| 132 |
104 std::unique_ptr<buzz::XmlElement> CreateTransportInfo(const std::string& id) { | 133 std::unique_ptr<buzz::XmlElement> CreateTransportInfo(const std::string& id) { |
105 std::unique_ptr<buzz::XmlElement> result( | 134 std::unique_ptr<buzz::XmlElement> result( |
106 buzz::XmlElement::ForStr("<transport xmlns='google:remoting:ice'/>")); | 135 buzz::XmlElement::ForStr("<transport xmlns='google:remoting:ice'/>")); |
107 result->AddAttr(buzz::QN_ID, id); | 136 result->AddAttr(buzz::QN_ID, id); |
108 return result; | 137 return result; |
109 } | 138 } |
110 | 139 |
111 } // namespace | 140 } // namespace |
112 | 141 |
113 class JingleSessionTest : public testing::Test { | 142 class JingleSessionTest : public testing::Test { |
114 public: | 143 public: |
115 JingleSessionTest() { | 144 JingleSessionTest() { |
116 message_loop_.reset(new base::MessageLoopForIO()); | 145 message_loop_.reset(new base::MessageLoopForIO()); |
117 network_settings_ = | 146 network_settings_ = |
118 NetworkSettings(NetworkSettings::NAT_TRAVERSAL_OUTGOING); | 147 NetworkSettings(NetworkSettings::NAT_TRAVERSAL_OUTGOING); |
119 } | 148 } |
120 | 149 |
121 // Helper method that handles OnIncomingSession(). | 150 // Helper method that handles OnIncomingSession(). |
122 void SetHostSession(Session* session) { | 151 void SetHostSession(Session* session) { |
123 DCHECK(session); | 152 DCHECK(session); |
124 host_session_.reset(session); | 153 host_session_.reset(session); |
125 host_session_->SetEventHandler(&host_session_event_handler_); | 154 host_session_->SetEventHandler(&host_session_event_handler_); |
126 host_session_->SetTransport(&host_transport_); | 155 host_session_->SetTransport(&host_transport_); |
| 156 std::unique_ptr<FakePlugin> plugin(new FakePlugin()); |
| 157 host_plugin_ = plugin.get(); |
| 158 host_session_->Attach(std::move(plugin)); |
127 } | 159 } |
128 | 160 |
129 void DeleteHostSession() { host_session_.reset(); } | 161 void DeleteHostSession() { host_session_.reset(); } |
130 | 162 |
131 void DeleteClientSession() { client_session_.reset(); } | 163 void DeleteClientSession() { client_session_.reset(); } |
132 | 164 |
133 protected: | 165 protected: |
134 void TearDown() override { | 166 void TearDown() override { |
135 CloseSessions(); | 167 CloseSessions(); |
136 CloseSessionManager(); | 168 CloseSessionManager(); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 OnSessionStateChange(Session::CLOSED)) | 262 OnSessionStateChange(Session::CLOSED)) |
231 .Times(AtMost(1)); | 263 .Times(AtMost(1)); |
232 } | 264 } |
233 } | 265 } |
234 | 266 |
235 void ConnectClient(std::unique_ptr<Authenticator> authenticator) { | 267 void ConnectClient(std::unique_ptr<Authenticator> authenticator) { |
236 client_session_ = | 268 client_session_ = |
237 client_server_->Connect(host_jid_, std::move(authenticator)); | 269 client_server_->Connect(host_jid_, std::move(authenticator)); |
238 client_session_->SetEventHandler(&client_session_event_handler_); | 270 client_session_->SetEventHandler(&client_session_event_handler_); |
239 client_session_->SetTransport(&client_transport_); | 271 client_session_->SetTransport(&client_transport_); |
| 272 std::unique_ptr<FakePlugin> plugin(new FakePlugin()); |
| 273 client_plugin_ = plugin.get(); |
| 274 client_session_->Attach(std::move(plugin)); |
240 base::RunLoop().RunUntilIdle(); | 275 base::RunLoop().RunUntilIdle(); |
241 } | 276 } |
242 | 277 |
243 void InitiateConnection(int auth_round_trips, | 278 void InitiateConnection(int auth_round_trips, |
244 FakeAuthenticator::Action auth_action, | 279 FakeAuthenticator::Action auth_action, |
245 bool expect_fail) { | 280 bool expect_fail) { |
246 SetHostExpectation(expect_fail); | 281 SetHostExpectation(expect_fail); |
247 SetClientExpectation(expect_fail); | 282 SetClientExpectation(expect_fail); |
248 ConnectClient(base::MakeUnique<FakeAuthenticator>( | 283 ConnectClient(base::MakeUnique<FakeAuthenticator>( |
249 FakeAuthenticator::CLIENT, auth_round_trips, auth_action, true)); | 284 FakeAuthenticator::CLIENT, auth_round_trips, auth_action, true)); |
(...skipping 20 matching lines...) Expand all Loading... |
270 std::unique_ptr<JingleSessionManager> host_server_; | 305 std::unique_ptr<JingleSessionManager> host_server_; |
271 MockSessionManagerListener host_server_listener_; | 306 MockSessionManagerListener host_server_listener_; |
272 std::unique_ptr<JingleSessionManager> client_server_; | 307 std::unique_ptr<JingleSessionManager> client_server_; |
273 | 308 |
274 std::unique_ptr<Session> host_session_; | 309 std::unique_ptr<Session> host_session_; |
275 MockSessionEventHandler host_session_event_handler_; | 310 MockSessionEventHandler host_session_event_handler_; |
276 FakeTransport host_transport_; | 311 FakeTransport host_transport_; |
277 std::unique_ptr<Session> client_session_; | 312 std::unique_ptr<Session> client_session_; |
278 MockSessionEventHandler client_session_event_handler_; | 313 MockSessionEventHandler client_session_event_handler_; |
279 FakeTransport client_transport_; | 314 FakeTransport client_transport_; |
| 315 |
| 316 FakePlugin* host_plugin_; |
| 317 FakePlugin* client_plugin_; |
280 }; | 318 }; |
281 | 319 |
282 | 320 |
283 // Verify that we can create and destroy session managers without a | 321 // Verify that we can create and destroy session managers without a |
284 // connection. | 322 // connection. |
285 TEST_F(JingleSessionTest, CreateAndDestoy) { | 323 TEST_F(JingleSessionTest, CreateAndDestoy) { |
286 CreateSessionManagers(1, FakeAuthenticator::ACCEPT); | 324 CreateSessionManagers(1, FakeAuthenticator::ACCEPT); |
287 } | 325 } |
288 | 326 |
289 // Verify that an incoming session can be rejected, and that the | 327 // 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. | 586 // Resume authentication. |
549 authenticator->Resume(); | 587 authenticator->Resume(); |
550 base::RunLoop().RunUntilIdle(); | 588 base::RunLoop().RunUntilIdle(); |
551 | 589 |
552 // Verify that transport-info that the first transport-info message was | 590 // Verify that transport-info that the first transport-info message was |
553 // received. | 591 // received. |
554 ASSERT_EQ(client_transport_.received_messages().size(), 1U); | 592 ASSERT_EQ(client_transport_.received_messages().size(), 1U); |
555 EXPECT_EQ("1", client_transport_.received_messages()[0]->Attr(buzz::QN_ID)); | 593 EXPECT_EQ("1", client_transport_.received_messages()[0]->Attr(buzz::QN_ID)); |
556 } | 594 } |
557 | 595 |
| 596 TEST_F(JingleSessionTest, TestSessionPlugin) { |
| 597 CreateSessionManagers(3, FakeAuthenticator::ACCEPT); |
| 598 ASSERT_NO_FATAL_FAILURE( |
| 599 InitiateConnection(3, FakeAuthenticator::ACCEPT, false)); |
| 600 // It's expected the plugins won't be able to be attached to the Session |
| 601 // before the first connect message has been sent. |
| 602 ASSERT_EQ(client_plugin_->on_sending_count(), 2); |
| 603 ASSERT_EQ(client_plugin_->on_receiving_count(), 3); |
| 604 ASSERT_EQ(host_plugin_->on_sending_count(), 3); |
| 605 ASSERT_EQ(host_plugin_->on_receiving_count(), 3); |
| 606 } |
| 607 |
558 } // namespace protocol | 608 } // namespace protocol |
559 } // namespace remoting | 609 } // namespace remoting |
OLD | NEW |