|
| 1 | +// Copyright (c) 2025 The Dash 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/llmq_tests.h> |
| 6 | +#include <test/util/setup_common.h> |
| 7 | + |
| 8 | +#include <llmq/clsig.h> |
| 9 | +#include <streams.h> |
| 10 | +#include <util/strencodings.h> |
| 11 | + |
| 12 | +#include <boost/test/unit_test.hpp> |
| 13 | + |
| 14 | +using namespace llmq; |
| 15 | +using namespace llmq::testutils; |
| 16 | + |
| 17 | +BOOST_FIXTURE_TEST_SUITE(llmq_chainlock_tests, BasicTestingSetup) |
| 18 | + |
| 19 | +BOOST_AUTO_TEST_CASE(chainlock_construction_test) |
| 20 | +{ |
| 21 | + // Test default constructor |
| 22 | + CChainLockSig clsig1; |
| 23 | + BOOST_CHECK(clsig1.IsNull()); |
| 24 | + BOOST_CHECK_EQUAL(clsig1.getHeight(), -1); |
| 25 | + BOOST_CHECK(clsig1.getBlockHash().IsNull()); |
| 26 | + BOOST_CHECK(!clsig1.getSig().IsValid()); |
| 27 | + |
| 28 | + // Test parameterized constructor |
| 29 | + int32_t height = 12345; |
| 30 | + uint256 blockHash = GetTestBlockHash(1); |
| 31 | + CBLSSignature sig = CreateRandomBLSSignature(); |
| 32 | + |
| 33 | + CChainLockSig clsig2(height, blockHash, sig); |
| 34 | + BOOST_CHECK(!clsig2.IsNull()); |
| 35 | + BOOST_CHECK_EQUAL(clsig2.getHeight(), height); |
| 36 | + BOOST_CHECK(clsig2.getBlockHash() == blockHash); |
| 37 | + BOOST_CHECK(clsig2.getSig() == sig); |
| 38 | +} |
| 39 | + |
| 40 | +BOOST_AUTO_TEST_CASE(chainlock_null_test) |
| 41 | +{ |
| 42 | + CChainLockSig clsig; |
| 43 | + |
| 44 | + // Default constructed should be null |
| 45 | + BOOST_CHECK(clsig.IsNull()); |
| 46 | + |
| 47 | + // With height set but null hash, should not be null |
| 48 | + clsig = CChainLockSig(100, uint256(), CBLSSignature()); |
| 49 | + BOOST_CHECK(!clsig.IsNull()); |
| 50 | + |
| 51 | + // With valid data should not be null |
| 52 | + clsig = CreateChainLock(100, GetTestBlockHash(1)); |
| 53 | + BOOST_CHECK(!clsig.IsNull()); |
| 54 | +} |
| 55 | + |
| 56 | +BOOST_AUTO_TEST_CASE(chainlock_serialization_test) |
| 57 | +{ |
| 58 | + // Test with valid chainlock |
| 59 | + CChainLockSig clsig = CreateChainLock(67890, GetTestBlockHash(42)); |
| 60 | + |
| 61 | + // Test serialization preserves all fields |
| 62 | + CDataStream ss(SER_NETWORK, PROTOCOL_VERSION); |
| 63 | + ss << clsig; |
| 64 | + |
| 65 | + CChainLockSig deserialized; |
| 66 | + ss >> deserialized; |
| 67 | + |
| 68 | + BOOST_CHECK_EQUAL(clsig.getHeight(), deserialized.getHeight()); |
| 69 | + BOOST_CHECK(clsig.getBlockHash() == deserialized.getBlockHash()); |
| 70 | + BOOST_CHECK(clsig.getSig() == deserialized.getSig()); |
| 71 | + BOOST_CHECK_EQUAL(clsig.IsNull(), deserialized.IsNull()); |
| 72 | +} |
| 73 | + |
| 74 | +BOOST_AUTO_TEST_CASE(chainlock_tostring_test) |
| 75 | +{ |
| 76 | + // Test null chainlock |
| 77 | + CChainLockSig nullClsig; |
| 78 | + std::string nullStr = nullClsig.ToString(); |
| 79 | + BOOST_CHECK(!nullStr.empty()); |
| 80 | + |
| 81 | + // Test valid chainlock |
| 82 | + int32_t height = 123456; |
| 83 | + uint256 blockHash = GetTestBlockHash(789); |
| 84 | + CChainLockSig clsig = CreateChainLock(height, blockHash); |
| 85 | + |
| 86 | + std::string str = clsig.ToString(); |
| 87 | + BOOST_CHECK(!str.empty()); |
| 88 | + |
| 89 | + // ToString should contain height and hash info |
| 90 | + BOOST_CHECK(str.find(strprintf("%d", height)) != std::string::npos); |
| 91 | + BOOST_CHECK(str.find(blockHash.ToString().substr(0, 10)) != std::string::npos); |
| 92 | +} |
| 93 | + |
| 94 | +BOOST_AUTO_TEST_CASE(chainlock_edge_cases_test) |
| 95 | +{ |
| 96 | + // Test with edge case heights |
| 97 | + CChainLockSig clsig1 = CreateChainLock(0, GetTestBlockHash(1)); |
| 98 | + BOOST_CHECK_EQUAL(clsig1.getHeight(), 0); |
| 99 | + BOOST_CHECK(!clsig1.IsNull()); |
| 100 | + |
| 101 | + CChainLockSig clsig2 = CreateChainLock(std::numeric_limits<int32_t>::max(), GetTestBlockHash(2)); |
| 102 | + BOOST_CHECK_EQUAL(clsig2.getHeight(), std::numeric_limits<int32_t>::max()); |
| 103 | + |
| 104 | + // Test serialization with extreme values |
| 105 | + CDataStream ss1(SER_NETWORK, PROTOCOL_VERSION); |
| 106 | + ss1 << clsig1; |
| 107 | + CChainLockSig clsig1_deserialized; |
| 108 | + ss1 >> clsig1_deserialized; |
| 109 | + BOOST_CHECK_EQUAL(clsig1.getHeight(), clsig1_deserialized.getHeight()); |
| 110 | + |
| 111 | + CDataStream ss2(SER_NETWORK, PROTOCOL_VERSION); |
| 112 | + ss2 << clsig2; |
| 113 | + CChainLockSig clsig2_deserialized; |
| 114 | + ss2 >> clsig2_deserialized; |
| 115 | + BOOST_CHECK_EQUAL(clsig2.getHeight(), clsig2_deserialized.getHeight()); |
| 116 | +} |
| 117 | + |
| 118 | +BOOST_AUTO_TEST_CASE(chainlock_comparison_test) |
| 119 | +{ |
| 120 | + // Create identical chainlocks |
| 121 | + int32_t height = 5000; |
| 122 | + uint256 blockHash = GetTestBlockHash(10); |
| 123 | + CBLSSignature sig = CreateRandomBLSSignature(); |
| 124 | + |
| 125 | + CChainLockSig clsig1(height, blockHash, sig); |
| 126 | + CChainLockSig clsig2(height, blockHash, sig); |
| 127 | + |
| 128 | + // Verify getters return same values |
| 129 | + BOOST_CHECK_EQUAL(clsig1.getHeight(), clsig2.getHeight()); |
| 130 | + BOOST_CHECK(clsig1.getBlockHash() == clsig2.getBlockHash()); |
| 131 | + BOOST_CHECK(clsig1.getSig() == clsig2.getSig()); |
| 132 | + |
| 133 | + // Different chainlocks |
| 134 | + CChainLockSig clsig3(height + 1, blockHash, sig); |
| 135 | + BOOST_CHECK(clsig1.getHeight() != clsig3.getHeight()); |
| 136 | + |
| 137 | + CChainLockSig clsig4(height, GetTestBlockHash(11), sig); |
| 138 | + BOOST_CHECK(clsig1.getBlockHash() != clsig4.getBlockHash()); |
| 139 | +} |
| 140 | + |
| 141 | +BOOST_AUTO_TEST_CASE(chainlock_malformed_data_test) |
| 142 | +{ |
| 143 | + // Test deserialization of truncated data |
| 144 | + CChainLockSig clsig = CreateChainLock(1000, GetTestBlockHash(5)); |
| 145 | + |
| 146 | + CDataStream ss(SER_NETWORK, PROTOCOL_VERSION); |
| 147 | + ss << clsig; |
| 148 | + |
| 149 | + // Truncate the stream |
| 150 | + std::string data = ss.str(); |
| 151 | + for (size_t truncateAt = 1; truncateAt < data.size(); truncateAt += 10) { |
| 152 | + CDataStream truncated(std::vector<unsigned char>(data.begin(), data.begin() + truncateAt), SER_NETWORK, |
| 153 | + PROTOCOL_VERSION); |
| 154 | + |
| 155 | + CChainLockSig deserialized; |
| 156 | + try { |
| 157 | + truncated >> deserialized; |
| 158 | + // If no exception, verify it's either complete or default |
| 159 | + if (truncateAt < sizeof(int32_t)) { |
| 160 | + BOOST_CHECK(deserialized.IsNull()); |
| 161 | + } |
| 162 | + } catch (const std::exception&) { |
| 163 | + // Expected for most truncation points |
| 164 | + } |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments