| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/signaling/xmpp_signal_strategy.h" | 5 #include "remoting/signaling/xmpp_signal_strategy.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/memory/ptr_util.h" |
| 10 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 12 #include "net/socket/socket_test_util.h" | 13 #include "net/socket/socket_test_util.h" |
| 13 #include "net/url_request/url_request_test_util.h" | 14 #include "net/url_request/url_request_test_util.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" | 16 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" |
| 16 | 17 |
| 17 namespace remoting { | 18 namespace remoting { |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 | 83 |
| 83 private: | 84 private: |
| 84 std::string written_data_; | 85 std::string written_data_; |
| 85 bool use_async_write_ = false; | 86 bool use_async_write_ = false; |
| 86 int pending_write_size_ = 0; | 87 int pending_write_size_ = 0; |
| 87 net::Error write_error_ = net::OK; | 88 net::Error write_error_ = net::OK; |
| 88 }; | 89 }; |
| 89 | 90 |
| 90 class MockClientSocketFactory : public net::MockClientSocketFactory { | 91 class MockClientSocketFactory : public net::MockClientSocketFactory { |
| 91 public: | 92 public: |
| 92 scoped_ptr<net::SSLClientSocket> CreateSSLClientSocket( | 93 std::unique_ptr<net::SSLClientSocket> CreateSSLClientSocket( |
| 93 scoped_ptr<net::ClientSocketHandle> transport_socket, | 94 std::unique_ptr<net::ClientSocketHandle> transport_socket, |
| 94 const net::HostPortPair& host_and_port, | 95 const net::HostPortPair& host_and_port, |
| 95 const net::SSLConfig& ssl_config, | 96 const net::SSLConfig& ssl_config, |
| 96 const net::SSLClientSocketContext& context) override { | 97 const net::SSLClientSocketContext& context) override { |
| 97 ssl_socket_created_ = true; | 98 ssl_socket_created_ = true; |
| 98 return net::MockClientSocketFactory::CreateSSLClientSocket( | 99 return net::MockClientSocketFactory::CreateSSLClientSocket( |
| 99 std::move(transport_socket), host_and_port, ssl_config, context); | 100 std::move(transport_socket), host_and_port, ssl_config, context); |
| 100 } | 101 } |
| 101 | 102 |
| 102 bool ssl_socket_created() const { return ssl_socket_created_; } | 103 bool ssl_socket_created() const { return ssl_socket_created_; } |
| 103 | 104 |
| 104 private: | 105 private: |
| 105 bool ssl_socket_created_ = false; | 106 bool ssl_socket_created_ = false; |
| 106 }; | 107 }; |
| 107 | 108 |
| 108 } // namespace | 109 } // namespace |
| 109 | 110 |
| 110 const char kTestUsername[] = "test_username@example.com"; | 111 const char kTestUsername[] = "test_username@example.com"; |
| 111 const char kTestAuthToken[] = "test_auth_token"; | 112 const char kTestAuthToken[] = "test_auth_token"; |
| 112 const int kDefaultPort = 443; | 113 const int kDefaultPort = 443; |
| 113 | 114 |
| 114 class XmppSignalStrategyTest : public testing::Test, | 115 class XmppSignalStrategyTest : public testing::Test, |
| 115 public SignalStrategy::Listener { | 116 public SignalStrategy::Listener { |
| 116 public: | 117 public: |
| 117 XmppSignalStrategyTest() : message_loop_(base::MessageLoop::TYPE_IO) {} | 118 XmppSignalStrategyTest() : message_loop_(base::MessageLoop::TYPE_IO) {} |
| 118 | 119 |
| 119 void SetUp() override { | 120 void SetUp() override { |
| 120 request_context_getter_ = new net::TestURLRequestContextGetter( | 121 request_context_getter_ = new net::TestURLRequestContextGetter( |
| 121 message_loop_.task_runner(), | 122 message_loop_.task_runner(), |
| 122 make_scoped_ptr(new net::TestURLRequestContext())); | 123 base::WrapUnique(new net::TestURLRequestContext())); |
| 123 } | 124 } |
| 124 | 125 |
| 125 void CreateSignalStrategy(int port) { | 126 void CreateSignalStrategy(int port) { |
| 126 XmppSignalStrategy::XmppServerConfig config; | 127 XmppSignalStrategy::XmppServerConfig config; |
| 127 config.host = "talk.google.com"; | 128 config.host = "talk.google.com"; |
| 128 config.port = port; | 129 config.port = port; |
| 129 config.username = kTestUsername; | 130 config.username = kTestUsername; |
| 130 config.auth_token = kTestAuthToken; | 131 config.auth_token = kTestAuthToken; |
| 131 signal_strategy_.reset(new XmppSignalStrategy( | 132 signal_strategy_.reset(new XmppSignalStrategy( |
| 132 &client_socket_factory_, request_context_getter_, config)); | 133 &client_socket_factory_, request_context_getter_, config)); |
| 133 signal_strategy_->AddListener(this); | 134 signal_strategy_->AddListener(this); |
| 134 } | 135 } |
| 135 | 136 |
| 136 void TearDown() override { | 137 void TearDown() override { |
| 137 signal_strategy_->RemoveListener(this); | 138 signal_strategy_->RemoveListener(this); |
| 138 signal_strategy_.reset(); | 139 signal_strategy_.reset(); |
| 139 base::RunLoop().RunUntilIdle(); | 140 base::RunLoop().RunUntilIdle(); |
| 140 } | 141 } |
| 141 | 142 |
| 142 void OnSignalStrategyStateChange(SignalStrategy::State state) override { | 143 void OnSignalStrategyStateChange(SignalStrategy::State state) override { |
| 143 state_history_.push_back(state); | 144 state_history_.push_back(state); |
| 144 } | 145 } |
| 145 | 146 |
| 146 bool OnSignalStrategyIncomingStanza(const buzz::XmlElement* stanza) override { | 147 bool OnSignalStrategyIncomingStanza(const buzz::XmlElement* stanza) override { |
| 147 received_messages_.push_back( | 148 received_messages_.push_back( |
| 148 make_scoped_ptr(new buzz::XmlElement(*stanza))); | 149 base::WrapUnique(new buzz::XmlElement(*stanza))); |
| 149 return true; | 150 return true; |
| 150 } | 151 } |
| 151 | 152 |
| 152 void Connect(bool success); | 153 void Connect(bool success); |
| 153 | 154 |
| 154 protected: | 155 protected: |
| 155 base::MessageLoop message_loop_; | 156 base::MessageLoop message_loop_; |
| 156 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | 157 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
| 157 MockClientSocketFactory client_socket_factory_; | 158 MockClientSocketFactory client_socket_factory_; |
| 158 scoped_ptr<XmppSocketDataProvider> socket_data_provider_; | 159 std::unique_ptr<XmppSocketDataProvider> socket_data_provider_; |
| 159 scoped_ptr<net::SSLSocketDataProvider> ssl_socket_data_provider_; | 160 std::unique_ptr<net::SSLSocketDataProvider> ssl_socket_data_provider_; |
| 160 scoped_ptr<XmppSignalStrategy> signal_strategy_; | 161 std::unique_ptr<XmppSignalStrategy> signal_strategy_; |
| 161 | 162 |
| 162 std::vector<SignalStrategy::State> state_history_; | 163 std::vector<SignalStrategy::State> state_history_; |
| 163 ScopedVector<buzz::XmlElement> received_messages_; | 164 ScopedVector<buzz::XmlElement> received_messages_; |
| 164 }; | 165 }; |
| 165 | 166 |
| 166 void XmppSignalStrategyTest::Connect(bool success) { | 167 void XmppSignalStrategyTest::Connect(bool success) { |
| 167 EXPECT_EQ(SignalStrategy::DISCONNECTED, signal_strategy_->GetState()); | 168 EXPECT_EQ(SignalStrategy::DISCONNECTED, signal_strategy_->GetState()); |
| 168 state_history_.clear(); | 169 state_history_.clear(); |
| 169 | 170 |
| 170 socket_data_provider_.reset(new XmppSocketDataProvider()); | 171 socket_data_provider_.reset(new XmppSocketDataProvider()); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 "<iq type=\"result\" id=\"1\"/>"); | 266 "<iq type=\"result\" id=\"1\"/>"); |
| 266 | 267 |
| 267 EXPECT_EQ(2U, state_history_.size()); | 268 EXPECT_EQ(2U, state_history_.size()); |
| 268 EXPECT_EQ(SignalStrategy::CONNECTED, state_history_[1]); | 269 EXPECT_EQ(SignalStrategy::CONNECTED, state_history_[1]); |
| 269 } | 270 } |
| 270 | 271 |
| 271 TEST_F(XmppSignalStrategyTest, SendAndReceive) { | 272 TEST_F(XmppSignalStrategyTest, SendAndReceive) { |
| 272 CreateSignalStrategy(kDefaultPort); | 273 CreateSignalStrategy(kDefaultPort); |
| 273 Connect(true); | 274 Connect(true); |
| 274 | 275 |
| 275 EXPECT_TRUE(signal_strategy_->SendStanza(make_scoped_ptr( | 276 EXPECT_TRUE(signal_strategy_->SendStanza(base::WrapUnique( |
| 276 new buzz::XmlElement(buzz::QName(std::string(), "hello"))))); | 277 new buzz::XmlElement(buzz::QName(std::string(), "hello"))))); |
| 277 EXPECT_EQ("<hello/>", socket_data_provider_->GetAndClearWrittenData()); | 278 EXPECT_EQ("<hello/>", socket_data_provider_->GetAndClearWrittenData()); |
| 278 | 279 |
| 279 socket_data_provider_->ReceiveData("<hi xmlns=\"hello\"/>"); | 280 socket_data_provider_->ReceiveData("<hi xmlns=\"hello\"/>"); |
| 280 EXPECT_EQ(1U, received_messages_.size()); | 281 EXPECT_EQ(1U, received_messages_.size()); |
| 281 EXPECT_EQ("<hi xmlns=\"hello\"/>", received_messages_[0]->Str()); | 282 EXPECT_EQ("<hi xmlns=\"hello\"/>", received_messages_[0]->Str()); |
| 282 } | 283 } |
| 283 | 284 |
| 284 TEST_F(XmppSignalStrategyTest, AuthError) { | 285 TEST_F(XmppSignalStrategyTest, AuthError) { |
| 285 CreateSignalStrategy(kDefaultPort); | 286 CreateSignalStrategy(kDefaultPort); |
| 286 Connect(false); | 287 Connect(false); |
| 287 } | 288 } |
| 288 | 289 |
| 289 TEST_F(XmppSignalStrategyTest, ConnectionClosed) { | 290 TEST_F(XmppSignalStrategyTest, ConnectionClosed) { |
| 290 CreateSignalStrategy(kDefaultPort); | 291 CreateSignalStrategy(kDefaultPort); |
| 291 Connect(true); | 292 Connect(true); |
| 292 | 293 |
| 293 socket_data_provider_->Close(); | 294 socket_data_provider_->Close(); |
| 294 | 295 |
| 295 EXPECT_EQ(3U, state_history_.size()); | 296 EXPECT_EQ(3U, state_history_.size()); |
| 296 EXPECT_EQ(SignalStrategy::DISCONNECTED, state_history_[2]); | 297 EXPECT_EQ(SignalStrategy::DISCONNECTED, state_history_[2]); |
| 297 EXPECT_EQ(SignalStrategy::DISCONNECTED, signal_strategy_->GetState()); | 298 EXPECT_EQ(SignalStrategy::DISCONNECTED, signal_strategy_->GetState()); |
| 298 EXPECT_EQ(SignalStrategy::OK, signal_strategy_->GetError()); | 299 EXPECT_EQ(SignalStrategy::OK, signal_strategy_->GetError()); |
| 299 | 300 |
| 300 // Can't send messages anymore. | 301 // Can't send messages anymore. |
| 301 EXPECT_FALSE(signal_strategy_->SendStanza(make_scoped_ptr( | 302 EXPECT_FALSE(signal_strategy_->SendStanza(base::WrapUnique( |
| 302 new buzz::XmlElement(buzz::QName(std::string(), "hello"))))); | 303 new buzz::XmlElement(buzz::QName(std::string(), "hello"))))); |
| 303 | 304 |
| 304 // Try connecting again. | 305 // Try connecting again. |
| 305 Connect(true); | 306 Connect(true); |
| 306 } | 307 } |
| 307 | 308 |
| 308 TEST_F(XmppSignalStrategyTest, NetworkReadError) { | 309 TEST_F(XmppSignalStrategyTest, NetworkReadError) { |
| 309 CreateSignalStrategy(kDefaultPort); | 310 CreateSignalStrategy(kDefaultPort); |
| 310 Connect(true); | 311 Connect(true); |
| 311 | 312 |
| 312 socket_data_provider_->SimulateAsyncReadError(); | 313 socket_data_provider_->SimulateAsyncReadError(); |
| 313 | 314 |
| 314 EXPECT_EQ(3U, state_history_.size()); | 315 EXPECT_EQ(3U, state_history_.size()); |
| 315 EXPECT_EQ(SignalStrategy::DISCONNECTED, state_history_[2]); | 316 EXPECT_EQ(SignalStrategy::DISCONNECTED, state_history_[2]); |
| 316 EXPECT_EQ(SignalStrategy::NETWORK_ERROR, signal_strategy_->GetError()); | 317 EXPECT_EQ(SignalStrategy::NETWORK_ERROR, signal_strategy_->GetError()); |
| 317 | 318 |
| 318 // Can't send messages anymore. | 319 // Can't send messages anymore. |
| 319 EXPECT_FALSE(signal_strategy_->SendStanza(make_scoped_ptr( | 320 EXPECT_FALSE(signal_strategy_->SendStanza(base::WrapUnique( |
| 320 new buzz::XmlElement(buzz::QName(std::string(), "hello"))))); | 321 new buzz::XmlElement(buzz::QName(std::string(), "hello"))))); |
| 321 | 322 |
| 322 // Try connecting again. | 323 // Try connecting again. |
| 323 Connect(true); | 324 Connect(true); |
| 324 } | 325 } |
| 325 | 326 |
| 326 TEST_F(XmppSignalStrategyTest, NetworkWriteError) { | 327 TEST_F(XmppSignalStrategyTest, NetworkWriteError) { |
| 327 CreateSignalStrategy(kDefaultPort); | 328 CreateSignalStrategy(kDefaultPort); |
| 328 Connect(true); | 329 Connect(true); |
| 329 | 330 |
| 330 socket_data_provider_->set_write_error(net::ERR_FAILED); | 331 socket_data_provider_->set_write_error(net::ERR_FAILED); |
| 331 | 332 |
| 332 // Next SendMessage() will call Write() which will fail. | 333 // Next SendMessage() will call Write() which will fail. |
| 333 EXPECT_FALSE(signal_strategy_->SendStanza(make_scoped_ptr( | 334 EXPECT_FALSE(signal_strategy_->SendStanza(base::WrapUnique( |
| 334 new buzz::XmlElement(buzz::QName(std::string(), "hello"))))); | 335 new buzz::XmlElement(buzz::QName(std::string(), "hello"))))); |
| 335 | 336 |
| 336 EXPECT_EQ(3U, state_history_.size()); | 337 EXPECT_EQ(3U, state_history_.size()); |
| 337 EXPECT_EQ(SignalStrategy::DISCONNECTED, state_history_[2]); | 338 EXPECT_EQ(SignalStrategy::DISCONNECTED, state_history_[2]); |
| 338 EXPECT_EQ(SignalStrategy::NETWORK_ERROR, signal_strategy_->GetError()); | 339 EXPECT_EQ(SignalStrategy::NETWORK_ERROR, signal_strategy_->GetError()); |
| 339 | 340 |
| 340 // Try connecting again. | 341 // Try connecting again. |
| 341 Connect(true); | 342 Connect(true); |
| 342 } | 343 } |
| 343 | 344 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 373 "<proceed xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>"); | 374 "<proceed xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>"); |
| 374 | 375 |
| 375 // Verify that SSL is connected only after write is finished. | 376 // Verify that SSL is connected only after write is finished. |
| 376 EXPECT_FALSE(client_socket_factory_.ssl_socket_created()); | 377 EXPECT_FALSE(client_socket_factory_.ssl_socket_created()); |
| 377 socket_data_provider_->CompletePendingWrite(); | 378 socket_data_provider_->CompletePendingWrite(); |
| 378 EXPECT_TRUE(client_socket_factory_.ssl_socket_created()); | 379 EXPECT_TRUE(client_socket_factory_.ssl_socket_created()); |
| 379 } | 380 } |
| 380 | 381 |
| 381 | 382 |
| 382 } // namespace remoting | 383 } // namespace remoting |
| OLD | NEW |