2015-12-13 14:51:43 +01:00
|
|
|
// Copyright (c) 2013-2015 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-03-18 10:11:00 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2013-03-19 16:35:04 +01:00
|
|
|
// Unit tests for alert system
|
|
|
|
|
|
|
|
#include "alert.h"
|
2015-03-26 16:20:59 +01:00
|
|
|
#include "chain.h"
|
|
|
|
#include "chainparams.h"
|
2014-10-29 02:33:23 +01:00
|
|
|
#include "clientversion.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
#include "data/alertTests.raw.h"
|
2013-03-19 16:35:04 +01:00
|
|
|
#include "serialize.h"
|
2014-10-22 21:08:30 +02:00
|
|
|
#include "streams.h"
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
#include "utilstrencodings.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2016-03-14 11:18:02 +01:00
|
|
|
#include "test/testutil.h"
|
2016-03-03 20:20:32 +01:00
|
|
|
#include "test/test_dash.h"
|
2015-03-03 16:49:12 +01:00
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
|
|
|
#include <boost/test/unit_test.hpp>
|
2013-03-19 16:35:04 +01:00
|
|
|
|
|
|
|
//
|
2017-03-15 12:54:34 +01:00
|
|
|
// Sign a CAlert and serialize it
|
2013-03-19 16:35:04 +01:00
|
|
|
//
|
2017-03-15 12:54:34 +01:00
|
|
|
bool SignAndSave(CAlert &alert)
|
|
|
|
{
|
|
|
|
// Sign
|
|
|
|
if(!alert.Sign())
|
|
|
|
{
|
|
|
|
printf("SignAndSave() : could not sign alert:\n%s", alert.ToString().c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string strFilePath = "src/test/data/alertTests.raw";
|
|
|
|
// open output file and associate it with CAutoFile
|
|
|
|
FILE *file = fopen(strFilePath.c_str(), "ab+");
|
|
|
|
CAutoFile fileout(file, SER_DISK, CLIENT_VERSION);
|
|
|
|
if (fileout.IsNull())
|
|
|
|
return error("%s: Failed to open file %s", __func__, strFilePath);
|
|
|
|
|
|
|
|
try {
|
|
|
|
fileout << alert;
|
|
|
|
}
|
|
|
|
catch (std::exception &e) {
|
|
|
|
return error("%s: Serialize or I/O error - %s", __func__, e.what());
|
|
|
|
}
|
|
|
|
fileout.fclose();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// alertTests contains 8 alerts, generated with this code
|
|
|
|
//
|
|
|
|
void GenerateAlertTests()
|
2013-03-19 16:35:04 +01:00
|
|
|
{
|
|
|
|
CAlert alert;
|
|
|
|
alert.nRelayUntil = 60;
|
|
|
|
alert.nExpiration = 24 * 60 * 60;
|
|
|
|
alert.nID = 1;
|
|
|
|
alert.nCancel = 0; // cancels previous messages up to this ID number
|
|
|
|
alert.nMinVer = 0; // These versions are protocol versions
|
2013-11-03 05:46:34 +01:00
|
|
|
alert.nMaxVer = 999001;
|
2013-03-19 16:35:04 +01:00
|
|
|
alert.nPriority = 1;
|
|
|
|
alert.strComment = "Alert comment";
|
|
|
|
alert.strStatusBar = "Alert 1";
|
|
|
|
|
2017-03-15 12:54:34 +01:00
|
|
|
SignAndSave(alert);
|
2013-03-19 16:35:04 +01:00
|
|
|
|
|
|
|
alert.setSubVer.insert(std::string("/Satoshi:0.1.0/"));
|
|
|
|
alert.strStatusBar = "Alert 1 for Satoshi 0.1.0";
|
2017-03-15 12:54:34 +01:00
|
|
|
SignAndSave(alert);
|
2013-03-19 16:35:04 +01:00
|
|
|
|
|
|
|
alert.setSubVer.insert(std::string("/Satoshi:0.2.0/"));
|
|
|
|
alert.strStatusBar = "Alert 1 for Satoshi 0.1.0, 0.2.0";
|
2017-03-15 12:54:34 +01:00
|
|
|
SignAndSave(alert);
|
2013-03-19 16:35:04 +01:00
|
|
|
|
|
|
|
alert.setSubVer.clear();
|
2013-03-19 19:08:21 +01:00
|
|
|
++alert.nID;
|
2013-03-19 16:35:04 +01:00
|
|
|
alert.nCancel = 1;
|
2013-03-19 19:08:21 +01:00
|
|
|
alert.nPriority = 100;
|
2013-03-19 16:35:04 +01:00
|
|
|
alert.strStatusBar = "Alert 2, cancels 1";
|
2017-03-15 12:54:34 +01:00
|
|
|
SignAndSave(alert);
|
2013-03-19 16:35:04 +01:00
|
|
|
|
|
|
|
alert.nExpiration += 60;
|
2013-03-19 19:08:21 +01:00
|
|
|
++alert.nID;
|
2017-03-15 12:54:34 +01:00
|
|
|
SignAndSave(alert);
|
2013-03-19 16:35:04 +01:00
|
|
|
|
2013-03-19 19:08:21 +01:00
|
|
|
++alert.nID;
|
2013-03-19 16:35:04 +01:00
|
|
|
alert.nMinVer = 11;
|
|
|
|
alert.nMaxVer = 22;
|
2017-03-15 12:54:34 +01:00
|
|
|
SignAndSave(alert);
|
2013-03-19 16:35:04 +01:00
|
|
|
|
2013-03-19 19:08:21 +01:00
|
|
|
++alert.nID;
|
2013-03-19 16:35:04 +01:00
|
|
|
alert.strStatusBar = "Alert 2 for Satoshi 0.1.0";
|
|
|
|
alert.setSubVer.insert(std::string("/Satoshi:0.1.0/"));
|
2017-03-15 12:54:34 +01:00
|
|
|
SignAndSave(alert);
|
2013-03-19 19:08:21 +01:00
|
|
|
|
|
|
|
++alert.nID;
|
|
|
|
alert.nMinVer = 0;
|
|
|
|
alert.nMaxVer = 999999;
|
|
|
|
alert.strStatusBar = "Evil Alert'; /bin/ls; echo '";
|
|
|
|
alert.setSubVer.clear();
|
2017-03-15 12:54:34 +01:00
|
|
|
SignAndSave(alert);
|
2013-03-19 16:35:04 +01:00
|
|
|
}
|
|
|
|
|
2015-03-03 16:49:12 +01:00
|
|
|
struct ReadAlerts : public TestingSetup
|
2013-03-19 16:35:04 +01:00
|
|
|
{
|
2013-03-19 19:08:21 +01:00
|
|
|
ReadAlerts()
|
2013-03-19 16:35:04 +01:00
|
|
|
{
|
2018-01-24 16:29:24 +01:00
|
|
|
std::vector<unsigned char> vch(raw_tests::alertTests, raw_tests::alertTests + sizeof(raw_tests::alertTests));
|
2013-09-10 21:18:09 +02:00
|
|
|
CDataStream stream(vch, SER_DISK, CLIENT_VERSION);
|
2013-03-19 19:08:21 +01:00
|
|
|
try {
|
2014-08-01 22:57:55 +02:00
|
|
|
while (!stream.eof())
|
2013-03-19 19:08:21 +01:00
|
|
|
{
|
|
|
|
CAlert alert;
|
2013-09-10 21:18:09 +02:00
|
|
|
stream >> alert;
|
2013-03-19 19:08:21 +01:00
|
|
|
alerts.push_back(alert);
|
|
|
|
}
|
2013-03-19 16:35:04 +01:00
|
|
|
}
|
2014-12-07 13:29:06 +01:00
|
|
|
catch (const std::exception&) { }
|
2013-03-19 16:35:04 +01:00
|
|
|
}
|
2013-03-19 19:08:21 +01:00
|
|
|
~ReadAlerts() { }
|
|
|
|
|
|
|
|
static std::vector<std::string> read_lines(boost::filesystem::path filepath)
|
|
|
|
{
|
|
|
|
std::vector<std::string> result;
|
|
|
|
|
|
|
|
std::ifstream f(filepath.string().c_str());
|
|
|
|
std::string line;
|
|
|
|
while (std::getline(f,line))
|
|
|
|
result.push_back(line);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<CAlert> alerts;
|
|
|
|
};
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_SUITE(Alert_tests, ReadAlerts)
|
2013-03-19 16:35:04 +01:00
|
|
|
|
2017-03-15 12:54:34 +01:00
|
|
|
// Steps to generate alert tests:
|
|
|
|
// - update alerts in GenerateAlertTests() (optional)
|
|
|
|
// - enable code below (#if 1)
|
|
|
|
// - replace "fffffffffffffffffffffffffffffffffffffffffffffffffff" with the actual MAINNET privkey
|
|
|
|
// - recompile and run "/path/to/test_dash -t Alert_test"
|
|
|
|
//
|
|
|
|
// NOTE: make sure to disable code and remove alert privkey when you're done!
|
|
|
|
//
|
|
|
|
#if 0
|
|
|
|
BOOST_AUTO_TEST_CASE(GenerateAlerts)
|
|
|
|
{
|
|
|
|
SoftSetArg("-alertkey", "fffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
|
|
GenerateAlertTests();
|
|
|
|
}
|
|
|
|
#endif
|
2013-03-19 16:35:04 +01:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(AlertApplies)
|
|
|
|
{
|
|
|
|
SetMockTime(11);
|
2015-04-03 17:42:06 +02:00
|
|
|
const std::vector<unsigned char>& alertKey = Params(CBaseChainParams::MAIN).AlertKey();
|
2013-03-19 16:35:04 +01:00
|
|
|
|
2018-02-06 12:09:33 +01:00
|
|
|
for (const auto& alert : alerts)
|
2013-03-19 16:35:04 +01:00
|
|
|
{
|
2015-04-03 17:42:06 +02:00
|
|
|
BOOST_CHECK(alert.CheckSignature(alertKey));
|
2013-03-19 16:35:04 +01:00
|
|
|
}
|
2013-09-11 01:04:45 +02:00
|
|
|
|
|
|
|
BOOST_CHECK(alerts.size() >= 3);
|
|
|
|
|
2013-03-19 16:35:04 +01:00
|
|
|
// Matches:
|
|
|
|
BOOST_CHECK(alerts[0].AppliesTo(1, ""));
|
2013-11-03 05:46:34 +01:00
|
|
|
BOOST_CHECK(alerts[0].AppliesTo(999001, ""));
|
2013-03-19 16:35:04 +01:00
|
|
|
BOOST_CHECK(alerts[0].AppliesTo(1, "/Satoshi:11.11.11/"));
|
|
|
|
|
|
|
|
BOOST_CHECK(alerts[1].AppliesTo(1, "/Satoshi:0.1.0/"));
|
2013-11-03 05:46:34 +01:00
|
|
|
BOOST_CHECK(alerts[1].AppliesTo(999001, "/Satoshi:0.1.0/"));
|
2013-03-19 16:35:04 +01:00
|
|
|
|
|
|
|
BOOST_CHECK(alerts[2].AppliesTo(1, "/Satoshi:0.1.0/"));
|
|
|
|
BOOST_CHECK(alerts[2].AppliesTo(1, "/Satoshi:0.2.0/"));
|
|
|
|
|
|
|
|
// Don't match:
|
|
|
|
BOOST_CHECK(!alerts[0].AppliesTo(-1, ""));
|
2013-11-03 05:46:34 +01:00
|
|
|
BOOST_CHECK(!alerts[0].AppliesTo(999002, ""));
|
2013-03-19 16:35:04 +01:00
|
|
|
|
|
|
|
BOOST_CHECK(!alerts[1].AppliesTo(1, ""));
|
|
|
|
BOOST_CHECK(!alerts[1].AppliesTo(1, "Satoshi:0.1.0"));
|
|
|
|
BOOST_CHECK(!alerts[1].AppliesTo(1, "/Satoshi:0.1.0"));
|
|
|
|
BOOST_CHECK(!alerts[1].AppliesTo(1, "Satoshi:0.1.0/"));
|
|
|
|
BOOST_CHECK(!alerts[1].AppliesTo(-1, "/Satoshi:0.1.0/"));
|
2013-11-03 05:46:34 +01:00
|
|
|
BOOST_CHECK(!alerts[1].AppliesTo(999002, "/Satoshi:0.1.0/"));
|
2013-03-19 16:35:04 +01:00
|
|
|
BOOST_CHECK(!alerts[1].AppliesTo(1, "/Satoshi:0.2.0/"));
|
|
|
|
|
|
|
|
BOOST_CHECK(!alerts[2].AppliesTo(1, "/Satoshi:0.3.0/"));
|
|
|
|
|
|
|
|
SetMockTime(0);
|
|
|
|
}
|
|
|
|
|
2013-03-19 19:08:21 +01:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(AlertNotify)
|
|
|
|
{
|
|
|
|
SetMockTime(11);
|
2015-04-03 17:42:06 +02:00
|
|
|
const std::vector<unsigned char>& alertKey = Params(CBaseChainParams::MAIN).AlertKey();
|
2013-03-19 19:08:21 +01:00
|
|
|
|
2015-08-06 05:10:14 +02:00
|
|
|
boost::filesystem::path temp = GetTempPath() /
|
|
|
|
boost::filesystem::unique_path("alertnotify-%%%%.txt");
|
2013-03-19 19:08:21 +01:00
|
|
|
|
2016-12-27 19:11:21 +01:00
|
|
|
ForceSetArg("-alertnotify", std::string("echo %s >> ") + temp.string());
|
2013-03-19 19:08:21 +01:00
|
|
|
|
2018-02-06 12:09:33 +01:00
|
|
|
for (const auto& alert : alerts)
|
2015-04-03 17:42:06 +02:00
|
|
|
alert.ProcessAlert(alertKey, false);
|
2013-03-19 19:08:21 +01:00
|
|
|
|
|
|
|
std::vector<std::string> r = read_lines(temp);
|
|
|
|
BOOST_CHECK_EQUAL(r.size(), 4u);
|
2014-10-30 23:47:08 +01:00
|
|
|
|
|
|
|
// Windows built-in echo semantics are different than posixy shells. Quotes and
|
|
|
|
// whitespace are printed literally.
|
|
|
|
|
|
|
|
#ifndef WIN32
|
2013-03-19 19:08:21 +01:00
|
|
|
BOOST_CHECK_EQUAL(r[0], "Alert 1");
|
|
|
|
BOOST_CHECK_EQUAL(r[1], "Alert 2, cancels 1");
|
|
|
|
BOOST_CHECK_EQUAL(r[2], "Alert 2, cancels 1");
|
|
|
|
BOOST_CHECK_EQUAL(r[3], "Evil Alert; /bin/ls; echo "); // single-quotes should be removed
|
2014-10-30 23:47:08 +01:00
|
|
|
#else
|
|
|
|
BOOST_CHECK_EQUAL(r[0], "'Alert 1' ");
|
|
|
|
BOOST_CHECK_EQUAL(r[1], "'Alert 2, cancels 1' ");
|
|
|
|
BOOST_CHECK_EQUAL(r[2], "'Alert 2, cancels 1' ");
|
|
|
|
BOOST_CHECK_EQUAL(r[3], "'Evil Alert; /bin/ls; echo ' ");
|
|
|
|
#endif
|
2013-03-19 19:08:21 +01:00
|
|
|
boost::filesystem::remove(temp);
|
|
|
|
|
|
|
|
SetMockTime(0);
|
|
|
|
}
|
|
|
|
|
2013-03-19 16:35:04 +01:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|