aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'src/test')
-rw-r--r--src/test/Checkpoints_tests.cpp34
-rw-r--r--src/test/DoS_tests.cpp119
-rw-r--r--src/test/base58_tests.cpp89
-rw-r--r--src/test/base64_tests.cpp22
-rw-r--r--src/test/miner_tests.cpp35
-rw-r--r--src/test/multisig_tests.cpp294
-rw-r--r--src/test/script_op_eval_tests.cpp234
-rw-r--r--src/test/script_tests.cpp46
-rw-r--r--src/test/test_bitcoin.cpp18
-rw-r--r--src/test/transaction_tests.cpp4
-rw-r--r--src/test/uint160_tests.cpp2
-rw-r--r--src/test/uint256_tests.cpp2
-rw-r--r--src/test/util_tests.cpp235
13 files changed, 1102 insertions, 32 deletions
diff --git a/src/test/Checkpoints_tests.cpp b/src/test/Checkpoints_tests.cpp
new file mode 100644
index 0000000000..0d8a366d7a
--- /dev/null
+++ b/src/test/Checkpoints_tests.cpp
@@ -0,0 +1,34 @@
+//
+// Unit tests for block-chain checkpoints
+//
+#include <boost/assign/list_of.hpp> // for 'map_list_of()'
+#include <boost/test/unit_test.hpp>
+#include <boost/foreach.hpp>
+
+#include "../checkpoints.h"
+#include "../util.h"
+
+using namespace std;
+
+BOOST_AUTO_TEST_SUITE(Checkpoints_tests)
+
+BOOST_AUTO_TEST_CASE(sanity)
+{
+ uint256 p11111 = uint256("0x0000000069e244f73d78e8fd29ba2fd2ed618bd6fa2ee92559f542fdb26e7c1d");
+ uint256 p140700 = uint256("0x000000000000033b512028abb90e1626d8b346fd0ed598ac0a3c371138dce2bd");
+ BOOST_CHECK(Checkpoints::CheckBlock(11111, p11111));
+ BOOST_CHECK(Checkpoints::CheckBlock(140700, p140700));
+
+
+ // Wrong hashes at checkpoints should fail:
+ BOOST_CHECK(!Checkpoints::CheckBlock(11111, p140700));
+ BOOST_CHECK(!Checkpoints::CheckBlock(140700, p11111));
+
+ // ... but any hash not at a checkpoint should succeed:
+ BOOST_CHECK(Checkpoints::CheckBlock(11111+1, p140700));
+ BOOST_CHECK(Checkpoints::CheckBlock(140700+1, p11111));
+
+ BOOST_CHECK(Checkpoints::GetTotalBlocksEstimate() >= 140700);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/test/DoS_tests.cpp b/src/test/DoS_tests.cpp
new file mode 100644
index 0000000000..e9b7b4517a
--- /dev/null
+++ b/src/test/DoS_tests.cpp
@@ -0,0 +1,119 @@
+//
+// Unit tests for denial-of-service detection/prevention code
+//
+#include <boost/assign/list_of.hpp> // for 'map_list_of()'
+#include <boost/test/unit_test.hpp>
+#include <boost/foreach.hpp>
+
+#include "main.h"
+#include "wallet.h"
+#include "net.h"
+#include "util.h"
+
+using namespace std;
+
+BOOST_AUTO_TEST_SUITE(DoS_tests)
+
+BOOST_AUTO_TEST_CASE(DoS_banning)
+{
+ CNode::ClearBanned();
+ CAddress addr1(0xa0b0c001);
+ CNode dummyNode1(INVALID_SOCKET, addr1, true);
+ dummyNode1.Misbehaving(100); // Should get banned
+ BOOST_CHECK(CNode::IsBanned(addr1.ip));
+ BOOST_CHECK(!CNode::IsBanned(addr1.ip|0x0000ff00)); // Different ip, not banned
+
+ CAddress addr2(0xa0b0c002);
+ CNode dummyNode2(INVALID_SOCKET, addr2, true);
+ dummyNode2.Misbehaving(50);
+ BOOST_CHECK(!CNode::IsBanned(addr2.ip)); // 2 not banned yet...
+ BOOST_CHECK(CNode::IsBanned(addr1.ip)); // ... but 1 still should be
+ dummyNode2.Misbehaving(50);
+ BOOST_CHECK(CNode::IsBanned(addr2.ip));
+}
+
+BOOST_AUTO_TEST_CASE(DoS_banscore)
+{
+ CNode::ClearBanned();
+ mapArgs["-banscore"] = "111"; // because 11 is my favorite number
+ CAddress addr1(0xa0b0c001);
+ CNode dummyNode1(INVALID_SOCKET, addr1, true);
+ dummyNode1.Misbehaving(100);
+ BOOST_CHECK(!CNode::IsBanned(addr1.ip));
+ dummyNode1.Misbehaving(10);
+ BOOST_CHECK(!CNode::IsBanned(addr1.ip));
+ dummyNode1.Misbehaving(1);
+ BOOST_CHECK(CNode::IsBanned(addr1.ip));
+ mapArgs["-banscore"] = "100";
+}
+
+BOOST_AUTO_TEST_CASE(DoS_bantime)
+{
+ CNode::ClearBanned();
+ int64 nStartTime = GetTime();
+ SetMockTime(nStartTime); // Overrides future calls to GetTime()
+
+ CAddress addr(0xa0b0c001);
+ CNode dummyNode(INVALID_SOCKET, addr, true);
+
+ dummyNode.Misbehaving(100);
+ BOOST_CHECK(CNode::IsBanned(addr.ip));
+
+ SetMockTime(nStartTime+60*60);
+ BOOST_CHECK(CNode::IsBanned(addr.ip));
+
+ SetMockTime(nStartTime+60*60*24+1);
+ BOOST_CHECK(!CNode::IsBanned(addr.ip));
+}
+
+static bool CheckNBits(unsigned int nbits1, int64 time1, unsigned int nbits2, int64 time2)
+{
+ if (time1 > time2)
+ return CheckNBits(nbits2, time2, nbits1, time1);
+ int64 deltaTime = time2-time1;
+
+ CBigNum required;
+ required.SetCompact(ComputeMinWork(nbits1, deltaTime));
+ CBigNum have;
+ have.SetCompact(nbits2);
+ return (have <= required);
+}
+
+BOOST_AUTO_TEST_CASE(DoS_checknbits)
+{
+ using namespace boost::assign; // for 'map_list_of()'
+
+ // Timestamps,nBits from the bitcoin blockchain.
+ // These are the block-chain checkpoint blocks
+ typedef std::map<int64, unsigned int> BlockData;
+ BlockData chainData =
+ map_list_of(1239852051,486604799)(1262749024,486594666)
+ (1279305360,469854461)(1280200847,469830746)(1281678674,469809688)
+ (1296207707,453179945)(1302624061,453036989)(1309640330,437004818)
+ (1313172719,436789733);
+
+ // Make sure CheckNBits considers every combination of block-chain-lock-in-points
+ // "sane":
+ BOOST_FOREACH(const BlockData::value_type& i, chainData)
+ {
+ BOOST_FOREACH(const BlockData::value_type& j, chainData)
+ {
+ BOOST_CHECK(CheckNBits(i.second, i.first, j.second, j.first));
+ }
+ }
+
+ // Test a couple of insane combinations:
+ BlockData::value_type firstcheck = *(chainData.begin());
+ BlockData::value_type lastcheck = *(chainData.rbegin());
+
+ // First checkpoint difficulty at or a while after the last checkpoint time should fail when
+ // compared to last checkpoint
+ BOOST_CHECK(!CheckNBits(firstcheck.second, lastcheck.first+60*10, lastcheck.second, lastcheck.first));
+ BOOST_CHECK(!CheckNBits(firstcheck.second, lastcheck.first+60*60*24*14, lastcheck.second, lastcheck.first));
+
+ // ... but OK if enough time passed for difficulty to adjust downward:
+ BOOST_CHECK(CheckNBits(firstcheck.second, lastcheck.first+60*60*24*365*4, lastcheck.second, lastcheck.first));
+
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/test/base58_tests.cpp b/src/test/base58_tests.cpp
new file mode 100644
index 0000000000..d52ac74982
--- /dev/null
+++ b/src/test/base58_tests.cpp
@@ -0,0 +1,89 @@
+#include <boost/test/unit_test.hpp>
+
+#include "main.h"
+#include "wallet.h"
+#include "util.h"
+
+BOOST_AUTO_TEST_SUITE(base58_tests)
+
+// TODO:
+// EncodeBase58Check
+// DecodeBase58Check
+// CBase58Data
+// bool SetString(const char* psz)
+ // bool SetString(const std::string& str)
+ // std::string ToString() const
+ // int CompareTo(const CBase58Data& b58) const
+ // bool operator==(const CBase58Data& b58) const
+ // bool operator<=(const CBase58Data& b58) const
+ // bool operator>=(const CBase58Data& b58) const
+ // bool operator< (const CBase58Data& b58) const
+ // bool operator> (const CBase58Data& b58) const
+
+// CBitcoinAddress
+ // bool SetHash160(const uint160& hash160)
+ // bool SetPubKey(const std::vector<unsigned char>& vchPubKey)
+ // bool IsValid() const
+ // CBitcoinAddress()
+ // CBitcoinAddress(uint160 hash160In)
+ // CBitcoinAddress(const std::vector<unsigned char>& vchPubKey)
+ // CBitcoinAddress(const std::string& strAddress)
+ // CBitcoinAddress(const char* pszAddress)
+ // uint160 GetHash160() const
+
+#define U(x) (reinterpret_cast<const unsigned char*>(x))
+static struct {
+ const unsigned char *data;
+ int size;
+} vstrIn[] = {
+{U(""), 0},
+{U("\x61"), 1},
+{U("\x62\x62\x62"), 3},
+{U("\x63\x63\x63"), 3},
+{U("\x73\x69\x6d\x70\x6c\x79\x20\x61\x20\x6c\x6f\x6e\x67\x20\x73\x74\x72\x69\x6e\x67"), 20},
+{U("\x00\xeb\x15\x23\x1d\xfc\xeb\x60\x92\x58\x86\xb6\x7d\x06\x52\x99\x92\x59\x15\xae\xb1\x72\xc0\x66\x47"), 25},
+{U("\x51\x6b\x6f\xcd\x0f"), 5},
+{U("\xbf\x4f\x89\x00\x1e\x67\x02\x74\xdd"), 9},
+{U("\x57\x2e\x47\x94"), 4},
+{U("\xec\xac\x89\xca\xd9\x39\x23\xc0\x23\x21"), 10},
+{U("\x10\xc8\x51\x1e"), 4},
+{U("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"), 10},
+};
+
+const char *vstrOut[] = {
+"",
+"2g",
+"a3gV",
+"aPEr",
+"2cFupjhnEsSn59qHXstmK2ffpLv2",
+"1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L",
+"ABnLTmg",
+"3SEo3LWLoPntC",
+"3EFU7m",
+"EJDM8drfXA6uyA",
+"Rt5zm",
+"1111111111"
+};
+
+BOOST_AUTO_TEST_CASE(base58_EncodeBase58)
+{
+ for (int i=0; i<sizeof(vstrIn)/sizeof(vstrIn[0]); i++)
+ {
+ BOOST_CHECK_EQUAL(EncodeBase58(vstrIn[i].data, vstrIn[i].data + vstrIn[i].size), vstrOut[i]);
+ }
+}
+
+BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
+{
+ std::vector<unsigned char> result;
+ for (int i=0; i<sizeof(vstrIn)/sizeof(vstrIn[0]); i++)
+ {
+ std::vector<unsigned char> expected(vstrIn[i].data, vstrIn[i].data + vstrIn[i].size);
+ BOOST_CHECK(DecodeBase58(vstrOut[i], result));
+ BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
+ }
+ BOOST_CHECK(!DecodeBase58("invalid", result));
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
diff --git a/src/test/base64_tests.cpp b/src/test/base64_tests.cpp
new file mode 100644
index 0000000000..fff30ef5eb
--- /dev/null
+++ b/src/test/base64_tests.cpp
@@ -0,0 +1,22 @@
+#include <boost/test/unit_test.hpp>
+
+#include "main.h"
+#include "wallet.h"
+#include "util.h"
+
+BOOST_AUTO_TEST_SUITE(base64_tests)
+
+BOOST_AUTO_TEST_CASE(base64_testvectors)
+{
+ static const std::string vstrIn[] = {"","f","fo","foo","foob","fooba","foobar"};
+ static const std::string vstrOut[] = {"","Zg==","Zm8=","Zm9v","Zm9vYg==","Zm9vYmE=","Zm9vYmFy"};
+ for (int i=0; i<sizeof(vstrIn)/sizeof(vstrIn[0]); i++)
+ {
+ std::string strEnc = EncodeBase64(vstrIn[i]);
+ BOOST_CHECK(strEnc == vstrOut[i]);
+ std::string strDec = DecodeBase64(strEnc);
+ BOOST_CHECK(strDec == vstrIn[i]);
+ }
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/test/miner_tests.cpp b/src/test/miner_tests.cpp
new file mode 100644
index 0000000000..d4abd6d2a7
--- /dev/null
+++ b/src/test/miner_tests.cpp
@@ -0,0 +1,35 @@
+#include <boost/test/unit_test.hpp>
+
+#include "uint256.h"
+
+extern void SHA256Transform(void* pstate, void* pinput, const void* pinit);
+
+BOOST_AUTO_TEST_SUITE(miner_tests)
+
+BOOST_AUTO_TEST_CASE(sha256transform_equality)
+{
+ unsigned int pSHA256InitState[8] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
+
+
+ unsigned char pstate[32];
+ unsigned char pinput[64];
+
+ int i;
+
+ for (i = 0; i < 32; i++) {
+ pinput[i] = i;
+ pinput[i+32] = 0;
+ }
+
+ uint256 hash;
+
+ SHA256Transform(&hash, pinput, pSHA256InitState);
+
+ BOOST_TEST_MESSAGE(hash.GetHex());
+
+ uint256 hash_reference("0x2df5e1c65ef9f8cde240d23cae2ec036d31a15ec64bc68f64be242b1da6631f3");
+
+ BOOST_CHECK(hash == hash_reference);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/test/multisig_tests.cpp b/src/test/multisig_tests.cpp
new file mode 100644
index 0000000000..58f62b9542
--- /dev/null
+++ b/src/test/multisig_tests.cpp
@@ -0,0 +1,294 @@
+#include <boost/assert.hpp>
+#include <boost/assign/list_of.hpp>
+#include <boost/assign/list_inserter.hpp>
+#include <boost/assign/std/vector.hpp>
+#include <boost/test/unit_test.hpp>
+#include <boost/foreach.hpp>
+#include <boost/tuple/tuple.hpp>
+
+#include <openssl/ec.h>
+#include <openssl/err.h>
+
+#include "keystore.h"
+#include "main.h"
+#include "script.h"
+#include "wallet.h"
+
+using namespace std;
+using namespace boost::assign;
+
+typedef vector<unsigned char> valtype;
+
+extern uint256 SignatureHash(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);
+extern bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn, int& nSigOpCount,
+ int nHashType, bool fStrictOpEval);
+
+BOOST_AUTO_TEST_SUITE(multisig_tests)
+
+CScript
+sign_multisig(CScript scriptPubKey, vector<CKey> keys, CTransaction transaction, int whichIn)
+{
+ uint256 hash = SignatureHash(scriptPubKey, transaction, whichIn, SIGHASH_ALL);
+
+ CScript result;
+ result << OP_0; // CHECKMULTISIG bug workaround
+ BOOST_FOREACH(CKey key, keys)
+ {
+ vector<unsigned char> vchSig;
+ BOOST_CHECK(key.Sign(hash, vchSig));
+ vchSig.push_back((unsigned char)SIGHASH_ALL);
+ result << vchSig;
+ }
+ return result;
+}
+
+BOOST_AUTO_TEST_CASE(multisig_verify)
+{
+ CKey key[4];
+ for (int i = 0; i < 4; i++)
+ key[i].MakeNewKey();
+
+ CScript a_and_b;
+ a_and_b << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG;
+
+ CScript a_or_b;
+ a_or_b << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG;
+
+ CScript escrow;
+ escrow << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << key[2].GetPubKey() << OP_3 << OP_CHECKMULTISIG;
+
+ CTransaction txFrom; // Funding transaction
+ txFrom.vout.resize(3);
+ txFrom.vout[0].scriptPubKey = a_and_b;
+ txFrom.vout[1].scriptPubKey = a_or_b;
+ txFrom.vout[2].scriptPubKey = escrow;
+
+ CTransaction txTo[3]; // Spending transaction
+ for (int i = 0; i < 3; i++)
+ {
+ txTo[i].vin.resize(1);
+ txTo[i].vout.resize(1);
+ txTo[i].vin[0].prevout.n = i;
+ txTo[i].vin[0].prevout.hash = txFrom.GetHash();
+ txTo[i].vout[0].nValue = 1;
+ }
+
+ vector<CKey> keys;
+ CScript s;
+ int nUnused = 0;
+
+ // Test a AND b:
+ keys.clear();
+ keys += key[0],key[1]; // magic operator+= from boost.assign
+ s = sign_multisig(a_and_b, keys, txTo[0], 0);
+ BOOST_CHECK(VerifyScript(s, a_and_b, txTo[0], 0, nUnused, 0, true));
+
+ for (int i = 0; i < 4; i++)
+ {
+ keys.clear();
+ keys += key[i];
+ s = sign_multisig(a_and_b, keys, txTo[0], 0);
+ BOOST_CHECK_MESSAGE(!VerifyScript(s, a_and_b, txTo[0], 0, nUnused, 0, true), strprintf("a&b 1: %d", i));
+
+ keys.clear();
+ keys += key[1],key[i];
+ s = sign_multisig(a_and_b, keys, txTo[0], 0);
+ BOOST_CHECK_MESSAGE(!VerifyScript(s, a_and_b, txTo[0], 0, nUnused, 0, true), strprintf("a&b 2: %d", i));
+ }
+
+ // Test a OR b:
+ for (int i = 0; i < 4; i++)
+ {
+ keys.clear();
+ keys += key[i];
+ s = sign_multisig(a_or_b, keys, txTo[1], 0);
+ if (i == 0 || i == 1)
+ BOOST_CHECK_MESSAGE(VerifyScript(s, a_or_b, txTo[1], 0, nUnused, 0, true), strprintf("a|b: %d", i));
+ else
+ BOOST_CHECK_MESSAGE(!VerifyScript(s, a_or_b, txTo[1], 0, nUnused, 0, true), strprintf("a|b: %d", i));
+ }
+ s.clear();
+ s << OP_0 << OP_0;
+ BOOST_CHECK(!VerifyScript(s, a_or_b, txTo[1], 0, nUnused, 0, true));
+ s.clear();
+ s << OP_0 << OP_1;
+ BOOST_CHECK(!VerifyScript(s, a_or_b, txTo[1], 0, nUnused, 0, true));
+
+
+ for (int i = 0; i < 4; i++)
+ for (int j = 0; j < 4; j++)
+ {
+ keys.clear();
+ keys += key[i],key[j];
+ s = sign_multisig(escrow, keys, txTo[2], 0);
+ if (i < j && i < 3 && j < 3)
+ BOOST_CHECK_MESSAGE(VerifyScript(s, escrow, txTo[2], 0, nUnused, 0, true), strprintf("escrow 1: %d %d", i, j));
+ else
+ BOOST_CHECK_MESSAGE(!VerifyScript(s, escrow, txTo[2], 0, nUnused, 0, true), strprintf("escrow 2: %d %d", i, j));
+ }
+}
+
+BOOST_AUTO_TEST_CASE(multisig_IsStandard)
+{
+ CKey key[4];
+ for (int i = 0; i < 4; i++)
+ key[i].MakeNewKey();
+
+ CScript a_and_b;
+ a_and_b << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG;
+ BOOST_CHECK(::IsStandard(a_and_b));
+
+ CScript a_or_b;
+ a_or_b << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG;
+ BOOST_CHECK(::IsStandard(a_or_b));
+
+ CScript escrow;
+ escrow << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << key[2].GetPubKey() << OP_3 << OP_CHECKMULTISIG;
+ BOOST_CHECK(::IsStandard(escrow));
+
+ CScript one_of_four;
+ one_of_four << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << key[2].GetPubKey() << key[3].GetPubKey() << OP_4 << OP_CHECKMULTISIG;
+ BOOST_CHECK(!::IsStandard(one_of_four));
+
+ CScript malformed[6];
+ malformed[0] << OP_3 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG;
+ malformed[1] << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << OP_3 << OP_CHECKMULTISIG;
+ malformed[2] << OP_0 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG;
+ malformed[3] << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << OP_0 << OP_CHECKMULTISIG;
+ malformed[4] << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << OP_CHECKMULTISIG;
+ malformed[5] << OP_1 << key[0].GetPubKey() << key[1].GetPubKey();
+
+ for (int i = 0; i < 6; i++)
+ BOOST_CHECK(!::IsStandard(malformed[i]));
+}
+
+BOOST_AUTO_TEST_CASE(multisig_Solver1)
+{
+ // Tests Solver() that returns lists of keys that are
+ // required to satisfy a ScriptPubKey
+ //
+ // Also tests IsMine() and ExtractAddress()
+ //
+ // Note: ExtractAddress for the multisignature transactions
+ // always returns false for this release, even if you have
+ // one key that would satisfy an (a|b) or 2-of-3 keys needed
+ // to spend an escrow transaction.
+ //
+ CBasicKeyStore keystore, emptykeystore;
+ CKey key[3];
+ CBitcoinAddress keyaddr[3];
+ for (int i = 0; i < 3; i++)
+ {
+ key[i].MakeNewKey();
+ keystore.AddKey(key[i]);
+ keyaddr[i].SetPubKey(key[i].GetPubKey());
+ }
+
+ {
+ vector<valtype> solutions;
+ txnouttype whichType;
+ CScript s;
+ s << key[0].GetPubKey() << OP_CHECKSIG;
+ BOOST_CHECK(Solver(s, whichType, solutions));
+ BOOST_CHECK(solutions.size() == 1);
+ CBitcoinAddress addr;
+ BOOST_CHECK(ExtractAddress(s, &keystore, addr));
+ BOOST_CHECK(addr == keyaddr[0]);
+ BOOST_CHECK(IsMine(keystore, s));
+ BOOST_CHECK(!IsMine(emptykeystore, s));
+ }
+ {
+ vector<valtype> solutions;
+ txnouttype whichType;
+ CScript s;
+ s << OP_DUP << OP_HASH160 << Hash160(key[0].GetPubKey()) << OP_EQUALVERIFY << OP_CHECKSIG;
+ BOOST_CHECK(Solver(s, whichType, solutions));
+ BOOST_CHECK(solutions.size() == 1);
+ CBitcoinAddress addr;
+ BOOST_CHECK(ExtractAddress(s, &keystore, addr));
+ BOOST_CHECK(addr == keyaddr[0]);
+ BOOST_CHECK(IsMine(keystore, s));
+ BOOST_CHECK(!IsMine(emptykeystore, s));
+ }
+ {
+ vector<valtype> solutions;
+ txnouttype whichType;
+ CScript s;
+ s << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG;
+ BOOST_CHECK(Solver(s, whichType, solutions));
+ BOOST_CHECK_EQUAL(solutions.size(), 4);
+ CBitcoinAddress addr;
+ BOOST_CHECK(!ExtractAddress(s, &keystore, addr));
+ BOOST_CHECK(IsMine(keystore, s));
+ BOOST_CHECK(!IsMine(emptykeystore, s));
+ }
+ {
+ vector<valtype> solutions;
+ txnouttype whichType;
+ CScript s;
+ s << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG;
+ BOOST_CHECK(Solver(s, whichType, solutions));
+ BOOST_CHECK_EQUAL(solutions.size(), 4);
+ vector<CBitcoinAddress> addrs;
+ int nRequired;
+ BOOST_CHECK(ExtractAddresses(s, &keystore, whichType, addrs, nRequired));
+ BOOST_CHECK(addrs[0] == keyaddr[0]);
+ BOOST_CHECK(addrs[1] == keyaddr[1]);
+ BOOST_CHECK(nRequired = 1);
+ BOOST_CHECK(IsMine(keystore, s));
+ BOOST_CHECK(!IsMine(emptykeystore, s));
+ }
+ {
+ vector<valtype> solutions;
+ txnouttype whichType;
+ CScript s;
+ s << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << key[2].GetPubKey() << OP_3 << OP_CHECKMULTISIG;
+ BOOST_CHECK(Solver(s, whichType, solutions));
+ BOOST_CHECK(solutions.size() == 5);
+ }
+}
+
+BOOST_AUTO_TEST_CASE(multisig_Sign)
+{
+ // Test SignSignature() (and therefore the version of Solver() that signs transactions)
+ CBasicKeyStore keystore;
+ CKey key[4];
+ for (int i = 0; i < 4; i++)
+ {
+ key[i].MakeNewKey();
+ keystore.AddKey(key[i]);
+ }
+
+ CScript a_and_b;
+ a_and_b << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG;
+
+ CScript a_or_b;
+ a_or_b << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG;
+
+ CScript escrow;
+ escrow << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << key[2].GetPubKey() << OP_3 << OP_CHECKMULTISIG;
+
+ CTransaction txFrom; // Funding transaction
+ txFrom.vout.resize(3);
+ txFrom.vout[0].scriptPubKey = a_and_b;
+ txFrom.vout[1].scriptPubKey = a_or_b;
+ txFrom.vout[2].scriptPubKey = escrow;
+
+ CTransaction txTo[3]; // Spending transaction
+ for (int i = 0; i < 3; i++)
+ {
+ txTo[i].vin.resize(1);
+ txTo[i].vout.resize(1);
+ txTo[i].vin[0].prevout.n = i;
+ txTo[i].vin[0].prevout.hash = txFrom.GetHash();
+ txTo[i].vout[0].nValue = 1;
+ }
+
+ for (int i = 0; i < 3; i++)
+ {
+ BOOST_CHECK_MESSAGE(SignSignature(keystore, txFrom, txTo[i], 0), strprintf("SignSignature %d", i));
+ }
+}
+
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/test/script_op_eval_tests.cpp b/src/test/script_op_eval_tests.cpp
new file mode 100644
index 0000000000..c44642c6e9
--- /dev/null
+++ b/src/test/script_op_eval_tests.cpp
@@ -0,0 +1,234 @@
+#include <boost/assert.hpp>
+#include <boost/assign/list_of.hpp>
+#include <boost/assign/list_inserter.hpp>
+#include <boost/assign/std/vector.hpp>
+#include <boost/test/unit_test.hpp>
+#include <boost/foreach.hpp>
+
+#include "../main.h"
+#include "../script.h"
+#include "../wallet.h"
+
+using namespace std;
+
+// Test routines internal to script.cpp:
+extern uint256 SignatureHash(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);
+extern bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn, int& nSigOps,
+ int nHashType, bool fStrictOpEval);
+
+BOOST_AUTO_TEST_SUITE(script_op_eval_tests)
+
+BOOST_AUTO_TEST_CASE(script_op_eval1)
+{
+ // OP_EVAL looks like this:
+ // scriptSig: <sig> <sig...> <serialized_script>
+ // scriptPubKey: DUP HASH160 <hash> EQUALVERIFY EVAL
+
+ // Test SignSignature() (and therefore the version of Solver() that signs transactions)
+ CBasicKeyStore keystore;
+ CKey key[4];
+ for (int i = 0; i < 4; i++)
+ {
+ key[i].MakeNewKey();
+ keystore.AddKey(key[i]);
+ }
+
+ // 8 Scripts: checking all combinations of
+ // different keys, straight/EVAL, pubkey/pubkeyhash
+ CScript standardScripts[4];
+ standardScripts[0] << key[0].GetPubKey() << OP_CHECKSIG;
+ standardScripts[1].SetBitcoinAddress(key[1].GetPubKey());
+ standardScripts[2] << key[1].GetPubKey() << OP_CHECKSIG;
+ standardScripts[3].SetBitcoinAddress(key[2].GetPubKey());
+ CScript evalScripts[4];
+ uint160 sigScriptHashes[4];
+ for (int i = 0; i < 4; i++)
+ {
+ sigScriptHashes[i] = Hash160(standardScripts[i]);
+ keystore.AddCScript(sigScriptHashes[i], standardScripts[i]);
+ evalScripts[i] << OP_DUP << OP_HASH160 << sigScriptHashes[i] << OP_EQUALVERIFY << OP_EVAL;
+ }
+
+ CTransaction txFrom; // Funding transaction:
+ txFrom.vout.resize(8);
+ for (int i = 0; i < 4; i++)
+ {
+ txFrom.vout[i].scriptPubKey = evalScripts[i];
+ txFrom.vout[i+4].scriptPubKey = standardScripts[i];
+ }
+ BOOST_CHECK(txFrom.IsStandard());
+
+ CTransaction txTo[8]; // Spending transactions
+ for (int i = 0; i < 8; i++)
+ {
+ txTo[i].vin.resize(1);
+ txTo[i].vout.resize(1);
+ txTo[i].vin[0].prevout.n = i;
+ txTo[i].vin[0].prevout.hash = txFrom.GetHash();
+ txTo[i].vout[0].nValue = 1;
+ BOOST_CHECK_MESSAGE(IsMine(keystore, txFrom.vout[i].scriptPubKey), strprintf("IsMine %d", i));
+ }
+ for (int i = 0; i < 8; i++)
+ {
+ BOOST_CHECK_MESSAGE(SignSignature(keystore, txFrom, txTo[i], 0), strprintf("SignSignature %d", i));
+ }
+ // All of the above should be OK, and the txTos have valid signatures
+ // Check to make sure signature verification fails if we use the wrong ScriptSig:
+ for (int i = 0; i < 8; i++)
+ for (int j = 0; j < 8; j++)
+ {
+ CScript sigSave = txTo[i].vin[0].scriptSig;
+ txTo[i].vin[0].scriptSig = txTo[j].vin[0].scriptSig;
+ int nUnused = 0;
+ bool sigOK = VerifySignature(txFrom, txTo[i], 0, nUnused);
+ if (i == j)
+ BOOST_CHECK_MESSAGE(sigOK, strprintf("VerifySignature %d %d", i, j));
+ else
+ BOOST_CHECK_MESSAGE(!sigOK, strprintf("VerifySignature %d %d", i, j));
+ txTo[i].vin[0].scriptSig = sigSave;
+ }
+}
+
+BOOST_AUTO_TEST_CASE(script_op_eval2)
+{
+ // Test OP_EVAL edge cases
+
+ CScript recurse;
+ recurse << OP_DUP << OP_EVAL;
+
+ uint160 recurseHash = Hash160(recurse);
+
+ CScript fund;
+ fund << OP_DUP << OP_HASH160 << recurseHash << OP_EQUALVERIFY << OP_EVAL;
+
+ CTransaction txFrom; // Funding transaction:
+ txFrom.vout.resize(1);
+ txFrom.vout[0].scriptPubKey = fund;
+
+ BOOST_CHECK(txFrom.IsStandard()); // Looks like a standard transaction until you try to spend it
+
+ CTransaction txTo;
+ txTo.vin.resize(1);
+ txTo.vout.resize(1);
+ txTo.vin[0].prevout.n = 0;
+ txTo.vin[0].prevout.hash = txFrom.GetHash();
+ txTo.vin[0].scriptSig = CScript() << static_cast<std::vector<unsigned char> >(recurse);
+ txTo.vout[0].nValue = 1;
+
+ int nUnused = 0;
+ BOOST_CHECK(!VerifyScript(txTo.vin[0].scriptSig, txFrom.vout[0].scriptPubKey, txTo, 0, nUnused, 0, true));
+ BOOST_CHECK(!VerifySignature(txFrom, txTo, 0, nUnused, true));
+}
+
+BOOST_AUTO_TEST_CASE(script_op_eval3)
+{
+ // Test the CScript::Set* methods
+ CBasicKeyStore keystore;
+ CKey key[4];
+ std::vector<CKey> keys;
+ for (int i = 0; i < 4; i++)
+ {
+ key[i].MakeNewKey();
+ keystore.AddKey(key[i]);
+ keys.push_back(key[i]);
+ }
+
+ CScript inner[4];
+ inner[0].SetBitcoinAddress(key[0].GetPubKey());
+ inner[1].SetMultisig(2, std::vector<CKey>(keys.begin(), keys.begin()+2));
+ inner[2].SetMultisig(1, std::vector<CKey>(keys.begin(), keys.begin()+2));
+ inner[3].SetMultisig(2, std::vector<CKey>(keys.begin(), keys.begin()+3));
+
+ CScript outer[4];
+ for (int i = 0; i < 4; i++)
+ {
+ outer[i].SetEval(inner[i]);
+ keystore.AddCScript(Hash160(inner[i]), inner[i]);
+ }
+
+ CTransaction txFrom; // Funding transaction:
+ txFrom.vout.resize(4);
+ for (int i = 0; i < 4; i++)
+ {
+ txFrom.vout[i].scriptPubKey = outer[i];
+ }
+ BOOST_CHECK(txFrom.IsStandard());
+
+ CTransaction txTo[4]; // Spending transactions
+ for (int i = 0; i < 4; i++)
+ {
+ txTo[i].vin.resize(1);
+ txTo[i].vout.resize(1);
+ txTo[i].vin[0].prevout.n = i;
+ txTo[i].vin[0].prevout.hash = txFrom.GetHash();
+ txTo[i].vout[0].nValue = 1;
+ txTo[i].vout[0].scriptPubKey = inner[i];
+ BOOST_CHECK_MESSAGE(IsMine(keystore, txFrom.vout[i].scriptPubKey), strprintf("IsMine %d", i));
+ }
+ for (int i = 0; i < 4; i++)
+ {
+ BOOST_CHECK_MESSAGE(SignSignature(keystore, txFrom, txTo[i], 0), strprintf("SignSignature %d", i));
+ BOOST_CHECK_MESSAGE(txTo[i].IsStandard(), strprintf("txTo[%d].IsStandard", i));
+ }
+}
+
+BOOST_AUTO_TEST_CASE(script_op_eval_backcompat1)
+{
+ // Check backwards-incompatibility-testing code
+ CScript returnsEleven;
+ returnsEleven << OP_11;
+
+ // This should validate on new clients, but will
+ // be invalid on old clients (that interpret OP_EVAL as a no-op)
+ // ... except there's a special rule that makes new clients reject
+ // it.
+ CScript fund;
+ fund << OP_EVAL << OP_11 << OP_EQUAL;
+
+ CTransaction txFrom; // Funding transaction:
+ txFrom.vout.resize(1);
+ txFrom.vout[0].scriptPubKey = fund;
+
+ CTransaction txTo;
+ txTo.vin.resize(1);
+ txTo.vout.resize(1);
+ txTo.vin[0].prevout.n = 0;
+ txTo.vin[0].prevout.hash = txFrom.GetHash();
+ txTo.vin[0].scriptSig = CScript() << static_cast<std::vector<unsigned char> >(returnsEleven);
+ txTo.vout[0].nValue = 1;
+
+ int nUnused = 0;
+ BOOST_CHECK(!VerifyScript(txTo.vin[0].scriptSig, txFrom.vout[0].scriptPubKey, txTo, 0, nUnused, 0, true));
+ BOOST_CHECK(!VerifySignature(txFrom, txTo, 0, nUnused, true));
+}
+
+BOOST_AUTO_TEST_CASE(script_op_eval_switchover)
+{
+ // Test OP_EVAL switchover code
+ CScript notValid;
+ notValid << OP_11 << OP_12 << OP_EQUALVERIFY;
+
+ // This will be valid under old rules, invalid under new:
+ CScript fund;
+ fund << OP_EVAL;
+
+ CTransaction txFrom; // Funding transaction:
+ txFrom.vout.resize(1);
+ txFrom.vout[0].scriptPubKey = fund;
+
+ CTransaction txTo;
+ txTo.vin.resize(1);
+ txTo.vout.resize(1);
+ txTo.vin[0].prevout.n = 0;
+ txTo.vin[0].prevout.hash = txFrom.GetHash();
+ txTo.vin[0].scriptSig = CScript() << static_cast<std::vector<unsigned char> >(notValid);
+ txTo.vout[0].nValue = 1;
+
+ int nUnused = 0;
+ BOOST_CHECK(VerifyScript(txTo.vin[0].scriptSig, txFrom.vout[0].scriptPubKey, txTo, 0, nUnused, 0, false));
+
+ // Under strict op_eval switchover, it should be considered invalid:
+ BOOST_CHECK(!VerifyScript(txTo.vin[0].scriptSig, txFrom.vout[0].scriptPubKey, txTo, 0, nUnused, 0, true));
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/test/script_tests.cpp b/src/test/script_tests.cpp
index 13feb86b97..22885a6438 100644
--- a/src/test/script_tests.cpp
+++ b/src/test/script_tests.cpp
@@ -2,13 +2,13 @@
#include <boost/test/unit_test.hpp>
#include <boost/foreach.hpp>
-#include "../main.h"
-#include "../wallet.h"
+#include "main.h"
+#include "wallet.h"
using namespace std;
extern uint256 SignatureHash(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);
-extern bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn, int nHashType);
-extern bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, int nHashType);
+extern bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn, int& nSigOps,
+ int nHashType, bool fStrictOpEval);
BOOST_AUTO_TEST_SUITE(script_tests)
@@ -21,19 +21,21 @@ BOOST_AUTO_TEST_CASE(script_PushData)
static const unsigned char pushdata2[] = { OP_PUSHDATA2, 1, 0, 0x5a };
static const unsigned char pushdata4[] = { OP_PUSHDATA4, 1, 0, 0, 0, 0x5a };
+ int nUnused = 0;
+
vector<vector<unsigned char> > directStack;
- BOOST_CHECK(EvalScript(directStack, CScript(&direct[0], &direct[sizeof(direct)]), CTransaction(), 0, 0));
+ BOOST_CHECK(EvalScript(directStack, CScript(&direct[0], &direct[sizeof(direct)]), CTransaction(), 0, 0, true, nUnused));
vector<vector<unsigned char> > pushdata1Stack;
- BOOST_CHECK(EvalScript(pushdata1Stack, CScript(&pushdata1[0], &pushdata1[sizeof(pushdata1)]), CTransaction(), 0, 0));
+ BOOST_CHECK(EvalScript(pushdata1Stack, CScript(&pushdata1[0], &pushdata1[sizeof(pushdata1)]), CTransaction(), 0, 0, true, nUnused));
BOOST_CHECK(pushdata1Stack == directStack);
vector<vector<unsigned char> > pushdata2Stack;
- BOOST_CHECK(EvalScript(pushdata2Stack, CScript(&pushdata2[0], &pushdata2[sizeof(pushdata2)]), CTransaction(), 0, 0));
+ BOOST_CHECK(EvalScript(pushdata2Stack, CScript(&pushdata2[0], &pushdata2[sizeof(pushdata2)]), CTransaction(), 0, 0, true, nUnused));
BOOST_CHECK(pushdata2Stack == directStack);
vector<vector<unsigned char> > pushdata4Stack;
- BOOST_CHECK(EvalScript(pushdata4Stack, CScript(&pushdata4[0], &pushdata4[sizeof(pushdata4)]), CTransaction(), 0, 0));
+ BOOST_CHECK(EvalScript(pushdata4Stack, CScript(&pushdata4[0], &pushdata4[sizeof(pushdata4)]), CTransaction(), 0, 0, true, nUnused));
BOOST_CHECK(pushdata4Stack == directStack);
}
@@ -71,6 +73,7 @@ sign_multisig(CScript scriptPubKey, CKey key, CTransaction transaction)
BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG12)
{
+ int nUnused = 0;
CKey key1, key2, key3;
key1.MakeNewKey();
key2.MakeNewKey();
@@ -91,19 +94,20 @@ BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG12)
txTo12.vout[0].nValue = 1;
CScript goodsig1 = sign_multisig(scriptPubKey12, key1, txTo12);
- BOOST_CHECK(VerifyScript(goodsig1, scriptPubKey12, txTo12, 0, 0));
+ BOOST_CHECK(VerifyScript(goodsig1, scriptPubKey12, txTo12, 0, nUnused, 0, true));
txTo12.vout[0].nValue = 2;
- BOOST_CHECK(!VerifyScript(goodsig1, scriptPubKey12, txTo12, 0, 0));
+ BOOST_CHECK(!VerifyScript(goodsig1, scriptPubKey12, txTo12, 0, nUnused, 0, true));
CScript goodsig2 = sign_multisig(scriptPubKey12, key2, txTo12);
- BOOST_CHECK(VerifyScript(goodsig2, scriptPubKey12, txTo12, 0, 0));
+ BOOST_CHECK(VerifyScript(goodsig2, scriptPubKey12, txTo12, 0, nUnused, 0, true));
CScript badsig1 = sign_multisig(scriptPubKey12, key3, txTo12);
- BOOST_CHECK(!VerifyScript(badsig1, scriptPubKey12, txTo12, 0, 0));
+ BOOST_CHECK(!VerifyScript(badsig1, scriptPubKey12, txTo12, 0, nUnused, 0, true));
}
BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG23)
{
+ int nUnused = 0;
CKey key1, key2, key3, key4;
key1.MakeNewKey();
key2.MakeNewKey();
@@ -127,46 +131,46 @@ BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG23)
std::vector<CKey> keys;
keys.push_back(key1); keys.push_back(key2);
CScript goodsig1 = sign_multisig(scriptPubKey23, keys, txTo23);
- BOOST_CHECK(VerifyScript(goodsig1, scriptPubKey23, txTo23, 0, 0));
+ BOOST_CHECK(VerifyScript(goodsig1, scriptPubKey23, txTo23, 0, nUnused, 0, true));
keys.clear();
keys.push_back(key1); keys.push_back(key3);
CScript goodsig2 = sign_multisig(scriptPubKey23, keys, txTo23);
- BOOST_CHECK(VerifyScript(goodsig2, scriptPubKey23, txTo23, 0, 0));
+ BOOST_CHECK(VerifyScript(goodsig2, scriptPubKey23, txTo23, 0, nUnused, 0, true));
keys.clear();
keys.push_back(key2); keys.push_back(key3);
CScript goodsig3 = sign_multisig(scriptPubKey23, keys, txTo23);
- BOOST_CHECK(VerifyScript(goodsig3, scriptPubKey23, txTo23, 0, 0));
+ BOOST_CHECK(VerifyScript(goodsig3, scriptPubKey23, txTo23, 0, nUnused, 0, true));
keys.clear();
keys.push_back(key2); keys.push_back(key2); // Can't re-use sig
CScript badsig1 = sign_multisig(scriptPubKey23, keys, txTo23);
- BOOST_CHECK(!VerifyScript(badsig1, scriptPubKey23, txTo23, 0, 0));
+ BOOST_CHECK(!VerifyScript(badsig1, scriptPubKey23, txTo23, 0, nUnused, 0, true));
keys.clear();
keys.push_back(key2); keys.push_back(key1); // sigs must be in correct order
CScript badsig2 = sign_multisig(scriptPubKey23, keys, txTo23);
- BOOST_CHECK(!VerifyScript(badsig2, scriptPubKey23, txTo23, 0, 0));
+ BOOST_CHECK(!VerifyScript(badsig2, scriptPubKey23, txTo23, 0, nUnused, 0, true));
keys.clear();
keys.push_back(key3); keys.push_back(key2); // sigs must be in correct order
CScript badsig3 = sign_multisig(scriptPubKey23, keys, txTo23);
- BOOST_CHECK(!VerifyScript(badsig3, scriptPubKey23, txTo23, 0, 0));
+ BOOST_CHECK(!VerifyScript(badsig3, scriptPubKey23, txTo23, 0, nUnused, 0, true));
keys.clear();
keys.push_back(key4); keys.push_back(key2); // sigs must match pubkeys
CScript badsig4 = sign_multisig(scriptPubKey23, keys, txTo23);
- BOOST_CHECK(!VerifyScript(badsig4, scriptPubKey23, txTo23, 0, 0));
+ BOOST_CHECK(!VerifyScript(badsig4, scriptPubKey23, txTo23, 0, nUnused, 0, true));
keys.clear();
keys.push_back(key1); keys.push_back(key4); // sigs must match pubkeys
CScript badsig5 = sign_multisig(scriptPubKey23, keys, txTo23);
- BOOST_CHECK(!VerifyScript(badsig5, scriptPubKey23, txTo23, 0, 0));
+ BOOST_CHECK(!VerifyScript(badsig5, scriptPubKey23, txTo23, 0, nUnused, 0, true));
keys.clear(); // Must have signatures
CScript badsig6 = sign_multisig(scriptPubKey23, keys, txTo23);
- BOOST_CHECK(!VerifyScript(badsig6, scriptPubKey23, txTo23, 0, 0));
+ BOOST_CHECK(!VerifyScript(badsig6, scriptPubKey23, txTo23, 0, nUnused, 0, true));
}
diff --git a/src/test/test_bitcoin.cpp b/src/test/test_bitcoin.cpp
index 0230bb6eca..4b52b74cfe 100644
--- a/src/test/test_bitcoin.cpp
+++ b/src/test/test_bitcoin.cpp
@@ -1,18 +1,22 @@
#define BOOST_TEST_MODULE Bitcoin Test Suite
#include <boost/test/unit_test.hpp>
-#include "../main.h"
-#include "../wallet.h"
+#include "main.h"
+#include "wallet.h"
-#include "uint160_tests.cpp"
-#include "uint256_tests.cpp"
-#include "script_tests.cpp"
-#include "transaction_tests.cpp"
+extern bool fPrintToConsole;
+struct TestingSetup {
+ TestingSetup() {
+ fPrintToConsole = true; // don't want to write to debug.log file
+ }
+ ~TestingSetup() { }
+};
+BOOST_GLOBAL_FIXTURE(TestingSetup);
CWallet* pwalletMain;
void Shutdown(void* parg)
{
- exit(0);
+ exit(0);
}
diff --git a/src/test/transaction_tests.cpp b/src/test/transaction_tests.cpp
index e6eb0f054f..3268343bbe 100644
--- a/src/test/transaction_tests.cpp
+++ b/src/test/transaction_tests.cpp
@@ -1,7 +1,7 @@
#include <boost/test/unit_test.hpp>
-#include "../main.h"
-#include "../wallet.h"
+#include "main.h"
+#include "wallet.h"
using namespace std;
diff --git a/src/test/uint160_tests.cpp b/src/test/uint160_tests.cpp
index 42c8275afe..35cb35b25a 100644
--- a/src/test/uint160_tests.cpp
+++ b/src/test/uint160_tests.cpp
@@ -1,6 +1,6 @@
#include <boost/test/unit_test.hpp>
-#include "../uint256.h"
+#include "uint256.h"
BOOST_AUTO_TEST_SUITE(uint160_tests)
diff --git a/src/test/uint256_tests.cpp b/src/test/uint256_tests.cpp
index c5d45e215e..efdc8a6aeb 100644
--- a/src/test/uint256_tests.cpp
+++ b/src/test/uint256_tests.cpp
@@ -1,6 +1,6 @@
#include <boost/test/unit_test.hpp>
-#include "../uint256.h"
+#include "uint256.h"
BOOST_AUTO_TEST_SUITE(uint256_tests)
diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp
new file mode 100644
index 0000000000..9571c47382
--- /dev/null
+++ b/src/test/util_tests.cpp
@@ -0,0 +1,235 @@
+#include <vector>
+#include <boost/test/unit_test.hpp>
+#include <boost/foreach.hpp>
+
+#include "main.h"
+#include "wallet.h"
+#include "util.h"
+
+using namespace std;
+
+BOOST_AUTO_TEST_SUITE(util_tests)
+
+BOOST_AUTO_TEST_CASE(util_criticalsection)
+{
+ CCriticalSection cs;
+
+ do {
+ CRITICAL_BLOCK(cs)
+ break;
+
+ BOOST_ERROR("break was swallowed!");
+ } while(0);
+
+ do {
+ TRY_CRITICAL_BLOCK(cs)
+ break;
+
+ BOOST_ERROR("break was swallowed!");
+ } while(0);
+}
+
+BOOST_AUTO_TEST_CASE(util_MedianFilter)
+{
+ CMedianFilter<int> filter(5, 15);
+
+ BOOST_CHECK_EQUAL(filter.median(), 15);
+
+ filter.input(20); // [15 20]
+ BOOST_CHECK_EQUAL(filter.median(), 17);
+
+ filter.input(30); // [15 20 30]
+ BOOST_CHECK_EQUAL(filter.median(), 20);
+
+ filter.input(3); // [3 15 20 30]
+ BOOST_CHECK_EQUAL(filter.median(), 17);
+
+ filter.input(7); // [3 7 15 20 30]
+ BOOST_CHECK_EQUAL(filter.median(), 15);
+
+ filter.input(18); // [3 7 18 20 30]
+ BOOST_CHECK_EQUAL(filter.median(), 18);
+
+ filter.input(0); // [0 3 7 18 30]
+ BOOST_CHECK_EQUAL(filter.median(), 7);
+}
+
+static const unsigned char ParseHex_expected[65] = {
+ 0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7,
+ 0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde,
+ 0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12,
+ 0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d,
+ 0x5f
+};
+BOOST_AUTO_TEST_CASE(util_ParseHex)
+{
+ std::vector<unsigned char> result;
+ std::vector<unsigned char> expected(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected));
+ // Basic test vector
+ result = ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
+ BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
+
+ // Spaces between bytes must be supported
+ result = ParseHex("12 34 56 78");
+ BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
+
+ // Stop parsing at invalid value
+ result = ParseHex("1234 invalid 1234");
+ BOOST_CHECK(result.size() == 2 && result[0] == 0x12 && result[1] == 0x34);
+}
+
+BOOST_AUTO_TEST_CASE(util_HexStr)
+{
+ BOOST_CHECK_EQUAL(
+ HexStr(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected)),
+ "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
+
+ BOOST_CHECK_EQUAL(
+ HexStr(ParseHex_expected, ParseHex_expected + 5, true),
+ "04 67 8a fd b0");
+}
+
+BOOST_AUTO_TEST_CASE(util_DateTimeStrFormat)
+{
+ BOOST_CHECK_EQUAL(DateTimeStrFormat("%x %H:%M:%S", 0), "01/01/70 00:00:00");
+ BOOST_CHECK_EQUAL(DateTimeStrFormat("%x %H:%M:%S", 0x7FFFFFFF), "01/19/38 03:14:07");
+ // Formats used within bitcoin
+ BOOST_CHECK_EQUAL(DateTimeStrFormat("%x %H:%M:%S", 1317425777), "09/30/11 23:36:17");
+ BOOST_CHECK_EQUAL(DateTimeStrFormat("%x %H:%M", 1317425777), "09/30/11 23:36");
+}
+
+BOOST_AUTO_TEST_CASE(util_ParseParameters)
+{
+ const char *argv_test[] = {"-ignored", "-a", "-b", "-ccc=argument", "-ccc=multiple", "f", "-d=e"};
+
+ ParseParameters(0, (char**)argv_test);
+ BOOST_CHECK(mapArgs.empty() && mapMultiArgs.empty());
+
+ ParseParameters(1, (char**)argv_test);
+ BOOST_CHECK(mapArgs.empty() && mapMultiArgs.empty());
+
+ ParseParameters(5, (char**)argv_test);
+ // expectation: -ignored is ignored (program name argument),
+ // -a, -b and -ccc end up in map, -d ignored because it is after
+ // a non-option argument (non-GNU option parsing)
+ BOOST_CHECK(mapArgs.size() == 3 && mapMultiArgs.size() == 3);
+ BOOST_CHECK(mapArgs.count("-a") && mapArgs.count("-b") && mapArgs.count("-ccc")
+ && !mapArgs.count("f") && !mapArgs.count("-d"));
+ BOOST_CHECK(mapMultiArgs.count("-a") && mapMultiArgs.count("-b") && mapMultiArgs.count("-ccc")
+ && !mapMultiArgs.count("f") && !mapMultiArgs.count("-d"));
+
+ BOOST_CHECK(mapArgs["-a"] == "" && mapArgs["-ccc"] == "multiple");
+ BOOST_CHECK(mapMultiArgs["-ccc"].size() == 2);
+}
+
+BOOST_AUTO_TEST_CASE(util_GetArg)
+{
+ mapArgs.clear();
+ mapArgs["strtest1"] = "string...";
+ // strtest2 undefined on purpose
+ mapArgs["inttest1"] = "12345";
+ mapArgs["inttest2"] = "81985529216486895";
+ // inttest3 undefined on purpose
+ mapArgs["booltest1"] = "";
+ // booltest2 undefined on purpose
+ mapArgs["booltest3"] = "0";
+ mapArgs["booltest4"] = "1";
+
+ BOOST_CHECK_EQUAL(GetArg("strtest1", "default"), "string...");
+ BOOST_CHECK_EQUAL(GetArg("strtest2", "default"), "default");
+ BOOST_CHECK_EQUAL(GetArg("inttest1", -1), 12345);
+ BOOST_CHECK_EQUAL(GetArg("inttest2", -1), 81985529216486895LL);
+ BOOST_CHECK_EQUAL(GetArg("inttest3", -1), -1);
+ BOOST_CHECK_EQUAL(GetBoolArg("booltest1"), true);
+ BOOST_CHECK_EQUAL(GetBoolArg("booltest2"), false);
+ BOOST_CHECK_EQUAL(GetBoolArg("booltest3"), false);
+ BOOST_CHECK_EQUAL(GetBoolArg("booltest4"), true);
+}
+
+BOOST_AUTO_TEST_CASE(util_WildcardMatch)
+{
+ BOOST_CHECK(WildcardMatch("127.0.0.1", "*"));
+ BOOST_CHECK(WildcardMatch("127.0.0.1", "127.*"));
+ BOOST_CHECK(WildcardMatch("abcdef", "a?cde?"));
+ BOOST_CHECK(!WildcardMatch("abcdef", "a?cde??"));
+ BOOST_CHECK(WildcardMatch("abcdef", "a*f"));
+ BOOST_CHECK(!WildcardMatch("abcdef", "a*x"));
+ BOOST_CHECK(WildcardMatch("", "*"));
+}
+
+BOOST_AUTO_TEST_CASE(util_FormatMoney)
+{
+ BOOST_CHECK_EQUAL(FormatMoney(0, false), "0.00");
+ BOOST_CHECK_EQUAL(FormatMoney((COIN/10000)*123456789, false), "12345.6789");
+ BOOST_CHECK_EQUAL(FormatMoney(COIN, true), "+1.00");
+ BOOST_CHECK_EQUAL(FormatMoney(-COIN, false), "-1.00");
+ BOOST_CHECK_EQUAL(FormatMoney(-COIN, true), "-1.00");
+
+ BOOST_CHECK_EQUAL(FormatMoney(COIN*100000000, false), "100000000.00");
+ BOOST_CHECK_EQUAL(FormatMoney(COIN*10000000, false), "10000000.00");
+ BOOST_CHECK_EQUAL(FormatMoney(COIN*1000000, false), "1000000.00");
+ BOOST_CHECK_EQUAL(FormatMoney(COIN*100000, false), "100000.00");
+ BOOST_CHECK_EQUAL(FormatMoney(COIN*10000, false), "10000.00");
+ BOOST_CHECK_EQUAL(FormatMoney(COIN*1000, false), "1000.00");
+ BOOST_CHECK_EQUAL(FormatMoney(COIN*100, false), "100.00");
+ BOOST_CHECK_EQUAL(FormatMoney(COIN*10, false), "10.00");
+ BOOST_CHECK_EQUAL(FormatMoney(COIN, false), "1.00");
+ BOOST_CHECK_EQUAL(FormatMoney(COIN/10, false), "0.10");
+ BOOST_CHECK_EQUAL(FormatMoney(COIN/100, false), "0.01");
+ BOOST_CHECK_EQUAL(FormatMoney(COIN/1000, false), "0.001");
+ BOOST_CHECK_EQUAL(FormatMoney(COIN/10000, false), "0.0001");
+ BOOST_CHECK_EQUAL(FormatMoney(COIN/100000, false), "0.00001");
+ BOOST_CHECK_EQUAL(FormatMoney(COIN/1000000, false), "0.000001");
+ BOOST_CHECK_EQUAL(FormatMoney(COIN/10000000, false), "0.0000001");
+ BOOST_CHECK_EQUAL(FormatMoney(COIN/100000000, false), "0.00000001");
+}
+
+BOOST_AUTO_TEST_CASE(util_ParseMoney)
+{
+ int64 ret = 0;
+ BOOST_CHECK(ParseMoney("0.0", ret));
+ BOOST_CHECK_EQUAL(ret, 0);
+
+ BOOST_CHECK(ParseMoney("12345.6789", ret));
+ BOOST_CHECK_EQUAL(ret, (COIN/10000)*123456789);
+
+ BOOST_CHECK(ParseMoney("100000000.00", ret));
+ BOOST_CHECK_EQUAL(ret, COIN*100000000);
+ BOOST_CHECK(ParseMoney("10000000.00", ret));
+ BOOST_CHECK_EQUAL(ret, COIN*10000000);
+ BOOST_CHECK(ParseMoney("1000000.00", ret));
+ BOOST_CHECK_EQUAL(ret, COIN*1000000);
+ BOOST_CHECK(ParseMoney("100000.00", ret));
+ BOOST_CHECK_EQUAL(ret, COIN*100000);
+ BOOST_CHECK(ParseMoney("10000.00", ret));
+ BOOST_CHECK_EQUAL(ret, COIN*10000);
+ BOOST_CHECK(ParseMoney("1000.00", ret));
+ BOOST_CHECK_EQUAL(ret, COIN*1000);
+ BOOST_CHECK(ParseMoney("100.00", ret));
+ BOOST_CHECK_EQUAL(ret, COIN*100);
+ BOOST_CHECK(ParseMoney("10.00", ret));
+ BOOST_CHECK_EQUAL(ret, COIN*10);
+ BOOST_CHECK(ParseMoney("1.00", ret));
+ BOOST_CHECK_EQUAL(ret, COIN);
+ BOOST_CHECK(ParseMoney("0.1", ret));
+ BOOST_CHECK_EQUAL(ret, COIN/10);
+ BOOST_CHECK(ParseMoney("0.01", ret));
+ BOOST_CHECK_EQUAL(ret, COIN/100);
+ BOOST_CHECK(ParseMoney("0.001", ret));
+ BOOST_CHECK_EQUAL(ret, COIN/1000);
+ BOOST_CHECK(ParseMoney("0.0001", ret));
+ BOOST_CHECK_EQUAL(ret, COIN/10000);
+ BOOST_CHECK(ParseMoney("0.00001", ret));
+ BOOST_CHECK_EQUAL(ret, COIN/100000);
+ BOOST_CHECK(ParseMoney("0.000001", ret));
+ BOOST_CHECK_EQUAL(ret, COIN/1000000);
+ BOOST_CHECK(ParseMoney("0.0000001", ret));
+ BOOST_CHECK_EQUAL(ret, COIN/10000000);
+ BOOST_CHECK(ParseMoney("0.00000001", ret));
+ BOOST_CHECK_EQUAL(ret, COIN/100000000);
+
+ // Attempted 63 bit overflow should fail
+ BOOST_CHECK(!ParseMoney("92233720368.54775808", ret));
+}
+
+BOOST_AUTO_TEST_SUITE_END()