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