Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / remoting / protocol / message_reader_unittest.cc
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.
4
5 #include <string>
6
7 #include "base/bind.h"
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"
21
22 using testing::_;
23 using testing::DoAll;
24 using testing::Mock;
25 using testing::SaveArg;
26
27 namespace remoting {
28 namespace protocol {
29
30 namespace {
31 const char kTestMessage1[] = "Message1";
32 const char kTestMessage2[] = "Message2";
33
34 ACTION(CallDoneTask) {
35   arg0.Run();
36 }
37 }  // namespace
38
39 class MockMessageReceivedCallback {
40  public:
41   MOCK_METHOD1(OnMessage, void(const base::Closure&));
42 };
43
44 class MessageReaderTest : public testing::Test {
45  public:
46   MessageReaderTest()
47       : in_callback_(false) {
48   }
49
50   // Following two methods are used by the ReadFromCallback test.
51   void AddSecondMessage(const base::Closure& task) {
52     AddMessage(kTestMessage2);
53     in_callback_ = true;
54     task.Run();
55     in_callback_ = false;
56   }
57
58   void OnSecondMessage(const base::Closure& task) {
59     EXPECT_FALSE(in_callback_);
60     task.Run();
61   }
62
63   // Used by the DeleteFromCallback() test.
64   void DeleteReader(const base::Closure& task) {
65     reader_.reset();
66     task.Run();
67   }
68
69  protected:
70   void SetUp() override { reader_.reset(new MessageReader()); }
71
72   void TearDown() override { STLDeleteElements(&messages_); }
73
74   void InitReader() {
75     reader_->Init(&socket_, base::Bind(
76         &MessageReaderTest::OnMessage, base::Unretained(this)));
77   }
78
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());
82
83     socket_.AppendInputData(data);
84   }
85
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;
90   }
91
92   void OnMessage(scoped_ptr<CompoundBuffer> buffer,
93                  const base::Closure& done_callback) {
94     messages_.push_back(buffer.release());
95     callback_.OnMessage(done_callback);
96   }
97
98   base::MessageLoop message_loop_;
99   scoped_ptr<MessageReader> reader_;
100   FakeStreamSocket socket_;
101   MockMessageReceivedCallback callback_;
102   std::vector<CompoundBuffer*> messages_;
103   bool in_callback_;
104 };
105
106 // Receive one message and process it with delay
107 TEST_F(MessageReaderTest, OneMessage_Delay) {
108   base::Closure done_task;
109
110   AddMessage(kTestMessage1);
111
112   EXPECT_CALL(callback_, OnMessage(_))
113       .Times(1)
114       .WillOnce(SaveArg<0>(&done_task));
115
116   InitReader();
117   base::RunLoop().RunUntilIdle();
118
119   Mock::VerifyAndClearExpectations(&callback_);
120   Mock::VerifyAndClearExpectations(&socket_);
121
122   EXPECT_TRUE(CompareResult(messages_[0], kTestMessage1));
123
124   // Verify that the reader starts reading again only after we've
125   // finished processing the previous message.
126   EXPECT_FALSE(socket_.read_pending());
127
128   done_task.Run();
129
130   EXPECT_TRUE(socket_.read_pending());
131 }
132
133 // Receive one message and process it instantly.
134 TEST_F(MessageReaderTest, OneMessage_Instant) {
135   AddMessage(kTestMessage1);
136
137   EXPECT_CALL(callback_, OnMessage(_))
138       .Times(1)
139       .WillOnce(CallDoneTask());
140
141   InitReader();
142   base::RunLoop().RunUntilIdle();
143
144   EXPECT_TRUE(socket_.read_pending());
145   EXPECT_EQ(1U, messages_.size());
146 }
147
148 // Receive two messages in one packet.
149 TEST_F(MessageReaderTest, TwoMessages_Together) {
150   base::Closure done_task1;
151   base::Closure done_task2;
152
153   AddMessage(kTestMessage1);
154   AddMessage(kTestMessage2);
155
156   EXPECT_CALL(callback_, OnMessage(_))
157       .Times(2)
158       .WillOnce(SaveArg<0>(&done_task1))
159       .WillOnce(SaveArg<0>(&done_task2));
160
161   InitReader();
162   base::RunLoop().RunUntilIdle();
163
164   Mock::VerifyAndClearExpectations(&callback_);
165   Mock::VerifyAndClearExpectations(&socket_);
166
167   EXPECT_TRUE(CompareResult(messages_[0], kTestMessage1));
168   EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2));
169
170   // Verify that the reader starts reading again only after we've
171   // finished processing the previous message.
172   EXPECT_FALSE(socket_.read_pending());
173
174   done_task1.Run();
175   base::RunLoop().RunUntilIdle();
176
177   EXPECT_FALSE(socket_.read_pending());
178
179   done_task2.Run();
180   base::RunLoop().RunUntilIdle();
181
182   EXPECT_TRUE(socket_.read_pending());
183 }
184
185 // Receive two messages in one packet, and process the first one
186 // instantly.
187 TEST_F(MessageReaderTest, TwoMessages_Instant) {
188   base::Closure done_task2;
189
190   AddMessage(kTestMessage1);
191   AddMessage(kTestMessage2);
192
193   EXPECT_CALL(callback_, OnMessage(_))
194       .Times(2)
195       .WillOnce(CallDoneTask())
196       .WillOnce(SaveArg<0>(&done_task2));
197
198   InitReader();
199   base::RunLoop().RunUntilIdle();
200
201   Mock::VerifyAndClearExpectations(&callback_);
202   Mock::VerifyAndClearExpectations(&socket_);
203
204   EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2));
205
206   // Verify that the reader starts reading again only after we've
207   // finished processing the second message.
208   EXPECT_FALSE(socket_.read_pending());
209
210   done_task2.Run();
211
212   EXPECT_TRUE(socket_.read_pending());
213 }
214
215 // Receive two messages in one packet, and process both of them
216 // instantly.
217 TEST_F(MessageReaderTest, TwoMessages_Instant2) {
218   AddMessage(kTestMessage1);
219   AddMessage(kTestMessage2);
220
221   EXPECT_CALL(callback_, OnMessage(_))
222       .Times(2)
223       .WillOnce(CallDoneTask())
224       .WillOnce(CallDoneTask());
225
226   InitReader();
227   base::RunLoop().RunUntilIdle();
228
229   EXPECT_TRUE(socket_.read_pending());
230 }
231
232 // Receive two messages in separate packets.
233 TEST_F(MessageReaderTest, TwoMessages_Separately) {
234   base::Closure done_task;
235
236   AddMessage(kTestMessage1);
237
238   EXPECT_CALL(callback_, OnMessage(_))
239       .Times(1)
240       .WillOnce(SaveArg<0>(&done_task));
241
242   InitReader();
243   base::RunLoop().RunUntilIdle();
244
245   Mock::VerifyAndClearExpectations(&callback_);
246   Mock::VerifyAndClearExpectations(&socket_);
247
248   EXPECT_TRUE(CompareResult(messages_[0], kTestMessage1));
249
250   // Verify that the reader starts reading again only after we've
251   // finished processing the previous message.
252   EXPECT_FALSE(socket_.read_pending());
253
254   done_task.Run();
255   base::RunLoop().RunUntilIdle();
256
257   EXPECT_TRUE(socket_.read_pending());
258
259   // Write another message and verify that we receive it.
260   EXPECT_CALL(callback_, OnMessage(_))
261       .Times(1)
262       .WillOnce(SaveArg<0>(&done_task));
263   AddMessage(kTestMessage2);
264   base::RunLoop().RunUntilIdle();
265
266   EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2));
267
268   // Verify that the reader starts reading again only after we've
269   // finished processing the previous message.
270   EXPECT_FALSE(socket_.read_pending());
271
272   done_task.Run();
273
274   EXPECT_TRUE(socket_.read_pending());
275 }
276
277 // Read() returns error.
278 TEST_F(MessageReaderTest, ReadError) {
279   socket_.set_next_read_error(net::ERR_FAILED);
280
281   // Add a message. It should never be read after the error above.
282   AddMessage(kTestMessage1);
283
284   EXPECT_CALL(callback_, OnMessage(_))
285       .Times(0);
286
287   InitReader();
288 }
289
290 // Verify that we the OnMessage callback is not reentered.
291 TEST_F(MessageReaderTest, ReadFromCallback) {
292   AddMessage(kTestMessage1);
293
294   EXPECT_CALL(callback_, OnMessage(_))
295       .Times(2)
296       .WillOnce(Invoke(this, &MessageReaderTest::AddSecondMessage))
297       .WillOnce(Invoke(this, &MessageReaderTest::OnSecondMessage));
298
299   InitReader();
300   base::RunLoop().RunUntilIdle();
301
302   EXPECT_TRUE(socket_.read_pending());
303 }
304
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;
309
310   AddMessage(kTestMessage1);
311   AddMessage(kTestMessage2);
312
313   // OnMessage() should never be called for the second message.
314   EXPECT_CALL(callback_, OnMessage(_))
315       .Times(1)
316       .WillOnce(Invoke(this, &MessageReaderTest::DeleteReader));
317
318   InitReader();
319   base::RunLoop().RunUntilIdle();
320 }
321
322 }  // namespace protocol
323 }  // namespace remoting