Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for Alerts Unit Tests #24

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/unit-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use the Boost::Test unit-testing framework.
To compile and run the tests:

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

If all tests succeed the last line of output will be:
Expand Down
4 changes: 4 additions & 0 deletions src/alert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ using namespace std;
map<uint256, CAlert> mapAlerts;
CCriticalSection cs_mapAlerts;

#ifdef UNIT_TEST
static const char* pszMainKey = "043bcb5ad652b5680fadc0002a17c64542d2aff59c605d820960ab568af2c9ac7e3f130f7e4b638cfdc9069282f5500d67ee3f2803d6f0d6be44d387ac7ac286e4";
#else
static const char* pszMainKey = "048240a8748a80a286b270ba126705ced4f2ce5a7847b3610ea3c06513150dade2a8512ed5ea86320824683fc0818f0ac019214973e677acd1244f6d0571fc5103";
#endif
static const char* pszTestKey = "04517d8a699cb43d3938d7b24faaff7cda448ca4ea267723ba614784de661949bf632d6304316b244646dea079735b9a6fc4af804efb4752075b9fe2245e14e412";

void CUnsignedAlert::SetNull()
Expand Down
3 changes: 3 additions & 0 deletions src/makefile.linux-mingw
Original file line number Diff line number Diff line change
Expand Up @@ -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)

TESTDEFS = -DTEST_DATA_DIR=$(abspath test/data)
ifdef UNIT_TEST
DEFS+=-DUNIT_TEST=1
endif

ifndef USE_UPNP
override USE_UPNP = -
Expand Down
3 changes: 3 additions & 0 deletions src/makefile.mingw
Original file line number Diff line number Diff line change
Expand Up @@ -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

TESTDEFS = -DTEST_DATA_DIR=$(abspath test/data)
ifdef UNIT_TEST
DEFS+=-DUNIT_TEST=1
endif

ifndef USE_UPNP
override USE_UPNP = -
Expand Down
3 changes: 3 additions & 0 deletions src/makefile.osx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ USE_IPV6:=1
LIBS= -dead_strip

TESTDEFS = -DTEST_DATA_DIR=$(abspath test/data)
ifdef UNIT_TEST
DEFS+=-DUNIT_TEST=1
endif

ifdef STATIC
# Build STATIC if you are redistributing the bitcoinf
Expand Down
3 changes: 3 additions & 0 deletions src/makefile.unix
Original file line number Diff line number Diff line change
Expand Up @@ -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))

TESTDEFS = -DTEST_DATA_DIR=$(abspath test/data)
ifdef UNIT_TEST
DEFS+=-DUNIT_TEST=1
endif

LMODE = dynamic
LMODE2 = dynamic
Expand Down
23 changes: 11 additions & 12 deletions src/test/Checkpoints_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,25 @@

using namespace std;

/*BOOST_AUTO_TEST_SUITE(Checkpoints_tests)
BOOST_AUTO_TEST_SUITE(Checkpoints_tests)

BOOST_AUTO_TEST_CASE(sanity)
{
uint256 p1500 = uint256("0x841a2965955dd288cfa707a755d05a54e45f8bd476835ec9af4402a2b59a2967");
uint256 p120000 = uint256("0xbd9d26924f05f6daa7f0155f32828ec89e8e29cee9e7121b026a7a3552ac6131");
uint256 p1500 = uint256("0x000000aaf0300f59f49bc3e970bad15c11f961fe2347accffff19d96ec9778e3");
uint256 p88805 = uint256("0x00000000001392f1652e9bf45cd8bc79dc60fe935277cd11538565b4a94fa85f");
BOOST_CHECK(Checkpoints::CheckBlock(1500, p1500));
BOOST_CHECK(Checkpoints::CheckBlock(120000, p120000));
BOOST_CHECK(Checkpoints::CheckBlock(88805, p88805));



// Wrong hashes at checkpoints should fail:
BOOST_CHECK(!Checkpoints::CheckBlock(1500, p120000));
BOOST_CHECK(!Checkpoints::CheckBlock(120000, p1500));
BOOST_CHECK(!Checkpoints::CheckBlock(1500, p88805));
BOOST_CHECK(!Checkpoints::CheckBlock(88805, p1500));

// ... but any hash not at a checkpoint should succeed:
BOOST_CHECK(Checkpoints::CheckBlock(1500+1, p120000));
BOOST_CHECK(Checkpoints::CheckBlock(120000+1, p1500));
BOOST_CHECK(Checkpoints::CheckBlock(1500+1, p88805));
BOOST_CHECK(Checkpoints::CheckBlock(88805+1, p1500));

BOOST_CHECK(Checkpoints::GetTotalBlocksEstimate() >= 120000);
}
BOOST_CHECK(Checkpoints::GetTotalBlocksEstimate() >= 88805);
}

BOOST_AUTO_TEST_SUITE_END()
*/
224 changes: 140 additions & 84 deletions src/test/alert_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,107 +5,165 @@
#include <boost/foreach.hpp>
#include <boost/test/unit_test.hpp>
#include <fstream>
#include <iostream>
#include <stdio.h>

#include "alert.h"
#include "base58.h"
#include "key.h"
#include "serialize.h"
#include "util.h"

