Chromium Code Reviews| 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..c4ccc76e0ea103680e3e3a004b37d40b47ebc7c4 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/socket/tls_socket_unittest.cc |
| @@ -0,0 +1,204 @@ |
| +// 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"; |
| + |
| +TEST(SocketTest, TestTLSSocketRead) { |
|
Ryan Sleevi
2014/02/24 20:06:49
Can you add comments to each of these (eg: on line
lally
2014/02/27 17:05:59
Done.
|
| + net::AddressList address_list; |
| + MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list); |
| + MockSSLClientSocket* ssl_socket = new MockSSLClientSocket; |
| + CompleteHandler handler; |
| + |
| + scoped_ptr<TLSSocket> socket( |
| + new TLSSocket(ssl_socket, tcp_client_socket, FAKE_ID)); |
| + |
| + 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))); |
| +} |
| + |
| +TEST(SocketTest, TestTLSSocketWrite) { |
| + net::AddressList address_list; |
| + MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list); |
| + MockSSLClientSocket* ssl_socket = new MockSSLClientSocket; |
| + CompleteHandler handler; |
| + |
| + scoped_ptr<TLSSocket> socket( |
| + new TLSSocket(ssl_socket, tcp_client_socket, FAKE_ID)); |
| + |
| + 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))); |
| +} |
| + |
| +TEST(SocketTest, TestTLSSocketBlockedWrite) { |
| + net::AddressList address_list; |
| + MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list); |
| + MockSSLClientSocket* ssl_socket = new MockSSLClientSocket; |
| + CompleteHandler handler; |
| + |
| + scoped_ptr<TLSSocket> socket( |
| + new TLSSocket(ssl_socket, tcp_client_socket, FAKE_ID)); |
|
Ryan Sleevi
2014/02/24 20:06:49
Given the redundancy of this setup logic, does it
lally
2014/02/27 17:05:59
Good point. Done.
|
| + |
| + net::CompletionCallback callback; |
| + 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))); |
| + |
| + // Good. Original call came back unable to complete. Now pretend the socket |
| + // finished, and confirm that we passed the error back. |
| + EXPECT_CALL(handler, OnComplete(42)).Times(1); |
| + callback.Run(40); |
| + callback.Run(2); |
| +} |
| + |
| +TEST(SocketTest, TestTLSSocketBlockedWriteReentry) { |
| + net::AddressList address_list; |
| + MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list); |
| + MockSSLClientSocket* ssl_socket = new MockSSLClientSocket; |
| + CompleteHandler handlers[5]; |
|
Ryan Sleevi
2014/02/24 20:06:49
magic numbers: What exists to guarantee that this
lally
2014/02/27 17:05:59
Documentation added, and the 5 has been replaced w
|
| + |
| + scoped_ptr<TLSSocket> socket( |
| + new TLSSocket(ssl_socket, tcp_client_socket, FAKE_ID)); |
| + |
| + net::CompletionCallback callback; |
| + EXPECT_CALL(*ssl_socket, Write(_, _, _)).Times(5).WillRepeatedly( |
| + testing::DoAll(SaveArg<2>(&callback), Return(net::ERR_IO_PENDING))); |
| + scoped_refptr<net::IOBufferWithSize> io_buffers[5]; |
| + int i; |
| + for (i = 0; i < 5; 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]))); |
| + |
| + EXPECT_CALL(handlers[i], OnComplete(io_buffers[i]->size())).Times(1); |
| + } |
| + |
| + for (i = 0; i < 5; i++) { |
| + callback.Run(128 + i * 50); |
| + } |
| +} |
| + |
| +} // namespace extensions |