Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
alert_tests.cpp
Go to the documentation of this file.
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 
13 #if 0
14 //
15 // alertTests contains 7 alerts, generated with this code:
16 // (SignAndSave code not shown, alert signing key is secret)
17 //
18 {
19  CAlert alert;
20  alert.nRelayUntil = 60;
21  alert.nExpiration = 24 * 60 * 60;
22  alert.nID = 1;
23  alert.nCancel = 0; // cancels previous messages up to this ID number
24  alert.nMinVer = 0; // These versions are protocol versions
25  alert.nMaxVer = 70001;
26  alert.nPriority = 1;
27  alert.strComment = "Alert comment";
28  alert.strStatusBar = "Alert 1";
29 
30  SignAndSave(alert, "test/alertTests");
31 
32  alert.setSubVer.insert(std::string("/Satoshi:0.1.0/"));
33  alert.strStatusBar = "Alert 1 for Satoshi 0.1.0";
34  SignAndSave(alert, "test/alertTests");
35 
36  alert.setSubVer.insert(std::string("/Satoshi:0.2.0/"));
37  alert.strStatusBar = "Alert 1 for Satoshi 0.1.0, 0.2.0";
38  SignAndSave(alert, "test/alertTests");
39 
40  alert.setSubVer.clear();
41  ++alert.nID;
42  alert.nCancel = 1;
43  alert.nPriority = 100;
44  alert.strStatusBar = "Alert 2, cancels 1";
45  SignAndSave(alert, "test/alertTests");
46 
47  alert.nExpiration += 60;
48  ++alert.nID;
49  SignAndSave(alert, "test/alertTests");
50 
51  ++alert.nID;
52  alert.nMinVer = 11;
53  alert.nMaxVer = 22;
54  SignAndSave(alert, "test/alertTests");
55 
56  ++alert.nID;
57  alert.strStatusBar = "Alert 2 for Satoshi 0.1.0";
58  alert.setSubVer.insert(std::string("/Satoshi:0.1.0/"));
59  SignAndSave(alert, "test/alertTests");
60 
61  ++alert.nID;
62  alert.nMinVer = 0;
63  alert.nMaxVer = 999999;
64  alert.strStatusBar = "Evil Alert'; /bin/ls; echo '";
65  alert.setSubVer.clear();
66  SignAndSave(alert, "test/alertTests");
67 }
68 #endif
69 
70 struct ReadAlerts
71 {
73  {
74  std::string filename("alertTests");
75  namespace fs = boost::filesystem;
76  fs::path testFile = fs::current_path() / "test" / "data" / filename;
77 #ifdef TEST_DATA_DIR
78  if (!fs::exists(testFile))
79  {
80  testFile = fs::path(BOOST_PP_STRINGIZE(TEST_DATA_DIR)) / filename;
81  }
82 #endif
83  FILE* fp = fopen(testFile.string().c_str(), "rb");
84  if (!fp) return;
85 
86 
87  CAutoFile filein = CAutoFile(fp, SER_DISK, CLIENT_VERSION);
88  if (!filein) return;
89 
90  try {
91  while (!feof(filein))
92  {
93  CAlert alert;
94  filein >> alert;
95  alerts.push_back(alert);
96  }
97  }
98  catch (std::exception) { }
99  }
101 
102  static std::vector<std::string> read_lines(boost::filesystem::path filepath)
103  {
104  std::vector<std::string> result;
105 
106  std::ifstream f(filepath.string().c_str());
107  std::string line;
108  while (std::getline(f,line))
109  result.push_back(line);
110 
111  return result;
112  }
113 
114  std::vector<CAlert> alerts;
115 };
116 
117 BOOST_FIXTURE_TEST_SUITE(Alert_tests, ReadAlerts)
118 
119 
120 BOOST_AUTO_TEST_CASE(AlertApplies)
121 {
122  SetMockTime(11);
123 
124  BOOST_FOREACH(const CAlert& alert, alerts)
125  {
126  BOOST_CHECK(alert.CheckSignature());
127  }
128  // Matches:
129  BOOST_CHECK(alerts[0].AppliesTo(1, ""));
130  BOOST_CHECK(alerts[0].AppliesTo(70001, ""));
131  BOOST_CHECK(alerts[0].AppliesTo(1, "/Satoshi:11.11.11/"));
132 
133  BOOST_CHECK(alerts[1].AppliesTo(1, "/Satoshi:0.1.0/"));
134  BOOST_CHECK(alerts[1].AppliesTo(70001, "/Satoshi:0.1.0/"));
135 
136  BOOST_CHECK(alerts[2].AppliesTo(1, "/Satoshi:0.1.0/"));
137  BOOST_CHECK(alerts[2].AppliesTo(1, "/Satoshi:0.2.0/"));
138 
139  // Don't match:
140  BOOST_CHECK(!alerts[0].AppliesTo(-1, ""));
141  BOOST_CHECK(!alerts[0].AppliesTo(70002, ""));
142 
143  BOOST_CHECK(!alerts[1].AppliesTo(1, ""));
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(1, "Satoshi:0.1.0/"));
147  BOOST_CHECK(!alerts[1].AppliesTo(-1, "/Satoshi:0.1.0/"));
148  BOOST_CHECK(!alerts[1].AppliesTo(70002, "/Satoshi:0.1.0/"));
149  BOOST_CHECK(!alerts[1].AppliesTo(1, "/Satoshi:0.2.0/"));
150 
151  BOOST_CHECK(!alerts[2].AppliesTo(1, "/Satoshi:0.3.0/"));
152 
153  SetMockTime(0);
154 }
155 
156 
157 // This uses sh 'echo' to test the -alertnotify function, writing to a
158 // /tmp file. So skip it on Windows:
159 #ifndef WIN32
161 {
162  SetMockTime(11);
163 
164  boost::filesystem::path temp = GetTempPath() / "alertnotify.txt";
165  boost::filesystem::remove(temp);
166 
167  mapArgs["-alertnotify"] = std::string("echo %s >> ") + temp.string();
168 
169  BOOST_FOREACH(CAlert alert, alerts)
170  alert.ProcessAlert(false);
171 
172  std::vector<std::string> r = read_lines(temp);
173  BOOST_CHECK_EQUAL(r.size(), 1u);
174  BOOST_CHECK_EQUAL(r[0], "Evil Alert; /bin/ls; echo "); // single-quotes should be removed
175 
176  boost::filesystem::remove(temp);
177 
178  SetMockTime(0);
179 }
180 #endif
181 
182 BOOST_AUTO_TEST_SUITE_END()
std::set< std::string > setSubVer
Definition: alert.h:34
int64 nExpiration
Definition: alert.h:28
boost::filesystem::path GetTempPath()
Definition: util.cpp:1438
bool ProcessAlert(bool fThread=true)
Definition: alert.cpp:169
std::string strStatusBar
Definition: alert.h:39
int64 nRelayUntil
Definition: alert.h:27
void SetMockTime(int64 nMockTimeIn)
Definition: util.cpp:1305
An alert is a combination of a serialized CUnsignedAlert and a signature.
Definition: alert.h:68
BOOST_AUTO_TEST_CASE(AlertApplies)
int nMaxVer
Definition: alert.h:33
static std::vector< std::string > read_lines(boost::filesystem::path filepath)
int nMinVer
Definition: alert.h:32
bool CheckSignature() const
Definition: alert.cpp:145
std::string strComment
Definition: alert.h:38
std::vector< CAlert > alerts
int nCancel
Definition: alert.h:30
RAII wrapper for FILE*.
Definition: serialize.h:1108
map< string, string > mapArgs
Definition: util.cpp:71
int nPriority
Definition: alert.h:35