OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/scoped_ptr.h" | 5 #include <memory> |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
6 #include "chrome/browser/extensions/api/socket/mock_tcp_client_socket.h" | 8 #include "chrome/browser/extensions/api/socket/mock_tcp_client_socket.h" |
7 #include "extensions/browser/api/socket/socket.h" | 9 #include "extensions/browser/api/socket/socket.h" |
8 #include "extensions/browser/api/socket/tcp_socket.h" | 10 #include "extensions/browser/api/socket/tcp_socket.h" |
9 #include "extensions/browser/api/socket/tls_socket.h" | 11 #include "extensions/browser/api/socket/tls_socket.h" |
10 #include "net/socket/stream_socket.h" | 12 #include "net/socket/stream_socket.h" |
11 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
13 | 15 |
14 namespace extensions { | 16 namespace extensions { |
15 | 17 |
16 const int kBufferLength = 10; | 18 const int kBufferLength = 10; |
17 | 19 |
18 template <typename T> | 20 template <typename T> |
19 scoped_ptr<T> CreateTestSocket(scoped_ptr<MockTCPClientSocket> stream); | 21 std::unique_ptr<T> CreateTestSocket( |
| 22 std::unique_ptr<MockTCPClientSocket> stream); |
20 | 23 |
21 template <> | 24 template <> |
22 scoped_ptr<TCPSocket> CreateTestSocket(scoped_ptr<MockTCPClientSocket> stream) { | 25 std::unique_ptr<TCPSocket> CreateTestSocket( |
23 return make_scoped_ptr(new TCPSocket(std::move(stream), "fake id", | 26 std::unique_ptr<MockTCPClientSocket> stream) { |
24 true /* is_connected */)); | 27 return base::WrapUnique( |
| 28 new TCPSocket(std::move(stream), "fake id", true /* is_connected */)); |
25 } | 29 } |
26 | 30 |
27 template <> | 31 template <> |
28 scoped_ptr<TLSSocket> CreateTestSocket(scoped_ptr<MockTCPClientSocket> stream) { | 32 std::unique_ptr<TLSSocket> CreateTestSocket( |
29 return make_scoped_ptr(new TLSSocket(std::move(stream), "fake id")); | 33 std::unique_ptr<MockTCPClientSocket> stream) { |
| 34 return base::WrapUnique(new TLSSocket(std::move(stream), "fake id")); |
30 } | 35 } |
31 | 36 |
32 class CombinedSocketTest : public testing::Test { | 37 class CombinedSocketTest : public testing::Test { |
33 public: | 38 public: |
34 CombinedSocketTest() : count_(0), io_buffer_(nullptr) {} | 39 CombinedSocketTest() : count_(0), io_buffer_(nullptr) {} |
35 | 40 |
36 // Strict test for synchronous (immediate) read behavior | 41 // Strict test for synchronous (immediate) read behavior |
37 template <typename T> | 42 template <typename T> |
38 void TestRead() { | 43 void TestRead() { |
39 net::IOBuffer* buffer = nullptr; | 44 net::IOBuffer* buffer = nullptr; |
40 | 45 |
41 scoped_ptr<MockTCPClientSocket> stream( | 46 std::unique_ptr<MockTCPClientSocket> stream( |
42 new testing::StrictMock<MockTCPClientSocket>()); | 47 new testing::StrictMock<MockTCPClientSocket>()); |
43 EXPECT_CALL(*stream, Read(testing::NotNull(), kBufferLength, testing::_)) | 48 EXPECT_CALL(*stream, Read(testing::NotNull(), kBufferLength, testing::_)) |
44 .WillOnce(DoAll(testing::SaveArg<0>(&buffer), | 49 .WillOnce(DoAll(testing::SaveArg<0>(&buffer), |
45 testing::Return(kBufferLength))); | 50 testing::Return(kBufferLength))); |
46 EXPECT_CALL(*stream, Disconnect()); | 51 EXPECT_CALL(*stream, Disconnect()); |
47 | 52 |
48 scoped_ptr<T> socket = CreateTestSocket<T>(std::move(stream)); | 53 std::unique_ptr<T> socket = CreateTestSocket<T>(std::move(stream)); |
49 ReadCompletionCallback read_callback = | 54 ReadCompletionCallback read_callback = |
50 base::Bind(&CombinedSocketTest::OnRead, base::Unretained(this)); | 55 base::Bind(&CombinedSocketTest::OnRead, base::Unretained(this)); |
51 socket->Read(kBufferLength, read_callback); | 56 socket->Read(kBufferLength, read_callback); |
52 EXPECT_EQ(kBufferLength, count_); | 57 EXPECT_EQ(kBufferLength, count_); |
53 EXPECT_NE(nullptr, buffer); | 58 EXPECT_NE(nullptr, buffer); |
54 EXPECT_EQ(buffer, io_buffer_); | 59 EXPECT_EQ(buffer, io_buffer_); |
55 } | 60 } |
56 | 61 |
57 // Strict test for async read behavior (read returns PENDING) | 62 // Strict test for async read behavior (read returns PENDING) |
58 template <typename T> | 63 template <typename T> |
59 void TestReadPending() { | 64 void TestReadPending() { |
60 net::IOBuffer* buffer = nullptr; | 65 net::IOBuffer* buffer = nullptr; |
61 net::CompletionCallback socket_cb; | 66 net::CompletionCallback socket_cb; |
62 | 67 |
63 scoped_ptr<MockTCPClientSocket> stream( | 68 std::unique_ptr<MockTCPClientSocket> stream( |
64 new testing::StrictMock<MockTCPClientSocket>()); | 69 new testing::StrictMock<MockTCPClientSocket>()); |
65 EXPECT_CALL(*stream, Read(testing::NotNull(), kBufferLength, testing::_)) | 70 EXPECT_CALL(*stream, Read(testing::NotNull(), kBufferLength, testing::_)) |
66 .WillOnce(DoAll(testing::SaveArg<0>(&buffer), | 71 .WillOnce(DoAll(testing::SaveArg<0>(&buffer), |
67 testing::SaveArg<2>(&socket_cb), | 72 testing::SaveArg<2>(&socket_cb), |
68 testing::Return(net::ERR_IO_PENDING))); | 73 testing::Return(net::ERR_IO_PENDING))); |
69 EXPECT_CALL(*stream, Disconnect()); | 74 EXPECT_CALL(*stream, Disconnect()); |
70 | 75 |
71 scoped_ptr<T> socket = CreateTestSocket<T>(std::move(stream)); | 76 std::unique_ptr<T> socket = CreateTestSocket<T>(std::move(stream)); |
72 ReadCompletionCallback read_callback = | 77 ReadCompletionCallback read_callback = |
73 base::Bind(&CombinedSocketTest::OnRead, base::Unretained(this)); | 78 base::Bind(&CombinedSocketTest::OnRead, base::Unretained(this)); |
74 socket->Read(kBufferLength, read_callback); | 79 socket->Read(kBufferLength, read_callback); |
75 EXPECT_EQ(0, count_); | 80 EXPECT_EQ(0, count_); |
76 EXPECT_EQ(nullptr, io_buffer_); | 81 EXPECT_EQ(nullptr, io_buffer_); |
77 socket_cb.Run(kBufferLength); | 82 socket_cb.Run(kBufferLength); |
78 EXPECT_EQ(kBufferLength, count_); | 83 EXPECT_EQ(kBufferLength, count_); |
79 EXPECT_NE(nullptr, buffer); | 84 EXPECT_NE(nullptr, buffer); |
80 EXPECT_EQ(buffer, io_buffer_); | 85 EXPECT_EQ(buffer, io_buffer_); |
81 } | 86 } |
82 | 87 |
83 // Even if the socket is closed, it may still have data left to read. | 88 // Even if the socket is closed, it may still have data left to read. |
84 template <typename T> | 89 template <typename T> |
85 void TestReadAfterDisconnect() { | 90 void TestReadAfterDisconnect() { |
86 net::IOBuffer* buffer = nullptr; | 91 net::IOBuffer* buffer = nullptr; |
87 | 92 |
88 scoped_ptr<MockTCPClientSocket> stream( | 93 std::unique_ptr<MockTCPClientSocket> stream( |
89 new testing::NiceMock<MockTCPClientSocket>()); | 94 new testing::NiceMock<MockTCPClientSocket>()); |
90 EXPECT_CALL(*stream, Read(testing::NotNull(), kBufferLength, testing::_)) | 95 EXPECT_CALL(*stream, Read(testing::NotNull(), kBufferLength, testing::_)) |
91 .WillOnce(DoAll(testing::SaveArg<0>(&buffer), | 96 .WillOnce(DoAll(testing::SaveArg<0>(&buffer), |
92 testing::Return(kBufferLength))); | 97 testing::Return(kBufferLength))); |
93 ON_CALL(*stream, IsConnected()).WillByDefault(testing::Return(false)); | 98 ON_CALL(*stream, IsConnected()).WillByDefault(testing::Return(false)); |
94 EXPECT_CALL(*stream, Disconnect()); | 99 EXPECT_CALL(*stream, Disconnect()); |
95 | 100 |
96 scoped_ptr<T> socket = CreateTestSocket<T>(std::move(stream)); | 101 std::unique_ptr<T> socket = CreateTestSocket<T>(std::move(stream)); |
97 ReadCompletionCallback read_callback = | 102 ReadCompletionCallback read_callback = |
98 base::Bind(&CombinedSocketTest::OnRead, base::Unretained(this)); | 103 base::Bind(&CombinedSocketTest::OnRead, base::Unretained(this)); |
99 socket->Read(kBufferLength, read_callback); | 104 socket->Read(kBufferLength, read_callback); |
100 EXPECT_EQ(kBufferLength, count_); | 105 EXPECT_EQ(kBufferLength, count_); |
101 EXPECT_NE(nullptr, buffer); | 106 EXPECT_NE(nullptr, buffer); |
102 EXPECT_EQ(buffer, io_buffer_); | 107 EXPECT_EQ(buffer, io_buffer_); |
103 } | 108 } |
104 | 109 |
105 void OnRead(int count, scoped_refptr<net::IOBuffer> io_buffer) { | 110 void OnRead(int count, scoped_refptr<net::IOBuffer> io_buffer) { |
106 count_ = count; | 111 count_ = count; |
(...skipping 23 matching lines...) Expand all Loading... |
130 | 135 |
131 TEST_F(CombinedSocketTest, TlsReadAfterDisconnect) { | 136 TEST_F(CombinedSocketTest, TlsReadAfterDisconnect) { |
132 TestReadAfterDisconnect<TLSSocket>(); | 137 TestReadAfterDisconnect<TLSSocket>(); |
133 } | 138 } |
134 | 139 |
135 TEST_F(CombinedSocketTest, TcpReadAfterDisconnect) { | 140 TEST_F(CombinedSocketTest, TcpReadAfterDisconnect) { |
136 TestReadAfterDisconnect<TCPSocket>(); | 141 TestReadAfterDisconnect<TCPSocket>(); |
137 } | 142 } |
138 | 143 |
139 } // namespace extensions | 144 } // namespace extensions |
OLD | NEW |