LCOV - code coverage report
Current view: top level - src/test/util - net.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 4 86 4.7 %
Date: 2023-09-26 12:08:55 Functions: 4 9 44.4 %

          Line data    Source code
       1             : // Copyright (c) 2020-2022 The Bitcoin Core developers
       2             : // Distributed under the MIT software license, see the accompanying
       3             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4             : 
       5             : #include <test/util/net.h>
       6             : 
       7             : #include <chainparams.h>
       8             : #include <node/eviction.h>
       9             : #include <net.h>
      10             : #include <net_processing.h>
      11             : #include <netmessagemaker.h>
      12             : #include <span.h>
      13             : 
      14             : #include <vector>
      15             : 
      16           0 : void ConnmanTestMsg::Handshake(CNode& node,
      17           2 :                                bool successfully_connected,
      18           2 :                                ServiceFlags remote_services,
      19             :                                ServiceFlags local_services,
      20             :                                int32_t version,
      21             :                                bool relay_txs)
      22             : {
      23           0 :     auto& peerman{static_cast<PeerManager&>(*m_msgproc)};
      24           0 :     auto& connman{*this};
      25           0 :     const CNetMsgMaker mm{0};
      26             : 
      27           0 :     peerman.InitializeNode(node, local_services);
      28           0 :     FlushSendBuffer(node); // Drop the version message added by InitializeNode.
      29             : 
      30           0 :     CSerializedNetMsg msg_version{
      31           0 :         mm.Make(NetMsgType::VERSION,
      32             :                 version,                                        //
      33           0 :                 Using<CustomUintFormatter<8>>(remote_services), //
      34           0 :                 int64_t{},                                      // dummy time
      35           0 :                 int64_t{},                                      // ignored service bits
      36           0 :                 CNetAddr::V1(CService{}),                       // dummy
      37           0 :                 int64_t{},                                      // ignored service bits
      38           0 :                 CNetAddr::V1(CService{}),                       // ignored
      39           0 :                 uint64_t{1},                                    // dummy nonce
      40           0 :                 std::string{},                                  // dummy subver
      41           0 :                 int32_t{},                                      // dummy starting_height
      42             :                 relay_txs),
      43             :     };
      44             : 
      45           0 :     (void)connman.ReceiveMsgFrom(node, std::move(msg_version));
      46           0 :     node.fPauseSend = false;
      47           0 :     connman.ProcessMessagesOnce(node);
      48           0 :     peerman.SendMessages(&node);
      49           0 :     FlushSendBuffer(node); // Drop the verack message added by SendMessages.
      50           0 :     if (node.fDisconnect) return;
      51           0 :     assert(node.nVersion == version);
      52           0 :     assert(node.GetCommonVersion() == std::min(version, PROTOCOL_VERSION));
      53           0 :     CNodeStateStats statestats;
      54           0 :     assert(peerman.GetNodeStateStats(node.GetId(), statestats));
      55           0 :     assert(statestats.m_relay_txs == (relay_txs && !node.IsBlockOnlyConn()));
      56           0 :     assert(statestats.their_services == remote_services);
      57           0 :     if (successfully_connected) {
      58           0 :         CSerializedNetMsg msg_verack{mm.Make(NetMsgType::VERACK)};
      59           0 :         (void)connman.ReceiveMsgFrom(node, std::move(msg_verack));
      60           0 :         node.fPauseSend = false;
      61           0 :         connman.ProcessMessagesOnce(node);
      62           0 :         peerman.SendMessages(&node);
      63           0 :         assert(node.fSuccessfullyConnected == true);
      64           0 :     }
      65           0 : }
      66             : 
      67           0 : void ConnmanTestMsg::NodeReceiveMsgBytes(CNode& node, Span<const uint8_t> msg_bytes, bool& complete) const
      68             : {
      69           0 :     assert(node.ReceiveMsgBytes(msg_bytes, complete));
      70           0 :     if (complete) {
      71           0 :         node.MarkReceivedMsgsForProcessing();
      72           0 :     }
      73           0 : }
      74           2 : 
      75           0 : void ConnmanTestMsg::FlushSendBuffer(CNode& node) const
      76             : {
      77           0 :     LOCK(node.cs_vSend);
      78           0 :     node.vSendMsg.clear();
      79           0 :     node.m_send_memusage = 0;
      80           0 :     while (true) {
      81           0 :         const auto& [to_send, _more, _msg_type] = node.m_transport->GetBytesToSend(false);
      82           0 :         if (to_send.empty()) break;
      83           2 :         node.m_transport->MarkBytesSent(to_send.size());
      84             :     }
      85           0 : }
      86             : 
      87           0 : bool ConnmanTestMsg::ReceiveMsgFrom(CNode& node, CSerializedNetMsg&& ser_msg) const
      88             : {
      89           0 :     bool queued = node.m_transport->SetMessageToSend(ser_msg);
      90           0 :     assert(queued);
      91           0 :     bool complete{false};
      92           0 :     while (true) {
      93           0 :         const auto& [to_send, _more, _msg_type] = node.m_transport->GetBytesToSend(false);
      94           0 :         if (to_send.empty()) break;
      95           0 :         NodeReceiveMsgBytes(node, to_send, complete);
      96           0 :         node.m_transport->MarkBytesSent(to_send.size());
      97             :     }
      98           0 :     return complete;
      99             : }
     100             : 
     101           0 : std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext& random_context)
     102             : {
     103           0 :     std::vector<NodeEvictionCandidate> candidates;
     104           0 :     candidates.reserve(n_candidates);
     105           0 :     for (int id = 0; id < n_candidates; ++id) {
     106           0 :         candidates.push_back({
     107           0 :             /*id=*/id,
     108           0 :             /*m_connected=*/std::chrono::seconds{random_context.randrange(100)},
     109           0 :             /*m_min_ping_time=*/std::chrono::microseconds{random_context.randrange(100)},
     110           0 :             /*m_last_block_time=*/std::chrono::seconds{random_context.randrange(100)},
     111           0 :             /*m_last_tx_time=*/std::chrono::seconds{random_context.randrange(100)},
     112           0 :             /*fRelevantServices=*/random_context.randbool(),
     113           0 :             /*m_relay_txs=*/random_context.randbool(),
     114           0 :             /*fBloomFilter=*/random_context.randbool(),
     115           0 :             /*nKeyedNetGroup=*/random_context.randrange(100),
     116           0 :             /*prefer_evict=*/random_context.randbool(),
     117           0 :             /*m_is_local=*/random_context.randbool(),
     118           0 :             /*m_network=*/ALL_NETWORKS[random_context.randrange(ALL_NETWORKS.size())],
     119             :             /*m_noban=*/false,
     120             :             /*m_conn_type=*/ConnectionType::INBOUND,
     121             :         });
     122           0 :     }
     123           0 :     return candidates;
     124           0 : }

Generated by: LCOV version 1.14