2017-04-24 23:06:29 -07:00
|
|
|
#include <gmock/gmock.h>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2018-01-22 16:29:08 -08:00
|
|
|
#include "chainparams.h"
|
2017-04-24 23:06:29 -07:00
|
|
|
#include "clientversion.h"
|
|
|
|
#include "deprecation.h"
|
2017-03-01 07:54:22 -08:00
|
|
|
#include "fs.h"
|
2017-04-24 23:06:29 -07:00
|
|
|
#include "init.h"
|
|
|
|
#include "ui_interface.h"
|
2018-10-22 15:51:11 -07:00
|
|
|
#include "util/system.h"
|
|
|
|
#include "util/strencodings.h"
|
2018-01-22 16:29:08 -08:00
|
|
|
|
|
|
|
#include <fstream>
|
2017-04-24 23:06:29 -07:00
|
|
|
|
2020-10-01 04:09:02 -07:00
|
|
|
using namespace boost::placeholders;
|
2017-04-24 23:06:29 -07:00
|
|
|
using ::testing::StrictMock;
|
|
|
|
|
|
|
|
static const std::string CLIENT_VERSION_STR = FormatVersion(CLIENT_VERSION);
|
|
|
|
extern std::atomic<bool> fRequestShutdown;
|
|
|
|
|
|
|
|
class MockUIInterface {
|
|
|
|
public:
|
|
|
|
MOCK_METHOD3(ThreadSafeMessageBox, bool(const std::string& message,
|
|
|
|
const std::string& caption,
|
|
|
|
unsigned int style));
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool ThreadSafeMessageBox(MockUIInterface *mock,
|
|
|
|
const std::string& message,
|
|
|
|
const std::string& caption,
|
|
|
|
unsigned int style)
|
|
|
|
{
|
2017-12-21 01:35:16 -08:00
|
|
|
return mock->ThreadSafeMessageBox(message, caption, style);
|
2017-04-24 23:06:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class DeprecationTest : public ::testing::Test {
|
|
|
|
protected:
|
2020-02-01 14:58:21 -08:00
|
|
|
void SetUp() override {
|
2017-04-24 23:06:29 -07:00
|
|
|
uiInterface.ThreadSafeMessageBox.disconnect_all_slots();
|
|
|
|
uiInterface.ThreadSafeMessageBox.connect(boost::bind(ThreadSafeMessageBox, &mock_, _1, _2, _3));
|
2018-03-07 08:33:55 -08:00
|
|
|
SelectParams(CBaseChainParams::MAIN);
|
2023-03-09 09:30:04 -08:00
|
|
|
|
2017-04-24 23:06:29 -07:00
|
|
|
}
|
|
|
|
|
2020-02-01 14:58:21 -08:00
|
|
|
void TearDown() override {
|
2017-04-24 23:06:29 -07:00
|
|
|
fRequestShutdown = false;
|
2018-03-07 08:33:55 -08:00
|
|
|
mapArgs.clear();
|
2017-04-24 23:06:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
StrictMock<MockUIInterface> mock_;
|
2018-01-22 16:29:08 -08:00
|
|
|
|
2017-03-01 08:05:50 -08:00
|
|
|
static std::vector<std::string> read_lines(fs::path filepath) {
|
2018-01-22 16:29:08 -08:00
|
|
|
std::vector<std::string> result;
|
|
|
|
|
|
|
|
std::ifstream f(filepath.string().c_str());
|
|
|
|
std::string line;
|
|
|
|
while (std::getline(f,line)) {
|
|
|
|
result.push_back(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2017-04-24 23:06:29 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(DeprecationTest, NonDeprecatedNodeKeepsRunning) {
|
|
|
|
EXPECT_FALSE(ShutdownRequested());
|
2023-03-09 09:30:04 -08:00
|
|
|
EnforceNodeDeprecation(Params(), DEPRECATION_HEIGHT - DEPRECATION_WARN_LIMIT - 1);
|
2017-04-24 23:06:29 -07:00
|
|
|
EXPECT_FALSE(ShutdownRequested());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DeprecationTest, NodeNearDeprecationIsWarned) {
|
|
|
|
EXPECT_FALSE(ShutdownRequested());
|
|
|
|
EXPECT_CALL(mock_, ThreadSafeMessageBox(::testing::_, "", CClientUIInterface::MSG_WARNING));
|
2023-03-09 09:30:04 -08:00
|
|
|
EnforceNodeDeprecation(Params(), DEPRECATION_HEIGHT - DEPRECATION_WARN_LIMIT);
|
2017-04-24 23:06:29 -07:00
|
|
|
EXPECT_FALSE(ShutdownRequested());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DeprecationTest, NodeNearDeprecationWarningIsNotDuplicated) {
|
|
|
|
EXPECT_FALSE(ShutdownRequested());
|
2023-03-09 09:30:04 -08:00
|
|
|
EnforceNodeDeprecation(Params(), DEPRECATION_HEIGHT - DEPRECATION_WARN_LIMIT + 1);
|
2017-04-24 23:06:29 -07:00
|
|
|
EXPECT_FALSE(ShutdownRequested());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DeprecationTest, NodeNearDeprecationWarningIsRepeatedOnStartup) {
|
|
|
|
EXPECT_FALSE(ShutdownRequested());
|
|
|
|
EXPECT_CALL(mock_, ThreadSafeMessageBox(::testing::_, "", CClientUIInterface::MSG_WARNING));
|
2023-03-09 09:30:04 -08:00
|
|
|
EnforceNodeDeprecation(Params(), DEPRECATION_HEIGHT - DEPRECATION_WARN_LIMIT + 1, true);
|
2017-04-24 23:06:29 -07:00
|
|
|
EXPECT_FALSE(ShutdownRequested());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DeprecationTest, DeprecatedNodeShutsDown) {
|
|
|
|
EXPECT_FALSE(ShutdownRequested());
|
|
|
|
EXPECT_CALL(mock_, ThreadSafeMessageBox(::testing::_, "", CClientUIInterface::MSG_ERROR));
|
2023-03-09 09:30:04 -08:00
|
|
|
EnforceNodeDeprecation(Params(), DEPRECATION_HEIGHT);
|
2017-04-24 23:06:29 -07:00
|
|
|
EXPECT_TRUE(ShutdownRequested());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DeprecationTest, DeprecatedNodeErrorIsNotDuplicated) {
|
|
|
|
EXPECT_FALSE(ShutdownRequested());
|
2023-03-09 09:30:04 -08:00
|
|
|
EnforceNodeDeprecation(Params(), DEPRECATION_HEIGHT + 1);
|
2017-04-24 23:06:29 -07:00
|
|
|
EXPECT_TRUE(ShutdownRequested());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DeprecationTest, DeprecatedNodeErrorIsRepeatedOnStartup) {
|
|
|
|
EXPECT_FALSE(ShutdownRequested());
|
|
|
|
EXPECT_CALL(mock_, ThreadSafeMessageBox(::testing::_, "", CClientUIInterface::MSG_ERROR));
|
2023-03-09 09:30:04 -08:00
|
|
|
EnforceNodeDeprecation(Params(), DEPRECATION_HEIGHT + 1, true);
|
2017-04-24 23:06:29 -07:00
|
|
|
EXPECT_TRUE(ShutdownRequested());
|
|
|
|
}
|
|
|
|
|
2018-03-07 08:33:55 -08:00
|
|
|
TEST_F(DeprecationTest, DeprecatedNodeIgnoredOnRegtest) {
|
|
|
|
SelectParams(CBaseChainParams::REGTEST);
|
|
|
|
EXPECT_FALSE(ShutdownRequested());
|
2023-03-09 09:30:04 -08:00
|
|
|
EnforceNodeDeprecation(Params(), DEPRECATION_HEIGHT+1);
|
2018-03-07 08:33:55 -08:00
|
|
|
EXPECT_FALSE(ShutdownRequested());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DeprecationTest, DeprecatedNodeIgnoredOnTestnet) {
|
|
|
|
SelectParams(CBaseChainParams::TESTNET);
|
|
|
|
EXPECT_FALSE(ShutdownRequested());
|
2023-03-09 09:30:04 -08:00
|
|
|
EnforceNodeDeprecation(Params(), DEPRECATION_HEIGHT+1);
|
2018-03-07 08:33:55 -08:00
|
|
|
EXPECT_FALSE(ShutdownRequested());
|
2018-01-22 16:29:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DeprecationTest, AlertNotify) {
|
2020-10-25 19:02:18 -07:00
|
|
|
fs::path temp = fs::temp_directory_path() /
|
2017-03-01 08:05:50 -08:00
|
|
|
fs::unique_path("alertnotify-%%%%.txt");
|
2018-01-22 16:29:08 -08:00
|
|
|
|
|
|
|
mapArgs["-alertnotify"] = std::string("echo %s >> ") + temp.string();
|
|
|
|
|
|
|
|
EXPECT_CALL(mock_, ThreadSafeMessageBox(::testing::_, "", CClientUIInterface::MSG_WARNING));
|
2023-03-09 09:30:04 -08:00
|
|
|
EnforceNodeDeprecation(Params(), DEPRECATION_HEIGHT - DEPRECATION_WARN_LIMIT, false, false);
|
2018-01-22 16:29:08 -08:00
|
|
|
|
|
|
|
std::vector<std::string> r = read_lines(temp);
|
|
|
|
EXPECT_EQ(r.size(), 1u);
|
|
|
|
|
|
|
|
// -alertnotify restricts the message to safe characters.
|
2018-04-03 05:25:44 -07:00
|
|
|
auto expectedMsg = strprintf(
|
|
|
|
"This version will be deprecated at block height %d, and will automatically shut down. You should upgrade to the latest version of Zcash.",
|
|
|
|
DEPRECATION_HEIGHT);
|
2018-01-22 16:29:08 -08:00
|
|
|
|
|
|
|
// Windows built-in echo semantics are different than posixy shells. Quotes and
|
|
|
|
// whitespace are printed literally.
|
|
|
|
#ifndef WIN32
|
|
|
|
EXPECT_EQ(r[0], expectedMsg);
|
|
|
|
#else
|
|
|
|
EXPECT_EQ(r[0], strprintf("'%s' ", expectedMsg));
|
|
|
|
#endif
|
2017-03-01 08:05:50 -08:00
|
|
|
fs::remove(temp);
|
2018-01-22 16:29:08 -08:00
|
|
|
}
|