Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / remoting / protocol / message_reader_unittest.cc
blobf8bf2d90943a900bf0400b8c8d4fb982ae8af4f6
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/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"
22 using testing::_;
23 using testing::DoAll;
24 using testing::Mock;
25 using testing::SaveArg;
27 namespace remoting {
28 namespace protocol {
30 namespace {
31 const char kTestMessage1[] = "Message1";
32 const char kTestMessage2[] = "Message2";
34 ACTION(CallDoneTask) {
35 arg0.Run();
37 } // namespace
39 class MockMessageReceivedCallback {
40 public:
41 MOCK_METHOD1(OnMessage, void(const base::Closure&));
44 class MessageReaderTest : public testing::Test {
45 public:
46 MessageReaderTest()
47 : in_callback_(false) {
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;
58 void OnSecondMessage(const base::Closure& task) {
59 EXPECT_FALSE(in_callback_);
60 task.Run();
63 // Used by the DeleteFromCallback() test.
64 void DeleteReader(const base::Closure& task) {
65 reader_.reset();
66 task.Run();
69 protected:
70 void SetUp() override {
71 reader_.reset(new MessageReader());
74 void TearDown() override { STLDeleteElements(&messages_); }
76 void InitReader() {
77 reader_->SetMessageReceivedCallback(
78 base::Bind(&MessageReaderTest::OnMessage, base::Unretained(this)));
79 reader_->StartReading(&socket_);
82 void AddMessage(const std::string& message) {
83 std::string data = std::string(4, ' ') + message;
84 rtc::SetBE32(const_cast<char*>(data.data()), message.size());
86 socket_.AppendInputData(data);
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 FakeStreamSocket 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 base::RunLoop().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 base::RunLoop().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 base::RunLoop().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 base::RunLoop().RunUntilIdle();
180 EXPECT_FALSE(socket_.read_pending());
182 done_task2.Run();
183 base::RunLoop().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 base::RunLoop().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 base::RunLoop().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 base::RunLoop().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 base::RunLoop().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 base::RunLoop().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_.AppendReadError(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 base::RunLoop().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 base::RunLoop().RunUntilIdle();
325 } // namespace protocol
326 } // namespace remoting