1 // Copyright (c) 2012 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 "base/memory/ref_counted.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/message_loop/message_loop.h"
8 #include "jingle/glue/channel_socket_adapter.h"
9 #include "net/base/io_buffer.h"
10 #include "net/base/net_errors.h"
11 #include "net/socket/socket.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/webrtc/p2p/base/transportchannel.h"
19 using testing::Return
;
21 namespace jingle_glue
{
24 const int kBufferSize
= 4096;
25 const char kTestData
[] = "data";
26 const int kTestDataSize
= 4;
27 const int kTestError
= -32123;
30 class MockTransportChannel
: public cricket::TransportChannel
{
32 MockTransportChannel() : cricket::TransportChannel(std::string(), 0) {
37 MOCK_METHOD4(SendPacket
, int(const char* data
,
39 const rtc::PacketOptions
& options
,
41 MOCK_METHOD2(SetOption
, int(rtc::Socket::Option opt
, int value
));
42 MOCK_METHOD0(GetError
, int());
43 MOCK_CONST_METHOD0(GetIceRole
, cricket::IceRole());
44 MOCK_METHOD1(GetStats
, bool(cricket::ConnectionInfos
* infos
));
45 MOCK_CONST_METHOD0(IsDtlsActive
, bool());
46 MOCK_CONST_METHOD1(GetSslRole
, bool(rtc::SSLRole
* role
));
47 MOCK_METHOD1(SetSrtpCiphers
, bool(const std::vector
<std::string
>& ciphers
));
48 MOCK_METHOD1(GetSrtpCipher
, bool(std::string
* cipher
));
49 MOCK_METHOD1(GetSslCipher
, bool(std::string
* cipher
));
50 MOCK_CONST_METHOD1(GetLocalIdentity
, bool(rtc::SSLIdentity
** identity
));
51 MOCK_CONST_METHOD1(GetRemoteCertificate
,
52 bool(rtc::SSLCertificate
** cert
));
53 MOCK_METHOD6(ExportKeyingMaterial
, bool(const std::string
& label
,
61 class TransportChannelSocketAdapterTest
: public testing::Test
{
63 TransportChannelSocketAdapterTest()
64 : callback_(base::Bind(&TransportChannelSocketAdapterTest::Callback
,
65 base::Unretained(this))),
70 void SetUp() override
{
71 target_
.reset(new TransportChannelSocketAdapter(&channel_
));
74 void Callback(int result
) {
75 callback_result_
= result
;
78 MockTransportChannel channel_
;
79 scoped_ptr
<TransportChannelSocketAdapter
> target_
;
80 net::CompletionCallback callback_
;
82 base::MessageLoopForIO message_loop_
;
85 // Verify that Read() returns net::ERR_IO_PENDING.
86 TEST_F(TransportChannelSocketAdapterTest
, Read
) {
87 scoped_refptr
<IOBuffer
> buffer(new IOBuffer(kBufferSize
));
89 int result
= target_
->Read(buffer
.get(), kBufferSize
, callback_
);
90 ASSERT_EQ(net::ERR_IO_PENDING
, result
);
92 channel_
.SignalReadPacket(&channel_
, kTestData
, kTestDataSize
,
93 rtc::CreatePacketTime(0), 0);
94 EXPECT_EQ(kTestDataSize
, callback_result_
);
97 // Verify that Read() after Close() returns error.
98 TEST_F(TransportChannelSocketAdapterTest
, ReadClose
) {
99 scoped_refptr
<IOBuffer
> buffer(new IOBuffer(kBufferSize
));
101 int result
= target_
->Read(buffer
.get(), kBufferSize
, callback_
);
102 ASSERT_EQ(net::ERR_IO_PENDING
, result
);
104 target_
->Close(kTestError
);
105 EXPECT_EQ(kTestError
, callback_result_
);
107 // All Read() calls after Close() should return the error.
108 EXPECT_EQ(kTestError
, target_
->Read(buffer
.get(), kBufferSize
, callback_
));
111 // Verify that Write sends the packet and returns correct result.
112 TEST_F(TransportChannelSocketAdapterTest
, Write
) {
113 scoped_refptr
<IOBuffer
> buffer(new IOBuffer(kTestDataSize
));
115 EXPECT_CALL(channel_
, SendPacket(buffer
->data(), kTestDataSize
, _
, 0))
116 .WillOnce(Return(kTestDataSize
));
118 int result
= target_
->Write(buffer
.get(), kTestDataSize
, callback_
);
119 EXPECT_EQ(kTestDataSize
, result
);
122 // Verify that the message is still sent if Write() is called while
123 // socket is not open yet. The result is the packet is lost.
124 TEST_F(TransportChannelSocketAdapterTest
, WritePending
) {
125 scoped_refptr
<IOBuffer
> buffer(new IOBuffer(kTestDataSize
));
127 EXPECT_CALL(channel_
, SendPacket(buffer
->data(), kTestDataSize
, _
, 0))
129 .WillOnce(Return(SOCKET_ERROR
));
131 EXPECT_CALL(channel_
, GetError())
132 .WillOnce(Return(EWOULDBLOCK
));
134 int result
= target_
->Write(buffer
.get(), kTestDataSize
, callback_
);
135 ASSERT_EQ(net::OK
, result
);
138 } // namespace jingle_glue