Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(576)

Unified Diff: remoting/protocol/fake_stream_socket.cc

Issue 580243003: Cleanup Fake* classes in remoting/protocol (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@sctp
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: remoting/protocol/fake_stream_socket.cc
diff --git a/remoting/protocol/fake_stream_socket.cc b/remoting/protocol/fake_stream_socket.cc
new file mode 100644
index 0000000000000000000000000000000000000000..ffe2021f8440751a06140b758c101bac012abe3d
--- /dev/null
+++ b/remoting/protocol/fake_stream_socket.cc
@@ -0,0 +1,261 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "remoting/protocol/fake_stream_socket.h"
+
+#include "base/bind.h"
+#include "base/single_thread_task_runner.h"
+#include "base/thread_task_runner_handle.h"
+#include "net/base/address_list.h"
+#include "net/base/io_buffer.h"
+#include "net/base/net_errors.h"
+#include "net/base/net_util.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace remoting {
+namespace protocol {
+
+FakeStreamSocket::FakeStreamSocket()
+ : async_write_(false),
+ write_pending_(false),
+ write_limit_(0),
+ next_write_error_(net::OK),
+ next_read_error_(net::OK),
+ read_pending_(false),
+ read_buffer_size_(0),
+ input_pos_(0),
+ task_runner_(base::ThreadTaskRunnerHandle::Get()),
+ weak_factory_(this) {
+}
+
+FakeStreamSocket::~FakeStreamSocket() {
+ EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
+}
+
+void FakeStreamSocket::AppendInputData(const std::string& data) {
+ EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
Wez 2014/09/20 00:25:10 Should these be ASSERT_TRUE, since bad things will
Sergey Ulanov 2014/09/22 19:22:11 ASSERT_TRUE() only works in functions that return
+ input_data_.insert(input_data_.end(), data.begin(), data.end());
+ // Complete pending read if any.
+ if (read_pending_) {
+ read_pending_ = false;
+ int result = std::min(read_buffer_size_,
+ static_cast<int>(input_data_.size() - input_pos_));
+ CHECK(result > 0);
Wez 2014/09/20 00:25:10 EXPECT_GT or ASSERT_GT?
Sergey Ulanov 2014/09/22 19:22:12 Done.
+ memcpy(read_buffer_->data(),
+ &(*input_data_.begin()) + input_pos_, result);
+ input_pos_ += result;
+ read_buffer_ = NULL;
+ read_callback_.Run(result);
+ }
+}
+
+void FakeStreamSocket::PairWith(FakeStreamSocket* peer_socket) {
+ EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
+ peer_socket_ = peer_socket->weak_factory_.GetWeakPtr();
+ peer_socket->peer_socket_ = weak_factory_.GetWeakPtr();
+}
+
+int FakeStreamSocket::Read(net::IOBuffer* buf, int buf_len,
+ const net::CompletionCallback& callback) {
+ EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
+
+ if (next_read_error_ != net::OK) {
+ int r = next_read_error_;
+ next_read_error_ = net::OK;
+ return r;
+ }
+
+ if (input_pos_ < static_cast<int>(input_data_.size())) {
Wez 2014/09/20 00:25:10 Looks like this fake provides no way to return EOF
Sergey Ulanov 2014/09/22 19:22:12 Yes. We've never needed it - that feature can be a
+ int result = std::min(buf_len,
+ static_cast<int>(input_data_.size()) - input_pos_);
+ memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result);
+ input_pos_ += result;
+ return result;
+ } else {
+ read_pending_ = true;
+ read_buffer_ = buf;
+ read_buffer_size_ = buf_len;
+ read_callback_ = callback;
+ return net::ERR_IO_PENDING;
+ }
+}
+
+int FakeStreamSocket::Write(net::IOBuffer* buf, int buf_len,
+ const net::CompletionCallback& callback) {
+ EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
+ EXPECT_FALSE(write_pending_);
+
+ if (write_limit_ > 0)
+ buf_len = std::min(write_limit_, buf_len);
+
+ if (async_write_) {
+ task_runner_->PostTask(FROM_HERE, base::Bind(
+ &FakeStreamSocket::DoAsyncWrite, weak_factory_.GetWeakPtr(),
+ scoped_refptr<net::IOBuffer>(buf), buf_len, callback));
+ write_pending_ = true;
+ return net::ERR_IO_PENDING;
+ } else {
+ if (next_write_error_ != net::OK) {
+ int r = next_write_error_;
+ next_write_error_ = net::OK;
+ return r;
+ }
+
+ DoWrite(buf, buf_len);
+ return buf_len;
+ }
+}
+
+void FakeStreamSocket::DoAsyncWrite(scoped_refptr<net::IOBuffer> buf,
+ int buf_len,
+ const net::CompletionCallback& callback) {
+ write_pending_ = false;
+
+ if (next_write_error_ != net::OK) {
+ int r = next_write_error_;
+ next_write_error_ = net::OK;
+ callback.Run(r);
+ return;
+ }
+
+ DoWrite(buf.get(), buf_len);
+ callback.Run(buf_len);
+}
+
+void FakeStreamSocket::DoWrite(net::IOBuffer* buf, int buf_len) {
+ written_data_.insert(written_data_.end(),
+ buf->data(), buf->data() + buf_len);
+
+ if (peer_socket_.get()) {
+ task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(&FakeStreamSocket::AppendInputData,
+ peer_socket_,
+ std::string(buf->data(), buf->data() + buf_len)));
+ }
+}
+
+int FakeStreamSocket::SetReceiveBufferSize(int32 size) {
+ NOTIMPLEMENTED();
+ return net::ERR_NOT_IMPLEMENTED;
+}
+
+int FakeStreamSocket::SetSendBufferSize(int32 size) {
+ NOTIMPLEMENTED();
+ return net::ERR_NOT_IMPLEMENTED;
+}
+
+int FakeStreamSocket::Connect(const net::CompletionCallback& callback) {
+ EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
+ return net::OK;
+}
+
+void FakeStreamSocket::Disconnect() {
+ peer_socket_.reset();
+}
+
+bool FakeStreamSocket::IsConnected() const {
+ EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
Wez 2014/09/20 00:25:10 Looks like several calls e.g. ::Disconnect(), ::Se
Sergey Ulanov 2014/09/22 19:22:12 Done.
+ return true;
+}
+
+bool FakeStreamSocket::IsConnectedAndIdle() const {
+ NOTIMPLEMENTED();
+ return false;
+}
+
+int FakeStreamSocket::GetPeerAddress(net::IPEndPoint* address) const {
+ net::IPAddressNumber ip(net::kIPv4AddressSize);
+ *address = net::IPEndPoint(ip, 0);
+ return net::OK;
+}
+
+int FakeStreamSocket::GetLocalAddress(net::IPEndPoint* address) const {
+ NOTIMPLEMENTED();
+ return net::ERR_NOT_IMPLEMENTED;
+}
+
+const net::BoundNetLog& FakeStreamSocket::NetLog() const {
+ EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
+ return net_log_;
+}
+
+void FakeStreamSocket::SetSubresourceSpeculation() {
+ NOTIMPLEMENTED();
+}
+
+void FakeStreamSocket::SetOmniboxSpeculation() {
+ NOTIMPLEMENTED();
+}
+
+bool FakeStreamSocket::WasEverUsed() const {
+ NOTIMPLEMENTED();
+ return true;
+}
+
+bool FakeStreamSocket::UsingTCPFastOpen() const {
+ NOTIMPLEMENTED();
+ return true;
+}
+
+bool FakeStreamSocket::WasNpnNegotiated() const {
+ return false;
+}
+
+net::NextProto FakeStreamSocket::GetNegotiatedProtocol() const {
+ NOTIMPLEMENTED();
+ return net::kProtoUnknown;
+}
+
+bool FakeStreamSocket::GetSSLInfo(net::SSLInfo* ssl_info) {
+ return false;
+}
+
+FakeStreamChannelFactory::FakeStreamChannelFactory()
+ : task_runner_(base::ThreadTaskRunnerHandle::Get()),
+ async_creation_(false),
+ fail_(false),
+ weak_factory_(this) {
+}
+
+FakeStreamChannelFactory::~FakeStreamChannelFactory() {}
+
+FakeStreamSocket* FakeStreamChannelFactory::GetChannel(
+ const std::string& name) {
+ return channels_[name];
+}
+
+void FakeStreamChannelFactory::CreateChannel(
+ const std::string& name,
+ const ChannelCreatedCallback& callback) {
+ scoped_ptr<FakeStreamSocket> channel;
Wez 2014/09/20 00:25:10 See comments on datagram equivalent.
Sergey Ulanov 2014/09/22 19:22:12 Done.
+ // If we are in the error state then we put NULL in the channels list, so that
+ // NotifyChannelCreated() still calls the callback.
+ if (!fail_)
+ channel.reset(new FakeStreamSocket());
+ channels_[name] = channel.release();
+
+ if (async_creation_) {
+ task_runner_->PostTask(FROM_HERE, base::Bind(
+ &FakeStreamChannelFactory::NotifyChannelCreated,
+ weak_factory_.GetWeakPtr(),
+ name, callback));
+ } else {
+ NotifyChannelCreated(name, callback);
+ }
+}
+
+void FakeStreamChannelFactory::NotifyChannelCreated(
+ const std::string& name,
+ const ChannelCreatedCallback& callback) {
+ if (channels_.find(name) != channels_.end())
+ callback.Run(scoped_ptr<net::StreamSocket>(channels_[name]));
Wez 2014/09/20 00:25:10 Set comments on datagram equivalent.
Sergey Ulanov 2014/09/22 19:22:12 Done.
+}
+
+void FakeStreamChannelFactory::CancelChannelCreation(const std::string& name) {
+ channels_.erase(name);
+}
+
+} // namespace protocol
+} // namespace remoting

Powered by Google App Engine
This is Rietveld 408576698