Fixes for Alerts Unit Tests

This commit is contained in:
Norm Fasey 2014-07-09 19:48:35 +01:00
parent b7764376e5
commit 89c669769b
8 changed files with 165 additions and 93 deletions

View File

@ -7,7 +7,7 @@ use the Boost::Test unit-testing framework.
To compile and run the tests: To compile and run the tests:
cd src cd src
make -f makefile.unix test_darkcoin # Replace makefile.unix if you're not on unix make -f makefile.unix test_darkcoin UNIT_TEST=1 # Replace makefile.unix if you're not on unix
./test_darkcoin # Runs the unit tests ./test_darkcoin # Runs the unit tests
If all tests succeed the last line of output will be: If all tests succeed the last line of output will be:

View File

@ -19,7 +19,11 @@ using namespace std;
map<uint256, CAlert> mapAlerts; map<uint256, CAlert> mapAlerts;
CCriticalSection cs_mapAlerts; CCriticalSection cs_mapAlerts;
#ifdef UNIT_TEST
static const char* pszMainKey = "043bcb5ad652b5680fadc0002a17c64542d2aff59c605d820960ab568af2c9ac7e3f130f7e4b638cfdc9069282f5500d67ee3f2803d6f0d6be44d387ac7ac286e4";
#else
static const char* pszMainKey = "048240a8748a80a286b270ba126705ced4f2ce5a7847b3610ea3c06513150dade2a8512ed5ea86320824683fc0818f0ac019214973e677acd1244f6d0571fc5103"; static const char* pszMainKey = "048240a8748a80a286b270ba126705ced4f2ce5a7847b3610ea3c06513150dade2a8512ed5ea86320824683fc0818f0ac019214973e677acd1244f6d0571fc5103";
#endif
static const char* pszTestKey = "04517d8a699cb43d3938d7b24faaff7cda448ca4ea267723ba614784de661949bf632d6304316b244646dea079735b9a6fc4af804efb4752075b9fe2245e14e412"; static const char* pszTestKey = "04517d8a699cb43d3938d7b24faaff7cda448ca4ea267723ba614784de661949bf632d6304316b244646dea079735b9a6fc4af804efb4752075b9fe2245e14e412";
void CUnsignedAlert::SetNull() void CUnsignedAlert::SetNull()

View File

@ -37,6 +37,9 @@ xCXXFLAGS=-O2 -w -Wall -Wextra -Wformat -Wformat-security -Wno-unused-parameter
xLDFLAGS=-Wl,--dynamicbase -Wl,--nxcompat -Wl,--large-address-aware -static-libgcc -static-libstdc++ $(LDFLAGS) xLDFLAGS=-Wl,--dynamicbase -Wl,--nxcompat -Wl,--large-address-aware -static-libgcc -static-libstdc++ $(LDFLAGS)
TESTDEFS = -DTEST_DATA_DIR=$(abspath test/data) TESTDEFS = -DTEST_DATA_DIR=$(abspath test/data)
ifdef UNIT_TEST
DEFS+=-DUNIT_TEST=1
endif
ifndef USE_UPNP ifndef USE_UPNP
override USE_UPNP = - override USE_UPNP = -

View File

@ -50,6 +50,9 @@ CFLAGS=-mthreads -O2 -w -Wall -Wextra -Wformat -Wformat-security -Wno-unused-par
LDFLAGS=-Wl,--dynamicbase -Wl,--nxcompat -Wl,--large-address-aware LDFLAGS=-Wl,--dynamicbase -Wl,--nxcompat -Wl,--large-address-aware
TESTDEFS = -DTEST_DATA_DIR=$(abspath test/data) TESTDEFS = -DTEST_DATA_DIR=$(abspath test/data)
ifdef UNIT_TEST
DEFS+=-DUNIT_TEST=1
endif
ifndef USE_UPNP ifndef USE_UPNP
override USE_UPNP = - override USE_UPNP = -

View File

@ -25,6 +25,9 @@ USE_IPV6:=1
LIBS= -dead_strip LIBS= -dead_strip
TESTDEFS = -DTEST_DATA_DIR=$(abspath test/data) TESTDEFS = -DTEST_DATA_DIR=$(abspath test/data)
ifdef UNIT_TEST
DEFS+=-DUNIT_TEST=1
endif
ifdef STATIC ifdef STATIC
# Build STATIC if you are redistributing the bitcoinf # Build STATIC if you are redistributing the bitcoinf

View File

