Skip to content

Commit 684c1b5

Browse files
committed
test: rewrite rpc_wallet_encrypted_wallet_sapzkeys unit test as functional test
As it requires a complete real wallet, and not a mocked one, in order to perform the encryption checks, it's by definition a functional test.
1 parent 2f8a034 commit 684c1b5

File tree

3 files changed

+64
-71
lines changed

3 files changed

+64
-71
lines changed

src/test/librust/sapling_rpc_wallet_tests.cpp

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -498,77 +498,6 @@ BOOST_AUTO_TEST_CASE(rpc_shieldsendmany_taddr_to_sapling)
498498
vpwallets.erase(vpwallets.begin());
499499
}
500500

501-
struct RealWalletTestingSetup : public WalletTestingSetupBase
502-
{
503-
RealWalletTestingSetup() : WalletTestingSetupBase(CBaseChainParams::MAIN,
504-
"test_wallet",
505-
WalletDatabase::Create(fs::absolute("test_wallet", GetWalletDir()))) {};
506-
};
507-
508-
BOOST_FIXTURE_TEST_CASE(rpc_wallet_encrypted_wallet_sapzkeys, RealWalletTestingSetup)
509-
{
510-
UniValue retValue;
511-
int n = 100;
512-
513-
{
514-
LOCK(m_wallet.cs_wallet);
515-
m_wallet.SetMinVersion(FEATURE_SAPLING);
516-
m_wallet.SetupSPKM(false);
517-
}
518-
vpwallets.insert(vpwallets.begin(), &m_wallet);
519-
520-
// wallet should currently be empty
521-
std::set<libzcash::SaplingPaymentAddress> addrs;
522-
m_wallet.GetSaplingPaymentAddresses(addrs);
523-
BOOST_CHECK(addrs.empty());
524-
525-
// create keys
526-
for (int i = 0; i < n; i++) {
527-
CallRPC("getnewshieldaddress");
528-
}
529-
530-
// Verify we can list the keys imported
531-
BOOST_CHECK_NO_THROW(retValue = CallRPC("listshieldaddresses"));
532-
UniValue arr = retValue.get_array();
533-
BOOST_CHECK((int) arr.size() == n);
534-
535-
// Verify that the wallet encryption RPC is disabled
536-
// TODO: We don't have the experimental mode to disable the encryptwallet disable.
537-
//BOOST_CHECK_THROW(CallRPC("encryptwallet passphrase"), std::runtime_error);
538-
539-
// Encrypt the wallet (we can't call RPC encryptwallet as that shuts down node)
540-
SecureString strWalletPass;
541-
strWalletPass.reserve(100);
542-
strWalletPass = "hello";
543-
544-
PushCurrentDirectory push_dir(gArgs.GetArg("-datadir","/tmp/thisshouldnothappen"));
545-
BOOST_CHECK(m_wallet.EncryptWallet(strWalletPass));
546-
BOOST_CHECK(m_wallet.IsCrypted());
547-
548-
// Verify we can still list the keys imported
549-
BOOST_CHECK_NO_THROW(retValue = CallRPC("listshieldaddresses"));
550-
arr = retValue.get_array();
551-
BOOST_CHECK((int) arr.size() == n);
552-
553-
// Try to add a new key, but we can't as the wallet is locked
554-
BOOST_CHECK_THROW(CallRPC("getnewshieldaddress"), std::runtime_error);
555-
556-
// We can't call RPC walletpassphrase as that invokes RPCRunLater which breaks tests.
557-
// So we manually unlock.
558-
BOOST_CHECK(m_wallet.Unlock(strWalletPass));
559-
BOOST_CHECK(m_wallet.IsCrypted());
560-
561-
// Now add a key
562-
BOOST_CHECK_NO_THROW(CallRPC("getnewshieldaddress"));
563-
564-
// Verify the key has been added
565-
BOOST_CHECK_NO_THROW(retValue = CallRPC("listshieldaddresses"));
566-
arr = retValue.get_array();
567-
BOOST_CHECK((int) arr.size() == n+1);
568-
569-
vpwallets.erase(vpwallets.begin());
570-
}
571-
572501
BOOST_AUTO_TEST_CASE(rpc_listshieldunspent_parameters)
573502
{
574503
{
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2021 The PIVX developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or https://www.opensource.org/licenses/mit-license.php .
5+
6+
from test_framework.test_framework import PivxTestFramework
7+
from test_framework.util import (
8+
assert_equal,
9+
assert_raises_rpc_error,
10+
assert_true
11+
)
12+
13+
# Test encrypted wallet behaviour with Sapling addresses
14+
class WalletSaplingTest(PivxTestFramework):
15+
16+
def set_test_params(self):
17+
self.num_nodes = 1
18+
self.setup_clean_chain = True
19+
20+
def run_test(self):
21+
node = self.nodes[0]
22+
node.generate(1)
23+
24+
# wallet should currently be empty
25+
assert_equal(len(node.listshieldaddresses()), 0)
26+
# create 100 keys
27+
keys_to_generate = 100
28+
for _ in range (keys_to_generate):
29+
node.getnewshieldaddress()
30+
# Verify we can list the created addresses
31+
addr_list = node.listshieldaddresses()
32+
assert_equal(len(addr_list), keys_to_generate)
33+
34+
password = "hello"
35+
node.encryptwallet(password)
36+
# assert that the wallet is encrypted
37+
assert_raises_rpc_error(-15, "Error: running with an encrypted wallet, but encryptwallet was called.", node.encryptwallet, 'ff')
38+
39+
# now verify addresses again
40+
addr_list_post_encryption = node.listshieldaddresses()
41+
assert_equal(len(addr_list_post_encryption), keys_to_generate)
42+
assert_equal(addr_list, addr_list_post_encryption)
43+
44+
# try to add a new key, but we can't as the wallet is locked
45+
assert_raises_rpc_error(-13, "Error: Please enter the wallet passphrase with walletpassphrase first.", node.getnewshieldaddress)
46+
47+
# now unlock the wallet and try to generate the key again
48+
node.walletpassphrase(password, 12000)
49+
new_addr = node.getnewshieldaddress()
50+
assert(new_addr is not None)
51+
52+
# and verify that the key has been added
53+
found = False
54+
addr_list_post_encryption = node.listshieldaddresses()
55+
assert_equal(len(addr_list_post_encryption), keys_to_generate + 1)
56+
for addr in addr_list_post_encryption:
57+
if addr == new_addr:
58+
found = True
59+
assert_true(found, "error: shield address not found in encrypted wallet")
60+
61+
if __name__ == '__main__':
62+
WalletSaplingTest().main()

test/functional/test_runner.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
'rpc_deprecated.py', # ~ 80 sec
116116
'interface_bitcoin_cli.py', # ~ 80 sec
117117
'mempool_packages.py', # ~ 63 sec
118+
'sapling_wallet_encryption.py',
118119

119120
# vv Tests less than 60s vv
120121
'rpc_users.py',
@@ -237,6 +238,7 @@
237238
'wallet_importmulti.py',
238239
'wallet_import_rescan.py',
239240
'wallet_multiwallet.py',
241+
'sapling_wallet_encryption.py'
240242
]
241243

242244
# Place the lists with the longest tests (on average) first

0 commit comments

Comments
 (0)