| 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/fake_session.h" | 5 #include "remoting/protocol/fake_session.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "net/base/address_list.h" | 9 #include "net/base/address_list.h" |
| 10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
| 11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 12 #include "net/base/net_util.h" | 12 #include "net/base/net_util.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 namespace remoting { | 15 namespace remoting { |
| 16 namespace protocol { | 16 namespace protocol { |
| 17 | 17 |
| 18 const char kTestJid[] = "host1@gmail.com/chromoting123"; | 18 const char kTestJid[] = "host1@gmail.com/chromoting123"; |
| 19 | 19 |
| 20 FakeSocket::FakeSocket() | 20 FakeSocket::FakeSocket() |
| 21 : next_read_error_(net::OK), | 21 : async_write_(false), |
| 22 write_pending_(false), |
| 23 write_limit_(0), |
| 24 next_write_error_(net::OK), |
| 25 next_read_error_(net::OK), |
| 22 read_pending_(false), | 26 read_pending_(false), |
| 23 read_buffer_size_(0), | 27 read_buffer_size_(0), |
| 24 input_pos_(0), | 28 input_pos_(0), |
| 25 message_loop_(MessageLoop::current()), | 29 message_loop_(MessageLoop::current()), |
| 26 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 30 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 27 } | 31 } |
| 28 | 32 |
| 29 FakeSocket::~FakeSocket() { | 33 FakeSocket::~FakeSocket() { |
| 30 EXPECT_EQ(message_loop_, MessageLoop::current()); | 34 EXPECT_EQ(message_loop_, MessageLoop::current()); |
| 31 } | 35 } |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 read_buffer_ = buf; | 78 read_buffer_ = buf; |
| 75 read_buffer_size_ = buf_len; | 79 read_buffer_size_ = buf_len; |
| 76 read_callback_ = callback; | 80 read_callback_ = callback; |
| 77 return net::ERR_IO_PENDING; | 81 return net::ERR_IO_PENDING; |
| 78 } | 82 } |
| 79 } | 83 } |
| 80 | 84 |
| 81 int FakeSocket::Write(net::IOBuffer* buf, int buf_len, | 85 int FakeSocket::Write(net::IOBuffer* buf, int buf_len, |
| 82 const net::CompletionCallback& callback) { | 86 const net::CompletionCallback& callback) { |
| 83 EXPECT_EQ(message_loop_, MessageLoop::current()); | 87 EXPECT_EQ(message_loop_, MessageLoop::current()); |
| 88 EXPECT_FALSE(write_pending_); |
| 89 |
| 90 if (write_limit_ > 0) |
| 91 buf_len = std::min(write_limit_, buf_len); |
| 92 |
| 93 if (async_write_) { |
| 94 message_loop_->PostTask(FROM_HERE, base::Bind( |
| 95 &FakeSocket::DoAsyncWrite, weak_factory_.GetWeakPtr(), |
| 96 scoped_refptr<net::IOBuffer>(buf), buf_len, callback)); |
| 97 write_pending_ = true; |
| 98 return net::ERR_IO_PENDING; |
| 99 } else { |
| 100 if (next_write_error_ != net::OK) { |
| 101 int r = next_write_error_; |
| 102 next_write_error_ = net::OK; |
| 103 return r; |
| 104 } |
| 105 |
| 106 DoWrite(buf, buf_len); |
| 107 return buf_len; |
| 108 } |
| 109 } |
| 110 |
| 111 void FakeSocket::DoAsyncWrite(scoped_refptr<net::IOBuffer> buf, int buf_len, |
| 112 const net::CompletionCallback& callback) { |
| 113 write_pending_ = false; |
| 114 |
| 115 if (next_write_error_ != net::OK) { |
| 116 int r = next_write_error_; |
| 117 next_write_error_ = net::OK; |
| 118 callback.Run(r); |
| 119 return; |
| 120 } |
| 121 |
| 122 DoWrite(buf, buf_len); |
| 123 callback.Run(buf_len); |
| 124 } |
| 125 |
| 126 void FakeSocket::DoWrite(net::IOBuffer* buf, int buf_len) { |
| 84 written_data_.insert(written_data_.end(), | 127 written_data_.insert(written_data_.end(), |
| 85 buf->data(), buf->data() + buf_len); | 128 buf->data(), buf->data() + buf_len); |
| 86 | 129 |
| 87 if (peer_socket_) { | 130 if (peer_socket_) { |
| 88 message_loop_->PostTask(FROM_HERE, base::Bind( | 131 message_loop_->PostTask(FROM_HERE, base::Bind( |
| 89 &FakeSocket::AppendInputData, peer_socket_, | 132 &FakeSocket::AppendInputData, peer_socket_, |
| 90 std::vector<char>(buf->data(), buf->data() + buf_len))); | 133 std::vector<char>(buf->data(), buf->data() + buf_len))); |
| 91 } | 134 } |
| 92 | |
| 93 return buf_len; | |
| 94 } | 135 } |
| 95 | 136 |
| 96 bool FakeSocket::SetReceiveBufferSize(int32 size) { | 137 bool FakeSocket::SetReceiveBufferSize(int32 size) { |
| 97 NOTIMPLEMENTED(); | 138 NOTIMPLEMENTED(); |
| 98 return false; | 139 return false; |
| 99 } | 140 } |
| 100 bool FakeSocket::SetSendBufferSize(int32 size) { | 141 bool FakeSocket::SetSendBufferSize(int32 size) { |
| 101 NOTIMPLEMENTED(); | 142 NOTIMPLEMENTED(); |
| 102 return false; | 143 return false; |
| 103 } | 144 } |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 void FakeSession::set_config(const SessionConfig& config) { | 340 void FakeSession::set_config(const SessionConfig& config) { |
| 300 config_ = config; | 341 config_ = config; |
| 301 } | 342 } |
| 302 | 343 |
| 303 void FakeSession::Close() { | 344 void FakeSession::Close() { |
| 304 closed_ = true; | 345 closed_ = true; |
| 305 } | 346 } |
| 306 | 347 |
| 307 } // namespace protocol | 348 } // namespace protocol |
| 308 } // namespace remoting | 349 } // namespace remoting |
| OLD | NEW |