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

Unified Diff: net/socket/ssl_server_socket_unittest.cc

Issue 2093923004: [Cast Channel] Add real SSL tests to CastSocketTest (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 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: net/socket/ssl_server_socket_unittest.cc
diff --git a/net/socket/ssl_server_socket_unittest.cc b/net/socket/ssl_server_socket_unittest.cc
index 92481a8e782f335232c1941abf1a72fab19993ef..20e2c4ec9e460c513e532a974df75855aaff46cc 100644
--- a/net/socket/ssl_server_socket_unittest.cc
+++ b/net/socket/ssl_server_socket_unittest.cc
@@ -11,7 +11,7 @@
// 2. FakeDataChannel
// Implements the actual exchange of data between two FakeSockets.
//
-// Implementations of these two classes are included in this file.
+// Implementations of these two classes are in net/socket/socket_test_util.h.
#include "net/socket/ssl_server_socket.h"
@@ -24,7 +24,6 @@
#include <openssl/ssl.h>
#include <openssl/x509.h>
-#include "base/callback_helpers.h"
#include "base/compiler_specific.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
@@ -33,19 +32,15 @@
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
-#include "base/single_thread_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "build/build_config.h"
#include "crypto/nss_util.h"
#include "crypto/rsa_private_key.h"
#include "crypto/scoped_openssl_types.h"
#include "crypto/signature_creator.h"
-#include "net/base/address_list.h"
#include "net/base/completion_callback.h"
#include "net/base/host_port_pair.h"
#include "net/base/io_buffer.h"
-#include "net/base/ip_address.h"
-#include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h"
#include "net/cert/cert_status_flags.h"
#include "net/cert/ct_policy_enforcer.h"
@@ -119,209 +114,6 @@ class MockCTPolicyEnforcer : public CTPolicyEnforcer {
}
};
-class FakeDataChannel {
- public:
- FakeDataChannel()
- : read_buf_len_(0),
- closed_(false),
- write_called_after_close_(false),
- weak_factory_(this) {
- }
-
- int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) {
- DCHECK(read_callback_.is_null());
- DCHECK(!read_buf_.get());
- if (closed_)
- return 0;
- if (data_.empty()) {
- read_callback_ = callback;
- read_buf_ = buf;
- read_buf_len_ = buf_len;
- return ERR_IO_PENDING;
- }
- return PropagateData(buf, buf_len);
- }
-
- int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback) {
- DCHECK(write_callback_.is_null());
- if (closed_) {
- if (write_called_after_close_)
- return ERR_CONNECTION_RESET;
- write_called_after_close_ = true;
- write_callback_ = callback;
- base::ThreadTaskRunnerHandle::Get()->PostTask(
- FROM_HERE, base::Bind(&FakeDataChannel::DoWriteCallback,
- weak_factory_.GetWeakPtr()));
- return ERR_IO_PENDING;
- }
- // This function returns synchronously, so make a copy of the buffer.
- data_.push(new DrainableIOBuffer(
- new StringIOBuffer(std::string(buf->data(), buf_len)),
- buf_len));
- base::ThreadTaskRunnerHandle::Get()->PostTask(
- FROM_HERE, base::Bind(&FakeDataChannel::DoReadCallback,
- weak_factory_.GetWeakPtr()));
- return buf_len;
- }
-
- // Closes the FakeDataChannel. After Close() is called, Read() returns 0,
- // indicating EOF, and Write() fails with ERR_CONNECTION_RESET. Note that
- // after the FakeDataChannel is closed, the first Write() call completes
- // asynchronously, which is necessary to reproduce bug 127822.
- void Close() {
- closed_ = true;
- if (!read_callback_.is_null()) {
- base::ThreadTaskRunnerHandle::Get()->PostTask(
- FROM_HERE, base::Bind(&FakeDataChannel::DoReadCallback,
- weak_factory_.GetWeakPtr()));
- }
- }
-
- private:
- void DoReadCallback() {
- if (read_callback_.is_null())
- return;
-
- if (closed_) {
- base::ResetAndReturn(&read_callback_).Run(ERR_CONNECTION_CLOSED);
- return;
- }
-
- if (data_.empty())
- return;
-
- int copied = PropagateData(read_buf_, read_buf_len_);
- CompletionCallback callback = read_callback_;
- read_callback_.Reset();
- read_buf_ = NULL;
- read_buf_len_ = 0;
- callback.Run(copied);
- }
-
- void DoWriteCallback() {
- if (write_callback_.is_null())
- return;
-
- CompletionCallback callback = write_callback_;
- write_callback_.Reset();
- callback.Run(ERR_CONNECTION_RESET);
- }
-
- int PropagateData(scoped_refptr<IOBuffer> read_buf, int read_buf_len) {
- scoped_refptr<DrainableIOBuffer> buf = data_.front();
- int copied = std::min(buf->BytesRemaining(), read_buf_len);
- memcpy(read_buf->data(), buf->data(), copied);
- buf->DidConsume(copied);
-
- if (!buf->BytesRemaining())
- data_.pop();
- return copied;
- }
-
- CompletionCallback read_callback_;
- scoped_refptr<IOBuffer> read_buf_;
- int read_buf_len_;
-
- CompletionCallback write_callback_;
-
- std::queue<scoped_refptr<DrainableIOBuffer> > data_;
-
- // True if Close() has been called.
- bool closed_;
-
- // Controls the completion of Write() after the FakeDataChannel is closed.
- // After the FakeDataChannel is closed, the first Write() call completes
- // asynchronously.
- bool write_called_after_close_;
-
- base::WeakPtrFactory<FakeDataChannel> weak_factory_;
-
- DISALLOW_COPY_AND_ASSIGN(FakeDataChannel);
-};
-
-class FakeSocket : public StreamSocket {
- public:
- FakeSocket(FakeDataChannel* incoming_channel,
- FakeDataChannel* outgoing_channel)
- : incoming_(incoming_channel), outgoing_(outgoing_channel) {}
-
- ~FakeSocket() override {}
-
- int Read(IOBuffer* buf,
- int buf_len,
- const CompletionCallback& callback) override {
- // Read random number of bytes.
- buf_len = rand() % buf_len + 1;
- return incoming_->Read(buf, buf_len, callback);
- }
-
- int Write(IOBuffer* buf,
- int buf_len,
- const CompletionCallback& callback) override {
- // Write random number of bytes.
- buf_len = rand() % buf_len + 1;
- return outgoing_->Write(buf, buf_len, callback);
- }
-
- int SetReceiveBufferSize(int32_t size) override { return OK; }
-
- int SetSendBufferSize(int32_t size) override { return OK; }
-
- int Connect(const CompletionCallback& callback) override { return OK; }
-
- void Disconnect() override {
- incoming_->Close();
- outgoing_->Close();
- }
-
- bool IsConnected() const override { return true; }
-
- bool IsConnectedAndIdle() const override { return true; }
-
- int GetPeerAddress(IPEndPoint* address) const override {
- *address = IPEndPoint(IPAddress::IPv4AllZeros(), 0 /*port*/);
- return OK;
- }
-
- int GetLocalAddress(IPEndPoint* address) const override {
- *address = IPEndPoint(IPAddress::IPv4AllZeros(), 0 /*port*/);
- return OK;
- }
-
- const BoundNetLog& NetLog() const override { return net_log_; }
-
- void SetSubresourceSpeculation() override {}
- void SetOmniboxSpeculation() override {}
-
- bool WasEverUsed() const override { return true; }
-
- bool WasNpnNegotiated() const override { return false; }
-
- NextProto GetNegotiatedProtocol() const override { return kProtoUnknown; }
-
- bool GetSSLInfo(SSLInfo* ssl_info) override { return false; }
-
- void GetConnectionAttempts(ConnectionAttempts* out) const override {
- out->clear();
- }
-
- void ClearConnectionAttempts() override {}
-
- void AddConnectionAttempts(const ConnectionAttempts& attempts) override {}
-
- int64_t GetTotalReceivedBytes() const override {
- NOTIMPLEMENTED();
- return 0;
- }
-
- private:
- BoundNetLog net_log_;
- FakeDataChannel* incoming_;
- FakeDataChannel* outgoing_;
-
- DISALLOW_COPY_AND_ASSIGN(FakeSocket);
-};
-
} // namespace
// Verify the correctness of the test helper classes first.
« extensions/browser/api/cast_channel/cast_socket_unittest.cc ('K') | « net/socket/socket_test_util.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698