@ -19,6 +19,9 @@ DEFS += $(addprefix -I,$(CURDIR) $(CURDIR)/obj $(BOOST_INCLUDE_PATH) $(BDB_INCLU
LIBS = $(addprefix -L,$(BOOST_LIB_PATH) $(BDB_LIB_PATH) $(OPENSSL_LIB_PATH)) LIBS = $(addprefix -L,$(BOOST_LIB_PATH) $(BDB_LIB_PATH) $(OPENSSL_LIB_PATH))
TESTDEFS = -DTEST_DATA_DIR=$(abspath test/data) TESTDEFS = -DTEST_DATA_DIR=$(abspath test/data)
ifdef UNIT_TEST
DEFS+=-DUNIT_TEST=1
endif
LMODE = dynamic LMODE = dynamic
LMODE2 = dynamic LMODE2 = dynamic

View File

@ -5,99 +5,157 @@
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
#include <fstream> #include <fstream>
#include <iostream>
#include <stdio.h>
#include "alert.h" #include "alert.h"
#include "base58.h"
#include "key.h"
#include "serialize.h" #include "serialize.h"
#include "util.h" #include "util.h"
#if 0 using namespace std;
// namespace fs = boost::filesystem;
// alertTests contains 7 alerts, generated with this code:
// (SignAndSave code not shown, alert signing key is secret) bool SignAndSave(CAlert &alert, std::vector<CAlert> &alerts)
//
{ {
CAlert alert; CDataStream ds(SER_DISK, PROTOCOL_VERSION);
alert.nRelayUntil = 60; ds << alert.nVersion
alert.nExpiration = 24 * 60 * 60; << alert.nRelayUntil
alert.nID = 1; << alert.nExpiration
alert.nCancel = 0; // cancels previous messages up to this ID number << alert.nID
alert.nMinVer = 0; // These versions are protocol versions << alert.nCancel
alert.nMaxVer = 70001; << alert.setCancel
alert.nPriority = 1; << alert.nMinVer
alert.strComment = "Alert comment"; << alert.nMaxVer
alert.strStatusBar = "Alert 1"; << alert.setSubVer
<< alert.nPriority
<< alert.strComment
<< alert.strStatusBar
<< alert.strReserved;
SignAndSave(alert, "test/alertTests"); alert.vchMsg.assign(ds.begin(),ds.end());
uint256 hash = alert.GetHash();
alert.setSubVer.insert(std::string("/Satoshi:0.1.0/")); std::vector<unsigned char> sig;
alert.strStatusBar = "Alert 1 for Satoshi 0.1.0"; CBitcoinSecret secret;
SignAndSave(alert, "test/alertTests"); if (!secret.SetString("7rDMuTnMxWdqRsvk5fYfwkaZoguWJMoDucyZvKmURVukdAkGiVb"))
{
alert.setSubVer.insert(std::string("/Satoshi:0.2.0/")); cout << "Error Setting Private Key" << endl;
alert.strStatusBar = "Alert 1 for Satoshi 0.1.0, 0.2.0"; return false;
SignAndSave(alert, "test/alertTests"); }
CKey key = secret.GetKey();
alert.setSubVer.clear();
++alert.nID; if (!key.Sign(hash, sig))
alert.nCancel = 1; {
alert.nPriority = 100; cout << "Could Not Sign Message" << endl;
alert.strStatusBar = "Alert 2, cancels 1"; return false;
SignAndSave(alert, "test/alertTests"); }
alert.vchSig = sig;
alert.nExpiration += 60;
++alert.nID; try
SignAndSave(alert, "test/alertTests"); {
alerts.push_back(alert);
++alert.nID; }
alert.nMinVer = 11;
alert.nMaxVer = 22; catch (std::exception &e)
SignAndSave(alert, "test/alertTests"); {
cout << "Exception caught " << e.what() << endl;
++alert.nID; }
alert.strStatusBar = "Alert 2 for Satoshi 0.1.0"; return true;
alert.setSubVer.insert(std::string("/Satoshi:0.1.0/"));
SignAndSave(alert, "test/alertTests"); }
++alert.nID; struct SetUpAlerts
alert.nMinVer = 0; {
alert.nMaxVer = 999999; SetUpAlerts()
alert.strStatusBar = "Evil Alert'; /bin/ls; echo '"; {
alert.setSubVer.clear();
SignAndSave(alert, "test/alertTests"); CAlert alert;
} alert.nRelayUntil = 60;
#endif alert.nExpiration = 24 * 60 * 60;
alert.nID = 1;
struct ReadAlerts alert.nCancel = 0; // cancels previous messages up to this ID number
{ alert.nMinVer = 0; // These versions are protocol versions
ReadAlerts() alert.nMaxVer = 70001;
alert.nPriority = 1;
alert.strComment = "Alert comment";
alert.strStatusBar = "Alert 1";
if (!SignAndSave(alert, alerts))
{
return;
}
CAlert alert2(alert);
alert2.setSubVer.insert(std::string("/Satoshi:0.1.0/"));
alert2.strStatusBar = "Alert 1 for Satoshi 0.1.0";
if (!SignAndSave(alert2, alerts))
{
return;
}
CAlert alert3(alert2);
alert3.setSubVer.insert(std::string("/Satoshi:0.2.0/"));
alert3.strStatusBar = "Alert 1 for Satoshi 0.1.0, 0.2.0";
if (!SignAndSave(alert3, alerts))
{
return;
}
CAlert alert4(alert3);
alert4.setSubVer.clear();
++alert4.nID;
alert4.nCancel = 1;
alert4.nPriority = 100;
alert4.strStatusBar = "Alert 2, cancels 1";
if (!SignAndSave(alert4, alerts))
{
return;
}
CAlert alert5(alert4);
alert5.nExpiration += 60;
++alert5.nID;
if (!SignAndSave(alert5, alerts))
{
return;
}
CAlert alert6(alert5);
++alert6.nID;
alert6.nMinVer = 11;
alert6.nMaxVer = 22;
if (!SignAndSave(alert6, alerts))
{
return;
}
CAlert alert7(alert6);
++alert7.nID;
alert7.strStatusBar = "Alert 2 for Satoshi 0.1.0";
alert7.setSubVer.insert(std::string("/Satoshi:0.1.0/"));
if (!SignAndSave(alert7, alerts))
{
return;
}
CAlert alert8(alert7);
++alert8.nID;
alert8.nMinVer = 0;
alert8.nMaxVer = 999999;
alert8.strStatusBar = "Evil Alert'; /bin/ls; echo '";
alert8.setSubVer.clear();
if (!SignAndSave(alert8, alerts))
{
return;
}
return;
}
~SetUpAlerts()
{ {
std::string filename("alertTests");
namespace fs = boost::filesystem;
fs::path testFile = fs::current_path() / "test" / "data" / filename;
#ifdef TEST_DATA_DIR
if (!fs::exists(testFile))
{
testFile = fs::path(BOOST_PP_STRINGIZE(TEST_DATA_DIR)) / filename;
}
#endif
FILE* fp = fopen(testFile.string().c_str(), "rb");
if (!fp) return;
CAutoFile filein = CAutoFile(fp, SER_DISK, CLIENT_VERSION);
if (!filein) return;
try {
while (!feof(filein))
{
CAlert alert;
filein >> alert;
alerts.push_back(alert);
}
}
catch (std::exception) { }
} }
~ReadAlerts() { }
static std::vector<std::string> read_lines(boost::filesystem::path filepath) static std::vector<std::string> read_lines(boost::filesystem::path filepath)
{ {
@ -105,7 +163,7 @@ struct ReadAlerts
std::ifstream f(filepath.string().c_str()); std::ifstream f(filepath.string().c_str());
std::string line; std::string line;
while (std::getline(f,line)) while (std::getline(f, line))
result.push_back(line); result.push_back(line);
return result; return result;
@ -114,8 +172,7 @@ struct ReadAlerts
std::vector<CAlert> alerts; std::vector<CAlert> alerts;
}; };
BOOST_FIXTURE_TEST_SUITE(Alert_tests, ReadAlerts) BOOST_FIXTURE_TEST_SUITE(Alert_tests, SetUpAlerts)
BOOST_AUTO_TEST_CASE(AlertApplies) BOOST_AUTO_TEST_CASE(AlertApplies)
{ {
@ -153,7 +210,6 @@ BOOST_AUTO_TEST_CASE(AlertApplies)
SetMockTime(0); SetMockTime(0);
} }
// This uses sh 'echo' to test the -alertnotify function, writing to a // This uses sh 'echo' to test the -alertnotify function, writing to a
// /tmp file. So skip it on Windows: // /tmp file. So skip it on Windows:
#ifndef WIN32 #ifndef WIN32
@ -167,17 +223,17 @@ BOOST_AUTO_TEST_CASE(AlertNotify)
mapArgs["-alertnotify"] = std::string("echo %s >> ") + temp.string(); mapArgs["-alertnotify"] = std::string("echo %s >> ") + temp.string();
BOOST_FOREACH(CAlert alert, alerts) BOOST_FOREACH(CAlert alert, alerts)
alert.ProcessAlert(false); alert.ProcessAlert(false);
std::vector<std::string> r = read_lines(temp); std::vector<std::string> r = read_lines(temp);
// //
// Only want to run these tests if the "alertnotify.txt" has been read OK and has at least one record // Only want to run these tests if the "alertnotify.txt" has been read OK and has at least one record
// in it. // in it.
// //
if (r.size() > 0 ) if (r.size() > 0)
{ {
BOOST_CHECK_EQUAL(r.size(), 1u); BOOST_CHECK_EQUAL(r.size(), 1u);
BOOST_CHECK_EQUAL(r[0], "Evil Alert; /bin/ls; echo "); // single-quotes should be removed BOOST_CHECK_EQUAL(r[0], "Evil Alert; /bin/ls; echo "); // single-quotes should be removed
} }
boost::filesystem::remove(temp); boost::filesystem::remove(temp);

Binary file not shown.