Upstream version 10.39.225.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   virtual void SetUp() OVERRIDE {
71     reader_.reset(new MessageReader());
72   }
73
74   virtual void TearDown() OVERRIDE {
75     STLDeleteElements(&messages_);
76   }
77
78   void InitReader() {
79     reader_->Init(&socket_, base::Bind(
80         &MessageReaderTest::OnMessage, base::Unretained(this)));
81   }
82
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());
86
87     socket_.AppendInputData(data);
88   }
89
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;
94   }
95
96   void OnMessage(scoped_ptr<CompoundBuffer> buffer,
97                  const base::Closure& done_callback) {
98     messages_.push_back(buffer.release());
99     callback_.OnMessage(done_callback);
100   }
101
102   base::MessageLoop message_loop_;
103   scoped_ptr<MessageReader> reader_;
104   FakeStreamSocket socket_;
105   MockMessageReceivedCallback callback_;
106   std::vector<CompoundBuffer*> messages_;
107   bool in_callback_;
108 };
109
110 // Receive one message and process it with delay
111 TEST_F(MessageReaderTest, OneMessage_Delay) {
112   base::Closure done_task;
113
114   AddMessage(kTestMessage1);
115
116   EXPECT_CALL(callback_, OnMessage(_))
117       .Times(1)
118       .WillOnce(SaveArg<0>(&done_task));
119
120   InitReader();
121   base::RunLoop().RunUntilIdle();
122
123   Mock::VerifyAndClearExpectations(&callback_);
124   Mock::VerifyAndClearExpectations(&socket_);
125
126   EXPECT_TRUE(CompareResult(messages_[0], kTestMessage1));
127
128   // Verify that the reader starts reading again only after we've
129   // finished processing the previous message.
130   EXPECT_FALSE(socket_.read_pending());
131
132   done_task.Run();
133
134   EXPECT_TRUE(socket_.read_pending());
135 }
136
137 // Receive one message and process it instantly.
138 TEST_F(MessageReaderTest, OneMessage_Instant) {
139   AddMessage(kTestMessage1);
140
141   EXPECT_CALL(callback_, OnMessage(_))
142       .Times(1)
143       .WillOnce(CallDoneTask());
144
145   InitReader();
146   base::RunLoop().RunUntilIdle();
147
148   EXPECT_TRUE(socket_.read_pending());
149   EXPECT_EQ(1U, messages_.size());
150 }
151
152 // Receive two messages in one packet.
153 TEST_F(MessageReaderTest, TwoMessages_Together) {
154   base::Closure done_task1;
155   base::Closure done_task2;
156
157   AddMessage(kTestMessage1);
158   AddMessage(kTestMessage2);
159
160   EXPECT_CALL(callback_, OnMessage(_))
161       .Times(2)
162       .WillOnce(SaveArg<0>(&done_task1))
163       .WillOnce(SaveArg<0>(&done_task2));
164
165   InitReader();
166   base::RunLoop().RunUntilIdle();
167
168   Mock::VerifyAndClearExpectations(&callback_);
169   Mock::VerifyAndClearExpectations(&socket_);
170
171   EXPECT_TRUE(CompareResult(messages_[0], kTestMessage1));
172   EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2));
173
174   // Verify that the reader starts reading again only after we've
175   // finished processing the previous message.
176   EXPECT_FALSE(socket_.read_pending());
177
178   done_task1.Run();
179   base::RunLoop().RunUntilIdle();
180
181   EXPECT_FALSE(socket_.read_pending());
182
183   done_task2.Run();
184   base::RunLoop().RunUntilIdle();
185
186   EXPECT_TRUE(socket_.read_pending());
187 }
188
189 // Receive two messages in one packet, and process the first one
190 // instantly.
191 TEST_F(MessageReaderTest, TwoMessages_Instant) {
192   base::Closure done_task2;
193
194   AddMessage(kTestMessage1);
195   AddMessage(kTestMessage2);
196
197   EXPECT_CALL(callback_, OnMessage(_))
198       .Times(2)
199       .WillOnce(CallDoneTask())
200       .WillOnce(SaveArg<0>(&done_task2));
201
202   InitReader();
203   base::RunLoop().RunUntilIdle();
204
205   Mock::VerifyAndClearExpectations(&callback_);
206   Mock::VerifyAndClearExpectations(&socket_);
207
208   EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2));
209
210   // Verify that the reader starts reading again only after we've
211   // finished processing the second message.
212   EXPECT_FALSE(socket_.read_pending());
213
214   done_task2.Run();
215
216   EXPECT_TRUE(socket_.read_pending());
217 }
218
219 // Receive two messages in one packet, and process both of them
220 // instantly.
221 TEST_F(MessageReaderTest, TwoMessages_Instant2) {
222   AddMessage(kTestMessage1);
223   AddMessage(kTestMessage2);
224
225   EXPECT_CALL(callback_, OnMessage(_))
226       .Times(2)
227       .WillOnce(CallDoneTask())
228       .WillOnce(CallDoneTask());
229
230   InitReader();
231   base::RunLoop().RunUntilIdle();
232
233   EXPECT_TRUE(socket_.read_pending());
234 }
235
236 // Receive two messages in separate packets.
237 TEST_F(MessageReaderTest, TwoMessages_Separately) {
238   base::Closure done_task;
239
240   AddMessage(kTestMessage1);
241
242   EXPECT_CALL(callback_, OnMessage(_))
243       .Times(1)
244       .WillOnce(SaveArg<0>(&done_task));
245
246   InitReader();
247   base::RunLoop().RunUntilIdle();
248
249   Mock::VerifyAndClearExpectations(&callback_);
250   Mock::VerifyAndClearExpectations(&socket_);
251
252   EXPECT_TRUE(CompareResult(messages_[0], kTestMessage1));
253
254   // Verify that the reader starts reading again only after we've
255   // finished processing the previous message.
256   EXPECT_FALSE(socket_.read_pending());
257
258   done_task.Run();
259   base::RunLoop().RunUntilIdle();
260
261   EXPECT_TRUE(socket_.read_pending());
262
263   // Write another message and verify that we receive it.
264   EXPECT_CALL(callback_, OnMessage(_))
265       .Times(1)
266       .WillOnce(SaveArg<0>(&done_task));
267   AddMessage(kTestMessage2);
268   base::RunLoop().RunUntilIdle();
269
270   EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2));
271
272   // Verify that the reader starts reading again only after we've
273   // finished processing the previous message.
274   EXPECT_FALSE(socket_.read_pending());
275
276   done_task.Run();
277
278   EXPECT_TRUE(socket_.read_pending());
279 }
280
281 // Read() returns error.
282 TEST_F(MessageReaderTest, ReadError) {
283   socket_.set_next_read_error(net::ERR_FAILED);
284
285   // Add a message. It should never be read after the error above.
286   AddMessage(kTestMessage1);
287
288   EXPECT_CALL(callback_, OnMessage(_))
289       .Times(0);
290
291   InitReader();
292 }
293
294 // Verify that we the OnMessage callback is not reentered.
295 TEST_F(MessageReaderTest, ReadFromCallback) {
296   AddMessage(kTestMessage1);
297
298   EXPECT_CALL(callback_, OnMessage(_))
299       .Times(2)
300       .WillOnce(Invoke(this, &MessageReaderTest::AddSecondMessage))
301       .WillOnce(Invoke(this, &MessageReaderTest::OnSecondMessage));
302
303   InitReader();
304   base::RunLoop().RunUntilIdle();
305
306   EXPECT_TRUE(socket_.read_pending());
307 }
308
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;
313
314   AddMessage(kTestMessage1);
315   AddMessage(kTestMessage2);
316
317   // OnMessage() should never be called for the second message.
318   EXPECT_CALL(callback_, OnMessage(_))
319       .Times(1)
320       .WillOnce(Invoke(this, &MessageReaderTest::DeleteReader));
321
322   InitReader();
323   base::RunLoop().RunUntilIdle();
324 }
325
326 }  // namespace protocol
327 }  // namespace remoting