]> Git Repo - VerusCoin.git/blob - src/test/alert_tests.cpp
Merge pull request #2945 from gmaxwell/fee-logic_encourage_sweeping
[VerusCoin.git] / src / test / alert_tests.cpp
1 //
2 // Unit tests for alert system
3 //
4
5 #include <boost/foreach.hpp>
6 #include <boost/test/unit_test.hpp>
7 #include <fstream>
8
9 #include "alert.h"
10 #include "serialize.h"
11 #include "util.h"
12 #include "data/alertTests.raw.h"
13
14 #if 0
15 //
16 // alertTests contains 7 alerts, generated with this code:
17 // (SignAndSave code not shown, alert signing key is secret)
18 //
19 {
20     CAlert alert;
21     alert.nRelayUntil   = 60;
22     alert.nExpiration   = 24 * 60 * 60;
23     alert.nID           = 1;
24     alert.nCancel       = 0;   // cancels previous messages up to this ID number
25     alert.nMinVer       = 0;  // These versions are protocol versions
26     alert.nMaxVer       = 70001;
27     alert.nPriority     = 1;
28     alert.strComment    = "Alert comment";
29     alert.strStatusBar  = "Alert 1";
30
31     SignAndSave(alert, "test/alertTests");
32
33     alert.setSubVer.insert(std::string("/Satoshi:0.1.0/"));
34     alert.strStatusBar  = "Alert 1 for Satoshi 0.1.0";
35     SignAndSave(alert, "test/alertTests");
36
37     alert.setSubVer.insert(std::string("/Satoshi:0.2.0/"));
38     alert.strStatusBar  = "Alert 1 for Satoshi 0.1.0, 0.2.0";
39     SignAndSave(alert, "test/alertTests");
40
41     alert.setSubVer.clear();
42     ++alert.nID;
43     alert.nCancel = 1;
44     alert.nPriority = 100;
45     alert.strStatusBar  = "Alert 2, cancels 1";
46     SignAndSave(alert, "test/alertTests");
47
48     alert.nExpiration += 60;
49     ++alert.nID;
50     SignAndSave(alert, "test/alertTests");
51
52     ++alert.nID;
53     alert.nMinVer = 11;
54     alert.nMaxVer = 22;
55     SignAndSave(alert, "test/alertTests");
56
57     ++alert.nID;
58     alert.strStatusBar  = "Alert 2 for Satoshi 0.1.0";
59     alert.setSubVer.insert(std::string("/Satoshi:0.1.0/"));
60     SignAndSave(alert, "test/alertTests");
61
62     ++alert.nID;
63     alert.nMinVer = 0;
64     alert.nMaxVer = 999999;
65     alert.strStatusBar  = "Evil Alert'; /bin/ls; echo '";
66     alert.setSubVer.clear();
67     SignAndSave(alert, "test/alertTests");
68 }
69 #endif
70
71 struct ReadAlerts
72 {
73     ReadAlerts()
74     {
75         std::vector<unsigned char> vch(alert_tests::alertTests, alert_tests::alertTests + sizeof(alert_tests::alertTests));
76         CDataStream stream(vch, SER_DISK, CLIENT_VERSION);
77         try {
78             while (stream.good())
79             {
80                 CAlert alert;
81                 stream >> alert;
82                 alerts.push_back(alert);
83             }
84         }
85         catch (std::exception) { }
86     }
87     ~ReadAlerts() { }
88
89     static std::vector<std::string> read_lines(boost::filesystem::path filepath)
90     {
91         std::vector<std::string> result;
92
93         std::ifstream f(filepath.string().c_str());
94         std::string line;
95         while (std::getline(f,line))
96             result.push_back(line);
97
98         return result;
99     }
100
101     std::vector<CAlert> alerts;
102 };
103
104 BOOST_FIXTURE_TEST_SUITE(Alert_tests, ReadAlerts)
105
106
107 BOOST_AUTO_TEST_CASE(AlertApplies)
108 {
109     SetMockTime(11);
110
111     BOOST_FOREACH(const CAlert& alert, alerts)
112     {
113         BOOST_CHECK(alert.CheckSignature());
114     }
115
116     BOOST_CHECK(alerts.size() >= 3);
117
118     // Matches:
119     BOOST_CHECK(alerts[0].AppliesTo(1, ""));
120     BOOST_CHECK(alerts[0].AppliesTo(70001, ""));
121     BOOST_CHECK(alerts[0].AppliesTo(1, "/Satoshi:11.11.11/"));
122
123     BOOST_CHECK(alerts[1].AppliesTo(1, "/Satoshi:0.1.0/"));
124     BOOST_CHECK(alerts[1].AppliesTo(70001, "/Satoshi:0.1.0/"));
125
126     BOOST_CHECK(alerts[2].AppliesTo(1, "/Satoshi:0.1.0/"));
127     BOOST_CHECK(alerts[2].AppliesTo(1, "/Satoshi:0.2.0/"));
128
129     // Don't match:
130     BOOST_CHECK(!alerts[0].AppliesTo(-1, ""));
131     BOOST_CHECK(!alerts[0].AppliesTo(70002, ""));
132
133     BOOST_CHECK(!alerts[1].AppliesTo(1, ""));
134     BOOST_CHECK(!alerts[1].AppliesTo(1, "Satoshi:0.1.0"));
135     BOOST_CHECK(!alerts[1].AppliesTo(1, "/Satoshi:0.1.0"));
136     BOOST_CHECK(!alerts[1].AppliesTo(1, "Satoshi:0.1.0/"));
137     BOOST_CHECK(!alerts[1].AppliesTo(-1, "/Satoshi:0.1.0/"));
138     BOOST_CHECK(!alerts[1].AppliesTo(70002, "/Satoshi:0.1.0/"));
139     BOOST_CHECK(!alerts[1].AppliesTo(1, "/Satoshi:0.2.0/"));
140
141     BOOST_CHECK(!alerts[2].AppliesTo(1, "/Satoshi:0.3.0/"));
142
143     SetMockTime(0);
144 }
145
146
147 // This uses sh 'echo' to test the -alertnotify function, writing to a
148 // /tmp file. So skip it on Windows:
149 #ifndef WIN32
150 BOOST_AUTO_TEST_CASE(AlertNotify)
151 {
152     SetMockTime(11);
153
154     boost::filesystem::path temp = GetTempPath() / "alertnotify.txt";
155     boost::filesystem::remove(temp);
156
157     mapArgs["-alertnotify"] = std::string("echo %s >> ") + temp.string();
158
159     BOOST_FOREACH(CAlert alert, alerts)
160         alert.ProcessAlert(false);
161
162     std::vector<std::string> r = read_lines(temp);
163     BOOST_CHECK_EQUAL(r.size(), 4u);
164     BOOST_CHECK_EQUAL(r[0], "Alert 1");
165     BOOST_CHECK_EQUAL(r[1], "Alert 2, cancels 1");
166     BOOST_CHECK_EQUAL(r[2], "Alert 2, cancels 1");
167     BOOST_CHECK_EQUAL(r[3], "Evil Alert; /bin/ls; echo "); // single-quotes should be removed
168
169     boost::filesystem::remove(temp);
170
171     SetMockTime(0);
172 }
173 #endif
174
175 BOOST_AUTO_TEST_SUITE_END()
This page took 0.034031 seconds and 4 git commands to generate.