Skip to content

Commit

Permalink
Add unit testing for the CompressScript functions
Browse files Browse the repository at this point in the history
Summary: This is a backport of Core [[bitcoin/bitcoin#17220 | PR17220]]

Test Plan: `ninja && ninja check`

Reviewers: O1 Bitcoin ABC, #bitcoin_abc, Fabien

Reviewed By: O1 Bitcoin ABC, #bitcoin_abc, Fabien

Subscribers: majcosta

Differential Revision: https://reviews.bitcoinabc.org/D8192
  • Loading branch information
mmachicao authored and PiRK committed Oct 30, 2020
1 parent 930510c commit c6b1044
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/test/compress_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <compressor.h>
#include <script/standard.h>

#include <test/util/setup_common.h>

Expand Down Expand Up @@ -65,4 +66,84 @@ BOOST_AUTO_TEST_CASE(compress_amounts) {
}
}

BOOST_AUTO_TEST_CASE(compress_script_to_ckey_id) {
// case CKeyID
CKey key;
key.MakeNewKey(true);
CPubKey pubkey = key.GetPubKey();

CScript script = CScript()
<< OP_DUP << OP_HASH160 << ToByteVector(pubkey.GetID())
<< OP_EQUALVERIFY << OP_CHECKSIG;
BOOST_CHECK_EQUAL(script.size(), 25);

std::vector<uint8_t> out;
bool done = CompressScript(script, out);
BOOST_CHECK_EQUAL(done, true);

// Check compressed script
BOOST_CHECK_EQUAL(out.size(), 21);
BOOST_CHECK_EQUAL(out[0], 0x00);
// compare the 20 relevant chars of the CKeyId in the script
BOOST_CHECK_EQUAL(memcmp(&out[1], &script[3], 20), 0);
}

BOOST_AUTO_TEST_CASE(compress_script_to_cscript_id) {
// case CScriptID
CScript script, redeemScript;
script << OP_HASH160 << ToByteVector(CScriptID(redeemScript)) << OP_EQUAL;
BOOST_CHECK_EQUAL(script.size(), 23);

std::vector<uint8_t> out;
bool done = CompressScript(script, out);
BOOST_CHECK_EQUAL(done, true);

// Check compressed script
BOOST_CHECK_EQUAL(out.size(), 21);
BOOST_CHECK_EQUAL(out[0], 0x01);
// compare the 20 relevant chars of the CScriptId in the script
BOOST_CHECK_EQUAL(memcmp(&out[1], &script[2], 20), 0);
}

BOOST_AUTO_TEST_CASE(compress_script_to_compressed_pubkey_id) {
CKey key;
// case compressed PubKeyID
key.MakeNewKey(true);

// COMPRESSED_PUBLIC_KEY_SIZE (33)
CScript script = CScript() << ToByteVector(key.GetPubKey()) << OP_CHECKSIG;
BOOST_CHECK_EQUAL(script.size(), 35);

std::vector<uint8_t> out;
bool done = CompressScript(script, out);
BOOST_CHECK_EQUAL(done, true);

// Check compressed script
BOOST_CHECK_EQUAL(out.size(), 33);
BOOST_CHECK_EQUAL(memcmp(&out[0], &script[1], 1), 0);
// compare the 32 chars of the compressed CPubKey
BOOST_CHECK_EQUAL(memcmp(&out[1], &script[2], 32), 0);
}

BOOST_AUTO_TEST_CASE(compress_script_to_uncompressed_pubkey_id) {
CKey key;
// case uncompressed PubKeyID
key.MakeNewKey(false);
// PUBLIC_KEY_SIZE (65)
CScript script = CScript() << ToByteVector(key.GetPubKey()) << OP_CHECKSIG;
// 1 char code + 65 char pubkey + OP_CHECKSIG
BOOST_CHECK_EQUAL(script.size(), 67);

std::vector<uint8_t> out;
bool done = CompressScript(script, out);
BOOST_CHECK_EQUAL(done, true);

// Check compressed script
BOOST_CHECK_EQUAL(out.size(), 33);
// first 32 chars of CPubKey are copied into out[1:]
BOOST_CHECK_EQUAL(memcmp(&out[1], &script[2], 32), 0);
// least significant bit (lsb) of last char of pubkey is mapped into out[0]
BOOST_CHECK_EQUAL(out[0], 0x04 | (script[65] & 0x01));
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit c6b1044

Please sign in to comment.