Skip to content

Commit

Permalink
Merge #14813: qa: Add wallet_encryption error tests
Browse files Browse the repository at this point in the history
fa739d4 qa: Add wallet_encryption error tests (MarcoFalke)

Pull request description:

  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.

Tree-SHA512: 3137e0f8f2e42a1f8ab1eeb57c99052557725f6f85139ff48c24acc8f3cf4087802de5216f3ce97375b291d21bddb7cd1379a6f280166136a306a0c9663bbd42
  • Loading branch information
MarcoFalke committed Nov 27, 2018
2 parents d491030 + fa739d4 commit 8c119b2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 31 deletions.
43 changes: 12 additions & 31 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2009,21 +2009,13 @@ static UniValue walletpassphrase(const JSONRPCRequest& request)
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.");
}
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.");
}
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());

pwallet->TopUpKeyPool();

Expand Down Expand Up @@ -2089,15 +2081,9 @@ static UniValue walletpassphrasechange(const JSONRPCRequest& request)
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)) {
throw JSONRPCError(RPC_WALLET_PASSPHRASE_INCORRECT, "Error: The wallet passphrase entered was incorrect.");
Expand Down Expand Up @@ -2200,14 +2186,9 @@ static UniValue encryptwallet(const JSONRPCRequest& request)
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)) {
throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, "Error: Failed to encrypt the wallet.");
Expand Down
6 changes: 6 additions & 0 deletions test/functional/wallet_encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@ 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)

# Test that the wallet is encrypted
assert_raises_rpc_error(-13, "Please enter the wallet passphrase with walletpassphrase first", self.nodes[0].dumpprivkey, address)
assert_raises_rpc_error(-15, "Error: running with an encrypted wallet, but encryptwallet was called.", self.nodes[0].encryptwallet, 'ff')
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 8c119b2

Please sign in to comment.