diff --git a/doc/unit-tests.md b/doc/unit-tests.md index 2318c0db25..5eaea0365d 100644 --- a/doc/unit-tests.md +++ b/doc/unit-tests.md @@ -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: diff --git a/src/alert.cpp b/src/alert.cpp index e5c6ec36c9..e6bed97d92 100644 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -19,7 +19,11 @@ using namespace std; map 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() diff --git a/src/makefile.linux-mingw b/src/makefile.linux-mingw index 5637b6f683..97a48ff7c3 100644 --- a/src/makefile.linux-mingw +++ b/src/makefile.linux-mingw @@ -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 = - diff --git a/src/makefile.mingw b/src/makefile.mingw index 1c5b634ee5..8f925a487c 100644 --- a/src/makefile.mingw +++ b/src/makefile.mingw @@ -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 = - diff --git a/src/makefile.osx b/src/makefile.osx index 038135cf30..46223b58a1 100644 --- a/src/makefile.osx +++ b/src/makefile.osx @@ -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 diff --git a/src/makefile.unix b/src/makefile.unix index 78f7d2377d..2fbb8364c6 100644 --- a/src/makefile.unix +++ b/src/makefile.unix @@ -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 diff --git a/src/test/alert_tests.cpp b/src/test/alert_tests.cpp index 3b32912551..db9df1c56a 100644 --- a/src/test/alert_tests.cpp +++ b/src/test/alert_tests.cpp @@ -5,99 +5,157 @@ #include #include #include +#include +#include #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 &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"; + 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; - 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"); -} -#endif - -struct ReadAlerts -{ - ReadAlerts() + alert.vchMsg.assign(ds.begin(),ds.end()); + uint256 hash = alert.GetHash(); + std::vector 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; + +} + +struct SetUpAlerts +{ + SetUpAlerts() + { + + 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)) + { + 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 read_lines(boost::filesystem::path filepath) { @@ -105,7 +163,7 @@ struct ReadAlerts 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; @@ -114,8 +172,7 @@ struct ReadAlerts std::vector alerts; }; -BOOST_FIXTURE_TEST_SUITE(Alert_tests, ReadAlerts) - +BOOST_FIXTURE_TEST_SUITE(Alert_tests, SetUpAlerts) BOOST_AUTO_TEST_CASE(AlertApplies) { @@ -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 @@ -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 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); diff --git a/src/test/data/alertTests b/src/test/data/alertTests deleted file mode 100644 index 24e906c1a5..0000000000 Binary files a/src/test/data/alertTests and /dev/null differ