1 // Copyright 2014 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 "extensions/browser/api/cast_channel/cast_transport.h" 6 7 #include <stddef.h> 8 #include <queue> 9 10 #include "base/test/simple_test_tick_clock.h" 11 #include "extensions/browser/api/cast_channel/cast_framer.h" 12 #include "extensions/browser/api/cast_channel/cast_transport.h" 13 #include "extensions/browser/api/cast_channel/logger.h" 14 #include "extensions/browser/api/cast_channel/logger_util.h" 15 #include "extensions/common/api/cast_channel/cast_channel.pb.h" 16 #include "net/base/capturing_net_log.h" 17 #include "net/base/completion_callback.h" 18 #include "net/base/net_errors.h" 19 #include "testing/gmock/include/gmock/gmock.h" 20 #include "testing/gtest/include/gtest/gtest.h" 21 22 using testing::_; 23 using testing::DoAll; 24 using testing::InSequence; 25 using testing::Invoke; 26 using testing::NotNull; 27 using testing::Return; 28 using testing::WithArg; 29 30 namespace extensions { 31 namespace core_api { 32 namespace cast_channel { 33 namespace { 34 // Mockable placeholder for write completion events. 35 class CompleteHandler { 36 public: 37 CompleteHandler() {} 38 MOCK_METHOD1(Complete, void(int result)); 39 40 private: 41 DISALLOW_COPY_AND_ASSIGN(CompleteHandler); 42 }; 43 44 // Creates a CastMessage proto with the bare minimum required fields set. 45 CastMessage CreateCastMessage() { 46 CastMessage output; 47 output.set_protocol_version(CastMessage::CASTV2_1_0); 48 output.set_namespace_("x"); 49 output.set_source_id("source"); 50 output.set_destination_id("destination"); 51 output.set_payload_type(CastMessage::STRING); 52 output.set_payload_utf8("payload"); 53 return output; 54 } 55 56 // FIFO queue of completion callbacks. Outstanding write operations are 57 // Push()ed into the queue. Callback completion is simulated by invoking 58 // Pop() in the same order as Push(). 59 class CompletionQueue { 60 public: 61 CompletionQueue() {} 62 ~CompletionQueue() { CHECK_EQ(0u, cb_queue_.size()); } 63 64 // Enqueues a pending completion callback. 65 void Push(const net::CompletionCallback& cb) { cb_queue_.push(cb); } 66 // Runs the next callback and removes it from the queue. 67 void Pop(int rv) { 68 CHECK_GT(cb_queue_.size(), 0u); 69 cb_queue_.front().Run(rv); 70 cb_queue_.pop(); 71 } 72 73 private: 74 std::queue<net::CompletionCallback> cb_queue_; 75 DISALLOW_COPY_AND_ASSIGN(CompletionQueue); 76 }; 77 78 // GMock action that reads data from an IOBuffer and writes it to a string 79 // variable. 80 // 81 // buf_idx (template parameter 0): 0-based index of the net::IOBuffer 82 // in the function mock arg list. 83 // size_idx (template parameter 1): 0-based index of the byte count arg. 84 // str: pointer to the string which will receive data from the buffer. 85 ACTION_TEMPLATE(ReadBufferToString, 86 HAS_2_TEMPLATE_PARAMS(int, buf_idx, int, size_idx), 87 AND_1_VALUE_PARAMS(str)) { 88 str->assign(testing::get<buf_idx>(args)->data(), 89 testing::get<size_idx>(args)); 90 } 91 92 // GMock action that writes data from a string to an IOBuffer. 93 // 94 // buf_idx (template parameter 0): 0-based index of the IOBuffer arg. 95 // str: the string containing data to be written to the IOBuffer. 96 ACTION_TEMPLATE(FillBufferFromString, 97 HAS_1_TEMPLATE_PARAMS(int, buf_idx), 98 AND_1_VALUE_PARAMS(str)) { 99 memcpy(testing::get<buf_idx>(args)->data(), str.data(), str.size()); 100 } 101 102 // GMock action that enqueues a write completion callback in a queue. 103 // 104 // buf_idx (template parameter 0): 0-based index of the CompletionCallback. 105 // completion_queue: a pointer to the CompletionQueue. 106 ACTION_TEMPLATE(EnqueueCallback, 107 HAS_1_TEMPLATE_PARAMS(int, cb_idx), 108 AND_1_VALUE_PARAMS(completion_queue)) { 109 completion_queue->Push(testing::get<cb_idx>(args)); 110 } 111 112 // Checks if two proto messages are the same. 113 // From 114 // third_party/cacheinvalidation/overrides/google/cacheinvalidation/deps/gmock.h 115 MATCHER_P(EqualsProto, message, "") { 116 std::string expected_serialized, actual_serialized; 117 message.SerializeToString(&expected_serialized); 118 arg.SerializeToString(&actual_serialized); 119 return expected_serialized == actual_serialized; 120 } 121 } // namespace 122 123 class MockCastTransportDelegate : public CastTransport::Delegate { 124 public: 125 MOCK_METHOD3(OnError, 126 void(const CastSocketInterface* socket, 127 ChannelError error, 128 const LastErrors& last_errors)); 129 MOCK_METHOD2(OnMessage, 130 void(const CastSocketInterface* socket, 131 const CastMessage& message)); 132 }; 133 134 class MockCastSocket : public CastSocketInterface { 135 public: 136 MockCastSocket() { 137 net::IPAddressNumber number; 138 number.push_back(192); 139 number.push_back(0); 140 number.push_back(0); 141 number.push_back(1); 142 ip_ = net::IPEndPoint(number, 8009); 143 } 144 145 virtual ~MockCastSocket() {} 146 147 // The IP endpoint for the destination of the channel. 148 virtual const net::IPEndPoint& ip_endpoint() const OVERRIDE { return ip_; } 149 150 // The authentication level requested for the channel. 151 virtual ChannelAuthType channel_auth() const OVERRIDE { 152 return CHANNEL_AUTH_TYPE_SSL_VERIFIED; 153 } 154 155 virtual int id() const OVERRIDE { return 1; } 156 157 MOCK_METHOD3(Write, 158 int(net::IOBuffer* buffer, 159 size_t size, 160 const net::CompletionCallback& callback)); 161 MOCK_METHOD3(Read, 162 int(net::IOBuffer* buf, 163 int buf_len, 164 const net::CompletionCallback& callback)); 165 MOCK_METHOD1(CloseWithError, void(ChannelError error)); 166 167 protected: 168 virtual void CloseInternal() {} 169 170 private: 171 net::IPEndPoint ip_; 172 net::CapturingNetLog capturing_net_log_; 173 }; 174 175 class CastTransportTest : public testing::Test { 176 public: 177 CastTransportTest() 178 : logger_(new Logger( 179 scoped_ptr<base::TickClock>(new base::SimpleTestTickClock), 180 base::TimeTicks())) { 181 transport_.reset(new CastTransport(&mock_socket_, &delegate_, logger_)); 182 } 183 virtual ~CastTransportTest() {} 184 185 protected: 186 MockCastTransportDelegate delegate_; 187 MockCastSocket mock_socket_; 188 scoped_refptr<Logger> logger_; 189 scoped_ptr<CastTransport> transport_; 190 }; 191 192 // ---------------------------------------------------------------------------- 193 // Asynchronous write tests 194 TEST_F(CastTransportTest, TestFullWriteAsync) { 195 CompletionQueue socket_cbs; 196 CompleteHandler write_handler; 197 std::string output; 198 199 CastMessage message = CreateCastMessage(); 200 std::string serialized_message; 201 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message)); 202 203 EXPECT_CALL(mock_socket_, Write(NotNull(), serialized_message.size(), _)) 204 .WillOnce(DoAll(ReadBufferToString<0, 1>(&output), 205 EnqueueCallback<2>(&socket_cbs), 206 Return(net::ERR_IO_PENDING))); 207 EXPECT_CALL(write_handler, Complete(net::OK)); 208 transport_->SendMessage( 209 message, 210 base::Bind(&CompleteHandler::Complete, base::Unretained(&write_handler))); 211 socket_cbs.Pop(serialized_message.size()); 212 EXPECT_EQ(serialized_message, output); 213 } 214 215 TEST_F(CastTransportTest, TestPartialWritesAsync) { 216 InSequence seq; 217 CompletionQueue socket_cbs; 218 CompleteHandler write_handler; 219 std::string output; 220 221 CastMessage message = CreateCastMessage(); 222 std::string serialized_message; 223 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message)); 224 225 // Only one byte is written. 226 EXPECT_CALL(mock_socket_, Write(NotNull(), serialized_message.size(), _)) 227 .WillOnce(DoAll(ReadBufferToString<0, 1>(&output), 228 EnqueueCallback<2>(&socket_cbs), 229 Return(net::ERR_IO_PENDING))); 230 // Remainder of bytes are written. 231 EXPECT_CALL(mock_socket_, Write(NotNull(), serialized_message.size() - 1, _)) 232 .WillOnce(DoAll(ReadBufferToString<0, 1>(&output), 233 EnqueueCallback<2>(&socket_cbs), 234 Return(net::ERR_IO_PENDING))); 235 236 transport_->SendMessage( 237 message, 238 base::Bind(&CompleteHandler::Complete, base::Unretained(&write_handler))); 239 EXPECT_EQ(serialized_message, output); 240 socket_cbs.Pop(1); 241 242 EXPECT_CALL(write_handler, Complete(net::OK)); 243 socket_cbs.Pop(serialized_message.size() - 1); 244 EXPECT_EQ(serialized_message.substr(1, serialized_message.size() - 1), 245 output); 246 } 247 248 TEST_F(CastTransportTest, TestWriteFailureAsync) { 249 CompletionQueue socket_cbs; 250 CompleteHandler write_handler; 251 CastMessage message = CreateCastMessage(); 252 EXPECT_CALL(mock_socket_, Write(NotNull(), _, _)).WillOnce( 253 DoAll(EnqueueCallback<2>(&socket_cbs), Return(net::ERR_IO_PENDING))); 254 EXPECT_CALL(write_handler, Complete(net::ERR_FAILED)); 255 transport_->SendMessage( 256 message, 257 base::Bind(&CompleteHandler::Complete, base::Unretained(&write_handler))); 258 socket_cbs.Pop(net::ERR_CONNECTION_RESET); 259 } 260 261 // ---------------------------------------------------------------------------- 262 // Synchronous write tests 263 TEST_F(CastTransportTest, TestFullWriteSync) { 264 CompleteHandler write_handler; 265 std::string output; 266 CastMessage message = CreateCastMessage(); 267 std::string serialized_message; 268 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message)); 269 EXPECT_CALL(mock_socket_, Write(NotNull(), serialized_message.size(), _)) 270 .WillOnce(DoAll(ReadBufferToString<0, 1>(&output), 271 Return(serialized_message.size()))); 272 EXPECT_CALL(write_handler, Complete(net::OK)); 273 transport_->SendMessage( 274 message, 275 base::Bind(&CompleteHandler::Complete, base::Unretained(&write_handler))); 276 EXPECT_EQ(serialized_message, output); 277 } 278 279 TEST_F(CastTransportTest, TestPartialWritesSync) { 280 InSequence seq; 281 CompleteHandler write_handler; 282 std::string output; 283 284 CastMessage message = CreateCastMessage(); 285 std::string serialized_message; 286 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message)); 287 288 // Only one byte is written. 289 EXPECT_CALL(mock_socket_, Write(NotNull(), serialized_message.size(), _)) 290 .WillOnce(DoAll(ReadBufferToString<0, 1>(&output), Return(1))); 291 // Remainder of bytes are written. 292 EXPECT_CALL(mock_socket_, Write(NotNull(), serialized_message.size() - 1, _)) 293 .WillOnce(DoAll(ReadBufferToString<0, 1>(&output), 294 Return(serialized_message.size() - 1))); 295 296 EXPECT_CALL(write_handler, Complete(net::OK)); 297 transport_->SendMessage( 298 message, 299 base::Bind(&CompleteHandler::Complete, base::Unretained(&write_handler))); 300 EXPECT_EQ(serialized_message.substr(1, serialized_message.size() - 1), 301 output); 302 } 303 304 TEST_F(CastTransportTest, TestWriteFailureSync) { 305 CompleteHandler write_handler; 306 CastMessage message = CreateCastMessage(); 307 EXPECT_CALL(mock_socket_, Write(NotNull(), _, _)) 308 .WillOnce(Return(net::ERR_CONNECTION_RESET)); 309 EXPECT_CALL(write_handler, Complete(net::ERR_FAILED)); 310 transport_->SendMessage( 311 message, 312 base::Bind(&CompleteHandler::Complete, base::Unretained(&write_handler))); 313 } 314 315 // ---------------------------------------------------------------------------- 316 // Asynchronous read tests 317 TEST_F(CastTransportTest, TestFullReadAsync) { 318 CompletionQueue socket_cbs; 319 320 CastMessage message = CreateCastMessage(); 321 std::string serialized_message; 322 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message)); 323 324 // Read bytes [0, 3]. 325 EXPECT_CALL(mock_socket_, 326 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _)) 327 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message), 328 EnqueueCallback<2>(&socket_cbs), 329 Return(net::ERR_IO_PENDING))) 330 .RetiresOnSaturation(); 331 // Read bytes [4, n]. 332 EXPECT_CALL(mock_socket_, 333 Read(NotNull(), 334 serialized_message.size() - 335 MessageFramer::MessageHeader::header_size(), 336 _)) 337 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message.substr( 338 MessageFramer::MessageHeader::header_size(), 339 serialized_message.size() - 340 MessageFramer::MessageHeader::header_size())), 341 EnqueueCallback<2>(&socket_cbs), 342 Return(net::ERR_IO_PENDING))) 343 .RetiresOnSaturation(); 344 345 EXPECT_CALL(delegate_, OnMessage(&mock_socket_, EqualsProto(message))); 346 transport_->StartReadLoop(); 347 socket_cbs.Pop(MessageFramer::MessageHeader::header_size()); 348 EXPECT_CALL(mock_socket_, 349 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _)) 350 .WillOnce(Return(net::ERR_IO_PENDING)); 351 socket_cbs.Pop(serialized_message.size() - 352 MessageFramer::MessageHeader::header_size()); 353 } 354 355 TEST_F(CastTransportTest, TestPartialReadAsync) { 356 CompletionQueue socket_cbs; 357 358 CastMessage message = CreateCastMessage(); 359 std::string serialized_message; 360 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message)); 361 362 // Read bytes [0, 3]. 363 EXPECT_CALL(mock_socket_, 364 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _)) 365 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message), 366 EnqueueCallback<2>(&socket_cbs), 367 Return(net::ERR_IO_PENDING))) 368 .RetiresOnSaturation(); 369 // Read bytes [4, n-1]. 370 EXPECT_CALL(mock_socket_, 371 Read(NotNull(), 372 serialized_message.size() - 373 MessageFramer::MessageHeader::header_size(), 374 _)) 375 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message.substr( 376 MessageFramer::MessageHeader::header_size(), 377 serialized_message.size() - 378 MessageFramer::MessageHeader::header_size() - 1)), 379 EnqueueCallback<2>(&socket_cbs), 380 Return(net::ERR_IO_PENDING))) 381 .RetiresOnSaturation(); 382 // Read final byte. 383 EXPECT_CALL(mock_socket_, Read(NotNull(), 1, _)) 384 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message.substr( 385 serialized_message.size() - 1, 1)), 386 EnqueueCallback<2>(&socket_cbs), 387 Return(net::ERR_IO_PENDING))) 388 .RetiresOnSaturation(); 389 390 EXPECT_CALL(delegate_, OnMessage(&mock_socket_, EqualsProto(message))); 391 transport_->StartReadLoop(); 392 socket_cbs.Pop(MessageFramer::MessageHeader::header_size()); 393 EXPECT_CALL(mock_socket_, 394 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _)) 395 .WillOnce(Return(net::ERR_IO_PENDING)); 396 socket_cbs.Pop(serialized_message.size() - 397 MessageFramer::MessageHeader::header_size() - 1); 398 socket_cbs.Pop(1); 399 } 400 401 TEST_F(CastTransportTest, TestReadErrorInHeaderAsync) { 402 CompletionQueue socket_cbs; 403 404 CastMessage message = CreateCastMessage(); 405 std::string serialized_message; 406 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message)); 407 408 // Read bytes [0, 3]. 409 EXPECT_CALL(mock_socket_, 410 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _)) 411 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message), 412 EnqueueCallback<2>(&socket_cbs), 413 Return(net::ERR_IO_PENDING))) 414 .RetiresOnSaturation(); 415 416 EXPECT_CALL(delegate_, 417 OnError(&mock_socket_, CHANNEL_ERROR_TRANSPORT_ERROR, _)); 418 EXPECT_CALL(mock_socket_, CloseWithError(CHANNEL_ERROR_TRANSPORT_ERROR)); 419 transport_->StartReadLoop(); 420 // Header read failure. 421 socket_cbs.Pop(net::ERR_CONNECTION_RESET); 422 } 423 424 TEST_F(CastTransportTest, TestReadErrorInBodyAsync) { 425 CompletionQueue socket_cbs; 426 427 CastMessage message = CreateCastMessage(); 428 std::string serialized_message; 429 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message)); 430 431 // Read bytes [0, 3]. 432 EXPECT_CALL(mock_socket_, 433 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _)) 434 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message), 435 EnqueueCallback<2>(&socket_cbs), 436 Return(net::ERR_IO_PENDING))) 437 .RetiresOnSaturation(); 438 // Read bytes [4, n-1]. 439 EXPECT_CALL(mock_socket_, 440 Read(NotNull(), 441 serialized_message.size() - 442 MessageFramer::MessageHeader::header_size(), 443 _)) 444 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message.substr( 445 MessageFramer::MessageHeader::header_size(), 446 serialized_message.size() - 447 MessageFramer::MessageHeader::header_size() - 1)), 448 EnqueueCallback<2>(&socket_cbs), 449 Return(net::ERR_IO_PENDING))) 450 .RetiresOnSaturation(); 451 452 EXPECT_CALL(delegate_, 453 OnError(&mock_socket_, CHANNEL_ERROR_TRANSPORT_ERROR, _)); 454 transport_->StartReadLoop(); 455 // Header read is OK. 456 socket_cbs.Pop(MessageFramer::MessageHeader::header_size()); 457 EXPECT_CALL(mock_socket_, CloseWithError(CHANNEL_ERROR_TRANSPORT_ERROR)); 458 // Body read fails. 459 socket_cbs.Pop(net::ERR_CONNECTION_RESET); 460 } 461 462 TEST_F(CastTransportTest, TestReadCorruptedMessageAsync) { 463 CompletionQueue socket_cbs; 464 465 CastMessage message = CreateCastMessage(); 466 std::string serialized_message; 467 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message)); 468 469 // Corrupt the serialized message body(set it to X's). 470 for (size_t i = MessageFramer::MessageHeader::header_size(); 471 i < serialized_message.size(); 472 ++i) { 473 serialized_message[i] = 'x'; 474 } 475 476 // Read bytes [0, 3]. 477 EXPECT_CALL(mock_socket_, 478 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _)) 479 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message), 480 EnqueueCallback<2>(&socket_cbs), 481 Return(net::ERR_IO_PENDING))) 482 .RetiresOnSaturation(); 483 // Read bytes [4, n]. 484 EXPECT_CALL(mock_socket_, 485 Read(NotNull(), 486 serialized_message.size() - 487 MessageFramer::MessageHeader::header_size(), 488 _)) 489 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message.substr( 490 MessageFramer::MessageHeader::header_size(), 491 serialized_message.size() - 492 MessageFramer::MessageHeader::header_size() - 1)), 493 EnqueueCallback<2>(&socket_cbs), 494 Return(net::ERR_IO_PENDING))) 495 .RetiresOnSaturation(); 496 497 EXPECT_CALL(delegate_, 498 OnError(&mock_socket_, CHANNEL_ERROR_INVALID_MESSAGE, _)); 499 transport_->StartReadLoop(); 500 socket_cbs.Pop(MessageFramer::MessageHeader::header_size()); 501 EXPECT_CALL(mock_socket_, CloseWithError(CHANNEL_ERROR_INVALID_MESSAGE)); 502 socket_cbs.Pop(serialized_message.size() - 503 MessageFramer::MessageHeader::header_size()); 504 } 505 506 // ---------------------------------------------------------------------------- 507 // Synchronous read tests 508 TEST_F(CastTransportTest, TestFullReadSync) { 509 InSequence s; 510 CastMessage message = CreateCastMessage(); 511 std::string serialized_message; 512 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message)); 513 514 // Read bytes [0, 3]. 515 EXPECT_CALL(mock_socket_, 516 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _)) 517 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message), 518 Return(MessageFramer::MessageHeader::header_size()))) 519 .RetiresOnSaturation(); 520 // Read bytes [4, n]. 521 EXPECT_CALL(mock_socket_, 522 Read(NotNull(), 523 serialized_message.size() - 524 MessageFramer::MessageHeader::header_size(), 525 _)) 526 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message.substr( 527 MessageFramer::MessageHeader::header_size(), 528 serialized_message.size() - 529 MessageFramer::MessageHeader::header_size())), 530 Return(serialized_message.size() - 531 MessageFramer::MessageHeader::header_size()))) 532 .RetiresOnSaturation(); 533 EXPECT_CALL(delegate_, OnMessage(&mock_socket_, EqualsProto(message))); 534 EXPECT_CALL(mock_socket_, 535 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _)) 536 .WillOnce(Return(net::ERR_IO_PENDING)); 537 transport_->StartReadLoop(); 538 } 539 540 TEST_F(CastTransportTest, TestPartialReadSync) { 541 InSequence s; 542 543 CastMessage message = CreateCastMessage(); 544 std::string serialized_message; 545 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message)); 546 547 // Read bytes [0, 3]. 548 EXPECT_CALL(mock_socket_, 549 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _)) 550 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message), 551 Return(MessageFramer::MessageHeader::header_size()))) 552 .RetiresOnSaturation(); 553 // Read bytes [4, n-1]. 554 EXPECT_CALL(mock_socket_, 555 Read(NotNull(), 556 serialized_message.size() - 557 MessageFramer::MessageHeader::header_size(), 558 _)) 559 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message.substr( 560 MessageFramer::MessageHeader::header_size(), 561 serialized_message.size() - 562 MessageFramer::MessageHeader::header_size() - 1)), 563 Return(serialized_message.size() - 564 MessageFramer::MessageHeader::header_size() - 1))) 565 .RetiresOnSaturation(); 566 // Read final byte. 567 EXPECT_CALL(mock_socket_, Read(NotNull(), 1, _)) 568 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message.substr( 569 serialized_message.size() - 1, 1)), 570 Return(1))) 571 .RetiresOnSaturation(); 572 EXPECT_CALL(delegate_, OnMessage(&mock_socket_, EqualsProto(message))); 573 EXPECT_CALL(mock_socket_, 574 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _)) 575 .WillOnce(Return(net::ERR_IO_PENDING)); 576 transport_->StartReadLoop(); 577 } 578 579 TEST_F(CastTransportTest, TestReadErrorInHeaderSync) { 580 CastMessage message = CreateCastMessage(); 581 std::string serialized_message; 582 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message)); 583 584 // Read bytes [0, 3]. 585 EXPECT_CALL(mock_socket_, 586 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _)) 587 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message), 588 Return(net::ERR_CONNECTION_RESET))) 589 .RetiresOnSaturation(); 590 EXPECT_CALL(delegate_, 591 OnError(&mock_socket_, CHANNEL_ERROR_TRANSPORT_ERROR, _)); 592 EXPECT_CALL(mock_socket_, CloseWithError(CHANNEL_ERROR_TRANSPORT_ERROR)); 593 transport_->StartReadLoop(); 594 } 595 596 TEST_F(CastTransportTest, TestReadErrorInBodySync) { 597 CastMessage message = CreateCastMessage(); 598 std::string serialized_message; 599 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message)); 600 601 // Read bytes [0, 3]. 602 EXPECT_CALL(mock_socket_, 603 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _)) 604 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message), 605 Return(MessageFramer::MessageHeader::header_size()))) 606 .RetiresOnSaturation(); 607 // Read bytes [4, n-1]. 608 EXPECT_CALL(mock_socket_, 609 Read(NotNull(), 610 serialized_message.size() - 611 MessageFramer::MessageHeader::header_size(), 612 _)) 613 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message.substr( 614 MessageFramer::MessageHeader::header_size(), 615 serialized_message.size() - 616 MessageFramer::MessageHeader::header_size() - 1)), 617 Return(net::ERR_CONNECTION_RESET))) 618 .RetiresOnSaturation(); 619 EXPECT_CALL(delegate_, 620 OnError(&mock_socket_, CHANNEL_ERROR_TRANSPORT_ERROR, _)); 621 EXPECT_CALL(mock_socket_, CloseWithError(CHANNEL_ERROR_TRANSPORT_ERROR)); 622 transport_->StartReadLoop(); 623 } 624 625 TEST_F(CastTransportTest, TestReadCorruptedMessageSync) { 626 CastMessage message = CreateCastMessage(); 627 std::string serialized_message; 628 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message)); 629 630 // Corrupt the serialized message body(set it to X's). 631 for (size_t i = MessageFramer::MessageHeader::header_size(); 632 i < serialized_message.size(); 633 ++i) { 634 serialized_message[i] = 'x'; 635 } 636 637 // Read bytes [0, 3]. 638 EXPECT_CALL(mock_socket_, 639 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _)) 640 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message), 641 Return(MessageFramer::MessageHeader::header_size()))) 642 .RetiresOnSaturation(); 643 // Read bytes [4, n]. 644 EXPECT_CALL(mock_socket_, 645 Read(NotNull(), 646 serialized_message.size() - 647 MessageFramer::MessageHeader::header_size(), 648 _)) 649 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message.substr( 650 MessageFramer::MessageHeader::header_size(), 651 serialized_message.size() - 652 MessageFramer::MessageHeader::header_size() - 1)), 653 Return(serialized_message.size() - 654 MessageFramer::MessageHeader::header_size()))) 655 .RetiresOnSaturation(); 656 EXPECT_CALL(delegate_, 657 OnError(&mock_socket_, CHANNEL_ERROR_INVALID_MESSAGE, _)); 658 EXPECT_CALL(mock_socket_, CloseWithError(CHANNEL_ERROR_INVALID_MESSAGE)); 659 transport_->StartReadLoop(); 660 } 661 } // namespace cast_channel 662 } // namespace core_api 663 } // namespace extensions 664