LCOV - code coverage report
Current view: top level - src/test - script_segwit_tests.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 106 108 98.1 %
Date: 2022-04-21 14:51:19 Functions: 14 14 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 1 2 50.0 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2012-2021 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 <script/script.h>
#       6                 :            : #include <test/util/setup_common.h>
#       7                 :            : 
#       8                 :            : #include <boost/test/unit_test.hpp>
#       9                 :            : 
#      10                 :            : BOOST_FIXTURE_TEST_SUITE(script_segwit_tests, BasicTestingSetup)
#      11                 :            : 
#      12                 :            : BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Valid)
#      13                 :          2 : {
#      14                 :          2 :     uint256 dummy;
#      15                 :          2 :     CScript p2wsh;
#      16                 :          2 :     p2wsh << OP_0 << ToByteVector(dummy);
#      17                 :          2 :     BOOST_CHECK(p2wsh.IsPayToWitnessScriptHash());
#      18                 :            : 
#      19                 :          2 :     std::vector<unsigned char> bytes = {OP_0, 32};
#      20                 :          2 :     bytes.insert(bytes.end(), 32, 0);
#      21                 :          2 :     BOOST_CHECK(CScript(bytes.begin(), bytes.end()).IsPayToWitnessScriptHash());
#      22                 :          2 : }
#      23                 :            : 
#      24                 :            : BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Invalid_NotOp0)
#      25                 :          2 : {
#      26                 :          2 :     uint256 dummy;
#      27                 :          2 :     CScript notp2wsh;
#      28                 :          2 :     notp2wsh << OP_1 << ToByteVector(dummy);
#      29                 :          2 :     BOOST_CHECK(!notp2wsh.IsPayToWitnessScriptHash());
#      30                 :          2 : }
#      31                 :            : 
#      32                 :            : BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Invalid_Size)
#      33                 :          2 : {
#      34                 :          2 :     uint160 dummy;
#      35                 :          2 :     CScript notp2wsh;
#      36                 :          2 :     notp2wsh << OP_0 << ToByteVector(dummy);
#      37                 :          2 :     BOOST_CHECK(!notp2wsh.IsPayToWitnessScriptHash());
#      38                 :          2 : }
#      39                 :            : 
#      40                 :            : BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Invalid_Nop)
#      41                 :          2 : {
#      42                 :          2 :     uint256 dummy;
#      43                 :          2 :     CScript notp2wsh;
#      44                 :          2 :     notp2wsh << OP_0 << OP_NOP << ToByteVector(dummy);
#      45                 :          2 :     BOOST_CHECK(!notp2wsh.IsPayToWitnessScriptHash());
#      46                 :          2 : }
#      47                 :            : 
#      48                 :            : BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Invalid_EmptyScript)
#      49                 :          2 : {
#      50                 :          2 :     CScript notp2wsh;
#      51                 :          2 :     BOOST_CHECK(!notp2wsh.IsPayToWitnessScriptHash());
#      52                 :          2 : }
#      53                 :            : 
#      54                 :            : BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Invalid_Pushdata)
#      55                 :          2 : {
#      56                 :            :     // A script is not P2WSH if OP_PUSHDATA is used to push the hash.
#      57                 :          2 :     std::vector<unsigned char> bytes = {OP_0, OP_PUSHDATA1, 32};
#      58                 :          2 :     bytes.insert(bytes.end(), 32, 0);
#      59                 :          2 :     BOOST_CHECK(!CScript(bytes.begin(), bytes.end()).IsPayToWitnessScriptHash());
#      60                 :            : 
#      61                 :          2 :     bytes = {OP_0, OP_PUSHDATA2, 32, 0};
#      62                 :          2 :     bytes.insert(bytes.end(), 32, 0);
#      63                 :          2 :     BOOST_CHECK(!CScript(bytes.begin(), bytes.end()).IsPayToWitnessScriptHash());
#      64                 :            : 
#      65                 :          2 :     bytes = {OP_0, OP_PUSHDATA4, 32, 0, 0, 0};
#      66                 :          2 :     bytes.insert(bytes.end(), 32, 0);
#      67                 :          2 :     BOOST_CHECK(!CScript(bytes.begin(), bytes.end()).IsPayToWitnessScriptHash());
#      68                 :          2 : }
#      69                 :            : 
#      70                 :            : namespace {
#      71                 :            : 
#      72                 :            : bool IsExpectedWitnessProgram(const CScript& script, const int expectedVersion, const std::vector<unsigned char>& expectedProgram)
#      73                 :          6 : {
#      74                 :          6 :     int actualVersion;
#      75                 :          6 :     std::vector<unsigned char> actualProgram;
#      76         [ -  + ]:          6 :     if (!script.IsWitnessProgram(actualVersion, actualProgram)) {
#      77                 :          0 :         return false;
#      78                 :          0 :     }
#      79                 :          6 :     BOOST_CHECK_EQUAL(actualVersion, expectedVersion);
#      80                 :          6 :     BOOST_CHECK(actualProgram == expectedProgram);
#      81                 :          6 :     return true;
#      82                 :          6 : }
#      83                 :            : 
#      84                 :            : bool IsNoWitnessProgram(const CScript& script)
#      85                 :         16 : {
#      86                 :         16 :     int dummyVersion;
#      87                 :         16 :     std::vector<unsigned char> dummyProgram;
#      88                 :         16 :     return !script.IsWitnessProgram(dummyVersion, dummyProgram);
#      89                 :         16 : }
#      90                 :            : 
#      91                 :            : } // anonymous namespace
#      92                 :            : 
#      93                 :            : BOOST_AUTO_TEST_CASE(IsWitnessProgram_Valid)
#      94                 :          2 : {
#      95                 :            :     // Witness programs have a minimum data push of 2 bytes.
#      96                 :          2 :     std::vector<unsigned char> program = {42, 18};
#      97                 :          2 :     CScript wit;
#      98                 :          2 :     wit << OP_0 << program;
#      99                 :          2 :     BOOST_CHECK(IsExpectedWitnessProgram(wit, 0, program));
#     100                 :            : 
#     101                 :          2 :     wit.clear();
#     102                 :            :     // Witness programs have a maximum data push of 40 bytes.
#     103                 :          2 :     program.resize(40);
#     104                 :          2 :     wit << OP_16 << program;
#     105                 :          2 :     BOOST_CHECK(IsExpectedWitnessProgram(wit, 16, program));
#     106                 :            : 
#     107                 :          2 :     program.resize(32);
#     108                 :          2 :     std::vector<unsigned char> bytes = {OP_5, static_cast<unsigned char>(program.size())};
#     109                 :          2 :     bytes.insert(bytes.end(), program.begin(), program.end());
#     110                 :          2 :     BOOST_CHECK(IsExpectedWitnessProgram(CScript(bytes.begin(), bytes.end()), 5, program));
#     111                 :          2 : }
#     112                 :            : 
#     113                 :            : BOOST_AUTO_TEST_CASE(IsWitnessProgram_Invalid_Version)
#     114                 :          2 : {
#     115                 :          2 :     std::vector<unsigned char> program(10);
#     116                 :          2 :     CScript nowit;
#     117                 :          2 :     nowit << OP_1NEGATE << program;
#     118                 :          2 :     BOOST_CHECK(IsNoWitnessProgram(nowit));
#     119                 :          2 : }
#     120                 :            : 
#     121                 :            : BOOST_AUTO_TEST_CASE(IsWitnessProgram_Invalid_Size)
#     122                 :          2 : {
#     123                 :          2 :     std::vector<unsigned char> program(1);
#     124                 :          2 :     CScript nowit;
#     125                 :          2 :     nowit << OP_0 << program;
#     126                 :          2 :     BOOST_CHECK(IsNoWitnessProgram(nowit));
#     127                 :            : 
#     128                 :          2 :     nowit.clear();
#     129                 :          2 :     program.resize(41);
#     130                 :          2 :     nowit << OP_0 << program;
#     131                 :          2 :     BOOST_CHECK(IsNoWitnessProgram(nowit));
#     132                 :          2 : }
#     133                 :            : 
#     134                 :            : BOOST_AUTO_TEST_CASE(IsWitnessProgram_Invalid_Nop)
#     135                 :          2 : {
#     136                 :          2 :     std::vector<unsigned char> program(10);
#     137                 :          2 :     CScript nowit;
#     138                 :          2 :     nowit << OP_0 << OP_NOP << program;
#     139                 :          2 :     BOOST_CHECK(IsNoWitnessProgram(nowit));
#     140                 :          2 : }
#     141                 :            : 
#     142                 :            : BOOST_AUTO_TEST_CASE(IsWitnessProgram_Invalid_EmptyScript)
#     143                 :          2 : {
#     144                 :          2 :     CScript nowit;
#     145                 :          2 :     BOOST_CHECK(IsNoWitnessProgram(nowit));
#     146                 :          2 : }
#     147                 :            : 
#     148                 :            : BOOST_AUTO_TEST_CASE(IsWitnessProgram_Invalid_Pushdata)
#     149                 :          2 : {
#     150                 :            :     // A script is no witness program if OP_PUSHDATA is used to push the hash.
#     151                 :          2 :     std::vector<unsigned char> bytes = {OP_0, OP_PUSHDATA1, 32};
#     152                 :          2 :     bytes.insert(bytes.end(), 32, 0);
#     153                 :          2 :     BOOST_CHECK(IsNoWitnessProgram(CScript(bytes.begin(), bytes.end())));
#     154                 :            : 
#     155                 :          2 :     bytes = {OP_0, OP_PUSHDATA2, 32, 0};
#     156                 :          2 :     bytes.insert(bytes.end(), 32, 0);
#     157                 :          2 :     BOOST_CHECK(IsNoWitnessProgram(CScript(bytes.begin(), bytes.end())));
#     158                 :            : 
#     159                 :          2 :     bytes = {OP_0, OP_PUSHDATA4, 32, 0, 0, 0};
#     160                 :          2 :     bytes.insert(bytes.end(), 32, 0);
#     161                 :          2 :     BOOST_CHECK(IsNoWitnessProgram(CScript(bytes.begin(), bytes.end())));
#     162                 :          2 : }
#     163                 :            : 
#     164                 :            : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a