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_session.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/libjingle/source/talk/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 virtual void SetUp() OVERRIDE
{
71 reader_
.reset(new MessageReader());
74 virtual void TearDown() OVERRIDE
{
75 STLDeleteElements(&messages_
);
79 reader_
->Init(&socket_
, base::Bind(
80 &MessageReaderTest::OnMessage
, base::Unretained(this)));
83 void AddMessage(const std::string
& message
) {
84 std::string data
= std::string(4, ' ') + message
;
85 talk_base::SetBE32(const_cast<char*>(data
.data()), message
.size());
87 socket_
.AppendInputData(std::vector
<char>(data
.begin(), data
.end()));
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 OnMessage(scoped_ptr
<CompoundBuffer
> buffer
,
97 const base::Closure
& done_callback
) {
98 messages_
.push_back(buffer
.release());
99 callback_
.OnMessage(done_callback
);
102 base::MessageLoop message_loop_
;
103 scoped_ptr
<MessageReader
> reader_
;
105 MockMessageReceivedCallback callback_
;
106 std::vector
<CompoundBuffer
*> messages_
;
110 // Receive one message and process it with delay
111 TEST_F(MessageReaderTest
, OneMessage_Delay
) {
112 base::Closure done_task
;
114 AddMessage(kTestMessage1
);
116 EXPECT_CALL(callback_
, OnMessage(_
))
118 .WillOnce(SaveArg
<0>(&done_task
));
121 base::RunLoop().RunUntilIdle();
123 Mock::VerifyAndClearExpectations(&callback_
);
124 Mock::VerifyAndClearExpectations(&socket_
);
126 EXPECT_TRUE(CompareResult(messages_
[0], kTestMessage1
));
128 // Verify that the reader starts reading again only after we've
129 // finished processing the previous message.
130 EXPECT_FALSE(socket_
.read_pending());
134 EXPECT_TRUE(socket_
.read_pending());
137 // Receive one message and process it instantly.
138 TEST_F(MessageReaderTest
, OneMessage_Instant
) {
139 AddMessage(kTestMessage1
);
141 EXPECT_CALL(callback_
, OnMessage(_
))
143 .WillOnce(CallDoneTask());
146 base::RunLoop().RunUntilIdle();
148 EXPECT_TRUE(socket_
.read_pending());
149 EXPECT_EQ(1U, messages_
.size());
152 // Receive two messages in one packet.
153 TEST_F(MessageReaderTest
, TwoMessages_Together
) {
154 base::Closure done_task1
;
155 base::Closure done_task2
;
157 AddMessage(kTestMessage1
);
158 AddMessage(kTestMessage2
);
160 EXPECT_CALL(callback_
, OnMessage(_
))
162 .WillOnce(SaveArg
<0>(&done_task1
))
163 .WillOnce(SaveArg
<0>(&done_task2
));
166 base::RunLoop().RunUntilIdle();
168 Mock::VerifyAndClearExpectations(&callback_
);
169 Mock::VerifyAndClearExpectations(&socket_
);
171 EXPECT_TRUE(CompareResult(messages_
[0], kTestMessage1
));
172 EXPECT_TRUE(CompareResult(messages_
[1], kTestMessage2
));
174 // Verify that the reader starts reading again only after we've
175 // finished processing the previous message.
176 EXPECT_FALSE(socket_
.read_pending());
179 base::RunLoop().RunUntilIdle();
181 EXPECT_FALSE(socket_
.read_pending());
184 base::RunLoop().RunUntilIdle();
186 EXPECT_TRUE(socket_
.read_pending());
189 // Receive two messages in one packet, and process the first one
191 TEST_F(MessageReaderTest
, TwoMessages_Instant
) {
192 base::Closure done_task2
;
194 AddMessage(kTestMessage1
);
195 AddMessage(kTestMessage2
);
197 EXPECT_CALL(callback_
, OnMessage(_
))
199 .WillOnce(CallDoneTask())
200 .WillOnce(SaveArg
<0>(&done_task2
));
203 base::RunLoop().RunUntilIdle();
205 Mock::VerifyAndClearExpectations(&callback_
);
206 Mock::VerifyAndClearExpectations(&socket_
);
208 EXPECT_TRUE(CompareResult(messages_
[1], kTestMessage2
));
210 // Verify that the reader starts reading again only after we've
211 // finished processing the second message.
212 EXPECT_FALSE(socket_
.read_pending());
216 EXPECT_TRUE(socket_
.read_pending());
219 // Receive two messages in one packet, and process both of them
221 TEST_F(MessageReaderTest
, TwoMessages_Instant2
) {
222 AddMessage(kTestMessage1
);
223 AddMessage(kTestMessage2
);
225 EXPECT_CALL(callback_
, OnMessage(_
))
227 .WillOnce(CallDoneTask())
228 .WillOnce(CallDoneTask());
231 base::RunLoop().RunUntilIdle();
233 EXPECT_TRUE(socket_
.read_pending());
236 // Receive two messages in separate packets.
237 TEST_F(MessageReaderTest
, TwoMessages_Separately
) {
238 base::Closure done_task
;
240 AddMessage(kTestMessage1
);
242 EXPECT_CALL(callback_
, OnMessage(_
))
244 .WillOnce(SaveArg
<0>(&done_task
));
247 base::RunLoop().RunUntilIdle();
249 Mock::VerifyAndClearExpectations(&callback_
);
250 Mock::VerifyAndClearExpectations(&socket_
);
252 EXPECT_TRUE(CompareResult(messages_
[0], kTestMessage1
));
254 // Verify that the reader starts reading again only after we've
255 // finished processing the previous message.
256 EXPECT_FALSE(socket_
.read_pending());
259 base::RunLoop().RunUntilIdle();
261 EXPECT_TRUE(socket_
.read_pending());
263 // Write another message and verify that we receive it.
264 EXPECT_CALL(callback_
, OnMessage(_
))
266 .WillOnce(SaveArg
<0>(&done_task
));
267 AddMessage(kTestMessage2
);
268 base::RunLoop().RunUntilIdle();
270 EXPECT_TRUE(CompareResult(messages_
[1], kTestMessage2
));
272 // Verify that the reader starts reading again only after we've
273 // finished processing the previous message.
274 EXPECT_FALSE(socket_
.read_pending());
278 EXPECT_TRUE(socket_
.read_pending());
281 // Read() returns error.
282 TEST_F(MessageReaderTest
, ReadError
) {
283 socket_
.set_next_read_error(net::ERR_FAILED
);
285 // Add a message. It should never be read after the error above.
286 AddMessage(kTestMessage1
);
288 EXPECT_CALL(callback_
, OnMessage(_
))
294 // Verify that we the OnMessage callback is not reentered.
295 TEST_F(MessageReaderTest
, ReadFromCallback
) {
296 AddMessage(kTestMessage1
);
298 EXPECT_CALL(callback_
, OnMessage(_
))
300 .WillOnce(Invoke(this, &MessageReaderTest::AddSecondMessage
))
301 .WillOnce(Invoke(this, &MessageReaderTest::OnSecondMessage
));
304 base::RunLoop().RunUntilIdle();
306 EXPECT_TRUE(socket_
.read_pending());
309 // Verify that we stop getting callbacks after deleting MessageReader.
310 TEST_F(MessageReaderTest
, DeleteFromCallback
) {
311 base::Closure done_task1
;
312 base::Closure done_task2
;
314 AddMessage(kTestMessage1
);
315 AddMessage(kTestMessage2
);
317 // OnMessage() should never be called for the second message.
318 EXPECT_CALL(callback_
, OnMessage(_
))
320 .WillOnce(Invoke(this, &MessageReaderTest::DeleteReader
));
323 base::RunLoop().RunUntilIdle();
326 } // namespace protocol
327 } // namespace remoting