Skip to content

Commit 0c064f8

Browse files
committed
[Tests] Add tests for SetTxPayload/GetTxPayload and CheckStringSig
1 parent 693772a commit 0c064f8

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ BITCOIN_TESTS =\
6262
test/crypto_tests.cpp \
6363
test/cuckoocache_tests.cpp \
6464
test/DoS_tests.cpp \
65+
test/evo_specialtx_tests.cpp \
6566
test/getarg_tests.cpp \
6667
test/hash_tests.cpp \
6768
test/key_tests.cpp \

src/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ set(BITCOIN_TESTS
6868
${CMAKE_CURRENT_SOURCE_DIR}/arith_uint256_tests.cpp
6969
${CMAKE_CURRENT_SOURCE_DIR}/addrman_tests.cpp
7070
${CMAKE_CURRENT_SOURCE_DIR}/allocator_tests.cpp
71+
${CMAKE_CURRENT_SOURCE_DIR}/evo_specialtx_tests.cpp
7172
${CMAKE_CURRENT_SOURCE_DIR}/librust/libsapling_utils_tests.cpp
7273
${CMAKE_CURRENT_SOURCE_DIR}/librust/sapling_key_tests.cpp
7374
${CMAKE_CURRENT_SOURCE_DIR}/librust/pedersen_hash_tests.cpp

src/test/evo_specialtx_tests.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright (c) 2021 The PIVX developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or https://www.opensource.org/licenses/mit-license.php.
4+
5+
#include "test/test_pivx.h"
6+
#include "primitives/transaction.h"
7+
#include "evo/providertx.h"
8+
#include "evo/specialtx.h"
9+
#include "messagesigner.h"
10+
#include "netbase.h"
11+
12+
#include <boost/test/unit_test.hpp>
13+
14+
BOOST_FIXTURE_TEST_SUITE(evo_specialtx_tests, TestingSetup)
15+
16+
static void RandomScript(CScript &script)
17+
{
18+
static const opcodetype oplist[] = {OP_FALSE, OP_1, OP_2, OP_3, OP_CHECKSIG, OP_IF, OP_VERIF, OP_RETURN, OP_CODESEPARATOR};
19+
script = CScript();
20+
int ops = (InsecureRandRange(10));
21+
for (int i=0; i<ops; i++)
22+
script << oplist[InsecureRandRange(sizeof(oplist)/sizeof(oplist[0]))];
23+
}
24+
25+
static CKey GetRandomKey()
26+
{
27+
CKey key;
28+
key.MakeNewKey(true);
29+
return key;
30+
}
31+
32+
static CKeyID GetRandomKeyID()
33+
{
34+
return GetRandomKey().GetPubKey().GetID();
35+
}
36+
37+
static ProRegPL GetRandomProRegPayload()
38+
{
39+
ProRegPL pl;
40+
pl.collateralOutpoint.hash = GetRandHash();
41+
pl.collateralOutpoint.n = InsecureRandBits(2);
42+
BOOST_CHECK(Lookup("127.0.0.1:51472", pl.addr, Params().GetDefaultPort(), false));
43+
pl.keyIDOwner = GetRandomKeyID();
44+
pl.keyIDOperator = GetRandomKeyID();
45+
pl.keyIDVoting = GetRandomKeyID();
46+
RandomScript(pl.scriptPayout);
47+
pl.nOperatorReward = InsecureRandRange(10000);
48+
RandomScript(pl.scriptOperatorPayout);
49+
pl.inputsHash = GetRandHash();
50+
pl.vchSig = InsecureRandBytes(63);
51+
return pl;
52+
}
53+
54+
BOOST_AUTO_TEST_CASE(providertx_setpayload_test)
55+
{
56+
const ProRegPL& pl = GetRandomProRegPayload();
57+
58+
CMutableTransaction mtx;
59+
SetTxPayload(mtx, pl);
60+
ProRegPL pl2;
61+
BOOST_CHECK(GetTxPayload(mtx, pl2));
62+
BOOST_CHECK(pl.collateralOutpoint == pl2.collateralOutpoint);
63+
BOOST_CHECK(pl.addr == pl2.addr);
64+
BOOST_CHECK(pl.keyIDOwner == pl2.keyIDOwner);
65+
BOOST_CHECK(pl.keyIDOperator == pl2.keyIDOperator);
66+
BOOST_CHECK(pl.keyIDVoting == pl2.keyIDVoting);
67+
BOOST_CHECK(pl.scriptPayout == pl2.scriptPayout);
68+
BOOST_CHECK(pl.nOperatorReward == pl2.nOperatorReward);
69+
BOOST_CHECK(pl.scriptOperatorPayout == pl2.scriptOperatorPayout);
70+
BOOST_CHECK(pl.inputsHash == pl2.inputsHash);
71+
BOOST_CHECK(pl.vchSig == pl2.vchSig);
72+
}
73+
74+
BOOST_AUTO_TEST_CASE(providertx_checkstringsig_test)
75+
{
76+
ProRegPL pl = GetRandomProRegPayload();
77+
pl.vchSig.clear();
78+
const CKey& key = GetRandomKey();
79+
BOOST_CHECK(CMessageSigner::SignMessage(pl.MakeSignString(), pl.vchSig, key));
80+
81+
std::string strError;
82+
const CKeyID& keyID = key.GetPubKey().GetID();
83+
BOOST_CHECK(CMessageSigner::VerifyMessage(keyID, pl.vchSig, pl.MakeSignString(), strError));
84+
// Change owner address or script payout
85+
pl.keyIDOwner = GetRandomKeyID();
86+
BOOST_CHECK(!CMessageSigner::VerifyMessage(keyID, pl.vchSig, pl.MakeSignString(), strError));
87+
RandomScript(pl.scriptPayout);
88+
BOOST_CHECK(!CMessageSigner::VerifyMessage(keyID, pl.vchSig, pl.MakeSignString(), strError));
89+
}
90+
91+
92+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)