Delete chrome.mediaGalleriesPrivate because the functionality unique to it has since...
[chromium-blink-merge.git] / extensions / browser / api / cast_channel / cast_transport_unittest.cc
blobf160b5ad79830f7b176867b426613c85b9377630
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"
7 #include <stddef.h>
8 #include <queue>
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/logger.h"
14 #include "extensions/browser/api/cast_channel/logger_util.h"
15 #include "extensions/browser/api/cast_channel/test_util.h"
16 #include "extensions/common/api/cast_channel/cast_channel.pb.h"
17 #include "net/base/capturing_net_log.h"
18 #include "net/base/completion_callback.h"
19 #include "net/base/net_errors.h"
20 #include "net/socket/socket.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 using testing::_;
25 using testing::DoAll;
26 using testing::InSequence;
27 using testing::Invoke;
28 using testing::NotNull;
29 using testing::Return;
30 using testing::WithArg;
32 namespace extensions {
33 namespace core_api {
34 namespace cast_channel {
35 namespace {
36 // Mockable placeholder for write completion events.
37 class CompleteHandler {
38 public:
39 CompleteHandler() {}
40 MOCK_METHOD1(Complete, void(int result));
42 private:
43 DISALLOW_COPY_AND_ASSIGN(CompleteHandler);
46 // Creates a CastMessage proto with the bare minimum required fields set.
47 CastMessage CreateCastMessage() {
48 CastMessage output;
49 output.set_protocol_version(CastMessage::CASTV2_1_0);
50 output.set_namespace_("x");
51 output.set_source_id("source");
52 output.set_destination_id("destination");
53 output.set_payload_type(CastMessage::STRING);
54 output.set_payload_utf8("payload");
55 return output;
58 // FIFO queue of completion callbacks. Outstanding write operations are
59 // Push()ed into the queue. Callback completion is simulated by invoking
60 // Pop() in the same order as Push().
61 class CompletionQueue {
62 public:
63 CompletionQueue() {}
64 ~CompletionQueue() { CHECK_EQ(0u, cb_queue_.size()); }
66 // Enqueues a pending completion callback.
67 void Push(const net::CompletionCallback& cb) { cb_queue_.push(cb); }
68 // Runs the next callback and removes it from the queue.
69 void Pop(int rv) {
70 CHECK_GT(cb_queue_.size(), 0u);
71 cb_queue_.front().Run(rv);
72 cb_queue_.pop();
75 private:
76 std::queue<net::CompletionCallback> cb_queue_;
77 DISALLOW_COPY_AND_ASSIGN(CompletionQueue);
80 // GMock action that reads data from an IOBuffer and writes it to a string
81 // variable.
83 // buf_idx (template parameter 0): 0-based index of the net::IOBuffer
84 // in the function mock arg list.
85 // size_idx (template parameter 1): 0-based index of the byte count arg.
86 // str: pointer to the string which will receive data from the buffer.
87 ACTION_TEMPLATE(ReadBufferToString,
88 HAS_2_TEMPLATE_PARAMS(int, buf_idx, int, size_idx),
89 AND_1_VALUE_PARAMS(str)) {
90 str->assign(testing::get<buf_idx>(args)->data(),
91 testing::get<size_idx>(args));
94 // GMock action that writes data from a string to an IOBuffer.
96 // buf_idx (template parameter 0): 0-based index of the IOBuffer arg.
97 // str: the string containing data to be written to the IOBuffer.
98 ACTION_TEMPLATE(FillBufferFromString,
99 HAS_1_TEMPLATE_PARAMS(int, buf_idx),
100 AND_1_VALUE_PARAMS(str)) {
101 memcpy(testing::get<buf_idx>(args)->data(), str.data(), str.size());
104 // GMock action that enqueues a write completion callback in a queue.
106 // buf_idx (template parameter 0): 0-based index of the CompletionCallback.
107 // completion_queue: a pointer to the CompletionQueue.
108 ACTION_TEMPLATE(EnqueueCallback,
109 HAS_1_TEMPLATE_PARAMS(int, cb_idx),
110 AND_1_VALUE_PARAMS(completion_queue)) {
111 completion_queue->Push(testing::get<cb_idx>(args));
114 } // namespace
116 class MockSocket : public net::Socket {
117 public:
118 MOCK_METHOD3(Read,
119 int(net::IOBuffer* buf,
120 int buf_len,
121 const net::CompletionCallback& callback));
123 MOCK_METHOD3(Write,
124 int(net::IOBuffer* buf,
125 int buf_len,
126 const net::CompletionCallback& callback));
128 virtual int SetReceiveBufferSize(int32 size) {
129 NOTREACHED();
130 return 0;
133 virtual int SetSendBufferSize(int32 size) {
134 NOTREACHED();
135 return 0;
139 class CastTransportTest : public testing::Test {
140 public:
141 CastTransportTest()
142 : logger_(new Logger(
143 scoped_ptr<base::TickClock>(new base::SimpleTestTickClock),
144 base::TimeTicks())) {
145 delegate_ = new MockCastTransportDelegate;
146 transport_.reset(new CastTransportImpl(
147 &mock_socket_, 0, CreateIPEndPointForTest(), auth_type_, logger_));
148 transport_->SetReadDelegate(make_scoped_ptr(delegate_));
150 ~CastTransportTest() override {}
152 protected:
153 MockCastTransportDelegate* delegate_;
154 MockSocket mock_socket_;
155 ChannelAuthType auth_type_;
156 Logger* logger_;
157 scoped_ptr<CastTransport> transport_;
160 // ----------------------------------------------------------------------------
161 // Asynchronous write tests
162 TEST_F(CastTransportTest, TestFullWriteAsync) {
163 CompletionQueue socket_cbs;
164 CompleteHandler write_handler;
165 std::string output;
167 CastMessage message = CreateCastMessage();
168 std::string serialized_message;
169 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message));
171 EXPECT_CALL(mock_socket_, Write(NotNull(), serialized_message.size(), _))
172 .WillOnce(DoAll(ReadBufferToString<0, 1>(&output),
173 EnqueueCallback<2>(&socket_cbs),
174 Return(net::ERR_IO_PENDING)));
175 EXPECT_CALL(write_handler, Complete(net::OK));
176 transport_->SendMessage(
177 message,
178 base::Bind(&CompleteHandler::Complete, base::Unretained(&write_handler)));
179 socket_cbs.Pop(serialized_message.size());
180 EXPECT_EQ(serialized_message, output);
183 TEST_F(CastTransportTest, TestPartialWritesAsync) {
184 InSequence seq;
185 CompletionQueue socket_cbs;
186 CompleteHandler write_handler;
187 std::string output;
189 CastMessage message = CreateCastMessage();
190 std::string serialized_message;
191 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message));
193 // Only one byte is written.
194 EXPECT_CALL(mock_socket_,
195 Write(NotNull(), static_cast<int>(serialized_message.size()), _))
196 .WillOnce(DoAll(ReadBufferToString<0, 1>(&output),
197 EnqueueCallback<2>(&socket_cbs),
198 Return(net::ERR_IO_PENDING)));
199 // Remainder of bytes are written.
200 EXPECT_CALL(
201 mock_socket_,
202 Write(NotNull(), static_cast<int>(serialized_message.size() - 1), _))
203 .WillOnce(DoAll(ReadBufferToString<0, 1>(&output),
204 EnqueueCallback<2>(&socket_cbs),
205 Return(net::ERR_IO_PENDING)));
207 transport_->SendMessage(
208 message,
209 base::Bind(&CompleteHandler::Complete, base::Unretained(&write_handler)));
210 EXPECT_EQ(serialized_message, output);
211 socket_cbs.Pop(1);
213 EXPECT_CALL(write_handler, Complete(net::OK));
214 socket_cbs.Pop(serialized_message.size() - 1);
215 EXPECT_EQ(serialized_message.substr(1, serialized_message.size() - 1),
216 output);
219 TEST_F(CastTransportTest, TestWriteFailureAsync) {
220 CompletionQueue socket_cbs;
221 CompleteHandler write_handler;
222 CastMessage message = CreateCastMessage();
223 EXPECT_CALL(mock_socket_, Write(NotNull(), _, _)).WillOnce(
224 DoAll(EnqueueCallback<2>(&socket_cbs), Return(net::ERR_IO_PENDING)));
225 EXPECT_CALL(write_handler, Complete(net::ERR_FAILED));
226 transport_->SendMessage(
227 message,
228 base::Bind(&CompleteHandler::Complete, base::Unretained(&write_handler)));
229 socket_cbs.Pop(net::ERR_CONNECTION_RESET);
232 // ----------------------------------------------------------------------------
233 // Synchronous write tests
234 TEST_F(CastTransportTest, TestFullWriteSync) {
235 CompleteHandler write_handler;
236 std::string output;
237 CastMessage message = CreateCastMessage();
238 std::string serialized_message;
239 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message));
240 EXPECT_CALL(mock_socket_, Write(NotNull(), serialized_message.size(), _))
241 .WillOnce(DoAll(ReadBufferToString<0, 1>(&output),
242 Return(serialized_message.size())));
243 EXPECT_CALL(write_handler, Complete(net::OK));
244 transport_->SendMessage(
245 message,
246 base::Bind(&CompleteHandler::Complete, base::Unretained(&write_handler)));
247 EXPECT_EQ(serialized_message, output);
250 TEST_F(CastTransportTest, TestPartialWritesSync) {
251 InSequence seq;
252 CompleteHandler write_handler;
253 std::string output;
255 CastMessage message = CreateCastMessage();
256 std::string serialized_message;
257 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message));
259 // Only one byte is written.
260 EXPECT_CALL(mock_socket_, Write(NotNull(), serialized_message.size(), _))
261 .WillOnce(DoAll(ReadBufferToString<0, 1>(&output), Return(1)));
262 // Remainder of bytes are written.
263 EXPECT_CALL(mock_socket_, Write(NotNull(), serialized_message.size() - 1, _))
264 .WillOnce(DoAll(ReadBufferToString<0, 1>(&output),
265 Return(serialized_message.size() - 1)));
267 EXPECT_CALL(write_handler, Complete(net::OK));
268 transport_->SendMessage(
269 message,
270 base::Bind(&CompleteHandler::Complete, base::Unretained(&write_handler)));
271 EXPECT_EQ(serialized_message.substr(1, serialized_message.size() - 1),
272 output);
275 TEST_F(CastTransportTest, TestWriteFailureSync) {
276 CompleteHandler write_handler;
277 CastMessage message = CreateCastMessage();
278 EXPECT_CALL(mock_socket_, Write(NotNull(), _, _))
279 .WillOnce(Return(net::ERR_CONNECTION_RESET));
280 EXPECT_CALL(write_handler, Complete(net::ERR_FAILED));
281 transport_->SendMessage(
282 message,
283 base::Bind(&CompleteHandler::Complete, base::Unretained(&write_handler)));
286 // ----------------------------------------------------------------------------
287 // Asynchronous read tests
288 TEST_F(CastTransportTest, TestFullReadAsync) {
289 InSequence s;
290 CompletionQueue socket_cbs;
292 CastMessage message = CreateCastMessage();
293 std::string serialized_message;
294 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message));
296 // Read bytes [0, 3].
297 EXPECT_CALL(mock_socket_,
298 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _))
299 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message),
300 EnqueueCallback<2>(&socket_cbs),
301 Return(net::ERR_IO_PENDING)));
303 // Read bytes [4, n].
304 EXPECT_CALL(mock_socket_,
305 Read(NotNull(),
306 serialized_message.size() -
307 MessageFramer::MessageHeader::header_size(),
309 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message.substr(
310 MessageFramer::MessageHeader::header_size(),
311 serialized_message.size() -
312 MessageFramer::MessageHeader::header_size())),
313 EnqueueCallback<2>(&socket_cbs),
314 Return(net::ERR_IO_PENDING)))
315 .RetiresOnSaturation();
317 EXPECT_CALL(*delegate_, OnMessage(EqualsProto(message)));
318 EXPECT_CALL(mock_socket_,
319 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _))
320 .WillOnce(Return(net::ERR_IO_PENDING));
321 transport_->Start();
322 socket_cbs.Pop(MessageFramer::MessageHeader::header_size());
323 socket_cbs.Pop(serialized_message.size() -
324 MessageFramer::MessageHeader::header_size());
327 TEST_F(CastTransportTest, TestPartialReadAsync) {
328 InSequence s;
329 CompletionQueue socket_cbs;
331 CastMessage message = CreateCastMessage();
332 std::string serialized_message;
333 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message));
335 // Read bytes [0, 3].
336 EXPECT_CALL(mock_socket_,
337 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _))
338 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message),
339 EnqueueCallback<2>(&socket_cbs),
340 Return(net::ERR_IO_PENDING)))
341 .RetiresOnSaturation();
342 // Read bytes [4, n-1].
343 EXPECT_CALL(mock_socket_,
344 Read(NotNull(),
345 serialized_message.size() -
346 MessageFramer::MessageHeader::header_size(),
348 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message.substr(
349 MessageFramer::MessageHeader::header_size(),
350 serialized_message.size() -
351 MessageFramer::MessageHeader::header_size() - 1)),
352 EnqueueCallback<2>(&socket_cbs),
353 Return(net::ERR_IO_PENDING)))
354 .RetiresOnSaturation();
355 // Read final byte.
356 EXPECT_CALL(mock_socket_, Read(NotNull(), 1, _))
357 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message.substr(
358 serialized_message.size() - 1, 1)),
359 EnqueueCallback<2>(&socket_cbs),
360 Return(net::ERR_IO_PENDING)))
361 .RetiresOnSaturation();
362 EXPECT_CALL(*delegate_, OnMessage(EqualsProto(message)));
363 transport_->Start();
364 socket_cbs.Pop(MessageFramer::MessageHeader::header_size());
365 socket_cbs.Pop(serialized_message.size() -
366 MessageFramer::MessageHeader::header_size() - 1);
367 EXPECT_CALL(mock_socket_,
368 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _))
369 .WillOnce(Return(net::ERR_IO_PENDING));
370 socket_cbs.Pop(1);
373 TEST_F(CastTransportTest, TestReadErrorInHeaderAsync) {
374 CompletionQueue socket_cbs;
376 CastMessage message = CreateCastMessage();
377 std::string serialized_message;
378 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message));
380 // Read bytes [0, 3].
381 EXPECT_CALL(mock_socket_,
382 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _))
383 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message),
384 EnqueueCallback<2>(&socket_cbs),
385 Return(net::ERR_IO_PENDING)))
386 .RetiresOnSaturation();
388 EXPECT_CALL(*delegate_, OnError(CHANNEL_ERROR_SOCKET_ERROR, _));
389 transport_->Start();
390 // Header read failure.
391 socket_cbs.Pop(net::ERR_CONNECTION_RESET);
394 TEST_F(CastTransportTest, TestReadErrorInBodyAsync) {
395 CompletionQueue socket_cbs;
397 CastMessage message = CreateCastMessage();
398 std::string serialized_message;
399 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message));
401 // Read bytes [0, 3].
402 EXPECT_CALL(mock_socket_,
403 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _))
404 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message),
405 EnqueueCallback<2>(&socket_cbs),
406 Return(net::ERR_IO_PENDING)))
407 .RetiresOnSaturation();
408 // Read bytes [4, n-1].
409 EXPECT_CALL(mock_socket_,
410 Read(NotNull(),
411 serialized_message.size() -
412 MessageFramer::MessageHeader::header_size(),
414 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message.substr(
415 MessageFramer::MessageHeader::header_size(),
416 serialized_message.size() -
417 MessageFramer::MessageHeader::header_size() - 1)),
418 EnqueueCallback<2>(&socket_cbs),
419 Return(net::ERR_IO_PENDING)))
420 .RetiresOnSaturation();
421 EXPECT_CALL(*delegate_, OnError(CHANNEL_ERROR_SOCKET_ERROR, _));
422 transport_->Start();
423 // Header read is OK.
424 socket_cbs.Pop(MessageFramer::MessageHeader::header_size());
425 // Body read fails.
426 socket_cbs.Pop(net::ERR_CONNECTION_RESET);
429 TEST_F(CastTransportTest, TestReadCorruptedMessageAsync) {
430 CompletionQueue socket_cbs;
432 CastMessage message = CreateCastMessage();
433 std::string serialized_message;
434 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message));
436 // Corrupt the serialized message body(set it to X's).
437 for (size_t i = MessageFramer::MessageHeader::header_size();
438 i < serialized_message.size();
439 ++i) {
440 serialized_message[i] = 'x';
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].
451 EXPECT_CALL(mock_socket_,
452 Read(NotNull(),
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();
464 EXPECT_CALL(*delegate_, OnError(CHANNEL_ERROR_INVALID_MESSAGE, _));
465 transport_->Start();
466 socket_cbs.Pop(MessageFramer::MessageHeader::header_size());
467 socket_cbs.Pop(serialized_message.size() -
468 MessageFramer::MessageHeader::header_size());
471 // ----------------------------------------------------------------------------
472 // Synchronous read tests
473 TEST_F(CastTransportTest, TestFullReadSync) {
474 InSequence s;
475 CastMessage message = CreateCastMessage();
476 std::string serialized_message;
477 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message));
479 // Read bytes [0, 3].
480 EXPECT_CALL(mock_socket_,
481 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _))
482 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message),
483 Return(MessageFramer::MessageHeader::header_size())))
484 .RetiresOnSaturation();
485 // Read bytes [4, n].
486 EXPECT_CALL(mock_socket_,
487 Read(NotNull(),
488 serialized_message.size() -
489 MessageFramer::MessageHeader::header_size(),
491 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message.substr(
492 MessageFramer::MessageHeader::header_size(),
493 serialized_message.size() -
494 MessageFramer::MessageHeader::header_size())),
495 Return(serialized_message.size() -
496 MessageFramer::MessageHeader::header_size())))
497 .RetiresOnSaturation();
498 EXPECT_CALL(*delegate_, OnMessage(EqualsProto(message)));
499 // Async result in order to discontinue the read loop.
500 EXPECT_CALL(mock_socket_,
501 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _))
502 .WillOnce(Return(net::ERR_IO_PENDING));
503 transport_->Start();
506 TEST_F(CastTransportTest, TestPartialReadSync) {
507 InSequence s;
509 CastMessage message = CreateCastMessage();
510 std::string serialized_message;
511 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message));
513 // Read bytes [0, 3].
514 EXPECT_CALL(mock_socket_,
515 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _))
516 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message),
517 Return(MessageFramer::MessageHeader::header_size())))
518 .RetiresOnSaturation();
519 // Read bytes [4, n-1].
520 EXPECT_CALL(mock_socket_,
521 Read(NotNull(),
522 serialized_message.size() -
523 MessageFramer::MessageHeader::header_size(),
525 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message.substr(
526 MessageFramer::MessageHeader::header_size(),
527 serialized_message.size() -
528 MessageFramer::MessageHeader::header_size() - 1)),
529 Return(serialized_message.size() -
530 MessageFramer::MessageHeader::header_size() - 1)))
531 .RetiresOnSaturation();
532 // Read final byte.
533 EXPECT_CALL(mock_socket_, Read(NotNull(), 1, _))
534 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message.substr(
535 serialized_message.size() - 1, 1)),
536 Return(1)))
537 .RetiresOnSaturation();
538 EXPECT_CALL(*delegate_, OnMessage(EqualsProto(message)));
539 EXPECT_CALL(mock_socket_,
540 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _))
541 .WillOnce(Return(net::ERR_IO_PENDING));
542 transport_->Start();
545 TEST_F(CastTransportTest, TestReadErrorInHeaderSync) {
546 InSequence s;
547 CastMessage message = CreateCastMessage();
548 std::string serialized_message;
549 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message));
551 // Read bytes [0, 3].
552 EXPECT_CALL(mock_socket_,
553 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _))
554 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message),
555 Return(net::ERR_CONNECTION_RESET)))
556 .RetiresOnSaturation();
557 EXPECT_CALL(*delegate_, OnError(CHANNEL_ERROR_SOCKET_ERROR, _));
558 transport_->Start();
561 TEST_F(CastTransportTest, TestReadErrorInBodySync) {
562 CastMessage message = CreateCastMessage();
563 std::string serialized_message;
564 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message));
566 // Read bytes [0, 3].
567 EXPECT_CALL(mock_socket_,
568 Read(NotNull(), MessageFramer::MessageHeader::header_size(), _))
569 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message),
570 Return(MessageFramer::MessageHeader::header_size())))
571 .RetiresOnSaturation();
572 // Read bytes [4, n-1].
573 EXPECT_CALL(mock_socket_,
574 Read(NotNull(),
575 serialized_message.size() -
576 MessageFramer::MessageHeader::header_size(),
578 .WillOnce(DoAll(FillBufferFromString<0>(serialized_message.substr(
579 MessageFramer::MessageHeader::header_size(),
580 serialized_message.size() -
581 MessageFramer::MessageHeader::header_size() - 1)),
582 Return(net::ERR_CONNECTION_RESET)))
583 .RetiresOnSaturation();
584 EXPECT_CALL(*delegate_, OnError(CHANNEL_ERROR_SOCKET_ERROR, _));
585 transport_->Start();
588 TEST_F(CastTransportTest, TestReadCorruptedMessageSync) {
589 InSequence s;
590 CastMessage message = CreateCastMessage();
591 std::string serialized_message;
592 EXPECT_TRUE(MessageFramer::Serialize(message, &serialized_message));
594 // Corrupt the serialized message body(set it to X's).
595 for (size_t i = MessageFramer::MessageHeader::header_size();
596 i < serialized_message.size();
597 ++i) {
598 serialized_message[i] = 'x';
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].
608 EXPECT_CALL(mock_socket_,
609 Read(NotNull(),
610 serialized_message.size() -
611 MessageFramer::MessageHeader::header_size(),
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(serialized_message.size() -
618 MessageFramer::MessageHeader::header_size())))
619 .RetiresOnSaturation();
620 EXPECT_CALL(*delegate_, OnError(CHANNEL_ERROR_INVALID_MESSAGE, _));
621 transport_->Start();
623 } // namespace cast_channel
624 } // namespace core_api
625 } // namespace extensions