Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / remoting / protocol / channel_socket_adapter_unittest.cc
blob85e73a19a9edd010fe970e7725150dac81698666
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_METHOD1(GetLocalIdentity, bool(rtc::SSLIdentity** identity));
53 MOCK_CONST_METHOD1(GetRemoteCertificate,
54 bool(rtc::SSLCertificate** cert));
55 MOCK_METHOD6(ExportKeyingMaterial, bool(const std::string& label,
56 const uint8* context,
57 size_t context_len,
58 bool use_context,
59 uint8* result,
60 size_t result_len));
63 class TransportChannelSocketAdapterTest : public testing::Test {
64 public:
65 TransportChannelSocketAdapterTest()
66 : callback_(base::Bind(&TransportChannelSocketAdapterTest::Callback,
67 base::Unretained(this))),
68 callback_result_(0) {
71 protected:
72 void SetUp() override {
73 target_.reset(new TransportChannelSocketAdapter(&channel_));
76 void Callback(int result) {
77 callback_result_ = result;
80 MockTransportChannel channel_;
81 scoped_ptr<TransportChannelSocketAdapter> target_;
82 net::CompletionCallback callback_;
83 int callback_result_;
84 base::MessageLoopForIO message_loop_;
87 // Verify that Read() returns net::ERR_IO_PENDING.
88 TEST_F(TransportChannelSocketAdapterTest, Read) {
89 scoped_refptr<IOBuffer> buffer(new IOBuffer(kBufferSize));
91 int result = target_->Recv(buffer.get(), kBufferSize, callback_);
92 ASSERT_EQ(net::ERR_IO_PENDING, result);
94 channel_.SignalReadPacket(&channel_, kTestData, kTestDataSize,
95 rtc::CreatePacketTime(0), 0);
96 EXPECT_EQ(kTestDataSize, callback_result_);
99 // Verify that Read() after Close() returns error.
100 TEST_F(TransportChannelSocketAdapterTest, ReadClose) {
101 scoped_refptr<IOBuffer> buffer(new IOBuffer(kBufferSize));
103 int result = target_->Recv(buffer.get(), kBufferSize, callback_);
104 ASSERT_EQ(net::ERR_IO_PENDING, result);
106 target_->Close(kTestError);
107 EXPECT_EQ(kTestError, callback_result_);
109 // All Recv() calls after Close() should return the error.
110 EXPECT_EQ(kTestError, target_->Recv(buffer.get(), kBufferSize, callback_));
113 // Verify that Send sends the packet and returns correct result.
114 TEST_F(TransportChannelSocketAdapterTest, Send) {
115 scoped_refptr<IOBuffer> buffer(new IOBuffer(kTestDataSize));
117 EXPECT_CALL(channel_, SendPacket(buffer->data(), kTestDataSize, _, 0))
118 .WillOnce(Return(kTestDataSize));
120 int result = target_->Send(buffer.get(), kTestDataSize, callback_);
121 EXPECT_EQ(kTestDataSize, result);
124 // Verify that the message is still sent if Send() is called while
125 // socket is not open yet. The result is the packet is lost.
126 TEST_F(TransportChannelSocketAdapterTest, SendPending) {
127 scoped_refptr<IOBuffer> buffer(new IOBuffer(kTestDataSize));
129 EXPECT_CALL(channel_, SendPacket(buffer->data(), kTestDataSize, _, 0))
130 .Times(1)
131 .WillOnce(Return(SOCKET_ERROR));
133 EXPECT_CALL(channel_, GetError())
134 .WillOnce(Return(EWOULDBLOCK));
136 int result = target_->Send(buffer.get(), kTestDataSize, callback_);
137 ASSERT_EQ(net::OK, result);
140 } // namespace protocol
141 } // namespace remoting