You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

alert_tests.cpp 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright (c) 2013 The Bitcoin Core developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. //
  5. // Unit tests for alert system
  6. //
  7. #include "alert.h"
  8. #include "clientversion.h"
  9. #include "data/alertTests.raw.h"
  10. #include "chainparams.h"
  11. #include "serialize.h"
  12. #include "streams.h"
  13. #include "util.h"
  14. #include "utilstrencodings.h"
  15. #include "test/test_bitcoin.h"
  16. #include <fstream>
  17. #include <boost/filesystem/operations.hpp>
  18. #include <boost/foreach.hpp>
  19. #include <boost/test/unit_test.hpp>
  20. #if 0
  21. //
  22. // alertTests contains 7 alerts, generated with this code:
  23. // (SignAndSave code not shown, alert signing key is secret)
  24. //
  25. {
  26. CAlert alert;
  27. alert.nRelayUntil = 60;
  28. alert.nExpiration = 24 * 60 * 60;
  29. alert.nID = 1;
  30. alert.nCancel = 0; // cancels previous messages up to this ID number
  31. alert.nMinVer = 0; // These versions are protocol versions
  32. alert.nMaxVer = 999001;
  33. alert.nPriority = 1;
  34. alert.strComment = "Alert comment";
  35. alert.strStatusBar = "Alert 1";
  36. SignAndSave(alert, "test/alertTests");
  37. alert.setSubVer.insert(std::string("/Satoshi:0.1.0/"));
  38. alert.strStatusBar = "Alert 1 for Satoshi 0.1.0";
  39. SignAndSave(alert, "test/alertTests");
  40. alert.setSubVer.insert(std::string("/Satoshi:0.2.0/"));
  41. alert.strStatusBar = "Alert 1 for Satoshi 0.1.0, 0.2.0";
  42. SignAndSave(alert, "test/alertTests");
  43. alert.setSubVer.clear();
  44. ++alert.nID;
  45. alert.nCancel = 1;
  46. alert.nPriority = 100;
  47. alert.strStatusBar = "Alert 2, cancels 1";
  48. SignAndSave(alert, "test/alertTests");
  49. alert.nExpiration += 60;
  50. ++alert.nID;
  51. SignAndSave(alert, "test/alertTests");
  52. ++alert.nID;
  53. alert.nMinVer = 11;
  54. alert.nMaxVer = 22;
  55. SignAndSave(alert, "test/alertTests");
  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. ++alert.nID;
  61. alert.nMinVer = 0;
  62. alert.nMaxVer = 999999;
  63. alert.strStatusBar = "Evil Alert'; /bin/ls; echo '";
  64. alert.setSubVer.clear();
  65. SignAndSave(alert, "test/alertTests");
  66. }
  67. #endif
  68. struct ReadAlerts : public TestingSetup
  69. {
  70. ReadAlerts()
  71. {
  72. std::vector<unsigned char> vch(alert_tests::alertTests, alert_tests::alertTests + sizeof(alert_tests::alertTests));
  73. CDataStream stream(vch, SER_DISK, CLIENT_VERSION);
  74. try {
  75. while (!stream.eof())
  76. {
  77. CAlert alert;
  78. stream >> alert;
  79. alerts.push_back(alert);
  80. }
  81. }
  82. catch (const std::exception&) { }
  83. }
  84. ~ReadAlerts() { }
  85. static std::vector<std::string> read_lines(boost::filesystem::path filepath)
  86. {
  87. std::vector<std::string> result;
  88. std::ifstream f(filepath.string().c_str());
  89. std::string line;
  90. while (std::getline(f,line))
  91. result.push_back(line);
  92. return result;
  93. }
  94. std::vector<CAlert> alerts;
  95. };
  96. BOOST_FIXTURE_TEST_SUITE(Alert_tests, ReadAlerts)
  97. BOOST_AUTO_TEST_CASE(AlertApplies)
  98. {
  99. SetMockTime(11);
  100. const std::vector<unsigned char>& alertKey = Params(CBaseChainParams::MAIN).AlertKey();
  101. BOOST_FOREACH(const CAlert& alert, alerts)
  102. {
  103. BOOST_CHECK(alert.CheckSignature(alertKey));
  104. }
  105. BOOST_CHECK(alerts.size() >= 3);
  106. // Matches:
  107. BOOST_CHECK(alerts[0].AppliesTo(1, ""));
  108. BOOST_CHECK(alerts[0].AppliesTo(999001, ""));
  109. BOOST_CHECK(alerts[0].AppliesTo(1, "/Satoshi:11.11.11/"));
  110. BOOST_CHECK(alerts[1].AppliesTo(1, "/Satoshi:0.1.0/"));
  111. BOOST_CHECK(alerts[1].AppliesTo(999001, "/Satoshi:0.1.0/"));
  112. BOOST_CHECK(alerts[2].AppliesTo(1, "/Satoshi:0.1.0/"));
  113. BOOST_CHECK(alerts[2].AppliesTo(1, "/Satoshi:0.2.0/"));
  114. // Don't match:
  115. BOOST_CHECK(!alerts[0].AppliesTo(-1, ""));
  116. BOOST_CHECK(!alerts[0].AppliesTo(999002, ""));
  117. BOOST_CHECK(!alerts[1].AppliesTo(1, ""));
  118. BOOST_CHECK(!alerts[1].AppliesTo(1, "Satoshi:0.1.0"));
  119. BOOST_CHECK(!alerts[1].AppliesTo(1, "/Satoshi:0.1.0"));
  120. BOOST_CHECK(!alerts[1].AppliesTo(1, "Satoshi:0.1.0/"));
  121. BOOST_CHECK(!alerts[1].AppliesTo(-1, "/Satoshi:0.1.0/"));
  122. BOOST_CHECK(!alerts[1].AppliesTo(999002, "/Satoshi:0.1.0/"));
  123. BOOST_CHECK(!alerts[1].AppliesTo(1, "/Satoshi:0.2.0/"));
  124. BOOST_CHECK(!alerts[2].AppliesTo(1, "/Satoshi:0.3.0/"));
  125. SetMockTime(0);
  126. }
  127. BOOST_AUTO_TEST_CASE(AlertNotify)
  128. {
  129. SetMockTime(11);
  130. const std::vector<unsigned char>& alertKey = Params(CBaseChainParams::MAIN).AlertKey();
  131. boost::filesystem::path temp = GetTempPath() / "alertnotify.txt";
  132. boost::filesystem::remove(temp);
  133. mapArgs["-alertnotify"] = std::string("echo %s >> ") + temp.string();
  134. BOOST_FOREACH(CAlert alert, alerts)
  135. alert.ProcessAlert(alertKey, false);
  136. std::vector<std::string> r = read_lines(temp);
  137. BOOST_CHECK_EQUAL(r.size(), 4u);
  138. // Windows built-in echo semantics are different than posixy shells. Quotes and
  139. // whitespace are printed literally.
  140. #ifndef WIN32
  141. BOOST_CHECK_EQUAL(r[0], "Alert 1");
  142. BOOST_CHECK_EQUAL(r[1], "Alert 2, cancels 1");
  143. BOOST_CHECK_EQUAL(r[2], "Alert 2, cancels 1");
  144. BOOST_CHECK_EQUAL(r[3], "Evil Alert; /bin/ls; echo "); // single-quotes should be removed
  145. #else
  146. BOOST_CHECK_EQUAL(r[0], "'Alert 1' ");
  147. BOOST_CHECK_EQUAL(r[1], "'Alert 2, cancels 1' ");
  148. BOOST_CHECK_EQUAL(r[2], "'Alert 2, cancels 1' ");
  149. BOOST_CHECK_EQUAL(r[3], "'Evil Alert; /bin/ls; echo ' ");
  150. #endif
  151. boost::filesystem::remove(temp);
  152. SetMockTime(0);
  153. }
  154. BOOST_AUTO_TEST_SUITE_END()