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.
5 #include "extensions/browser/api/cast_channel/cast_transport.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/test/simple_test_clock.h"
13 #include "extensions/browser/api/cast_channel/cast_framer.h"
14 #include "extensions/browser/api/cast_channel/cast_socket.h"
15 #include "extensions/browser/api/cast_channel/cast_test_util.h"
16 #include "extensions/browser/api/cast_channel/logger.h"
17 #include "extensions/browser/api/cast_channel/logger_util.h"
18 #include "extensions/common/api/cast_channel/cast_channel.pb.h"
19 #include "net/base/completion_callback.h"
20 #include "net/base/net_errors.h"
21 #include "net/log/test_net_log.h"
22 #include "net/socket/socket.h"
23 #include "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h"
28 using testing::InSequence
;
29 using testing::Invoke
;
30 using testing::NotNull
;
31 using testing::Return
;
32 using testing::WithArg
;
34 namespace extensions
{
36 namespace cast_channel
{
39 const int kChannelId
= 0;
41 // Mockable placeholder for write completion events.
42 class CompleteHandler
{
45 MOCK_METHOD1(Complete
, void(int result
));
48 DISALLOW_COPY_AND_ASSIGN(CompleteHandler
);
51 // Creates a CastMessage proto with the bare minimum required fields set.
52 CastMessage
CreateCastMessage() {
54 output
.set_protocol_version(CastMessage::CASTV2_1_0
);
55 output
.set_namespace_("x");
56 output
.set_source_id("source");
57 output
.set_destination_id("destination");
58 output
.set_payload_type(CastMessage::STRING
);
59 output
.set_payload_utf8("payload");
63 // FIFO queue of completion callbacks. Outstanding write operations are
64 // Push()ed into the queue. Callback completion is simulated by invoking
65 // Pop() in the same order as Push().
66 class CompletionQueue
{
69 ~CompletionQueue() { CHECK_EQ(0u, cb_queue_
.size()); }
71 // Enqueues a pending completion callback.
72 void Push(const net::CompletionCallback
& cb
) { cb_queue_
.push(cb
); }
73 // Runs the next callback and removes it from the queue.
75 CHECK_GT(cb_queue_
.size(), 0u);
76 cb_queue_
.front().Run(rv
);
81 std::queue
<net::CompletionCallback
> cb_queue_
;
82 DISALLOW_COPY_AND_ASSIGN(CompletionQueue
);
85 // GMock action that reads data from an IOBuffer and writes it to a string
88 // buf_idx (template parameter 0): 0-based index of the net::IOBuffer
89 // in the function mock arg list.
90 // size_idx (template parameter 1): 0-based index of the byte count arg.
91 // str: pointer to the string which will receive data from the buffer.
92 ACTION_TEMPLATE(ReadBufferToString
,
93 HAS_2_TEMPLATE_PARAMS(int, buf_idx
, int, size_idx
),
94 AND_1_VALUE_PARAMS(str
)) {
95 str
->assign(testing::get
<buf_idx
>(args
)->data(),
96 testing::get
<size_idx
>(args
));
99 // GMock action that writes data from a string to an IOBuffer.
101 // buf_idx (template parameter 0): 0-based index of the IOBuffer arg.
102 // str: the string containing data to be written to the IOBuffer.
103 ACTION_TEMPLATE(FillBufferFromString
,
104 HAS_1_TEMPLATE_PARAMS(int, buf_idx
),
105 AND_1_VALUE_PARAMS(str
)) {
106 memcpy(testing::get
<buf_idx
>(args
)->data(), str
.data(), str
.size());
109 // GMock action that enqueues a write completion callback in a queue.
111 // buf_idx (template parameter 0): 0-based index of the CompletionCallback.
112 // completion_queue: a pointer to the CompletionQueue.
113 ACTION_TEMPLATE(EnqueueCallback
,
114 HAS_1_TEMPLATE_PARAMS(int, cb_idx
),
115 AND_1_VALUE_PARAMS(completion_queue
)) {
116 completion_queue
->Push(testing::get
<cb_idx
>(args
));
121 class MockSocket
: public net::Socket
{
124 int(net::IOBuffer
* buf
,
126 const net::CompletionCallback
& callback
));
129 int(net::IOBuffer
* buf
,
131 const net::CompletionCallback
& callback
));
133 virtual int SetReceiveBufferSize(int32 size
) {
138 virtual int SetSendBufferSize(int32 size
) {
144 class CastTransportTest
: public testing::Test
{
148 new Logger(make_scoped_ptr
<base::Clock
>(new base::SimpleTestClock
),
150 delegate_
= new MockCastTransportDelegate
;
151 transport_
.reset(new CastTransportImpl(&mock_socket_
, kChannelId
,
152 CreateIPEndPointForTest(),
153 auth_type_
, logger_
));
154 transport_
->SetReadDelegate(make_scoped_ptr(delegate_
));
156 ~CastTransportTest() override
{}
159 // Runs all pending tasks in the message loop.
160 void RunPendingTasks() {
161 base::RunLoop run_loop
;
162 run_loop
.RunUntilIdle();
165 base::MessageLoop message_loop_
;
166 MockCastTransportDelegate
* delegate_
;
167 MockSocket mock_socket_
;
168 ChannelAuthType auth_type_
;
170 scoped_ptr
<CastTransport
> transport_
;
173 // ----------------------------------------------------------------------------
174 // Asynchronous write tests
175 TEST_F(CastTransportTest
, TestFullWriteAsync
) {
176 CompletionQueue socket_cbs
;
177 CompleteHandler write_handler
;
180 CastMessage message
= CreateCastMessage();
181 std::string serialized_message
;
182 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
184 EXPECT_CALL(mock_socket_
, Write(NotNull(), serialized_message
.size(), _
))
185 .WillOnce(DoAll(ReadBufferToString
<0, 1>(&output
),
186 EnqueueCallback
<2>(&socket_cbs
),
187 Return(net::ERR_IO_PENDING
)));
188 EXPECT_CALL(write_handler
, Complete(net::OK
));
189 transport_
->SendMessage(
191 base::Bind(&CompleteHandler::Complete
, base::Unretained(&write_handler
)));
193 socket_cbs
.Pop(serialized_message
.size());
195 EXPECT_EQ(serialized_message
, output
);
198 TEST_F(CastTransportTest
, TestPartialWritesAsync
) {
200 CompletionQueue socket_cbs
;
201 CompleteHandler write_handler
;
204 CastMessage message
= CreateCastMessage();
205 std::string serialized_message
;
206 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
208 // Only one byte is written.
209 EXPECT_CALL(mock_socket_
,
210 Write(NotNull(), static_cast<int>(serialized_message
.size()), _
))
211 .WillOnce(DoAll(ReadBufferToString
<0, 1>(&output
),
212 EnqueueCallback
<2>(&socket_cbs
),
213 Return(net::ERR_IO_PENDING
)));
214 // Remainder of bytes are written.
217 Write(NotNull(), static_cast<int>(serialized_message
.size() - 1), _
))
218 .WillOnce(DoAll(ReadBufferToString
<0, 1>(&output
),
219 EnqueueCallback
<2>(&socket_cbs
),
220 Return(net::ERR_IO_PENDING
)));
222 transport_
->SendMessage(
224 base::Bind(&CompleteHandler::Complete
, base::Unretained(&write_handler
)));
226 EXPECT_EQ(serialized_message
, output
);
230 EXPECT_CALL(write_handler
, Complete(net::OK
));
231 socket_cbs
.Pop(serialized_message
.size() - 1);
233 EXPECT_EQ(serialized_message
.substr(1, serialized_message
.size() - 1),
237 TEST_F(CastTransportTest
, TestWriteFailureAsync
) {
238 CompletionQueue socket_cbs
;
239 CompleteHandler write_handler
;
240 CastMessage message
= CreateCastMessage();
241 EXPECT_CALL(mock_socket_
, Write(NotNull(), _
, _
)).WillOnce(
242 DoAll(EnqueueCallback
<2>(&socket_cbs
), Return(net::ERR_IO_PENDING
)));
243 EXPECT_CALL(write_handler
, Complete(net::ERR_FAILED
));
244 EXPECT_CALL(*delegate_
, OnError(CHANNEL_ERROR_SOCKET_ERROR
));
245 transport_
->SendMessage(
247 base::Bind(&CompleteHandler::Complete
, base::Unretained(&write_handler
)));
249 socket_cbs
.Pop(net::ERR_CONNECTION_RESET
);
251 EXPECT_EQ(proto::SOCKET_WRITE
, logger_
->GetLastErrors(kChannelId
).event_type
);
252 EXPECT_EQ(net::ERR_CONNECTION_RESET
,
253 logger_
->GetLastErrors(kChannelId
).net_return_value
);
256 // ----------------------------------------------------------------------------
257 // Synchronous write tests
258 TEST_F(CastTransportTest
, TestFullWriteSync
) {
259 CompleteHandler write_handler
;
261 CastMessage message
= CreateCastMessage();
262 std::string serialized_message
;
263 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
264 EXPECT_CALL(mock_socket_
, Write(NotNull(), serialized_message
.size(), _
))
265 .WillOnce(DoAll(ReadBufferToString
<0, 1>(&output
),
266 Return(serialized_message
.size())));
267 EXPECT_CALL(write_handler
, Complete(net::OK
));
268 transport_
->SendMessage(
270 base::Bind(&CompleteHandler::Complete
, base::Unretained(&write_handler
)));
272 EXPECT_EQ(serialized_message
, output
);
275 TEST_F(CastTransportTest
, TestPartialWritesSync
) {
277 CompleteHandler write_handler
;
280 CastMessage message
= CreateCastMessage();
281 std::string serialized_message
;
282 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
284 // Only one byte is written.
285 EXPECT_CALL(mock_socket_
, Write(NotNull(), serialized_message
.size(), _
))
286 .WillOnce(DoAll(ReadBufferToString
<0, 1>(&output
), Return(1)));
287 // Remainder of bytes are written.
288 EXPECT_CALL(mock_socket_
, Write(NotNull(), serialized_message
.size() - 1, _
))
289 .WillOnce(DoAll(ReadBufferToString
<0, 1>(&output
),
290 Return(serialized_message
.size() - 1)));
292 EXPECT_CALL(write_handler
, Complete(net::OK
));
293 transport_
->SendMessage(
295 base::Bind(&CompleteHandler::Complete
, base::Unretained(&write_handler
)));
297 EXPECT_EQ(serialized_message
.substr(1, serialized_message
.size() - 1),
301 TEST_F(CastTransportTest
, TestWriteFailureSync
) {
302 CompleteHandler write_handler
;
303 CastMessage message
= CreateCastMessage();
304 EXPECT_CALL(mock_socket_
, Write(NotNull(), _
, _
))
305 .WillOnce(Return(net::ERR_CONNECTION_RESET
));
306 EXPECT_CALL(write_handler
, Complete(net::ERR_FAILED
));
307 transport_
->SendMessage(
309 base::Bind(&CompleteHandler::Complete
, base::Unretained(&write_handler
)));
311 EXPECT_EQ(proto::SOCKET_WRITE
, logger_
->GetLastErrors(kChannelId
).event_type
);
312 EXPECT_EQ(net::ERR_CONNECTION_RESET
,
313 logger_
->GetLastErrors(kChannelId
).net_return_value
);
316 // ----------------------------------------------------------------------------
317 // Asynchronous read tests
318 TEST_F(CastTransportTest
, TestFullReadAsync
) {
320 CompletionQueue socket_cbs
;
322 CastMessage message
= CreateCastMessage();
323 std::string serialized_message
;
324 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
325 EXPECT_CALL(*delegate_
, Start());
327 // Read bytes [0, 3].
328 EXPECT_CALL(mock_socket_
,
329 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
330 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
),
331 EnqueueCallback
<2>(&socket_cbs
),
332 Return(net::ERR_IO_PENDING
)));
334 // Read bytes [4, n].
335 EXPECT_CALL(mock_socket_
,
337 serialized_message
.size() -
338 MessageFramer::MessageHeader::header_size(),
340 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
.substr(
341 MessageFramer::MessageHeader::header_size(),
342 serialized_message
.size() -
343 MessageFramer::MessageHeader::header_size())),
344 EnqueueCallback
<2>(&socket_cbs
),
345 Return(net::ERR_IO_PENDING
)))
346 .RetiresOnSaturation();
348 EXPECT_CALL(*delegate_
, OnMessage(EqualsProto(message
)));
349 EXPECT_CALL(mock_socket_
,
350 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
351 .WillOnce(Return(net::ERR_IO_PENDING
));
354 socket_cbs
.Pop(MessageFramer::MessageHeader::header_size());
355 socket_cbs
.Pop(serialized_message
.size() -
356 MessageFramer::MessageHeader::header_size());
360 TEST_F(CastTransportTest
, TestPartialReadAsync
) {
362 CompletionQueue socket_cbs
;
364 CastMessage message
= CreateCastMessage();
365 std::string serialized_message
;
366 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
368 EXPECT_CALL(*delegate_
, Start());
370 // Read bytes [0, 3].
371 EXPECT_CALL(mock_socket_
,
372 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
373 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
),
374 EnqueueCallback
<2>(&socket_cbs
),
375 Return(net::ERR_IO_PENDING
)))
376 .RetiresOnSaturation();
377 // Read bytes [4, n-1].
378 EXPECT_CALL(mock_socket_
,
380 serialized_message
.size() -
381 MessageFramer::MessageHeader::header_size(),
383 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
.substr(
384 MessageFramer::MessageHeader::header_size(),
385 serialized_message
.size() -
386 MessageFramer::MessageHeader::header_size() - 1)),
387 EnqueueCallback
<2>(&socket_cbs
),
388 Return(net::ERR_IO_PENDING
)))
389 .RetiresOnSaturation();
391 EXPECT_CALL(mock_socket_
, Read(NotNull(), 1, _
))
392 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
.substr(
393 serialized_message
.size() - 1, 1)),
394 EnqueueCallback
<2>(&socket_cbs
),
395 Return(net::ERR_IO_PENDING
)))
396 .RetiresOnSaturation();
397 EXPECT_CALL(*delegate_
, OnMessage(EqualsProto(message
)));
399 socket_cbs
.Pop(MessageFramer::MessageHeader::header_size());
400 socket_cbs
.Pop(serialized_message
.size() -
401 MessageFramer::MessageHeader::header_size() - 1);
402 EXPECT_CALL(mock_socket_
,
403 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
404 .WillOnce(Return(net::ERR_IO_PENDING
));
408 TEST_F(CastTransportTest
, TestReadErrorInHeaderAsync
) {
409 CompletionQueue socket_cbs
;
411 CastMessage message
= CreateCastMessage();
412 std::string serialized_message
;
413 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
415 EXPECT_CALL(*delegate_
, Start());
417 // Read bytes [0, 3].
418 EXPECT_CALL(mock_socket_
,
419 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
420 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
),
421 EnqueueCallback
<2>(&socket_cbs
),
422 Return(net::ERR_IO_PENDING
)))
423 .RetiresOnSaturation();
425 EXPECT_CALL(*delegate_
, OnError(CHANNEL_ERROR_SOCKET_ERROR
));
427 // Header read failure.
428 socket_cbs
.Pop(net::ERR_CONNECTION_RESET
);
429 EXPECT_EQ(proto::SOCKET_READ
, logger_
->GetLastErrors(kChannelId
).event_type
);
430 EXPECT_EQ(net::ERR_CONNECTION_RESET
,
431 logger_
->GetLastErrors(kChannelId
).net_return_value
);
434 TEST_F(CastTransportTest
, TestReadErrorInBodyAsync
) {
435 CompletionQueue socket_cbs
;
437 CastMessage message
= CreateCastMessage();
438 std::string serialized_message
;
439 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
441 EXPECT_CALL(*delegate_
, Start());
443 // Read bytes [0, 3].
444 EXPECT_CALL(mock_socket_
,
445 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
446 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
),
447 EnqueueCallback
<2>(&socket_cbs
),
448 Return(net::ERR_IO_PENDING
)))
449 .RetiresOnSaturation();
450 // Read bytes [4, n-1].
451 EXPECT_CALL(mock_socket_
,
453 serialized_message
.size() -
454 MessageFramer::MessageHeader::header_size(),
456 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
.substr(
457 MessageFramer::MessageHeader::header_size(),
458 serialized_message
.size() -
459 MessageFramer::MessageHeader::header_size() - 1)),
460 EnqueueCallback
<2>(&socket_cbs
),
461 Return(net::ERR_IO_PENDING
)))
462 .RetiresOnSaturation();
463 EXPECT_CALL(*delegate_
, OnError(CHANNEL_ERROR_SOCKET_ERROR
));
465 // Header read is OK.
466 socket_cbs
.Pop(MessageFramer::MessageHeader::header_size());
468 socket_cbs
.Pop(net::ERR_CONNECTION_RESET
);
469 EXPECT_EQ(proto::SOCKET_READ
, logger_
->GetLastErrors(kChannelId
).event_type
);
470 EXPECT_EQ(net::ERR_CONNECTION_RESET
,
471 logger_
->GetLastErrors(kChannelId
).net_return_value
);
474 TEST_F(CastTransportTest
, TestReadCorruptedMessageAsync
) {
475 CompletionQueue socket_cbs
;
477 CastMessage message
= CreateCastMessage();
478 std::string serialized_message
;
479 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
481 // Corrupt the serialized message body(set it to X's).
482 for (size_t i
= MessageFramer::MessageHeader::header_size();
483 i
< serialized_message
.size();
485 serialized_message
[i
] = 'x';
488 EXPECT_CALL(*delegate_
, Start());
489 // Read bytes [0, 3].
490 EXPECT_CALL(mock_socket_
,
491 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
492 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
),
493 EnqueueCallback
<2>(&socket_cbs
),
494 Return(net::ERR_IO_PENDING
)))
495 .RetiresOnSaturation();
496 // Read bytes [4, n].
497 EXPECT_CALL(mock_socket_
,
499 serialized_message
.size() -
500 MessageFramer::MessageHeader::header_size(),
502 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
.substr(
503 MessageFramer::MessageHeader::header_size(),
504 serialized_message
.size() -
505 MessageFramer::MessageHeader::header_size() - 1)),
506 EnqueueCallback
<2>(&socket_cbs
),
507 Return(net::ERR_IO_PENDING
)))
508 .RetiresOnSaturation();
510 EXPECT_CALL(*delegate_
, OnError(CHANNEL_ERROR_INVALID_MESSAGE
));
512 socket_cbs
.Pop(MessageFramer::MessageHeader::header_size());
513 socket_cbs
.Pop(serialized_message
.size() -
514 MessageFramer::MessageHeader::header_size());
517 // ----------------------------------------------------------------------------
518 // Synchronous read tests
519 TEST_F(CastTransportTest
, TestFullReadSync
) {
521 CastMessage message
= CreateCastMessage();
522 std::string serialized_message
;
523 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
525 EXPECT_CALL(*delegate_
, Start());
527 // Read bytes [0, 3].
528 EXPECT_CALL(mock_socket_
,
529 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
530 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
),
531 Return(MessageFramer::MessageHeader::header_size())))
532 .RetiresOnSaturation();
533 // Read bytes [4, n].
534 EXPECT_CALL(mock_socket_
,
536 serialized_message
.size() -
537 MessageFramer::MessageHeader::header_size(),
539 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
.substr(
540 MessageFramer::MessageHeader::header_size(),
541 serialized_message
.size() -
542 MessageFramer::MessageHeader::header_size())),
543 Return(serialized_message
.size() -
544 MessageFramer::MessageHeader::header_size())))
545 .RetiresOnSaturation();
546 EXPECT_CALL(*delegate_
, OnMessage(EqualsProto(message
)));
547 // Async result in order to discontinue the read loop.
548 EXPECT_CALL(mock_socket_
,
549 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
550 .WillOnce(Return(net::ERR_IO_PENDING
));
554 TEST_F(CastTransportTest
, TestPartialReadSync
) {
557 CastMessage message
= CreateCastMessage();
558 std::string serialized_message
;
559 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
561 EXPECT_CALL(*delegate_
, Start());
563 // Read bytes [0, 3].
564 EXPECT_CALL(mock_socket_
,
565 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
566 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
),
567 Return(MessageFramer::MessageHeader::header_size())))
568 .RetiresOnSaturation();
569 // Read bytes [4, n-1].
570 EXPECT_CALL(mock_socket_
,
572 serialized_message
.size() -
573 MessageFramer::MessageHeader::header_size(),
575 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
.substr(
576 MessageFramer::MessageHeader::header_size(),
577 serialized_message
.size() -
578 MessageFramer::MessageHeader::header_size() - 1)),
579 Return(serialized_message
.size() -
580 MessageFramer::MessageHeader::header_size() - 1)))
581 .RetiresOnSaturation();
583 EXPECT_CALL(mock_socket_
, Read(NotNull(), 1, _
))
584 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
.substr(
585 serialized_message
.size() - 1, 1)),
587 .RetiresOnSaturation();
588 EXPECT_CALL(*delegate_
, OnMessage(EqualsProto(message
)));
589 EXPECT_CALL(mock_socket_
,
590 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
591 .WillOnce(Return(net::ERR_IO_PENDING
));
595 TEST_F(CastTransportTest
, TestReadErrorInHeaderSync
) {
597 CastMessage message
= CreateCastMessage();
598 std::string serialized_message
;
599 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
601 EXPECT_CALL(*delegate_
, Start());
603 // Read bytes [0, 3].
604 EXPECT_CALL(mock_socket_
,
605 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
606 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
),
607 Return(net::ERR_CONNECTION_RESET
)))
608 .RetiresOnSaturation();
609 EXPECT_CALL(*delegate_
, OnError(CHANNEL_ERROR_SOCKET_ERROR
));
613 TEST_F(CastTransportTest
, TestReadErrorInBodySync
) {
614 CastMessage message
= CreateCastMessage();
615 std::string serialized_message
;
616 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
618 EXPECT_CALL(*delegate_
, Start());
620 // Read bytes [0, 3].
621 EXPECT_CALL(mock_socket_
,
622 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
623 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
),
624 Return(MessageFramer::MessageHeader::header_size())))
625 .RetiresOnSaturation();
626 // Read bytes [4, n-1].
627 EXPECT_CALL(mock_socket_
,
629 serialized_message
.size() -
630 MessageFramer::MessageHeader::header_size(),
632 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
.substr(
633 MessageFramer::MessageHeader::header_size(),
634 serialized_message
.size() -
635 MessageFramer::MessageHeader::header_size() - 1)),
636 Return(net::ERR_CONNECTION_RESET
)))
637 .RetiresOnSaturation();
638 EXPECT_CALL(*delegate_
, OnError(CHANNEL_ERROR_SOCKET_ERROR
));
640 EXPECT_EQ(proto::SOCKET_READ
, logger_
->GetLastErrors(kChannelId
).event_type
);
641 EXPECT_EQ(net::ERR_CONNECTION_RESET
,
642 logger_
->GetLastErrors(kChannelId
).net_return_value
);
645 TEST_F(CastTransportTest
, TestReadCorruptedMessageSync
) {
647 CastMessage message
= CreateCastMessage();
648 std::string serialized_message
;
649 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
651 // Corrupt the serialized message body(set it to X's).
652 for (size_t i
= MessageFramer::MessageHeader::header_size();
653 i
< serialized_message
.size();
655 serialized_message
[i
] = 'x';
658 EXPECT_CALL(*delegate_
, Start());
660 // Read bytes [0, 3].
661 EXPECT_CALL(mock_socket_
,
662 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
663 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
),
664 Return(MessageFramer::MessageHeader::header_size())))
665 .RetiresOnSaturation();
666 // Read bytes [4, n].
667 EXPECT_CALL(mock_socket_
,
669 serialized_message
.size() -
670 MessageFramer::MessageHeader::header_size(),
672 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
.substr(
673 MessageFramer::MessageHeader::header_size(),
674 serialized_message
.size() -
675 MessageFramer::MessageHeader::header_size() - 1)),
676 Return(serialized_message
.size() -
677 MessageFramer::MessageHeader::header_size())))
678 .RetiresOnSaturation();
679 EXPECT_CALL(*delegate_
, OnError(CHANNEL_ERROR_INVALID_MESSAGE
));
682 } // namespace cast_channel
684 } // namespace extensions