jingle_session_unittest.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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
  3. // found in the LICENSE file.
  4. #include "remoting/protocol/jingle_session.h"
  5. #include <memory>
  6. #include <utility>
  7. #include <vector>
  8. #include "base/bind.h"
  9. #include "base/memory/ptr_util.h"
  10. #include "base/run_loop.h"
  11. #include "base/strings/string_number_conversions.h"
  12. #include "base/strings/string_util.h"
  13. #include "base/test/task_environment.h"
  14. #include "base/test/test_timeouts.h"
  15. #include "base/time/time.h"
  16. #include "net/socket/socket.h"
  17. #include "net/socket/stream_socket.h"
  18. #include "net/url_request/url_request_context_getter.h"
  19. #include "remoting/base/constants.h"
  20. #include "remoting/protocol/authenticator.h"
  21. #include "remoting/protocol/channel_authenticator.h"
  22. #include "remoting/protocol/chromium_port_allocator_factory.h"
  23. #include "remoting/protocol/connection_tester.h"
  24. #include "remoting/protocol/fake_authenticator.h"
  25. #include "remoting/protocol/jingle_session_manager.h"
  26. #include "remoting/protocol/network_settings.h"
  27. #include "remoting/protocol/session_plugin.h"
  28. #include "remoting/protocol/transport.h"
  29. #include "remoting/protocol/transport_context.h"
  30. #include "remoting/signaling/fake_signal_strategy.h"
  31. #include "remoting/signaling/xmpp_constants.h"
  32. #include "testing/gmock/include/gmock/gmock.h"
  33. #include "testing/gtest/include/gtest/gtest.h"
  34. using testing::_;
  35. using testing::AtLeast;
  36. using testing::AtMost;
  37. using testing::DeleteArg;
  38. using testing::DoAll;
  39. using testing::InSequence;
  40. using testing::Invoke;
  41. using testing::InvokeWithoutArgs;
  42. using testing::Return;
  43. using testing::SaveArg;
  44. using testing::SetArgPointee;
  45. using testing::WithArg;
  46. namespace remoting {
  47. namespace protocol {
  48. namespace {
  49. const char kHostJid[] = "Host@gmail.com/123";
  50. const char kClientJid[] = "Client@gmail.com/321";
  51. // kHostJid the way it would be stored in the directory.
  52. const char kNormalizedHostJid[] = "host@gmail.com/123";
  53. class MockSessionManagerListener {
  54. public:
  55. MOCK_METHOD2(OnIncomingSession,
  56. void(Session*,
  57. SessionManager::IncomingSessionResponse*));
  58. };
  59. class MockSessionEventHandler : public Session::EventHandler {
  60. public:
  61. MOCK_METHOD1(OnSessionStateChange, void(Session::State));
  62. MOCK_METHOD2(OnSessionRouteChange, void(const std::string& channel_name,
  63. const TransportRoute& route));
  64. };
  65. class FakeTransport : public Transport {
  66. public:
  67. SendTransportInfoCallback send_transport_info_callback() {
  68. return send_transport_info_callback_;
  69. }
  70. const std::vector<std::unique_ptr<jingle_xmpp::XmlElement>>& received_messages() {
  71. return received_messages_;
  72. }
  73. void set_on_message_callback(
  74. const base::RepeatingClosure& on_message_callback) {
  75. on_message_callback_ = on_message_callback;
  76. }
  77. // Transport interface.
  78. void Start(Authenticator* authenticator,
  79. SendTransportInfoCallback send_transport_info_callback) override {
  80. send_transport_info_callback_ = send_transport_info_callback;
  81. }
  82. bool ProcessTransportInfo(jingle_xmpp::XmlElement* transport_info) override {
  83. received_messages_.push_back(
  84. std::make_unique<jingle_xmpp::XmlElement>(*transport_info));
  85. if (on_message_callback_)
  86. on_message_callback_.Run();
  87. return true;
  88. }
  89. private:
  90. SendTransportInfoCallback send_transport_info_callback_;
  91. std::vector<std::unique_ptr<jingle_xmpp::XmlElement>> received_messages_;
  92. base::RepeatingClosure on_message_callback_;
  93. };
  94. class FakePlugin : public SessionPlugin {
  95. public:
  96. std::unique_ptr<jingle_xmpp::XmlElement> GetNextMessage() override {
  97. std::string tag_name = "test-tag-";
  98. tag_name += base::NumberToString(outgoing_messages_.size());
  99. std::unique_ptr<jingle_xmpp::XmlElement> new_message(new jingle_xmpp::XmlElement(
  100. jingle_xmpp::QName("test-namespace", tag_name)));
  101. outgoing_messages_.push_back(*new_message);
  102. return new_message;
  103. }
  104. void OnIncomingMessage(const jingle_xmpp::XmlElement& attachments) override {
  105. for (const jingle_xmpp::XmlElement* it = attachments.FirstElement();
  106. it != nullptr;
  107. it = it->NextElement()) {
  108. incoming_messages_.push_back(*it);
  109. }
  110. }
  111. const std::vector<jingle_xmpp::XmlElement>& outgoing_messages() const {
  112. return outgoing_messages_;
  113. }
  114. const std::vector<jingle_xmpp::XmlElement>& incoming_messages() const {
  115. return incoming_messages_;
  116. }
  117. void Clear() {
  118. outgoing_messages_.clear();
  119. incoming_messages_.clear();
  120. }
  121. private:
  122. std::vector<jingle_xmpp::XmlElement> outgoing_messages_;
  123. std::vector<jingle_xmpp::XmlElement> incoming_messages_;
  124. };
  125. std::unique_ptr<jingle_xmpp::XmlElement> CreateTransportInfo(const std::string& id) {
  126. std::unique_ptr<jingle_xmpp::XmlElement> result(
  127. jingle_xmpp::XmlElement::ForStr("<transport xmlns='google:remoting:ice'/>"));
  128. result->AddAttr(kQNameId, id);
  129. return result;
  130. }
  131. } // namespace
  132. class JingleSessionTest : public testing::Test {
  133. public:
  134. JingleSessionTest() {
  135. network_settings_ =
  136. NetworkSettings(NetworkSettings::NAT_TRAVERSAL_OUTGOING);
  137. }
  138. // Helper method that handles OnIncomingSession().
  139. void SetHostSession(Session* session) {
  140. DCHECK(session);
  141. host_session_.reset(session);
  142. host_session_->SetEventHandler(&host_session_event_handler_);
  143. host_session_->SetTransport(&host_transport_);
  144. host_session_->AddPlugin(&host_plugin_);
  145. }
  146. void DeleteHostSession() { host_session_.reset(); }
  147. void DeleteClientSession() { client_session_.reset(); }
  148. protected:
  149. void TearDown() override {
  150. CloseSessions();
  151. CloseSessionManager();
  152. base::RunLoop().RunUntilIdle();
  153. }
  154. void CloseSessions() {
  155. host_session_.reset();
  156. client_session_.reset();
  157. }
  158. void CreateSessionManagers(FakeAuthenticator::Config auth_config,
  159. int messages_till_start) {
  160. host_signal_strategy_ =
  161. std::make_unique<FakeSignalStrategy>(SignalingAddress(kHostJid));
  162. client_signal_strategy_ =
  163. std::make_unique<FakeSignalStrategy>(SignalingAddress(kClientJid));
  164. FakeSignalStrategy::Connect(host_signal_strategy_.get(),
  165. client_signal_strategy_.get());
  166. host_server_ =
  167. std::make_unique<JingleSessionManager>(host_signal_strategy_.get());
  168. host_server_->AcceptIncoming(
  169. base::BindRepeating(&MockSessionManagerListener::OnIncomingSession,
  170. base::Unretained(&host_server_listener_)));
  171. std::unique_ptr<AuthenticatorFactory> factory(
  172. new FakeHostAuthenticatorFactory(messages_till_start, auth_config));
  173. host_server_->set_authenticator_factory(std::move(factory));
  174. client_server_ =
  175. std::make_unique<JingleSessionManager>(client_signal_strategy_.get());
  176. }
  177. void CreateSessionManagers(FakeAuthenticator::Config auth_config) {
  178. CreateSessionManagers(auth_config, 0);
  179. }
  180. void CloseSessionManager() {
  181. host_server_.reset();
  182. client_server_.reset();
  183. host_signal_strategy_.reset();
  184. client_signal_strategy_.reset();
  185. }
  186. void SetHostExpectation(bool expect_fail) {
  187. EXPECT_CALL(host_server_listener_, OnIncomingSession(_, _))
  188. .WillOnce(
  189. DoAll(WithArg<0>(Invoke(this, &JingleSessionTest::SetHostSession)),
  190. SetArgPointee<1>(protocol::SessionManager::ACCEPT)));
  191. {
  192. InSequence dummy;
  193. EXPECT_CALL(host_session_event_handler_,
  194. OnSessionStateChange(Session::ACCEPTED))
  195. .Times(AtMost(1));
  196. EXPECT_CALL(host_session_event_handler_,
  197. OnSessionStateChange(Session::AUTHENTICATING))
  198. .Times(AtMost(1));
  199. if (expect_fail) {
  200. EXPECT_CALL(host_session_event_handler_,
  201. OnSessionStateChange(Session::FAILED))
  202. .Times(1);
  203. } else {
  204. EXPECT_CALL(host_session_event_handler_,
  205. OnSessionStateChange(Session::AUTHENTICATED))
  206. .Times(1);
  207. // Expect that the connection will be closed eventually.
  208. EXPECT_CALL(host_session_event_handler_,
  209. OnSessionStateChange(Session::CLOSED))
  210. .Times(AtMost(1));
  211. }
  212. }
  213. }
  214. void SetClientExpectation(bool expect_fail) {
  215. InSequence dummy;
  216. EXPECT_CALL(client_session_event_handler_,
  217. OnSessionStateChange(Session::ACCEPTED))
  218. .Times(AtMost(1));
  219. EXPECT_CALL(client_session_event_handler_,
  220. OnSessionStateChange(Session::AUTHENTICATING))
  221. .Times(AtMost(1));
  222. if (expect_fail) {
  223. EXPECT_CALL(client_session_event_handler_,
  224. OnSessionStateChange(Session::FAILED))
  225. .Times(1);
  226. } else {
  227. EXPECT_CALL(client_session_event_handler_,
  228. OnSessionStateChange(Session::AUTHENTICATED))
  229. .Times(1);
  230. // Expect that the connection will be closed eventually.
  231. EXPECT_CALL(client_session_event_handler_,
  232. OnSessionStateChange(Session::CLOSED))
  233. .Times(AtMost(1));
  234. }
  235. }
  236. void ConnectClient(std::unique_ptr<Authenticator> authenticator) {
  237. client_session_ = client_server_->Connect(
  238. SignalingAddress(kNormalizedHostJid), std::move(authenticator));
  239. client_session_->SetEventHandler(&client_session_event_handler_);
  240. client_session_->SetTransport(&client_transport_);
  241. client_session_->AddPlugin(&client_plugin_);
  242. base::RunLoop().RunUntilIdle();
  243. }
  244. void ConnectClient(FakeAuthenticator::Config auth_config) {
  245. ConnectClient(std::make_unique<FakeAuthenticator>(
  246. FakeAuthenticator::CLIENT, auth_config,
  247. client_signal_strategy_->GetLocalAddress().id(), kNormalizedHostJid));
  248. }
  249. void InitiateConnection(FakeAuthenticator::Config auth_config,
  250. bool expect_fail) {
  251. SetHostExpectation(expect_fail);
  252. SetClientExpectation(expect_fail);
  253. ConnectClient(auth_config);
  254. }
  255. void ExpectRouteChange(const std::string& channel_name) {
  256. EXPECT_CALL(host_session_event_handler_,
  257. OnSessionRouteChange(channel_name, _))
  258. .Times(AtLeast(1));
  259. EXPECT_CALL(client_session_event_handler_,
  260. OnSessionRouteChange(channel_name, _))
  261. .Times(AtLeast(1));
  262. }
  263. void ExpectPluginMessagesEqual() const {
  264. ASSERT_EQ(client_plugin_.outgoing_messages().size(),
  265. host_plugin_.incoming_messages().size());
  266. for (size_t i = 0; i < client_plugin_.outgoing_messages().size(); i++) {
  267. ASSERT_EQ(client_plugin_.outgoing_messages()[i].Str(),
  268. host_plugin_.incoming_messages()[i].Str());
  269. }
  270. ASSERT_EQ(client_plugin_.incoming_messages().size(),
  271. host_plugin_.outgoing_messages().size());
  272. for (size_t i = 0; i < client_plugin_.incoming_messages().size(); i++) {
  273. ASSERT_EQ(client_plugin_.incoming_messages()[i].Str(),
  274. host_plugin_.outgoing_messages()[i].Str());
  275. }
  276. }
  277. base::test::SingleThreadTaskEnvironment task_environment_{
  278. base::test::SingleThreadTaskEnvironment::MainThreadType::IO};
  279. NetworkSettings network_settings_;
  280. std::unique_ptr<FakeSignalStrategy> host_signal_strategy_;
  281. std::unique_ptr<FakeSignalStrategy> client_signal_strategy_;
  282. std::unique_ptr<JingleSessionManager> host_server_;
  283. MockSessionManagerListener host_server_listener_;
  284. std::unique_ptr<JingleSessionManager> client_server_;
  285. std::unique_ptr<Session> host_session_;
  286. MockSessionEventHandler host_session_event_handler_;
  287. FakeTransport host_transport_;
  288. std::unique_ptr<Session> client_session_;
  289. MockSessionEventHandler client_session_event_handler_;
  290. FakeTransport client_transport_;
  291. FakePlugin host_plugin_;
  292. FakePlugin client_plugin_;
  293. };
  294. // Verify that we can create and destroy session managers without a
  295. // connection.
  296. TEST_F(JingleSessionTest, CreateAndDestoy) {
  297. CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT));
  298. }
  299. // Verify that an incoming session can be rejected, and that the
  300. // status of the connection is set to FAILED in this case.
  301. TEST_F(JingleSessionTest, RejectConnection) {
  302. CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT));
  303. // Reject incoming session.
  304. EXPECT_CALL(host_server_listener_, OnIncomingSession(_, _))
  305. .WillOnce(SetArgPointee<1>(protocol::SessionManager::DECLINE));
  306. {
  307. InSequence dummy;
  308. EXPECT_CALL(client_session_event_handler_,
  309. OnSessionStateChange(Session::FAILED))
  310. .Times(1);
  311. }
  312. ConnectClient(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT));
  313. client_session_->SetEventHandler(&client_session_event_handler_);
  314. base::RunLoop().RunUntilIdle();
  315. }
  316. // Verify that we can connect two endpoints with single-step authentication.
  317. TEST_F(JingleSessionTest, Connect) {
  318. CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT));
  319. InitiateConnection(FakeAuthenticator::Config(), false);
  320. // Verify that the client specified correct initiator value.
  321. ASSERT_GT(host_signal_strategy_->received_messages().size(), 0U);
  322. const jingle_xmpp::XmlElement* initiate_xml =
  323. host_signal_strategy_->received_messages().front().get();
  324. const jingle_xmpp::XmlElement* jingle_element =
  325. initiate_xml->FirstNamed(jingle_xmpp::QName("urn:xmpp:jingle:1", "jingle"));
  326. ASSERT_TRUE(jingle_element);
  327. ASSERT_EQ(client_signal_strategy_->GetLocalAddress().id(),
  328. jingle_element->Attr(jingle_xmpp::QName(std::string(), "initiator")));
  329. }
  330. // Verify that we can connect two endpoints with multi-step authentication.
  331. TEST_F(JingleSessionTest, ConnectWithMultistep) {
  332. const int kAuthRoundtrips = 3;
  333. FakeAuthenticator::Config auth_config(kAuthRoundtrips,
  334. FakeAuthenticator::ACCEPT, true);
  335. CreateSessionManagers(auth_config);
  336. InitiateConnection(auth_config, false);
  337. }
  338. TEST_F(JingleSessionTest, ConnectWithOutOfOrderIqs) {
  339. CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT));
  340. InitiateConnection(FakeAuthenticator::Config(), false);
  341. client_signal_strategy_->SimulateMessageReordering();
  342. // Verify that out of order transport messages are received correctly.
  343. host_transport_.send_transport_info_callback().Run(CreateTransportInfo("1"));
  344. host_transport_.send_transport_info_callback().Run(CreateTransportInfo("2"));
  345. base::RunLoop().RunUntilIdle();
  346. ASSERT_EQ(client_transport_.received_messages().size(), 2U);
  347. EXPECT_EQ("1", client_transport_.received_messages()[0]->Attr(kQNameId));
  348. EXPECT_EQ("2", client_transport_.received_messages()[1]->Attr(kQNameId));
  349. }
  350. // Verify that out-of-order messages are handled correctly when the session is
  351. // torn down after the first message.
  352. TEST_F(JingleSessionTest, ConnectWithOutOfOrderIqsDestroyOnFirstMessage) {
  353. CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT));
  354. InitiateConnection(FakeAuthenticator::Config(), false);
  355. client_signal_strategy_->SimulateMessageReordering();
  356. // Verify that out of order transport messages are received correctly.
  357. host_transport_.send_transport_info_callback().Run(CreateTransportInfo("1"));
  358. host_transport_.send_transport_info_callback().Run(CreateTransportInfo("2"));
  359. // Destroy the session as soon as the first message is received.
  360. client_transport_.set_on_message_callback(base::BindRepeating(
  361. &JingleSessionTest::DeleteClientSession, base::Unretained(this)));
  362. base::RunLoop().RunUntilIdle();
  363. ASSERT_EQ(client_transport_.received_messages().size(), 1U);
  364. EXPECT_EQ("1", client_transport_.received_messages()[0]->Attr(kQNameId));
  365. }
  366. // Verify that connection is terminated when single-step auth fails.
  367. TEST_F(JingleSessionTest, ConnectWithBadAuth) {
  368. CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::REJECT));
  369. InitiateConnection(FakeAuthenticator::Config(), true);
  370. }
  371. // Verify that connection is terminated when multi-step auth fails.
  372. TEST_F(JingleSessionTest, ConnectWithBadMultistepAuth) {
  373. const int kAuthRoundtrips = 3;
  374. CreateSessionManagers(FakeAuthenticator::Config(
  375. kAuthRoundtrips, FakeAuthenticator::REJECT, false));
  376. InitiateConnection(FakeAuthenticator::Config(
  377. kAuthRoundtrips, FakeAuthenticator::ACCEPT, false),
  378. true);
  379. }
  380. // Verify that incompatible protocol configuration is handled properly.
  381. TEST_F(JingleSessionTest, TestIncompatibleProtocol) {
  382. CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT));
  383. EXPECT_CALL(host_server_listener_, OnIncomingSession(_, _)).Times(0);
  384. EXPECT_CALL(client_session_event_handler_,
  385. OnSessionStateChange(Session::FAILED))
  386. .Times(1);
  387. std::unique_ptr<CandidateSessionConfig> config =
  388. CandidateSessionConfig::CreateDefault();
  389. // Disable all video codecs so the host will reject connection.
  390. config->mutable_video_configs()->clear();
  391. client_server_->set_protocol_config(std::move(config));
  392. ConnectClient(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT));
  393. EXPECT_EQ(INCOMPATIBLE_PROTOCOL, client_session_->error());
  394. EXPECT_FALSE(host_session_);
  395. }
  396. // Verify that GICE-only client is rejected with an appropriate error code.
  397. TEST_F(JingleSessionTest, TestLegacyIceConnection) {
  398. CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT));
  399. EXPECT_CALL(host_server_listener_, OnIncomingSession(_, _)).Times(0);
  400. EXPECT_CALL(client_session_event_handler_,
  401. OnSessionStateChange(Session::FAILED))
  402. .Times(1);
  403. std::unique_ptr<CandidateSessionConfig> config =
  404. CandidateSessionConfig::CreateDefault();
  405. config->set_ice_supported(false);
  406. client_server_->set_protocol_config(std::move(config));
  407. ConnectClient(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT));
  408. EXPECT_EQ(INCOMPATIBLE_PROTOCOL, client_session_->error());
  409. EXPECT_FALSE(host_session_);
  410. }
  411. TEST_F(JingleSessionTest, DeleteSessionOnIncomingConnection) {
  412. const int kAuthRoundtrips = 3;
  413. FakeAuthenticator::Config auth_config(kAuthRoundtrips,
  414. FakeAuthenticator::ACCEPT, true);
  415. CreateSessionManagers(auth_config);
  416. EXPECT_CALL(host_server_listener_, OnIncomingSession(_, _))
  417. .WillOnce(
  418. DoAll(WithArg<0>(Invoke(this, &JingleSessionTest::SetHostSession)),
  419. SetArgPointee<1>(protocol::SessionManager::ACCEPT)));
  420. EXPECT_CALL(host_session_event_handler_,
  421. OnSessionStateChange(Session::ACCEPTED))
  422. .Times(AtMost(1));
  423. EXPECT_CALL(host_session_event_handler_,
  424. OnSessionStateChange(Session::AUTHENTICATING))
  425. .WillOnce(InvokeWithoutArgs(this, &JingleSessionTest::DeleteHostSession));
  426. ConnectClient(auth_config);
  427. }
  428. TEST_F(JingleSessionTest, DeleteSessionOnAuth) {
  429. // Same as the previous test, but set messages_till_started to 2 in
  430. // CreateSessionManagers so that the session will goes into the
  431. // AUTHENTICATING state after two message exchanges.
  432. const int kMessagesTillStarted = 2;
  433. const int kAuthRoundtrips = 3;
  434. FakeAuthenticator::Config auth_config(kAuthRoundtrips,
  435. FakeAuthenticator::ACCEPT, true);
  436. CreateSessionManagers(auth_config, kMessagesTillStarted);
  437. EXPECT_CALL(host_server_listener_, OnIncomingSession(_, _))
  438. .WillOnce(
  439. DoAll(WithArg<0>(Invoke(this, &JingleSessionTest::SetHostSession)),
  440. SetArgPointee<1>(protocol::SessionManager::ACCEPT)));
  441. EXPECT_CALL(host_session_event_handler_,
  442. OnSessionStateChange(Session::ACCEPTED))
  443. .Times(AtMost(1));
  444. EXPECT_CALL(host_session_event_handler_,
  445. OnSessionStateChange(Session::AUTHENTICATING))
  446. .WillOnce(InvokeWithoutArgs(this, &JingleSessionTest::DeleteHostSession));
  447. ConnectClient(auth_config);
  448. }
  449. // Verify that incoming transport-info messages are handled correctly while in
  450. // AUTHENTICATING state.
  451. TEST_F(JingleSessionTest, TransportInfoDuringAuthentication) {
  452. const int kAuthRoundtrips = 2;
  453. FakeAuthenticator::Config auth_config(kAuthRoundtrips,
  454. FakeAuthenticator::ACCEPT, true);
  455. CreateSessionManagers(auth_config);
  456. SetHostExpectation(false);
  457. {
  458. InSequence dummy;
  459. EXPECT_CALL(client_session_event_handler_,
  460. OnSessionStateChange(Session::ACCEPTED))
  461. .Times(AtMost(1));
  462. EXPECT_CALL(client_session_event_handler_,
  463. OnSessionStateChange(Session::AUTHENTICATING))
  464. .Times(AtMost(1));
  465. }
  466. // Create connection and pause it before authentication is finished.
  467. FakeAuthenticator* authenticator = new FakeAuthenticator(
  468. FakeAuthenticator::CLIENT, auth_config,
  469. client_signal_strategy_->GetLocalAddress().id(), kNormalizedHostJid);
  470. authenticator->set_pause_message_index(4);
  471. ConnectClient(base::WrapUnique(authenticator));
  472. // Send 2 transport messages.
  473. host_transport_.send_transport_info_callback().Run(CreateTransportInfo("1"));
  474. host_transport_.send_transport_info_callback().Run(CreateTransportInfo("2"));
  475. base::RunLoop().RunUntilIdle();
  476. // The transport-info messages should not be received here because
  477. // authentication hasn't finished.
  478. EXPECT_TRUE(client_transport_.received_messages().empty());
  479. // Destroy the session as soon as the first message is received.
  480. client_transport_.set_on_message_callback(base::BindRepeating(
  481. &JingleSessionTest::DeleteClientSession, base::Unretained(this)));
  482. // Resume authentication.
  483. authenticator->Resume();
  484. base::RunLoop().RunUntilIdle();
  485. // Verify that transport-info that the first transport-info message was
  486. // received.
  487. ASSERT_EQ(client_transport_.received_messages().size(), 1U);
  488. EXPECT_EQ("1", client_transport_.received_messages()[0]->Attr(kQNameId));
  489. }
  490. TEST_F(JingleSessionTest, TestSessionPlugin) {
  491. host_plugin_.Clear();
  492. client_plugin_.Clear();
  493. const int kAuthRoundtrips = 3;
  494. FakeAuthenticator::Config auth_config(kAuthRoundtrips,
  495. FakeAuthenticator::ACCEPT, true);
  496. CreateSessionManagers(auth_config);
  497. ASSERT_NO_FATAL_FAILURE(InitiateConnection(auth_config, false));
  498. ExpectPluginMessagesEqual();
  499. }
  500. TEST_F(JingleSessionTest, SessionPluginShouldNotBeInvolvedInSessionTerminate) {
  501. host_plugin_.Clear();
  502. client_plugin_.Clear();
  503. CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::REJECT));
  504. InitiateConnection(FakeAuthenticator::Config(), true);
  505. // It's expected the client sends one more plugin message than host, the host
  506. // won't send plugin message in the SESSION_TERMINATE message.
  507. ASSERT_EQ(client_plugin_.outgoing_messages().size() - 1,
  508. client_plugin_.incoming_messages().size());
  509. ExpectPluginMessagesEqual();
  510. }
  511. TEST_F(JingleSessionTest, ImmediatelyCloseSessionAfterConnect) {
  512. const int kAuthRoundtrips = 3;
  513. FakeAuthenticator::Config auth_config(kAuthRoundtrips,
  514. FakeAuthenticator::ACCEPT, true);
  515. CreateSessionManagers(auth_config);
  516. client_session_ = client_server_->Connect(
  517. SignalingAddress(kNormalizedHostJid),
  518. std::make_unique<FakeAuthenticator>(
  519. FakeAuthenticator::CLIENT, auth_config,
  520. client_signal_strategy_->GetLocalAddress().id(), kNormalizedHostJid));
  521. client_session_->Close(HOST_OVERLOAD);
  522. base::RunLoop().RunUntilIdle();
  523. // We should only send a SESSION_TERMINATE message if the session has been
  524. // closed before SESSION_INITIATE message.
  525. ASSERT_EQ(1U, host_signal_strategy_->received_messages().size());
  526. }
  527. } // namespace protocol
  528. } // namespace remoting