File: channel_socket_adapter_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (120 lines) | stat: -rw-r--r-- 4,050 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/protocol/channel_socket_adapter.h"

#include <stddef.h>
#include <stdint.h>

#include <memory>

#include "base/functional/bind.h"
#include "base/memory/ref_counted.h"
#include "base/test/task_environment.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/socket/socket.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/webrtc/api/array_view.h"
#include "third_party/webrtc/p2p/base/mock_ice_transport.h"
#include "third_party/webrtc/rtc_base/network/received_packet.h"

using net::IOBufferWithSize;

using testing::_;
using testing::Return;

namespace remoting::protocol {

namespace {
const int kBufferSize = 4096;
const uint8_t kTestData[] = "data";
const int kTestDataSize = 4;
const int kTestError = -32123;
}  // namespace

class IceTransportForTest : public webrtc::MockIceTransport {
 public:
  // Exposed for testing.
  using webrtc::PacketTransportInternal::NotifyPacketReceived;
};

class TransportChannelSocketAdapterTest : public testing::Test {
 public:
  TransportChannelSocketAdapterTest()
      : callback_(
            base::BindRepeating(&TransportChannelSocketAdapterTest::Callback,
                                base::Unretained(this))),
        callback_result_(0) {}

 protected:
  void SetUp() override {
    target_ = std::make_unique<TransportChannelSocketAdapter>(&channel_);
  }

  void Callback(int result) { callback_result_ = result; }

  IceTransportForTest channel_;
  std::unique_ptr<TransportChannelSocketAdapter> target_;
  net::CompletionRepeatingCallback callback_;
  int callback_result_;
  base::test::SingleThreadTaskEnvironment task_environment_{
      base::test::SingleThreadTaskEnvironment::MainThreadType::IO};
};

// Verify that Read() returns net::ERR_IO_PENDING.
TEST_F(TransportChannelSocketAdapterTest, Read) {
  auto buffer = base::MakeRefCounted<IOBufferWithSize>(kBufferSize);
  int result = target_->Recv(buffer.get(), kBufferSize, callback_);
  ASSERT_EQ(net::ERR_IO_PENDING, result);

  channel_.NotifyPacketReceived(
      webrtc::ReceivedIpPacket(webrtc::MakeArrayView(kTestData, kTestDataSize),
                               webrtc::SocketAddress()));
  EXPECT_EQ(kTestDataSize, callback_result_);
}

// Verify that Read() after Close() returns error.
TEST_F(TransportChannelSocketAdapterTest, ReadClose) {
  auto buffer = base::MakeRefCounted<IOBufferWithSize>(kBufferSize);
  int result = target_->Recv(buffer.get(), kBufferSize, callback_);
  ASSERT_EQ(net::ERR_IO_PENDING, result);

  target_->Close(kTestError);
  EXPECT_EQ(kTestError, callback_result_);

  // All Recv() calls after Close() should return the error.
  EXPECT_EQ(kTestError, target_->Recv(buffer.get(), kBufferSize, callback_));
}

// Verify that Send sends the packet and returns correct result.
TEST_F(TransportChannelSocketAdapterTest, Send) {
  auto buffer = base::MakeRefCounted<IOBufferWithSize>(kTestDataSize);

  EXPECT_CALL(channel_, writable()).WillOnce(Return(true));
  EXPECT_CALL(channel_, SendPacket(buffer->data(), kTestDataSize, _, 0))
      .WillOnce(Return(kTestDataSize));

  int result = target_->Send(buffer.get(), kTestDataSize, callback_);
  EXPECT_EQ(kTestDataSize, result);
}

// Verify that the message is still sent if Send() is called while
// socket is not open yet. The result is the packet is lost.
TEST_F(TransportChannelSocketAdapterTest, SendPending) {
  auto buffer = base::MakeRefCounted<IOBufferWithSize>(kTestDataSize);

  EXPECT_CALL(channel_, writable()).WillOnce(Return(true));
  EXPECT_CALL(channel_, SendPacket(buffer->data(), kTestDataSize, _, 0))
      .Times(1)
      .WillOnce(Return(SOCKET_ERROR));

  EXPECT_CALL(channel_, GetError()).WillOnce(Return(EWOULDBLOCK));

  int result = target_->Send(buffer.get(), kTestDataSize, callback_);
  ASSERT_EQ(net::OK, result);
}

}  // namespace remoting::protocol