LCOV - code coverage report
Current view: top level - src/test - txreconciliation_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 100.0 % 44 44
Test Date: 2024-07-07 05:05:19 Functions: 100.0 % 6 6
Branches: 50.0 % 362 181

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2021-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 <node/txreconciliation.h>
       6                 :             : 
       7                 :             : #include <test/util/setup_common.h>
       8                 :             : 
       9                 :             : #include <boost/test/unit_test.hpp>
      10                 :             : 
      11                 :             : BOOST_FIXTURE_TEST_SUITE(txreconciliation_tests, BasicTestingSetup)
      12                 :             : 
      13   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(RegisterPeerTest)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      14                 :             : {
      15                 :           1 :     TxReconciliationTracker tracker(TXRECONCILIATION_VERSION);
      16                 :           1 :     const uint64_t salt = 0;
      17                 :             : 
      18                 :             :     // Prepare a peer for reconciliation.
      19         [ +  - ]:           1 :     tracker.PreRegisterPeer(0);
      20                 :             : 
      21                 :             :     // Invalid version.
      22   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(tracker.RegisterPeer(/*peer_id=*/0, /*is_peer_inbound=*/true,
                   +  - ]
      23                 :             :                                            /*peer_recon_version=*/0, salt),
      24                 :             :                       ReconciliationRegisterResult::PROTOCOL_VIOLATION);
      25                 :             : 
      26                 :             :     // Valid registration (inbound and outbound peers).
      27   [ +  -  +  -  :           2 :     BOOST_REQUIRE(!tracker.IsPeerRegistered(0));
             +  -  +  - ]
      28   [ +  -  +  -  :           1 :     BOOST_REQUIRE_EQUAL(tracker.RegisterPeer(0, true, 1, salt), ReconciliationRegisterResult::SUCCESS);
                   +  - ]
      29   [ +  -  +  -  :           2 :     BOOST_CHECK(tracker.IsPeerRegistered(0));
             +  -  +  - ]
      30   [ +  -  +  -  :           2 :     BOOST_REQUIRE(!tracker.IsPeerRegistered(1));
             +  -  +  - ]
      31         [ +  - ]:           1 :     tracker.PreRegisterPeer(1);
      32   [ +  -  +  -  :           2 :     BOOST_REQUIRE(tracker.RegisterPeer(1, false, 1, salt) == ReconciliationRegisterResult::SUCCESS);
             +  -  +  - ]
      33   [ +  -  +  -  :           2 :     BOOST_CHECK(tracker.IsPeerRegistered(1));
             +  -  +  - ]
      34                 :             : 
      35                 :             :     // Reconciliation version is higher than ours, should be able to register.
      36   [ +  -  +  -  :           2 :     BOOST_REQUIRE(!tracker.IsPeerRegistered(2));
             +  -  +  - ]
      37         [ +  - ]:           1 :     tracker.PreRegisterPeer(2);
      38   [ +  -  +  -  :           2 :     BOOST_REQUIRE(tracker.RegisterPeer(2, true, 2, salt) == ReconciliationRegisterResult::SUCCESS);
             +  -  +  - ]
      39   [ +  -  +  -  :           1 :     BOOST_CHECK(tracker.IsPeerRegistered(2));
                   +  - ]
      40                 :             : 
      41                 :             :     // Try registering for the second time.
      42   [ +  -  +  -  :           2 :     BOOST_REQUIRE(tracker.RegisterPeer(1, false, 1, salt) == ReconciliationRegisterResult::ALREADY_REGISTERED);
             +  -  +  - ]
      43                 :             : 
      44                 :             :     // Do not register if there were no pre-registration for the peer.
      45   [ +  -  +  -  :           1 :     BOOST_REQUIRE_EQUAL(tracker.RegisterPeer(100, true, 1, salt), ReconciliationRegisterResult::NOT_FOUND);
                   +  - ]
      46   [ +  -  +  -  :           2 :     BOOST_CHECK(!tracker.IsPeerRegistered(100));
                   +  - ]
      47                 :           1 : }
      48                 :             : 
      49   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(ForgetPeerTest)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      50                 :             : {
      51                 :           1 :     TxReconciliationTracker tracker(TXRECONCILIATION_VERSION);
      52                 :           1 :     NodeId peer_id0 = 0;
      53                 :             : 
      54                 :             :     // Removing peer after pre-registring works and does not let to register the peer.
      55         [ +  - ]:           1 :     tracker.PreRegisterPeer(peer_id0);
      56         [ +  - ]:           1 :     tracker.ForgetPeer(peer_id0);
      57   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(tracker.RegisterPeer(peer_id0, true, 1, 1), ReconciliationRegisterResult::NOT_FOUND);
                   +  - ]
      58                 :             : 
      59                 :             :     // Removing peer after it is registered works.
      60         [ +  - ]:           1 :     tracker.PreRegisterPeer(peer_id0);
      61   [ +  -  +  -  :           2 :     BOOST_REQUIRE(!tracker.IsPeerRegistered(peer_id0));
             +  -  +  - ]
      62   [ +  -  +  -  :           1 :     BOOST_REQUIRE_EQUAL(tracker.RegisterPeer(peer_id0, true, 1, 1), ReconciliationRegisterResult::SUCCESS);
                   +  - ]
      63   [ +  -  +  -  :           2 :     BOOST_CHECK(tracker.IsPeerRegistered(peer_id0));
             +  -  +  - ]
      64         [ +  - ]:           1 :     tracker.ForgetPeer(peer_id0);
      65   [ +  -  +  -  :           2 :     BOOST_CHECK(!tracker.IsPeerRegistered(peer_id0));
                   +  - ]
      66                 :           1 : }
      67                 :             : 
      68   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(IsPeerRegisteredTest)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      69                 :             : {
      70                 :           1 :     TxReconciliationTracker tracker(TXRECONCILIATION_VERSION);
      71                 :           1 :     NodeId peer_id0 = 0;
      72                 :             : 
      73   [ +  -  +  -  :           2 :     BOOST_REQUIRE(!tracker.IsPeerRegistered(peer_id0));
             +  -  +  - ]
      74         [ +  - ]:           1 :     tracker.PreRegisterPeer(peer_id0);
      75   [ +  -  +  -  :           2 :     BOOST_REQUIRE(!tracker.IsPeerRegistered(peer_id0));
             +  -  +  - ]
      76                 :             : 
      77   [ +  -  +  -  :           1 :     BOOST_REQUIRE_EQUAL(tracker.RegisterPeer(peer_id0, true, 1, 1), ReconciliationRegisterResult::SUCCESS);
                   +  - ]
      78   [ +  -  +  -  :           2 :     BOOST_CHECK(tracker.IsPeerRegistered(peer_id0));
             +  -  +  - ]
      79                 :             : 
      80         [ +  - ]:           1 :     tracker.ForgetPeer(peer_id0);
      81   [ +  -  +  -  :           2 :     BOOST_CHECK(!tracker.IsPeerRegistered(peer_id0));
                   +  - ]
      82                 :           1 : }
      83                 :             : 
      84                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1