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