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 : async_write_(false), | 21 : async_write_(false), |
22 write_pending_(false), | 22 write_pending_(false), |
23 write_limit_(0), | 23 write_limit_(0), |
24 next_write_error_(net::OK), | 24 next_write_error_(net::OK), |
25 next_read_error_(net::OK), | 25 next_read_error_(net::OK), |
26 read_pending_(false), | 26 read_pending_(false), |
27 read_buffer_size_(0), | 27 read_buffer_size_(0), |
28 input_pos_(0), | 28 input_pos_(0), |
29 message_loop_(MessageLoop::current()), | 29 message_loop_(base::MessageLoop::current()), |
30 weak_factory_(this) { | 30 weak_factory_(this) {} |
alexeypa (please no reviews)
2013/04/29 17:17:41
nit: Please move the closing bracket to the next l
xhwang
2013/04/30 00:02:36
Done.
| |
31 } | |
32 | 31 |
33 FakeSocket::~FakeSocket() { | 32 FakeSocket::~FakeSocket() { |
34 EXPECT_EQ(message_loop_, MessageLoop::current()); | 33 EXPECT_EQ(message_loop_, base::MessageLoop::current()); |
35 } | 34 } |
36 | 35 |
37 void FakeSocket::AppendInputData(const std::vector<char>& data) { | 36 void FakeSocket::AppendInputData(const std::vector<char>& data) { |
38 EXPECT_EQ(message_loop_, MessageLoop::current()); | 37 EXPECT_EQ(message_loop_, base::MessageLoop::current()); |
39 input_data_.insert(input_data_.end(), data.begin(), data.end()); | 38 input_data_.insert(input_data_.end(), data.begin(), data.end()); |
40 // Complete pending read if any. | 39 // Complete pending read if any. |
41 if (read_pending_) { | 40 if (read_pending_) { |
42 read_pending_ = false; | 41 read_pending_ = false; |
43 int result = std::min(read_buffer_size_, | 42 int result = std::min(read_buffer_size_, |
44 static_cast<int>(input_data_.size() - input_pos_)); | 43 static_cast<int>(input_data_.size() - input_pos_)); |
45 CHECK(result > 0); | 44 CHECK(result > 0); |
46 memcpy(read_buffer_->data(), | 45 memcpy(read_buffer_->data(), |
47 &(*input_data_.begin()) + input_pos_, result); | 46 &(*input_data_.begin()) + input_pos_, result); |
48 input_pos_ += result; | 47 input_pos_ += result; |
49 read_buffer_ = NULL; | 48 read_buffer_ = NULL; |
50 read_callback_.Run(result); | 49 read_callback_.Run(result); |
51 } | 50 } |
52 } | 51 } |
53 | 52 |
54 void FakeSocket::PairWith(FakeSocket* peer_socket) { | 53 void FakeSocket::PairWith(FakeSocket* peer_socket) { |
55 EXPECT_EQ(message_loop_, MessageLoop::current()); | 54 EXPECT_EQ(message_loop_, base::MessageLoop::current()); |
56 peer_socket_ = peer_socket->weak_factory_.GetWeakPtr(); | 55 peer_socket_ = peer_socket->weak_factory_.GetWeakPtr(); |
57 peer_socket->peer_socket_ = weak_factory_.GetWeakPtr(); | 56 peer_socket->peer_socket_ = weak_factory_.GetWeakPtr(); |
58 } | 57 } |
59 | 58 |
60 int FakeSocket::Read(net::IOBuffer* buf, int buf_len, | 59 int FakeSocket::Read(net::IOBuffer* buf, int buf_len, |
61 const net::CompletionCallback& callback) { | 60 const net::CompletionCallback& callback) { |
62 EXPECT_EQ(message_loop_, MessageLoop::current()); | 61 EXPECT_EQ(message_loop_, base::MessageLoop::current()); |
63 | 62 |
64 if (next_read_error_ != net::OK) { | 63 if (next_read_error_ != net::OK) { |
65 int r = next_read_error_; | 64 int r = next_read_error_; |
66 next_read_error_ = net::OK; | 65 next_read_error_ = net::OK; |
67 return r; | 66 return r; |
68 } | 67 } |
69 | 68 |
70 if (input_pos_ < static_cast<int>(input_data_.size())) { | 69 if (input_pos_ < static_cast<int>(input_data_.size())) { |
71 int result = std::min(buf_len, | 70 int result = std::min(buf_len, |
72 static_cast<int>(input_data_.size()) - input_pos_); | 71 static_cast<int>(input_data_.size()) - input_pos_); |
73 memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result); | 72 memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result); |
74 input_pos_ += result; | 73 input_pos_ += result; |
75 return result; | 74 return result; |
76 } else { | 75 } else { |
77 read_pending_ = true; | 76 read_pending_ = true; |
78 read_buffer_ = buf; | 77 read_buffer_ = buf; |
79 read_buffer_size_ = buf_len; | 78 read_buffer_size_ = buf_len; |
80 read_callback_ = callback; | 79 read_callback_ = callback; |
81 return net::ERR_IO_PENDING; | 80 return net::ERR_IO_PENDING; |
82 } | 81 } |
83 } | 82 } |
84 | 83 |
85 int FakeSocket::Write(net::IOBuffer* buf, int buf_len, | 84 int FakeSocket::Write(net::IOBuffer* buf, int buf_len, |
86 const net::CompletionCallback& callback) { | 85 const net::CompletionCallback& callback) { |
87 EXPECT_EQ(message_loop_, MessageLoop::current()); | 86 EXPECT_EQ(message_loop_, base::MessageLoop::current()); |
88 EXPECT_FALSE(write_pending_); | 87 EXPECT_FALSE(write_pending_); |
89 | 88 |
90 if (write_limit_ > 0) | 89 if (write_limit_ > 0) |
91 buf_len = std::min(write_limit_, buf_len); | 90 buf_len = std::min(write_limit_, buf_len); |
92 | 91 |
93 if (async_write_) { | 92 if (async_write_) { |
94 message_loop_->PostTask(FROM_HERE, base::Bind( | 93 message_loop_->PostTask(FROM_HERE, base::Bind( |
95 &FakeSocket::DoAsyncWrite, weak_factory_.GetWeakPtr(), | 94 &FakeSocket::DoAsyncWrite, weak_factory_.GetWeakPtr(), |
96 scoped_refptr<net::IOBuffer>(buf), buf_len, callback)); | 95 scoped_refptr<net::IOBuffer>(buf), buf_len, callback)); |
97 write_pending_ = true; | 96 write_pending_ = true; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
137 bool FakeSocket::SetReceiveBufferSize(int32 size) { | 136 bool FakeSocket::SetReceiveBufferSize(int32 size) { |
138 NOTIMPLEMENTED(); | 137 NOTIMPLEMENTED(); |
139 return false; | 138 return false; |
140 } | 139 } |
141 bool FakeSocket::SetSendBufferSize(int32 size) { | 140 bool FakeSocket::SetSendBufferSize(int32 size) { |
142 NOTIMPLEMENTED(); | 141 NOTIMPLEMENTED(); |
143 return false; | 142 return false; |
144 } | 143 } |
145 | 144 |
146 int FakeSocket::Connect(const net::CompletionCallback& callback) { | 145 int FakeSocket::Connect(const net::CompletionCallback& callback) { |
147 EXPECT_EQ(message_loop_, MessageLoop::current()); | 146 EXPECT_EQ(message_loop_, base::MessageLoop::current()); |
148 return net::OK; | 147 return net::OK; |
149 } | 148 } |
150 | 149 |
151 void FakeSocket::Disconnect() { | 150 void FakeSocket::Disconnect() { |
152 peer_socket_.reset(); | 151 peer_socket_.reset(); |
153 } | 152 } |
154 | 153 |
155 bool FakeSocket::IsConnected() const { | 154 bool FakeSocket::IsConnected() const { |
156 EXPECT_EQ(message_loop_, MessageLoop::current()); | 155 EXPECT_EQ(message_loop_, base::MessageLoop::current()); |
157 return true; | 156 return true; |
158 } | 157 } |
159 | 158 |
160 bool FakeSocket::IsConnectedAndIdle() const { | 159 bool FakeSocket::IsConnectedAndIdle() const { |
161 NOTIMPLEMENTED(); | 160 NOTIMPLEMENTED(); |
162 return false; | 161 return false; |
163 } | 162 } |
164 | 163 |
165 int FakeSocket::GetPeerAddress(net::IPEndPoint* address) const { | 164 int FakeSocket::GetPeerAddress(net::IPEndPoint* address) const { |
166 net::IPAddressNumber ip(net::kIPv4AddressSize); | 165 net::IPAddressNumber ip(net::kIPv4AddressSize); |
167 *address = net::IPEndPoint(ip, 0); | 166 *address = net::IPEndPoint(ip, 0); |
168 return net::OK; | 167 return net::OK; |
169 } | 168 } |
170 | 169 |
171 int FakeSocket::GetLocalAddress(net::IPEndPoint* address) const { | 170 int FakeSocket::GetLocalAddress(net::IPEndPoint* address) const { |
172 NOTIMPLEMENTED(); | 171 NOTIMPLEMENTED(); |
173 return net::ERR_FAILED; | 172 return net::ERR_FAILED; |
174 } | 173 } |
175 | 174 |
176 const net::BoundNetLog& FakeSocket::NetLog() const { | 175 const net::BoundNetLog& FakeSocket::NetLog() const { |
177 EXPECT_EQ(message_loop_, MessageLoop::current()); | 176 EXPECT_EQ(message_loop_, base::MessageLoop::current()); |
178 return net_log_; | 177 return net_log_; |
179 } | 178 } |
180 | 179 |
181 void FakeSocket::SetSubresourceSpeculation() { | 180 void FakeSocket::SetSubresourceSpeculation() { |
182 NOTIMPLEMENTED(); | 181 NOTIMPLEMENTED(); |
183 } | 182 } |
184 | 183 |
185 void FakeSocket::SetOmniboxSpeculation() { | 184 void FakeSocket::SetOmniboxSpeculation() { |
186 NOTIMPLEMENTED(); | 185 NOTIMPLEMENTED(); |
187 } | 186 } |
(...skipping 17 matching lines...) Expand all Loading... | |
205 return net::kProtoUnknown; | 204 return net::kProtoUnknown; |
206 } | 205 } |
207 | 206 |
208 bool FakeSocket::GetSSLInfo(net::SSLInfo* ssl_info) { | 207 bool FakeSocket::GetSSLInfo(net::SSLInfo* ssl_info) { |
209 return false; | 208 return false; |
210 } | 209 } |
211 | 210 |
212 FakeUdpSocket::FakeUdpSocket() | 211 FakeUdpSocket::FakeUdpSocket() |
213 : read_pending_(false), | 212 : read_pending_(false), |
214 input_pos_(0), | 213 input_pos_(0), |
215 message_loop_(MessageLoop::current()) { | 214 message_loop_(base::MessageLoop::current()) {} |
alexeypa (please no reviews)
2013/04/29 17:17:41
nit: Please move the closing bracket to the next l
xhwang
2013/04/30 00:02:36
Done.
| |
216 } | |
217 | 215 |
218 FakeUdpSocket::~FakeUdpSocket() { | 216 FakeUdpSocket::~FakeUdpSocket() { |
219 EXPECT_EQ(message_loop_, MessageLoop::current()); | 217 EXPECT_EQ(message_loop_, base::MessageLoop::current()); |
220 } | 218 } |
221 | 219 |
222 void FakeUdpSocket::AppendInputPacket(const char* data, int data_size) { | 220 void FakeUdpSocket::AppendInputPacket(const char* data, int data_size) { |
223 EXPECT_EQ(message_loop_, MessageLoop::current()); | 221 EXPECT_EQ(message_loop_, base::MessageLoop::current()); |
224 input_packets_.push_back(std::string()); | 222 input_packets_.push_back(std::string()); |
225 input_packets_.back().assign(data, data + data_size); | 223 input_packets_.back().assign(data, data + data_size); |
226 | 224 |
227 // Complete pending read if any. | 225 // Complete pending read if any. |
228 if (read_pending_) { | 226 if (read_pending_) { |
229 read_pending_ = false; | 227 read_pending_ = false; |
230 int result = std::min(data_size, read_buffer_size_); | 228 int result = std::min(data_size, read_buffer_size_); |
231 memcpy(read_buffer_->data(), data, result); | 229 memcpy(read_buffer_->data(), data, result); |
232 input_pos_ = input_packets_.size(); | 230 input_pos_ = input_packets_.size(); |
233 read_callback_.Run(result); | 231 read_callback_.Run(result); |
234 read_buffer_ = NULL; | 232 read_buffer_ = NULL; |
235 } | 233 } |
236 } | 234 } |
237 | 235 |
238 int FakeUdpSocket::Read(net::IOBuffer* buf, int buf_len, | 236 int FakeUdpSocket::Read(net::IOBuffer* buf, int buf_len, |
239 const net::CompletionCallback& callback) { | 237 const net::CompletionCallback& callback) { |
240 EXPECT_EQ(message_loop_, MessageLoop::current()); | 238 EXPECT_EQ(message_loop_, base::MessageLoop::current()); |
241 if (input_pos_ < static_cast<int>(input_packets_.size())) { | 239 if (input_pos_ < static_cast<int>(input_packets_.size())) { |
242 int result = std::min( | 240 int result = std::min( |
243 buf_len, static_cast<int>(input_packets_[input_pos_].size())); | 241 buf_len, static_cast<int>(input_packets_[input_pos_].size())); |
244 memcpy(buf->data(), &(*input_packets_[input_pos_].begin()), result); | 242 memcpy(buf->data(), &(*input_packets_[input_pos_].begin()), result); |
245 ++input_pos_; | 243 ++input_pos_; |
246 return result; | 244 return result; |
247 } else { | 245 } else { |
248 read_pending_ = true; | 246 read_pending_ = true; |
249 read_buffer_ = buf; | 247 read_buffer_ = buf; |
250 read_buffer_size_ = buf_len; | 248 read_buffer_size_ = buf_len; |
251 read_callback_ = callback; | 249 read_callback_ = callback; |
252 return net::ERR_IO_PENDING; | 250 return net::ERR_IO_PENDING; |
253 } | 251 } |
254 } | 252 } |
255 | 253 |
256 int FakeUdpSocket::Write(net::IOBuffer* buf, int buf_len, | 254 int FakeUdpSocket::Write(net::IOBuffer* buf, int buf_len, |
257 const net::CompletionCallback& callback) { | 255 const net::CompletionCallback& callback) { |
258 EXPECT_EQ(message_loop_, MessageLoop::current()); | 256 EXPECT_EQ(message_loop_, base::MessageLoop::current()); |
259 written_packets_.push_back(std::string()); | 257 written_packets_.push_back(std::string()); |
260 written_packets_.back().assign(buf->data(), buf->data() + buf_len); | 258 written_packets_.back().assign(buf->data(), buf->data() + buf_len); |
261 return buf_len; | 259 return buf_len; |
262 } | 260 } |
263 | 261 |
264 bool FakeUdpSocket::SetReceiveBufferSize(int32 size) { | 262 bool FakeUdpSocket::SetReceiveBufferSize(int32 size) { |
265 NOTIMPLEMENTED(); | 263 NOTIMPLEMENTED(); |
266 return false; | 264 return false; |
267 } | 265 } |
268 bool FakeUdpSocket::SetSendBufferSize(int32 size) { | 266 bool FakeUdpSocket::SetSendBufferSize(int32 size) { |
269 NOTIMPLEMENTED(); | 267 NOTIMPLEMENTED(); |
270 return false; | 268 return false; |
271 } | 269 } |
272 | 270 |
273 FakeSession::FakeSession() | 271 FakeSession::FakeSession() |
274 : event_handler_(NULL), | 272 : event_handler_(NULL), |
275 candidate_config_(CandidateSessionConfig::CreateDefault()), | 273 candidate_config_(CandidateSessionConfig::CreateDefault()), |
276 config_(SessionConfig::ForTest()), | 274 config_(SessionConfig::ForTest()), |
277 message_loop_(MessageLoop::current()), | 275 message_loop_(base::MessageLoop::current()), |
278 async_creation_(false), | 276 async_creation_(false), |
279 jid_(kTestJid), | 277 jid_(kTestJid), |
280 error_(OK), | 278 error_(OK), |
281 closed_(false), | 279 closed_(false), |
282 weak_factory_(this) { | 280 weak_factory_(this) {} |
alexeypa (please no reviews)
2013/04/29 17:17:41
nit: Please move the closing bracket to the next l
xhwang
2013/04/30 00:02:36
Done.
| |
283 } | |
284 | 281 |
285 FakeSession::~FakeSession() { } | 282 FakeSession::~FakeSession() { } |
286 | 283 |
287 FakeSocket* FakeSession::GetStreamChannel(const std::string& name) { | 284 FakeSocket* FakeSession::GetStreamChannel(const std::string& name) { |
288 return stream_channels_[name]; | 285 return stream_channels_[name]; |
289 } | 286 } |
290 | 287 |
291 FakeUdpSocket* FakeSession::GetDatagramChannel(const std::string& name) { | 288 FakeUdpSocket* FakeSession::GetDatagramChannel(const std::string& name) { |
292 return datagram_channels_[name]; | 289 return datagram_channels_[name]; |
293 } | 290 } |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
380 callback.Run(scoped_ptr<net::Socket>(datagram_channels_[name])); | 377 callback.Run(scoped_ptr<net::Socket>(datagram_channels_[name])); |
381 } | 378 } |
382 | 379 |
383 void FakeSession::CancelChannelCreation(const std::string& name) { | 380 void FakeSession::CancelChannelCreation(const std::string& name) { |
384 stream_channels_.erase(name); | 381 stream_channels_.erase(name); |
385 datagram_channels_.erase(name); | 382 datagram_channels_.erase(name); |
386 } | 383 } |
387 | 384 |
388 } // namespace protocol | 385 } // namespace protocol |
389 } // namespace remoting | 386 } // namespace remoting |
OLD | NEW |