OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "net/base/address_list.h" |
8 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
9 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 12 #include "net/base/net_util.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
11 | 14 |
12 namespace remoting { | 15 namespace remoting { |
13 namespace protocol { | 16 namespace protocol { |
14 | 17 |
15 const char kTestJid[] = "host1@gmail.com/chromoting123"; | 18 const char kTestJid[] = "host1@gmail.com/chromoting123"; |
16 | 19 |
17 FakeSocket::FakeSocket() | 20 FakeSocket::FakeSocket() |
18 : read_pending_(false), | 21 : read_pending_(false), |
| 22 read_buffer_size_(0), |
19 input_pos_(0), | 23 input_pos_(0), |
20 message_loop_(MessageLoop::current()) { | 24 message_loop_(MessageLoop::current()), |
| 25 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
21 } | 26 } |
22 | 27 |
23 FakeSocket::~FakeSocket() { | 28 FakeSocket::~FakeSocket() { |
24 EXPECT_EQ(message_loop_, MessageLoop::current()); | 29 EXPECT_EQ(message_loop_, MessageLoop::current()); |
25 } | 30 } |
26 | 31 |
27 void FakeSocket::AppendInputData(const char* data, int data_size) { | 32 void FakeSocket::AppendInputData(const std::vector<char>& data) { |
28 EXPECT_EQ(message_loop_, MessageLoop::current()); | 33 EXPECT_EQ(message_loop_, MessageLoop::current()); |
29 input_data_.insert(input_data_.end(), data, data + data_size); | 34 input_data_.insert(input_data_.end(), data.begin(), data.end()); |
30 // Complete pending read if any. | 35 // Complete pending read if any. |
31 if (read_pending_) { | 36 if (read_pending_) { |
32 read_pending_ = false; | 37 read_pending_ = false; |
33 int result = std::min(read_buffer_size_, | 38 int result = std::min(read_buffer_size_, |
34 static_cast<int>(input_data_.size() - input_pos_)); | 39 static_cast<int>(input_data_.size() - input_pos_)); |
35 CHECK(result > 0); | 40 CHECK(result > 0); |
36 memcpy(read_buffer_->data(), | 41 memcpy(read_buffer_->data(), |
37 &(*input_data_.begin()) + input_pos_, result); | 42 &(*input_data_.begin()) + input_pos_, result); |
38 input_pos_ += result; | 43 input_pos_ += result; |
| 44 read_buffer_ = NULL; |
39 read_callback_.Run(result); | 45 read_callback_.Run(result); |
40 read_buffer_ = NULL; | |
41 } | 46 } |
42 } | 47 } |
43 | 48 |
| 49 void FakeSocket::PairWith(FakeSocket* peer_socket) { |
| 50 EXPECT_EQ(message_loop_, MessageLoop::current()); |
| 51 peer_socket_ = peer_socket->weak_factory_.GetWeakPtr(); |
| 52 peer_socket->peer_socket_ = weak_factory_.GetWeakPtr(); |
| 53 } |
| 54 |
44 int FakeSocket::Read(net::IOBuffer* buf, int buf_len, | 55 int FakeSocket::Read(net::IOBuffer* buf, int buf_len, |
45 const net::CompletionCallback& callback) { | 56 const net::CompletionCallback& callback) { |
46 EXPECT_EQ(message_loop_, MessageLoop::current()); | 57 EXPECT_EQ(message_loop_, MessageLoop::current()); |
47 if (input_pos_ < static_cast<int>(input_data_.size())) { | 58 if (input_pos_ < static_cast<int>(input_data_.size())) { |
48 int result = std::min(buf_len, | 59 int result = std::min(buf_len, |
49 static_cast<int>(input_data_.size()) - input_pos_); | 60 static_cast<int>(input_data_.size()) - input_pos_); |
50 memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result); | 61 memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result); |
51 input_pos_ += result; | 62 input_pos_ += result; |
52 return result; | 63 return result; |
53 } else { | 64 } else { |
54 read_pending_ = true; | 65 read_pending_ = true; |
55 read_buffer_ = buf; | 66 read_buffer_ = buf; |
56 read_buffer_size_ = buf_len; | 67 read_buffer_size_ = buf_len; |
57 read_callback_ = callback; | 68 read_callback_ = callback; |
58 return net::ERR_IO_PENDING; | 69 return net::ERR_IO_PENDING; |
59 } | 70 } |
60 } | 71 } |
61 | 72 |
62 int FakeSocket::Write(net::IOBuffer* buf, int buf_len, | 73 int FakeSocket::Write(net::IOBuffer* buf, int buf_len, |
63 const net::CompletionCallback& callback) { | 74 const net::CompletionCallback& callback) { |
64 EXPECT_EQ(message_loop_, MessageLoop::current()); | 75 EXPECT_EQ(message_loop_, MessageLoop::current()); |
65 written_data_.insert(written_data_.end(), | 76 written_data_.insert(written_data_.end(), |
66 buf->data(), buf->data() + buf_len); | 77 buf->data(), buf->data() + buf_len); |
| 78 |
| 79 if (peer_socket_) { |
| 80 message_loop_->PostTask(FROM_HERE, base::Bind( |
| 81 &FakeSocket::AppendInputData, peer_socket_, |
| 82 std::vector<char>(buf->data(), buf->data() + buf_len))); |
| 83 } |
| 84 |
67 return buf_len; | 85 return buf_len; |
68 } | 86 } |
69 | 87 |
70 bool FakeSocket::SetReceiveBufferSize(int32 size) { | 88 bool FakeSocket::SetReceiveBufferSize(int32 size) { |
71 NOTIMPLEMENTED(); | 89 NOTIMPLEMENTED(); |
72 return false; | 90 return false; |
73 } | 91 } |
74 bool FakeSocket::SetSendBufferSize(int32 size) { | 92 bool FakeSocket::SetSendBufferSize(int32 size) { |
75 NOTIMPLEMENTED(); | 93 NOTIMPLEMENTED(); |
76 return false; | 94 return false; |
77 } | 95 } |
78 | 96 |
79 int FakeSocket::Connect(const net::CompletionCallback& callback) { | 97 int FakeSocket::Connect(const net::CompletionCallback& callback) { |
80 EXPECT_EQ(message_loop_, MessageLoop::current()); | 98 EXPECT_EQ(message_loop_, MessageLoop::current()); |
81 return net::OK; | 99 return net::OK; |
82 } | 100 } |
83 | 101 |
84 void FakeSocket::Disconnect() { | 102 void FakeSocket::Disconnect() { |
85 NOTIMPLEMENTED(); | 103 peer_socket_.reset(); |
86 } | 104 } |
87 | 105 |
88 bool FakeSocket::IsConnected() const { | 106 bool FakeSocket::IsConnected() const { |
89 EXPECT_EQ(message_loop_, MessageLoop::current()); | 107 EXPECT_EQ(message_loop_, MessageLoop::current()); |
90 return true; | 108 return true; |
91 } | 109 } |
92 | 110 |
93 bool FakeSocket::IsConnectedAndIdle() const { | 111 bool FakeSocket::IsConnectedAndIdle() const { |
94 NOTIMPLEMENTED(); | 112 NOTIMPLEMENTED(); |
95 return false; | 113 return false; |
96 } | 114 } |
97 | 115 |
98 int FakeSocket::GetPeerAddress( | 116 int FakeSocket::GetPeerAddress(net::AddressList* address) const { |
99 net::AddressList* address) const { | 117 net::IPAddressNumber ip; |
100 NOTIMPLEMENTED(); | 118 ip.resize(net::kIPv4AddressSize); |
101 return net::ERR_FAILED; | 119 *address = net::AddressList::CreateFromIPAddress(ip, 0); |
| 120 return net::OK; |
102 } | 121 } |
103 | 122 |
104 int FakeSocket::GetLocalAddress( | 123 int FakeSocket::GetLocalAddress( |
105 net::IPEndPoint* address) const { | 124 net::IPEndPoint* address) const { |
106 NOTIMPLEMENTED(); | 125 NOTIMPLEMENTED(); |
107 return net::ERR_FAILED; | 126 return net::ERR_FAILED; |
108 } | 127 } |
109 | 128 |
110 const net::BoundNetLog& FakeSocket::NetLog() const { | 129 const net::BoundNetLog& FakeSocket::NetLog() const { |
111 EXPECT_EQ(message_loop_, MessageLoop::current()); | 130 EXPECT_EQ(message_loop_, MessageLoop::current()); |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 void FakeSession::set_config(const SessionConfig& config) { | 279 void FakeSession::set_config(const SessionConfig& config) { |
261 config_ = config; | 280 config_ = config; |
262 } | 281 } |
263 | 282 |
264 void FakeSession::Close() { | 283 void FakeSession::Close() { |
265 closed_ = true; | 284 closed_ = true; |
266 } | 285 } |
267 | 286 |
268 } // namespace protocol | 287 } // namespace protocol |
269 } // namespace remoting | 288 } // namespace remoting |
OLD | NEW |