Skip to content

Commit fa739d4

Browse files
author
MarcoFalke
committed
qa: Add wallet_encryption error tests
1 parent 327129f commit fa739d4

File tree

2 files changed

+18
-31
lines changed

2 files changed

+18
-31
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,21 +2009,13 @@ static UniValue walletpassphrase(const JSONRPCRequest& request)
20092009
nSleepTime = MAX_SLEEP_TIME;
20102010
}
20112011

2012-
if (strWalletPass.length() > 0)
2013-
{
2014-
if (!pwallet->Unlock(strWalletPass)) {
2015-
throw JSONRPCError(RPC_WALLET_PASSPHRASE_INCORRECT, "Error: The wallet passphrase entered was incorrect.");
2016-
}
2012+
if (strWalletPass.empty()) {
2013+
throw JSONRPCError(RPC_INVALID_PARAMETER, "passphrase can not be empty");
2014+
}
2015+
2016+
if (!pwallet->Unlock(strWalletPass)) {
2017+
throw JSONRPCError(RPC_WALLET_PASSPHRASE_INCORRECT, "Error: The wallet passphrase entered was incorrect.");
20172018
}
2018-
else
2019-
throw std::runtime_error(
2020-
RPCHelpMan{"walletpassphrase",
2021-
"Stores the wallet decryption key in memory for <timeout> seconds.",
2022-
{
2023-
{"passphrase", RPCArg::Type::STR, false},
2024-
{"timeout", RPCArg::Type::NUM, false},
2025-
}}
2026-
.ToString());
20272019

20282020
pwallet->TopUpKeyPool();
20292021

@@ -2089,15 +2081,9 @@ static UniValue walletpassphrasechange(const JSONRPCRequest& request)
20892081
strNewWalletPass.reserve(100);
20902082
strNewWalletPass = request.params[1].get_str().c_str();
20912083

2092-
if (strOldWalletPass.length() < 1 || strNewWalletPass.length() < 1)
2093-
throw std::runtime_error(
2094-
RPCHelpMan{"walletpassphrasechange",
2095-
"Changes the wallet passphrase from <oldpassphrase> to <newpassphrase>.",
2096-
{
2097-
{"oldpassphrase", RPCArg::Type::STR, false},
2098-
{"newpassphrase", RPCArg::Type::STR, false},
2099-
}}
2100-
.ToString());
2084+
if (strOldWalletPass.empty() || strNewWalletPass.empty()) {
2085+
throw JSONRPCError(RPC_INVALID_PARAMETER, "passphrase can not be empty");
2086+
}
21012087

21022088
if (!pwallet->ChangeWalletPassphrase(strOldWalletPass, strNewWalletPass)) {
21032089
throw JSONRPCError(RPC_WALLET_PASSPHRASE_INCORRECT, "Error: The wallet passphrase entered was incorrect.");
@@ -2200,14 +2186,9 @@ static UniValue encryptwallet(const JSONRPCRequest& request)
22002186
strWalletPass.reserve(100);
22012187
strWalletPass = request.params[0].get_str().c_str();
22022188

2203-
if (strWalletPass.length() < 1)
2204-
throw std::runtime_error(
2205-
RPCHelpMan{"encryptwallet",
2206-
"Encrypts the wallet with <passphrase>.",
2207-
{
2208-
{"passphrase", RPCArg::Type::STR, false},
2209-
}}
2210-
.ToString());
2189+
if (strWalletPass.empty()) {
2190+
throw JSONRPCError(RPC_INVALID_PARAMETER, "passphrase can not be empty");
2191+
}
22112192

22122193
if (!pwallet->EncryptWallet(strWalletPass)) {
22132194
throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, "Error: Failed to encrypt the wallet.");

test/functional/wallet_encryption.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,18 @@ def run_test(self):
3131
privkey = self.nodes[0].dumpprivkey(address)
3232
assert_equal(privkey[:1], "c")
3333
assert_equal(len(privkey), 52)
34+
assert_raises_rpc_error(-15, "Error: running with an unencrypted wallet, but walletpassphrase was called", self.nodes[0].walletpassphrase, 'ff', 1)
35+
assert_raises_rpc_error(-15, "Error: running with an unencrypted wallet, but walletpassphrasechange was called.", self.nodes[0].walletpassphrasechange, 'ff', 'ff')
3436

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

3841
# Test that the wallet is encrypted
3942
assert_raises_rpc_error(-13, "Please enter the wallet passphrase with walletpassphrase first", self.nodes[0].dumpprivkey, address)
43+
assert_raises_rpc_error(-15, "Error: running with an encrypted wallet, but encryptwallet was called.", self.nodes[0].encryptwallet, 'ff')
44+
assert_raises_rpc_error(-8, "passphrase can not be empty", self.nodes[0].walletpassphrase, '', 1)
45+
assert_raises_rpc_error(-8, "passphrase can not be empty", self.nodes[0].walletpassphrasechange, '', 'ff')
4046

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

0 commit comments

Comments
 (0)