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

Unified Diff: chrome/browser/extensions/api/socket/tls_socket_unittest.cc

Issue 76403004: An implementation of chrome.socket.secure(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: An un-DCHECK-ification, a new test, a test clarification, and a lot of nit-double-spaces. Created 6 years, 9 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: chrome/browser/extensions/api/socket/tls_socket_unittest.cc
diff --git a/chrome/browser/extensions/api/socket/tls_socket_unittest.cc b/chrome/browser/extensions/api/socket/tls_socket_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..1761469b976256ac6ea080a0f2f5ec7597209460
--- /dev/null
+++ b/chrome/browser/extensions/api/socket/tls_socket_unittest.cc
@@ -0,0 +1,227 @@
+// Copyright 2013 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 "chrome/browser/extensions/api/socket/tls_socket.h"
+
+#include "base/memory/scoped_ptr.h"
+#include "base/strings/string_piece.h"
+#include "net/base/address_list.h"
+#include "net/base/completion_callback.h"
+#include "net/base/io_buffer.h"
+#include "net/base/net_errors.h"
+#include "net/base/rand_callback.h"
+#include "net/socket/ssl_client_socket.h"
+#include "net/socket/tcp_client_socket.h"
+#include "testing/gmock/include/gmock/gmock.h"
+
+using testing::_;
+using testing::DoAll;
+using testing::Return;
+using testing::SaveArg;
+using base::StringPiece;
+
+namespace net {
+class ServerBoundCertService;
+}
+
+namespace extensions {
+
+class MockSSLClientSocket : public net::SSLClientSocket {
+ public:
+ MockSSLClientSocket() {}
+ MOCK_METHOD0(Disconnect, void());
+ MOCK_METHOD3(Read,
+ int(net::IOBuffer* buf,
+ int buf_len,
+ const net::CompletionCallback& callback));
+ MOCK_METHOD3(Write,
+ int(net::IOBuffer* buf,
+ int buf_len,
+ const net::CompletionCallback& callback));
+ MOCK_METHOD1(SetReceiveBufferSize, bool(int32));
+ MOCK_METHOD1(SetSendBufferSize, bool(int32));
+ MOCK_METHOD1(Connect, int(const CompletionCallback&));
+ MOCK_CONST_METHOD0(IsConnectedAndIdle, bool());
+ MOCK_CONST_METHOD1(GetPeerAddress, int(net::IPEndPoint*));
+ MOCK_CONST_METHOD1(GetLocalAddress, int(net::IPEndPoint*));
+ MOCK_CONST_METHOD0(NetLog, const net::BoundNetLog&());
+ MOCK_METHOD0(SetSubresourceSpeculation, void());
+ MOCK_METHOD0(SetOmniboxSpeculation, void());
+ MOCK_CONST_METHOD0(WasEverUsed, bool());
+ MOCK_CONST_METHOD0(UsingTCPFastOpen, bool());
+ MOCK_METHOD1(GetSSLInfo, bool(net::SSLInfo*));
+ MOCK_METHOD5(ExportKeyingMaterial,
+ int(const StringPiece&,
+ bool,
+ const StringPiece&,
+ unsigned char*,
+ unsigned int));
+ MOCK_METHOD1(GetTLSUniqueChannelBinding, int(std::string*));
+ MOCK_METHOD1(GetSSLCertRequestInfo, void(net::SSLCertRequestInfo*));
+ MOCK_METHOD2(GetNextProto,
+ net::SSLClientSocket::NextProtoStatus(std::string*,
+ std::string*));
+ MOCK_CONST_METHOD0(GetServerBoundCertService, net::ServerBoundCertService*());
+ virtual bool IsConnected() const OVERRIDE { return true; }
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(MockSSLClientSocket);
+};
+
+class MockTCPSocket : public net::TCPClientSocket {
+ public:
+ explicit MockTCPSocket(const net::AddressList& address_list)
+ : net::TCPClientSocket(address_list, NULL, net::NetLog::Source()) {}
+
+ MOCK_METHOD3(Read,
+ int(net::IOBuffer* buf,
+ int buf_len,
+ const net::CompletionCallback& callback));
+ MOCK_METHOD3(Write,
+ int(net::IOBuffer* buf,
+ int buf_len,
+ const net::CompletionCallback& callback));
+ MOCK_METHOD2(SetKeepAlive, bool(bool enable, int delay));
+ MOCK_METHOD1(SetNoDelay, bool(bool no_delay));
+
+ virtual bool IsConnected() const OVERRIDE { return true; }
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(MockTCPSocket);
+};
+
+class CompleteHandler {
+ public:
+ CompleteHandler() {}
+ MOCK_METHOD1(OnComplete, void(int result_code));
+ MOCK_METHOD2(OnReadComplete,
+ void(int result_code, scoped_refptr<net::IOBuffer> io_buffer));
+ MOCK_METHOD2(OnAccept, void(int, net::TCPClientSocket*));
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(CompleteHandler);
+};
+
+static const char FAKE_ID[] = "faktetesttlssocketunittest";
+
+class SocketTest : public ::testing::Test {
+pubic:
Ryan Sleevi 2014/03/18 01:05:13 typo: public != pubic
lally 2014/03/26 03:17:51 That's just embarrassing.
+ SocketTest() {}
+
+ virtual void Setup() {
+ net::AddressList address_list;
+ MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list);
+ // ssl_socket_ is owned by socket_. SocketTest keeps a pointer to it to
+ // expect invocations from TLSSocket to ssl_socket_.
+ ssl_socket_ = new MockSSLClientSocket;
+ socket_.reset(new TLSSocket(ssl_socket_, tcp_client_socket_ID));
+ };
+
+ virtual void TearDown() {
+ ssl_socket_ = NULL;
+ socket_.reset();
+ };
+
+ protected:
+ MockSSLClientSocket* ssl_socket_;
+ scoped_ptr<TLSSocket> socket_;
+};
+
+// Verify that a Read() on TLSSocket will pass through into a Read() on
+// ssl_socket_ and invoke its completion callback.
+TEST_F(SocketTest, TestTLSSocketRead) {
+ CompleteHandler handler;
+
+ EXPECT_CALL(*ssl_socket_, Read(_, _, _)).Times(1);
+ EXPECT_CALL(handler, OnReadComplete(_, _)).Times(1);
+
+ const int count = 512;
+ socket_->Read(
+ count,
+ base::Bind(&CompleteHandler::OnReadComplete, base::Unretained(&handler)));
+}
+
+// Verify that a Write() on a TLSSocket will pass through to Write()
+// invocations on ssl_socket_, handling partial writes correctly, and calls
+// the completion callback correctly.
+TEST_F(SocketTest, TestTLSSocketWrite) {
+ CompleteHandler handler;
+ net::CompletionCallback callback;
+
+ EXPECT_CALL(*ssl_socket_, Write(_, _, _)).Times(2).WillRepeatedly(
+ testing::DoAll(SaveArg<2>(&callback), Return(128)));
+ EXPECT_CALL(handler, OnComplete(_)).Times(1);
+
+ scoped_refptr<net::IOBufferWithSize> io_buffer(
+ new net::IOBufferWithSize(256));
+ socket->Write(
+ io_buffer.get(),
+ io_buffer->size(),
+ base::Bind(&CompleteHandler::OnComplete, base::Unretained(&handler)));
+}
+
+// Simulate a blocked Write, and verify that, when we simulate the Write going
+// through, the callback gets invoked.
+TEST_F(SocketTest, TestTLSSocketBlockedWrite) {
+ CompleteHandler handler;
+ net::CompletionCallback callback;
+
+ // Return ERR_IO_PENDING to say the Write()'s blocked. Save the |callback|
+ // Write()'s passed.
+ EXPECT_CALL(*ssl_socket_, Write(_, _, _)).Times(2).WillRepeatedly(
+ testing::DoAll(SaveArg<2>(&callback), Return(net::ERR_IO_PENDING)));
+
+ scoped_refptr<net::IOBufferWithSize> io_buffer(new net::IOBufferWithSize(42));
+ socket_->Write(
+ io_buffer.get(),
+ io_buffer->size(),
+ base::Bind(&CompleteHandler::OnComplete, base::Unretained(&handler)));
+
+ // After the simulated asynchronous writes come back (via calls to
+ // callback.Run()), hander's OnComplete should get invoked with the total
+ // amount written.
+ EXPECT_CALL(handler, OnComplete(42)).Times(1);
+ callback.Run(40);
+ callback.Run(2);
+}
+
+// Simulate multiple blocked Write()s.
+TEST_F(SocketTest, TestTLSSocketBlockedWriteReentry) {
+ const int kNumIOs = 5;
+ CompleteHandler handlers[kNumIOs];
+ net::CompletionCallback callback;
+ scoped_refptr<net::IOBufferWithSize> io_buffers[kNumIOs];
+
+ // The implemntation of TLSSocket::Write() is inherited from
Ryan Sleevi 2014/03/18 01:05:13 typo: implementation
lally 2014/03/26 03:17:51 Done.
+ // Socket::Write(), which implements an internal write queue that wraps
+ // TLSSocket::WriteImpl(). Each call from TLSSocket::WriteImpl() will invoke
+ // ssl_socket_'s Write(), (mocked here). Save the |callback| (we assume
+ // they will all be equivalent), and return ERR_IO_PENDING, to indicate a
+ // blocked request. The mocked Write() will get one request per Write()
Ryan Sleevi 2014/03/18 01:05:13 the mocked SSLClientSocket::Write() will get one r
+ // request we invoke on |socket_| below.
+ EXPECT_CALL(*ssl_socket_, Write(_, _, _)).Times(kNumIOs).WillRepeatedly(
+ testing::DoAll(SaveArg<2>(&callback), Return(net::ERR_IO_PENDING)));
+
+ // Send out |kNuMIOs| requests, each with a different size.
+ for (int i = 0; i < kNumIOs; i++) {
+ io_buffers[i] = new net::IOBufferWithSize(128 + i * 50);
+ scoped_refptr<net::IOBufferWithSize> io_buffer1(
+ new net::IOBufferWithSize(42));
+ socket_->Write(io_buffers[i].get(),
+ io_buffers[i]->size(),
+ base::Bind(&CompleteHandler::OnComplete,
+ base::Unretained(&handlers[i])));
+
+ // Set up expectations on all |kNumIOs| handlers.
+ EXPECT_CALL(handlers[i], OnComplete(io_buffers[i]->size())).Times(1);
+ }
+
+ // Finish each pending I/O. This should satisfy the expectations on the
+ // handlers.
+ for (int i = 0; i < kNumIOs; i++) {
+ callback.Run(128 + i * 50);
+ }
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698