forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Convert Extensions TCP & TLS Socket APIs to mojo sockets"
This reverts commit 41066e8. Reason for revert: This CL broke an internal app. See crbug.com/865958 Original change's description: > Convert Extensions TCP & TLS Socket APIs to mojo sockets > > This CL converts extensions TCP and TLS socket apis to network service's mojo > socket. > > - Remove combined_socket_unittest.cc because the test cases no longer apply. I > tried to write equivalent ones in tcp_socket_unittest.cc and > tls_socket_unittest.cc > - Add a ContentUtilityClient implementation for extensions_browsertests, so we > can inject a NetworkServiceTestHelper to the utility process in which network > service runs. > > Bug: 721401 > Cq-Include-Trybots: luci.chromium.try:linux_chromium_dbg_ng;master.tryserver.chromium.linux:linux_mojo > Change-Id: I333a3021a5c66eb94618be6830ba0ff68c442eba > Reviewed-on: https://chromium-review.googlesource.com/1070434 > Commit-Queue: Helen Li <xunjieli@chromium.org> > Reviewed-by: Maks Orlovich <morlovich@chromium.org> > Reviewed-by: John Abd-El-Malek <jam@chromium.org> > Reviewed-by: Reilly Grant <reillyg@chromium.org> > Cr-Commit-Position: refs/heads/master@{#563390} TBR=jam@chromium.org,reillyg@chromium.org,xunjieli@chromium.org,morlovich@chromium.org # Not skipping CQ checks because original CL landed > 1 day ago. Bug: 721401, 865958 Change-Id: I9e9408f31887ab45d0bd967e2f17f91c465836c6 Cq-Include-Trybots: luci.chromium.try:linux_chromium_dbg_ng;luci.chromium.try:linux_mojo Reviewed-on: https://chromium-review.googlesource.com/1145340 Commit-Queue: Helen Li <xunjieli@chromium.org> Reviewed-by: Helen Li <xunjieli@chromium.org> Cr-Commit-Position: refs/heads/master@{#576982}
- Loading branch information
Helen Li
authored and
Commit Bot
committed
Jul 20, 2018
1 parent
756d07e
commit 494108a
Showing
27 changed files
with
1,470 additions
and
2,236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
chrome/browser/extensions/api/socket/combined_socket_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
// Copyright 2016 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 <memory> | ||
|
||
#include "chrome/browser/extensions/api/socket/mock_tcp_client_socket.h" | ||
#include "extensions/browser/api/socket/socket.h" | ||
#include "extensions/browser/api/socket/tcp_socket.h" | ||
#include "extensions/browser/api/socket/tls_socket.h" | ||
#include "net/socket/stream_socket.h" | ||
#include "testing/gmock/include/gmock/gmock.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace extensions { | ||
|
||
const int kBufferLength = 10; | ||
|
||
template <typename T> | ||
std::unique_ptr<T> CreateTestSocket( | ||
std::unique_ptr<MockTCPClientSocket> stream); | ||
|
||
template <> | ||
std::unique_ptr<TCPSocket> CreateTestSocket( | ||
std::unique_ptr<MockTCPClientSocket> stream) { | ||
return std::make_unique<TCPSocket>(std::move(stream), "fake id", | ||
true /* is_connected */); | ||
} | ||
|
||
template <> | ||
std::unique_ptr<TLSSocket> CreateTestSocket( | ||
std::unique_ptr<MockTCPClientSocket> stream) { | ||
return std::make_unique<TLSSocket>(std::move(stream), "fake id"); | ||
} | ||
|
||
class CombinedSocketTest : public testing::Test { | ||
public: | ||
CombinedSocketTest() : count_(0), io_buffer_(nullptr) {} | ||
|
||
// Strict test for synchronous (immediate) read behavior | ||
template <typename T> | ||
void TestRead() { | ||
net::IOBuffer* buffer = nullptr; | ||
|
||
std::unique_ptr<MockTCPClientSocket> stream( | ||
new testing::StrictMock<MockTCPClientSocket>()); | ||
EXPECT_CALL(*stream, Read(testing::NotNull(), kBufferLength, testing::_)) | ||
.WillOnce(DoAll(testing::SaveArg<0>(&buffer), | ||
testing::Return(kBufferLength))); | ||
EXPECT_CALL(*stream, Disconnect()); | ||
|
||
std::unique_ptr<T> socket = CreateTestSocket<T>(std::move(stream)); | ||
ReadCompletionCallback read_callback = | ||
base::Bind(&CombinedSocketTest::OnRead, base::Unretained(this)); | ||
socket->Read(kBufferLength, read_callback); | ||
EXPECT_EQ(kBufferLength, count_); | ||
EXPECT_NE(nullptr, buffer); | ||
EXPECT_EQ(buffer, io_buffer_); | ||
} | ||
|
||
// Strict test for async read behavior (read returns PENDING) | ||
template <typename T> | ||
void TestReadPending() { | ||
net::IOBuffer* buffer = nullptr; | ||
net::CompletionCallback socket_cb; | ||
|
||
std::unique_ptr<MockTCPClientSocket> stream( | ||
new testing::StrictMock<MockTCPClientSocket>()); | ||
EXPECT_CALL(*stream, Read(testing::NotNull(), kBufferLength, testing::_)) | ||
.WillOnce(DoAll(testing::SaveArg<0>(&buffer), | ||
testing::SaveArg<2>(&socket_cb), | ||
testing::Return(net::ERR_IO_PENDING))); | ||
EXPECT_CALL(*stream, Disconnect()); | ||
|
||
std::unique_ptr<T> socket = CreateTestSocket<T>(std::move(stream)); | ||
ReadCompletionCallback read_callback = | ||
base::Bind(&CombinedSocketTest::OnRead, base::Unretained(this)); | ||
socket->Read(kBufferLength, read_callback); | ||
EXPECT_EQ(0, count_); | ||
EXPECT_EQ(nullptr, io_buffer_); | ||
socket_cb.Run(kBufferLength); | ||
EXPECT_EQ(kBufferLength, count_); | ||
EXPECT_NE(nullptr, buffer); | ||
EXPECT_EQ(buffer, io_buffer_); | ||
} | ||
|
||
// Even if the socket is closed, it may still have data left to read. | ||
template <typename T> | ||
void TestReadAfterDisconnect() { | ||
net::IOBuffer* buffer = nullptr; | ||
|
||
std::unique_ptr<MockTCPClientSocket> stream( | ||
new testing::NiceMock<MockTCPClientSocket>()); | ||
EXPECT_CALL(*stream, Read(testing::NotNull(), kBufferLength, testing::_)) | ||
.WillOnce(DoAll(testing::SaveArg<0>(&buffer), | ||
testing::Return(kBufferLength))); | ||
ON_CALL(*stream, IsConnected()).WillByDefault(testing::Return(false)); | ||
EXPECT_CALL(*stream, Disconnect()); | ||
|
||
std::unique_ptr<T> socket = CreateTestSocket<T>(std::move(stream)); | ||
ReadCompletionCallback read_callback = | ||
base::Bind(&CombinedSocketTest::OnRead, base::Unretained(this)); | ||
socket->Read(kBufferLength, read_callback); | ||
EXPECT_EQ(kBufferLength, count_); | ||
EXPECT_NE(nullptr, buffer); | ||
EXPECT_EQ(buffer, io_buffer_); | ||
} | ||
|
||
void OnRead(int count, | ||
scoped_refptr<net::IOBuffer> io_buffer, | ||
bool socket_destroying) { | ||
count_ = count; | ||
io_buffer_ = io_buffer.get(); | ||
} | ||
|
||
protected: | ||
int count_; | ||
net::IOBuffer* io_buffer_; | ||
}; | ||
|
||
TEST_F(CombinedSocketTest, TlsRead) { | ||
TestRead<TLSSocket>(); | ||
} | ||
|
||
TEST_F(CombinedSocketTest, TcpRead) { | ||
TestRead<TCPSocket>(); | ||
} | ||
|
||
TEST_F(CombinedSocketTest, TlsReadPending) { | ||
TestReadPending<TLSSocket>(); | ||
} | ||
|
||
TEST_F(CombinedSocketTest, TcpReadPending) { | ||
TestReadPending<TCPSocket>(); | ||
} | ||
|
||
TEST_F(CombinedSocketTest, TlsReadAfterDisconnect) { | ||
TestReadAfterDisconnect<TLSSocket>(); | ||
} | ||
|
||
TEST_F(CombinedSocketTest, TcpReadAfterDisconnect) { | ||
TestReadAfterDisconnect<TCPSocket>(); | ||
} | ||
|
||
} // namespace extensions |
82 changes: 82 additions & 0 deletions
82
chrome/browser/extensions/api/socket/mock_tcp_client_socket.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2016 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. | ||
|
||
#ifndef CHROME_BROWSER_EXTENSIONS_API_SOCKET_MOCK_TCP_CLIENT_SOCKET_H_ | ||
#define CHROME_BROWSER_EXTENSIONS_API_SOCKET_MOCK_TCP_CLIENT_SOCKET_H_ | ||
|
||
#include "base/callback_helpers.h" | ||
#include "net/log/net_log_source.h" | ||
#include "net/log/net_log_with_source.h" | ||
#include "net/socket/tcp_client_socket.h" | ||
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h" | ||
#include "testing/gmock/include/gmock/gmock.h" | ||
|
||
namespace extensions { | ||
class MockTCPClientSocket : public net::TCPClientSocket { | ||
public: | ||
MockTCPClientSocket(); | ||
~MockTCPClientSocket() override; | ||
|
||
int Read(net::IOBuffer* buffer, | ||
int bytes, | ||
net::CompletionOnceCallback callback) override { | ||
return Read(buffer, bytes, | ||
base::AdaptCallbackForRepeating(std::move(callback))); | ||
} | ||
|
||
int Write(net::IOBuffer* buffer, | ||
int bytes, | ||
net::CompletionOnceCallback callback, | ||
const net::NetworkTrafficAnnotationTag& tag) override { | ||
return Write(buffer, bytes, | ||
base::AdaptCallbackForRepeating(std::move(callback)), tag); | ||
} | ||
|
||
int Connect(net::CompletionOnceCallback callback) override { | ||
return Connect(base::AdaptCallbackForRepeating(std::move(callback))); | ||
} | ||
|
||
MOCK_METHOD3(Read, int(net::IOBuffer*, int, const net::CompletionCallback&)); | ||
MOCK_METHOD4(Write, | ||
int(net::IOBuffer*, | ||
int, | ||
const net::CompletionCallback&, | ||
const net::NetworkTrafficAnnotationTag&)); | ||
MOCK_METHOD1(SetReceiveBufferSize, int(int32_t)); | ||
MOCK_METHOD1(SetSendBufferSize, int(int32_t)); | ||
MOCK_METHOD1(Connect, int(const net::CompletionCallback&)); | ||
MOCK_METHOD0(Disconnect, void()); | ||
MOCK_CONST_METHOD0(IsConnected, bool()); | ||
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::NetLogWithSource&()); | ||
MOCK_CONST_METHOD0(WasEverUsed, bool()); | ||
MOCK_CONST_METHOD0(UsingTCPFastOpen, bool()); | ||
MOCK_CONST_METHOD0(NumBytesRead, int64_t()); | ||
MOCK_CONST_METHOD0(GetConnectTimeMicros, base::TimeDelta()); | ||
MOCK_CONST_METHOD0(WasAlpnNegotiated, bool()); | ||
MOCK_CONST_METHOD0(GetNegotiatedProtocol, net::NextProto()); | ||
MOCK_METHOD1(GetSSLInfo, bool(net::SSLInfo*)); | ||
MOCK_CONST_METHOD1(GetConnectionAttempts, void(net::ConnectionAttempts*)); | ||
MOCK_METHOD0(ClearConnectionAttempts, void()); | ||
MOCK_METHOD1(AddConnectionAttempts, void(const net::ConnectionAttempts&)); | ||
MOCK_CONST_METHOD0(GetTotalReceivedBytes, int64_t()); | ||
|
||
// Methods specific to MockTCPClientSocket | ||
MOCK_METHOD1(Bind, int(const net::IPEndPoint&)); | ||
MOCK_METHOD2(SetKeepAlive, bool(bool, int)); | ||
MOCK_METHOD1(SetNoDelay, bool(bool)); | ||
}; | ||
|
||
MockTCPClientSocket::MockTCPClientSocket() | ||
: TCPClientSocket(net::AddressList(), | ||
nullptr, | ||
nullptr, | ||
net::NetLogSource()) {} | ||
MockTCPClientSocket::~MockTCPClientSocket() {} | ||
|
||
} // namespace extensions | ||
|
||
#endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_MOCK_TCP_CLIENT_SOCKET_H_ |
Oops, something went wrong.