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.cpp177
-rw-r--r--src/test/base58_tests.cpp4
-rw-r--r--src/test/base64_tests.cpp12
-rw-r--r--src/test/getarg_tests.cpp167
-rw-r--r--src/test/key_tests.cpp138
-rw-r--r--src/test/miner_tests.cpp2
-rw-r--r--src/test/mruset_tests.cpp90
-rw-r--r--src/test/multisig_tests.cpp296
-rw-r--r--src/test/rpc_tests.cpp74
-rw-r--r--src/test/script_P2SH_tests.cpp330
-rw-r--r--src/test/script_tests.cpp48
-rw-r--r--src/test/sigopcount_tests.cpp60
-rw-r--r--src/test/test_bitcoin.cpp30
-rw-r--r--src/test/transaction_tests.cpp99
-rw-r--r--src/test/uint160_tests.cpp2
-rw-r--r--src/test/uint256_tests.cpp2
-rw-r--r--src/test/util_tests.cpp38
18 files changed, 1535 insertions, 68 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
index e60bb742dd..0b89414048 100644
--- a/src/test/DoS_tests.cpp
+++ b/src/test/DoS_tests.cpp
@@ -1,47 +1,62 @@
//
// 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 "../net.h"
-#include "../util.h"
+#include "main.h"
+#include "wallet.h"
+#include "net.h"
+#include "util.h"
-using namespace std;
+#include <stdint.h>
+
+// Tests this internal-to-main.cpp method:
+extern void AddOrphanTx(const CDataStream& vMsg);
+extern int LimitOrphanTxSize(int nMaxOrphans);
+extern std::map<uint256, CDataStream*> mapOrphanTransactions;
+extern std::multimap<uint256, CDataStream*> mapOrphanTransactionsByPrev;
+
+CService ip(uint32_t i)
+{
+ struct in_addr s;
+ s.s_addr = i;
+ return CService(CNetAddr(s), GetDefaultPort());
+}
BOOST_AUTO_TEST_SUITE(DoS_tests)
BOOST_AUTO_TEST_CASE(DoS_banning)
{
CNode::ClearBanned();
- CAddress addr1(0xa0b0c001);
+ CAddress addr1(ip(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
+ BOOST_CHECK(CNode::IsBanned(addr1));
+ BOOST_CHECK(!CNode::IsBanned(ip(0xa0b0c001|0x0000ff00))); // Different ip, not banned
- CAddress addr2(0xa0b0c002);
+ CAddress addr2(ip(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
+ BOOST_CHECK(!CNode::IsBanned(addr2)); // 2 not banned yet...
+ BOOST_CHECK(CNode::IsBanned(addr1)); // ... but 1 still should be
dummyNode2.Misbehaving(50);
- BOOST_CHECK(CNode::IsBanned(addr2.ip));
+ BOOST_CHECK(CNode::IsBanned(addr2));
}
BOOST_AUTO_TEST_CASE(DoS_banscore)
{
CNode::ClearBanned();
mapArgs["-banscore"] = "111"; // because 11 is my favorite number
- CAddress addr1(0xa0b0c001);
+ CAddress addr1(ip(0xa0b0c001));
CNode dummyNode1(INVALID_SOCKET, addr1, true);
dummyNode1.Misbehaving(100);
- BOOST_CHECK(!CNode::IsBanned(addr1.ip));
+ BOOST_CHECK(!CNode::IsBanned(addr1));
dummyNode1.Misbehaving(10);
- BOOST_CHECK(!CNode::IsBanned(addr1.ip));
+ BOOST_CHECK(!CNode::IsBanned(addr1));
dummyNode1.Misbehaving(1);
- BOOST_CHECK(CNode::IsBanned(addr1.ip));
+ BOOST_CHECK(CNode::IsBanned(addr1));
mapArgs["-banscore"] = "100";
}
@@ -51,18 +66,140 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
int64 nStartTime = GetTime();
SetMockTime(nStartTime); // Overrides future calls to GetTime()
- CAddress addr(0xa0b0c001);
+ CAddress addr(ip(0xa0b0c001));
CNode dummyNode(INVALID_SOCKET, addr, true);
dummyNode.Misbehaving(100);
- BOOST_CHECK(CNode::IsBanned(addr.ip));
+ BOOST_CHECK(CNode::IsBanned(addr));
SetMockTime(nStartTime+60*60);
- BOOST_CHECK(CNode::IsBanned(addr.ip));
+ BOOST_CHECK(CNode::IsBanned(addr));
SetMockTime(nStartTime+60*60*24+1);
- BOOST_CHECK(!CNode::IsBanned(addr.ip));
-}
+ BOOST_CHECK(!CNode::IsBanned(addr));
+}
+
+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));
+
+}
+
+static uint256 RandomHash()
+{
+ std::vector<unsigned char> randbytes(32);
+ RAND_bytes(&randbytes[0], 32);
+ uint256 randomhash(randbytes);
+ return randomhash;
+}
+
+CTransaction RandomOrphan()
+{
+ std::map<uint256, CDataStream*>::iterator it;
+ it = mapOrphanTransactions.lower_bound(RandomHash());
+ if (it == mapOrphanTransactions.end())
+ it = mapOrphanTransactions.begin();
+ const CDataStream* pvMsg = it->second;
+ CTransaction tx;
+ CDataStream(*pvMsg) >> tx;
+ return tx;
+}
+
+BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
+{
+ CKey key;
+ key.MakeNewKey(true);
+ CBasicKeyStore keystore;
+ keystore.AddKey(key);
+
+ // 50 orphan transactions:
+ for (int i = 0; i < 50; i++)
+ {
+ CTransaction tx;
+ tx.vin.resize(1);
+ tx.vin[0].prevout.n = 0;
+ tx.vin[0].prevout.hash = RandomHash();
+ tx.vin[0].scriptSig << OP_1;
+ tx.vout.resize(1);
+ tx.vout[0].nValue = 1*CENT;
+ tx.vout[0].scriptPubKey.SetBitcoinAddress(key.GetPubKey());
+
+ CDataStream ds;
+ ds << tx;
+ AddOrphanTx(ds);
+ }
+
+ // ... and 50 that depend on other orphans:
+ for (int i = 0; i < 50; i++)
+ {
+ CTransaction txPrev = RandomOrphan();
+
+ CTransaction tx;
+ tx.vin.resize(1);
+ tx.vin[0].prevout.n = 0;
+ tx.vin[0].prevout.hash = txPrev.GetHash();
+ tx.vout.resize(1);
+ tx.vout[0].nValue = 1*CENT;
+ tx.vout[0].scriptPubKey.SetBitcoinAddress(key.GetPubKey());
+ SignSignature(keystore, txPrev, tx, 0);
+
+ CDataStream ds;
+ ds << tx;
+ AddOrphanTx(ds);
+ }
+
+ // Test LimitOrphanTxSize() function:
+ LimitOrphanTxSize(40);
+ BOOST_CHECK(mapOrphanTransactions.size() <= 40);
+ LimitOrphanTxSize(10);
+ BOOST_CHECK(mapOrphanTransactions.size() <= 10);
+ LimitOrphanTxSize(0);
+ BOOST_CHECK(mapOrphanTransactions.empty());
+ BOOST_CHECK(mapOrphanTransactionsByPrev.empty());
+}
BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/test/base58_tests.cpp b/src/test/base58_tests.cpp
index c7fa74e96d..d52ac74982 100644
--- a/src/test/base58_tests.cpp
+++ b/src/test/base58_tests.cpp
@@ -1,6 +1,8 @@
#include <boost/test/unit_test.hpp>
-#include "../util.h"
+#include "main.h"
+#include "wallet.h"
+#include "util.h"
BOOST_AUTO_TEST_SUITE(base58_tests)
diff --git a/src/test/base64_tests.cpp b/src/test/base64_tests.cpp
index f30f7f8936..fff30ef5eb 100644
--- a/src/test/base64_tests.cpp
+++ b/src/test/base64_tests.cpp
@@ -1,18 +1,20 @@
#include <boost/test/unit_test.hpp>
-#include "../util.h"
+#include "main.h"
+#include "wallet.h"
+#include "util.h"
BOOST_AUTO_TEST_SUITE(base64_tests)
BOOST_AUTO_TEST_CASE(base64_testvectors)
{
- static const string vstrIn[] = {"","f","fo","foo","foob","fooba","foobar"};
- static const string vstrOut[] = {"","Zg==","Zm8=","Zm9v","Zm9vYg==","Zm9vYmE=","Zm9vYmFy"};
+ 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++)
{
- string strEnc = EncodeBase64(vstrIn[i]);
+ std::string strEnc = EncodeBase64(vstrIn[i]);
BOOST_CHECK(strEnc == vstrOut[i]);
- string strDec = DecodeBase64(strEnc);
+ std::string strDec = DecodeBase64(strEnc);
BOOST_CHECK(strDec == vstrIn[i]);
}
}
diff --git a/src/test/getarg_tests.cpp b/src/test/getarg_tests.cpp
new file mode 100644
index 0000000000..78953d296f
--- /dev/null
+++ b/src/test/getarg_tests.cpp
@@ -0,0 +1,167 @@
+#include <boost/algorithm/string.hpp>
+#include <boost/foreach.hpp>
+#include <boost/test/unit_test.hpp>
+
+#include "util.h"
+
+BOOST_AUTO_TEST_SUITE(getarg_tests)
+
+static void
+ResetArgs(const std::string& strArg)
+{
+ std::vector<std::string> vecArg;
+ boost::split(vecArg, strArg, boost::is_space(), boost::token_compress_on);
+
+ // Insert dummy executable name:
+ vecArg.insert(vecArg.begin(), "testbitcoin");
+
+ // Convert to char*:
+ std::vector<const char*> vecChar;
+ BOOST_FOREACH(std::string& s, vecArg)
+ vecChar.push_back(s.c_str());
+
+ ParseParameters(vecChar.size(), &vecChar[0]);
+}
+
+BOOST_AUTO_TEST_CASE(boolarg)
+{
+ ResetArgs("-foo");
+ BOOST_CHECK(GetBoolArg("-foo"));
+ BOOST_CHECK(GetBoolArg("-foo", false));
+ BOOST_CHECK(GetBoolArg("-foo", true));
+
+ BOOST_CHECK(!GetBoolArg("-fo"));
+ BOOST_CHECK(!GetBoolArg("-fo", false));
+ BOOST_CHECK(GetBoolArg("-fo", true));
+
+ BOOST_CHECK(!GetBoolArg("-fooo"));
+ BOOST_CHECK(!GetBoolArg("-fooo", false));
+ BOOST_CHECK(GetBoolArg("-fooo", true));
+
+ ResetArgs("-foo=0");
+ BOOST_CHECK(!GetBoolArg("-foo"));
+ BOOST_CHECK(!GetBoolArg("-foo", false));
+ BOOST_CHECK(!GetBoolArg("-foo", true));
+
+ ResetArgs("-foo=1");
+ BOOST_CHECK(GetBoolArg("-foo"));
+ BOOST_CHECK(GetBoolArg("-foo", false));
+ BOOST_CHECK(GetBoolArg("-foo", true));
+
+ // New 0.6 feature: auto-map -nosomething to !-something:
+ ResetArgs("-nofoo");
+ BOOST_CHECK(!GetBoolArg("-foo"));
+ BOOST_CHECK(!GetBoolArg("-foo", false));
+ BOOST_CHECK(!GetBoolArg("-foo", true));
+
+ ResetArgs("-nofoo=1");
+ BOOST_CHECK(!GetBoolArg("-foo"));
+ BOOST_CHECK(!GetBoolArg("-foo", false));
+ BOOST_CHECK(!GetBoolArg("-foo", true));
+
+ ResetArgs("-foo -nofoo"); // -foo should win
+ BOOST_CHECK(GetBoolArg("-foo"));
+ BOOST_CHECK(GetBoolArg("-foo", false));
+ BOOST_CHECK(GetBoolArg("-foo", true));
+
+ ResetArgs("-foo=1 -nofoo=1"); // -foo should win
+ BOOST_CHECK(GetBoolArg("-foo"));
+ BOOST_CHECK(GetBoolArg("-foo", false));
+ BOOST_CHECK(GetBoolArg("-foo", true));
+
+ ResetArgs("-foo=0 -nofoo=0"); // -foo should win
+ BOOST_CHECK(!GetBoolArg("-foo"));
+ BOOST_CHECK(!GetBoolArg("-foo", false));
+ BOOST_CHECK(!GetBoolArg("-foo", true));
+
+ // New 0.6 feature: treat -- same as -:
+ ResetArgs("--foo=1");
+ BOOST_CHECK(GetBoolArg("-foo"));
+ BOOST_CHECK(GetBoolArg("-foo", false));
+ BOOST_CHECK(GetBoolArg("-foo", true));
+
+ ResetArgs("--nofoo=1");
+ BOOST_CHECK(!GetBoolArg("-foo"));
+ BOOST_CHECK(!GetBoolArg("-foo", false));
+ BOOST_CHECK(!GetBoolArg("-foo", true));
+
+}
+
+BOOST_AUTO_TEST_CASE(stringarg)
+{
+ ResetArgs("");
+ BOOST_CHECK_EQUAL(GetArg("-foo", ""), "");
+ BOOST_CHECK_EQUAL(GetArg("-foo", "eleven"), "eleven");
+
+ ResetArgs("-foo -bar");
+ BOOST_CHECK_EQUAL(GetArg("-foo", ""), "");
+ BOOST_CHECK_EQUAL(GetArg("-foo", "eleven"), "");
+
+ ResetArgs("-foo=");
+ BOOST_CHECK_EQUAL(GetArg("-foo", ""), "");
+ BOOST_CHECK_EQUAL(GetArg("-foo", "eleven"), "");
+
+ ResetArgs("-foo=11");
+ BOOST_CHECK_EQUAL(GetArg("-foo", ""), "11");
+ BOOST_CHECK_EQUAL(GetArg("-foo", "eleven"), "11");
+
+ ResetArgs("-foo=eleven");
+ BOOST_CHECK_EQUAL(GetArg("-foo", ""), "eleven");
+ BOOST_CHECK_EQUAL(GetArg("-foo", "eleven"), "eleven");
+
+}
+
+BOOST_AUTO_TEST_CASE(intarg)
+{
+ ResetArgs("");
+ BOOST_CHECK_EQUAL(GetArg("-foo", 11), 11);
+ BOOST_CHECK_EQUAL(GetArg("-foo", 0), 0);
+
+ ResetArgs("-foo -bar");
+ BOOST_CHECK_EQUAL(GetArg("-foo", 11), 0);
+ BOOST_CHECK_EQUAL(GetArg("-bar", 11), 0);
+
+ ResetArgs("-foo=11 -bar=12");
+ BOOST_CHECK_EQUAL(GetArg("-foo", 0), 11);
+ BOOST_CHECK_EQUAL(GetArg("-bar", 11), 12);
+
+ ResetArgs("-foo=NaN -bar=NotANumber");
+ BOOST_CHECK_EQUAL(GetArg("-foo", 1), 0);
+ BOOST_CHECK_EQUAL(GetArg("-bar", 11), 0);
+}
+
+BOOST_AUTO_TEST_CASE(doubledash)
+{
+ ResetArgs("--foo");
+ BOOST_CHECK_EQUAL(GetBoolArg("-foo"), true);
+
+ ResetArgs("--foo=verbose --bar=1");
+ BOOST_CHECK_EQUAL(GetArg("-foo", ""), "verbose");
+ BOOST_CHECK_EQUAL(GetArg("-bar", 0), 1);
+}
+
+BOOST_AUTO_TEST_CASE(boolargno)
+{
+ ResetArgs("-nofoo");
+ BOOST_CHECK(!GetBoolArg("-foo"));
+ BOOST_CHECK(!GetBoolArg("-foo", true));
+ BOOST_CHECK(!GetBoolArg("-foo", false));
+
+ ResetArgs("-nofoo=1");
+ BOOST_CHECK(!GetBoolArg("-foo"));
+ BOOST_CHECK(!GetBoolArg("-foo", true));
+ BOOST_CHECK(!GetBoolArg("-foo", false));
+
+ ResetArgs("-nofoo=0");
+ BOOST_CHECK(GetBoolArg("-foo"));
+ BOOST_CHECK(GetBoolArg("-foo", true));
+ BOOST_CHECK(GetBoolArg("-foo", false));
+
+ ResetArgs("-foo --nofoo");
+ BOOST_CHECK(GetBoolArg("-foo"));
+
+ ResetArgs("-nofoo -foo"); // foo always wins:
+ BOOST_CHECK(GetBoolArg("-foo"));
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/test/key_tests.cpp b/src/test/key_tests.cpp
new file mode 100644
index 0000000000..bc8759b6fa
--- /dev/null
+++ b/src/test/key_tests.cpp
@@ -0,0 +1,138 @@
+#include <boost/test/unit_test.hpp>
+
+#include <string>
+#include <vector>
+
+#include "key.h"
+#include "base58.h"
+#include "uint256.h"
+#include "util.h"
+
+using namespace std;
+
+static const string strSecret1 ("5HxWvvfubhXpYYpS3tJkw6fq9jE9j18THftkZjHHfmFiWtmAbrj");
+static const string strSecret2 ("5KC4ejrDjv152FGwP386VD1i2NYc5KkfSMyv1nGy1VGDxGHqVY3");
+static const string strSecret1C("Kwr371tjA9u2rFSMZjTNun2PXXP3WPZu2afRHTcta6KxEUdm1vEw");
+static const string strSecret2C("L3Hq7a8FEQwJkW1M2GNKDW28546Vp5miewcCzSqUD9kCAXrJdS3g");
+
+#ifdef KEY_TESTS_DUMPINFO
+void dumpKeyInfo(uint256 privkey)
+{
+ CSecret secret;
+ secret.resize(32);
+ memcpy(&secret[0], &privkey, 32);
+ vector<unsigned char> sec;
+ sec.resize(32);
+ memcpy(&sec[0], &secret[0], 32);
+ printf(" * secret (hex): %s\n", HexStr(sec).c_str());
+
+ for (int nCompressed=0; nCompressed<2; nCompressed++)
+ {
+ bool fCompressed = nCompressed == 1;
+ printf(" * %s:\n", fCompressed ? "compressed" : "uncompressed");
+ CBitcoinSecret bsecret;
+ bsecret.SetSecret(secret, fCompressed);
+ printf(" * secret (base58): %s\n", bsecret.ToString().c_str());
+ CKey key;
+ key.SetSecret(secret, fCompressed);
+ vector<unsigned char> vchPubKey = key.GetPubKey();
+ printf(" * pubkey (hex): %s\n", HexStr(vchPubKey).c_str());
+ printf(" * address (base58): %s\n", CBitcoinAddress(vchPubKey).ToString().c_str());
+ }
+}
+#endif
+
+
+BOOST_AUTO_TEST_SUITE(key_tests)
+
+BOOST_AUTO_TEST_CASE(key_test1)
+{
+ CBitcoinSecret bsecret1, bsecret2, bsecret1C, bsecret2C;
+ bsecret1.SetString (strSecret1);
+ bsecret2.SetString (strSecret2);
+ bsecret1C.SetString(strSecret1C);
+ bsecret2C.SetString(strSecret2C);
+
+ bool fCompressed;
+ CSecret secret1 = bsecret1.GetSecret (fCompressed);
+ BOOST_CHECK(fCompressed == false);
+ CSecret secret2 = bsecret2.GetSecret (fCompressed);
+ BOOST_CHECK(fCompressed == false);
+ CSecret secret1C = bsecret1C.GetSecret(fCompressed);
+ BOOST_CHECK(fCompressed == true);
+ CSecret secret2C = bsecret2C.GetSecret(fCompressed);
+ BOOST_CHECK(fCompressed == true);
+
+ BOOST_CHECK(secret1 == secret1C);
+ BOOST_CHECK(secret2 == secret2C);
+
+ CKey key1, key2, key1C, key2C;
+ key1.SetSecret(secret1, false);
+ key2.SetSecret(secret2, false);
+ key1C.SetSecret(secret1, true);
+ key2C.SetSecret(secret2, true);
+
+ BOOST_CHECK(CBitcoinAddress(key1.GetPubKey ()).ToString() == "1QFqqMUD55ZV3PJEJZtaKCsQmjLT6JkjvJ");
+ BOOST_CHECK(CBitcoinAddress(key2.GetPubKey ()).ToString() == "1F5y5E5FMc5YzdJtB9hLaUe43GDxEKXENJ");
+ BOOST_CHECK(CBitcoinAddress(key1C.GetPubKey()).ToString() == "1NoJrossxPBKfCHuJXT4HadJrXRE9Fxiqs");
+ BOOST_CHECK(CBitcoinAddress(key2C.GetPubKey()).ToString() == "1CRj2HyM1CXWzHAXLQtiGLyggNT9WQqsDs");
+
+ for (int n=0; n<16; n++)
+ {
+ string strMsg = strprintf("Very secret message %i: 11", n);
+ uint256 hashMsg = Hash(strMsg.begin(), strMsg.end());
+
+ // normal signatures
+
+ vector<unsigned char> sign1, sign2, sign1C, sign2C;
+
+ BOOST_CHECK(key1.Sign (hashMsg, sign1));
+ BOOST_CHECK(key2.Sign (hashMsg, sign2));
+ BOOST_CHECK(key1C.Sign(hashMsg, sign1C));
+ BOOST_CHECK(key2C.Sign(hashMsg, sign2C));
+
+ BOOST_CHECK( key1.Verify(hashMsg, sign1));
+ BOOST_CHECK(!key1.Verify(hashMsg, sign2));
+ BOOST_CHECK( key1.Verify(hashMsg, sign1C));
+ BOOST_CHECK(!key1.Verify(hashMsg, sign2C));
+
+ BOOST_CHECK(!key2.Verify(hashMsg, sign1));
+ BOOST_CHECK( key2.Verify(hashMsg, sign2));
+ BOOST_CHECK(!key2.Verify(hashMsg, sign1C));
+ BOOST_CHECK( key2.Verify(hashMsg, sign2C));
+
+ BOOST_CHECK( key1C.Verify(hashMsg, sign1));
+ BOOST_CHECK(!key1C.Verify(hashMsg, sign2));
+ BOOST_CHECK( key1C.Verify(hashMsg, sign1C));
+ BOOST_CHECK(!key1C.Verify(hashMsg, sign2C));
+
+ BOOST_CHECK(!key2C.Verify(hashMsg, sign1));
+ BOOST_CHECK( key2C.Verify(hashMsg, sign2));
+ BOOST_CHECK(!key2C.Verify(hashMsg, sign1C));
+ BOOST_CHECK( key2C.Verify(hashMsg, sign2C));
+
+ // compact signatures (with key recovery)
+
+ vector<unsigned char> csign1, csign2, csign1C, csign2C;
+
+ BOOST_CHECK(key1.SignCompact (hashMsg, csign1));
+ BOOST_CHECK(key2.SignCompact (hashMsg, csign2));
+ BOOST_CHECK(key1C.SignCompact(hashMsg, csign1C));
+ BOOST_CHECK(key2C.SignCompact(hashMsg, csign2C));
+
+ CKey rkey1, rkey2, rkey1C, rkey2C;
+
+ BOOST_CHECK(rkey1.SetCompactSignature (hashMsg, csign1));
+ BOOST_CHECK(rkey2.SetCompactSignature (hashMsg, csign2));
+ BOOST_CHECK(rkey1C.SetCompactSignature(hashMsg, csign1C));
+ BOOST_CHECK(rkey2C.SetCompactSignature(hashMsg, csign2C));
+
+
+ BOOST_CHECK(rkey1.GetPubKey() == key1.GetPubKey());
+ BOOST_CHECK(rkey2.GetPubKey() == key2.GetPubKey());
+ BOOST_CHECK(rkey1C.GetPubKey() == key1C.GetPubKey());
+ BOOST_CHECK(rkey2C.GetPubKey() == key2C.GetPubKey());
+ }
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/test/miner_tests.cpp b/src/test/miner_tests.cpp
index 8fc534762a..d4abd6d2a7 100644
--- a/src/test/miner_tests.cpp
+++ b/src/test/miner_tests.cpp
@@ -1,6 +1,6 @@
#include <boost/test/unit_test.hpp>
-#include "../uint256.h"
+#include "uint256.h"
extern void SHA256Transform(void* pstate, void* pinput, const void* pinit);
diff --git a/src/test/mruset_tests.cpp b/src/test/mruset_tests.cpp
new file mode 100644
index 0000000000..ca5a1f1b12
--- /dev/null
+++ b/src/test/mruset_tests.cpp
@@ -0,0 +1,90 @@
+#include <boost/test/unit_test.hpp>
+
+using namespace std;
+
+#include "mruset.h"
+#include "util.h"
+
+#define NUM_TESTS 16
+#define MAX_SIZE 100
+
+class mrutester
+{
+private:
+ mruset<int> mru;
+ std::set<int> set;
+
+public:
+ mrutester() { mru.max_size(MAX_SIZE); }
+ int size() const { return set.size(); }
+
+ void insert(int n)
+ {
+ mru.insert(n);
+ set.insert(n);
+ BOOST_CHECK(mru == set);
+ }
+};
+
+BOOST_AUTO_TEST_SUITE(mruset_tests)
+
+// Test that an mruset behaves like a set, as long as no more than MAX_SIZE elements are in it
+BOOST_AUTO_TEST_CASE(mruset_like_set)
+{
+
+ for (int nTest=0; nTest<NUM_TESTS; nTest++)
+ {
+ mrutester tester;
+ while (tester.size() < MAX_SIZE)
+ tester.insert(GetRandInt(2 * MAX_SIZE));
+ }
+
+}
+
+// Test that an mruset's size never exceeds its max_size
+BOOST_AUTO_TEST_CASE(mruset_limited_size)
+{
+ for (int nTest=0; nTest<NUM_TESTS; nTest++)
+ {
+ mruset<int> mru(MAX_SIZE);
+ for (int nAction=0; nAction<3*MAX_SIZE; nAction++)
+ {
+ int n = GetRandInt(2 * MAX_SIZE);
+ mru.insert(n);
+ BOOST_CHECK(mru.size() <= MAX_SIZE);
+ }
+ }
+}
+
+// 16-bit permutation function
+int static permute(int n)
+{
+ // hexadecimals of pi; verified to be linearly independent
+ static const int table[16] = {0x243F, 0x6A88, 0x85A3, 0x08D3, 0x1319, 0x8A2E, 0x0370, 0x7344,
+ 0xA409, 0x3822, 0x299F, 0x31D0, 0x082E, 0xFA98, 0xEC4E, 0x6C89};
+
+ int ret = 0;
+ for (int bit=0; bit<16; bit++)
+ if (n & (1<<bit))
+ ret ^= table[bit];
+
+ return ret;
+}
+
+// Test that an mruset acts like a moving window, if no duplcate elements are added
+BOOST_AUTO_TEST_CASE(mruset_window)
+{
+ mruset<int> mru(MAX_SIZE);
+ for (int n=0; n<10*MAX_SIZE; n++)
+ {
+ mru.insert(permute(n));
+
+ set<int> tester;
+ for (int m=max(0,n-MAX_SIZE+1); m<=n; m++)
+ tester.insert(permute(m));
+
+ BOOST_CHECK(mru == tester);
+ }
+}
+
+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..8ae9290fcc
--- /dev/null
+++ b/src/test/multisig_tests.cpp
@@ -0,0 +1,296 @@
+#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,
+ bool fValidatePayToScriptHash, int nHashType);
+
+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(true);
+
+ 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;
+
+ // 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, true, 0));
+
+ 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, true, 0), 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, true, 0), 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, true, 0), strprintf("a|b: %d", i));
+ else
+ BOOST_CHECK_MESSAGE(!VerifyScript(s, a_or_b, txTo[1], 0, true, 0), strprintf("a|b: %d", i));
+ }
+ s.clear();
+ s << OP_0 << OP_0;
+ BOOST_CHECK(!VerifyScript(s, a_or_b, txTo[1], 0, true, 0));
+ s.clear();
+ s << OP_0 << OP_1;
+ BOOST_CHECK(!VerifyScript(s, a_or_b, txTo[1], 0, true, 0));
+
+
+ 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, true, 0), strprintf("escrow 1: %d %d", i, j));
+ else
+ BOOST_CHECK_MESSAGE(!VerifyScript(s, escrow, txTo[2], 0, true, 0), 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(true);
+
+ 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, partialkeystore;
+ CKey key[3];
+ CBitcoinAddress keyaddr[3];
+ for (int i = 0; i < 3; i++)
+ {
+ key[i].MakeNewKey(true);
+ keystore.AddKey(key[i]);
+ keyaddr[i].SetPubKey(key[i].GetPubKey());
+ }
+ partialkeystore.AddKey(key[0]);
+
+ {
+ 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, 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, 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, addr));
+ BOOST_CHECK(IsMine(keystore, s));
+ BOOST_CHECK(!IsMine(emptykeystore, s));
+ BOOST_CHECK(!IsMine(partialkeystore, 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, 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));
+ BOOST_CHECK(!IsMine(partialkeystore, 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(true);
+ 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/rpc_tests.cpp b/src/test/rpc_tests.cpp
new file mode 100644
index 0000000000..87462f765b
--- /dev/null
+++ b/src/test/rpc_tests.cpp
@@ -0,0 +1,74 @@
+#include <boost/test/unit_test.hpp>
+#include <boost/foreach.hpp>
+
+#include "base58.h"
+#include "util.h"
+#include "json/json_spirit_reader_template.h"
+#include "json/json_spirit_writer_template.h"
+#include "json/json_spirit_utils.h"
+
+using namespace std;
+using namespace json_spirit;
+
+typedef Value(*rpcfn_type)(const Array& params, bool fHelp);
+extern map<string, rpcfn_type> mapCallTable;
+
+BOOST_AUTO_TEST_SUITE(rpc_tests)
+
+static Array
+createArgs(int nRequired, const char* address1=NULL, const char* address2=NULL)
+{
+ Array result;
+ result.push_back(nRequired);
+ Array addresses;
+ if (address1) addresses.push_back(address1);
+ if (address2) addresses.push_back(address1);
+ result.push_back(addresses);
+ return result;
+}
+
+// This can be removed this when addmultisigaddress is enabled on main net:
+struct TestNetFixture
+{
+ TestNetFixture() { fTestNet = true; }
+ ~TestNetFixture() { fTestNet = false; }
+};
+
+BOOST_FIXTURE_TEST_CASE(rpc_addmultisig, TestNetFixture)
+{
+ rpcfn_type addmultisig = mapCallTable["addmultisigaddress"];
+
+ // 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+2, address1Hex+sizeof(address1Hex)); // first byte missing
+ BOOST_CHECK_THROW(addmultisig(createArgs(2, short2.c_str()), false), runtime_error);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/test/script_P2SH_tests.cpp b/src/test/script_P2SH_tests.cpp
new file mode 100644
index 0000000000..aa72c00092
--- /dev/null
+++ b/src/test/script_P2SH_tests.cpp
@@ -0,0 +1,330 @@
+#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,
+ bool fValidatePayToScriptHash, int nHashType);
+
+// Helpers:
+static std::vector<unsigned char>
+Serialize(const CScript& s)
+{
+ std::vector<unsigned char> sSerialized(s);
+ return sSerialized;
+}
+
+static bool
+Verify(const CScript& scriptSig, const CScript& scriptPubKey, bool fStrict)
+{
+ // Create dummy to/from transactions:
+ CTransaction txFrom;
+ txFrom.vout.resize(1);
+ txFrom.vout[0].scriptPubKey = scriptPubKey;
+
+ 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 = scriptSig;
+ txTo.vout[0].nValue = 1;
+
+ return VerifyScript(scriptSig, scriptPubKey, txTo, 0, fStrict, 0);
+}
+
+
+BOOST_AUTO_TEST_SUITE(script_P2SH_tests)
+
+BOOST_AUTO_TEST_CASE(sign)
+{
+ // Pay-to-script-hash looks like this:
+ // scriptSig: <sig> <sig...> <serialized_script>
+ // scriptPubKey: HASH160 <hash> EQUAL
+
+ // 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(true);
+ keystore.AddKey(key[i]);
+ }
+
+ // 8 Scripts: checking all combinations of
+ // different keys, straight/P2SH, 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];
+ for (int i = 0; i < 4; i++)
+ {
+ keystore.AddCScript(standardScripts[i]);
+ evalScripts[i].SetPayToScriptHash(standardScripts[i]);
+ }
+
+ 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;
+ bool sigOK = VerifySignature(txFrom, txTo[i], 0, true, 0);
+ 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(norecurse)
+{
+ // Make sure only the outer pay-to-script-hash does the
+ // extra-validation thing:
+ CScript invalidAsScript;
+ invalidAsScript << OP_INVALIDOPCODE << OP_INVALIDOPCODE;
+
+ CScript p2sh;
+ p2sh.SetPayToScriptHash(invalidAsScript);
+
+ CScript scriptSig;
+ scriptSig << Serialize(invalidAsScript);
+
+ // Should not verify, because it will try to execute OP_INVALIDOPCODE
+ BOOST_CHECK(!Verify(scriptSig, p2sh, true));
+
+ // Try to recurse, and verification should succeed because
+ // the inner HASH160 <> EQUAL should only check the hash:
+ CScript p2sh2;
+ p2sh2.SetPayToScriptHash(p2sh);
+ CScript scriptSig2;
+ scriptSig2 << Serialize(invalidAsScript) << Serialize(p2sh);
+
+ BOOST_CHECK(Verify(scriptSig2, p2sh2, true));
+}
+
+BOOST_AUTO_TEST_CASE(set)
+{
+ // Test the CScript::Set* methods
+ CBasicKeyStore keystore;
+ CKey key[4];
+ std::vector<CKey> keys;
+ for (int i = 0; i < 4; i++)
+ {
+ key[i].MakeNewKey(true);
+ 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].SetPayToScriptHash(inner[i]);
+ keystore.AddCScript(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(is)
+{
+ // Test CScript::IsPayToScriptHash()
+ uint160 dummy;
+ CScript p2sh;
+ p2sh << OP_HASH160 << dummy << OP_EQUAL;
+ BOOST_CHECK(p2sh.IsPayToScriptHash());
+
+ // Not considered pay-to-script-hash if using one of the OP_PUSHDATA opcodes:
+ static const unsigned char direct[] = { OP_HASH160, 20, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, OP_EQUAL };
+ BOOST_CHECK(CScript(direct, direct+sizeof(direct)).IsPayToScriptHash());
+ static const unsigned char pushdata1[] = { OP_HASH160, OP_PUSHDATA1, 20, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, OP_EQUAL };
+ BOOST_CHECK(!CScript(pushdata1, pushdata1+sizeof(pushdata1)).IsPayToScriptHash());
+ static const unsigned char pushdata2[] = { OP_HASH160, OP_PUSHDATA2, 20,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, OP_EQUAL };
+ BOOST_CHECK(!CScript(pushdata2, pushdata2+sizeof(pushdata2)).IsPayToScriptHash());
+ static const unsigned char pushdata4[] = { OP_HASH160, OP_PUSHDATA4, 20,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, OP_EQUAL };
+ BOOST_CHECK(!CScript(pushdata4, pushdata4+sizeof(pushdata4)).IsPayToScriptHash());
+
+ CScript not_p2sh;
+ BOOST_CHECK(!not_p2sh.IsPayToScriptHash());
+
+ not_p2sh.clear(); not_p2sh << OP_HASH160 << dummy << dummy << OP_EQUAL;
+ BOOST_CHECK(!not_p2sh.IsPayToScriptHash());
+
+ not_p2sh.clear(); not_p2sh << OP_NOP << dummy << OP_EQUAL;
+ BOOST_CHECK(!not_p2sh.IsPayToScriptHash());
+
+ not_p2sh.clear(); not_p2sh << OP_HASH160 << dummy << OP_CHECKSIG;
+ BOOST_CHECK(!not_p2sh.IsPayToScriptHash());
+}
+
+BOOST_AUTO_TEST_CASE(switchover)
+{
+ // Test switchover code
+ CScript notValid;
+ notValid << OP_11 << OP_12 << OP_EQUALVERIFY;
+ CScript scriptSig;
+ scriptSig << Serialize(notValid);
+
+ CScript fund;
+ fund.SetPayToScriptHash(notValid);
+
+
+ // Validation should succeed under old rules (hash is correct):
+ BOOST_CHECK(Verify(scriptSig, fund, false));
+ // Fail under new:
+ BOOST_CHECK(!Verify(scriptSig, fund, true));
+}
+
+BOOST_AUTO_TEST_CASE(AreInputsStandard)
+{
+ std::map<uint256, std::pair<CTxIndex, CTransaction> > mapInputs;
+ CBasicKeyStore keystore;
+ CKey key[3];
+ vector<CKey> keys;
+ for (int i = 0; i < 3; i++)
+ {
+ key[i].MakeNewKey(true);
+ keystore.AddKey(key[i]);
+ keys.push_back(key[i]);
+ }
+
+ CTransaction txFrom;
+ txFrom.vout.resize(6);
+
+ // First three are standard:
+ CScript pay1; pay1.SetBitcoinAddress(key[0].GetPubKey());
+ keystore.AddCScript(pay1);
+ CScript payScriptHash1; payScriptHash1.SetPayToScriptHash(pay1);
+ CScript pay1of3; pay1of3.SetMultisig(1, keys);
+
+ txFrom.vout[0].scriptPubKey = payScriptHash1;
+ txFrom.vout[1].scriptPubKey = pay1;
+ txFrom.vout[2].scriptPubKey = pay1of3;
+
+ // Last three non-standard:
+ CScript empty;
+ keystore.AddCScript(empty);
+ txFrom.vout[3].scriptPubKey = empty;
+ // Can't use SetPayToScriptHash, it checks for the empty Script. So:
+ txFrom.vout[4].scriptPubKey << OP_HASH160 << Hash160(empty) << OP_EQUAL;
+ CScript oneOfEleven;
+ oneOfEleven << OP_1;
+ for (int i = 0; i < 11; i++)
+ oneOfEleven << key[0].GetPubKey();
+ oneOfEleven << OP_11 << OP_CHECKMULTISIG;
+ txFrom.vout[5].scriptPubKey.SetPayToScriptHash(oneOfEleven);
+
+ mapInputs[txFrom.GetHash()] = make_pair(CTxIndex(), txFrom);
+
+ CTransaction txTo;
+ txTo.vout.resize(1);
+ txTo.vout[0].scriptPubKey.SetBitcoinAddress(key[1].GetPubKey());
+
+ txTo.vin.resize(3);
+ txTo.vin[0].prevout.n = 0;
+ txTo.vin[0].prevout.hash = txFrom.GetHash();
+ BOOST_CHECK(SignSignature(keystore, txFrom, txTo, 0));
+ txTo.vin[1].prevout.n = 1;
+ txTo.vin[1].prevout.hash = txFrom.GetHash();
+ BOOST_CHECK(SignSignature(keystore, txFrom, txTo, 1));
+ txTo.vin[2].prevout.n = 2;
+ txTo.vin[2].prevout.hash = txFrom.GetHash();
+ BOOST_CHECK(SignSignature(keystore, txFrom, txTo, 2));
+
+ BOOST_CHECK(txTo.AreInputsStandard(mapInputs));
+ BOOST_CHECK_EQUAL(txTo.GetP2SHSigOpCount(mapInputs), 1);
+
+ // Make sure adding crap to the scriptSigs makes them non-standard:
+ for (int i = 0; i < 3; i++)
+ {
+ CScript t = txTo.vin[i].scriptSig;
+ txTo.vin[i].scriptSig = (CScript() << 11) + t;
+ BOOST_CHECK(!txTo.AreInputsStandard(mapInputs));
+ txTo.vin[i].scriptSig = t;
+ }
+
+ CTransaction txToNonStd;
+ txToNonStd.vout.resize(1);
+ txToNonStd.vout[0].scriptPubKey.SetBitcoinAddress(key[1].GetPubKey());
+ txToNonStd.vin.resize(2);
+ txToNonStd.vin[0].prevout.n = 4;
+ txToNonStd.vin[0].prevout.hash = txFrom.GetHash();
+ txToNonStd.vin[0].scriptSig << Serialize(empty);
+ txToNonStd.vin[1].prevout.n = 5;
+ txToNonStd.vin[1].prevout.hash = txFrom.GetHash();
+ txToNonStd.vin[1].scriptSig << OP_0 << Serialize(oneOfEleven);
+
+ BOOST_CHECK(!txToNonStd.AreInputsStandard(mapInputs));
+ BOOST_CHECK_EQUAL(txToNonStd.GetP2SHSigOpCount(mapInputs), 11);
+
+ txToNonStd.vin[0].scriptSig.clear();
+ BOOST_CHECK(!txToNonStd.AreInputsStandard(mapInputs));
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/test/script_tests.cpp b/src/test/script_tests.cpp
index 13feb86b97..493ea69d93 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,
+ bool fValidatePayToScriptHash, int nHashType);
BOOST_AUTO_TEST_SUITE(script_tests)
@@ -72,9 +72,9 @@ sign_multisig(CScript scriptPubKey, CKey key, CTransaction transaction)
BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG12)
{
CKey key1, key2, key3;
- key1.MakeNewKey();
- key2.MakeNewKey();
- key3.MakeNewKey();
+ key1.MakeNewKey(true);
+ key2.MakeNewKey(false);
+ key3.MakeNewKey(true);
CScript scriptPubKey12;
scriptPubKey12 << OP_1 << key1.GetPubKey() << key2.GetPubKey() << OP_2 << OP_CHECKMULTISIG;
@@ -91,24 +91,24 @@ 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, true, 0));
txTo12.vout[0].nValue = 2;
- BOOST_CHECK(!VerifyScript(goodsig1, scriptPubKey12, txTo12, 0, 0));
+ BOOST_CHECK(!VerifyScript(goodsig1, scriptPubKey12, txTo12, 0, true, 0));
CScript goodsig2 = sign_multisig(scriptPubKey12, key2, txTo12);
- BOOST_CHECK(VerifyScript(goodsig2, scriptPubKey12, txTo12, 0, 0));
+ BOOST_CHECK(VerifyScript(goodsig2, scriptPubKey12, txTo12, 0, true, 0));
CScript badsig1 = sign_multisig(scriptPubKey12, key3, txTo12);
- BOOST_CHECK(!VerifyScript(badsig1, scriptPubKey12, txTo12, 0, 0));
+ BOOST_CHECK(!VerifyScript(badsig1, scriptPubKey12, txTo12, 0, true, 0));
}
BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG23)
{
CKey key1, key2, key3, key4;
- key1.MakeNewKey();
- key2.MakeNewKey();
- key3.MakeNewKey();
- key4.MakeNewKey();
+ key1.MakeNewKey(true);
+ key2.MakeNewKey(false);
+ key3.MakeNewKey(true);
+ key4.MakeNewKey(false);
CScript scriptPubKey23;
scriptPubKey23 << OP_2 << key1.GetPubKey() << key2.GetPubKey() << key3.GetPubKey() << OP_3 << OP_CHECKMULTISIG;
@@ -127,46 +127,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, true, 0));
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, true, 0));
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, true, 0));
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, true, 0));
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, true, 0));
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, true, 0));
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, true, 0));
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, true, 0));
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, true, 0));
}
diff --git a/src/test/sigopcount_tests.cpp b/src/test/sigopcount_tests.cpp
new file mode 100644
index 0000000000..d301313a9d
--- /dev/null
+++ b/src/test/sigopcount_tests.cpp
@@ -0,0 +1,60 @@
+#include <vector>
+#include <boost/test/unit_test.hpp>
+#include <boost/foreach.hpp>
+
+#include "script.h"
+#include "key.h"
+
+using namespace std;
+
+// Helpers:
+static std::vector<unsigned char>
+Serialize(const CScript& s)
+{
+ std::vector<unsigned char> sSerialized(s);
+ return sSerialized;
+}
+
+BOOST_AUTO_TEST_SUITE(sigopcount_tests)
+
+BOOST_AUTO_TEST_CASE(GetSigOpCount)
+{
+ // Test CScript::GetSigOpCount()
+ CScript s1;
+ BOOST_CHECK_EQUAL(s1.GetSigOpCount(false), 0);
+ BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 0);
+
+ uint160 dummy;
+ s1 << OP_1 << dummy << dummy << OP_2 << OP_CHECKMULTISIG;
+ BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 2);
+ s1 << OP_IF << OP_CHECKSIG << OP_ENDIF;
+ BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 3);
+ BOOST_CHECK_EQUAL(s1.GetSigOpCount(false), 21);
+
+ CScript p2sh;
+ p2sh.SetPayToScriptHash(s1);
+ CScript scriptSig;
+ scriptSig << OP_0 << Serialize(s1);
+ BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(scriptSig), 3);
+
+ std::vector<CKey> keys;
+ for (int i = 0; i < 3; i++)
+ {
+ CKey k;
+ k.MakeNewKey(true);
+ keys.push_back(k);
+ }
+ CScript s2;
+ s2.SetMultisig(1, keys);
+ BOOST_CHECK_EQUAL(s2.GetSigOpCount(true), 3);
+ BOOST_CHECK_EQUAL(s2.GetSigOpCount(false), 20);
+
+ p2sh.SetPayToScriptHash(s2);
+ BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(true), 0);
+ BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(false), 0);
+ CScript scriptSig2;
+ scriptSig2 << OP_1 << dummy << dummy << Serialize(s2);
+ BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(scriptSig2), 3);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/test/test_bitcoin.cpp b/src/test/test_bitcoin.cpp
index 8863aad478..7ff7545ab4 100644
--- a/src/test/test_bitcoin.cpp
+++ b/src/test/test_bitcoin.cpp
@@ -1,21 +1,27 @@
#define BOOST_TEST_MODULE Bitcoin Test Suite
#include <boost/test/unit_test.hpp>
-#include "../main.h"
-#include "../wallet.h"
-
-#include "uint160_tests.cpp"
-#include "uint256_tests.cpp"
-#include "script_tests.cpp"
-#include "transaction_tests.cpp"
-#include "DoS_tests.cpp"
-#include "base64_tests.cpp"
-#include "util_tests.cpp"
-#include "base58_tests.cpp"
-#include "miner_tests.cpp"
+#include "main.h"
+#include "wallet.h"
CWallet* pwalletMain;
+extern bool fPrintToConsole;
+struct TestingSetup {
+ TestingSetup() {
+ fPrintToConsole = true; // don't want to write to debug.log file
+ pwalletMain = new CWallet();
+ RegisterWallet(pwalletMain);
+ }
+ ~TestingSetup()
+ {
+ delete pwalletMain;
+ pwalletMain = NULL;
+ }
+};
+
+BOOST_GLOBAL_FIXTURE(TestingSetup);
+
void Shutdown(void* parg)
{
exit(0);
diff --git a/src/test/transaction_tests.cpp b/src/test/transaction_tests.cpp
index e6eb0f054f..99163e55f2 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;
@@ -22,4 +22,99 @@ BOOST_AUTO_TEST_CASE(basic_transaction_tests)
BOOST_CHECK_MESSAGE(!tx.CheckTransaction(), "Transaction with duplicate txins should be invalid.");
}
+//
+// Helper: create two dummy transactions, each with
+// two outputs. The first has 11 and 50 CENT outputs
+// paid to a TX_PUBKEY, the second 21 and 22 CENT outputs
+// paid to a TX_PUBKEYHASH.
+//
+static std::vector<CTransaction>
+SetupDummyInputs(CBasicKeyStore& keystoreRet, MapPrevTx& inputsRet)
+{
+ std::vector<CTransaction> dummyTransactions;
+ dummyTransactions.resize(2);
+
+ // Add some keys to the keystore:
+ CKey key[4];
+ for (int i = 0; i < 4; i++)
+ {
+ key[i].MakeNewKey(i % 2);
+ keystoreRet.AddKey(key[i]);
+ }
+
+ // Create some dummy input transactions
+ dummyTransactions[0].vout.resize(2);
+ dummyTransactions[0].vout[0].nValue = 11*CENT;
+ dummyTransactions[0].vout[0].scriptPubKey << key[0].GetPubKey() << OP_CHECKSIG;
+ dummyTransactions[0].vout[1].nValue = 50*CENT;
+ dummyTransactions[0].vout[1].scriptPubKey << key[1].GetPubKey() << OP_CHECKSIG;
+ inputsRet[dummyTransactions[0].GetHash()] = make_pair(CTxIndex(), dummyTransactions[0]);
+
+ dummyTransactions[1].vout.resize(2);
+ dummyTransactions[1].vout[0].nValue = 21*CENT;
+ dummyTransactions[1].vout[0].scriptPubKey.SetBitcoinAddress(key[2].GetPubKey());
+ dummyTransactions[1].vout[1].nValue = 22*CENT;
+ dummyTransactions[1].vout[1].scriptPubKey.SetBitcoinAddress(key[3].GetPubKey());
+ inputsRet[dummyTransactions[1].GetHash()] = make_pair(CTxIndex(), dummyTransactions[1]);
+
+ return dummyTransactions;
+}
+
+BOOST_AUTO_TEST_CASE(test_Get)
+{
+ CBasicKeyStore keystore;
+ MapPrevTx dummyInputs;
+ std::vector<CTransaction> dummyTransactions = SetupDummyInputs(keystore, dummyInputs);
+
+ CTransaction t1;
+ t1.vin.resize(3);
+ t1.vin[0].prevout.hash = dummyTransactions[0].GetHash();
+ t1.vin[0].prevout.n = 1;
+ t1.vin[0].scriptSig << std::vector<unsigned char>(65, 0);
+ t1.vin[1].prevout.hash = dummyTransactions[1].GetHash();
+ t1.vin[1].prevout.n = 0;
+ t1.vin[1].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);
+ t1.vin[2].prevout.hash = dummyTransactions[1].GetHash();
+ t1.vin[2].prevout.n = 1;
+ t1.vin[2].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);
+ t1.vout.resize(2);
+ t1.vout[0].nValue = 90*CENT;
+ t1.vout[0].scriptPubKey << OP_1;
+
+ BOOST_CHECK(t1.AreInputsStandard(dummyInputs));
+ BOOST_CHECK_EQUAL(t1.GetValueIn(dummyInputs), (50+21+22)*CENT);
+
+ // Adding extra junk to the scriptSig should make it non-standard:
+ t1.vin[0].scriptSig << OP_11;
+ BOOST_CHECK(!t1.AreInputsStandard(dummyInputs));
+
+ // ... as should not having enough:
+ t1.vin[0].scriptSig = CScript();
+ BOOST_CHECK(!t1.AreInputsStandard(dummyInputs));
+}
+
+BOOST_AUTO_TEST_CASE(test_GetThrow)
+{
+ CBasicKeyStore keystore;
+ MapPrevTx dummyInputs;
+ std::vector<CTransaction> dummyTransactions = SetupDummyInputs(keystore, dummyInputs);
+
+ MapPrevTx missingInputs;
+
+ CTransaction t1;
+ t1.vin.resize(3);
+ t1.vin[0].prevout.hash = dummyTransactions[0].GetHash();
+ t1.vin[0].prevout.n = 0;
+ t1.vin[1].prevout.hash = dummyTransactions[1].GetHash();;
+ t1.vin[1].prevout.n = 0;
+ t1.vin[2].prevout.hash = dummyTransactions[1].GetHash();;
+ t1.vin[2].prevout.n = 1;
+ t1.vout.resize(2);
+ t1.vout[0].nValue = 90*CENT;
+ t1.vout[0].scriptPubKey << OP_1;
+
+ BOOST_CHECK_THROW(t1.AreInputsStandard(missingInputs), runtime_error);
+ BOOST_CHECK_THROW(t1.GetValueIn(missingInputs), runtime_error);
+}
+
BOOST_AUTO_TEST_SUITE_END()
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
index 8afc85c507..94c0c77a44 100644
--- a/src/test/util_tests.cpp
+++ b/src/test/util_tests.cpp
@@ -2,12 +2,33 @@
#include <boost/test/unit_test.hpp>
#include <boost/foreach.hpp>
-#include "../util.h"
+#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);
@@ -211,4 +232,19 @@ BOOST_AUTO_TEST_CASE(util_ParseMoney)
BOOST_CHECK(!ParseMoney("92233720368.54775808", ret));
}
+BOOST_AUTO_TEST_CASE(util_IsHex)
+{
+ BOOST_CHECK(IsHex("00"));
+ BOOST_CHECK(IsHex("00112233445566778899aabbccddeeffAABBCCDDEEFF"));
+ BOOST_CHECK(IsHex("ff"));
+ BOOST_CHECK(IsHex("FF"));
+
+ BOOST_CHECK(!IsHex(""));
+ BOOST_CHECK(!IsHex("0"));
+ BOOST_CHECK(!IsHex("a"));
+ BOOST_CHECK(!IsHex("eleven"));
+ BOOST_CHECK(!IsHex("00xx00"));
+ BOOST_CHECK(!IsHex("0x0000"));
+}
+
BOOST_AUTO_TEST_SUITE_END()