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/stl_util.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "net/base/net_errors.h"
14 #include "net/socket/socket.h"
15 #include "remoting/protocol/fake_session.h"
16 #include "remoting/protocol/message_reader.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "third_party/libjingle/source/talk/base/byteorder.h"
24 using testing::SaveArg
;
30 const char kTestMessage1
[] = "Message1";
31 const char kTestMessage2
[] = "Message2";
33 ACTION(CallDoneTask
) {
38 class MockMessageReceivedCallback
{
40 MOCK_METHOD1(OnMessage
, void(const base::Closure
&));
43 class MessageReaderTest
: public testing::Test
{
46 : in_callback_(false) {
49 // Following two methods are used by the ReadFromCallback test.
50 void AddSecondMessage(const base::Closure
& task
) {
51 AddMessage(kTestMessage2
);
57 void OnSecondMessage(const base::Closure
& task
) {
58 EXPECT_FALSE(in_callback_
);
62 // Used by the DeleteFromCallback() test.
63 void DeleteReader(const base::Closure
& task
) {
69 virtual void SetUp() OVERRIDE
{
70 reader_
.reset(new MessageReader());
73 virtual void TearDown() OVERRIDE
{
74 STLDeleteElements(&messages_
);
78 reader_
->Init(&socket_
, base::Bind(
79 &MessageReaderTest::OnMessage
, base::Unretained(this)));
82 void AddMessage(const std::string
& message
) {
83 std::string data
= std::string(4, ' ') + message
;
84 talk_base::SetBE32(const_cast<char*>(data
.data()), message
.size());
86 socket_
.AppendInputData(std::vector
<char>(data
.begin(), data
.end()));
89 bool CompareResult(CompoundBuffer
* buffer
, const std::string
& expected
) {
90 std::string
result(buffer
->total_bytes(), ' ');
91 buffer
->CopyTo(const_cast<char*>(result
.data()), result
.size());
92 return result
== expected
;
95 void OnMessage(scoped_ptr
<CompoundBuffer
> buffer
,
96 const base::Closure
& done_callback
) {
97 messages_
.push_back(buffer
.release());
98 callback_
.OnMessage(done_callback
);
101 base::MessageLoop message_loop_
;
102 scoped_ptr
<MessageReader
> reader_
;
104 MockMessageReceivedCallback callback_
;
105 std::vector
<CompoundBuffer
*> messages_
;
109 // Receive one message and process it with delay
110 TEST_F(MessageReaderTest
, OneMessage_Delay
) {
111 base::Closure done_task
;
113 AddMessage(kTestMessage1
);
115 EXPECT_CALL(callback_
, OnMessage(_
))
117 .WillOnce(SaveArg
<0>(&done_task
));
120 message_loop_
.RunUntilIdle();
122 Mock::VerifyAndClearExpectations(&callback_
);
123 Mock::VerifyAndClearExpectations(&socket_
);
125 EXPECT_TRUE(CompareResult(messages_
[0], kTestMessage1
));
127 // Verify that the reader starts reading again only after we've
128 // finished processing the previous message.
129 EXPECT_FALSE(socket_
.read_pending());
133 EXPECT_TRUE(socket_
.read_pending());
136 // Receive one message and process it instantly.
137 TEST_F(MessageReaderTest
, OneMessage_Instant
) {
138 AddMessage(kTestMessage1
);
140 EXPECT_CALL(callback_
, OnMessage(_
))
142 .WillOnce(CallDoneTask());
145 message_loop_
.RunUntilIdle();
147 EXPECT_TRUE(socket_
.read_pending());
148 EXPECT_EQ(1U, messages_
.size());
151 // Receive two messages in one packet.
152 TEST_F(MessageReaderTest
, TwoMessages_Together
) {
153 base::Closure done_task1
;
154 base::Closure done_task2
;
156 AddMessage(kTestMessage1
);
157 AddMessage(kTestMessage2
);
159 EXPECT_CALL(callback_
, OnMessage(_
))
161 .WillOnce(SaveArg
<0>(&done_task1
))
162 .WillOnce(SaveArg
<0>(&done_task2
));
165 message_loop_
.RunUntilIdle();
167 Mock::VerifyAndClearExpectations(&callback_
);
168 Mock::VerifyAndClearExpectations(&socket_
);
170 EXPECT_TRUE(CompareResult(messages_
[0], kTestMessage1
));
171 EXPECT_TRUE(CompareResult(messages_
[1], kTestMessage2
));
173 // Verify that the reader starts reading again only after we've
174 // finished processing the previous message.
175 EXPECT_FALSE(socket_
.read_pending());
178 message_loop_
.RunUntilIdle();
180 EXPECT_FALSE(socket_
.read_pending());
183 message_loop_
.RunUntilIdle();
185 EXPECT_TRUE(socket_
.read_pending());
188 // Receive two messages in one packet, and process the first one
190 TEST_F(MessageReaderTest
, TwoMessages_Instant
) {
191 base::Closure done_task2
;
193 AddMessage(kTestMessage1
);
194 AddMessage(kTestMessage2
);
196 EXPECT_CALL(callback_
, OnMessage(_
))
198 .WillOnce(CallDoneTask())
199 .WillOnce(SaveArg
<0>(&done_task2
));
202 message_loop_
.RunUntilIdle();
204 Mock::VerifyAndClearExpectations(&callback_
);
205 Mock::VerifyAndClearExpectations(&socket_
);
207 EXPECT_TRUE(CompareResult(messages_
[1], kTestMessage2
));
209 // Verify that the reader starts reading again only after we've
210 // finished processing the second message.
211 EXPECT_FALSE(socket_
.read_pending());
215 EXPECT_TRUE(socket_
.read_pending());
218 // Receive two messages in one packet, and process both of them
220 TEST_F(MessageReaderTest
, TwoMessages_Instant2
) {
221 AddMessage(kTestMessage1
);
222 AddMessage(kTestMessage2
);
224 EXPECT_CALL(callback_
, OnMessage(_
))
226 .WillOnce(CallDoneTask())
227 .WillOnce(CallDoneTask());
230 message_loop_
.RunUntilIdle();
232 EXPECT_TRUE(socket_
.read_pending());
235 // Receive two messages in separate packets.
236 TEST_F(MessageReaderTest
, TwoMessages_Separately
) {
237 base::Closure done_task
;
239 AddMessage(kTestMessage1
);
241 EXPECT_CALL(callback_
, OnMessage(_
))
243 .WillOnce(SaveArg
<0>(&done_task
));
246 message_loop_
.RunUntilIdle();
248 Mock::VerifyAndClearExpectations(&callback_
);
249 Mock::VerifyAndClearExpectations(&socket_
);
251 EXPECT_TRUE(CompareResult(messages_
[0], kTestMessage1
));
253 // Verify that the reader starts reading again only after we've
254 // finished processing the previous message.
255 EXPECT_FALSE(socket_
.read_pending());
258 message_loop_
.RunUntilIdle();
260 EXPECT_TRUE(socket_
.read_pending());
262 // Write another message and verify that we receive it.
263 EXPECT_CALL(callback_
, OnMessage(_
))
265 .WillOnce(SaveArg
<0>(&done_task
));
266 AddMessage(kTestMessage2
);
267 message_loop_
.RunUntilIdle();
269 EXPECT_TRUE(CompareResult(messages_
[1], kTestMessage2
));
271 // Verify that the reader starts reading again only after we've
272 // finished processing the previous message.
273 EXPECT_FALSE(socket_
.read_pending());
277 EXPECT_TRUE(socket_
.read_pending());
280 // Read() returns error.
281 TEST_F(MessageReaderTest
, ReadError
) {
282 socket_
.set_next_read_error(net::ERR_FAILED
);
284 // Add a message. It should never be read after the error above.
285 AddMessage(kTestMessage1
);
287 EXPECT_CALL(callback_
, OnMessage(_
))
293 // Verify that we the OnMessage callback is not reentered.
294 TEST_F(MessageReaderTest
, ReadFromCallback
) {
295 AddMessage(kTestMessage1
);
297 EXPECT_CALL(callback_
, OnMessage(_
))
299 .WillOnce(Invoke(this, &MessageReaderTest::AddSecondMessage
))
300 .WillOnce(Invoke(this, &MessageReaderTest::OnSecondMessage
));
303 message_loop_
.RunUntilIdle();
305 EXPECT_TRUE(socket_
.read_pending());
308 // Verify that we stop getting callbacks after deleting MessageReader.
309 TEST_F(MessageReaderTest
, DeleteFromCallback
) {
310 base::Closure done_task1
;
311 base::Closure done_task2
;
313 AddMessage(kTestMessage1
);
314 AddMessage(kTestMessage2
);
316 // OnMessage() should never be called for the second message.
317 EXPECT_CALL(callback_
, OnMessage(_
))
319 .WillOnce(Invoke(this, &MessageReaderTest::DeleteReader
));
322 message_loop_
.RunUntilIdle();
325 } // namespace protocol
326 } // namespace remoting