1 // Copyright (c) 2013 The Bitcoin Core developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 // Unit tests for alert system
10 #include "data/alertTests.raw.h"
12 #include "serialize.h"
18 #include <boost/filesystem/operations.hpp>
19 #include <boost/foreach.hpp>
20 #include <boost/test/unit_test.hpp>
24 // alertTests contains 7 alerts, generated with this code:
25 // (SignAndSave code not shown, alert signing key is secret)
29 alert
.nRelayUntil
= 60;
30 alert
.nExpiration
= 24 * 60 * 60;
32 alert
.nCancel
= 0; // cancels previous messages up to this ID number
33 alert
.nMinVer
= 0; // These versions are protocol versions
34 alert
.nMaxVer
= 999001;
36 alert
.strComment
= "Alert comment";
37 alert
.strStatusBar
= "Alert 1";
39 SignAndSave(alert
, "test/alertTests");
41 alert
.setSubVer
.insert(std::string("/Satoshi:0.1.0/"));
42 alert
.strStatusBar
= "Alert 1 for Satoshi 0.1.0";
43 SignAndSave(alert
, "test/alertTests");
45 alert
.setSubVer
.insert(std::string("/Satoshi:0.2.0/"));
46 alert
.strStatusBar
= "Alert 1 for Satoshi 0.1.0, 0.2.0";
47 SignAndSave(alert
, "test/alertTests");
49 alert
.setSubVer
.clear();
52 alert
.nPriority
= 100;
53 alert
.strStatusBar
= "Alert 2, cancels 1";
54 SignAndSave(alert
, "test/alertTests");
56 alert
.nExpiration
+= 60;
58 SignAndSave(alert
, "test/alertTests");
63 SignAndSave(alert
, "test/alertTests");
66 alert
.strStatusBar
= "Alert 2 for Satoshi 0.1.0";
67 alert
.setSubVer
.insert(std::string("/Satoshi:0.1.0/"));
68 SignAndSave(alert
, "test/alertTests");
72 alert
.nMaxVer
= 999999;
73 alert
.strStatusBar
= "Evil Alert'; /bin/ls; echo '";
74 alert
.setSubVer
.clear();
75 SignAndSave(alert
, "test/alertTests");
83 std::vector
<unsigned char> vch(alert_tests::alertTests
, alert_tests::alertTests
+ sizeof(alert_tests::alertTests
));
84 CDataStream
stream(vch
, SER_DISK
, CLIENT_VERSION
);
90 alerts
.push_back(alert
);
93 catch (std::exception
) { }
97 static std::vector
<std::string
> read_lines(boost::filesystem::path filepath
)
99 std::vector
<std::string
> result
;
101 std::ifstream
f(filepath
.string().c_str());
103 while (std::getline(f
,line
))
104 result
.push_back(line
);
109 std::vector
<CAlert
> alerts
;
112 BOOST_FIXTURE_TEST_SUITE(Alert_tests
, ReadAlerts
)
115 BOOST_AUTO_TEST_CASE(AlertApplies
)
119 BOOST_FOREACH(const CAlert
& alert
, alerts
)
121 BOOST_CHECK(alert
.CheckSignature());
124 BOOST_CHECK(alerts
.size() >= 3);
127 BOOST_CHECK(alerts
[0].AppliesTo(1, ""));
128 BOOST_CHECK(alerts
[0].AppliesTo(999001, ""));
129 BOOST_CHECK(alerts
[0].AppliesTo(1, "/Satoshi:11.11.11/"));
131 BOOST_CHECK(alerts
[1].AppliesTo(1, "/Satoshi:0.1.0/"));
132 BOOST_CHECK(alerts
[1].AppliesTo(999001, "/Satoshi:0.1.0/"));
134 BOOST_CHECK(alerts
[2].AppliesTo(1, "/Satoshi:0.1.0/"));
135 BOOST_CHECK(alerts
[2].AppliesTo(1, "/Satoshi:0.2.0/"));
138 BOOST_CHECK(!alerts
[0].AppliesTo(-1, ""));
139 BOOST_CHECK(!alerts
[0].AppliesTo(999002, ""));
141 BOOST_CHECK(!alerts
[1].AppliesTo(1, ""));
142 BOOST_CHECK(!alerts
[1].AppliesTo(1, "Satoshi:0.1.0"));
143 BOOST_CHECK(!alerts
[1].AppliesTo(1, "/Satoshi:0.1.0"));
144 BOOST_CHECK(!alerts
[1].AppliesTo(1, "Satoshi:0.1.0/"));
145 BOOST_CHECK(!alerts
[1].AppliesTo(-1, "/Satoshi:0.1.0/"));
146 BOOST_CHECK(!alerts
[1].AppliesTo(999002, "/Satoshi:0.1.0/"));
147 BOOST_CHECK(!alerts
[1].AppliesTo(1, "/Satoshi:0.2.0/"));
149 BOOST_CHECK(!alerts
[2].AppliesTo(1, "/Satoshi:0.3.0/"));
155 // This uses sh 'echo' to test the -alertnotify function, writing to a
156 // /tmp file. So skip it on Windows:
158 BOOST_AUTO_TEST_CASE(AlertNotify
)
162 boost::filesystem::path temp
= GetTempPath() / "alertnotify.txt";
163 boost::filesystem::remove(temp
);
165 mapArgs
["-alertnotify"] = std::string("echo %s >> ") + temp
.string();
167 BOOST_FOREACH(CAlert alert
, alerts
)
168 alert
.ProcessAlert(false);
170 std::vector
<std::string
> r
= read_lines(temp
);
171 BOOST_CHECK_EQUAL(r
.size(), 4u);
172 BOOST_CHECK_EQUAL(r
[0], "Alert 1");
173 BOOST_CHECK_EQUAL(r
[1], "Alert 2, cancels 1");
174 BOOST_CHECK_EQUAL(r
[2], "Alert 2, cancels 1");
175 BOOST_CHECK_EQUAL(r
[3], "Evil Alert; /bin/ls; echo "); // single-quotes should be removed
177 boost::filesystem::remove(temp
);
183 BOOST_AUTO_TEST_SUITE_END()