add SignAndSave to Alert_tests

This commit is contained in:
Jannis Froese 2014-04-12 23:03:58 +02:00
parent d02ef684c6
commit e791535df3

View file

@ -5,6 +5,7 @@
#include "alert.h" #include "alert.h"
#include "data/alertTests.raw.h" #include "data/alertTests.raw.h"
#include "key.h"
#include "serialize.h" #include "serialize.h"
#include "util.h" #include "util.h"
#include "version.h" #include "version.h"
@ -15,12 +16,57 @@
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
#if 0 /*this test relies on signed alerts in src/test/data/alertTests.raw
how to create this data:
1. set secretKey below to the secret key belonging to the mainnet vAlertPubKey from chainparams.cpp
2. delete the file src/test/alertTests.raw if it exists
3. compile the test suite with CREATE_ALERTS defined
4. execute this test suite
5. move the now created file src/test/data/alertTests.raw to src/test/data/alertTests.raw
6. undefine CREATE_ALERTS and compile the test suite again
*/
#ifdef CREATE_ALERTS
void SignAndSave(CAlert alert, const std::string filename) {
//serialze alert message
CDataStream sMsg(SER_NETWORK, PROTOCOL_VERSION);
sMsg << (CUnsignedAlert)alert;
alert.vchMsg.reserve(sMsg.size());
for(size_t i=0; i<sMsg.size(); i++) {
alert.vchMsg.push_back(sMsg[i]);
}
//a dummy secret key with the public key
//0469204F0E1800E16C1F85176BDC27A245F09987DB71A1EF5C4BD48A42F9AFD1D74F21469488DB552B594AC29CE667AD60DAAD0FFBCE03FB0C2AC49FFB07B36DC5
//set to match the mainnet vAlertPubKey from chainparams.cpp
const std::vector<unsigned char> secretKey = ParseHex("1A4976EE6174D80B8B3FF5E9B9C6E638CABFA9A5975557FEA967BC089A5584EE");
CKey secret;
secret.Set(secretKey.begin(), secretKey.end(), false);
assert(secret.IsValid());
//sign alert
secret.Sign(alert.GetHash(), alert.vchSig);
assert(alert.CheckSignature());
//serialize alert
CDataStream ss(SER_DISK, CLIENT_VERSION);
ss << alert;
//write alert
std::ofstream fs;
fs.open(filename.c_str(), std::ios::out | std::ios::app | std::ios::binary);
fs.write((char*)&ss[0], ss.size());
fs.close();
}
// //
// alertTests contains 7 alerts, generated with this code: // alertTests contains 7 alerts, generated with this code:
// (SignAndSave code not shown, alert signing key is secret)
// //
void CreateAlerts()
{ {
std::string filename("src/test/alertTests.raw");
CAlert alert; CAlert alert;
alert.nRelayUntil = 60; alert.nRelayUntil = 60;
alert.nExpiration = 24 * 60 * 60; alert.nExpiration = 24 * 60 * 60;
@ -32,43 +78,43 @@
alert.strComment = "Alert comment"; alert.strComment = "Alert comment";
alert.strStatusBar = "Alert 1"; alert.strStatusBar = "Alert 1";
SignAndSave(alert, "test/alertTests"); SignAndSave(alert, filename);
alert.setSubVer.insert(std::string("/Satoshi:0.1.0/")); alert.setSubVer.insert(std::string("/Satoshi:0.1.0/"));
alert.strStatusBar = "Alert 1 for Satoshi 0.1.0"; alert.strStatusBar = "Alert 1 for Satoshi 0.1.0";
SignAndSave(alert, "test/alertTests"); SignAndSave(alert, filename);
alert.setSubVer.insert(std::string("/Satoshi:0.2.0/")); alert.setSubVer.insert(std::string("/Satoshi:0.2.0/"));
alert.strStatusBar = "Alert 1 for Satoshi 0.1.0, 0.2.0"; alert.strStatusBar = "Alert 1 for Satoshi 0.1.0, 0.2.0";
SignAndSave(alert, "test/alertTests"); SignAndSave(alert, filename);
alert.setSubVer.clear(); alert.setSubVer.clear();
++alert.nID; ++alert.nID;
alert.nCancel = 1; alert.nCancel = 1;
alert.nPriority = 100; alert.nPriority = 100;
alert.strStatusBar = "Alert 2, cancels 1"; alert.strStatusBar = "Alert 2, cancels 1";
SignAndSave(alert, "test/alertTests"); SignAndSave(alert, filename);
alert.nExpiration += 60; alert.nExpiration += 60;
++alert.nID; ++alert.nID;
SignAndSave(alert, "test/alertTests"); SignAndSave(alert, filename);
++alert.nID; ++alert.nID;
alert.nMinVer = 11; alert.nMinVer = 11;
alert.nMaxVer = 22; alert.nMaxVer = 22;
SignAndSave(alert, "test/alertTests"); SignAndSave(alert, filename);
++alert.nID; ++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, filename);
++alert.nID; ++alert.nID;
alert.nMinVer = 0; alert.nMinVer = 0;
alert.nMaxVer = 999999; alert.nMaxVer = 999999;
alert.strStatusBar = "Evil Alert'; /bin/ls; echo '"; alert.strStatusBar = "Evil Alert'; /bin/ls; echo '";
alert.setSubVer.clear(); alert.setSubVer.clear();
SignAndSave(alert, "test/alertTests"); SignAndSave(alert, filename);
} }
#endif #endif
@ -76,6 +122,9 @@ struct ReadAlerts
{ {
ReadAlerts() ReadAlerts()
{ {
#ifdef CREATE_ALERTS
CreateAlerts();
#endif
std::vector<unsigned char> vch(alert_tests::alertTests, alert_tests::alertTests + sizeof(alert_tests::alertTests)); std::vector<unsigned char> vch(alert_tests::alertTests, alert_tests::alertTests + sizeof(alert_tests::alertTests));
CDataStream stream(vch, SER_DISK, CLIENT_VERSION); CDataStream stream(vch, SER_DISK, CLIENT_VERSION);
try { try {