Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / remoting / protocol / channel_socket_adapter_unittest.cc
blobd488c4b517a09928ec74e9936a97c20f2dd2e54d
1 // Copyright 2015 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 "remoting/protocol/channel_socket_adapter.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h"
12 #include "net/socket/socket.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/webrtc/p2p/base/transportchannel.h"
17 using net::IOBuffer;
19 using testing::_;
20 using testing::Return;
22 namespace remoting {
23 namespace protocol {
25 namespace {
26 const int kBufferSize = 4096;
27 const char kTestData[] = "data";
28 const int kTestDataSize = 4;
29 const int kTestError = -32123;
30 } // namespace
32 class MockTransportChannel : public cricket::TransportChannel {
33 public:
34 MockTransportChannel() : cricket::TransportChannel(std::string(), 0) {
35 set_writable(true);
36 set_readable(true);
39 MOCK_METHOD4(SendPacket, int(const char* data,
40 size_t len,
41 const rtc::PacketOptions& options,
42 int flags));
43 MOCK_METHOD2(SetOption, int(rtc::Socket::Option opt, int value));
44 MOCK_METHOD0(GetError, int());
45 MOCK_CONST_METHOD0(GetIceRole, cricket::IceRole());
46 MOCK_METHOD1(GetStats, bool(cricket::ConnectionInfos* infos));
47 MOCK_CONST_METHOD0(IsDtlsActive, bool());
48 MOCK_CONST_METHOD1(GetSslRole, bool(rtc::SSLRole* role));
49 MOCK_METHOD1(SetSrtpCiphers, bool(const std::vector<std::string>& ciphers));
50 MOCK_METHOD1(GetSrtpCipher, bool(std::string* cipher));
51 MOCK_METHOD1(GetSslCipher, bool(std::string* cipher));
52 MOCK_CONST_METHOD0(GetLocalCertificate,
53 rtc::scoped_refptr<rtc::RTCCertificate>());
54 MOCK_CONST_METHOD1(GetRemoteSSLCertificate,
55 bool(rtc::SSLCertificate** cert));
56 MOCK_METHOD6(ExportKeyingMaterial, bool(const std::string& label,
57 const uint8* context,
58 size_t context_len,
59 bool use_context,
60 uint8* result,
61 size_t result_len));
64 class TransportChannelSocketAdapterTest : public testing::Test {
65 public:
66 TransportChannelSocketAdapterTest()
67 : callback_(base::Bind(&TransportChannelSocketAdapterTest::Callback,
68 base::Unretained(this))),
69 callback_result_(0) {
72 protected:
73 void SetUp() override {
74 target_.reset(new TransportChannelSocketAdapter(&channel_));
77 void Callback(int result) {
78 callback_result_ = result;
81 MockTransportChannel channel_;
82 scoped_ptr<TransportChannelSocketAdapter> target_;
83 net::CompletionCallback callback_;
84 int callback_result_;
85 base::MessageLoopForIO message_loop_;
88 // Verify that Read() returns net::ERR_IO_PENDING.
89 TEST_F(TransportChannelSocketAdapterTest, Read) {
90 scoped_refptr<IOBuffer> buffer(new IOBuffer(kBufferSize));
92 int result = target_->Recv(buffer.get(), kBufferSize, callback_);
93 ASSERT_EQ(net::ERR_IO_PENDING, result);
95 channel_.SignalReadPacket(&channel_, kTestData, kTestDataSize,
96 rtc::CreatePacketTime(0), 0);
97 EXPECT_EQ(kTestDataSize, callback_result_);
100 // Verify that Read() after Close() returns error.
101 TEST_F(TransportChannelSocketAdapterTest, ReadClose) {
102 scoped_refptr<IOBuffer> buffer(new IOBuffer(kBufferSize));
104 int result = target_->Recv(buffer.get(), kBufferSize, callback_);
105 ASSERT_EQ(net::ERR_IO_PENDING, result);
107 target_->Close(kTestError);
108 EXPECT_EQ(kTestError, callback_result_);
110 // All Recv() calls after Close() should return the error.
111 EXPECT_EQ(kTestError, target_->Recv(buffer.get(), kBufferSize, callback_));
114 // Verify that Send sends the packet and returns correct result.
115 TEST_F(TransportChannelSocketAdapterTest, Send) {
116 scoped_refptr<IOBuffer> buffer(new IOBuffer(kTestDataSize));
118 EXPECT_CALL(channel_, SendPacket(buffer->data(), kTestDataSize, _, 0))
119 .WillOnce(Return(kTestDataSize));
121 int result = target_->Send(buffer.get(), kTestDataSize, callback_);
122 EXPECT_EQ(kTestDataSize, result);
125 // Verify that the message is still sent if Send() is called while
126 // socket is not open yet. The result is the packet is lost.
127 TEST_F(TransportChannelSocketAdapterTest, SendPending) {
128 scoped_refptr<IOBuffer> buffer(new IOBuffer(kTestDataSize));
130 EXPECT_CALL(channel_, SendPacket(buffer->data(), kTestDataSize, _, 0))
131 .Times(1)
132 .WillOnce(Return(SOCKET_ERROR));
134 EXPECT_CALL(channel_, GetError())
135 .WillOnce(Return(EWOULDBLOCK));
137 int result = target_->Send(buffer.get(), kTestDataSize, callback_);
138 ASSERT_EQ(net::OK, result);
141 } // namespace protocol
142 } // namespace remoting