Skip to content

Commit

Permalink
Merge branch 'pr14813' into 'master'
Browse files Browse the repository at this point in the history
qa: Add wallet_encryption error tests

See merge request bitcoin-cash-node/bitcoin-cash-node!865

Port of [PR14813](bitcoin/bitcoin#14813)

> The errors for empty passphrases are the help text of the RPC call, which is not very specific. Replace that with proper RPC errors and test them.

Backport by Dagur <dagurval@pvv.ntnu.no>

## Test plan

RPC test improved. Run `ninja check-functional`.
  • Loading branch information
ftrader committed Nov 19, 2020
2 parents ef21184 + a7f8f5e commit 9fb1956
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 32 deletions.
42 changes: 10 additions & 32 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2385,21 +2385,12 @@ static UniValue walletpassphrase(const Config &config,
nSleepTime = MAX_SLEEP_TIME;
}

if (strWalletPass.length() > 0) {
if (!pwallet->Unlock(strWalletPass)) {
throw JSONRPCError(
RPC_WALLET_PASSPHRASE_INCORRECT,
"Error: The wallet passphrase entered was incorrect.");
}
} else {
throw std::runtime_error(
RPCHelpMan{"walletpassphrase",
"Stores the wallet decryption key in memory for <timeout> seconds.",
{
{"passphrase", RPCArg::Type::STR, false},
{"timeout", RPCArg::Type::NUM, false},
}}
.ToString());
if (strWalletPass.empty()) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "passphrase can not be empty");
}

if (!pwallet->Unlock(strWalletPass)) {
throw JSONRPCError(RPC_WALLET_PASSPHRASE_INCORRECT, "Error: The wallet passphrase entered was incorrect.");
}

pwallet->TopUpKeyPool();
Expand Down Expand Up @@ -2476,15 +2467,8 @@ static UniValue walletpassphrasechange(const Config &config,
strNewWalletPass.reserve(100);
strNewWalletPass = request.params[1].get_str().c_str();

if (strOldWalletPass.length() < 1 || strNewWalletPass.length() < 1) {
throw std::runtime_error(
RPCHelpMan{"walletpassphrasechange",
"Changes the wallet passphrase from <oldpassphrase> to <newpassphrase>.",
{
{"oldpassphrase", RPCArg::Type::STR, false},
{"newpassphrase", RPCArg::Type::STR, false},
}}
.ToString());
if (strOldWalletPass.empty() || strNewWalletPass.empty()) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "passphrase can not be empty");
}

if (!pwallet->ChangeWalletPassphrase(strOldWalletPass, strNewWalletPass)) {
Expand Down Expand Up @@ -2602,14 +2586,8 @@ static UniValue encryptwallet(const Config &config,
strWalletPass.reserve(100);
strWalletPass = request.params[0].get_str().c_str();

if (strWalletPass.length() < 1) {
throw std::runtime_error(
RPCHelpMan{"encryptwallet",
"Encrypts the wallet with <passphrase>.",
{
{"passphrase", RPCArg::Type::STR, false},
}}
.ToString());
if (strWalletPass.empty()) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "passphrase can not be empty");
}

if (!pwallet->EncryptWallet(strWalletPass)) {
Expand Down
5 changes: 5 additions & 0 deletions test/functional/wallet_encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ def run_test(self):
privkey = self.nodes[0].dumpprivkey(address)
assert_equal(privkey[:1], "c")
assert_equal(len(privkey), 52)
assert_raises_rpc_error(-15, "Error: running with an unencrypted wallet, but walletpassphrase was called", self.nodes[0].walletpassphrase, 'ff', 1)
assert_raises_rpc_error(-15, "Error: running with an unencrypted wallet, but walletpassphrasechange was called.", self.nodes[0].walletpassphrasechange, 'ff', 'ff')

# Encrypt the wallet
assert_raises_rpc_error(-8, "passphrase can not be empty", self.nodes[0].encryptwallet, '')
self.nodes[0].encryptwallet(passphrase)

# Check the encrypted wallet is marked as locked on initialization
Expand All @@ -44,6 +47,8 @@ def run_test(self):
assert_raises_rpc_error(
-13, "Please enter the wallet passphrase with walletpassphrase first",
self.nodes[0].dumpprivkey, address)
assert_raises_rpc_error(-8, "passphrase can not be empty", self.nodes[0].walletpassphrase, '', 1)
assert_raises_rpc_error(-8, "passphrase can not be empty", self.nodes[0].walletpassphrasechange, '', 'ff')

# Check that walletpassphrase works
self.nodes[0].walletpassphrase(passphrase, 2)
Expand Down

0 comments on commit 9fb1956

Please sign in to comment.