OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "blimp/net/blimp_connection.h" | 5 #include "blimp/net/tcp_connection.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/callback_helpers.h" | 12 #include "base/callback_helpers.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
15 #include "blimp/common/create_blimp_message.h" | 15 #include "blimp/common/create_blimp_message.h" |
(...skipping 10 matching lines...) Expand all Loading... |
26 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
27 | 27 |
28 using testing::_; | 28 using testing::_; |
29 using testing::InSequence; | 29 using testing::InSequence; |
30 using testing::Return; | 30 using testing::Return; |
31 using testing::SaveArg; | 31 using testing::SaveArg; |
32 | 32 |
33 namespace blimp { | 33 namespace blimp { |
34 namespace { | 34 namespace { |
35 | 35 |
36 class BlimpConnectionTest : public testing::Test { | 36 class TCPConnectionTest : public testing::Test { |
37 public: | 37 public: |
38 BlimpConnectionTest() { | 38 TCPConnectionTest() { |
39 std::unique_ptr<MockPacketReader> mock_reader(new MockPacketReader); | 39 std::unique_ptr<MockPacketReader> mock_reader(new MockPacketReader); |
40 std::unique_ptr<MockPacketWriter> mock_writer(new MockPacketWriter); | 40 std::unique_ptr<MockPacketWriter> mock_writer(new MockPacketWriter); |
41 mock_reader_ = mock_reader.get(); | 41 mock_reader_ = mock_reader.get(); |
42 mock_writer_ = mock_writer.get(); | 42 mock_writer_ = mock_writer.get(); |
43 connection_ = | 43 connection_ = base::MakeUnique<TCPConnection>(base::MakeUnique<MessagePort>( |
44 base::MakeUnique<BlimpConnection>(base::MakeUnique<MessagePort>( | 44 std::move(mock_reader), std::move(mock_writer))); |
45 std::move(mock_reader), std::move(mock_writer))); | |
46 | 45 |
47 connection_->AddConnectionErrorObserver(&error_observer1_); | 46 connection_->AddConnectionErrorObserver(&error_observer1_); |
48 connection_->AddConnectionErrorObserver(&error_observer2_); | 47 connection_->AddConnectionErrorObserver(&error_observer2_); |
49 connection_->AddConnectionErrorObserver(&error_observer3_); | 48 connection_->AddConnectionErrorObserver(&error_observer3_); |
50 connection_->RemoveConnectionErrorObserver(&error_observer3_); | 49 connection_->RemoveConnectionErrorObserver(&error_observer3_); |
51 } | 50 } |
52 | 51 |
53 ~BlimpConnectionTest() override {} | 52 ~TCPConnectionTest() override {} |
54 | 53 |
55 void DropConnection() { connection_.reset(); } | 54 void DropConnection() { connection_.reset(); } |
56 | 55 |
57 protected: | 56 protected: |
58 std::unique_ptr<BlimpMessage> CreateInputMessage() { | 57 std::unique_ptr<BlimpMessage> CreateInputMessage() { |
59 InputMessage* input; | 58 InputMessage* input; |
60 return CreateBlimpMessage(&input); | 59 return CreateBlimpMessage(&input); |
61 } | 60 } |
62 | 61 |
63 std::unique_ptr<BlimpMessage> CreateControlMessage() { | 62 std::unique_ptr<BlimpMessage> CreateControlMessage() { |
64 TabControlMessage* control; | 63 TabControlMessage* control; |
65 return CreateBlimpMessage(&control); | 64 return CreateBlimpMessage(&control); |
66 } | 65 } |
67 | 66 |
68 base::MessageLoop message_loop_; | 67 base::MessageLoop message_loop_; |
69 MockPacketReader* mock_reader_; | 68 MockPacketReader* mock_reader_; |
70 MockPacketWriter* mock_writer_; | 69 MockPacketWriter* mock_writer_; |
71 testing::StrictMock<MockConnectionErrorObserver> error_observer1_; | 70 testing::StrictMock<MockConnectionErrorObserver> error_observer1_; |
72 testing::StrictMock<MockConnectionErrorObserver> error_observer2_; | 71 testing::StrictMock<MockConnectionErrorObserver> error_observer2_; |
73 | 72 |
74 // This error observer is Removed() immediately after it's added; | 73 // This error observer is Removed() immediately after it's added; |
75 // it should never be called. | 74 // it should never be called. |
76 testing::StrictMock<MockConnectionErrorObserver> error_observer3_; | 75 testing::StrictMock<MockConnectionErrorObserver> error_observer3_; |
77 | 76 |
78 testing::StrictMock<MockBlimpMessageProcessor> receiver_; | 77 testing::StrictMock<MockBlimpMessageProcessor> receiver_; |
79 std::unique_ptr<BlimpConnection> connection_; | 78 std::unique_ptr<BlimpConnection> connection_; |
80 }; | 79 }; |
81 | 80 |
82 // Write completes writing two packets asynchronously. | 81 // Write completes writing two packets asynchronously. |
83 TEST_F(BlimpConnectionTest, AsyncTwoPacketsWrite) { | 82 TEST_F(TCPConnectionTest, AsyncTwoPacketsWrite) { |
84 net::CompletionCallback write_packet_cb; | 83 net::CompletionCallback write_packet_cb; |
85 | 84 |
86 InSequence s; | 85 InSequence s; |
87 EXPECT_CALL(*mock_writer_, | 86 EXPECT_CALL(*mock_writer_, |
88 WritePacket(BufferEqualsProto(*CreateInputMessage()), _)) | 87 WritePacket(BufferEqualsProto(*CreateInputMessage()), _)) |
89 .WillOnce(SaveArg<1>(&write_packet_cb)) | 88 .WillOnce(SaveArg<1>(&write_packet_cb)) |
90 .RetiresOnSaturation(); | 89 .RetiresOnSaturation(); |
91 EXPECT_CALL(*mock_writer_, | 90 EXPECT_CALL(*mock_writer_, |
92 WritePacket(BufferEqualsProto(*CreateControlMessage()), _)) | 91 WritePacket(BufferEqualsProto(*CreateControlMessage()), _)) |
93 .WillOnce(SaveArg<1>(&write_packet_cb)) | 92 .WillOnce(SaveArg<1>(&write_packet_cb)) |
94 .RetiresOnSaturation(); | 93 .RetiresOnSaturation(); |
95 EXPECT_CALL(error_observer1_, OnConnectionError(_)).Times(0); | 94 EXPECT_CALL(error_observer1_, OnConnectionError(_)).Times(0); |
96 EXPECT_CALL(error_observer2_, OnConnectionError(_)).Times(0); | 95 EXPECT_CALL(error_observer2_, OnConnectionError(_)).Times(0); |
97 EXPECT_CALL(error_observer3_, OnConnectionError(_)).Times(0); | 96 EXPECT_CALL(error_observer3_, OnConnectionError(_)).Times(0); |
98 | 97 |
99 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor(); | 98 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor(); |
100 net::TestCompletionCallback complete_cb_1; | 99 net::TestCompletionCallback complete_cb_1; |
101 ASSERT_TRUE(write_packet_cb.is_null()); | 100 ASSERT_TRUE(write_packet_cb.is_null()); |
102 sender->ProcessMessage(CreateInputMessage(), | 101 sender->ProcessMessage(CreateInputMessage(), complete_cb_1.callback()); |
103 complete_cb_1.callback()); | |
104 ASSERT_FALSE(write_packet_cb.is_null()); | 102 ASSERT_FALSE(write_packet_cb.is_null()); |
105 base::ResetAndReturn(&write_packet_cb).Run(net::OK); | 103 base::ResetAndReturn(&write_packet_cb).Run(net::OK); |
106 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult()); | 104 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult()); |
107 | 105 |
108 net::TestCompletionCallback complete_cb_2; | 106 net::TestCompletionCallback complete_cb_2; |
109 ASSERT_TRUE(write_packet_cb.is_null()); | 107 ASSERT_TRUE(write_packet_cb.is_null()); |
110 sender->ProcessMessage(CreateControlMessage(), | 108 sender->ProcessMessage(CreateControlMessage(), complete_cb_2.callback()); |
111 complete_cb_2.callback()); | |
112 ASSERT_FALSE(write_packet_cb.is_null()); | 109 ASSERT_FALSE(write_packet_cb.is_null()); |
113 base::ResetAndReturn(&write_packet_cb).Run(net::OK); | 110 base::ResetAndReturn(&write_packet_cb).Run(net::OK); |
114 EXPECT_EQ(net::OK, complete_cb_2.WaitForResult()); | 111 EXPECT_EQ(net::OK, complete_cb_2.WaitForResult()); |
115 } | 112 } |
116 | 113 |
117 // Writer completes writing two packets asynchronously. | 114 // Writer completes writing two packets asynchronously. |
118 // First write succeeds, second fails. | 115 // First write succeeds, second fails. |
119 TEST_F(BlimpConnectionTest, AsyncTwoPacketsWriteWithError) { | 116 TEST_F(TCPConnectionTest, AsyncTwoPacketsWriteWithError) { |
120 net::CompletionCallback write_packet_cb; | 117 net::CompletionCallback write_packet_cb; |
121 | 118 |
122 InSequence s; | 119 InSequence s; |
123 EXPECT_CALL(*mock_writer_, | 120 EXPECT_CALL(*mock_writer_, |
124 WritePacket(BufferEqualsProto(*CreateInputMessage()), _)) | 121 WritePacket(BufferEqualsProto(*CreateInputMessage()), _)) |
125 .WillOnce(SaveArg<1>(&write_packet_cb)) | 122 .WillOnce(SaveArg<1>(&write_packet_cb)) |
126 .RetiresOnSaturation(); | 123 .RetiresOnSaturation(); |
127 EXPECT_CALL(*mock_writer_, | 124 EXPECT_CALL(*mock_writer_, |
128 WritePacket(BufferEqualsProto(*CreateControlMessage()), _)) | 125 WritePacket(BufferEqualsProto(*CreateControlMessage()), _)) |
129 .WillOnce(SaveArg<1>(&write_packet_cb)) | 126 .WillOnce(SaveArg<1>(&write_packet_cb)) |
130 .RetiresOnSaturation(); | 127 .RetiresOnSaturation(); |
131 EXPECT_CALL(error_observer1_, OnConnectionError(net::ERR_FAILED)); | 128 EXPECT_CALL(error_observer1_, OnConnectionError(net::ERR_FAILED)); |
132 EXPECT_CALL(error_observer2_, OnConnectionError(net::ERR_FAILED)); | 129 EXPECT_CALL(error_observer2_, OnConnectionError(net::ERR_FAILED)); |
133 EXPECT_CALL(error_observer3_, OnConnectionError(_)).Times(0); | 130 EXPECT_CALL(error_observer3_, OnConnectionError(_)).Times(0); |
134 | 131 |
135 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor(); | 132 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor(); |
136 net::TestCompletionCallback complete_cb_1; | 133 net::TestCompletionCallback complete_cb_1; |
137 sender->ProcessMessage(CreateInputMessage(), | 134 sender->ProcessMessage(CreateInputMessage(), complete_cb_1.callback()); |
138 complete_cb_1.callback()); | |
139 base::ResetAndReturn(&write_packet_cb).Run(net::OK); | 135 base::ResetAndReturn(&write_packet_cb).Run(net::OK); |
140 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult()); | 136 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult()); |
141 | 137 |
142 net::TestCompletionCallback complete_cb_2; | 138 net::TestCompletionCallback complete_cb_2; |
143 sender->ProcessMessage(CreateControlMessage(), | 139 sender->ProcessMessage(CreateControlMessage(), complete_cb_2.callback()); |
144 complete_cb_2.callback()); | |
145 base::ResetAndReturn(&write_packet_cb).Run(net::ERR_FAILED); | 140 base::ResetAndReturn(&write_packet_cb).Run(net::ERR_FAILED); |
146 EXPECT_EQ(net::ERR_FAILED, complete_cb_2.WaitForResult()); | 141 EXPECT_EQ(net::ERR_FAILED, complete_cb_2.WaitForResult()); |
147 } | 142 } |
148 | 143 |
149 TEST_F(BlimpConnectionTest, DeleteHappyObserversAreOK) { | 144 TEST_F(TCPConnectionTest, DeleteHappyObserversAreOK) { |
150 net::CompletionCallback write_packet_cb; | 145 net::CompletionCallback write_packet_cb; |
151 | 146 |
152 InSequence s; | 147 InSequence s; |
153 EXPECT_CALL(*mock_writer_, | 148 EXPECT_CALL(*mock_writer_, |
154 WritePacket(BufferEqualsProto(*CreateInputMessage()), _)) | 149 WritePacket(BufferEqualsProto(*CreateInputMessage()), _)) |
155 .WillOnce(SaveArg<1>(&write_packet_cb)) | 150 .WillOnce(SaveArg<1>(&write_packet_cb)) |
156 .RetiresOnSaturation(); | 151 .RetiresOnSaturation(); |
157 EXPECT_CALL(error_observer1_, OnConnectionError(net::ERR_FAILED)) | 152 EXPECT_CALL(error_observer1_, OnConnectionError(net::ERR_FAILED)) |
158 .WillOnce(testing::InvokeWithoutArgs( | 153 .WillOnce( |
159 this, &BlimpConnectionTest::DropConnection)); | 154 testing::InvokeWithoutArgs(this, &TCPConnectionTest::DropConnection)); |
160 | 155 |
161 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor(); | 156 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor(); |
162 net::TestCompletionCallback complete_cb_1; | 157 net::TestCompletionCallback complete_cb_1; |
163 sender->ProcessMessage(CreateInputMessage(), complete_cb_1.callback()); | 158 sender->ProcessMessage(CreateInputMessage(), complete_cb_1.callback()); |
164 base::ResetAndReturn(&write_packet_cb).Run(net::ERR_FAILED); | 159 base::ResetAndReturn(&write_packet_cb).Run(net::ERR_FAILED); |
165 EXPECT_EQ(net::ERR_FAILED, complete_cb_1.WaitForResult()); | 160 EXPECT_EQ(net::ERR_FAILED, complete_cb_1.WaitForResult()); |
166 } | 161 } |
167 | 162 |
168 // Verifies that a ReadPacket error causes ErrorObservers to be notified. | 163 // Verifies that a ReadPacket error causes ErrorObservers to be notified. |
169 TEST_F(BlimpConnectionTest, ReadPacketErrorInvokesErrorObservers) { | 164 TEST_F(TCPConnectionTest, ReadPacketErrorInvokesErrorObservers) { |
170 scoped_refptr<net::GrowableIOBuffer> read_packet_buffer; | 165 scoped_refptr<net::GrowableIOBuffer> read_packet_buffer; |
171 net::CompletionCallback read_packet_cb; | 166 net::CompletionCallback read_packet_cb; |
172 | 167 |
173 EXPECT_CALL(*mock_reader_, ReadPacket(_, _)) | 168 EXPECT_CALL(*mock_reader_, ReadPacket(_, _)) |
174 .WillOnce( | 169 .WillOnce( |
175 DoAll(SaveArg<0>(&read_packet_buffer), SaveArg<1>(&read_packet_cb))) | 170 DoAll(SaveArg<0>(&read_packet_buffer), SaveArg<1>(&read_packet_cb))) |
176 .RetiresOnSaturation(); | 171 .RetiresOnSaturation(); |
177 | 172 |
178 EXPECT_CALL(error_observer1_, OnConnectionError(net::ERR_FAILED)); | 173 EXPECT_CALL(error_observer1_, OnConnectionError(net::ERR_FAILED)); |
179 EXPECT_CALL(error_observer2_, OnConnectionError(net::ERR_FAILED)); | 174 EXPECT_CALL(error_observer2_, OnConnectionError(net::ERR_FAILED)); |
180 EXPECT_CALL(error_observer3_, OnConnectionError(_)).Times(0); | 175 EXPECT_CALL(error_observer3_, OnConnectionError(_)).Times(0); |
181 | 176 |
182 EXPECT_CALL(receiver_, MockableProcessMessage(_, _)).Times(0); | 177 EXPECT_CALL(receiver_, MockableProcessMessage(_, _)).Times(0); |
183 | 178 |
184 // Trigger the first ReadPacket() call by setting the MessageProcessor. | 179 // Trigger the first ReadPacket() call by setting the MessageProcessor. |
185 connection_->SetIncomingMessageProcessor(&receiver_); | 180 connection_->SetIncomingMessageProcessor(&receiver_); |
186 EXPECT_TRUE(read_packet_buffer); | 181 EXPECT_TRUE(read_packet_buffer); |
187 EXPECT_FALSE(read_packet_cb.is_null()); | 182 EXPECT_FALSE(read_packet_cb.is_null()); |
188 | 183 |
189 // Signal an error back from the ReadPacket operation. | 184 // Signal an error back from the ReadPacket operation. |
190 base::ResetAndReturn(&read_packet_cb).Run(net::ERR_FAILED); | 185 base::ResetAndReturn(&read_packet_cb).Run(net::ERR_FAILED); |
191 } | 186 } |
192 | 187 |
193 // Verifies that EndConnection messages received from the peer are | 188 // Verifies that EndConnection messages received from the peer are |
194 // routed through to registered ConnectionErrorObservers as errors. | 189 // routed through to registered ConnectionErrorObservers as errors. |
195 TEST_F(BlimpConnectionTest, EndConnectionInvokesErrorObservers) { | 190 TEST_F(TCPConnectionTest, EndConnectionInvokesErrorObservers) { |
196 scoped_refptr<net::GrowableIOBuffer> read_packet_buffer; | 191 scoped_refptr<net::GrowableIOBuffer> read_packet_buffer; |
197 net::CompletionCallback read_packet_cb; | 192 net::CompletionCallback read_packet_cb; |
198 | 193 |
199 EXPECT_CALL(*mock_reader_, ReadPacket(_, _)) | 194 EXPECT_CALL(*mock_reader_, ReadPacket(_, _)) |
200 .WillOnce( | 195 .WillOnce( |
201 DoAll(SaveArg<0>(&read_packet_buffer), SaveArg<1>(&read_packet_cb))) | 196 DoAll(SaveArg<0>(&read_packet_buffer), SaveArg<1>(&read_packet_cb))) |
202 .WillOnce(Return()) | 197 .WillOnce(Return()) |
203 .RetiresOnSaturation(); | 198 .RetiresOnSaturation(); |
204 | 199 |
205 EXPECT_CALL(error_observer1_, | 200 EXPECT_CALL(error_observer1_, |
(...skipping 15 matching lines...) Expand all Loading... |
221 | 216 |
222 // Put the EndConnection message in the buffer and invoke the read callback. | 217 // Put the EndConnection message in the buffer and invoke the read callback. |
223 read_packet_buffer->SetCapacity(message->ByteSize()); | 218 read_packet_buffer->SetCapacity(message->ByteSize()); |
224 ASSERT_TRUE(message->SerializeToArray(read_packet_buffer->data(), | 219 ASSERT_TRUE(message->SerializeToArray(read_packet_buffer->data(), |
225 message->GetCachedSize())); | 220 message->GetCachedSize())); |
226 base::ResetAndReturn(&read_packet_cb).Run(message->ByteSize()); | 221 base::ResetAndReturn(&read_packet_cb).Run(message->ByteSize()); |
227 } | 222 } |
228 | 223 |
229 } // namespace | 224 } // namespace |
230 } // namespace blimp | 225 } // namespace blimp |
OLD | NEW |