Skip to content

Commit

Permalink
dcsctp: Add mocks
Browse files Browse the repository at this point in the history
This is for convenience to the users of dcSCTP, which may want to have
unit tests where the socket is mocked. And since it's best practice not
to mock other teams' or project's classes, a mock will be provided by
the upstream project - this one.

Bug: webrtc:12614
Change-Id: I65d5d21097e7feda9162567560d3838759c962fc
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/224161
Reviewed-by: Florent Castelli <[email protected]>
Commit-Queue: Victor Boivie <[email protected]>
Cr-Commit-Position: refs/heads/master@{#34385}
  • Loading branch information
Victor Boivie authored and WebRTC LUCI CQ committed Jun 29, 2021
1 parent da9dfae commit c362eb2
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
11 changes: 11 additions & 0 deletions net/dcsctp/public/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,21 @@ rtc_source_set("factory") {
absl_deps = [ "//third_party/abseil-cpp/absl/strings" ]
}

rtc_source_set("mocks") {
testonly = true
sources = [ "mock_dcsctp_socket.h" ]
deps = [
":socket",
"../../../test:test_support",
]
}

if (rtc_include_tests) {
rtc_library("dcsctp_public_unittests") {
testonly = true

deps = [
":mocks",
":strong_alias",
":types",
"../../../rtc_base:checks",
Expand All @@ -69,6 +79,7 @@ if (rtc_include_tests) {
"../../../test:test_support",
]
sources = [
"mock_dcsctp_socket_test.cc",
"strong_alias_test.cc",
"types_test.cc",
]
Expand Down
64 changes: 64 additions & 0 deletions net/dcsctp/public/mock_dcsctp_socket.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2021 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.
*/
#ifndef NET_DCSCTP_PUBLIC_MOCK_DCSCTP_SOCKET_H_
#define NET_DCSCTP_PUBLIC_MOCK_DCSCTP_SOCKET_H_

#include "net/dcsctp/public/dcsctp_socket.h"
#include "test/gmock.h"

namespace dcsctp {

class MockDcSctpSocket : public DcSctpSocketInterface {
public:
MOCK_METHOD(void,
ReceivePacket,
(rtc::ArrayView<const uint8_t> data),
(override));

MOCK_METHOD(void, HandleTimeout, (TimeoutID timeout_id), (override));

MOCK_METHOD(void, Connect, (), (override));

MOCK_METHOD(void, Shutdown, (), (override));

MOCK_METHOD(void, Close, (), (override));

MOCK_METHOD(SocketState, state, (), (const, override));

MOCK_METHOD(const DcSctpOptions&, options, (), (const, override));

MOCK_METHOD(void, SetMaxMessageSize, (size_t max_message_size), (override));

MOCK_METHOD(SendStatus,
Send,
(DcSctpMessage message, const SendOptions& send_options),
(override));

MOCK_METHOD(ResetStreamsStatus,
ResetStreams,
(rtc::ArrayView<const StreamID> outgoing_streams),
(override));

MOCK_METHOD(size_t, buffered_amount, (StreamID stream_id), (const, override));

MOCK_METHOD(size_t,
buffered_amount_low_threshold,
(StreamID stream_id),
(const, override));

MOCK_METHOD(void,
SetBufferedAmountLowThreshold,
(StreamID stream_id, size_t bytes),
(override));
};

} // namespace dcsctp

#endif // NET_DCSCTP_PUBLIC_MOCK_DCSCTP_SOCKET_H_
27 changes: 27 additions & 0 deletions net/dcsctp/public/mock_dcsctp_socket_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2021 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 "net/dcsctp/public/mock_dcsctp_socket.h"

#include "rtc_base/gunit.h"
#include "test/gmock.h"

namespace dcsctp {

// This test exists to ensure that all methods are mocked correctly, and to
// generate compiler errors if they are not.
TEST(MockDcSctpSocketTest, CanInstantiateAndConnect) {
testing::StrictMock<MockDcSctpSocket> socket;

EXPECT_CALL(socket, Connect);

socket.Connect();
}

} // namespace dcsctp

0 comments on commit c362eb2

Please sign in to comment.