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/test/simple_test_tick_clock.h"
11 #include "extensions/browser/api/cast_channel/cast_framer.h"
12 #include "extensions/browser/api/cast_channel/cast_socket.h"
13 #include "extensions/browser/api/cast_channel/cast_test_util.h"
14 #include "extensions/browser/api/cast_channel/logger.h"
15 #include "extensions/browser/api/cast_channel/logger_util.h"
16 #include "extensions/common/api/cast_channel/cast_channel.pb.h"
17 #include "net/base/completion_callback.h"
18 #include "net/base/net_errors.h"
19 #include "net/log/test_net_log.h"
20 #include "net/socket/socket.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
26 using testing::InSequence
;
27 using testing::Invoke
;
28 using testing::NotNull
;
29 using testing::Return
;
30 using testing::WithArg
;
32 namespace extensions
{
34 namespace cast_channel
{
37 const int kChannelId
= 0;
39 // Mockable placeholder for write completion events.
40 class CompleteHandler
{
43 MOCK_METHOD1(Complete
, void(int result
));
46 DISALLOW_COPY_AND_ASSIGN(CompleteHandler
);
49 // Creates a CastMessage proto with the bare minimum required fields set.
50 CastMessage
CreateCastMessage() {
52 output
.set_protocol_version(CastMessage::CASTV2_1_0
);
53 output
.set_namespace_("x");
54 output
.set_source_id("source");
55 output
.set_destination_id("destination");
56 output
.set_payload_type(CastMessage::STRING
);
57 output
.set_payload_utf8("payload");
61 // FIFO queue of completion callbacks. Outstanding write operations are
62 // Push()ed into the queue. Callback completion is simulated by invoking
63 // Pop() in the same order as Push().
64 class CompletionQueue
{
67 ~CompletionQueue() { CHECK_EQ(0u, cb_queue_
.size()); }
69 // Enqueues a pending completion callback.
70 void Push(const net::CompletionCallback
& cb
) { cb_queue_
.push(cb
); }
71 // Runs the next callback and removes it from the queue.
73 CHECK_GT(cb_queue_
.size(), 0u);
74 cb_queue_
.front().Run(rv
);
79 std::queue
<net::CompletionCallback
> cb_queue_
;
80 DISALLOW_COPY_AND_ASSIGN(CompletionQueue
);
83 // GMock action that reads data from an IOBuffer and writes it to a string
86 // buf_idx (template parameter 0): 0-based index of the net::IOBuffer
87 // in the function mock arg list.
88 // size_idx (template parameter 1): 0-based index of the byte count arg.
89 // str: pointer to the string which will receive data from the buffer.
90 ACTION_TEMPLATE(ReadBufferToString
,
91 HAS_2_TEMPLATE_PARAMS(int, buf_idx
, int, size_idx
),
92 AND_1_VALUE_PARAMS(str
)) {
93 str
->assign(testing::get
<buf_idx
>(args
)->data(),
94 testing::get
<size_idx
>(args
));
97 // GMock action that writes data from a string to an IOBuffer.
99 // buf_idx (template parameter 0): 0-based index of the IOBuffer arg.
100 // str: the string containing data to be written to the IOBuffer.
101 ACTION_TEMPLATE(FillBufferFromString
,
102 HAS_1_TEMPLATE_PARAMS(int, buf_idx
),
103 AND_1_VALUE_PARAMS(str
)) {
104 memcpy(testing::get
<buf_idx
>(args
)->data(), str
.data(), str
.size());
107 // GMock action that enqueues a write completion callback in a queue.
109 // buf_idx (template parameter 0): 0-based index of the CompletionCallback.
110 // completion_queue: a pointer to the CompletionQueue.
111 ACTION_TEMPLATE(EnqueueCallback
,
112 HAS_1_TEMPLATE_PARAMS(int, cb_idx
),
113 AND_1_VALUE_PARAMS(completion_queue
)) {
114 completion_queue
->Push(testing::get
<cb_idx
>(args
));
119 class MockSocket
: public net::Socket
{
122 int(net::IOBuffer
* buf
,
124 const net::CompletionCallback
& callback
));
127 int(net::IOBuffer
* buf
,
129 const net::CompletionCallback
& callback
));
131 virtual int SetReceiveBufferSize(int32 size
) {
136 virtual int SetSendBufferSize(int32 size
) {
142 class CastTransportTest
: public testing::Test
{
145 : logger_(new Logger(
146 scoped_ptr
<base::TickClock
>(new base::SimpleTestTickClock
),
147 base::TimeTicks())) {
148 delegate_
= new MockCastTransportDelegate
;
149 transport_
.reset(new CastTransportImpl(&mock_socket_
, kChannelId
,
150 CreateIPEndPointForTest(),
151 auth_type_
, logger_
));
152 transport_
->SetReadDelegate(make_scoped_ptr(delegate_
));
154 ~CastTransportTest() override
{}
157 MockCastTransportDelegate
* delegate_
;
158 MockSocket mock_socket_
;
159 ChannelAuthType auth_type_
;
161 scoped_ptr
<CastTransport
> transport_
;
164 // ----------------------------------------------------------------------------
165 // Asynchronous write tests
166 TEST_F(CastTransportTest
, TestFullWriteAsync
) {
167 CompletionQueue socket_cbs
;
168 CompleteHandler write_handler
;
171 CastMessage message
= CreateCastMessage();
172 std::string serialized_message
;
173 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
175 EXPECT_CALL(mock_socket_
, Write(NotNull(), serialized_message
.size(), _
))
176 .WillOnce(DoAll(ReadBufferToString
<0, 1>(&output
),
177 EnqueueCallback
<2>(&socket_cbs
),
178 Return(net::ERR_IO_PENDING
)));
179 EXPECT_CALL(write_handler
, Complete(net::OK
));
180 transport_
->SendMessage(
182 base::Bind(&CompleteHandler::Complete
, base::Unretained(&write_handler
)));
183 socket_cbs
.Pop(serialized_message
.size());
184 EXPECT_EQ(serialized_message
, output
);
187 TEST_F(CastTransportTest
, TestPartialWritesAsync
) {
189 CompletionQueue socket_cbs
;
190 CompleteHandler write_handler
;
193 CastMessage message
= CreateCastMessage();
194 std::string serialized_message
;
195 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
197 // Only one byte is written.
198 EXPECT_CALL(mock_socket_
,
199 Write(NotNull(), static_cast<int>(serialized_message
.size()), _
))
200 .WillOnce(DoAll(ReadBufferToString
<0, 1>(&output
),
201 EnqueueCallback
<2>(&socket_cbs
),
202 Return(net::ERR_IO_PENDING
)));
203 // Remainder of bytes are written.
206 Write(NotNull(), static_cast<int>(serialized_message
.size() - 1), _
))
207 .WillOnce(DoAll(ReadBufferToString
<0, 1>(&output
),
208 EnqueueCallback
<2>(&socket_cbs
),
209 Return(net::ERR_IO_PENDING
)));
211 transport_
->SendMessage(
213 base::Bind(&CompleteHandler::Complete
, base::Unretained(&write_handler
)));
214 EXPECT_EQ(serialized_message
, output
);
217 EXPECT_CALL(write_handler
, Complete(net::OK
));
218 socket_cbs
.Pop(serialized_message
.size() - 1);
219 EXPECT_EQ(serialized_message
.substr(1, serialized_message
.size() - 1),
223 TEST_F(CastTransportTest
, TestWriteFailureAsync
) {
224 CompletionQueue socket_cbs
;
225 CompleteHandler write_handler
;
226 CastMessage message
= CreateCastMessage();
227 EXPECT_CALL(mock_socket_
, Write(NotNull(), _
, _
)).WillOnce(
228 DoAll(EnqueueCallback
<2>(&socket_cbs
), Return(net::ERR_IO_PENDING
)));
229 EXPECT_CALL(write_handler
, Complete(net::ERR_FAILED
));
230 transport_
->SendMessage(
232 base::Bind(&CompleteHandler::Complete
, base::Unretained(&write_handler
)));
233 socket_cbs
.Pop(net::ERR_CONNECTION_RESET
);
234 EXPECT_EQ(proto::SOCKET_WRITE
, logger_
->GetLastErrors(kChannelId
).event_type
);
235 EXPECT_EQ(net::ERR_CONNECTION_RESET
,
236 logger_
->GetLastErrors(kChannelId
).net_return_value
);
239 // ----------------------------------------------------------------------------
240 // Synchronous write tests
241 TEST_F(CastTransportTest
, TestFullWriteSync
) {
242 CompleteHandler write_handler
;
244 CastMessage message
= CreateCastMessage();
245 std::string serialized_message
;
246 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
247 EXPECT_CALL(mock_socket_
, Write(NotNull(), serialized_message
.size(), _
))
248 .WillOnce(DoAll(ReadBufferToString
<0, 1>(&output
),
249 Return(serialized_message
.size())));
250 EXPECT_CALL(write_handler
, Complete(net::OK
));
251 transport_
->SendMessage(
253 base::Bind(&CompleteHandler::Complete
, base::Unretained(&write_handler
)));
254 EXPECT_EQ(serialized_message
, output
);
257 TEST_F(CastTransportTest
, TestPartialWritesSync
) {
259 CompleteHandler write_handler
;
262 CastMessage message
= CreateCastMessage();
263 std::string serialized_message
;
264 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
266 // Only one byte is written.
267 EXPECT_CALL(mock_socket_
, Write(NotNull(), serialized_message
.size(), _
))
268 .WillOnce(DoAll(ReadBufferToString
<0, 1>(&output
), Return(1)));
269 // Remainder of bytes are written.
270 EXPECT_CALL(mock_socket_
, Write(NotNull(), serialized_message
.size() - 1, _
))
271 .WillOnce(DoAll(ReadBufferToString
<0, 1>(&output
),
272 Return(serialized_message
.size() - 1)));
274 EXPECT_CALL(write_handler
, Complete(net::OK
));
275 transport_
->SendMessage(
277 base::Bind(&CompleteHandler::Complete
, base::Unretained(&write_handler
)));
278 EXPECT_EQ(serialized_message
.substr(1, serialized_message
.size() - 1),
282 TEST_F(CastTransportTest
, TestWriteFailureSync
) {
283 CompleteHandler write_handler
;
284 CastMessage message
= CreateCastMessage();
285 EXPECT_CALL(mock_socket_
, Write(NotNull(), _
, _
))
286 .WillOnce(Return(net::ERR_CONNECTION_RESET
));
287 EXPECT_CALL(write_handler
, Complete(net::ERR_FAILED
));
288 transport_
->SendMessage(
290 base::Bind(&CompleteHandler::Complete
, base::Unretained(&write_handler
)));
291 EXPECT_EQ(proto::SOCKET_WRITE
, logger_
->GetLastErrors(kChannelId
).event_type
);
292 EXPECT_EQ(net::ERR_CONNECTION_RESET
,
293 logger_
->GetLastErrors(kChannelId
).net_return_value
);
296 // ----------------------------------------------------------------------------
297 // Asynchronous read tests
298 TEST_F(CastTransportTest
, TestFullReadAsync
) {
300 CompletionQueue socket_cbs
;
302 CastMessage message
= CreateCastMessage();
303 std::string serialized_message
;
304 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
306 // Read bytes [0, 3].
307 EXPECT_CALL(mock_socket_
,
308 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
309 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
),
310 EnqueueCallback
<2>(&socket_cbs
),
311 Return(net::ERR_IO_PENDING
)));
313 // Read bytes [4, n].
314 EXPECT_CALL(mock_socket_
,
316 serialized_message
.size() -
317 MessageFramer::MessageHeader::header_size(),
319 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
.substr(
320 MessageFramer::MessageHeader::header_size(),
321 serialized_message
.size() -
322 MessageFramer::MessageHeader::header_size())),
323 EnqueueCallback
<2>(&socket_cbs
),
324 Return(net::ERR_IO_PENDING
)))
325 .RetiresOnSaturation();
327 EXPECT_CALL(*delegate_
, OnMessage(EqualsProto(message
)));
328 EXPECT_CALL(mock_socket_
,
329 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
330 .WillOnce(Return(net::ERR_IO_PENDING
));
332 socket_cbs
.Pop(MessageFramer::MessageHeader::header_size());
333 socket_cbs
.Pop(serialized_message
.size() -
334 MessageFramer::MessageHeader::header_size());
337 TEST_F(CastTransportTest
, TestPartialReadAsync
) {
339 CompletionQueue socket_cbs
;
341 CastMessage message
= CreateCastMessage();
342 std::string serialized_message
;
343 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
345 // Read bytes [0, 3].
346 EXPECT_CALL(mock_socket_
,
347 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
348 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
),
349 EnqueueCallback
<2>(&socket_cbs
),
350 Return(net::ERR_IO_PENDING
)))
351 .RetiresOnSaturation();
352 // Read bytes [4, n-1].
353 EXPECT_CALL(mock_socket_
,
355 serialized_message
.size() -
356 MessageFramer::MessageHeader::header_size(),
358 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
.substr(
359 MessageFramer::MessageHeader::header_size(),
360 serialized_message
.size() -
361 MessageFramer::MessageHeader::header_size() - 1)),
362 EnqueueCallback
<2>(&socket_cbs
),
363 Return(net::ERR_IO_PENDING
)))
364 .RetiresOnSaturation();
366 EXPECT_CALL(mock_socket_
, Read(NotNull(), 1, _
))
367 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
.substr(
368 serialized_message
.size() - 1, 1)),
369 EnqueueCallback
<2>(&socket_cbs
),
370 Return(net::ERR_IO_PENDING
)))
371 .RetiresOnSaturation();
372 EXPECT_CALL(*delegate_
, OnMessage(EqualsProto(message
)));
374 socket_cbs
.Pop(MessageFramer::MessageHeader::header_size());
375 socket_cbs
.Pop(serialized_message
.size() -
376 MessageFramer::MessageHeader::header_size() - 1);
377 EXPECT_CALL(mock_socket_
,
378 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
379 .WillOnce(Return(net::ERR_IO_PENDING
));
383 TEST_F(CastTransportTest
, TestReadErrorInHeaderAsync
) {
384 CompletionQueue socket_cbs
;
386 CastMessage message
= CreateCastMessage();
387 std::string serialized_message
;
388 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
390 // Read bytes [0, 3].
391 EXPECT_CALL(mock_socket_
,
392 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
393 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
),
394 EnqueueCallback
<2>(&socket_cbs
),
395 Return(net::ERR_IO_PENDING
)))
396 .RetiresOnSaturation();
398 EXPECT_CALL(*delegate_
, OnError(CHANNEL_ERROR_SOCKET_ERROR
));
400 // Header read failure.
401 socket_cbs
.Pop(net::ERR_CONNECTION_RESET
);
402 EXPECT_EQ(proto::SOCKET_READ
, logger_
->GetLastErrors(kChannelId
).event_type
);
403 EXPECT_EQ(net::ERR_CONNECTION_RESET
,
404 logger_
->GetLastErrors(kChannelId
).net_return_value
);
407 TEST_F(CastTransportTest
, TestReadErrorInBodyAsync
) {
408 CompletionQueue socket_cbs
;
410 CastMessage message
= CreateCastMessage();
411 std::string serialized_message
;
412 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
414 // Read bytes [0, 3].
415 EXPECT_CALL(mock_socket_
,
416 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
417 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
),
418 EnqueueCallback
<2>(&socket_cbs
),
419 Return(net::ERR_IO_PENDING
)))
420 .RetiresOnSaturation();
421 // Read bytes [4, n-1].
422 EXPECT_CALL(mock_socket_
,
424 serialized_message
.size() -
425 MessageFramer::MessageHeader::header_size(),
427 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
.substr(
428 MessageFramer::MessageHeader::header_size(),
429 serialized_message
.size() -
430 MessageFramer::MessageHeader::header_size() - 1)),
431 EnqueueCallback
<2>(&socket_cbs
),
432 Return(net::ERR_IO_PENDING
)))
433 .RetiresOnSaturation();
434 EXPECT_CALL(*delegate_
, OnError(CHANNEL_ERROR_SOCKET_ERROR
));
436 // Header read is OK.
437 socket_cbs
.Pop(MessageFramer::MessageHeader::header_size());
439 socket_cbs
.Pop(net::ERR_CONNECTION_RESET
);
440 EXPECT_EQ(proto::SOCKET_READ
, logger_
->GetLastErrors(kChannelId
).event_type
);
441 EXPECT_EQ(net::ERR_CONNECTION_RESET
,
442 logger_
->GetLastErrors(kChannelId
).net_return_value
);
445 TEST_F(CastTransportTest
, TestReadCorruptedMessageAsync
) {
446 CompletionQueue socket_cbs
;
448 CastMessage message
= CreateCastMessage();
449 std::string serialized_message
;
450 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
452 // Corrupt the serialized message body(set it to X's).
453 for (size_t i
= MessageFramer::MessageHeader::header_size();
454 i
< serialized_message
.size();
456 serialized_message
[i
] = 'x';
459 // Read bytes [0, 3].
460 EXPECT_CALL(mock_socket_
,
461 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
462 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
),
463 EnqueueCallback
<2>(&socket_cbs
),
464 Return(net::ERR_IO_PENDING
)))
465 .RetiresOnSaturation();
466 // Read bytes [4, n].
467 EXPECT_CALL(mock_socket_
,
469 serialized_message
.size() -
470 MessageFramer::MessageHeader::header_size(),
472 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
.substr(
473 MessageFramer::MessageHeader::header_size(),
474 serialized_message
.size() -
475 MessageFramer::MessageHeader::header_size() - 1)),
476 EnqueueCallback
<2>(&socket_cbs
),
477 Return(net::ERR_IO_PENDING
)))
478 .RetiresOnSaturation();
480 EXPECT_CALL(*delegate_
, OnError(CHANNEL_ERROR_INVALID_MESSAGE
));
482 socket_cbs
.Pop(MessageFramer::MessageHeader::header_size());
483 socket_cbs
.Pop(serialized_message
.size() -
484 MessageFramer::MessageHeader::header_size());
487 // ----------------------------------------------------------------------------
488 // Synchronous read tests
489 TEST_F(CastTransportTest
, TestFullReadSync
) {
491 CastMessage message
= CreateCastMessage();
492 std::string serialized_message
;
493 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
495 // Read bytes [0, 3].
496 EXPECT_CALL(mock_socket_
,
497 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
498 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
),
499 Return(MessageFramer::MessageHeader::header_size())))
500 .RetiresOnSaturation();
501 // Read bytes [4, n].
502 EXPECT_CALL(mock_socket_
,
504 serialized_message
.size() -
505 MessageFramer::MessageHeader::header_size(),
507 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
.substr(
508 MessageFramer::MessageHeader::header_size(),
509 serialized_message
.size() -
510 MessageFramer::MessageHeader::header_size())),
511 Return(serialized_message
.size() -
512 MessageFramer::MessageHeader::header_size())))
513 .RetiresOnSaturation();
514 EXPECT_CALL(*delegate_
, OnMessage(EqualsProto(message
)));
515 // Async result in order to discontinue the read loop.
516 EXPECT_CALL(mock_socket_
,
517 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
518 .WillOnce(Return(net::ERR_IO_PENDING
));
522 TEST_F(CastTransportTest
, TestPartialReadSync
) {
525 CastMessage message
= CreateCastMessage();
526 std::string serialized_message
;
527 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
529 // Read bytes [0, 3].
530 EXPECT_CALL(mock_socket_
,
531 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
532 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
),
533 Return(MessageFramer::MessageHeader::header_size())))
534 .RetiresOnSaturation();
535 // Read bytes [4, n-1].
536 EXPECT_CALL(mock_socket_
,
538 serialized_message
.size() -
539 MessageFramer::MessageHeader::header_size(),
541 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
.substr(
542 MessageFramer::MessageHeader::header_size(),
543 serialized_message
.size() -
544 MessageFramer::MessageHeader::header_size() - 1)),
545 Return(serialized_message
.size() -
546 MessageFramer::MessageHeader::header_size() - 1)))
547 .RetiresOnSaturation();
549 EXPECT_CALL(mock_socket_
, Read(NotNull(), 1, _
))
550 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
.substr(
551 serialized_message
.size() - 1, 1)),
553 .RetiresOnSaturation();
554 EXPECT_CALL(*delegate_
, OnMessage(EqualsProto(message
)));
555 EXPECT_CALL(mock_socket_
,
556 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
557 .WillOnce(Return(net::ERR_IO_PENDING
));
561 TEST_F(CastTransportTest
, TestReadErrorInHeaderSync
) {
563 CastMessage message
= CreateCastMessage();
564 std::string serialized_message
;
565 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
567 // Read bytes [0, 3].
568 EXPECT_CALL(mock_socket_
,
569 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
570 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
),
571 Return(net::ERR_CONNECTION_RESET
)))
572 .RetiresOnSaturation();
573 EXPECT_CALL(*delegate_
, OnError(CHANNEL_ERROR_SOCKET_ERROR
));
577 TEST_F(CastTransportTest
, TestReadErrorInBodySync
) {
578 CastMessage message
= CreateCastMessage();
579 std::string serialized_message
;
580 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
582 // Read bytes [0, 3].
583 EXPECT_CALL(mock_socket_
,
584 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _
))
585 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
),
586 Return(MessageFramer::MessageHeader::header_size())))
587 .RetiresOnSaturation();
588 // Read bytes [4, n-1].
589 EXPECT_CALL(mock_socket_
,
591 serialized_message
.size() -
592 MessageFramer::MessageHeader::header_size(),
594 .WillOnce(DoAll(FillBufferFromString
<0>(serialized_message
.substr(
595 MessageFramer::MessageHeader::header_size(),
596 serialized_message
.size() -
597 MessageFramer::MessageHeader::header_size() - 1)),
598 Return(net::ERR_CONNECTION_RESET
)))
599 .RetiresOnSaturation();
600 EXPECT_CALL(*delegate_
, OnError(CHANNEL_ERROR_SOCKET_ERROR
));
602 EXPECT_EQ(proto::SOCKET_READ
, logger_
->GetLastErrors(kChannelId
).event_type
);
603 EXPECT_EQ(net::ERR_CONNECTION_RESET
,
604 logger_
->GetLastErrors(kChannelId
).net_return_value
);
607 TEST_F(CastTransportTest
, TestReadCorruptedMessageSync
) {
609 CastMessage message
= CreateCastMessage();
610 std::string serialized_message
;
611 EXPECT_TRUE(MessageFramer::Serialize(message
, &serialized_message
));
613 // Corrupt the serialized message body(set it to X's).
614 for (size_t i
= MessageFramer::MessageHeader::header_size();
615 i
< serialized_message
.size();
617 serialized_message
[i
] = 'x';
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].
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(serialized_message
.size() -
637 MessageFramer::MessageHeader::header_size())))
638 .RetiresOnSaturation();
639 EXPECT_CALL(*delegate_
, OnError(CHANNEL_ERROR_INVALID_MESSAGE
));
642 } // namespace cast_channel
643 } // namespace core_api
644 } // namespace extensions