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

Side by Side Diff: remoting/signaling/xmpp_signal_strategy_unittest.cc

Issue 1258323003: Fix XmppSignalStrategy to return correct result from SendStanza(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@move_config
Patch Set: Created 5 years, 4 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
« no previous file with comments | « remoting/signaling/xmpp_signal_strategy.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "base/base64.h" 7 #include "base/base64.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "net/socket/socket_test_util.h" 10 #include "net/socket/socket_test_util.h"
11 #include "net/url_request/url_request_test_util.h" 11 #include "net/url_request/url_request_test_util.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" 13 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
14 14
15 namespace remoting { 15 namespace remoting {
16 16
17 namespace { 17 namespace {
18 18
19 class XmppSocketDataProvider: public net::SocketDataProvider { 19 class XmppSocketDataProvider : public net::SocketDataProvider {
20 public: 20 public:
21 net::MockRead OnRead() override { 21 net::MockRead OnRead() override {
22 return net::MockRead(net::ASYNC, net::ERR_IO_PENDING); 22 return net::MockRead(net::ASYNC, net::ERR_IO_PENDING);
23 } 23 }
24 24
25 net::MockWriteResult OnWrite(const std::string& data) override { 25 net::MockWriteResult OnWrite(const std::string& data) override {
26 if (write_error_ != net::OK)
27 return net::MockWriteResult(net::SYNCHRONOUS, write_error_);
28
26 written_data_.append(data); 29 written_data_.append(data);
27 30
28 if (use_async_write_) { 31 if (use_async_write_) {
29 pending_write_size_ = data.size(); 32 pending_write_size_ = data.size();
30 return net::MockWriteResult(net::ASYNC, net::ERR_IO_PENDING); 33 return net::MockWriteResult(net::ASYNC, net::ERR_IO_PENDING);
31 } 34 }
32 35
33 return net::MockWriteResult(net::SYNCHRONOUS, data.size()); 36 return net::MockWriteResult(net::SYNCHRONOUS, data.size());
34 } 37 }
35 38
36 void Reset() override {} 39 void Reset() override {}
37 40
38 bool AllReadDataConsumed() const override { 41 bool AllReadDataConsumed() const override {
39 return true; 42 return true;
40 } 43 }
41 44
42 bool AllWriteDataConsumed() const override { 45 bool AllWriteDataConsumed() const override {
43 return true; 46 return true;
44 } 47 }
45 48
46 void ReceiveData(const std::string& text) { 49 void ReceiveData(const std::string& text) {
47 socket()->OnReadComplete( 50 socket()->OnReadComplete(
48 net::MockRead(net::ASYNC, text.data(), text.size())); 51 net::MockRead(net::ASYNC, text.data(), text.size()));
49 } 52 }
50 53
51 void Close() { 54 void Close() {
52 ReceiveData(std::string()); 55 ReceiveData(std::string());
53 } 56 }
54 57
55 void SimulateNetworkError() { 58 void SimulateAsyncReadError() {
56 socket()->OnReadComplete( 59 socket()->OnReadComplete(
57 net::MockRead(net::ASYNC, net::ERR_CONNECTION_RESET)); 60 net::MockRead(net::ASYNC, net::ERR_CONNECTION_RESET));
58 } 61 }
59 62
60 std::string GetAndClearWrittenData() { 63 std::string GetAndClearWrittenData() {
61 std::string data; 64 std::string data;
62 data.swap(written_data_); 65 data.swap(written_data_);
63 return data; 66 return data;
64 } 67 }
65 68
66 void set_use_async_write(bool use_async_write) { 69 void set_use_async_write(bool use_async_write) {
67 use_async_write_ = use_async_write; 70 use_async_write_ = use_async_write;
68 } 71 }
69 72
73 void set_write_error(net::Error error) {
74 write_error_ = error;
75 }
76
70 void CompletePendingWrite() { 77 void CompletePendingWrite() {
71 socket()->OnWriteComplete(pending_write_size_); 78 socket()->OnWriteComplete(pending_write_size_);
72 } 79 }
73 80
74 private: 81 private:
75 std::string written_data_; 82 std::string written_data_;
76 bool use_async_write_ = false; 83 bool use_async_write_ = false;
77 int pending_write_size_ = 0; 84 int pending_write_size_ = 0;
85 net::Error write_error_ = net::OK;
78 }; 86 };
79 87
80 class MockClientSocketFactory : public net::MockClientSocketFactory { 88 class MockClientSocketFactory : public net::MockClientSocketFactory {
81 public: 89 public:
82 scoped_ptr<net::SSLClientSocket> CreateSSLClientSocket( 90 scoped_ptr<net::SSLClientSocket> CreateSSLClientSocket(
83 scoped_ptr<net::ClientSocketHandle> transport_socket, 91 scoped_ptr<net::ClientSocketHandle> transport_socket,
84 const net::HostPortPair& host_and_port, 92 const net::HostPortPair& host_and_port,
85 const net::SSLConfig& ssl_config, 93 const net::SSLConfig& ssl_config,
86 const net::SSLClientSocketContext& context) override { 94 const net::SSLClientSocketContext& context) override {
87 ssl_socket_created_ = true; 95 ssl_socket_created_ = true;
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 EXPECT_EQ(SignalStrategy::OK, signal_strategy_->GetError()); 297 EXPECT_EQ(SignalStrategy::OK, signal_strategy_->GetError());
290 298
291 // Can't send messages anymore. 299 // Can't send messages anymore.
292 EXPECT_FALSE(signal_strategy_->SendStanza(make_scoped_ptr( 300 EXPECT_FALSE(signal_strategy_->SendStanza(make_scoped_ptr(
293 new buzz::XmlElement(buzz::QName(std::string(), "hello"))))); 301 new buzz::XmlElement(buzz::QName(std::string(), "hello")))));
294 302
295 // Try connecting again. 303 // Try connecting again.
296 Connect(true); 304 Connect(true);
297 } 305 }
298 306
299 TEST_F(XmppSignalStrategyTest, NetworkError) { 307 TEST_F(XmppSignalStrategyTest, NetworkReadError) {
300 CreateSignalStrategy(kDefaultPort); 308 CreateSignalStrategy(kDefaultPort);
301 Connect(true); 309 Connect(true);
302 310
303 socket_data_provider_->SimulateNetworkError(); 311 socket_data_provider_->SimulateAsyncReadError();
304 312
305 EXPECT_EQ(3U, state_history_.size()); 313 EXPECT_EQ(3U, state_history_.size());
306 EXPECT_EQ(SignalStrategy::DISCONNECTED, state_history_[2]); 314 EXPECT_EQ(SignalStrategy::DISCONNECTED, state_history_[2]);
307 EXPECT_EQ(SignalStrategy::NETWORK_ERROR, signal_strategy_->GetError()); 315 EXPECT_EQ(SignalStrategy::NETWORK_ERROR, signal_strategy_->GetError());
308 316
309 // Can't send messages anymore. 317 // Can't send messages anymore.
310 EXPECT_FALSE(signal_strategy_->SendStanza(make_scoped_ptr( 318 EXPECT_FALSE(signal_strategy_->SendStanza(make_scoped_ptr(
311 new buzz::XmlElement(buzz::QName(std::string(), "hello"))))); 319 new buzz::XmlElement(buzz::QName(std::string(), "hello")))));
312 320
313 // Try connecting again. 321 // Try connecting again.
314 Connect(true); 322 Connect(true);
315 } 323 }
316 324
325 TEST_F(XmppSignalStrategyTest, NetworkWriteError) {
326 CreateSignalStrategy(kDefaultPort);
327 Connect(true);
328
329 socket_data_provider_->set_write_error(net::ERR_FAILED);
330
331 // Next SendMessage() will call Write() which will fail.
332 EXPECT_FALSE(signal_strategy_->SendStanza(make_scoped_ptr(
333 new buzz::XmlElement(buzz::QName(std::string(), "hello")))));
334
335 EXPECT_EQ(3U, state_history_.size());
336 EXPECT_EQ(SignalStrategy::DISCONNECTED, state_history_[2]);
337 EXPECT_EQ(SignalStrategy::NETWORK_ERROR, signal_strategy_->GetError());
338
339 // Try connecting again.
340 Connect(true);
341 }
342
317 TEST_F(XmppSignalStrategyTest, StartTlsWithPendingWrite) { 343 TEST_F(XmppSignalStrategyTest, StartTlsWithPendingWrite) {
318 // Use port 5222 so that XmppLoginHandler uses starttls/proceed handshake 344 // Use port 5222 so that XmppLoginHandler uses starttls/proceed handshake
319 // before starting TLS. 345 // before starting TLS.
320 CreateSignalStrategy(5222); 346 CreateSignalStrategy(5222);
321 347
322 socket_data_provider_.reset(new XmppSocketDataProvider()); 348 socket_data_provider_.reset(new XmppSocketDataProvider());
323 socket_data_provider_->set_connect_data( 349 socket_data_provider_->set_connect_data(
324 net::MockConnect(net::SYNCHRONOUS, net::OK)); 350 net::MockConnect(net::SYNCHRONOUS, net::OK));
325 client_socket_factory_.AddSocketDataProvider(socket_data_provider_.get()); 351 client_socket_factory_.AddSocketDataProvider(socket_data_provider_.get());
326 352
(...skipping 19 matching lines...) Expand all
346 "<proceed xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>"); 372 "<proceed xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>");
347 373
348 // Verify that SSL is connected only after write is finished. 374 // Verify that SSL is connected only after write is finished.
349 EXPECT_FALSE(client_socket_factory_.ssl_socket_created()); 375 EXPECT_FALSE(client_socket_factory_.ssl_socket_created());
350 socket_data_provider_->CompletePendingWrite(); 376 socket_data_provider_->CompletePendingWrite();
351 EXPECT_TRUE(client_socket_factory_.ssl_socket_created()); 377 EXPECT_TRUE(client_socket_factory_.ssl_socket_created());
352 } 378 }
353 379
354 380
355 } // namespace remoting 381 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/signaling/xmpp_signal_strategy.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698