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
{ reader_
.reset(new MessageReader()); }
72 void TearDown() override
{ STLDeleteElements(&messages_
); }
75 reader_
->Init(&socket_
, base::Bind(
76 &MessageReaderTest::OnMessage
, base::Unretained(this)));
79 void AddMessage(const std::string
& message
) {
80 std::string data
= std::string(4, ' ') + message
;
81 rtc::SetBE32(const_cast<char*>(data
.data()), message
.size());
83 socket_
.AppendInputData(data
);
86 bool CompareResult(CompoundBuffer
* buffer
, const std::string
& expected
) {
87 std::string
result(buffer
->total_bytes(), ' ');
88 buffer
->CopyTo(const_cast<char*>(result
.data()), result
.size());
89 return result
== expected
;
92 void OnMessage(scoped_ptr
<CompoundBuffer
> buffer
,
93 const base::Closure
& done_callback
) {
94 messages_
.push_back(buffer
.release());
95 callback_
.OnMessage(done_callback
);
98 base::MessageLoop message_loop_
;
99 scoped_ptr
<MessageReader
> reader_
;
100 FakeStreamSocket socket_
;
101 MockMessageReceivedCallback callback_
;
102 std::vector
<CompoundBuffer
*> messages_
;
106 // Receive one message and process it with delay
107 TEST_F(MessageReaderTest
, OneMessage_Delay
) {
108 base::Closure done_task
;
110 AddMessage(kTestMessage1
);
112 EXPECT_CALL(callback_
, OnMessage(_
))
114 .WillOnce(SaveArg
<0>(&done_task
));
117 base::RunLoop().RunUntilIdle();
119 Mock::VerifyAndClearExpectations(&callback_
);
120 Mock::VerifyAndClearExpectations(&socket_
);
122 EXPECT_TRUE(CompareResult(messages_
[0], kTestMessage1
));
124 // Verify that the reader starts reading again only after we've
125 // finished processing the previous message.
126 EXPECT_FALSE(socket_
.read_pending());
130 EXPECT_TRUE(socket_
.read_pending());
133 // Receive one message and process it instantly.
134 TEST_F(MessageReaderTest
, OneMessage_Instant
) {
135 AddMessage(kTestMessage1
);
137 EXPECT_CALL(callback_
, OnMessage(_
))
139 .WillOnce(CallDoneTask());
142 base::RunLoop().RunUntilIdle();
144 EXPECT_TRUE(socket_
.read_pending());
145 EXPECT_EQ(1U, messages_
.size());
148 // Receive two messages in one packet.
149 TEST_F(MessageReaderTest
, TwoMessages_Together
) {
150 base::Closure done_task1
;
151 base::Closure done_task2
;
153 AddMessage(kTestMessage1
);
154 AddMessage(kTestMessage2
);
156 EXPECT_CALL(callback_
, OnMessage(_
))
158 .WillOnce(SaveArg
<0>(&done_task1
))
159 .WillOnce(SaveArg
<0>(&done_task2
));
162 base::RunLoop().RunUntilIdle();
164 Mock::VerifyAndClearExpectations(&callback_
);
165 Mock::VerifyAndClearExpectations(&socket_
);
167 EXPECT_TRUE(CompareResult(messages_
[0], kTestMessage1
));
168 EXPECT_TRUE(CompareResult(messages_
[1], kTestMessage2
));
170 // Verify that the reader starts reading again only after we've
171 // finished processing the previous message.
172 EXPECT_FALSE(socket_
.read_pending());
175 base::RunLoop().RunUntilIdle();
177 EXPECT_FALSE(socket_
.read_pending());
180 base::RunLoop().RunUntilIdle();
182 EXPECT_TRUE(socket_
.read_pending());
185 // Receive two messages in one packet, and process the first one
187 TEST_F(MessageReaderTest
, TwoMessages_Instant
) {
188 base::Closure done_task2
;
190 AddMessage(kTestMessage1
);
191 AddMessage(kTestMessage2
);
193 EXPECT_CALL(callback_
, OnMessage(_
))
195 .WillOnce(CallDoneTask())
196 .WillOnce(SaveArg
<0>(&done_task2
));
199 base::RunLoop().RunUntilIdle();
201 Mock::VerifyAndClearExpectations(&callback_
);
202 Mock::VerifyAndClearExpectations(&socket_
);
204 EXPECT_TRUE(CompareResult(messages_
[1], kTestMessage2
));
206 // Verify that the reader starts reading again only after we've
207 // finished processing the second message.
208 EXPECT_FALSE(socket_
.read_pending());
212 EXPECT_TRUE(socket_
.read_pending());
215 // Receive two messages in one packet, and process both of them
217 TEST_F(MessageReaderTest
, TwoMessages_Instant2
) {
218 AddMessage(kTestMessage1
);
219 AddMessage(kTestMessage2
);
221 EXPECT_CALL(callback_
, OnMessage(_
))
223 .WillOnce(CallDoneTask())
224 .WillOnce(CallDoneTask());
227 base::RunLoop().RunUntilIdle();
229 EXPECT_TRUE(socket_
.read_pending());
232 // Receive two messages in separate packets.
233 TEST_F(MessageReaderTest
, TwoMessages_Separately
) {
234 base::Closure done_task
;
236 AddMessage(kTestMessage1
);
238 EXPECT_CALL(callback_
, OnMessage(_
))
240 .WillOnce(SaveArg
<0>(&done_task
));
243 base::RunLoop().RunUntilIdle();
245 Mock::VerifyAndClearExpectations(&callback_
);
246 Mock::VerifyAndClearExpectations(&socket_
);
248 EXPECT_TRUE(CompareResult(messages_
[0], kTestMessage1
));
250 // Verify that the reader starts reading again only after we've
251 // finished processing the previous message.
252 EXPECT_FALSE(socket_
.read_pending());
255 base::RunLoop().RunUntilIdle();
257 EXPECT_TRUE(socket_
.read_pending());
259 // Write another message and verify that we receive it.
260 EXPECT_CALL(callback_
, OnMessage(_
))
262 .WillOnce(SaveArg
<0>(&done_task
));
263 AddMessage(kTestMessage2
);
264 base::RunLoop().RunUntilIdle();
266 EXPECT_TRUE(CompareResult(messages_
[1], kTestMessage2
));
268 // Verify that the reader starts reading again only after we've
269 // finished processing the previous message.
270 EXPECT_FALSE(socket_
.read_pending());
274 EXPECT_TRUE(socket_
.read_pending());
277 // Read() returns error.
278 TEST_F(MessageReaderTest
, ReadError
) {
279 socket_
.set_next_read_error(net::ERR_FAILED
);
281 // Add a message. It should never be read after the error above.
282 AddMessage(kTestMessage1
);
284 EXPECT_CALL(callback_
, OnMessage(_
))
290 // Verify that we the OnMessage callback is not reentered.
291 TEST_F(MessageReaderTest
, ReadFromCallback
) {
292 AddMessage(kTestMessage1
);
294 EXPECT_CALL(callback_
, OnMessage(_
))
296 .WillOnce(Invoke(this, &MessageReaderTest::AddSecondMessage
))
297 .WillOnce(Invoke(this, &MessageReaderTest::OnSecondMessage
));
300 base::RunLoop().RunUntilIdle();
302 EXPECT_TRUE(socket_
.read_pending());
305 // Verify that we stop getting callbacks after deleting MessageReader.
306 TEST_F(MessageReaderTest
, DeleteFromCallback
) {
307 base::Closure done_task1
;
308 base::Closure done_task2
;
310 AddMessage(kTestMessage1
);
311 AddMessage(kTestMessage2
);
313 // OnMessage() should never be called for the second message.
314 EXPECT_CALL(callback_
, OnMessage(_
))
316 .WillOnce(Invoke(this, &MessageReaderTest::DeleteReader
));
319 base::RunLoop().RunUntilIdle();
322 } // namespace protocol
323 } // namespace remoting