Line data Source code
1 : // Copyright (c) 2012-2013 The Bitcoin Core developers
2 : // Distributed under the MIT software license, see the accompanying
3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 :
5 : #include "pubkey.h"
6 : #include "key.h"
7 : #include "script/script.h"
8 : #include "script/standard.h"
9 : #include "uint256.h"
10 : #include "test/test_bitcoin.h"
11 :
12 : #include <vector>
13 :
14 : #include <boost/foreach.hpp>
15 : #include <boost/test/unit_test.hpp>
16 :
17 : using namespace std;
18 :
19 : // Helpers:
20 : static std::vector<unsigned char>
21 : Serialize(const CScript& s)
22 : {
23 2 : std::vector<unsigned char> sSerialized(s);
24 : return sSerialized;
25 : }
26 :
27 1 : BOOST_FIXTURE_TEST_SUITE(sigopcount_tests, BasicTestingSetup)
28 :
29 6 : BOOST_AUTO_TEST_CASE(GetSigOpCount)
30 : {
31 : // Test CScript::GetSigOpCount()
32 : CScript s1;
33 5 : BOOST_CHECK_EQUAL(s1.GetSigOpCount(false), 0U);
34 5 : BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 0U);
35 :
36 : uint160 dummy;
37 3 : s1 << OP_1 << ToByteVector(dummy) << ToByteVector(dummy) << OP_2 << OP_CHECKMULTISIG;
38 5 : BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 2U);
39 1 : s1 << OP_IF << OP_CHECKSIG << OP_ENDIF;
40 5 : BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 3U);
41 5 : BOOST_CHECK_EQUAL(s1.GetSigOpCount(false), 21U);
42 :
43 3 : CScript p2sh = GetScriptForDestination(CScriptID(s1));
44 : CScript scriptSig;
45 2 : scriptSig << OP_0 << Serialize(s1);
46 5 : BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(scriptSig), 3U);
47 :
48 : std::vector<CPubKey> keys;
49 4 : for (int i = 0; i < 3; i++)
50 : {
51 : CKey k;
52 3 : k.MakeNewKey(true);
53 3 : keys.push_back(k.GetPubKey());
54 : }
55 1 : CScript s2 = GetScriptForMultisig(1, keys);
56 5 : BOOST_CHECK_EQUAL(s2.GetSigOpCount(true), 3U);
57 5 : BOOST_CHECK_EQUAL(s2.GetSigOpCount(false), 20U);
58 :
59 4 : p2sh = GetScriptForDestination(CScriptID(s2));
60 5 : BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(true), 0U);
61 5 : BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(false), 0U);
62 : CScript scriptSig2;
63 4 : scriptSig2 << OP_1 << ToByteVector(dummy) << ToByteVector(dummy) << Serialize(s2);
64 4 : BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(scriptSig2), 3U);
65 1 : }
66 :
67 3 : BOOST_AUTO_TEST_SUITE_END()
|