#if 0
//
// alertTests contains 7 alerts, generated with this code:
// (SignAndSave code not shown, alert signing key is secret)
//
using namespace std;
namespace fs = boost::filesystem;

bool SignAndSave(CAlert &alert, std::vector<CAlert> &alerts)
{
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
alert.nMaxVer = 70001;
alert.nPriority = 1;
alert.strComment = "Alert comment";
alert.strStatusBar = "Alert 1";

SignAndSave(alert, "test/alertTests");

alert.setSubVer.insert(std::string("/Satoshi:0.1.0/"));
alert.strStatusBar = "Alert 1 for Satoshi 0.1.0";
SignAndSave(alert, "test/alertTests");

alert.setSubVer.insert(std::string("/Satoshi:0.2.0/"));
alert.strStatusBar = "Alert 1 for Satoshi 0.1.0, 0.2.0";
SignAndSave(alert, "test/alertTests");

alert.setSubVer.clear();
++alert.nID;
alert.nCancel = 1;
alert.nPriority = 100;
alert.strStatusBar = "Alert 2, cancels 1";
SignAndSave(alert, "test/alertTests");

alert.nExpiration += 60;
++alert.nID;
SignAndSave(alert, "test/alertTests");

++alert.nID;
alert.nMinVer = 11;
alert.nMaxVer = 22;
SignAndSave(alert, "test/alertTests");

++alert.nID;
alert.strStatusBar = "Alert 2 for Satoshi 0.1.0";
alert.setSubVer.insert(std::string("/Satoshi:0.1.0/"));
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");
CDataStream ds(SER_DISK, PROTOCOL_VERSION);
ds << alert.nVersion
<< alert.nRelayUntil
<< alert.nExpiration
<< alert.nID
<< alert.nCancel
<< alert.setCancel
<< alert.nMinVer
<< alert.nMaxVer
<< alert.setSubVer
<< alert.nPriority
<< alert.strComment
<< alert.strStatusBar
<< alert.strReserved;

alert.vchMsg.assign(ds.begin(),ds.end());
uint256 hash = alert.GetHash();
std::vector<unsigned char> sig;
CBitcoinSecret secret;
if (!secret.SetString("7rDMuTnMxWdqRsvk5fYfwkaZoguWJMoDucyZvKmURVukdAkGiVb"))
{
cout << "Error Setting Private Key" << endl;
return false;
}
CKey key = secret.GetKey();

if (!key.Sign(hash, sig))
{
cout << "Could Not Sign Message" << endl;
return false;
}
alert.vchSig = sig;

try
{
alerts.push_back(alert);
}

catch (std::exception &e)
{
cout << "Exception caught " << e.what() << endl;
}
return true;

}
#endif

struct ReadAlerts
struct SetUpAlerts
{
ReadAlerts()
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))

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
alert.nMaxVer = 70001;
alert.nPriority = 1;
alert.strComment = "Alert comment";
alert.strStatusBar = "Alert 1";

if (!SignAndSave(alert, alerts))
{
testFile = fs::path(BOOST_PP_STRINGIZE(TEST_DATA_DIR)) / filename;
return;
}
#endif
FILE* fp = fopen(testFile.string().c_str(), "rb");
if (!fp) 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;
}

CAutoFile filein = CAutoFile(fp, SER_DISK, CLIENT_VERSION);
if (!filein) 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;
}

try {
while (!feof(filein))
{
CAlert alert;
filein >> alert;
alerts.push_back(alert);
}
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;
}
catch (std::exception) { }

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()
{
}
~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))
while (std::getline(f, line))
result.push_back(line);

return result;
Expand All @@ -114,8 +172,7 @@ struct ReadAlerts
std::vector<CAlert> alerts;
};

BOOST_FIXTURE_TEST_SUITE(Alert_tests, ReadAlerts)

BOOST_FIXTURE_TEST_SUITE(Alert_tests, SetUpAlerts)

BOOST_AUTO_TEST_CASE(AlertApplies)
{
Expand Down Expand Up @@ -153,7 +210,6 @@ BOOST_AUTO_TEST_CASE(AlertApplies)
SetMockTime(0);
}


// This uses sh 'echo' to test the -alertnotify function, writing to a
// /tmp file. So skip it on Windows:
#ifndef WIN32
Expand All @@ -167,17 +223,17 @@ BOOST_AUTO_TEST_CASE(AlertNotify)
mapArgs["-alertnotify"] = std::string("echo %s >> ") + temp.string();

BOOST_FOREACH(CAlert alert, alerts)
alert.ProcessAlert(false);
alert.ProcessAlert(false);

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
// in it.
//
if (r.size() > 0 )
if (r.size() > 0)
{
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.size(), 1u);
BOOST_CHECK_EQUAL(r[0], "Evil Alert; /bin/ls; echo "); // single-quotes should be removed
}
boost::filesystem::remove(temp);

Expand Down
3 changes: 1 addition & 2 deletions src/test/base58_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "base58.h"
#include "util.h"

/*using namespace json_spirit;
using namespace json_spirit;
extern Array read_json(const std::string& filename);

BOOST_AUTO_TEST_SUITE(base58_tests)
Expand Down Expand Up @@ -259,4 +259,3 @@ BOOST_AUTO_TEST_CASE(base58_keys_invalid)

BOOST_AUTO_TEST_SUITE_END()

*/
Loading