Line data Source code
1 : // Copyright (c) 2014-2025 The Dash 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 <governance/object.h> 6 : #include <util/strencodings.h> 7 : 8 : #include <test/data/proposals_invalid.json.h> 9 : #include <test/data/proposals_valid.json.h> 10 : 11 : #include <test/util/json.h> 12 : #include <test/util/setup_common.h> 13 : 14 : #include <string> 15 : 16 : #include <boost/test/unit_test.hpp> 17 : 18 : #include <univalue.h> 19 : 20 146 : BOOST_FIXTURE_TEST_SUITE(governance_validators_tests, BasicTestingSetup) 21 : 22 25 : static std::string CreateEncodedProposalObject(const UniValue& objJSON) 23 : { 24 25 : UniValue innerArray(UniValue::VARR); 25 25 : innerArray.push_back(UniValue("proposal")); 26 25 : innerArray.push_back(objJSON); 27 : 28 25 : UniValue outerArray(UniValue::VARR); 29 25 : outerArray.push_back(innerArray); 30 : 31 25 : std::string strData = outerArray.write(); 32 25 : std::string strHex = HexStr(strData); 33 25 : return strHex; 34 25 : } 35 : 36 149 : BOOST_AUTO_TEST_CASE(valid_proposals_test) 37 : { 38 : // all proposals are valid but expired 39 1 : UniValue tests = read_json(std::string(json_tests::proposals_valid, json_tests::proposals_valid + sizeof(json_tests::proposals_valid))); 40 : 41 1 : BOOST_CHECK_MESSAGE(tests.size(), "Empty `tests`"); 42 7 : for(size_t i = 0; i < tests.size(); ++i) { 43 6 : const UniValue& objProposal = tests[i][0]; 44 6 : bool fAllowScript = tests[i][1].get_bool(); 45 : 46 : // legacy format 47 6 : std::string strHexData1 = CreateEncodedProposalObject(objProposal); 48 6 : std::string strErrorMessages; 49 6 : BOOST_CHECK(!governance::ValidateProposal(strHexData1, strErrorMessages, /*fCheckExpiration=*/true, fAllowScript)); 50 6 : BOOST_CHECK_EQUAL(strErrorMessages, "Proposal must be a JSON object;JSON parsing error;"); 51 : 52 : // current format 53 6 : std::string strHexData2 = HexStr(objProposal.write()); 54 6 : std::string strErrorMessages2; 55 6 : BOOST_CHECK_MESSAGE(governance::ValidateProposal(strHexData2, strErrorMessages2, /*fCheckExpiration=*/false, 56 : fAllowScript), 57 : strErrorMessages2); 58 6 : std::string strErrorMessages3; 59 6 : BOOST_CHECK_MESSAGE(!governance::ValidateProposal(strHexData2, strErrorMessages3, /*fCheckExpiration=*/true, 60 : fAllowScript), 61 : strErrorMessages3); 62 6 : } 63 1 : } 64 : 65 149 : BOOST_AUTO_TEST_CASE(invalid_proposals_test) 66 : { 67 : // all proposals are invalid regardless of being expired or not 68 : // (i.e. we don't even check for expiration here) 69 1 : UniValue tests = read_json(std::string(json_tests::proposals_invalid, json_tests::proposals_invalid + sizeof(json_tests::proposals_invalid))); 70 : 71 1 : BOOST_CHECK_MESSAGE(tests.size(), "Empty `tests`"); 72 20 : for(size_t i = 0; i < tests.size(); ++i) { 73 19 : const UniValue& objProposal = tests[i]; 74 : 75 : // legacy format 76 19 : std::string strHexData1 = CreateEncodedProposalObject(objProposal); 77 19 : std::string strErrorMessages1; 78 19 : BOOST_CHECK(!governance::ValidateProposal(strHexData1, strErrorMessages1, /*fCheckExpiration=*/true, 79 : /*fAllowScript=*/false)); 80 19 : std::string strErrorMessages1b; 81 19 : BOOST_CHECK_MESSAGE(!governance::ValidateProposal(strHexData1, strErrorMessages1b, /*fCheckExpiration=*/false, 82 : /*fAllowScript=*/false), 83 : strErrorMessages1b); 84 : 85 : // current format 86 19 : std::string strHexData2 = HexStr(objProposal.write()); 87 19 : std::string strErrorMessages2; 88 19 : BOOST_CHECK_MESSAGE(!governance::ValidateProposal(strHexData2, strErrorMessages2, /*fCheckExpiration=*/false, 89 : /*fAllowScript=*/false), 90 : strErrorMessages2); 91 19 : } 92 1 : } 93 : 94 146 : BOOST_AUTO_TEST_SUITE_END()