|
|
@ -4,13 +4,12 @@ |
|
|
|
|
|
|
|
|
|
|
|
#include <boost/foreach.hpp> |
|
|
|
#include <boost/foreach.hpp> |
|
|
|
#include <boost/test/unit_test.hpp> |
|
|
|
#include <boost/test/unit_test.hpp> |
|
|
|
|
|
|
|
#include <fstream> |
|
|
|
|
|
|
|
|
|
|
|
#include "alert.h" |
|
|
|
#include "alert.h" |
|
|
|
#include "serialize.h" |
|
|
|
#include "serialize.h" |
|
|
|
#include "util.h" |
|
|
|
#include "util.h" |
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(Alert_tests) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if 0 |
|
|
|
#if 0 |
|
|
|
//
|
|
|
|
//
|
|
|
|
// alertTests contains 7 alerts, generated with this code:
|
|
|
|
// alertTests contains 7 alerts, generated with this code:
|
|
|
@ -39,30 +38,40 @@ BOOST_AUTO_TEST_SUITE(Alert_tests) |
|
|
|
SignAndSave(alert, "test/alertTests"); |
|
|
|
SignAndSave(alert, "test/alertTests"); |
|
|
|
|
|
|
|
|
|
|
|
alert.setSubVer.clear(); |
|
|
|
alert.setSubVer.clear(); |
|
|
|
alert.nID = 2; |
|
|
|
++alert.nID; |
|
|
|
alert.nCancel = 1; |
|
|
|
alert.nCancel = 1; |
|
|
|
|
|
|
|
alert.nPriority = 100; |
|
|
|
alert.strStatusBar = "Alert 2, cancels 1"; |
|
|
|
alert.strStatusBar = "Alert 2, cancels 1"; |
|
|
|
SignAndSave(alert, "test/alertTests"); |
|
|
|
SignAndSave(alert, "test/alertTests"); |
|
|
|
|
|
|
|
|
|
|
|
alert.nExpiration += 60; |
|
|
|
alert.nExpiration += 60; |
|
|
|
|
|
|
|
++alert.nID; |
|
|
|
SignAndSave(alert, "test/alertTests"); |
|
|
|
SignAndSave(alert, "test/alertTests"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
++alert.nID; |
|
|
|
alert.nMinVer = 11; |
|
|
|
alert.nMinVer = 11; |
|
|
|
alert.nMaxVer = 22; |
|
|
|
alert.nMaxVer = 22; |
|
|
|
SignAndSave(alert, "test/alertTests"); |
|
|
|
SignAndSave(alert, "test/alertTests"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
++alert.nID; |
|
|
|
alert.strStatusBar = "Alert 2 for Satoshi 0.1.0"; |
|
|
|
alert.strStatusBar = "Alert 2 for Satoshi 0.1.0"; |
|
|
|
alert.setSubVer.insert(std::string("/Satoshi:0.1.0/")); |
|
|
|
alert.setSubVer.insert(std::string("/Satoshi:0.1.0/")); |
|
|
|
SignAndSave(alert, "test/alertTests"); |
|
|
|
SignAndSave(alert, "test/alertTests"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
++alert.nID; |
|
|
|
|
|
|
|
alert.nMinVer = 0; |
|
|
|
|
|
|
|
alert.nMaxVer = 999999; |
|
|
|
|
|
|
|
alert.strStatusBar = "Evil Alert'; /bin/ls; echo '"; |
|
|
|
|
|
|
|
alert.setSubVer.clear(); |
|
|
|
|
|
|
|
SignAndSave(alert, "test/alertTests"); |
|
|
|
} |
|
|
|
} |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct ReadAlerts |
|
|
|
std::vector<CAlert> |
|
|
|
|
|
|
|
read_alerts(const std::string& filename) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
std::vector<CAlert> result; |
|
|
|
ReadAlerts() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
std::string filename("alertTests"); |
|
|
|
namespace fs = boost::filesystem; |
|
|
|
namespace fs = boost::filesystem; |
|
|
|
fs::path testFile = fs::current_path() / "test" / "data" / filename; |
|
|
|
fs::path testFile = fs::current_path() / "test" / "data" / filename; |
|
|
|
#ifdef TEST_DATA_DIR |
|
|
|
#ifdef TEST_DATA_DIR |
|
|
@ -72,31 +81,46 @@ read_alerts(const std::string& filename) |
|
|
|
} |
|
|
|
} |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
FILE* fp = fopen(testFile.string().c_str(), "rb"); |
|
|
|
FILE* fp = fopen(testFile.string().c_str(), "rb"); |
|
|
|
if (!fp) return result; |
|
|
|
if (!fp) return; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CAutoFile filein = CAutoFile(fp, SER_DISK, CLIENT_VERSION); |
|
|
|
CAutoFile filein = CAutoFile(fp, SER_DISK, CLIENT_VERSION); |
|
|
|
if (!filein) return result; |
|
|
|
if (!filein) return; |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
while (!feof(filein)) |
|
|
|
while (!feof(filein)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
CAlert alert; |
|
|
|
CAlert alert; |
|
|
|
filein >> alert; |
|
|
|
filein >> alert; |
|
|
|
result.push_back(alert); |
|
|
|
alerts.push_back(alert); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
catch (std::exception) { } |
|
|
|
catch (std::exception) { } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
~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; |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<CAlert> alerts; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_SUITE(Alert_tests, ReadAlerts) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(AlertApplies) |
|
|
|
BOOST_AUTO_TEST_CASE(AlertApplies) |
|
|
|
{ |
|
|
|
{ |
|
|
|
SetMockTime(11); |
|
|
|
SetMockTime(11); |
|
|
|
|
|
|
|
|
|
|
|
std::vector<CAlert> alerts = read_alerts("alertTests"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_FOREACH(const CAlert& alert, alerts) |
|
|
|
BOOST_FOREACH(const CAlert& alert, alerts) |
|
|
|
{ |
|
|
|
{ |
|
|
|
BOOST_CHECK(alert.CheckSignature()); |
|
|
|
BOOST_CHECK(alert.CheckSignature()); |
|
|
@ -129,4 +153,33 @@ BOOST_AUTO_TEST_CASE(AlertApplies) |
|
|
|
SetMockTime(0); |
|
|
|
SetMockTime(0); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// This uses sh 'echo' to test the -alertnotify function, writing to a
|
|
|
|
|
|
|
|
// /tmp file. So skip it on Windows:
|
|
|
|
|
|
|
|
#ifndef WIN32 |
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(AlertNotify) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
SetMockTime(11); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
boost::filesystem::path temp = GetTempPath() / "alertnotify.txt"; |
|
|
|
|
|
|
|
boost::filesystem::remove(temp); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mapArgs["-alertnotify"] = std::string("echo %s >> ") + temp.string(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_FOREACH(CAlert alert, alerts) |
|
|
|
|
|
|
|
alert.ProcessAlert(false); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> r = read_lines(temp); |
|
|
|
|
|
|
|
BOOST_CHECK_EQUAL(r.size(), 4u); |
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
boost::filesystem::remove(temp); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SetMockTime(0); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END() |
|
|
|
BOOST_AUTO_TEST_SUITE_END() |
|
|
|