diff options
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/DoS_tests.cpp | 26 | ||||
-rw-r--r-- | src/test/Makefile.am | 65 | ||||
-rw-r--r-- | src/test/accounting_tests.cpp | 2 | ||||
-rw-r--r-- | src/test/data/tx_invalid.json | 13 | ||||
-rw-r--r-- | src/test/data/tx_valid.json | 33 | ||||
-rw-r--r-- | src/test/miner_tests.cpp | 99 | ||||
-rw-r--r-- | src/test/rpc_tests.cpp | 74 | ||||
-rw-r--r-- | src/test/rpc_wallet_tests.cpp | 89 | ||||
-rw-r--r-- | src/test/script_tests.cpp | 18 | ||||
-rw-r--r-- | src/test/serialize_tests.cpp | 9 | ||||
-rw-r--r-- | src/test/test_bitcoin.cpp | 17 | ||||
-rw-r--r-- | src/test/uint160_tests.cpp | 20 | ||||
-rw-r--r-- | src/test/uint256_tests.cpp | 634 | ||||
-rw-r--r-- | src/test/util_tests.cpp | 8 | ||||
-rw-r--r-- | src/test/wallet_tests.cpp | 2 |
15 files changed, 953 insertions, 156 deletions
diff --git a/src/test/DoS_tests.cpp b/src/test/DoS_tests.cpp index f0fb84bc54..fbca09b4dc 100644 --- a/src/test/DoS_tests.cpp +++ b/src/test/DoS_tests.cpp @@ -21,6 +21,7 @@ // Tests this internal-to-main.cpp method: extern bool AddOrphanTx(const CTransaction& tx); extern unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans); +extern void Misbehaving(NodeId nodeid, int howmuch); extern std::map<uint256, CTransaction> mapOrphanTransactions; extern std::map<uint256, std::set<uint256> > mapOrphanTransactionsByPrev; @@ -38,16 +39,21 @@ BOOST_AUTO_TEST_CASE(DoS_banning) CNode::ClearBanned(); CAddress addr1(ip(0xa0b0c001)); CNode dummyNode1(INVALID_SOCKET, addr1, "", true); - dummyNode1.Misbehaving(100); // Should get banned + dummyNode1.nVersion = 1; + Misbehaving(dummyNode1.GetId(), 100); // Should get banned + SendMessages(&dummyNode1, false); BOOST_CHECK(CNode::IsBanned(addr1)); BOOST_CHECK(!CNode::IsBanned(ip(0xa0b0c001|0x0000ff00))); // Different IP, not banned CAddress addr2(ip(0xa0b0c002)); CNode dummyNode2(INVALID_SOCKET, addr2, "", true); - dummyNode2.Misbehaving(50); + dummyNode2.nVersion = 1; + Misbehaving(dummyNode2.GetId(), 50); + SendMessages(&dummyNode2, false); BOOST_CHECK(!CNode::IsBanned(addr2)); // 2 not banned yet... BOOST_CHECK(CNode::IsBanned(addr1)); // ... but 1 still should be - dummyNode2.Misbehaving(50); + Misbehaving(dummyNode2.GetId(), 50); + SendMessages(&dummyNode2, false); BOOST_CHECK(CNode::IsBanned(addr2)); } @@ -57,11 +63,15 @@ BOOST_AUTO_TEST_CASE(DoS_banscore) mapArgs["-banscore"] = "111"; // because 11 is my favorite number CAddress addr1(ip(0xa0b0c001)); CNode dummyNode1(INVALID_SOCKET, addr1, "", true); - dummyNode1.Misbehaving(100); + dummyNode1.nVersion = 1; + Misbehaving(dummyNode1.GetId(), 100); + SendMessages(&dummyNode1, false); BOOST_CHECK(!CNode::IsBanned(addr1)); - dummyNode1.Misbehaving(10); + Misbehaving(dummyNode1.GetId(), 10); + SendMessages(&dummyNode1, false); BOOST_CHECK(!CNode::IsBanned(addr1)); - dummyNode1.Misbehaving(1); + Misbehaving(dummyNode1.GetId(), 1); + SendMessages(&dummyNode1, false); BOOST_CHECK(CNode::IsBanned(addr1)); mapArgs.erase("-banscore"); } @@ -74,8 +84,10 @@ BOOST_AUTO_TEST_CASE(DoS_bantime) CAddress addr(ip(0xa0b0c001)); CNode dummyNode(INVALID_SOCKET, addr, "", true); + dummyNode.nVersion = 1; - dummyNode.Misbehaving(100); + Misbehaving(dummyNode.GetId(), 100); + SendMessages(&dummyNode, false); BOOST_CHECK(CNode::IsBanned(addr)); SetMockTime(nStartTime+60*60); diff --git a/src/test/Makefile.am b/src/test/Makefile.am index 180fd7effa..667e53c6b4 100644 --- a/src/test/Makefile.am +++ b/src/test/Makefile.am @@ -6,12 +6,15 @@ bin_PROGRAMS = test_bitcoin TESTS = test_bitcoin -JSON_TEST_FILES= data/script_valid.json \ - data/base58_keys_valid.json data/sig_canonical.json \ +JSON_TEST_FILES = \ + data/script_valid.json \ + data/base58_keys_valid.json \ + data/sig_canonical.json \ data/sig_noncanonical.json \ data/base58_encode_decode.json \ data/base58_keys_invalid.json \ - data/script_invalid.json data/tx_invalid.json \ + data/script_invalid.json \ + data/tx_invalid.json \ data/tx_valid.json RAW_TEST_FILES = data/alertTests.raw @@ -20,17 +23,51 @@ BUILT_SOURCES = $(JSON_TEST_FILES:.json=.json.h) $(RAW_TEST_FILES:.raw=.raw.h) # test_bitcoin binary # test_bitcoin_CPPFLAGS = $(AM_CPPFLAGS) $(TESTDEFS) -test_bitcoin_LDADD = $(LIBBITCOIN) $(LIBLEVELDB) $(LIBMEMENV) \ - $(BOOST_LIBS) $(BOOST_UNIT_TEST_FRAMEWORK_LIB) $(BDB_LIBS) -test_bitcoin_SOURCES = accounting_tests.cpp alert_tests.cpp \ - allocator_tests.cpp base32_tests.cpp base58_tests.cpp base64_tests.cpp \ - bignum_tests.cpp bloom_tests.cpp canonical_tests.cpp checkblock_tests.cpp \ - Checkpoints_tests.cpp compress_tests.cpp DoS_tests.cpp getarg_tests.cpp \ - key_tests.cpp miner_tests.cpp mruset_tests.cpp multisig_tests.cpp \ - netbase_tests.cpp pmt_tests.cpp rpc_tests.cpp script_P2SH_tests.cpp \ - script_tests.cpp serialize_tests.cpp sigopcount_tests.cpp test_bitcoin.cpp \ - transaction_tests.cpp uint160_tests.cpp uint256_tests.cpp util_tests.cpp \ - wallet_tests.cpp sighash_tests.cpp $(JSON_TEST_FILES) $(RAW_TEST_FILES) +test_bitcoin_LDADD = $(LIBBITCOIN_SERVER) $(LIBBITCOIN_CLI) $(LIBBITCOIN_COMMON) $(LIBLEVELDB) $(LIBMEMENV) \ + $(BOOST_LIBS) $(BOOST_UNIT_TEST_FRAMEWORK_LIB) +if ENABLE_WALLET +test_bitcoin_LDADD += $(LIBBITCOIN_WALLET) +endif +test_bitcoin_LDADD += $(BDB_LIBS) + +test_bitcoin_SOURCES = \ + alert_tests.cpp \ + allocator_tests.cpp \ + base32_tests.cpp \ + base58_tests.cpp \ + base64_tests.cpp \ + bignum_tests.cpp \ + bloom_tests.cpp \ + canonical_tests.cpp \ + checkblock_tests.cpp \ + Checkpoints_tests.cpp \ + compress_tests.cpp \ + DoS_tests.cpp \ + getarg_tests.cpp \ + key_tests.cpp \ + miner_tests.cpp \ + mruset_tests.cpp \ + multisig_tests.cpp \ + netbase_tests.cpp \ + pmt_tests.cpp \ + rpc_tests.cpp \ + script_P2SH_tests.cpp \ + script_tests.cpp \ + serialize_tests.cpp \ + sigopcount_tests.cpp \ + test_bitcoin.cpp \ + transaction_tests.cpp \ + uint256_tests.cpp \ + util_tests.cpp \ + sighash_tests.cpp \ + $(JSON_TEST_FILES) $(RAW_TEST_FILES) + +if ENABLE_WALLET +test_bitcoin_SOURCES += \ + accounting_tests.cpp \ + wallet_tests.cpp \ + rpc_wallet_tests.cpp +endif nodist_test_bitcoin_SOURCES = $(BUILT_SOURCES) diff --git a/src/test/accounting_tests.cpp b/src/test/accounting_tests.cpp index 5f79436e48..bfdb95927b 100644 --- a/src/test/accounting_tests.cpp +++ b/src/test/accounting_tests.cpp @@ -34,6 +34,8 @@ BOOST_AUTO_TEST_CASE(acc_orderupgrade) CAccountingEntry ae; std::map<int64_t, CAccountingEntry> results; + LOCK(pwalletMain->cs_wallet); + ae.strAccount = ""; ae.nCreditDebit = 1; ae.nTime = 1333333333; diff --git a/src/test/data/tx_invalid.json b/src/test/data/tx_invalid.json index a26f4a87db..faf40ef23d 100644 --- a/src/test/data/tx_invalid.json +++ b/src/test/data/tx_invalid.json @@ -65,5 +65,16 @@ ["Same as the transactions in valid with one input SIGHASH_ALL and one SIGHASH_ANYONECANPAY, but we set the _ANYONECANPAY sequence number, invalidating the SIGHASH_ALL signature"], [[["0000000000000000000000000000000000000000000000000000000000000100", 0, "0x21 0x035e7f0d4d0841bcd56c39337ed086b1a633ee770c1ffdd94ac552a95ac2ce0efc CHECKSIG"], ["0000000000000000000000000000000000000000000000000000000000000200", 0, "0x21 0x035e7f0d4d0841bcd56c39337ed086b1a633ee770c1ffdd94ac552a95ac2ce0efc CHECKSIG"]], - "01000000020001000000000000000000000000000000000000000000000000000000000000000000004948304502203a0f5f0e1f2bdbcd04db3061d18f3af70e07f4f467cbc1b8116f267025f5360b022100c792b6e215afc5afc721a351ec413e714305cb749aae3d7fee76621313418df10101000000000200000000000000000000000000000000000000000000000000000000000000000000484730440220201dc2d030e380e8f9cfb41b442d930fa5a685bb2c8db5906671f865507d0670022018d9e7a8d4c8d86a73c2a724ee38ef983ec249827e0e464841735955c707ece98101000000010100000000000000015100000000", true] + "01000000020001000000000000000000000000000000000000000000000000000000000000000000004948304502203a0f5f0e1f2bdbcd04db3061d18f3af70e07f4f467cbc1b8116f267025f5360b022100c792b6e215afc5afc721a351ec413e714305cb749aae3d7fee76621313418df10101000000000200000000000000000000000000000000000000000000000000000000000000000000484730440220201dc2d030e380e8f9cfb41b442d930fa5a685bb2c8db5906671f865507d0670022018d9e7a8d4c8d86a73c2a724ee38ef983ec249827e0e464841735955c707ece98101000000010100000000000000015100000000", true], + +["Incorrect signature order"], +["Note the input is just required to make the tester happy"], +[[["b3da01dd4aae683c7aee4d5d8b52a540a508e1115f77cd7fa9a291243f501223", 0, "HASH160 0x14 0xb1ce99298d5f07364b57b1e5c9cc00be0b04a954 EQUAL"]], +"01000000012312503f2491a2a97fcd775f11e108a540a5528b5d4dee7a3c68ae4add01dab300000000fdfe000048304502207aacee820e08b0b174e248abd8d7a34ed63b5da3abedb99934df9fddd65c05c4022100dfe87896ab5ee3df476c2655f9fbe5bd089dccbef3e4ea05b5d121169fe7f5f401483045022100f6649b0eddfdfd4ad55426663385090d51ee86c3481bdc6b0c18ea6c0ece2c0b0220561c315b07cffa6f7dd9df96dbae9200c2dee09bf93cc35ca05e6cdf613340aa014c695221031d11db38972b712a9fe1fc023577c7ae3ddb4a3004187d41c45121eecfdbb5b7210207ec36911b6ad2382860d32989c7b8728e9489d7bbc94a6b5509ef0029be128821024ea9fac06f666a4adc3fc1357b7bec1fd0bdece2b9d08579226a8ebde53058e453aeffffffff0180380100000000001976a914c9b99cddf847d10685a4fabaa0baf505f7c3dfab88ac00000000", true], + +["Empty stack when we try to run CHECKSIG"], +[[["ad503f72c18df5801ee64d76090afe4c607fb2b822e9b7b63c5826c50e22fc3b", 0, "0x21 0x027c3a97665bf283a102a587a62a30a0c102d4d3b141015e2cae6f64e2543113e5 CHECKSIG NOT"]], +"01000000013bfc220ec526583cb6b7e922b8b27f604cfe0a09764de61e80f58dc1723f50ad0000000000ffffffff0101000000000000002321027c3a97665bf283a102a587a62a30a0c102d4d3b141015e2cae6f64e2543113e5ac00000000", true], + +["Make diffs cleaner by leaving a comment here without comma at the end"] ] diff --git a/src/test/data/tx_valid.json b/src/test/data/tx_valid.json index 63e7074a32..c206f7a722 100644 --- a/src/test/data/tx_valid.json +++ b/src/test/data/tx_valid.json @@ -87,5 +87,36 @@ ["ddc454a1c0c35c188c98976b17670f69e586d9c0f3593ea879928332f0a069e7, which spends an input that pushes using a PUSHDATA1 that is negative when read as signed"], [[["c5510a5dd97a25f43175af1fe649b707b1df8e1a41489bac33a23087027a2f48", 0, "0x4c 0xae 0x606563686f2022553246736447566b58312b5a536e587574356542793066794778625456415675534a6c376a6a334878416945325364667657734f53474f36633338584d7439435c6e543249584967306a486956304f376e775236644546673d3d22203e20743b206f70656e73736c20656e63202d7061737320706173733a5b314a564d7751432d707269766b65792d6865785d202d64202d6165732d3235362d636263202d61202d696e207460 DROP DUP HASH160 0x14 0xbfd7436b6265aa9de506f8a994f881ff08cc2872 EQUALVERIFY CHECKSIG"]], - "0100000001482f7a028730a233ac9b48411a8edfb107b749e61faf7531f4257ad95d0a51c5000000008b483045022100bf0bbae9bde51ad2b222e87fbf67530fbafc25c903519a1e5dcc52a32ff5844e022028c4d9ad49b006dd59974372a54291d5764be541574bb0c4dc208ec51f80b7190141049dd4aad62741dc27d5f267f7b70682eee22e7e9c1923b9c0957bdae0b96374569b460eb8d5b40d972e8c7c0ad441de3d94c4a29864b212d56050acb980b72b2bffffffff0180969800000000001976a914e336d0017a9d28de99d16472f6ca6d5a3a8ebc9988ac00000000", true] + "0100000001482f7a028730a233ac9b48411a8edfb107b749e61faf7531f4257ad95d0a51c5000000008b483045022100bf0bbae9bde51ad2b222e87fbf67530fbafc25c903519a1e5dcc52a32ff5844e022028c4d9ad49b006dd59974372a54291d5764be541574bb0c4dc208ec51f80b7190141049dd4aad62741dc27d5f267f7b70682eee22e7e9c1923b9c0957bdae0b96374569b460eb8d5b40d972e8c7c0ad441de3d94c4a29864b212d56050acb980b72b2bffffffff0180969800000000001976a914e336d0017a9d28de99d16472f6ca6d5a3a8ebc9988ac00000000", true], + +["Correct signature order"], +["Note the input is just required to make the tester happy"], +[[["b3da01dd4aae683c7aee4d5d8b52a540a508e1115f77cd7fa9a291243f501223", 0, "HASH160 0x14 0xb1ce99298d5f07364b57b1e5c9cc00be0b04a954 EQUAL"]], +"01000000012312503f2491a2a97fcd775f11e108a540a5528b5d4dee7a3c68ae4add01dab300000000fdfe0000483045022100f6649b0eddfdfd4ad55426663385090d51ee86c3481bdc6b0c18ea6c0ece2c0b0220561c315b07cffa6f7dd9df96dbae9200c2dee09bf93cc35ca05e6cdf613340aa0148304502207aacee820e08b0b174e248abd8d7a34ed63b5da3abedb99934df9fddd65c05c4022100dfe87896ab5ee3df476c2655f9fbe5bd089dccbef3e4ea05b5d121169fe7f5f4014c695221031d11db38972b712a9fe1fc023577c7ae3ddb4a3004187d41c45121eecfdbb5b7210207ec36911b6ad2382860d32989c7b8728e9489d7bbc94a6b5509ef0029be128821024ea9fac06f666a4adc3fc1357b7bec1fd0bdece2b9d08579226a8ebde53058e453aeffffffff0180380100000000001976a914c9b99cddf847d10685a4fabaa0baf505f7c3dfab88ac00000000", true], + +["cc60b1f899ec0a69b7c3f25ddf32c4524096a9c5b01cbd84c6d0312a0c478984, which is a fairly strange transaction which relies on OP_CHECKSIG returning 0 when checking a completely invalid sig of length 0"], +[[["cbebc4da731e8995fe97f6fadcd731b36ad40e5ecb31e38e904f6e5982fa09f7", 0, "0x2102085c6600657566acc2d6382a47bc3f324008d2aa10940dd7705a48aa2a5a5e33ac7c2103f5d0fb955f95dd6be6115ce85661db412ec6a08abcbfce7da0ba8297c6cc0ec4ac7c5379a820d68df9e32a147cffa36193c6f7c43a1c8c69cda530e1c6db354bfabdcfefaf3c875379a820f531f3041d3136701ea09067c53e7159c8f9b2746a56c3d82966c54bbc553226879a5479827701200122a59a5379827701200122a59a6353798277537982778779679a68"]], +"0100000001f709fa82596e4f908ee331cb5e0ed46ab331d7dcfaf697fe95891e73dac4ebcb000000008c20ca42095840735e89283fec298e62ac2ddea9b5f34a8cbb7097ad965b87568100201b1b01dc829177da4a14551d2fc96a9db00c6501edfa12f22cd9cefd335c227f483045022100a9df60536df5733dd0de6bc921fab0b3eee6426501b43a228afa2c90072eb5ca02201c78b74266fac7d1db5deff080d8a403743203f109fbcabf6d5a760bf87386d20100ffffffff01c075790000000000232103611f9a45c18f28f06f19076ad571c344c82ce8fcfe34464cf8085217a2d294a6ac00000000", true], + +["Empty pubkey"], +[[["229257c295e7f555421c1bfec8538dd30a4b5c37c1c8810bbe83cafa7811652c", 0, "0x00 CHECKSIG NOT"]], +"01000000012c651178faca83be0b81c8c1375c4b0ad38d53c8fe1b1c4255f5e795c25792220000000049483045022100d6044562284ac76c985018fc4a90127847708c9edb280996c507b28babdc4b2a02203d74eca3f1a4d1eea7ff77b528fde6d5dc324ec2dbfdb964ba885f643b9704cd01ffffffff010100000000000000232102c2410f8891ae918cab4ffc4bb4a3b0881be67c7a1e7faa8b5acf9ab8932ec30cac00000000", true], + +["Empty signature"], +[[["9ca93cfd8e3806b9d9e2ba1cf64e3cc6946ee0119670b1796a09928d14ea25f7", 0, "0x21 0x028a1d66975dbdf97897e3a4aef450ebeb5b5293e4a0b4a6d3a2daaa0b2b110e02 CHECKSIG NOT"]], +"0100000001f725ea148d92096a79b1709611e06e94c63c4ef61cbae2d9b906388efd3ca99c000000000100ffffffff0101000000000000002321028a1d66975dbdf97897e3a4aef450ebeb5b5293e4a0b4a6d3a2daaa0b2b110e02ac00000000", true], + +[[["444e00ed7840d41f20ecd9c11d3f91982326c731a02f3c05748414a4fa9e59be", 0, "1 0x00 0x21 0x02136b04758b0b6e363e7a6fbe83aaf527a153db2b060d36cc29f7f8309ba6e458 2 CHECKMULTISIG"]], +"0100000001be599efaa4148474053c2fa031c7262398913f1dc1d9ec201fd44078ed004e44000000004900473044022022b29706cb2ed9ef0cb3c97b72677ca2dfd7b4160f7b4beb3ba806aa856c401502202d1e52582412eba2ed474f1f437a427640306fd3838725fab173ade7fe4eae4a01ffffffff010100000000000000232103ac4bba7e7ca3e873eea49e08132ad30c7f03640b6539e9b59903cf14fd016bbbac00000000", true], + +[[["e16abbe80bf30c080f63830c8dbf669deaef08957446e95940227d8c5e6db612", 0, "1 0x21 0x03905380c7013e36e6e19d305311c1b81fce6581f5ee1c86ef0627c68c9362fc9f 0x00 2 CHECKMULTISIG"]], +"010000000112b66d5e8c7d224059e946749508efea9d66bf8d0c83630f080cf30be8bb6ae100000000490047304402206ffe3f14caf38ad5c1544428e99da76ffa5455675ec8d9780fac215ca17953520220779502985e194d84baa36b9bd40a0dbd981163fa191eb884ae83fc5bd1c86b1101ffffffff010100000000000000232103905380c7013e36e6e19d305311c1b81fce6581f5ee1c86ef0627c68c9362fc9fac00000000", true], + +[[["ebbcf4bfce13292bd791d6a65a2a858d59adbf737e387e40370d4e64cc70efb0", 0, "2 0x21 0x033bcaa0a602f0d44cc9d5637c6e515b0471db514c020883830b7cefd73af04194 0x21 0x03a88b326f8767f4f192ce252afe33c94d25ab1d24f27f159b3cb3aa691ffe1423 2 CHECKMULTISIG NOT"]], +"0100000001b0ef70cc644e0d37407e387e73bfad598d852a5aa6d691d72b2913cebff4bceb000000004a00473044022068cd4851fc7f9a892ab910df7a24e616f293bcb5c5fbdfbc304a194b26b60fba022078e6da13d8cb881a22939b952c24f88b97afd06b4c47a47d7f804c9a352a6d6d0100ffffffff0101000000000000002321033bcaa0a602f0d44cc9d5637c6e515b0471db514c020883830b7cefd73af04194ac00000000", true], + +[[["ba4cd7ae2ad4d4d13ebfc8ab1d93a63e4a6563f25089a18bf0fc68f282aa88c1", 0, "2 0x21 0x037c615d761e71d38903609bf4f46847266edc2fb37532047d747ba47eaae5ffe1 0x21 0x02edc823cd634f2c4033d94f5755207cb6b60c4b1f1f056ad7471c47de5f2e4d50 2 CHECKMULTISIG NOT"]], +"0100000001c188aa82f268fcf08ba18950f263654a3ea6931dabc8bf3ed1d4d42aaed74cba000000004b0000483045022100940378576e069aca261a6b26fb38344e4497ca6751bb10905c76bb689f4222b002204833806b014c26fd801727b792b1260003c55710f87c5adbd7a9cb57446dbc9801ffffffff0101000000000000002321037c615d761e71d38903609bf4f46847266edc2fb37532047d747ba47eaae5ffe1ac00000000", true], + +["Make diffs cleaner by leaving a comment here without comma at the end"] ] diff --git a/src/test/miner_tests.cpp b/src/test/miner_tests.cpp index dcb7f9abd4..d35137a663 100644 --- a/src/test/miner_tests.cpp +++ b/src/test/miner_tests.cpp @@ -4,11 +4,9 @@ #include "miner.h" #include "uint256.h" #include "util.h" -#include "wallet.h" #include <boost/test/unit_test.hpp> -extern CWallet* pwalletMain; extern void SHA256Transform(void* pstate, void* pinput, const void* pinit); BOOST_AUTO_TEST_SUITE(miner_tests) @@ -51,14 +49,16 @@ struct { // NOTE: These tests rely on CreateNewBlock doing its own self-validation! BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) { - CReserveKey reservekey(pwalletMain); + CScript scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG; CBlockTemplate *pblocktemplate; - CTransaction tx; + CTransaction tx,tx2; CScript script; uint256 hash; + LOCK(cs_main); + // Simple block creation, nothing special yet: - BOOST_CHECK(pblocktemplate = CreateNewBlockWithKey(reservekey)); + BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey)); // We can't make transactions until we have inputs // Therefore, load 100 blocks :) @@ -84,7 +84,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) delete pblocktemplate; // Just to make sure we can still make simple blocks - BOOST_CHECK(pblocktemplate = CreateNewBlockWithKey(reservekey)); + BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey)); delete pblocktemplate; // block sigops > limit: 1000 CHECKMULTISIG + 1 @@ -99,10 +99,10 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) { tx.vout[0].nValue -= 1000000; hash = tx.GetHash(); - mempool.addUnchecked(hash, tx); + mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11)); tx.vin[0].prevout.hash = hash; } - BOOST_CHECK(pblocktemplate = CreateNewBlockWithKey(reservekey)); + BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey)); delete pblocktemplate; mempool.clear(); @@ -119,17 +119,17 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) { tx.vout[0].nValue -= 10000000; hash = tx.GetHash(); - mempool.addUnchecked(hash, tx); + mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11)); tx.vin[0].prevout.hash = hash; } - BOOST_CHECK(pblocktemplate = CreateNewBlockWithKey(reservekey)); + BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey)); delete pblocktemplate; mempool.clear(); // orphan in mempool hash = tx.GetHash(); - mempool.addUnchecked(hash, tx); - BOOST_CHECK(pblocktemplate = CreateNewBlockWithKey(reservekey)); + mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11)); + BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey)); delete pblocktemplate; mempool.clear(); @@ -138,7 +138,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) tx.vin[0].prevout.hash = txFirst[1]->GetHash(); tx.vout[0].nValue = 4900000000LL; hash = tx.GetHash(); - mempool.addUnchecked(hash, tx); + mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11)); tx.vin[0].prevout.hash = hash; tx.vin.resize(2); tx.vin[1].scriptSig = CScript() << OP_1; @@ -146,8 +146,8 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) tx.vin[1].prevout.n = 0; tx.vout[0].nValue = 5900000000LL; hash = tx.GetHash(); - mempool.addUnchecked(hash, tx); - BOOST_CHECK(pblocktemplate = CreateNewBlockWithKey(reservekey)); + mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11)); + BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey)); delete pblocktemplate; mempool.clear(); @@ -157,8 +157,8 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) tx.vin[0].scriptSig = CScript() << OP_0 << OP_1; tx.vout[0].nValue = 0; hash = tx.GetHash(); - mempool.addUnchecked(hash, tx); - BOOST_CHECK(pblocktemplate = CreateNewBlockWithKey(reservekey)); + mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11)); + BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey)); delete pblocktemplate; mempool.clear(); @@ -170,13 +170,13 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) script = CScript() << OP_0; tx.vout[0].scriptPubKey.SetDestination(script.GetID()); hash = tx.GetHash(); - mempool.addUnchecked(hash, tx); + mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11)); tx.vin[0].prevout.hash = hash; tx.vin[0].scriptSig = CScript() << (std::vector<unsigned char>)script; tx.vout[0].nValue -= 1000000; hash = tx.GetHash(); - mempool.addUnchecked(hash,tx); - BOOST_CHECK(pblocktemplate = CreateNewBlockWithKey(reservekey)); + mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11)); + BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey)); delete pblocktemplate; mempool.clear(); @@ -186,26 +186,75 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) tx.vout[0].nValue = 4900000000LL; tx.vout[0].scriptPubKey = CScript() << OP_1; hash = tx.GetHash(); - mempool.addUnchecked(hash, tx); + mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11)); tx.vout[0].scriptPubKey = CScript() << OP_2; hash = tx.GetHash(); - mempool.addUnchecked(hash, tx); - BOOST_CHECK(pblocktemplate = CreateNewBlockWithKey(reservekey)); + mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11)); + BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey)); delete pblocktemplate; mempool.clear(); // subsidy changing int nHeight = chainActive.Height(); chainActive.Tip()->nHeight = 209999; - BOOST_CHECK(pblocktemplate = CreateNewBlockWithKey(reservekey)); + BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey)); delete pblocktemplate; chainActive.Tip()->nHeight = 210000; - BOOST_CHECK(pblocktemplate = CreateNewBlockWithKey(reservekey)); + BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey)); delete pblocktemplate; chainActive.Tip()->nHeight = nHeight; + // non-final txs in mempool + SetMockTime(chainActive.Tip()->GetMedianTimePast()+1); + + // height locked + tx.vin[0].prevout.hash = txFirst[0]->GetHash(); + tx.vin[0].scriptSig = CScript() << OP_1; + tx.vin[0].nSequence = 0; + tx.vout[0].nValue = 4900000000LL; + tx.vout[0].scriptPubKey = CScript() << OP_1; + tx.nLockTime = chainActive.Tip()->nHeight+1; + hash = tx.GetHash(); + mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11)); + BOOST_CHECK(!IsFinalTx(tx, chainActive.Tip()->nHeight + 1)); + + // time locked + tx2.vin.resize(1); + tx2.vin[0].prevout.hash = txFirst[1]->GetHash(); + tx2.vin[0].prevout.n = 0; + tx2.vin[0].scriptSig = CScript() << OP_1; + tx2.vin[0].nSequence = 0; + tx2.vout.resize(1); + tx2.vout[0].nValue = 4900000000LL; + tx2.vout[0].scriptPubKey = CScript() << OP_1; + tx2.nLockTime = chainActive.Tip()->GetMedianTimePast()+1; + hash = tx2.GetHash(); + mempool.addUnchecked(hash, CTxMemPoolEntry(tx2, 11, GetTime(), 111.0, 11)); + BOOST_CHECK(!IsFinalTx(tx2)); + + BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey)); + + // Neither tx should have make it into the template. + BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 1); + delete pblocktemplate; + + // However if we advance height and time by one, both will. + chainActive.Tip()->nHeight++; + SetMockTime(chainActive.Tip()->GetMedianTimePast()+2); + + BOOST_CHECK(IsFinalTx(tx, chainActive.Tip()->nHeight + 1)); + BOOST_CHECK(IsFinalTx(tx2)); + + BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey)); + BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 3); + delete pblocktemplate; + + chainActive.Tip()->nHeight--; + SetMockTime(0); + BOOST_FOREACH(CTransaction *tx, txFirst) delete tx; + } BOOST_AUTO_TEST_CASE(sha256transform_equality) diff --git a/src/test/rpc_tests.cpp b/src/test/rpc_tests.cpp index 4fb2aeb6ae..29195545da 100644 --- a/src/test/rpc_tests.cpp +++ b/src/test/rpc_tests.cpp @@ -1,4 +1,5 @@ -#include "bitcoinrpc.h" +#include "rpcserver.h" +#include "rpcclient.h" #include "base58.h" @@ -8,9 +9,7 @@ using namespace std; using namespace json_spirit; -BOOST_AUTO_TEST_SUITE(rpc_tests) - -static Array +Array createArgs(int nRequired, const char* address1=NULL, const char* address2=NULL) { Array result; @@ -22,44 +21,7 @@ createArgs(int nRequired, const char* address1=NULL, const char* address2=NULL) return result; } -BOOST_AUTO_TEST_CASE(rpc_addmultisig) -{ - rpcfn_type addmultisig = tableRPC["addmultisigaddress"]->actor; - - // old, 65-byte-long: - const char address1Hex[] = "0434e3e09f49ea168c5bbf53f877ff4206923858aab7c7e1df25bc263978107c95e35065a27ef6f1b27222db0ec97e0e895eaca603d3ee0d4c060ce3d8a00286c8"; - // new, compressed: - const char address2Hex[] = "0388c2037017c62240b6b72ac1a2a5f94da790596ebd06177c8572752922165cb4"; - - Value v; - CBitcoinAddress address; - BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(1, address1Hex), false)); - address.SetString(v.get_str()); - BOOST_CHECK(address.IsValid() && address.IsScript()); - - BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(1, address1Hex, address2Hex), false)); - address.SetString(v.get_str()); - BOOST_CHECK(address.IsValid() && address.IsScript()); - - BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(2, address1Hex, address2Hex), false)); - address.SetString(v.get_str()); - BOOST_CHECK(address.IsValid() && address.IsScript()); - - BOOST_CHECK_THROW(addmultisig(createArgs(0), false), runtime_error); - BOOST_CHECK_THROW(addmultisig(createArgs(1), false), runtime_error); - BOOST_CHECK_THROW(addmultisig(createArgs(2, address1Hex), false), runtime_error); - - BOOST_CHECK_THROW(addmultisig(createArgs(1, ""), false), runtime_error); - BOOST_CHECK_THROW(addmultisig(createArgs(1, "NotAValidPubkey"), false), runtime_error); - - string short1(address1Hex, address1Hex+sizeof(address1Hex)-2); // last byte missing - BOOST_CHECK_THROW(addmultisig(createArgs(2, short1.c_str()), false), runtime_error); - - string short2(address1Hex+1, address1Hex+sizeof(address1Hex)); // first byte missing - BOOST_CHECK_THROW(addmultisig(createArgs(2, short2.c_str()), false), runtime_error); -} - -static Value CallRPC(string args) +Value CallRPC(string args) { vector<string> vArgs; boost::split(vArgs, args, boost::is_any_of(" \t")); @@ -78,34 +40,8 @@ static Value CallRPC(string args) } } -BOOST_AUTO_TEST_CASE(rpc_wallet) -{ - // Test RPC calls for various wallet statistics - Value r; - - BOOST_CHECK_NO_THROW(CallRPC("listunspent")); - BOOST_CHECK_THROW(CallRPC("listunspent string"), runtime_error); - BOOST_CHECK_THROW(CallRPC("listunspent 0 string"), runtime_error); - BOOST_CHECK_THROW(CallRPC("listunspent 0 1 not_array"), runtime_error); - BOOST_CHECK_THROW(CallRPC("listunspent 0 1 [] extra"), runtime_error); - BOOST_CHECK_NO_THROW(r=CallRPC("listunspent 0 1 []")); - BOOST_CHECK(r.get_array().empty()); - - BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaddress")); - BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaddress 0")); - BOOST_CHECK_THROW(CallRPC("listreceivedbyaddress not_int"), runtime_error); - BOOST_CHECK_THROW(CallRPC("listreceivedbyaddress 0 not_bool"), runtime_error); - BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaddress 0 true")); - BOOST_CHECK_THROW(CallRPC("listreceivedbyaddress 0 true extra"), runtime_error); - - BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaccount")); - BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaccount 0")); - BOOST_CHECK_THROW(CallRPC("listreceivedbyaccount not_int"), runtime_error); - BOOST_CHECK_THROW(CallRPC("listreceivedbyaccount 0 not_bool"), runtime_error); - BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaccount 0 true")); - BOOST_CHECK_THROW(CallRPC("listreceivedbyaccount 0 true extra"), runtime_error); -} +BOOST_AUTO_TEST_SUITE(rpc_tests) BOOST_AUTO_TEST_CASE(rpc_rawparams) { diff --git a/src/test/rpc_wallet_tests.cpp b/src/test/rpc_wallet_tests.cpp new file mode 100644 index 0000000000..628ba95067 --- /dev/null +++ b/src/test/rpc_wallet_tests.cpp @@ -0,0 +1,89 @@ +#include "rpcserver.h" +#include "rpcclient.h" + +#include "base58.h" +#include "wallet.h" + +#include <boost/algorithm/string.hpp> +#include <boost/test/unit_test.hpp> + +using namespace std; +using namespace json_spirit; + +extern Array createArgs(int nRequired, const char* address1=NULL, const char* address2=NULL); +extern Value CallRPC(string args); + +extern CWallet* pwalletMain; + +BOOST_AUTO_TEST_SUITE(rpc_wallet_tests) + +BOOST_AUTO_TEST_CASE(rpc_addmultisig) +{ + LOCK(pwalletMain->cs_wallet); + + rpcfn_type addmultisig = tableRPC["addmultisigaddress"]->actor; + + // old, 65-byte-long: + const char address1Hex[] = "0434e3e09f49ea168c5bbf53f877ff4206923858aab7c7e1df25bc263978107c95e35065a27ef6f1b27222db0ec97e0e895eaca603d3ee0d4c060ce3d8a00286c8"; + // new, compressed: + const char address2Hex[] = "0388c2037017c62240b6b72ac1a2a5f94da790596ebd06177c8572752922165cb4"; + + Value v; + CBitcoinAddress address; + BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(1, address1Hex), false)); + address.SetString(v.get_str()); + BOOST_CHECK(address.IsValid() && address.IsScript()); + + BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(1, address1Hex, address2Hex), false)); + address.SetString(v.get_str()); + BOOST_CHECK(address.IsValid() && address.IsScript()); + + BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(2, address1Hex, address2Hex), false)); + address.SetString(v.get_str()); + BOOST_CHECK(address.IsValid() && address.IsScript()); + + BOOST_CHECK_THROW(addmultisig(createArgs(0), false), runtime_error); + BOOST_CHECK_THROW(addmultisig(createArgs(1), false), runtime_error); + BOOST_CHECK_THROW(addmultisig(createArgs(2, address1Hex), false), runtime_error); + + BOOST_CHECK_THROW(addmultisig(createArgs(1, ""), false), runtime_error); + BOOST_CHECK_THROW(addmultisig(createArgs(1, "NotAValidPubkey"), false), runtime_error); + + string short1(address1Hex, address1Hex+sizeof(address1Hex)-2); // last byte missing + BOOST_CHECK_THROW(addmultisig(createArgs(2, short1.c_str()), false), runtime_error); + + string short2(address1Hex+1, address1Hex+sizeof(address1Hex)); // first byte missing + BOOST_CHECK_THROW(addmultisig(createArgs(2, short2.c_str()), false), runtime_error); +} + +BOOST_AUTO_TEST_CASE(rpc_wallet) +{ + // Test RPC calls for various wallet statistics + Value r; + + LOCK(pwalletMain->cs_wallet); + + BOOST_CHECK_NO_THROW(CallRPC("listunspent")); + BOOST_CHECK_THROW(CallRPC("listunspent string"), runtime_error); + BOOST_CHECK_THROW(CallRPC("listunspent 0 string"), runtime_error); + BOOST_CHECK_THROW(CallRPC("listunspent 0 1 not_array"), runtime_error); + BOOST_CHECK_THROW(CallRPC("listunspent 0 1 [] extra"), runtime_error); + BOOST_CHECK_NO_THROW(r=CallRPC("listunspent 0 1 []")); + BOOST_CHECK(r.get_array().empty()); + + BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaddress")); + BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaddress 0")); + BOOST_CHECK_THROW(CallRPC("listreceivedbyaddress not_int"), runtime_error); + BOOST_CHECK_THROW(CallRPC("listreceivedbyaddress 0 not_bool"), runtime_error); + BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaddress 0 true")); + BOOST_CHECK_THROW(CallRPC("listreceivedbyaddress 0 true extra"), runtime_error); + + BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaccount")); + BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaccount 0")); + BOOST_CHECK_THROW(CallRPC("listreceivedbyaccount not_int"), runtime_error); + BOOST_CHECK_THROW(CallRPC("listreceivedbyaccount 0 not_bool"), runtime_error); + BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaccount 0 true")); + BOOST_CHECK_THROW(CallRPC("listreceivedbyaccount 0 true extra"), runtime_error); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/script_tests.cpp b/src/test/script_tests.cpp index dee0f110ad..dd1b613047 100644 --- a/src/test/script_tests.cpp +++ b/src/test/script_tests.cpp @@ -438,4 +438,22 @@ BOOST_AUTO_TEST_CASE(script_combineSigs) BOOST_CHECK(combined == partial3c); } +BOOST_AUTO_TEST_CASE(script_standard_push) +{ + for (int i=0; i<1000; i++) { + CScript script; + script << i; + BOOST_CHECK_MESSAGE(script.IsPushOnly(), "Number " << i << " is not pure push."); + BOOST_CHECK_MESSAGE(script.HasCanonicalPushes(), "Number " << i << " push is not canonical."); + } + + for (int i=0; i<1000; i++) { + std::vector<unsigned char> data(i, '\111'); + CScript script; + script << data; + BOOST_CHECK_MESSAGE(script.IsPushOnly(), "Length " << i << " is not pure push."); + BOOST_CHECK_MESSAGE(script.HasCanonicalPushes(), "Length " << i << " push is not canonical."); + } +} + BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/serialize_tests.cpp b/src/test/serialize_tests.cpp index afcdd118bc..415f957811 100644 --- a/src/test/serialize_tests.cpp +++ b/src/test/serialize_tests.cpp @@ -61,9 +61,16 @@ BOOST_AUTO_TEST_CASE(compactsize) static bool isCanonicalException(const std::ios_base::failure& ex) { - return std::string("non-canonical ReadCompactSize()") == ex.what(); + std::ios_base::failure expectedException("non-canonical ReadCompactSize()"); + + // The string returned by what() can be different for different platforms. + // Instead of directly comparing the ex.what() with an expected string, + // create an instance of exception to see if ex.what() matches + // the expected explanatory string returned by the exception instance. + return strcmp(expectedException.what(), ex.what()) == 0; } + BOOST_AUTO_TEST_CASE(noncanonical) { // Write some non-canonical CompactSize encodings, and diff --git a/src/test/test_bitcoin.cpp b/src/test/test_bitcoin.cpp index f947167597..96d0712403 100644 --- a/src/test/test_bitcoin.cpp +++ b/src/test/test_bitcoin.cpp @@ -2,19 +2,20 @@ -#include "db.h" #include "main.h" #include "txdb.h" #include "ui_interface.h" #include "util.h" +#ifdef ENABLE_WALLET +#include "db.h" #include "wallet.h" +#endif #include <boost/filesystem.hpp> #include <boost/test/unit_test.hpp> CWallet* pwalletMain; -CClientUIInterface uiInterface; extern bool fPrintToConsole; extern void noui_connect(); @@ -25,9 +26,11 @@ struct TestingSetup { boost::thread_group threadGroup; TestingSetup() { - fPrintToDebugger = true; // don't want to write to debug.log file + fPrintToDebugLog = false; // don't want to write to debug.log file noui_connect(); +#ifdef ENABLE_WALLET bitdb.MakeMock(); +#endif pathTemp = GetTempPath() / strprintf("test_bitcoin_%lu_%i", (unsigned long)GetTime(), (int)(GetRand(100000))); boost::filesystem::create_directories(pathTemp); mapArgs["-datadir"] = pathTemp.string(); @@ -35,24 +38,32 @@ struct TestingSetup { pcoinsdbview = new CCoinsViewDB(1 << 23, true); pcoinsTip = new CCoinsViewCache(*pcoinsdbview); InitBlockIndex(); +#ifdef ENABLE_WALLET bool fFirstRun; pwalletMain = new CWallet("wallet.dat"); pwalletMain->LoadWallet(fFirstRun); RegisterWallet(pwalletMain); +#endif nScriptCheckThreads = 3; for (int i=0; i < nScriptCheckThreads-1; i++) threadGroup.create_thread(&ThreadScriptCheck); + RegisterNodeSignals(GetNodeSignals()); } ~TestingSetup() { threadGroup.interrupt_all(); threadGroup.join_all(); + UnregisterNodeSignals(GetNodeSignals()); +#ifdef ENABLE_WALLET delete pwalletMain; pwalletMain = NULL; +#endif delete pcoinsTip; delete pcoinsdbview; delete pblocktree; +#ifdef ENABLE_WALLET bitdb.Flush(true); +#endif boost::filesystem::remove_all(pathTemp); } }; diff --git a/src/test/uint160_tests.cpp b/src/test/uint160_tests.cpp deleted file mode 100644 index 87418fabec..0000000000 --- a/src/test/uint160_tests.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include "uint256.h" - -#include <stdint.h> - -#include <boost/test/unit_test.hpp> - -BOOST_AUTO_TEST_SUITE(uint160_tests) - -BOOST_AUTO_TEST_CASE(uint160_equality) -{ - uint160 num1 = 10; - uint160 num2 = 11; - BOOST_CHECK(num1+1 == num2); - - uint64_t num3 = 10; - BOOST_CHECK(num1 == num3); - BOOST_CHECK(num1+num2 == num3+num2); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/uint256_tests.cpp b/src/test/uint256_tests.cpp index 188635dcbd..368484fdff 100644 --- a/src/test/uint256_tests.cpp +++ b/src/test/uint256_tests.cpp @@ -1,29 +1,633 @@ +#include <boost/test/unit_test.hpp> +#include <stdint.h> +#include <sstream> +#include <iomanip> +#include <limits> +#include <cmath> #include "uint256.h" #include <string> +#include "version.h" -#include <stdint.h> +BOOST_AUTO_TEST_SUITE(uint256_tests) + +const unsigned char R1Array[] = + "\x9c\x52\x4a\xdb\xcf\x56\x11\x12\x2b\x29\x12\x5e\x5d\x35\xd2\xd2" + "\x22\x81\xaa\xb5\x33\xf0\x08\x32\xd5\x56\xb1\xf9\xea\xe5\x1d\x7d"; +const char R1ArrayHex[] = "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c"; +const double R1Ldouble = 0.4887374590559308955; // R1L equals roughly R1Ldouble * 2^256 +const double R1Sdouble = 0.7096329412477836074; +const uint256 R1L = uint256(std::vector<unsigned char>(R1Array,R1Array+32)); +const uint160 R1S = uint160(std::vector<unsigned char>(R1Array,R1Array+20)); +const uint64_t R1LLow64 = 0x121156cfdb4a529cULL; -#include <boost/test/unit_test.hpp> +const unsigned char R2Array[] = + "\x70\x32\x1d\x7c\x47\xa5\x6b\x40\x26\x7e\x0a\xc3\xa6\x9c\xb6\xbf" + "\x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7"; +const uint256 R2L = uint256(std::vector<unsigned char>(R2Array,R2Array+32)); +const uint160 R2S = uint160(std::vector<unsigned char>(R2Array,R2Array+20)); -BOOST_AUTO_TEST_SUITE(uint256_tests) +const char R1LplusR2L[] = "549FB09FEA236A1EA3E31D4D58F1B1369288D204211CA751527CFC175767850C"; + +const unsigned char ZeroArray[] = + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; +const uint256 ZeroL = uint256(std::vector<unsigned char>(ZeroArray,ZeroArray+32)); +const uint160 ZeroS = uint160(std::vector<unsigned char>(ZeroArray,ZeroArray+20)); + +const unsigned char OneArray[] = + "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; +const uint256 OneL = uint256(std::vector<unsigned char>(OneArray,OneArray+32)); +const uint160 OneS = uint160(std::vector<unsigned char>(OneArray,OneArray+20)); + +const unsigned char MaxArray[] = + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"; +const uint256 MaxL = uint256(std::vector<unsigned char>(MaxArray,MaxArray+32)); +const uint160 MaxS = uint160(std::vector<unsigned char>(MaxArray,MaxArray+20)); + +const uint256 HalfL = (OneL << 255); +const uint160 HalfS = (OneS << 159); +std::string ArrayToString(const unsigned char A[], unsigned int width) +{ + std::stringstream Stream; + Stream << std::hex; + for (unsigned int i = 0; i < width; ++i) + { + Stream<<std::setw(2)<<std::setfill('0')<<(unsigned int)A[width-i-1]; + } + return Stream.str(); +} -BOOST_AUTO_TEST_CASE(uint256_equality) +BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality { - uint256 num1 = 10; - uint256 num2 = 11; - BOOST_CHECK(num1+1 == num2); + BOOST_CHECK(1 == 0+1); + // constructor uint256(vector<char>): + BOOST_CHECK(R1L.ToString() == ArrayToString(R1Array,32)); + BOOST_CHECK(R1S.ToString() == ArrayToString(R1Array,20)); + BOOST_CHECK(R2L.ToString() == ArrayToString(R2Array,32)); + BOOST_CHECK(R2S.ToString() == ArrayToString(R2Array,20)); + BOOST_CHECK(ZeroL.ToString() == ArrayToString(ZeroArray,32)); + BOOST_CHECK(ZeroS.ToString() == ArrayToString(ZeroArray,20)); + BOOST_CHECK(OneL.ToString() == ArrayToString(OneArray,32)); + BOOST_CHECK(OneS.ToString() == ArrayToString(OneArray,20)); + BOOST_CHECK(MaxL.ToString() == ArrayToString(MaxArray,32)); + BOOST_CHECK(MaxS.ToString() == ArrayToString(MaxArray,20)); + BOOST_CHECK(OneL.ToString() != ArrayToString(ZeroArray,32)); + BOOST_CHECK(OneS.ToString() != ArrayToString(ZeroArray,20)); + + // == and != + BOOST_CHECK(R1L != R2L && R1S != R2S); + BOOST_CHECK(ZeroL != OneL && ZeroS != OneS); + BOOST_CHECK(OneL != ZeroL && OneS != ZeroS); + BOOST_CHECK(MaxL != ZeroL && MaxS != ZeroS); + BOOST_CHECK(~MaxL == ZeroL && ~MaxS == ZeroS); + BOOST_CHECK( ((R1L ^ R2L) ^ R1L) == R2L); + BOOST_CHECK( ((R1S ^ R2S) ^ R1S) == R2S); + + uint64_t Tmp64 = 0xc4dab720d9c7acaaULL; + for (unsigned int i = 0; i < 256; ++i) + { + BOOST_CHECK(ZeroL != (OneL << i)); + BOOST_CHECK((OneL << i) != ZeroL); + BOOST_CHECK(R1L != (R1L ^ (OneL << i))); + BOOST_CHECK(((uint256(Tmp64) ^ (OneL << i) ) != Tmp64 )); + } + BOOST_CHECK(ZeroL == (OneL << 256)); + + for (unsigned int i = 0; i < 160; ++i) + { + BOOST_CHECK(ZeroS != (OneS << i)); + BOOST_CHECK((OneS << i) != ZeroS); + BOOST_CHECK(R1S != (R1S ^ (OneS << i))); + BOOST_CHECK(((uint160(Tmp64) ^ (OneS << i) ) != Tmp64 )); + } + BOOST_CHECK(ZeroS == (OneS << 256)); + + // String Constructor and Copy Constructor + BOOST_CHECK(uint256("0x"+R1L.ToString()) == R1L); + BOOST_CHECK(uint256("0x"+R2L.ToString()) == R2L); + BOOST_CHECK(uint256("0x"+ZeroL.ToString()) == ZeroL); + BOOST_CHECK(uint256("0x"+OneL.ToString()) == OneL); + BOOST_CHECK(uint256("0x"+MaxL.ToString()) == MaxL); + BOOST_CHECK(uint256(R1L.ToString()) == R1L); + BOOST_CHECK(uint256(" 0x"+R1L.ToString()+" ") == R1L); + BOOST_CHECK(uint256("") == ZeroL); + BOOST_CHECK(R1L == uint256(R1ArrayHex)); + BOOST_CHECK(uint256(R1L) == R1L); + BOOST_CHECK((uint256(R1L^R2L)^R2L) == R1L); + BOOST_CHECK(uint256(ZeroL) == ZeroL); + BOOST_CHECK(uint256(OneL) == OneL); + + BOOST_CHECK(uint160("0x"+R1S.ToString()) == R1S); + BOOST_CHECK(uint160("0x"+R2S.ToString()) == R2S); + BOOST_CHECK(uint160("0x"+ZeroS.ToString()) == ZeroS); + BOOST_CHECK(uint160("0x"+OneS.ToString()) == OneS); + BOOST_CHECK(uint160("0x"+MaxS.ToString()) == MaxS); + BOOST_CHECK(uint160(R1S.ToString()) == R1S); + BOOST_CHECK(uint160(" 0x"+R1S.ToString()+" ") == R1S); + BOOST_CHECK(uint160("") == ZeroS); + BOOST_CHECK(R1S == uint160(R1ArrayHex)); + + BOOST_CHECK(uint160(R1S) == R1S); + BOOST_CHECK((uint160(R1S^R2S)^R2S) == R1S); + BOOST_CHECK(uint160(ZeroS) == ZeroS); + BOOST_CHECK(uint160(OneS) == OneS); + + // uint64_t constructor + BOOST_CHECK( (R1L & uint256("0xffffffffffffffff")) == uint256(R1LLow64)); + BOOST_CHECK(ZeroL == uint256(0)); + BOOST_CHECK(OneL == uint256(1)); + BOOST_CHECK(uint256("0xffffffffffffffff") = uint256(0xffffffffffffffffULL)); + BOOST_CHECK( (R1S & uint160("0xffffffffffffffff")) == uint160(R1LLow64)); + BOOST_CHECK(ZeroS == uint160(0)); + BOOST_CHECK(OneS == uint160(1)); + BOOST_CHECK(uint160("0xffffffffffffffff") = uint160(0xffffffffffffffffULL)); + + // Assignment (from base_uint) + uint256 tmpL = ~ZeroL; BOOST_CHECK(tmpL == ~ZeroL); + tmpL = ~OneL; BOOST_CHECK(tmpL == ~OneL); + tmpL = ~R1L; BOOST_CHECK(tmpL == ~R1L); + tmpL = ~R2L; BOOST_CHECK(tmpL == ~R2L); + tmpL = ~MaxL; BOOST_CHECK(tmpL == ~MaxL); + uint160 tmpS = ~ZeroS; BOOST_CHECK(tmpS == ~ZeroS); + tmpS = ~OneS; BOOST_CHECK(tmpS == ~OneS); + tmpS = ~R1S; BOOST_CHECK(tmpS == ~R1S); + tmpS = ~R2S; BOOST_CHECK(tmpS == ~R2S); + tmpS = ~MaxS; BOOST_CHECK(tmpS == ~MaxS); - uint64_t num3 = 10; - BOOST_CHECK(num1 == num3); - BOOST_CHECK(num1+num2 == num3+num2); + // Wrong length must give 0 + BOOST_CHECK(uint256(std::vector<unsigned char>(OneArray,OneArray+31)) == 0); + BOOST_CHECK(uint256(std::vector<unsigned char>(OneArray,OneArray+20)) == 0); + BOOST_CHECK(uint160(std::vector<unsigned char>(OneArray,OneArray+32)) == 0); + BOOST_CHECK(uint160(std::vector<unsigned char>(OneArray,OneArray+19)) == 0); } -BOOST_AUTO_TEST_CASE(uint256_hex) +void shiftArrayRight(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift) { - std::string hexStr = "d35583ed493a5eee756931353144f558e6a9ab3ad6024a63ced7f10daf7faad9"; - uint256 num1; - num1.SetHex(hexStr); - BOOST_CHECK(num1.GetHex() == hexStr); + for (unsigned int T=0; T < arrayLength; ++T) + { + unsigned int F = (T+bitsToShift/8); + if (F < arrayLength) + to[T] = from[F] >> (bitsToShift%8); + else + to[T] = 0; + if (F + 1 < arrayLength) + to[T] |= from[(F+1)] << (8-bitsToShift%8); + } +} + +void shiftArrayLeft(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift) +{ + for (unsigned int T=0; T < arrayLength; ++T) + { + if (T >= bitsToShift/8) + { + unsigned int F = T-bitsToShift/8; + to[T] = from[F] << (bitsToShift%8); + if (T >= bitsToShift/8+1) + to[T] |= from[F-1] >> (8-bitsToShift%8); + } + else { + to[T] = 0; + } + } +} + +BOOST_AUTO_TEST_CASE( shifts ) { // "<<" ">>" "<<=" ">>=" + unsigned char TmpArray[32]; + uint256 TmpL; + for (unsigned int i = 0; i < 256; ++i) + { + shiftArrayLeft(TmpArray, OneArray, 32, i); + BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (OneL << i)); + TmpL = OneL; TmpL <<= i; + BOOST_CHECK(TmpL == (OneL << i)); + BOOST_CHECK((HalfL >> (255-i)) == (OneL << i)); + TmpL = HalfL; TmpL >>= (255-i); + BOOST_CHECK(TmpL == (OneL << i)); + + shiftArrayLeft(TmpArray, R1Array, 32, i); + BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L << i)); + TmpL = R1L; TmpL <<= i; + BOOST_CHECK(TmpL == (R1L << i)); + + shiftArrayRight(TmpArray, R1Array, 32, i); + BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L >> i)); + TmpL = R1L; TmpL >>= i; + BOOST_CHECK(TmpL == (R1L >> i)); + + shiftArrayLeft(TmpArray, MaxArray, 32, i); + BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL << i)); + TmpL = MaxL; TmpL <<= i; + BOOST_CHECK(TmpL == (MaxL << i)); + + shiftArrayRight(TmpArray, MaxArray, 32, i); + BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL >> i)); + TmpL = MaxL; TmpL >>= i; + BOOST_CHECK(TmpL == (MaxL >> i)); + } + uint256 c1L = uint256(0x0123456789abcdefULL); + uint256 c2L = c1L << 128; + for (unsigned int i = 0; i < 128; ++i) { + BOOST_CHECK((c1L << i) == (c2L >> (128-i))); + } + for (unsigned int i = 128; i < 256; ++i) { + BOOST_CHECK((c1L << i) == (c2L << (i-128))); + } + + uint160 TmpS; + for (unsigned int i = 0; i < 160; ++i) + { + shiftArrayLeft(TmpArray, OneArray, 20, i); + BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (OneS << i)); + TmpS = OneS; TmpS <<= i; + BOOST_CHECK(TmpS == (OneS << i)); + BOOST_CHECK((HalfS >> (159-i)) == (OneS << i)); + TmpS = HalfS; TmpS >>= (159-i); + BOOST_CHECK(TmpS == (OneS << i)); + + shiftArrayLeft(TmpArray, R1Array, 20, i); + BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (R1S << i)); + TmpS = R1S; TmpS <<= i; + BOOST_CHECK(TmpS == (R1S << i)); + + shiftArrayRight(TmpArray, R1Array, 20, i); + BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (R1S >> i)); + TmpS = R1S; TmpS >>= i; + BOOST_CHECK(TmpS == (R1S >> i)); + + shiftArrayLeft(TmpArray, MaxArray, 20, i); + BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (MaxS << i)); + TmpS = MaxS; TmpS <<= i; + BOOST_CHECK(TmpS == (MaxS << i)); + + shiftArrayRight(TmpArray, MaxArray, 20, i); + BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (MaxS >> i)); + TmpS = MaxS; TmpS >>= i; + BOOST_CHECK(TmpS == (MaxS >> i)); + } + uint160 c1S = uint160(0x0123456789abcdefULL); + uint160 c2S = c1S << 80; + for (unsigned int i = 0; i < 80; ++i) { + BOOST_CHECK((c1S << i) == (c2S >> (80-i))); + } + for (unsigned int i = 80; i < 160; ++i) { + BOOST_CHECK((c1S << i) == (c2S << (i-80))); + } +} + +BOOST_AUTO_TEST_CASE( unaryOperators ) // ! ~ - +{ + BOOST_CHECK(!ZeroL); BOOST_CHECK(!ZeroS); + BOOST_CHECK(!(!OneL));BOOST_CHECK(!(!OneS)); + for (unsigned int i = 0; i < 256; ++i) + BOOST_CHECK(!(!(OneL<<i))); + for (unsigned int i = 0; i < 160; ++i) + BOOST_CHECK(!(!(OneS<<i))); + BOOST_CHECK(!(!R1L));BOOST_CHECK(!(!R1S)); + BOOST_CHECK(!(!R2S));BOOST_CHECK(!(!R2S)); + BOOST_CHECK(!(!MaxL));BOOST_CHECK(!(!MaxS)); + + BOOST_CHECK(~ZeroL == MaxL); BOOST_CHECK(~ZeroS == MaxS); + + unsigned char TmpArray[32]; + for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = ~R1Array[i]; } + BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (~R1L)); + BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (~R1S)); + + BOOST_CHECK(-ZeroL == ZeroL); BOOST_CHECK(-ZeroS == ZeroS); + BOOST_CHECK(-R1L == (~R1L)+1); + BOOST_CHECK(-R1S == (~R1S)+1); + for (unsigned int i = 0; i < 256; ++i) + BOOST_CHECK(-(OneL<<i) == (MaxL << i)); + for (unsigned int i = 0; i < 160; ++i) + BOOST_CHECK(-(OneS<<i) == (MaxS << i)); +} + + +// Check if doing _A_ _OP_ _B_ results in the same as applying _OP_ onto each +// element of Aarray and Barray, and then converting the result into a uint256. +#define CHECKBITWISEOPERATOR(_A_,_B_,_OP_) \ + for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = _A_##Array[i] _OP_ _B_##Array[i]; } \ + BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (_A_##L _OP_ _B_##L)); \ + for (unsigned int i = 0; i < 20; ++i) { TmpArray[i] = _A_##Array[i] _OP_ _B_##Array[i]; } \ + BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (_A_##S _OP_ _B_##S)); + +#define CHECKASSIGNMENTOPERATOR(_A_,_B_,_OP_) \ + TmpL = _A_##L; TmpL _OP_##= _B_##L; BOOST_CHECK(TmpL == (_A_##L _OP_ _B_##L)); \ + TmpS = _A_##S; TmpS _OP_##= _B_##S; BOOST_CHECK(TmpS == (_A_##S _OP_ _B_##S)); + +BOOST_AUTO_TEST_CASE( bitwiseOperators ) +{ + unsigned char TmpArray[32]; + + CHECKBITWISEOPERATOR(R1,R2,|) + CHECKBITWISEOPERATOR(R1,R2,^) + CHECKBITWISEOPERATOR(R1,R2,&) + CHECKBITWISEOPERATOR(R1,Zero,|) + CHECKBITWISEOPERATOR(R1,Zero,^) + CHECKBITWISEOPERATOR(R1,Zero,&) + CHECKBITWISEOPERATOR(R1,Max,|) + CHECKBITWISEOPERATOR(R1,Max,^) + CHECKBITWISEOPERATOR(R1,Max,&) + CHECKBITWISEOPERATOR(Zero,R1,|) + CHECKBITWISEOPERATOR(Zero,R1,^) + CHECKBITWISEOPERATOR(Zero,R1,&) + CHECKBITWISEOPERATOR(Max,R1,|) + CHECKBITWISEOPERATOR(Max,R1,^) + CHECKBITWISEOPERATOR(Max,R1,&) + + uint256 TmpL; + uint160 TmpS; + CHECKASSIGNMENTOPERATOR(R1,R2,|) + CHECKASSIGNMENTOPERATOR(R1,R2,^) + CHECKASSIGNMENTOPERATOR(R1,R2,&) + CHECKASSIGNMENTOPERATOR(R1,Zero,|) + CHECKASSIGNMENTOPERATOR(R1,Zero,^) + CHECKASSIGNMENTOPERATOR(R1,Zero,&) + CHECKASSIGNMENTOPERATOR(R1,Max,|) + CHECKASSIGNMENTOPERATOR(R1,Max,^) + CHECKASSIGNMENTOPERATOR(R1,Max,&) + CHECKASSIGNMENTOPERATOR(Zero,R1,|) + CHECKASSIGNMENTOPERATOR(Zero,R1,^) + CHECKASSIGNMENTOPERATOR(Zero,R1,&) + CHECKASSIGNMENTOPERATOR(Max,R1,|) + CHECKASSIGNMENTOPERATOR(Max,R1,^) + CHECKASSIGNMENTOPERATOR(Max,R1,&) + + uint64_t Tmp64 = 0xe1db685c9a0b47a2ULL; + TmpL = R1L; TmpL |= Tmp64; BOOST_CHECK(TmpL == (R1L | uint256(Tmp64))); + TmpS = R1S; TmpS |= Tmp64; BOOST_CHECK(TmpS == (R1S | uint160(Tmp64))); + TmpL = R1L; TmpL |= 0; BOOST_CHECK(TmpL == R1L); + TmpS = R1S; TmpS |= 0; BOOST_CHECK(TmpS == R1S); + TmpL ^= 0; BOOST_CHECK(TmpL == R1L); + TmpS ^= 0; BOOST_CHECK(TmpS == R1S); + TmpL ^= Tmp64; BOOST_CHECK(TmpL == (R1L ^ uint256(Tmp64))); + TmpS ^= Tmp64; BOOST_CHECK(TmpS == (R1S ^ uint160(Tmp64))); +} + +BOOST_AUTO_TEST_CASE( comparison ) // <= >= < > +{ + uint256 TmpL; + for (unsigned int i = 0; i < 256; ++i) { + TmpL= OneL<< i; + BOOST_CHECK( TmpL >= ZeroL && TmpL > ZeroL && ZeroL < TmpL && ZeroL <= TmpL); + BOOST_CHECK( TmpL >= 0 && TmpL > 0 && 0 < TmpL && 0 <= TmpL); + TmpL |= R1L; + BOOST_CHECK( TmpL >= R1L ); BOOST_CHECK( (TmpL == R1L) != (TmpL > R1L)); BOOST_CHECK( (TmpL == R1L) || !( TmpL <= R1L)); + BOOST_CHECK( R1L <= TmpL ); BOOST_CHECK( (R1L == TmpL) != (R1L < TmpL)); BOOST_CHECK( (TmpL == R1L) || !( R1L >= TmpL)); + BOOST_CHECK(! (TmpL < R1L)); BOOST_CHECK(! (R1L > TmpL)); + } + uint160 TmpS; + for (unsigned int i = 0; i < 160; ++i) { + TmpS= OneS<< i; + BOOST_CHECK( TmpS >= ZeroS && TmpS > ZeroS && ZeroS < TmpS && ZeroS <= TmpS); + BOOST_CHECK( TmpS >= 0 && TmpS > 0 && 0 < TmpS && 0 <= TmpS); + TmpS |= R1S; + BOOST_CHECK( TmpS >= R1S ); BOOST_CHECK( (TmpS == R1S) != (TmpS > R1S)); BOOST_CHECK( (TmpS == R1S) || !( TmpS <= R1S)); + BOOST_CHECK( R1S <= TmpS ); BOOST_CHECK( (R1S == TmpS) != (R1S < TmpS)); BOOST_CHECK( (TmpS == R1S) || !( R1S >= TmpS)); + BOOST_CHECK(! (TmpS < R1S)); BOOST_CHECK(! (R1S > TmpS)); + } +} + +BOOST_AUTO_TEST_CASE( plusMinus ) +{ + uint256 TmpL = 0; + BOOST_CHECK(R1L+R2L == uint256(R1LplusR2L)); + TmpL += R1L; + BOOST_CHECK(TmpL == R1L); + TmpL += R2L; + BOOST_CHECK(TmpL == R1L + R2L); + BOOST_CHECK(OneL+MaxL == ZeroL); + BOOST_CHECK(MaxL+OneL == ZeroL); + for (unsigned int i = 1; i < 256; ++i) { + BOOST_CHECK( (MaxL >> i) + OneL == (HalfL >> (i-1)) ); + BOOST_CHECK( OneL + (MaxL >> i) == (HalfL >> (i-1)) ); + TmpL = (MaxL>>i); TmpL += OneL; + BOOST_CHECK( TmpL == (HalfL >> (i-1)) ); + TmpL = (MaxL>>i); TmpL += 1; + BOOST_CHECK( TmpL == (HalfL >> (i-1)) ); + TmpL = (MaxL>>i); + BOOST_CHECK( TmpL++ == (MaxL>>i) ); + BOOST_CHECK( TmpL == (HalfL >> (i-1))); + } + BOOST_CHECK(uint256(0xbedc77e27940a7ULL) + 0xee8d836fce66fbULL == uint256(0xbedc77e27940a7ULL + 0xee8d836fce66fbULL)); + TmpL = uint256(0xbedc77e27940a7ULL); TmpL += 0xee8d836fce66fbULL; + BOOST_CHECK(TmpL == uint256(0xbedc77e27940a7ULL+0xee8d836fce66fbULL)); + TmpL -= 0xee8d836fce66fbULL; BOOST_CHECK(TmpL == 0xbedc77e27940a7ULL); + TmpL = R1L; + BOOST_CHECK(++TmpL == R1L+1); + + BOOST_CHECK(R1L -(-R2L) == R1L+R2L); + BOOST_CHECK(R1L -(-OneL) == R1L+OneL); + BOOST_CHECK(R1L - OneL == R1L+(-OneL)); + for (unsigned int i = 1; i < 256; ++i) { + BOOST_CHECK((MaxL>>i) - (-OneL) == (HalfL >> (i-1))); + BOOST_CHECK((HalfL >> (i-1)) - OneL == (MaxL>>i)); + TmpL = (HalfL >> (i-1)); + BOOST_CHECK(TmpL-- == (HalfL >> (i-1))); + BOOST_CHECK(TmpL == (MaxL >> i)); + TmpL = (HalfL >> (i-1)); + BOOST_CHECK(--TmpL == (MaxL >> i)); + } + TmpL = R1L; + BOOST_CHECK(--TmpL == R1L-1); + + // 160-bit; copy-pasted + uint160 TmpS = 0; + BOOST_CHECK(R1S+R2S == uint160(R1LplusR2L)); + TmpS += R1S; + BOOST_CHECK(TmpS == R1S); + TmpS += R2S; + BOOST_CHECK(TmpS == R1S + R2S); + BOOST_CHECK(OneS+MaxS == ZeroS); + BOOST_CHECK(MaxS+OneS == ZeroS); + for (unsigned int i = 1; i < 160; ++i) { + BOOST_CHECK( (MaxS >> i) + OneS == (HalfS >> (i-1)) ); + BOOST_CHECK( OneS + (MaxS >> i) == (HalfS >> (i-1)) ); + TmpS = (MaxS>>i); TmpS += OneS; + BOOST_CHECK( TmpS == (HalfS >> (i-1)) ); + TmpS = (MaxS>>i); TmpS += 1; + BOOST_CHECK( TmpS == (HalfS >> (i-1)) ); + TmpS = (MaxS>>i); + BOOST_CHECK( TmpS++ == (MaxS>>i) ); + BOOST_CHECK( TmpS == (HalfS >> (i-1))); + } + BOOST_CHECK(uint160(0xbedc77e27940a7ULL) + 0xee8d836fce66fbULL == uint160(0xbedc77e27940a7ULL + 0xee8d836fce66fbULL)); + TmpS = uint160(0xbedc77e27940a7ULL); TmpS += 0xee8d836fce66fbULL; + BOOST_CHECK(TmpS == uint160(0xbedc77e27940a7ULL+0xee8d836fce66fbULL)); + TmpS -= 0xee8d836fce66fbULL; BOOST_CHECK(TmpS == 0xbedc77e27940a7ULL); + TmpS = R1S; + BOOST_CHECK(++TmpS == R1S+1); + + BOOST_CHECK(R1S -(-R2S) == R1S+R2S); + BOOST_CHECK(R1S -(-OneS) == R1S+OneS); + BOOST_CHECK(R1S - OneS == R1S+(-OneS)); + for (unsigned int i = 1; i < 160; ++i) { + BOOST_CHECK((MaxS>>i) - (-OneS) == (HalfS >> (i-1))); + BOOST_CHECK((HalfS >> (i-1)) - OneS == (MaxS>>i)); + TmpS = (HalfS >> (i-1)); + BOOST_CHECK(TmpS-- == (HalfS >> (i-1))); + BOOST_CHECK(TmpS == (MaxS >> i)); + TmpS = (HalfS >> (i-1)); + BOOST_CHECK(--TmpS == (MaxS >> i)); + } + TmpS = R1S; + BOOST_CHECK(--TmpS == R1S-1); + +} + +bool almostEqual(double d1, double d2) +{ + return fabs(d1-d2) <= 4*fabs(d1)*std::numeric_limits<double>::epsilon(); +} + +BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 GetSerializeSize, Serialize, Unserialize +{ + BOOST_CHECK(R1L.GetHex() == R1L.ToString()); + BOOST_CHECK(R2L.GetHex() == R2L.ToString()); + BOOST_CHECK(OneL.GetHex() == OneL.ToString()); + BOOST_CHECK(MaxL.GetHex() == MaxL.ToString()); + uint256 TmpL(R1L); + BOOST_CHECK(TmpL == R1L); + TmpL.SetHex(R2L.ToString()); BOOST_CHECK(TmpL == R2L); + TmpL.SetHex(ZeroL.ToString()); BOOST_CHECK(TmpL == 0); + TmpL.SetHex(HalfL.ToString()); BOOST_CHECK(TmpL == HalfL); + + TmpL.SetHex(R1L.ToString()); + BOOST_CHECK(memcmp(R1L.begin(), R1Array, 32)==0); + BOOST_CHECK(memcmp(TmpL.begin(), R1Array, 32)==0); + BOOST_CHECK(memcmp(R2L.begin(), R2Array, 32)==0); + BOOST_CHECK(memcmp(ZeroL.begin(), ZeroArray, 32)==0); + BOOST_CHECK(memcmp(OneL.begin(), OneArray, 32)==0); + BOOST_CHECK(R1L.size() == 32); + BOOST_CHECK(R2L.size() == 32); + BOOST_CHECK(ZeroL.size() == 32); + BOOST_CHECK(MaxL.size() == 32); + BOOST_CHECK(R1L.begin() + 32 == R1L.end()); + BOOST_CHECK(R2L.begin() + 32 == R2L.end()); + BOOST_CHECK(OneL.begin() + 32 == OneL.end()); + BOOST_CHECK(MaxL.begin() + 32 == MaxL.end()); + BOOST_CHECK(TmpL.begin() + 32 == TmpL.end()); + BOOST_CHECK(R1L.GetLow64() == R1LLow64); + BOOST_CHECK(HalfL.GetLow64() ==0x0000000000000000ULL); + BOOST_CHECK(OneL.GetLow64() ==0x0000000000000001ULL); + BOOST_CHECK(R1L.GetSerializeSize(0,PROTOCOL_VERSION) == 32); + BOOST_CHECK(ZeroL.GetSerializeSize(0,PROTOCOL_VERSION) == 32); + + std::stringstream ss; + R1L.Serialize(ss,0,PROTOCOL_VERSION); + BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+32)); + TmpL.Unserialize(ss,0,PROTOCOL_VERSION); + BOOST_CHECK(R1L == TmpL); + ss.str(""); + ZeroL.Serialize(ss,0,PROTOCOL_VERSION); + BOOST_CHECK(ss.str() == std::string(ZeroArray,ZeroArray+32)); + TmpL.Unserialize(ss,0,PROTOCOL_VERSION); + BOOST_CHECK(ZeroL == TmpL); + ss.str(""); + MaxL.Serialize(ss,0,PROTOCOL_VERSION); + BOOST_CHECK(ss.str() == std::string(MaxArray,MaxArray+32)); + TmpL.Unserialize(ss,0,PROTOCOL_VERSION); + BOOST_CHECK(MaxL == TmpL); + ss.str(""); + + BOOST_CHECK(R1S.GetHex() == R1S.ToString()); + BOOST_CHECK(R2S.GetHex() == R2S.ToString()); + BOOST_CHECK(OneS.GetHex() == OneS.ToString()); + BOOST_CHECK(MaxS.GetHex() == MaxS.ToString()); + uint160 TmpS(R1S); + BOOST_CHECK(TmpS == R1S); + TmpS.SetHex(R2S.ToString()); BOOST_CHECK(TmpS == R2S); + TmpS.SetHex(ZeroS.ToString()); BOOST_CHECK(TmpS == 0); + TmpS.SetHex(HalfS.ToString()); BOOST_CHECK(TmpS == HalfS); + + TmpS.SetHex(R1S.ToString()); + BOOST_CHECK(memcmp(R1S.begin(), R1Array, 20)==0); + BOOST_CHECK(memcmp(TmpS.begin(), R1Array, 20)==0); + BOOST_CHECK(memcmp(R2S.begin(), R2Array, 20)==0); + BOOST_CHECK(memcmp(ZeroS.begin(), ZeroArray, 20)==0); + BOOST_CHECK(memcmp(OneS.begin(), OneArray, 20)==0); + BOOST_CHECK(R1S.size() == 20); + BOOST_CHECK(R2S.size() == 20); + BOOST_CHECK(ZeroS.size() == 20); + BOOST_CHECK(MaxS.size() == 20); + BOOST_CHECK(R1S.begin() + 20 == R1S.end()); + BOOST_CHECK(R2S.begin() + 20 == R2S.end()); + BOOST_CHECK(OneS.begin() + 20 == OneS.end()); + BOOST_CHECK(MaxS.begin() + 20 == MaxS.end()); + BOOST_CHECK(TmpS.begin() + 20 == TmpS.end()); + BOOST_CHECK(R1S.GetLow64() == R1LLow64); + BOOST_CHECK(HalfS.GetLow64() ==0x0000000000000000ULL); + BOOST_CHECK(OneS.GetLow64() ==0x0000000000000001ULL); + BOOST_CHECK(R1S.GetSerializeSize(0,PROTOCOL_VERSION) == 20); + BOOST_CHECK(ZeroS.GetSerializeSize(0,PROTOCOL_VERSION) == 20); + + R1S.Serialize(ss,0,PROTOCOL_VERSION); + BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+20)); + TmpS.Unserialize(ss,0,PROTOCOL_VERSION); + BOOST_CHECK(R1S == TmpS); + ss.str(""); + ZeroS.Serialize(ss,0,PROTOCOL_VERSION); + BOOST_CHECK(ss.str() == std::string(ZeroArray,ZeroArray+20)); + TmpS.Unserialize(ss,0,PROTOCOL_VERSION); + BOOST_CHECK(ZeroS == TmpS); + ss.str(""); + MaxS.Serialize(ss,0,PROTOCOL_VERSION); + BOOST_CHECK(ss.str() == std::string(MaxArray,MaxArray+20)); + TmpS.Unserialize(ss,0,PROTOCOL_VERSION); + BOOST_CHECK(MaxS == TmpS); + ss.str(""); + + for (unsigned int i = 0; i < 255; ++i) + { + BOOST_CHECK((OneL << i).getdouble() == ldexp(1.0,i)); + if (i < 160) BOOST_CHECK((OneS << i).getdouble() == ldexp(1.0,i)); + } + BOOST_CHECK(ZeroL.getdouble() == 0.0); + BOOST_CHECK(ZeroS.getdouble() == 0.0); + for (int i = 256; i > 53; --i) + BOOST_CHECK(almostEqual((R1L>>(256-i)).getdouble(), ldexp(R1Ldouble,i))); + for (int i = 160; i > 53; --i) + BOOST_CHECK(almostEqual((R1S>>(160-i)).getdouble(), ldexp(R1Sdouble,i))); + uint64_t R1L64part = (R1L>>192).GetLow64(); + uint64_t R1S64part = (R1S>>96).GetLow64(); + for (int i = 53; i > 0; --i) // doubles can store all integers in {0,...,2^54-1} exactly + { + BOOST_CHECK((R1L>>(256-i)).getdouble() == (double)(R1L64part >> (64-i))); + BOOST_CHECK((R1S>>(160-i)).getdouble() == (double)(R1S64part >> (64-i))); + } +} + +BOOST_AUTO_TEST_CASE( getmaxcoverage ) // some more tests just to get 100% coverage +{ + // ~R1L give a base_uint<256> + BOOST_CHECK((~~R1L >> 10) == (R1L >> 10)); BOOST_CHECK((~~R1S >> 10) == (R1S >> 10)); + BOOST_CHECK((~~R1L << 10) == (R1L << 10)); BOOST_CHECK((~~R1S << 10) == (R1S << 10)); + BOOST_CHECK(!(~~R1L < R1L)); BOOST_CHECK(!(~~R1S < R1S)); + BOOST_CHECK(~~R1L <= R1L); BOOST_CHECK(~~R1S <= R1S); + BOOST_CHECK(!(~~R1L > R1L)); BOOST_CHECK(!(~~R1S > R1S)); + BOOST_CHECK(~~R1L >= R1L); BOOST_CHECK(~~R1S >= R1S); + BOOST_CHECK(!(R1L < ~~R1L)); BOOST_CHECK(!(R1S < ~~R1S)); + BOOST_CHECK(R1L <= ~~R1L); BOOST_CHECK(R1S <= ~~R1S); + BOOST_CHECK(!(R1L > ~~R1L)); BOOST_CHECK(!(R1S > ~~R1S)); + BOOST_CHECK(R1L >= ~~R1L); BOOST_CHECK(R1S >= ~~R1S); + + BOOST_CHECK(~~R1L + R2L == R1L + ~~R2L); + BOOST_CHECK(~~R1S + R2S == R1S + ~~R2S); + BOOST_CHECK(~~R1L - R2L == R1L - ~~R2L); + BOOST_CHECK(~~R1S - R2S == R1S - ~~R2S); + BOOST_CHECK(~R1L != R1L); BOOST_CHECK(R1L != ~R1L); + BOOST_CHECK(~R1S != R1S); BOOST_CHECK(R1S != ~R1S); + unsigned char TmpArray[32]; + CHECKBITWISEOPERATOR(~R1,R2,|) + CHECKBITWISEOPERATOR(~R1,R2,^) + CHECKBITWISEOPERATOR(~R1,R2,&) + CHECKBITWISEOPERATOR(R1,~R2,|) + CHECKBITWISEOPERATOR(R1,~R2,^) + CHECKBITWISEOPERATOR(R1,~R2,&) } BOOST_AUTO_TEST_SUITE_END() + diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index b57945d061..691f02a9d7 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -330,4 +330,12 @@ BOOST_AUTO_TEST_CASE(strprintf_numbers) #undef B #undef E +/* Check for mingw/wine issue #3494 + * Remove this test before time.ctime(0xffffffff) == 'Sun Feb 7 07:28:15 2106' + */ +BOOST_AUTO_TEST_CASE(gettime) +{ + BOOST_CHECK((GetTime() & ~0xFFFFFFFFLL) == 0); +} + BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/wallet_tests.cpp b/src/test/wallet_tests.cpp index 0acd94ef36..bd0517ae08 100644 --- a/src/test/wallet_tests.cpp +++ b/src/test/wallet_tests.cpp @@ -62,6 +62,8 @@ BOOST_AUTO_TEST_CASE(coin_selection_tests) CoinSet setCoinsRet, setCoinsRet2; int64_t nValueRet; + LOCK(wallet.cs_wallet); + // test multiple times to allow for differences in the shuffle order for (int i = 0; i < RUN_TESTS; i++) { |