1 // Copyright (c) 2012 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.
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/stl_util.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "net/base/net_errors.h"
15 #include "net/socket/socket.h"
16 #include "remoting/protocol/fake_stream_socket.h"
17 #include "remoting/protocol/message_reader.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "third_party/webrtc/base/byteorder.h"
25 using testing::SaveArg
;
31 const char kTestMessage1
[] = "Message1";
32 const char kTestMessage2
[] = "Message2";
34 ACTION(CallDoneTask
) {
39 class MockMessageReceivedCallback
{
41 MOCK_METHOD1(OnMessage
, void(const base::Closure
&));
44 class MessageReaderTest
: public testing::Test
{
47 : in_callback_(false) {
50 // Following two methods are used by the ReadFromCallback test.
51 void AddSecondMessage(const base::Closure
& task
) {
52 AddMessage(kTestMessage2
);
58 void OnSecondMessage(const base::Closure
& task
) {
59 EXPECT_FALSE(in_callback_
);
63 // Used by the DeleteFromCallback() test.
64 void DeleteReader(const base::Closure
& task
) {
70 void SetUp() override
{
71 reader_
.reset(new MessageReader());
74 void TearDown() override
{ STLDeleteElements(&messages_
); }
77 reader_
->SetMessageReceivedCallback(
78 base::Bind(&MessageReaderTest::OnMessage
, base::Unretained(this)));
79 reader_
->StartReading(&socket_
, base::Bind(&MessageReaderTest::OnReadError
,
80 base::Unretained(this)));
83 void AddMessage(const std::string
& message
) {
84 std::string data
= std::string(4, ' ') + message
;
85 rtc::SetBE32(const_cast<char*>(data
.data()), message
.size());
87 socket_
.AppendInputData(data
);
90 bool CompareResult(CompoundBuffer
* buffer
, const std::string
& expected
) {
91 std::string
result(buffer
->total_bytes(), ' ');
92 buffer
->CopyTo(const_cast<char*>(result
.data()), result
.size());
93 return result
== expected
;
96 void OnReadError(int error
) {
101 void OnMessage(scoped_ptr
<CompoundBuffer
> buffer
,
102 const base::Closure
& done_callback
) {
103 messages_
.push_back(buffer
.release());
104 callback_
.OnMessage(done_callback
);
107 base::MessageLoop message_loop_
;
108 scoped_ptr
<MessageReader
> reader_
;
109 FakeStreamSocket socket_
;
110 MockMessageReceivedCallback callback_
;
112 std::vector
<CompoundBuffer
*> messages_
;
116 // Receive one message and process it with delay
117 TEST_F(MessageReaderTest
, OneMessage_Delay
) {
118 base::Closure done_task
;
120 AddMessage(kTestMessage1
);
122 EXPECT_CALL(callback_
, OnMessage(_
))
124 .WillOnce(SaveArg
<0>(&done_task
));
127 base::RunLoop().RunUntilIdle();
129 Mock::VerifyAndClearExpectations(&callback_
);
130 Mock::VerifyAndClearExpectations(&socket_
);
132 EXPECT_TRUE(CompareResult(messages_
[0], kTestMessage1
));
134 // Verify that the reader starts reading again only after we've
135 // finished processing the previous message.
136 EXPECT_FALSE(socket_
.read_pending());
140 EXPECT_TRUE(socket_
.read_pending());
143 // Receive one message and process it instantly.
144 TEST_F(MessageReaderTest
, OneMessage_Instant
) {
145 AddMessage(kTestMessage1
);
147 EXPECT_CALL(callback_
, OnMessage(_
))
149 .WillOnce(CallDoneTask());
152 base::RunLoop().RunUntilIdle();
154 EXPECT_TRUE(socket_
.read_pending());
155 EXPECT_EQ(1U, messages_
.size());
158 // Receive two messages in one packet.
159 TEST_F(MessageReaderTest
, TwoMessages_Together
) {
160 base::Closure done_task1
;
161 base::Closure done_task2
;
163 AddMessage(kTestMessage1
);
164 AddMessage(kTestMessage2
);
166 EXPECT_CALL(callback_
, OnMessage(_
))
168 .WillOnce(SaveArg
<0>(&done_task1
))
169 .WillOnce(SaveArg
<0>(&done_task2
));
172 base::RunLoop().RunUntilIdle();
174 Mock::VerifyAndClearExpectations(&callback_
);
175 Mock::VerifyAndClearExpectations(&socket_
);
177 EXPECT_TRUE(CompareResult(messages_
[0], kTestMessage1
));
178 EXPECT_TRUE(CompareResult(messages_
[1], kTestMessage2
));
180 // Verify that the reader starts reading again only after we've
181 // finished processing the previous message.
182 EXPECT_FALSE(socket_
.read_pending());
185 base::RunLoop().RunUntilIdle();
187 EXPECT_FALSE(socket_
.read_pending());
190 base::RunLoop().RunUntilIdle();
192 EXPECT_TRUE(socket_
.read_pending());
195 // Receive two messages in one packet, and process the first one
197 TEST_F(MessageReaderTest
, TwoMessages_Instant
) {
198 base::Closure done_task2
;
200 AddMessage(kTestMessage1
);
201 AddMessage(kTestMessage2
);
203 EXPECT_CALL(callback_
, OnMessage(_
))
205 .WillOnce(CallDoneTask())
206 .WillOnce(SaveArg
<0>(&done_task2
));
209 base::RunLoop().RunUntilIdle();
211 Mock::VerifyAndClearExpectations(&callback_
);
212 Mock::VerifyAndClearExpectations(&socket_
);
214 EXPECT_TRUE(CompareResult(messages_
[1], kTestMessage2
));
216 // Verify that the reader starts reading again only after we've
217 // finished processing the second message.
218 EXPECT_FALSE(socket_
.read_pending());
222 EXPECT_TRUE(socket_
.read_pending());
225 // Receive two messages in one packet, and process both of them
227 TEST_F(MessageReaderTest
, TwoMessages_Instant2
) {
228 AddMessage(kTestMessage1
);
229 AddMessage(kTestMessage2
);
231 EXPECT_CALL(callback_
, OnMessage(_
))
233 .WillOnce(CallDoneTask())
234 .WillOnce(CallDoneTask());
237 base::RunLoop().RunUntilIdle();
239 EXPECT_TRUE(socket_
.read_pending());
242 // Receive two messages in separate packets.
243 TEST_F(MessageReaderTest
, TwoMessages_Separately
) {
244 base::Closure done_task
;
246 AddMessage(kTestMessage1
);
248 EXPECT_CALL(callback_
, OnMessage(_
))
250 .WillOnce(SaveArg
<0>(&done_task
));
253 base::RunLoop().RunUntilIdle();
255 Mock::VerifyAndClearExpectations(&callback_
);
256 Mock::VerifyAndClearExpectations(&socket_
);
258 EXPECT_TRUE(CompareResult(messages_
[0], kTestMessage1
));
260 // Verify that the reader starts reading again only after we've
261 // finished processing the previous message.
262 EXPECT_FALSE(socket_
.read_pending());
265 base::RunLoop().RunUntilIdle();
267 EXPECT_TRUE(socket_
.read_pending());
269 // Write another message and verify that we receive it.
270 EXPECT_CALL(callback_
, OnMessage(_
))
272 .WillOnce(SaveArg
<0>(&done_task
));
273 AddMessage(kTestMessage2
);
274 base::RunLoop().RunUntilIdle();
276 EXPECT_TRUE(CompareResult(messages_
[1], kTestMessage2
));
278 // Verify that the reader starts reading again only after we've
279 // finished processing the previous message.
280 EXPECT_FALSE(socket_
.read_pending());
284 EXPECT_TRUE(socket_
.read_pending());
287 // Read() returns error.
288 TEST_F(MessageReaderTest
, ReadError
) {
289 socket_
.AppendReadError(net::ERR_FAILED
);
291 EXPECT_CALL(callback_
, OnMessage(_
)).Times(0);
295 EXPECT_EQ(net::ERR_FAILED
, read_error_
);
296 EXPECT_FALSE(reader_
);
299 // Verify that we the OnMessage callback is not reentered.
300 TEST_F(MessageReaderTest
, ReadFromCallback
) {
301 AddMessage(kTestMessage1
);
303 EXPECT_CALL(callback_
, OnMessage(_
))
305 .WillOnce(Invoke(this, &MessageReaderTest::AddSecondMessage
))
306 .WillOnce(Invoke(this, &MessageReaderTest::OnSecondMessage
));
309 base::RunLoop().RunUntilIdle();
311 EXPECT_TRUE(socket_
.read_pending());
314 // Verify that we stop getting callbacks after deleting MessageReader.
315 TEST_F(MessageReaderTest
, DeleteFromCallback
) {
316 base::Closure done_task1
;
317 base::Closure done_task2
;
319 AddMessage(kTestMessage1
);
320 AddMessage(kTestMessage2
);
322 // OnMessage() should never be called for the second message.
323 EXPECT_CALL(callback_
, OnMessage(_
))
325 .WillOnce(Invoke(this, &MessageReaderTest::DeleteReader
));
328 base::RunLoop().RunUntilIdle();
331 } // namespace protocol
332 } // namespace remoting