xref: /aosp_15_r20/external/webrtc/net/dcsctp/socket/mock_context.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 #ifndef NET_DCSCTP_SOCKET_MOCK_CONTEXT_H_
11 #define NET_DCSCTP_SOCKET_MOCK_CONTEXT_H_
12 
13 #include <cstdint>
14 
15 #include "absl/strings/string_view.h"
16 #include "absl/types/optional.h"
17 #include "net/dcsctp/packet/sctp_packet.h"
18 #include "net/dcsctp/public/dcsctp_options.h"
19 #include "net/dcsctp/public/dcsctp_socket.h"
20 #include "net/dcsctp/socket/context.h"
21 #include "net/dcsctp/socket/mock_dcsctp_socket_callbacks.h"
22 #include "test/gmock.h"
23 
24 namespace dcsctp {
25 
26 class MockContext : public Context {
27  public:
MyInitialTsn()28   static constexpr TSN MyInitialTsn() { return TSN(990); }
PeerInitialTsn()29   static constexpr TSN PeerInitialTsn() { return TSN(10); }
PeerVerificationTag()30   static constexpr VerificationTag PeerVerificationTag() {
31     return VerificationTag(0x01234567);
32   }
33 
MockContext(MockDcSctpSocketCallbacks * callbacks)34   explicit MockContext(MockDcSctpSocketCallbacks* callbacks)
35       : callbacks_(*callbacks) {
36     ON_CALL(*this, is_connection_established)
37         .WillByDefault(testing::Return(true));
38     ON_CALL(*this, my_initial_tsn)
39         .WillByDefault(testing::Return(MyInitialTsn()));
40     ON_CALL(*this, peer_initial_tsn)
41         .WillByDefault(testing::Return(PeerInitialTsn()));
42     ON_CALL(*this, callbacks).WillByDefault(testing::ReturnRef(callbacks_));
43     ON_CALL(*this, current_rto).WillByDefault(testing::Return(DurationMs(123)));
44     ON_CALL(*this, Send).WillByDefault([this](SctpPacket::Builder& builder) {
45       callbacks_.SendPacketWithStatus(builder.Build());
46     });
47   }
48 
49   MOCK_METHOD(bool, is_connection_established, (), (const, override));
50   MOCK_METHOD(TSN, my_initial_tsn, (), (const, override));
51   MOCK_METHOD(TSN, peer_initial_tsn, (), (const, override));
52   MOCK_METHOD(DcSctpSocketCallbacks&, callbacks, (), (const, override));
53 
54   MOCK_METHOD(void, ObserveRTT, (DurationMs rtt_ms), (override));
55   MOCK_METHOD(DurationMs, current_rto, (), (const, override));
56   MOCK_METHOD(bool,
57               IncrementTxErrorCounter,
58               (absl::string_view reason),
59               (override));
60   MOCK_METHOD(void, ClearTxErrorCounter, (), (override));
61   MOCK_METHOD(bool, HasTooManyTxErrors, (), (const, override));
PacketBuilder()62   SctpPacket::Builder PacketBuilder() const override {
63     return SctpPacket::Builder(PeerVerificationTag(), options_);
64   }
65   MOCK_METHOD(void, Send, (SctpPacket::Builder & builder), (override));
66 
67   DcSctpOptions options_;
68   MockDcSctpSocketCallbacks& callbacks_;
69 };
70 }  // namespace dcsctp
71 
72 #endif  // NET_DCSCTP_SOCKET_MOCK_CONTEXT_H_
73