summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/rtc_base/async_packet_socket_unittest.cc
blob: 1d66821958d2bf98dff743142bd2d88bc9552a4d (plain)
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
/*
 *  Copyright 2023 The WebRTC Project Authors. All rights reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "rtc_base/async_packet_socket.h"

#include "rtc_base/third_party/sigslot/sigslot.h"
#include "test/gmock.h"
#include "test/gtest.h"

namespace rtc {
namespace {

using ::testing::MockFunction;

class MockAsyncPacketSocket : public rtc::AsyncPacketSocket {
 public:
  ~MockAsyncPacketSocket() = default;

  MOCK_METHOD(SocketAddress, GetLocalAddress, (), (const, override));
  MOCK_METHOD(SocketAddress, GetRemoteAddress, (), (const, override));
  MOCK_METHOD(int,
              Send,
              (const void* pv, size_t cb, const rtc::PacketOptions& options),
              (override));

  MOCK_METHOD(int,
              SendTo,
              (const void* pv,
               size_t cb,
               const SocketAddress& addr,
               const rtc::PacketOptions& options),
              (override));
  MOCK_METHOD(int, Close, (), (override));
  MOCK_METHOD(State, GetState, (), (const, override));
  MOCK_METHOD(int,
              GetOption,
              (rtc::Socket::Option opt, int* value),
              (override));
  MOCK_METHOD(int, SetOption, (rtc::Socket::Option opt, int value), (override));
  MOCK_METHOD(int, GetError, (), (const, override));
  MOCK_METHOD(void, SetError, (int error), (override));

  void NotifyPacketReceived() {
    char data[1] = {'a'};
    AsyncPacketSocket::NotifyPacketReceived(this, data, 1, SocketAddress(), -1);
  }
};

TEST(AsyncPacketSocket, RegisteredCallbackReceivePacketsFromNotify) {
  MockAsyncPacketSocket mock_socket;
  MockFunction<void(AsyncPacketSocket*, const rtc::ReceivedPacket&)>
      received_packet;

  EXPECT_CALL(received_packet, Call);
  mock_socket.RegisterReceivedPacketCallback(received_packet.AsStdFunction());
  mock_socket.NotifyPacketReceived();
}

}  // namespace
}  // namespace rtc