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::SetPeer(FakeSocket* peer_socket) { |
| 50 EXPECT_EQ(message_loop_, MessageLoop::current()); |
| 51 peer_socket_ = peer_socket->weak_factory_.GetWeakPtr(); |
| 52 } |
| 53 |
44 int FakeSocket::Read(net::IOBuffer* buf, int buf_len, | 54 int FakeSocket::Read(net::IOBuffer* buf, int buf_len, |
45 const net::CompletionCallback& callback) { | 55 const net::CompletionCallback& callback) { |
46 EXPECT_EQ(message_loop_, MessageLoop::current()); | 56 EXPECT_EQ(message_loop_, MessageLoop::current()); |
47 if (input_pos_ < static_cast<int>(input_data_.size())) { | 57 if (input_pos_ < static_cast<int>(input_data_.size())) { |
48 int result = std::min(buf_len, | 58 int result = std::min(buf_len, |
49 static_cast<int>(input_data_.size()) - input_pos_); | 59 static_cast<int>(input_data_.size()) - input_pos_); |
50 memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result); | 60 memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result); |
51 input_pos_ += result; | 61 input_pos_ += result; |
52 return result; | 62 return result; |
53 } else { | 63 } else { |
54 read_pending_ = true; | 64 read_pending_ = true; |
55 read_buffer_ = buf; | 65 read_buffer_ = buf; |
56 read_buffer_size_ = buf_len; | 66 read_buffer_size_ = buf_len; |
57 read_callback_ = callback; | 67 read_callback_ = callback; |
58 return net::ERR_IO_PENDING; | 68 return net::ERR_IO_PENDING; |
59 } | 69 } |
60 } | 70 } |
61 | 71 |
62 int FakeSocket::Write(net::IOBuffer* buf, int buf_len, | 72 int FakeSocket::Write(net::IOBuffer* buf, int buf_len, |
63 const net::CompletionCallback& callback) { | 73 const net::CompletionCallback& callback) { |
64 EXPECT_EQ(message_loop_, MessageLoop::current()); | 74 EXPECT_EQ(message_loop_, MessageLoop::current()); |
65 written_data_.insert(written_data_.end(), | 75 written_data_.insert(written_data_.end(), |
66 buf->data(), buf->data() + buf_len); | 76 buf->data(), buf->data() + buf_len); |
| 77 |
| 78 if (peer_socket_) { |
| 79 message_loop_->PostTask(FROM_HERE, base::Bind( |
| 80 &FakeSocket::AppendInputData, peer_socket_, |
| 81 std::vector<char>(buf->data(), buf->data() + buf_len))); |
| 82 } |
| 83 |
67 return buf_len; | 84 return buf_len; |
68 } | 85 } |
69 | 86 |
70 bool FakeSocket::SetReceiveBufferSize(int32 size) { | 87 bool FakeSocket::SetReceiveBufferSize(int32 size) { |
71 NOTIMPLEMENTED(); | 88 NOTIMPLEMENTED(); |
72 return false; | 89 return false; |
73 } | 90 } |
74 bool FakeSocket::SetSendBufferSize(int32 size) { | 91 bool FakeSocket::SetSendBufferSize(int32 size) { |
75 NOTIMPLEMENTED(); | 92 NOTIMPLEMENTED(); |
76 return false; | 93 return false; |
77 } | 94 } |
78 | 95 |
79 int FakeSocket::Connect(const net::CompletionCallback& callback) { | 96 int FakeSocket::Connect(const net::CompletionCallback& callback) { |
80 EXPECT_EQ(message_loop_, MessageLoop::current()); | 97 EXPECT_EQ(message_loop_, MessageLoop::current()); |
81 return net::OK; | 98 return net::OK; |
82 } | 99 } |
83 | 100 |
84 void FakeSocket::Disconnect() { | 101 void FakeSocket::Disconnect() { |
85 NOTIMPLEMENTED(); | 102 peer_socket_.reset(); |
86 } | 103 } |
87 | 104 |
88 bool FakeSocket::IsConnected() const { | 105 bool FakeSocket::IsConnected() const { |
89 EXPECT_EQ(message_loop_, MessageLoop::current()); | 106 EXPECT_EQ(message_loop_, MessageLoop::current()); |
90 return true; | 107 return true; |
91 } | 108 } |
92 | 109 |
93 bool FakeSocket::IsConnectedAndIdle() const { | 110 bool FakeSocket::IsConnectedAndIdle() const { |
94 NOTIMPLEMENTED(); | 111 NOTIMPLEMENTED(); |
95 return false; | 112 return false; |
96 } | 113 } |
97 | 114 |
98 int FakeSocket::GetPeerAddress( | 115 int FakeSocket::GetPeerAddress(net::AddressList* address) const { |
99 net::AddressList* address) const { | 116 net::IPAddressNumber ip; |
100 NOTIMPLEMENTED(); | 117 ip.resize(net::kIPv4AddressSize); |
101 return net::ERR_FAILED; | 118 *address = net::AddressList::CreateFromIPAddress(ip, 0); |
| 119 return net::OK; |
102 } | 120 } |
103 | 121 |
104 int FakeSocket::GetLocalAddress( | 122 int FakeSocket::GetLocalAddress( |
105 net::IPEndPoint* address) const { | 123 net::IPEndPoint* address) const { |
106 NOTIMPLEMENTED(); | 124 NOTIMPLEMENTED(); |
107 return net::ERR_FAILED; | 125 return net::ERR_FAILED; |
108 } | 126 } |
109 | 127 |
110 const net::BoundNetLog& FakeSocket::NetLog() const { | 128 const net::BoundNetLog& FakeSocket::NetLog() const { |
111 EXPECT_EQ(message_loop_, MessageLoop::current()); | 129 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) { | 278 void FakeSession::set_config(const SessionConfig& config) { |
261 config_ = config; | 279 config_ = config; |
262 } | 280 } |
263 | 281 |
264 void FakeSession::Close() { | 282 void FakeSession::Close() { |
265 closed_ = true; | 283 closed_ = true; |
266 } | 284 } |
267 | 285 |
268 } // namespace protocol | 286 } // namespace protocol |
269 } // namespace remoting | 287 } // namespace remoting |
OLD | NEW |