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_socket.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/sys_byteorder.h"
14 #include "base/test/simple_test_tick_clock.h"
15 #include "base/timer/mock_timer.h"
16 #include "extensions/browser/api/cast_channel/cast_auth_util.h"
17 #include "extensions/browser/api/cast_channel/cast_framer.h"
18 #include "extensions/browser/api/cast_channel/cast_message_util.h"
19 #include "extensions/browser/api/cast_channel/cast_transport.h"
20 #include "extensions/browser/api/cast_channel/logger.h"
21 #include "extensions/browser/api/cast_channel/test_util.h"
22 #include "extensions/common/api/cast_channel/cast_channel.pb.h"
23 #include "net/base/address_list.h"
24 #include "net/base/capturing_net_log.h"
25 #include "net/base/net_errors.h"
26 #include "net/base/net_util.h"
27 #include "net/socket/socket_test_util.h"
28 #include "net/socket/ssl_client_socket.h"
29 #include "net/socket/tcp_client_socket.h"
30 #include "net/ssl/ssl_info.h"
31 #include "testing/gmock/include/gmock/gmock.h"
32 #include "testing/gtest/include/gtest/gtest.h"
34 const int64 kDistantTimeoutMillis
= 100000; // 100 seconds (never hit).
38 using ::testing::DoAll
;
39 using ::testing::Invoke
;
40 using ::testing::InvokeArgument
;
41 using ::testing::NotNull
;
42 using ::testing::Return
;
43 using ::testing::SaveArg
;
45 namespace extensions
{
47 namespace cast_channel
{
48 const char kAuthNamespace
[] = "urn:x-cast:com.google.cast.tp.deviceauth";
50 // Returns an auth challenge message inline.
51 CastMessage
CreateAuthChallenge() {
53 CreateAuthChallengeMessage(&output
);
57 // Returns an auth challenge response message inline.
58 CastMessage
CreateAuthReply() {
60 output
.set_protocol_version(CastMessage::CASTV2_1_0
);
61 output
.set_source_id("sender-0");
62 output
.set_destination_id("receiver-0");
63 output
.set_payload_type(CastMessage::BINARY
);
64 output
.set_payload_binary("abcd");
65 output
.set_namespace_(kAuthNamespace
);
69 CastMessage
CreateTestMessage() {
70 CastMessage test_message
;
71 test_message
.set_protocol_version(CastMessage::CASTV2_1_0
);
72 test_message
.set_namespace_("ns");
73 test_message
.set_source_id("source");
74 test_message
.set_destination_id("dest");
75 test_message
.set_payload_type(CastMessage::STRING
);
76 test_message
.set_payload_utf8("payload");
80 class MockTCPSocket
: public net::TCPClientSocket
{
82 explicit MockTCPSocket(const net::MockConnect
& connect_data
)
83 : TCPClientSocket(net::AddressList(), nullptr, net::NetLog::Source()),
84 connect_data_(connect_data
),
87 explicit MockTCPSocket(bool do_nothing
)
88 : TCPClientSocket(net::AddressList(), nullptr, net::NetLog::Source()) {
90 do_nothing_
= do_nothing
;
93 virtual int Connect(const net::CompletionCallback
& callback
) {
95 // Stall the I/O event loop.
96 return net::ERR_IO_PENDING
;
99 if (connect_data_
.mode
== net::ASYNC
) {
100 CHECK_NE(connect_data_
.result
, net::ERR_IO_PENDING
);
101 base::MessageLoop::current()->PostTask(
103 base::Bind(callback
, connect_data_
.result
));
104 return net::ERR_IO_PENDING
;
106 return connect_data_
.result
;
110 virtual bool SetKeepAlive(bool enable
, int delay
) {
111 // Always return true in tests
115 virtual bool SetNoDelay(bool no_delay
) {
116 // Always return true in tests
121 int(net::IOBuffer
*, int, const net::CompletionCallback
&));
123 int(net::IOBuffer
*, int, const net::CompletionCallback
&));
125 virtual void Disconnect() {
126 // Do nothing in tests
130 net::MockConnect connect_data_
;
133 DISALLOW_COPY_AND_ASSIGN(MockTCPSocket
);
136 class MockDelegate
: public CastTransport::Delegate
{
139 virtual ~MockDelegate() {}
140 MOCK_METHOD2(OnError
,
141 void(ChannelError error_state
, const LastErrors
& last_errors
));
142 MOCK_METHOD1(OnMessage
, void(const CastMessage
& message
));
143 MOCK_METHOD0(Start
, void());
146 DISALLOW_COPY_AND_ASSIGN(MockDelegate
);
149 class CompleteHandler
{
152 MOCK_METHOD1(OnCloseComplete
, void(int result
));
153 MOCK_METHOD1(OnConnectComplete
, void(ChannelError error_state
));
154 MOCK_METHOD1(OnWriteComplete
, void(int result
));
155 MOCK_METHOD1(OnReadComplete
, void(int result
));
158 DISALLOW_COPY_AND_ASSIGN(CompleteHandler
);
161 class TestCastSocket
: public CastSocketImpl
{
163 static scoped_ptr
<TestCastSocket
> Create(
165 long device_capabilities
= cast_channel::CastDeviceCapability::NONE
) {
166 return scoped_ptr
<TestCastSocket
>(
167 new TestCastSocket(CreateIPEndPointForTest(), CHANNEL_AUTH_TYPE_SSL
,
168 kDistantTimeoutMillis
, logger
, device_capabilities
));
171 static scoped_ptr
<TestCastSocket
> CreateSecure(
173 long device_capabilities
= cast_channel::CastDeviceCapability::NONE
) {
174 return scoped_ptr
<TestCastSocket
>(new TestCastSocket(
175 CreateIPEndPointForTest(), CHANNEL_AUTH_TYPE_SSL_VERIFIED
,
176 kDistantTimeoutMillis
, logger
, device_capabilities
));
179 explicit TestCastSocket(const net::IPEndPoint
& ip_endpoint
,
180 ChannelAuthType channel_auth
,
183 long device_capabilities
)
184 : CastSocketImpl("some_extension_id",
188 base::TimeDelta::FromMilliseconds(timeout_ms
),
191 device_capabilities
),
194 extract_cert_result_(true),
195 verify_challenge_result_(true),
196 verify_challenge_disallow_(false),
197 tcp_unresponsive_(false),
198 mock_timer_(new base::MockTimer(false, false)),
199 mock_transport_(nullptr) {}
201 ~TestCastSocket() override
{}
203 void SetupMockTransport() {
204 mock_transport_
= new MockCastTransport
;
205 SetTransportForTesting(make_scoped_ptr(mock_transport_
));
208 // Socket connection helpers.
209 void SetupTcp1Connect(net::IoMode mode
, int result
) {
210 tcp_connect_data_
[0].reset(new net::MockConnect(mode
, result
));
212 void SetupTcp2Connect(net::IoMode mode
, int result
) {
213 tcp_connect_data_
[1].reset(new net::MockConnect(mode
, result
));
215 void SetupSsl1Connect(net::IoMode mode
, int result
) {
216 ssl_connect_data_
[0].reset(new net::MockConnect(mode
, result
));
218 void SetupSsl2Connect(net::IoMode mode
, int result
) {
219 ssl_connect_data_
[1].reset(new net::MockConnect(mode
, result
));
222 // Socket I/O helpers.
223 void AddWriteResult(const net::MockWrite
& write
) {
224 writes_
.push_back(write
);
226 void AddWriteResult(net::IoMode mode
, int result
) {
227 AddWriteResult(net::MockWrite(mode
, result
));
229 void AddWriteResultForData(net::IoMode mode
, const std::string
& msg
) {
230 AddWriteResult(mode
, msg
.size());
232 void AddReadResult(const net::MockRead
& read
) {
233 reads_
.push_back(read
);
235 void AddReadResult(net::IoMode mode
, int result
) {
236 AddReadResult(net::MockRead(mode
, result
));
238 void AddReadResultForData(net::IoMode mode
, const std::string
& data
) {
239 AddReadResult(net::MockRead(mode
, data
.c_str(), data
.size()));
242 // Helpers for modifying other connection-related behaviors.
243 void SetupTcp1ConnectUnresponsive() { tcp_unresponsive_
= true; }
245 void SetExtractCertResult(bool value
) {
246 extract_cert_result_
= value
;
249 void SetVerifyChallengeResult(bool value
) {
250 verify_challenge_result_
= value
;
253 void TriggerTimeout() {
257 bool TestVerifyChannelPolicyNone() {
258 AuthResult authResult
;
259 return VerifyChannelPolicy(authResult
);
262 bool TestVerifyChannelPolicyAudioOnly() {
263 AuthResult authResult
;
264 authResult
.channel_policies
|= AuthResult::POLICY_AUDIO_ONLY
;
265 return VerifyChannelPolicy(authResult
);
268 void DisallowVerifyChallengeResult() { verify_challenge_disallow_
= true; }
270 MockCastTransport
* GetMockTransport() {
271 CHECK(mock_transport_
);
272 return mock_transport_
;
276 scoped_ptr
<net::TCPClientSocket
> CreateTcpSocket() override
{
277 if (tcp_unresponsive_
) {
278 return scoped_ptr
<net::TCPClientSocket
>(new MockTCPSocket(true));
280 net::MockConnect
* connect_data
= tcp_connect_data_
[connect_index_
].get();
281 connect_data
->peer_addr
= ip_
;
282 return scoped_ptr
<net::TCPClientSocket
>(new MockTCPSocket(*connect_data
));
286 scoped_ptr
<net::SSLClientSocket
> CreateSslSocket(
287 scoped_ptr
<net::StreamSocket
> socket
) override
{
288 net::MockConnect
* connect_data
= ssl_connect_data_
[connect_index_
].get();
289 connect_data
->peer_addr
= ip_
;
292 ssl_data_
.reset(new net::StaticSocketDataProvider(
293 reads_
.data(), reads_
.size(), writes_
.data(), writes_
.size()));
294 ssl_data_
->set_connect_data(*connect_data
);
295 // NOTE: net::MockTCPClientSocket inherits from net::SSLClientSocket !!
296 return scoped_ptr
<net::SSLClientSocket
>(
297 new net::MockTCPClientSocket(
298 net::AddressList(), &capturing_net_log_
, ssl_data_
.get()));
301 bool ExtractPeerCert(std::string
* cert
) override
{
302 if (extract_cert_result_
)
303 cert
->assign("dummy_test_cert");
304 return extract_cert_result_
;
307 bool VerifyChallengeReply() override
{
308 EXPECT_FALSE(verify_challenge_disallow_
);
309 return verify_challenge_result_
;
312 base::Timer
* GetTimer() override
{ return mock_timer_
.get(); }
314 net::CapturingNetLog capturing_net_log_
;
316 // Simulated connect data
317 scoped_ptr
<net::MockConnect
> tcp_connect_data_
[2];
318 scoped_ptr
<net::MockConnect
> ssl_connect_data_
[2];
319 // Simulated read / write data
320 std::vector
<net::MockWrite
> writes_
;
321 std::vector
<net::MockRead
> reads_
;
322 scoped_ptr
<net::SocketDataProvider
> ssl_data_
;
323 // Number of times Connect method is called
324 size_t connect_index_
;
325 // Simulated result of peer cert extraction.
326 bool extract_cert_result_
;
327 // Simulated result of verifying challenge reply.
328 bool verify_challenge_result_
;
329 bool verify_challenge_disallow_
;
330 // If true, makes TCP connection process stall. For timeout testing.
331 bool tcp_unresponsive_
;
332 scoped_ptr
<base::MockTimer
> mock_timer_
;
333 MockCastTransport
* mock_transport_
;
335 DISALLOW_COPY_AND_ASSIGN(TestCastSocket
);
338 class CastSocketTest
: public testing::Test
{
341 : logger_(new Logger(
342 scoped_ptr
<base::TickClock
>(new base::SimpleTestTickClock
),
344 read_delegate_(new MockDelegate
) {}
345 ~CastSocketTest() override
{}
347 void SetUp() override
{
348 EXPECT_CALL(*read_delegate_
, OnMessage(_
)).Times(0);
349 EXPECT_CALL(*read_delegate_
, OnError(_
, _
)).Times(0);
352 void TearDown() override
{
354 EXPECT_CALL(handler_
, OnCloseComplete(net::OK
));
355 socket_
->Close(base::Bind(&CompleteHandler::OnCloseComplete
,
356 base::Unretained(&handler_
)));
360 void CreateCastSocket() { socket_
= TestCastSocket::Create(logger_
); }
362 void CreateCastSocketSecure() {
363 socket_
= TestCastSocket::CreateSecure(logger_
);
366 void HandleAuthHandshake() {
367 socket_
->SetupMockTransport();
368 CastMessage challenge_proto
= CreateAuthChallenge();
369 EXPECT_CALL(*socket_
->GetMockTransport(),
370 SendMessage(EqualsProto(challenge_proto
), _
))
371 .WillOnce(RunCompletionCallback
<1>(net::OK
));
372 EXPECT_CALL(*socket_
->GetMockTransport(), Start());
373 EXPECT_CALL(handler_
, OnConnectComplete(CHANNEL_ERROR_NONE
));
374 socket_
->Connect(read_delegate_
.Pass(),
375 base::Bind(&CompleteHandler::OnConnectComplete
,
376 base::Unretained(&handler_
)));
378 socket_
->GetMockTransport()->current_delegate()->OnMessage(
384 // Runs all pending tasks in the message loop.
385 void RunPendingTasks() {
386 base::RunLoop run_loop
;
387 run_loop
.RunUntilIdle();
390 base::MessageLoop message_loop_
;
392 scoped_ptr
<TestCastSocket
> socket_
;
393 CompleteHandler handler_
;
394 scoped_ptr
<MockDelegate
> read_delegate_
;
397 DISALLOW_COPY_AND_ASSIGN(CastSocketTest
);
400 // Tests connecting and closing the socket.
401 TEST_F(CastSocketTest
, TestConnectAndClose
) {
403 socket_
->SetupMockTransport();
404 socket_
->SetupTcp1Connect(net::SYNCHRONOUS
, net::OK
);
405 socket_
->SetupSsl1Connect(net::SYNCHRONOUS
, net::OK
);
407 EXPECT_CALL(handler_
, OnConnectComplete(CHANNEL_ERROR_NONE
));
408 socket_
->Connect(read_delegate_
.Pass(),
409 base::Bind(&CompleteHandler::OnConnectComplete
,
410 base::Unretained(&handler_
)));
413 EXPECT_EQ(cast_channel::READY_STATE_OPEN
, socket_
->ready_state());
414 EXPECT_EQ(cast_channel::CHANNEL_ERROR_NONE
, socket_
->error_state());
416 EXPECT_CALL(handler_
, OnCloseComplete(net::OK
));
417 socket_
->Close(base::Bind(&CompleteHandler::OnCloseComplete
,
418 base::Unretained(&handler_
)));
419 EXPECT_EQ(cast_channel::READY_STATE_CLOSED
, socket_
->ready_state());
420 EXPECT_EQ(cast_channel::CHANNEL_ERROR_NONE
, socket_
->error_state());
423 // Tests that the following connection flow works:
424 // - TCP connection succeeds (async)
425 // - SSL connection succeeds (async)
426 TEST_F(CastSocketTest
, TestConnect
) {
428 socket_
->SetupTcp1Connect(net::ASYNC
, net::OK
);
429 socket_
->SetupSsl1Connect(net::ASYNC
, net::OK
);
430 socket_
->AddReadResult(net::ASYNC
, net::ERR_IO_PENDING
);
432 EXPECT_CALL(handler_
, OnConnectComplete(CHANNEL_ERROR_NONE
));
433 socket_
->Connect(read_delegate_
.Pass(),
434 base::Bind(&CompleteHandler::OnConnectComplete
,
435 base::Unretained(&handler_
)));
438 EXPECT_EQ(cast_channel::READY_STATE_OPEN
, socket_
->ready_state());
439 EXPECT_EQ(cast_channel::CHANNEL_ERROR_NONE
, socket_
->error_state());
442 // Tests that the following connection flow works:
443 // - TCP connection fails (async)
444 TEST_F(CastSocketTest
, TestConnectFails
) {
446 socket_
->SetupTcp1Connect(net::ASYNC
, net::ERR_FAILED
);
448 EXPECT_CALL(handler_
, OnConnectComplete(CHANNEL_ERROR_CONNECT_ERROR
));
449 socket_
->Connect(read_delegate_
.Pass(),
450 base::Bind(&CompleteHandler::OnConnectComplete
,
451 base::Unretained(&handler_
)));
454 EXPECT_EQ(cast_channel::READY_STATE_CLOSED
, socket_
->ready_state());
455 EXPECT_EQ(cast_channel::CHANNEL_ERROR_CONNECT_ERROR
, socket_
->error_state());
458 // Test that the following connection flow works:
459 // - TCP connection succeeds (async)
460 // - SSL connection fails with cert error (async)
461 // - Cert is extracted successfully
462 // - Second TCP connection succeeds (async)
463 // - Second SSL connection succeeds (async)
464 TEST_F(CastSocketTest
, TestConnectTwoStep
) {
466 socket_
->SetupMockTransport();
467 socket_
->SetupTcp1Connect(net::ASYNC
, net::OK
);
468 socket_
->SetupSsl1Connect(net::ASYNC
, net::ERR_CERT_AUTHORITY_INVALID
);
469 socket_
->SetupTcp2Connect(net::ASYNC
, net::OK
);
470 socket_
->SetupSsl2Connect(net::ASYNC
, net::OK
);
472 EXPECT_CALL(handler_
, OnConnectComplete(CHANNEL_ERROR_NONE
));
473 socket_
->Connect(read_delegate_
.Pass(),
474 base::Bind(&CompleteHandler::OnConnectComplete
,
475 base::Unretained(&handler_
)));
477 EXPECT_EQ(cast_channel::READY_STATE_OPEN
, socket_
->ready_state());
478 EXPECT_EQ(cast_channel::CHANNEL_ERROR_NONE
, socket_
->error_state());
481 // Test that the following connection flow works:
482 // - TCP connection succeeds (async)
483 // - SSL connection fails with cert error (async)
484 // - Cert is extracted successfully
485 // - Second TCP connection succeeds (async)
486 // - Second SSL connection fails (async)
487 // - The flow should NOT be tried again
488 TEST_F(CastSocketTest
, TestConnectMaxTwoAttempts
) {
490 socket_
->SetupTcp1Connect(net::ASYNC
, net::OK
);
491 socket_
->SetupSsl1Connect(net::ASYNC
, net::ERR_CERT_AUTHORITY_INVALID
);
492 socket_
->SetupTcp2Connect(net::ASYNC
, net::OK
);
493 socket_
->SetupSsl2Connect(net::ASYNC
, net::ERR_CERT_AUTHORITY_INVALID
);
495 EXPECT_CALL(handler_
, OnConnectComplete(CHANNEL_ERROR_AUTHENTICATION_ERROR
));
496 socket_
->Connect(read_delegate_
.Pass(),
497 base::Bind(&CompleteHandler::OnConnectComplete
,
498 base::Unretained(&handler_
)));
501 EXPECT_EQ(cast_channel::READY_STATE_CLOSED
, socket_
->ready_state());
502 EXPECT_EQ(cast_channel::CHANNEL_ERROR_AUTHENTICATION_ERROR
,
503 socket_
->error_state());
506 // Tests that the following connection flow works:
507 // - TCP connection succeeds (async)
508 // - SSL connection fails with cert error (async)
509 // - Cert is extracted successfully
510 // - Second TCP connection succeeds (async)
511 // - Second SSL connection succeeds (async)
512 // - Challenge request is sent (async)
513 // - Challenge response is received (async)
514 // - Credentials are verified successfuly
515 TEST_F(CastSocketTest
, TestConnectFullSecureFlowAsync
) {
516 CreateCastSocketSecure();
517 socket_
->SetupTcp1Connect(net::ASYNC
, net::OK
);
518 socket_
->SetupSsl1Connect(net::ASYNC
, net::ERR_CERT_AUTHORITY_INVALID
);
519 socket_
->SetupTcp2Connect(net::ASYNC
, net::OK
);
520 socket_
->SetupSsl2Connect(net::ASYNC
, net::OK
);
522 HandleAuthHandshake();
524 EXPECT_EQ(cast_channel::READY_STATE_OPEN
, socket_
->ready_state());
525 EXPECT_EQ(cast_channel::CHANNEL_ERROR_NONE
, socket_
->error_state());
528 // Tests that the following connection flow works:
529 // - TCP connection succeeds (sync)
530 // - SSL connection fails with cert error (sync)
531 // - Cert is extracted successfully
532 // - Second TCP connection succeeds (sync)
533 // - Second SSL connection succeeds (sync)
534 // - Challenge request is sent (sync)
535 // - Challenge response is received (sync)
536 // - Credentials are verified successfuly
537 TEST_F(CastSocketTest
, TestConnectFullSecureFlowSync
) {
538 CreateCastSocketSecure();
539 socket_
->SetupTcp1Connect(net::SYNCHRONOUS
, net::OK
);
540 socket_
->SetupSsl1Connect(net::SYNCHRONOUS
, net::ERR_CERT_AUTHORITY_INVALID
);
541 socket_
->SetupTcp2Connect(net::SYNCHRONOUS
, net::OK
);
542 socket_
->SetupSsl2Connect(net::SYNCHRONOUS
, net::OK
);
544 HandleAuthHandshake();
546 EXPECT_EQ(cast_channel::READY_STATE_OPEN
, socket_
->ready_state());
547 EXPECT_EQ(cast_channel::CHANNEL_ERROR_NONE
, socket_
->error_state());
550 // Test that an AuthMessage with a mangled namespace triggers cancelation
551 // of the connection event loop.
552 TEST_F(CastSocketTest
, TestConnectAuthMessageCorrupted
) {
553 CreateCastSocketSecure();
554 socket_
->SetupMockTransport();
556 socket_
->SetupTcp1Connect(net::ASYNC
, net::OK
);
557 socket_
->SetupSsl1Connect(net::ASYNC
, net::ERR_CERT_AUTHORITY_INVALID
);
558 socket_
->SetupTcp2Connect(net::ASYNC
, net::OK
);
559 socket_
->SetupSsl2Connect(net::ASYNC
, net::OK
);
561 CastMessage challenge_proto
= CreateAuthChallenge();
562 EXPECT_CALL(*socket_
->GetMockTransport(),
563 SendMessage(EqualsProto(challenge_proto
), _
))
564 .WillOnce(RunCompletionCallback
<1>(net::OK
));
565 EXPECT_CALL(*socket_
->GetMockTransport(), Start());
566 EXPECT_CALL(handler_
, OnConnectComplete(CHANNEL_ERROR_TRANSPORT_ERROR
));
567 socket_
->Connect(read_delegate_
.Pass(),
568 base::Bind(&CompleteHandler::OnConnectComplete
,
569 base::Unretained(&handler_
)));
571 CastMessage mangled_auth_reply
= CreateAuthReply();
572 mangled_auth_reply
.set_namespace_("BOGUS_NAMESPACE");
574 socket_
->GetMockTransport()->current_delegate()->OnMessage(
578 EXPECT_EQ(cast_channel::READY_STATE_CLOSED
, socket_
->ready_state());
579 EXPECT_EQ(cast_channel::CHANNEL_ERROR_TRANSPORT_ERROR
,
580 socket_
->error_state());
583 // Test connection error - TCP connect fails (async)
584 TEST_F(CastSocketTest
, TestConnectTcpConnectErrorAsync
) {
585 CreateCastSocketSecure();
587 socket_
->SetupTcp1Connect(net::ASYNC
, net::ERR_FAILED
);
589 EXPECT_CALL(handler_
, OnConnectComplete(CHANNEL_ERROR_CONNECT_ERROR
));
590 socket_
->Connect(read_delegate_
.Pass(),
591 base::Bind(&CompleteHandler::OnConnectComplete
,
592 base::Unretained(&handler_
)));
595 EXPECT_EQ(cast_channel::READY_STATE_CLOSED
, socket_
->ready_state());
596 EXPECT_EQ(cast_channel::CHANNEL_ERROR_CONNECT_ERROR
, socket_
->error_state());
599 // Test connection error - TCP connect fails (sync)
600 TEST_F(CastSocketTest
, TestConnectTcpConnectErrorSync
) {
601 CreateCastSocketSecure();
603 socket_
->SetupTcp1Connect(net::SYNCHRONOUS
, net::ERR_FAILED
);
605 EXPECT_CALL(handler_
, OnConnectComplete(CHANNEL_ERROR_CONNECT_ERROR
));
606 socket_
->Connect(read_delegate_
.Pass(),
607 base::Bind(&CompleteHandler::OnConnectComplete
,
608 base::Unretained(&handler_
)));
611 EXPECT_EQ(cast_channel::READY_STATE_CLOSED
, socket_
->ready_state());
612 EXPECT_EQ(cast_channel::CHANNEL_ERROR_CONNECT_ERROR
, socket_
->error_state());
615 // Test connection error - timeout
616 TEST_F(CastSocketTest
, TestConnectTcpTimeoutError
) {
617 CreateCastSocketSecure();
618 socket_
->SetupTcp1ConnectUnresponsive();
619 EXPECT_CALL(handler_
, OnConnectComplete(CHANNEL_ERROR_CONNECT_TIMEOUT
));
620 socket_
->Connect(read_delegate_
.Pass(),
621 base::Bind(&CompleteHandler::OnConnectComplete
,
622 base::Unretained(&handler_
)));
625 EXPECT_EQ(cast_channel::READY_STATE_CONNECTING
, socket_
->ready_state());
626 EXPECT_EQ(cast_channel::CHANNEL_ERROR_NONE
, socket_
->error_state());
627 socket_
->TriggerTimeout();
630 EXPECT_EQ(cast_channel::READY_STATE_CLOSED
, socket_
->ready_state());
631 EXPECT_EQ(cast_channel::CHANNEL_ERROR_CONNECT_TIMEOUT
,
632 socket_
->error_state());
635 // Test connection error - SSL connect fails (async)
636 TEST_F(CastSocketTest
, TestConnectSslConnectErrorAsync
) {
637 CreateCastSocketSecure();
639 socket_
->SetupTcp1Connect(net::SYNCHRONOUS
, net::OK
);
640 socket_
->SetupSsl1Connect(net::SYNCHRONOUS
, net::ERR_FAILED
);
642 EXPECT_CALL(handler_
, OnConnectComplete(CHANNEL_ERROR_AUTHENTICATION_ERROR
));
643 socket_
->Connect(read_delegate_
.Pass(),
644 base::Bind(&CompleteHandler::OnConnectComplete
,
645 base::Unretained(&handler_
)));
648 EXPECT_EQ(cast_channel::READY_STATE_CLOSED
, socket_
->ready_state());
649 EXPECT_EQ(cast_channel::CHANNEL_ERROR_AUTHENTICATION_ERROR
,
650 socket_
->error_state());
653 // Test connection error - SSL connect fails (sync)
654 TEST_F(CastSocketTest
, TestConnectSslConnectErrorSync
) {
655 CreateCastSocketSecure();
657 socket_
->SetupTcp1Connect(net::SYNCHRONOUS
, net::OK
);
658 socket_
->SetupSsl1Connect(net::ASYNC
, net::ERR_FAILED
);
660 EXPECT_CALL(handler_
, OnConnectComplete(CHANNEL_ERROR_AUTHENTICATION_ERROR
));
661 socket_
->Connect(read_delegate_
.Pass(),
662 base::Bind(&CompleteHandler::OnConnectComplete
,
663 base::Unretained(&handler_
)));
666 EXPECT_EQ(cast_channel::READY_STATE_CLOSED
, socket_
->ready_state());
667 EXPECT_EQ(cast_channel::CHANNEL_ERROR_AUTHENTICATION_ERROR
,
668 socket_
->error_state());
671 // Test connection error - cert extraction error (async)
672 TEST_F(CastSocketTest
, TestConnectCertExtractionErrorAsync
) {
674 socket_
->SetupTcp1Connect(net::ASYNC
, net::OK
);
675 socket_
->SetupSsl1Connect(net::ASYNC
, net::ERR_CERT_AUTHORITY_INVALID
);
676 // Set cert extraction to fail
677 socket_
->SetExtractCertResult(false);
679 EXPECT_CALL(handler_
, OnConnectComplete(CHANNEL_ERROR_AUTHENTICATION_ERROR
));
680 socket_
->Connect(read_delegate_
.Pass(),
681 base::Bind(&CompleteHandler::OnConnectComplete
,
682 base::Unretained(&handler_
)));
685 EXPECT_EQ(cast_channel::READY_STATE_CLOSED
, socket_
->ready_state());
686 EXPECT_EQ(cast_channel::CHANNEL_ERROR_AUTHENTICATION_ERROR
,
687 socket_
->error_state());
690 // Test connection error - cert extraction error (sync)
691 TEST_F(CastSocketTest
, TestConnectCertExtractionErrorSync
) {
693 socket_
->SetupTcp1Connect(net::SYNCHRONOUS
, net::OK
);
694 socket_
->SetupSsl1Connect(net::SYNCHRONOUS
, net::ERR_CERT_AUTHORITY_INVALID
);
695 // Set cert extraction to fail
696 socket_
->SetExtractCertResult(false);
698 EXPECT_CALL(handler_
, OnConnectComplete(CHANNEL_ERROR_AUTHENTICATION_ERROR
));
699 socket_
->Connect(read_delegate_
.Pass(),
700 base::Bind(&CompleteHandler::OnConnectComplete
,
701 base::Unretained(&handler_
)));
704 EXPECT_EQ(cast_channel::READY_STATE_CLOSED
, socket_
->ready_state());
705 EXPECT_EQ(cast_channel::CHANNEL_ERROR_AUTHENTICATION_ERROR
,
706 socket_
->error_state());
709 // Test connection error - challenge send fails
710 TEST_F(CastSocketTest
, TestConnectChallengeSendError
) {
711 CreateCastSocketSecure();
712 socket_
->SetupMockTransport();
714 socket_
->SetupTcp1Connect(net::SYNCHRONOUS
, net::OK
);
715 socket_
->SetupSsl1Connect(net::SYNCHRONOUS
, net::OK
);
716 EXPECT_CALL(*socket_
->GetMockTransport(),
717 SendMessage(EqualsProto(CreateAuthChallenge()), _
))
718 .WillOnce(RunCompletionCallback
<1>(net::ERR_CONNECTION_RESET
));
720 EXPECT_CALL(handler_
, OnConnectComplete(CHANNEL_ERROR_SOCKET_ERROR
));
721 socket_
->Connect(read_delegate_
.Pass(),
722 base::Bind(&CompleteHandler::OnConnectComplete
,
723 base::Unretained(&handler_
)));
726 EXPECT_EQ(cast_channel::READY_STATE_CLOSED
, socket_
->ready_state());
727 EXPECT_EQ(cast_channel::CHANNEL_ERROR_SOCKET_ERROR
, socket_
->error_state());
730 // Test connection error - challenge reply receive fails
731 TEST_F(CastSocketTest
, TestConnectChallengeReplyReceiveError
) {
732 CreateCastSocketSecure();
733 socket_
->SetupMockTransport();
735 socket_
->SetupTcp1Connect(net::SYNCHRONOUS
, net::OK
);
736 socket_
->SetupSsl1Connect(net::SYNCHRONOUS
, net::OK
);
737 EXPECT_CALL(*socket_
->GetMockTransport(),
738 SendMessage(EqualsProto(CreateAuthChallenge()), _
))
739 .WillOnce(RunCompletionCallback
<1>(net::OK
));
740 socket_
->AddReadResult(net::SYNCHRONOUS
, net::ERR_FAILED
);
742 EXPECT_CALL(handler_
, OnConnectComplete(CHANNEL_ERROR_SOCKET_ERROR
));
743 EXPECT_CALL(*socket_
->GetMockTransport(), Start());
744 socket_
->Connect(read_delegate_
.Pass(),
745 base::Bind(&CompleteHandler::OnConnectComplete
,
746 base::Unretained(&handler_
)));
747 socket_
->GetMockTransport()->current_delegate()->OnError(
748 CHANNEL_ERROR_SOCKET_ERROR
, LastErrors());
751 EXPECT_EQ(cast_channel::READY_STATE_CLOSED
, socket_
->ready_state());
752 EXPECT_EQ(cast_channel::CHANNEL_ERROR_SOCKET_ERROR
, socket_
->error_state());
755 TEST_F(CastSocketTest
, TestConnectChallengeVerificationFails
) {
756 CreateCastSocketSecure();
757 socket_
->SetupMockTransport();
758 socket_
->SetupTcp1Connect(net::ASYNC
, net::OK
);
759 socket_
->SetupSsl1Connect(net::ASYNC
, net::ERR_CERT_AUTHORITY_INVALID
);
760 socket_
->SetupTcp2Connect(net::ASYNC
, net::OK
);
761 socket_
->SetupSsl2Connect(net::ASYNC
, net::OK
);
762 socket_
->SetVerifyChallengeResult(false);
764 CastMessage challenge_proto
= CreateAuthChallenge();
765 EXPECT_CALL(*socket_
->GetMockTransport(),
766 SendMessage(EqualsProto(challenge_proto
), _
))
767 .WillOnce(RunCompletionCallback
<1>(net::OK
));
768 EXPECT_CALL(handler_
, OnConnectComplete(CHANNEL_ERROR_AUTHENTICATION_ERROR
));
769 EXPECT_CALL(*socket_
->GetMockTransport(), Start());
770 socket_
->Connect(read_delegate_
.Pass(),
771 base::Bind(&CompleteHandler::OnConnectComplete
,
772 base::Unretained(&handler_
)));
774 socket_
->GetMockTransport()->current_delegate()->OnMessage(CreateAuthReply());
777 EXPECT_EQ(cast_channel::READY_STATE_CLOSED
, socket_
->ready_state());
778 EXPECT_EQ(cast_channel::CHANNEL_ERROR_AUTHENTICATION_ERROR
,
779 socket_
->error_state());
782 // Sends message data through an actual non-mocked CastTransport object,
783 // testing the two components in integration.
784 TEST_F(CastSocketTest
, TestConnectEndToEndWithRealTransportAsync
) {
785 CreateCastSocketSecure();
786 socket_
->SetupTcp1Connect(net::ASYNC
, net::OK
);
787 socket_
->SetupSsl1Connect(net::ASYNC
, net::ERR_CERT_AUTHORITY_INVALID
);
788 socket_
->SetupTcp2Connect(net::ASYNC
, net::OK
);
789 socket_
->SetupSsl2Connect(net::ASYNC
, net::OK
);
791 // Set low-level auth challenge expectations.
792 CastMessage challenge
= CreateAuthChallenge();
793 std::string challenge_str
;
794 EXPECT_TRUE(MessageFramer::Serialize(challenge
, &challenge_str
));
795 socket_
->AddWriteResultForData(net::ASYNC
, challenge_str
);
797 // Set low-level auth reply expectations.
798 CastMessage reply
= CreateAuthReply();
799 std::string reply_str
;
800 EXPECT_TRUE(MessageFramer::Serialize(reply
, &reply_str
));
801 socket_
->AddReadResultForData(net::ASYNC
, reply_str
);
802 socket_
->AddReadResult(net::ASYNC
, net::ERR_IO_PENDING
);
804 CastMessage test_message
= CreateTestMessage();
805 std::string test_message_str
;
806 EXPECT_TRUE(MessageFramer::Serialize(test_message
, &test_message_str
));
807 socket_
->AddWriteResultForData(net::ASYNC
, test_message_str
);
809 EXPECT_CALL(handler_
, OnConnectComplete(CHANNEL_ERROR_NONE
));
810 socket_
->Connect(read_delegate_
.Pass(),
811 base::Bind(&CompleteHandler::OnConnectComplete
,
812 base::Unretained(&handler_
)));
814 EXPECT_EQ(cast_channel::READY_STATE_OPEN
, socket_
->ready_state());
815 EXPECT_EQ(cast_channel::CHANNEL_ERROR_NONE
, socket_
->error_state());
817 // Send the test message through a real transport object.
818 EXPECT_CALL(handler_
, OnWriteComplete(net::OK
));
819 socket_
->transport()->SendMessage(
820 test_message
, base::Bind(&CompleteHandler::OnWriteComplete
,
821 base::Unretained(&handler_
)));
824 EXPECT_EQ(cast_channel::READY_STATE_OPEN
, socket_
->ready_state());
825 EXPECT_EQ(cast_channel::CHANNEL_ERROR_NONE
, socket_
->error_state());
828 // Same as TestConnectEndToEndWithRealTransportAsync, except synchronous.
829 TEST_F(CastSocketTest
, TestConnectEndToEndWithRealTransportSync
) {
830 CreateCastSocketSecure();
831 socket_
->SetupTcp1Connect(net::SYNCHRONOUS
, net::OK
);
832 socket_
->SetupSsl1Connect(net::SYNCHRONOUS
, net::ERR_CERT_AUTHORITY_INVALID
);
833 socket_
->SetupTcp2Connect(net::SYNCHRONOUS
, net::OK
);
834 socket_
->SetupSsl2Connect(net::SYNCHRONOUS
, net::OK
);
836 // Set low-level auth challenge expectations.
837 CastMessage challenge
= CreateAuthChallenge();
838 std::string challenge_str
;
839 EXPECT_TRUE(MessageFramer::Serialize(challenge
, &challenge_str
));
840 socket_
->AddWriteResultForData(net::SYNCHRONOUS
, challenge_str
);
842 // Set low-level auth reply expectations.
843 CastMessage reply
= CreateAuthReply();
844 std::string reply_str
;
845 EXPECT_TRUE(MessageFramer::Serialize(reply
, &reply_str
));
846 socket_
->AddReadResultForData(net::SYNCHRONOUS
, reply_str
);
847 socket_
->AddReadResult(net::ASYNC
, net::ERR_IO_PENDING
);
849 CastMessage test_message
= CreateTestMessage();
850 std::string test_message_str
;
851 EXPECT_TRUE(MessageFramer::Serialize(test_message
, &test_message_str
));
852 socket_
->AddWriteResultForData(net::SYNCHRONOUS
, test_message_str
);
854 EXPECT_CALL(handler_
, OnConnectComplete(CHANNEL_ERROR_NONE
));
855 socket_
->Connect(read_delegate_
.Pass(),
856 base::Bind(&CompleteHandler::OnConnectComplete
,
857 base::Unretained(&handler_
)));
859 EXPECT_EQ(cast_channel::READY_STATE_OPEN
, socket_
->ready_state());
860 EXPECT_EQ(cast_channel::CHANNEL_ERROR_NONE
, socket_
->error_state());
862 // Send the test message through a real transport object.
863 EXPECT_CALL(handler_
, OnWriteComplete(net::OK
));
864 socket_
->transport()->SendMessage(
865 test_message
, base::Bind(&CompleteHandler::OnWriteComplete
,
866 base::Unretained(&handler_
)));
869 EXPECT_EQ(cast_channel::READY_STATE_OPEN
, socket_
->ready_state());
870 EXPECT_EQ(cast_channel::CHANNEL_ERROR_NONE
, socket_
->error_state());
873 // Tests channel policy verification for device with no capabilities.
874 TEST_F(CastSocketTest
, TestChannelPolicyVerificationCapabilitiesNone
) {
876 TestCastSocket::Create(logger_
, cast_channel::CastDeviceCapability::NONE
);
877 EXPECT_TRUE(socket_
->TestVerifyChannelPolicyNone());
878 EXPECT_TRUE(socket_
->TestVerifyChannelPolicyAudioOnly());
881 // Tests channel policy verification for device with video out capability.
882 TEST_F(CastSocketTest
, TestChannelPolicyVerificationCapabilitiesVideoOut
) {
883 socket_
= TestCastSocket::Create(
884 logger_
, cast_channel::CastDeviceCapability::VIDEO_OUT
);
885 EXPECT_TRUE(socket_
->TestVerifyChannelPolicyNone());
886 EXPECT_FALSE(socket_
->TestVerifyChannelPolicyAudioOnly());
888 } // namespace cast_channel
889 } // namespace core_api
890 } // namespace extensions