Add Download.ShowDangerousDownloadConfirmationPrompt for Windows/CrOS
[chromium-blink-merge.git] / remoting / protocol / message_reader_unittest.cc
blob934a5b66dc365660a01862e8d8163ea867a04b31
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.
5 #include <string>
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/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"
21 using testing::_;
22 using testing::DoAll;
23 using testing::Mock;
24 using testing::SaveArg;
26 namespace remoting {
27 namespace protocol {
29 namespace {
30 const char kTestMessage1[] = "Message1";
31 const char kTestMessage2[] = "Message2";
33 ACTION(CallDoneTask) {
34 arg0.Run();
36 } // namespace
38 class MockMessageReceivedCallback {
39 public:
40 MOCK_METHOD1(OnMessage, void(const base::Closure&));
43 class MessageReaderTest : public testing::Test {
44 public:
45 MessageReaderTest()
46 : in_callback_(false) {
49 // Following two methods are used by the ReadFromCallback test.
50 void AddSecondMessage(const base::Closure& task) {
51 AddMessage(kTestMessage2);
52 in_callback_ = true;
53 task.Run();
54 in_callback_ = false;
57 void OnSecondMessage(const base::Closure& task) {
58 EXPECT_FALSE(in_callback_);
59 task.Run();
62 // Used by the DeleteFromCallback() test.
63 void DeleteReader(const base::Closure& task) {
64 reader_.reset();
65 task.Run();
68 protected:
69 virtual void SetUp() OVERRIDE {
70 reader_.reset(new MessageReader());
73 virtual void TearDown() OVERRIDE {
74 STLDeleteElements(&messages_);
77 void InitReader() {
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_;
103 FakeSocket socket_;
104 MockMessageReceivedCallback callback_;
105 std::vector<CompoundBuffer*> messages_;
106 bool in_callback_;
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(_))
116 .Times(1)
117 .WillOnce(SaveArg<0>(&done_task));
119 InitReader();
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());
131 done_task.Run();
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(_))
141 .Times(1)
142 .WillOnce(CallDoneTask());
144 InitReader();
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(_))
160 .Times(2)
161 .WillOnce(SaveArg<0>(&done_task1))
162 .WillOnce(SaveArg<0>(&done_task2));
164 InitReader();
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());
177 done_task1.Run();
178 message_loop_.RunUntilIdle();
180 EXPECT_FALSE(socket_.read_pending());
182 done_task2.Run();
183 message_loop_.RunUntilIdle();
185 EXPECT_TRUE(socket_.read_pending());
188 // Receive two messages in one packet, and process the first one
189 // instantly.
190 TEST_F(MessageReaderTest, TwoMessages_Instant) {
191 base::Closure done_task2;
193 AddMessage(kTestMessage1);
194 AddMessage(kTestMessage2);
196 EXPECT_CALL(callback_, OnMessage(_))
197 .Times(2)
198 .WillOnce(CallDoneTask())
199 .WillOnce(SaveArg<0>(&done_task2));
201 InitReader();
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());
213 done_task2.Run();
215 EXPECT_TRUE(socket_.read_pending());
218 // Receive two messages in one packet, and process both of them
219 // instantly.
220 TEST_F(MessageReaderTest, TwoMessages_Instant2) {
221 AddMessage(kTestMessage1);
222 AddMessage(kTestMessage2);
224 EXPECT_CALL(callback_, OnMessage(_))
225 .Times(2)
226 .WillOnce(CallDoneTask())
227 .WillOnce(CallDoneTask());
229 InitReader();
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(_))
242 .Times(1)
243 .WillOnce(SaveArg<0>(&done_task));
245 InitReader();
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());
257 done_task.Run();
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(_))
264 .Times(1)
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());
275 done_task.Run();
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(_))
288 .Times(0);
290 InitReader();
293 // Verify that we the OnMessage callback is not reentered.
294 TEST_F(MessageReaderTest, ReadFromCallback) {
295 AddMessage(kTestMessage1);
297 EXPECT_CALL(callback_, OnMessage(_))
298 .Times(2)
299 .WillOnce(Invoke(this, &MessageReaderTest::AddSecondMessage))
300 .WillOnce(Invoke(this, &MessageReaderTest::OnSecondMessage));
302 InitReader();
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(_))
318 .Times(1)
319 .WillOnce(Invoke(this, &MessageReaderTest::DeleteReader));
321 InitReader();
322 message_loop_.RunUntilIdle();
325 } // namespace protocol
326 } // namespace remoting