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