Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: remoting/protocol/jingle_session_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698