Skip to content

Commit 9f0868a

Browse files
committed
[Tests] Add tests for CCoins deserialization
Backports bitcoin/bitcoin 1e44169
1 parent 5006d45 commit 9f0868a

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

src/test/coins_tests.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include "coins.h"
6+
#include "script/standard.h"
67
#include "uint256.h"
8+
#include "utilstrencodings.h"
79
#include "test/test_pivx.h"
810

911
#include <vector>
@@ -175,4 +177,73 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
175177
BOOST_CHECK(missed_an_entry);
176178
}
177179

180+
BOOST_AUTO_TEST_CASE(ccoins_serialization)
181+
{
182+
// Good example
183+
CDataStream ss1(ParseHex("0108835800816115944e077fe7c803cfa57f29b36bf87c1d35b4934b"), SER_DISK, CLIENT_VERSION);
184+
CCoins cc1;
185+
ss1 >> cc1;
186+
BOOST_CHECK_EQUAL(cc1.nVersion, 1);
187+
BOOST_CHECK_EQUAL(cc1.fCoinBase, false);
188+
BOOST_CHECK_EQUAL(cc1.nHeight, 870987);
189+
BOOST_CHECK_EQUAL(cc1.vout.size(), 2);
190+
BOOST_CHECK_EQUAL(cc1.IsAvailable(0), false);
191+
BOOST_CHECK_EQUAL(cc1.IsAvailable(1), true);
192+
BOOST_CHECK_EQUAL(cc1.vout[1].nValue, 60000000000ULL);
193+
BOOST_CHECK_EQUAL(HexStr(cc1.vout[1].scriptPubKey), HexStr(GetScriptForDestination(CKeyID(uint160(ParseHex("816115944e077fe7c803cfa57f29b36bf87c1d35"))))));
194+
195+
// Good example
196+
CDataStream ss2(ParseHex("0111044086ef97d5790061b01caab50f1b8e9c50a5057eb43c2d9563a4eebbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa482d21f"), SER_DISK, CLIENT_VERSION);
197+
CCoins cc2;
198+
ss2 >> cc2;
199+
BOOST_CHECK_EQUAL(cc2.nVersion, 1);
200+
BOOST_CHECK_EQUAL(cc2.fCoinBase, true);
201+
BOOST_CHECK_EQUAL(cc2.nHeight, 59807);
202+
BOOST_CHECK_EQUAL(cc2.vout.size(), 17);
203+
for (int i = 0; i < 17; i++) {
204+
BOOST_CHECK_EQUAL(cc2.IsAvailable(i), i == 4 || i == 16);
205+
}
206+
BOOST_CHECK_EQUAL(cc2.vout[4].nValue, 234925952);
207+
BOOST_CHECK_EQUAL(HexStr(cc2.vout[4].scriptPubKey), HexStr(GetScriptForDestination(CKeyID(uint160(ParseHex("61b01caab50f1b8e9c50a5057eb43c2d9563a4ee"))))));
208+
BOOST_CHECK_EQUAL(cc2.vout[16].nValue, 110397);
209+
BOOST_CHECK_EQUAL(HexStr(cc2.vout[16].scriptPubKey), HexStr(GetScriptForDestination(CKeyID(uint160(ParseHex("8c988f1a4a4de2161e0f50aac7f17e7f9555caa4"))))));
210+
211+
// Smallest possible example
212+
CDataStream ssx(SER_DISK, CLIENT_VERSION);
213+
BOOST_CHECK_EQUAL(HexStr(ssx.begin(), ssx.end()), "");
214+
215+
CDataStream ss3(ParseHex("0004000600"), SER_DISK, CLIENT_VERSION);
216+
CCoins cc3;
217+
ss3 >> cc3;
218+
BOOST_CHECK_EQUAL(cc3.nVersion, 0);
219+
BOOST_CHECK_EQUAL(cc3.fCoinBase, false);
220+
BOOST_CHECK_EQUAL(cc3.nHeight, 0);
221+
BOOST_CHECK_EQUAL(cc3.vout.size(), 1);
222+
BOOST_CHECK_EQUAL(cc3.IsAvailable(0), true);
223+
BOOST_CHECK_EQUAL(cc3.vout[0].nValue, 0);
224+
BOOST_CHECK_EQUAL(cc3.vout[0].scriptPubKey.size(), 0);
225+
226+
// scriptPubKey that ends beyond the end of the stream
227+
CDataStream ss4(ParseHex("0004000800"), SER_DISK, CLIENT_VERSION);
228+
try {
229+
CCoins cc4;
230+
ss4 >> cc4;
231+
BOOST_CHECK_MESSAGE(false, "We should have thrown");
232+
} catch (const std::ios_base::failure& e) {
233+
}
234+
235+
// Very large scriptPubKey (3*10^9 bytes) past the end of the stream
236+
CDataStream tmp(SER_DISK, CLIENT_VERSION);
237+
uint64_t x = 3000000000ULL;
238+
tmp << VARINT(x);
239+
BOOST_CHECK_EQUAL(HexStr(tmp.begin(), tmp.end()), "8a95c0bb00");
240+
CDataStream ss5(ParseHex("0002008a95c0bb0000"), SER_DISK, CLIENT_VERSION);
241+
try {
242+
CCoins cc5;
243+
ss5 >> cc5;
244+
BOOST_CHECK_MESSAGE(false, "We should have thrown");
245+
} catch (const std::ios_base::failure& e) {
246+
}
247+
}
248+
178249
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)