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