2019-06-11 13:46:07 +02:00
|
|
|
// Copyright (c) 2014-2019 The Dash Core developers
|
2017-06-26 15:56:29 +02:00
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <governance/governance-validators.h>
|
|
|
|
#include <utilstrencodings.h>
|
2017-06-26 15:56:29 +02:00
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <test/data/proposals_valid.json.h>
|
|
|
|
#include <test/data/proposals_invalid.json.h>
|
2017-06-26 15:56:29 +02:00
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <test/test_dash.h>
|
2017-06-26 15:56:29 +02:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
|
2018-02-27 14:39:48 +01:00
|
|
|
#include <boost/test/unit_test.hpp>
|
2017-06-26 15:56:29 +02:00
|
|
|
|
2018-02-27 14:39:48 +01:00
|
|
|
#include <univalue.h>
|
2017-06-26 15:56:29 +02:00
|
|
|
|
2018-02-27 14:39:48 +01:00
|
|
|
extern UniValue read_json(const std::string& jsondata);
|
2017-06-26 15:56:29 +02:00
|
|
|
|
2018-02-27 14:39:48 +01:00
|
|
|
BOOST_FIXTURE_TEST_SUITE(governance_validators_tests, BasicTestingSetup)
|
2017-06-26 15:56:29 +02:00
|
|
|
|
|
|
|
std::string CreateEncodedProposalObject(const UniValue& objJSON)
|
|
|
|
{
|
|
|
|
UniValue innerArray(UniValue::VARR);
|
|
|
|
innerArray.push_back(UniValue("proposal"));
|
|
|
|
innerArray.push_back(objJSON);
|
|
|
|
|
|
|
|
UniValue outerArray(UniValue::VARR);
|
|
|
|
outerArray.push_back(innerArray);
|
|
|
|
|
|
|
|
std::string strData = outerArray.write();
|
|
|
|
std::string strHex = HexStr(strData);
|
|
|
|
return strHex;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(valid_proposals_test)
|
|
|
|
{
|
2018-03-21 12:09:13 +01:00
|
|
|
// all proposals are valid but expired
|
2018-02-27 14:39:48 +01:00
|
|
|
UniValue tests = read_json(std::string(json_tests::proposals_valid, json_tests::proposals_valid + sizeof(json_tests::proposals_valid)));
|
|
|
|
|
|
|
|
BOOST_CHECK_MESSAGE(tests.size(), "Empty `tests`");
|
|
|
|
for(size_t i = 0; i < tests.size(); ++i) {
|
|
|
|
const UniValue& objProposal = tests[i];
|
2018-02-28 00:20:16 +01:00
|
|
|
|
|
|
|
// legacy format
|
|
|
|
std::string strHexData1 = CreateEncodedProposalObject(objProposal);
|
2019-02-26 11:44:43 +01:00
|
|
|
CProposalValidator validator1(strHexData1, true);
|
2018-03-21 12:09:13 +01:00
|
|
|
BOOST_CHECK_MESSAGE(validator1.Validate(false), validator1.GetErrorMessages());
|
|
|
|
BOOST_CHECK_MESSAGE(!validator1.Validate(), validator1.GetErrorMessages());
|
2018-02-28 00:20:16 +01:00
|
|
|
|
2019-02-26 11:44:43 +01:00
|
|
|
// legacy format w/validation flag off
|
|
|
|
CProposalValidator validator0(strHexData1, false);
|
|
|
|
BOOST_CHECK(!validator0.Validate());
|
|
|
|
BOOST_CHECK_EQUAL(validator0.GetErrorMessages(), "Legacy proposal serialization format not allowed;JSON parsing error;");
|
|
|
|
|
2018-02-28 00:20:16 +01:00
|
|
|
// new format
|
|
|
|
std::string strHexData2 = HexStr(objProposal.write());
|
2019-02-26 11:44:43 +01:00
|
|
|
CProposalValidator validator2(strHexData2, false);
|
2018-03-21 12:09:13 +01:00
|
|
|
BOOST_CHECK_MESSAGE(validator2.Validate(false), validator2.GetErrorMessages());
|
|
|
|
BOOST_CHECK_MESSAGE(!validator2.Validate(), validator2.GetErrorMessages());
|
2017-06-26 15:56:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(invalid_proposals_test)
|
|
|
|
{
|
2018-03-21 12:09:13 +01:00
|
|
|
// all proposals are invalid regardless of being expired or not
|
|
|
|
// (i.e. we don't even check for expiration here)
|
2018-02-27 14:39:48 +01:00
|
|
|
UniValue tests = read_json(std::string(json_tests::proposals_invalid, json_tests::proposals_invalid + sizeof(json_tests::proposals_invalid)));
|
|
|
|
|
|
|
|
BOOST_CHECK_MESSAGE(tests.size(), "Empty `tests`");
|
|
|
|
for(size_t i = 0; i < tests.size(); ++i) {
|
|
|
|
const UniValue& objProposal = tests[i];
|
2018-03-21 12:09:13 +01:00
|
|
|
|
|
|
|
// legacy format
|
|
|
|
std::string strHexData1 = CreateEncodedProposalObject(objProposal);
|
2019-02-26 11:44:43 +01:00
|
|
|
CProposalValidator validator1(strHexData1, true);
|
2018-03-21 12:09:13 +01:00
|
|
|
BOOST_CHECK_MESSAGE(!validator1.Validate(false), validator1.GetErrorMessages());
|
|
|
|
|
|
|
|
// new format
|
|
|
|
std::string strHexData2 = HexStr(objProposal.write());
|
2019-02-26 11:44:43 +01:00
|
|
|
CProposalValidator validator2(strHexData2, false);
|
2018-03-21 12:09:13 +01:00
|
|
|
BOOST_CHECK_MESSAGE(!validator2.Validate(false), validator2.GetErrorMessages());
|
2017-06-26 15:56:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|