From 939aaeb6208d0bbdf115f95799dd5b1271f386a1 Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 17 Jul 2016 12:36:26 -0700 Subject: [PATCH] New alert test data generated for new alert key pair. Added test fixture to create new test data. Added instructions for developer. --- src/test/alert_tests.cpp | 178 ++++++++++++++++++++++++++++++++--- src/test/data/alertTests.raw | Bin 1279 -> 1280 bytes 2 files changed, 163 insertions(+), 15 deletions(-) diff --git a/src/test/alert_tests.cpp b/src/test/alert_tests.cpp index daaec13ed..ccfb6132b 100644 --- a/src/test/alert_tests.cpp +++ b/src/test/alert_tests.cpp @@ -26,62 +26,208 @@ #include #include -#if 0 -// -// alertTests contains 7 alerts, generated with this code: -// (SignAndSave code not shown, alert signing key is secret) -// +#include "key.h" +#include "alertkeys.h" +#include + +/* + * If the alert key pairs have changed, the test suite will fail as the + * test data is now invalid. To create valid test data, signed with a + * new alert private key, follow these steps: + * + * 1. Copy your private key into alertkeys.h. Don't commit this file! + * See sendalert.cpp for more info. + * + * 2. Set the GENERATE_ALERTS_FLAG to true. + * + * 3. Build and run: + * test_bitcoin -t Generate_Alert_Test_Data + * + * 4. Test data is saved in your current directory as alertTests.raw.NEW + * Copy this file to: src/test/data/alertTests.raw + * + * For debugging purposes, terminal output can be copied into: + * src/test/data/alertTests.raw.h + * + * 5. Clean up... + * - Set GENERATE_ALERTS_FLAG back to false. + * - Remove your private key from alertkeys.h + * + * 6. Build and verify the new test data: + * test_bitcoin -t Alert_tests + * + */ +#define GENERATE_ALERTS_FLAG false + +#if GENERATE_ALERTS_FLAG + +// NOTE: +// A function SignAndSave() was used by Bitcoin Core to create alert test data +// but it has not been made publicly available. So instead, we have adapted +// some publicly available code which achieves the intended result: +// https://gist.github.com/lukem512/9b272bd35e2cdefbf386 + + +// Code to output a C-style array of values +template +std::string HexStrArray(const T itbegin, const T itend, int lineLength) { + std::string rv; + static const char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; + rv.reserve((itend-itbegin)*3); + int i = 0; + for(T it = itbegin; it < itend; ++it) + { + unsigned char val = (unsigned char)(*it); + if(it != itbegin) + { + if (i % lineLength == 0) + rv.push_back('\n'); + else + rv.push_back(' '); + } + rv.push_back('0'); + rv.push_back('x'); + rv.push_back(hexmap[val>>4]); + rv.push_back(hexmap[val&15]); + rv.push_back(','); + i++; + } + + return rv; +} + +template +inline std::string HexStrArray(const T& vch, int lineLength) +{ + return HexStrArray(vch.begin(), vch.end(), lineLength); +} + + +// Sign CAlert with alert private key +bool SignAlert(CAlert &alert) +{ + // serialize alert data + CDataStream sMsg(SER_NETWORK, PROTOCOL_VERSION); + sMsg << *(CUnsignedAlert*)&alert; + alert.vchMsg = std::vector(sMsg.begin(), sMsg.end()); + + // sign alert + std::vector vchTmp(ParseHex(pszPrivKey)); + CPrivKey vchPrivKey(vchTmp.begin(), vchTmp.end()); + CKey key; + if (!key.SetPrivKey(vchPrivKey, false)) + { + printf("key.SetPrivKey failed\n"); + return false; + } + if (!key.Sign(Hash(alert.vchMsg.begin(), alert.vchMsg.end()), alert.vchSig)) + { + printf("SignAlert() : key.Sign failed\n"); + return false; + } + return true; +} + +// Sign a CAlert and serialize it +bool SignAndSerialize(CAlert &alert, CDataStream &buffer) +{ + // Sign + if(!SignAlert(alert)) + { + printf("SignAndSerialize() : could not sign alert\n"); + return false; + } + // ...and save! + buffer << alert; + return true; +} + +void GenerateAlertTests() +{ + CDataStream sBuffer(SER_DISK, CLIENT_VERSION); + 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.nCancel = 0; // cancels previous messages up to this ID number alert.nMinVer = 0; // These versions are protocol versions alert.nMaxVer = 999001; alert.nPriority = 1; alert.strComment = "Alert comment"; alert.strStatusBar = "Alert 1"; - SignAndSave(alert, "test/alertTests"); + // Replace SignAndSave with SignAndSerialize + SignAndSerialize(alert, sBuffer); + // More tests go here ... alert.setSubVer.insert(std::string("/Satoshi:0.1.0/")); alert.strStatusBar = "Alert 1 for Satoshi 0.1.0"; - SignAndSave(alert, "test/alertTests"); + SignAndSerialize(alert, sBuffer); 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"); + SignAndSerialize(alert, sBuffer); alert.setSubVer.clear(); ++alert.nID; alert.nCancel = 1; alert.nPriority = 100; alert.strStatusBar = "Alert 2, cancels 1"; - SignAndSave(alert, "test/alertTests"); + SignAndSerialize(alert, sBuffer); alert.nExpiration += 60; ++alert.nID; - SignAndSave(alert, "test/alertTests"); + SignAndSerialize(alert, sBuffer); ++alert.nID; alert.nMinVer = 11; alert.nMaxVer = 22; - SignAndSave(alert, "test/alertTests"); + SignAndSerialize(alert, sBuffer); ++alert.nID; alert.strStatusBar = "Alert 2 for Satoshi 0.1.0"; alert.setSubVer.insert(std::string("/Satoshi:0.1.0/")); - SignAndSave(alert, "test/alertTests"); + SignAndSerialize(alert, sBuffer); ++alert.nID; alert.nMinVer = 0; alert.nMaxVer = 999999; alert.strStatusBar = "Evil Alert'; /bin/ls; echo '"; alert.setSubVer.clear(); - SignAndSave(alert, "test/alertTests"); + bool b = SignAndSerialize(alert, sBuffer); + + if (b) { + // Print the hex array, which will become the contents of alertTest.raw.h + std::vector vch = std::vector(sBuffer.begin(), sBuffer.end()); + printf("%s\n", HexStrArray(vch, 8).c_str()); + + // Write the data to alertTests.raw.NEW, to be copied to src/test/data/alertTests.raw + std::ofstream outfile("alertTests.raw.NEW", std::ios::out | std::ios::binary); + outfile.write((const char*)&vch[0], vch.size()); + outfile.close(); + } } -#endif + + + +struct GenerateAlertTestsFixture : public TestingSetup { + GenerateAlertTestsFixture() {} + ~GenerateAlertTestsFixture() {} +}; + +BOOST_FIXTURE_TEST_SUITE(Generate_Alert_Test_Data, GenerateAlertTestsFixture); +BOOST_AUTO_TEST_CASE(GenerateTheAlertTests) +{ + GenerateAlertTests(); +} +BOOST_AUTO_TEST_SUITE_END() + + +#else + struct ReadAlerts : public TestingSetup { @@ -255,3 +401,5 @@ BOOST_AUTO_TEST_CASE(PartitionAlert) } BOOST_AUTO_TEST_SUITE_END() + +#endif diff --git a/src/test/data/alertTests.raw b/src/test/data/alertTests.raw index 01f50680b95aa307a1a013b643ded4ae0bc47162..019f2b0df7b1ce6d483dff6d22a148970c652d26 100644 GIT binary patch delta 607 zcmV-l0-*i>34jWaNJG)Af1qUj_*g=5VAQ*Ax@)#gBRt_1y}+gYE94` zn2TX}pMFiofATx&%8qg{Kye#vm z&%g43J2xi1Hl#ikv~}#_8nB&M{Y;So6q5`BI7mk@MFJrJu;W7e)*A4BVEC`3NHYwFBatzVFoTmI z0~M2?0zN|hgf6G!(y%cSQY!6D zL;@gpEOmcwys%z}z!lI-MfXVM0P3&7P6&gZRSeK$H9FY>Ab34T7eq9>%Ei3rpT)>q zz delta 601 zcmV-f0;c_d3jYa^NJE8;b+;Sx;MM_n_qSI=uI_T@JBiVZ&z}$vcMWoWe#rtLFO`Ad zW4{DI=!mp$?Qe4j;D6mVK$hFIGC*=!|EhP8W03(Rk+4HU8rjpv4@oAeKBfWp>hRUU zTi*EpwpJ1qLE8F6z6nZ<0w4_=TsqWmy68o&hhF(s)zAz*X!{ySP&)qFaOgN1tG9ZQ z0VR`L0a`;Z8YGKV6rm1(kto%O`B6l~8)aUp@Um`BbuD&(`ATf^=g87GZ(T&>t zjU1NZDX5|vVZWm)ebMJ7*F~UCHcXKL6q5`BI7db>L;@f#*Rq=*q!kH8@OG&`dnyHy zemm`c1i!uG*D+PovaX~8AU8w2H$NvsT41Ja^PbA~V7ZWHz0Ctqm^7@&R%H-bCzBxq z6_cR?K0=3;{q|EZ-MQ$8_;$cxXW3%=diB+jEX?@on6wyO(;xyMMh~mW!Ai!fEbt3i zQ6suCpTF6jf3GSzyEz2H)+y87lOY3BLb*4;UE*aml`oG+jA3)t8#ezUM-sC&(S>fF znRu}6<^mv86DIjio&imfTihyRn`96x8FAL!*$D7xp_10|D9eAIQ$P>(A*sE>gwyWSVS